summaryrefslogtreecommitdiff
path: root/block/test-iosched.c
blob: 41ef689f1d5994534506e5cf6ef59ecb88085555 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
/* Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * The test scheduler allows to test the block device by dispatching
 * specific requests according to the test case and declare PASS/FAIL
 * according to the requests completion error code.
 * Each test is exposed via debugfs and can be triggered by writing to
 * the debugfs file.
 *
 */
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt"\n"

/* elevator test iosched */
#include <linux/blkdev.h>
#include <linux/elevator.h>
#include <linux/bio.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/debugfs.h>
#include <linux/test-iosched.h>
#include <linux/delay.h>
#include "blk.h"

#define MODULE_NAME "test-iosched"
#define WR_RD_START_REQ_ID 1234
#define UNIQUE_START_REQ_ID 5678
#define TIMEOUT_TIMER_MS 40000
#define TEST_MAX_TESTCASE_ROUNDS 15

static DEFINE_SPINLOCK(blk_dev_test_list_lock);
static LIST_HEAD(blk_dev_test_list);
static struct test_data *ptd;


/**
 * test_iosched_get_req_queue() - returns the request queue
 * served by the scheduler
 */
struct request_queue *test_iosched_get_req_queue(void)
{
	if (!ptd)
		return NULL;

	return ptd->req_q;
}
EXPORT_SYMBOL(test_iosched_get_req_queue);

/**
 * test_iosched_mark_test_completion() - Wakeup the debugfs
 * thread, waiting on the test completion
 */
void test_iosched_mark_test_completion(void)
{
	if (!ptd)
		return;

	pr_info("%s: mark test is completed, test_count=%d, ", __func__,
		     ptd->test_count);
	pr_info("%s: urgent_count=%d, reinsert_count=%d,", __func__,
		     ptd->urgent_count, ptd->reinsert_count);

	ptd->test_state = TEST_COMPLETED;
	wake_up(&ptd->wait_q);
}
EXPORT_SYMBOL(test_iosched_mark_test_completion);

/**
 *  check_test_completion() - Check if all the queued test
 *  requests were completed
 */
void check_test_completion(void)
{
	struct test_request *test_rq;

	if (!ptd)
		goto exit;

	list_for_each_entry(test_rq, &ptd->dispatched_queue, queuelist)
		if (!test_rq->req_completed)
			goto exit;

	if (!list_empty(&ptd->test_queue)
			|| !list_empty(&ptd->reinsert_queue)
			|| !list_empty(&ptd->urgent_queue)) {
		pr_info("%s: Test still not completed,", __func__);
		pr_info("%s: test_count=%d, reinsert_count=%d",
			     __func__, ptd->test_count, ptd->reinsert_count);
		pr_info("%s: dispatched_count=%d, urgent_count=%d",
			    __func__, ptd->dispatched_count, ptd->urgent_count);
		goto exit;
	}

	ptd->test_info.test_duration = ktime_sub(ktime_get(),
				ptd->test_info.test_duration);

	test_iosched_mark_test_completion();

exit:
	return;
}
EXPORT_SYMBOL(check_test_completion);

/*
 * A callback to be called per bio completion.
 * Frees the bio memory.
 */
static void end_test_bio(struct bio *bio, int err)
{
	if (err)
		clear_bit(BIO_UPTODATE, &bio->bi_flags);
	bio_put(bio);
}

/*
 * A callback to be called per request completion.
 * the request memory is not freed here, will be freed later after the test
 * results checking.
 */
static void end_test_req(struct request *rq, int err)
{
	struct test_request *test_rq;

	test_rq = (struct test_request *)rq->elv.priv[0];
	BUG_ON(!test_rq);

	pr_debug("%s: request %d completed, err=%d",
	       __func__, test_rq->req_id, err);

	test_rq->req_completed = true;
	test_rq->req_result = err;

	check_test_completion();
}

/**
 * test_iosched_add_unique_test_req() - Create and queue a non
 * read/write request (such as FLUSH/DISCRAD/SANITIZE).
 * @is_err_expcted:	A flag to indicate if this request
 *			should succeed or not
 * @req_unique:		The type of request to add
 * @start_sec:		start address of the first bio
 * @nr_sects:		number of sectors in the request
 * @end_req_io:		specific completion callback. When not
 *			set, the defaulcallback will be used
 */
int test_iosched_add_unique_test_req(int is_err_expcted,
			enum req_unique_type req_unique,
			int start_sec, int nr_sects, rq_end_io_fn *end_req_io)
{
	struct bio *bio;
	struct request *rq;
	int rw_flags;
	struct test_request *test_rq;

	if (!ptd)
		return -ENODEV;

	bio = bio_alloc(GFP_KERNEL, 0);
	if (!bio) {
		pr_err("%s: Failed to allocate a bio", __func__);
		return -ENODEV;
	}
	bio_get(bio);
	bio->bi_end_io = end_test_bio;

	switch (req_unique) {
	case REQ_UNIQUE_FLUSH:
		bio->bi_rw = WRITE_FLUSH;
		break;
	case REQ_UNIQUE_DISCARD:
		bio->bi_rw = REQ_WRITE | REQ_DISCARD;
		bio->bi_iter.bi_size = nr_sects << 9;
		bio->bi_iter.bi_sector = start_sec;
		break;
	default:
		pr_err("%s: Invalid request type %d", __func__,
			    req_unique);
		bio_put(bio);
		return -ENODEV;
	}

	rw_flags = bio_data_dir(bio);
	if (bio->bi_rw & REQ_SYNC)
		rw_flags |= REQ_SYNC;

	rq = blk_get_request(ptd->req_q, rw_flags, GFP_KERNEL);
	if (!rq) {
		pr_err("%s: Failed to allocate a request", __func__);
		bio_put(bio);
		return -ENODEV;
	}

	init_request_from_bio(rq, bio);
	if (end_req_io)
		rq->end_io = end_req_io;
	else
		rq->end_io = end_test_req;

	test_rq = kzalloc(sizeof(struct test_request), GFP_KERNEL);
	if (!test_rq) {
		pr_err("%s: Failed to allocate a test request", __func__);
		bio_put(bio);
		blk_put_request(rq);
		return -ENODEV;
	}
	test_rq->req_completed = false;
	test_rq->req_result = -EINVAL;
	test_rq->rq = rq;
	test_rq->is_err_expected = is_err_expcted;
	rq->elv.priv[0] = (void *)test_rq;
	test_rq->req_id = ptd->unique_next_req_id++;

	pr_debug(
		"%s: added request %d to the test requests list, type = %d",
		__func__, test_rq->req_id, req_unique);

	spin_lock_irq(ptd->req_q->queue_lock);
	list_add_tail(&test_rq->queuelist, &ptd->test_queue);
	ptd->test_count++;
	spin_unlock_irq(ptd->req_q->queue_lock);

	return 0;
}
EXPORT_SYMBOL(test_iosched_add_unique_test_req);

/*
 * Get a pattern to be filled in the request data buffer.
 * If the pattern used is (-1) the buffer will be filled with sequential
 * numbers
 */
static void fill_buf_with_pattern(int *buf, int num_bytes, int pattern)
{
	int i = 0;
	int num_of_dwords = num_bytes/sizeof(int);

	if (pattern == TEST_NO_PATTERN)
		return;

	/* num_bytes should be aligned to sizeof(int) */
	BUG_ON((num_bytes % sizeof(int)) != 0);

	if (pattern == TEST_PATTERN_SEQUENTIAL) {
		for (i = 0; i < num_of_dwords; i++)
			buf[i] = i;
	} else {
		for (i = 0; i < num_of_dwords; i++)
			buf[i] = pattern;
	}
}

/**
 * test_iosched_create_test_req() - Create a read/write request.
 * @is_err_expcted:	A flag to indicate if this request
 *			should succeed or not
 * @direction:		READ/WRITE
 * @start_sec:		start address of the first bio
 * @num_bios:		number of BIOs to be allocated for the
 *			request
 * @pattern:		A pattern, to be written into the write
 *			requests data buffer. In case of READ
 *			request, the given pattern is kept as
 *			the expected pattern. The expected
 *			pattern will be compared in the test
 *			check result function. If no comparisson
 *			is required, set pattern to
 *			TEST_NO_PATTERN.
 * @end_req_io:		specific completion callback. When not
 *			set,the default callback will be used
 *
 * This function allocates the test request and the block
 * request and calls blk_rq_map_kern which allocates the
 * required BIO. The allocated test request and the block
 * request memory is freed at the end of the test and the
 * allocated BIO memory is freed by end_test_bio.
 */
struct test_request *test_iosched_create_test_req(int is_err_expcted,
		      int direction, int start_sec,
		      int num_bios, int pattern, rq_end_io_fn *end_req_io)
{
	struct request *rq;
	struct test_request *test_rq;
	int rw_flags, buf_size;
	int ret = 0, i;
	unsigned int *bio_ptr = NULL;
	struct bio *bio = NULL;

	if (!ptd)
		return NULL;

	rw_flags = direction;

	rq = blk_get_request(ptd->req_q, rw_flags, GFP_KERNEL);
	if (!rq) {
		pr_err("%s: Failed to allocate a request", __func__);
		return NULL;
	}

	test_rq = kzalloc(sizeof(struct test_request), GFP_KERNEL);
	if (!test_rq) {
		pr_err("%s: Failed to allocate test request", __func__);
		blk_put_request(rq);
		return NULL;
	}

	buf_size = TEST_BIO_SIZE * num_bios;
	test_rq->bios_buffer = kzalloc(buf_size, GFP_KERNEL);
	if (!test_rq->bios_buffer) {
		pr_err("%s: Failed to allocate the data buf", __func__);
		goto err;
	}
	test_rq->buf_size = buf_size;

	if (direction == WRITE)
		fill_buf_with_pattern(test_rq->bios_buffer,
						   buf_size, pattern);
	test_rq->wr_rd_data_pattern = pattern;

	bio_ptr = test_rq->bios_buffer;
	for (i = 0; i < num_bios; ++i) {
		ret = blk_rq_map_kern(ptd->req_q, rq,
				      (void *)bio_ptr,
				      sizeof(unsigned int)*BIO_U32_SIZE,
				      GFP_KERNEL);
		if (ret) {
			pr_err("%s: blk_rq_map_kern returned error %d",
				    __func__, ret);
			goto err;
		}
		bio_ptr += BIO_U32_SIZE;
	}

	if (end_req_io)
		rq->end_io = end_req_io;
	else
		rq->end_io = end_test_req;
	rq->__sector = start_sec;
	rq->cmd_type |= REQ_TYPE_FS;
	rq->cmd_flags |= REQ_SORTED;
	rq->cmd_flags &= ~REQ_IO_STAT;

	if (rq->bio) {
		rq->bio->bi_iter.bi_sector = start_sec;
		rq->bio->bi_end_io = end_test_bio;
		bio = rq->bio;
		while ((bio = bio->bi_next) != NULL)
			bio->bi_end_io = end_test_bio;
	}

	ptd->num_of_write_bios += num_bios;
	test_rq->req_id = ptd->wr_rd_next_req_id++;

	test_rq->req_completed = false;
	test_rq->req_result = -EINVAL;
	test_rq->rq = rq;
	if (ptd->test_info.get_rq_disk_fn)
		test_rq->rq->rq_disk = ptd->test_info.get_rq_disk_fn();
	test_rq->is_err_expected = is_err_expcted;
	rq->elv.priv[0] = (void *)test_rq;

	pr_debug("%s: created test request %d, buf_size=%d",
			__func__, test_rq->req_id, buf_size);

	return test_rq;
err:
	blk_put_request(rq);
	kfree(test_rq->bios_buffer);
	return NULL;
}
EXPORT_SYMBOL(test_iosched_create_test_req);


/**
 * test_iosched_add_wr_rd_test_req() - Create and queue a
 * read/write request.
 * @is_err_expcted:	A flag to indicate if this request
 *			should succeed or not
 * @direction:		READ/WRITE
 * @start_sec:		start address of the first bio
 * @num_bios:		number of BIOs to be allocated for the
 *			request
 * @pattern:		A pattern, to be written into the write
 *			requests data buffer. In case of READ
 *			request, the given pattern is kept as
 *			the expected pattern. The expected
 *			pattern will be compared in the test
 *			check result function. If no comparisson
 *			is required, set pattern to
 *			TEST_NO_PATTERN.
 * @end_req_io:		specific completion callback. When not
 *			set,the default callback will be used
 *
 * This function allocates the test request and the block
 * request and calls blk_rq_map_kern which allocates the
 * required BIO. Upon success the new request is added to the
 * test_queue. The allocated test request and the block request
 * memory is freed at the end of the test and the allocated BIO
 * memory is freed by end_test_bio.
 */
int test_iosched_add_wr_rd_test_req(int is_err_expcted,
		      int direction, int start_sec,
		      int num_bios, int pattern, rq_end_io_fn *end_req_io)
{
	struct test_request *test_rq = NULL;

	test_rq = test_iosched_create_test_req(is_err_expcted,
			direction, start_sec,
			num_bios, pattern, end_req_io);
	if (test_rq) {
		spin_lock_irq(ptd->req_q->queue_lock);
		list_add_tail(&test_rq->queuelist, &ptd->test_queue);
		ptd->test_count++;
		spin_unlock_irq(ptd->req_q->queue_lock);
		return 0;
	}
	return -ENODEV;
}
EXPORT_SYMBOL(test_iosched_add_wr_rd_test_req);

/* Converts the testcase number into a string */
static char *get_test_case_str(struct test_data *td)
{
	if (td->test_info.get_test_case_str_fn)
		return td->test_info.get_test_case_str_fn(td);

	return "Unknown testcase";
}

/*
 * Verify that the test request data buffer includes the expected
 * pattern
 */
static int compare_buffer_to_pattern(struct test_request *test_rq)
{
	int i = 0;
	int num_of_dwords = test_rq->buf_size/sizeof(int);

	/* num_bytes should be aligned to sizeof(int) */
	BUG_ON((test_rq->buf_size % sizeof(int)) != 0);
	BUG_ON(test_rq->bios_buffer == NULL);

	if (test_rq->wr_rd_data_pattern == TEST_NO_PATTERN)
		return 0;

	if (test_rq->wr_rd_data_pattern == TEST_PATTERN_SEQUENTIAL) {
		for (i = 0; i < num_of_dwords; i++) {
			if (test_rq->bios_buffer[i] != i) {
				pr_err(
					"%s: wrong pattern 0x%x in index %d",
					__func__, test_rq->bios_buffer[i], i);
				return -EINVAL;
			}
		}
	} else {
		for (i = 0; i < num_of_dwords; i++) {
			if (test_rq->bios_buffer[i] !=
			    test_rq->wr_rd_data_pattern) {
				pr_err(
					"%s: wrong pattern 0x%x in index %d",
					__func__, test_rq->bios_buffer[i], i);
				return -EINVAL;
			}
		}
	}

	return 0;
}

/*
 * Determine if the test passed or failed.
 * The function checks the test request completion value and calls
 * check_testcase_result for result checking that are specific
 * to a test case.
 */
static int check_test_result(struct test_data *td)
{
	struct test_request *test_rq;
	int res = 0;
	static int run;

	if (!ptd)
		goto err;

	list_for_each_entry(test_rq, &ptd->dispatched_queue, queuelist) {
		if (!test_rq->rq) {
			pr_info("%s: req_id %d is contains empty req",
					__func__, test_rq->req_id);
			continue;
		}
		if (!test_rq->req_completed) {
			pr_err("%s: rq %d not completed", __func__,
				    test_rq->req_id);
			res = -EINVAL;
			goto err;
		}

		if ((test_rq->req_result < 0) && !test_rq->is_err_expected) {
			pr_err(
				"%s: rq %d completed with err, not as expected",
				__func__, test_rq->req_id);
			res = -EINVAL;
			goto err;
		}
		if ((test_rq->req_result == 0) && test_rq->is_err_expected) {
			pr_err("%s: rq %d succeeded, not as expected",
				    __func__, test_rq->req_id);
			res = -EINVAL;
			goto err;
		}
		if (rq_data_dir(test_rq->rq) == READ) {
			res = compare_buffer_to_pattern(test_rq);
			if (res) {
				pr_err("%s: read pattern not as expected",
					    __func__);
				res = -EINVAL;
				goto err;
			}
		}
	}

	if (td->test_info.check_test_result_fn) {
		res = td->test_info.check_test_result_fn(td);
		if (res)
			goto err;
	}

	pr_info("%s: %s, run# %03d, PASSED",
			    __func__, get_test_case_str(td), ++run);
	td->test_result = TEST_PASSED;

	return 0;
err:
	pr_err("%s: %s, run# %03d, FAILED",
		    __func__, get_test_case_str(td), ++run);
	td->test_result = TEST_FAILED;
	return res;
}

/* Create and queue the required requests according to the test case */
static int prepare_test(struct test_data *td)
{
	int ret = 0;

	if (td->test_info.prepare_test_fn) {
		ret = td->test_info.prepare_test_fn(td);
		return ret;
	}

	return 0;
}

/* Run the test */
static int run_test(struct test_data *td)
{
	int ret = 0;

	if (td->test_info.run_test_fn) {
		ret = td->test_info.run_test_fn(td);
		return ret;
	}

	blk_run_queue(td->req_q);

	return 0;
}

/*
 * free_test_queue() - Free all allocated test requests in the given test_queue:
 * free their requests and BIOs buffer
 * @test_queue		the test queue to be freed
 */
static void free_test_queue(struct list_head *test_queue)
{
	struct test_request *test_rq;
	struct bio *bio;

	while (!list_empty(test_queue)) {
		test_rq = list_entry(test_queue->next, struct test_request,
				queuelist);

		list_del_init(&test_rq->queuelist);
		/*
		 * If the request was not completed we need to free its BIOs
		 * and remove it from the packed list
		 */
		if (!test_rq->req_completed) {
			pr_info(
				"%s: Freeing memory of an uncompleted request",
					__func__);
			list_del_init(&test_rq->rq->queuelist);
			while ((bio = test_rq->rq->bio) != NULL) {
				test_rq->rq->bio = bio->bi_next;
				bio_put(bio);
			}
		}
		blk_put_request(test_rq->rq);
		kfree(test_rq->bios_buffer);
		kfree(test_rq);
	}
}

/*
 * free_test_requests() - Free all allocated test requests in
 * all test queues in given test_data.
 * @td		The test_data struct whos test requests will be
 *		freed.
 */
static void free_test_requests(struct test_data *td)
{
	if (!td)
		return;

	if (td->urgent_count) {
		free_test_queue(&td->urgent_queue);
		td->urgent_count = 0;
	}
	if (td->test_count) {
		free_test_queue(&td->test_queue);
		td->test_count = 0;
	}
	if (td->dispatched_count) {
		free_test_queue(&td->dispatched_queue);
		td->dispatched_count = 0;
	}
	if (td->reinsert_count) {
		free_test_queue(&td->reinsert_queue);
		td->reinsert_count = 0;
	}
}

/*
 * post_test() - Do post test operations. Free the allocated
 * test requests, their requests and BIOs buffer.
 * @td		The test_data struct for the test that has
 *		ended.
 */
static int post_test(struct test_data *td)
{
	int ret = 0;

	if (td->test_info.post_test_fn)
		ret = td->test_info.post_test_fn(td);

	ptd->test_info.testcase = 0;
	ptd->test_state = TEST_IDLE;

	free_test_requests(td);

	return ret;
}

/*
 * The timer verifies that the test will be completed even if we don't get
 * the completion callback for all the requests.
 */
static void test_timeout_handler(unsigned long data)
{
	struct test_data *td = (struct test_data *)data;

	pr_info("%s: TIMEOUT timer expired", __func__);
	td->test_state = TEST_COMPLETED;
	wake_up(&td->wait_q);
	return;
}

static unsigned int get_timeout_msec(struct test_data *td)
{
	if (td->test_info.timeout_msec)
		return td->test_info.timeout_msec;
	else
		return TIMEOUT_TIMER_MS;
}

/**
 * test_iosched_start_test() - Prepares and runs the test.
 * The members test_duration and test_byte_count of the input
 * parameter t_info are modified by this function.
 * @t_info:	the current test testcase and callbacks
 *		functions
 *
 * The function also checks the test result upon test completion
 */
int test_iosched_start_test(struct test_info *t_info)
{
	int ret = 0;
	unsigned timeout_msec;
	int counter = 0;
	char *test_name = NULL;

	if (!ptd)
		return -ENODEV;

	if (!t_info) {
		ptd->test_result = TEST_FAILED;
		return -EINVAL;
	}

	do {
		if (ptd->ignore_round)
			/*
			 * We ignored the last run due to FS write requests.
			 * Sleep to allow those requests to be issued
			 */
			msleep(2000);

		spin_lock(&ptd->lock);

		if (ptd->test_state != TEST_IDLE) {
			pr_info(
				"%s: Another test is running, try again later",
				__func__);
			spin_unlock(&ptd->lock);
			return -EBUSY;
		}

		if (ptd->start_sector == 0) {
			pr_err("%s: Invalid start sector", __func__);
			ptd->test_result = TEST_FAILED;
			spin_unlock(&ptd->lock);
			return -EINVAL;
		}

		memcpy(&ptd->test_info, t_info, sizeof(struct test_info));

		ptd->test_result = TEST_NO_RESULT;
		ptd->num_of_write_bios = 0;

		ptd->unique_next_req_id = UNIQUE_START_REQ_ID;
		ptd->wr_rd_next_req_id = WR_RD_START_REQ_ID;

		ptd->ignore_round = false;
		ptd->fs_wr_reqs_during_test = false;

		ptd->test_state = TEST_RUNNING;

		spin_unlock(&ptd->lock);
		/*
		 * Give an already dispatch request from
		 * FS a chanse to complete
		 */
		msleep(2000);

		timeout_msec = get_timeout_msec(ptd);
		mod_timer(&ptd->timeout_timer, jiffies +
			  msecs_to_jiffies(timeout_msec));

		if (ptd->test_info.get_test_case_str_fn)
			test_name = ptd->test_info.get_test_case_str_fn(ptd);
		else
			test_name = "Unknown testcase";
		pr_info("%s: Starting test %s", __func__, test_name);

		ret = prepare_test(ptd);
		if (ret) {
			pr_err("%s: failed to prepare the test",
				    __func__);
			goto error;
		}

		ptd->test_info.test_duration = ktime_get();
		ret = run_test(ptd);
		if (ret) {
			pr_err("%s: failed to run the test", __func__);
			goto error;
		}

		pr_info("%s: Waiting for the test completion", __func__);

		wait_event(ptd->wait_q, ptd->test_state == TEST_COMPLETED);
		t_info->test_duration = ptd->test_info.test_duration;
		t_info->test_byte_count = ptd->test_info.test_byte_count;
		del_timer_sync(&ptd->timeout_timer);

		ret = check_test_result(ptd);
		if (ret) {
			pr_err("%s: check_test_result failed",
				    __func__);
			goto error;
		}

		ret = post_test(ptd);
		if (ret) {
			pr_err("%s: post_test failed", __func__);
			goto error;
		}

		/*
		 * Wakeup the queue thread to fetch FS requests that might got
		 * postponded due to the test
		 */
		blk_run_queue(ptd->req_q);

		if (ptd->ignore_round)
			pr_info(
			"%s: Round canceled (Got wr reqs in the middle)",
			__func__);

		if (++counter == TEST_MAX_TESTCASE_ROUNDS) {
			pr_info("%s: Too many rounds, did not succeed...",
			     __func__);
			ptd->test_result = TEST_FAILED;
		}

	} while ((ptd->ignore_round) && (counter < TEST_MAX_TESTCASE_ROUNDS));

	if (ptd->test_result == TEST_PASSED)
		return 0;
	else
		return -EINVAL;

error:
	post_test(ptd);
	ptd->test_result = TEST_FAILED;
	return ret;
}
EXPORT_SYMBOL(test_iosched_start_test);

/**
 * test_iosched_register() - register a block device test
 * utility.
 * @bdt:	the block device test type to register
 */
void test_iosched_register(struct blk_dev_test_type *bdt)
{
	spin_lock(&blk_dev_test_list_lock);
	list_add_tail(&bdt->list, &blk_dev_test_list);
	spin_unlock(&blk_dev_test_list_lock);
}
EXPORT_SYMBOL_GPL(test_iosched_register);

/**
 * test_iosched_unregister() - unregister a block device test
 * utility.
 * @bdt:	the block device test type to unregister
 */
void test_iosched_unregister(struct blk_dev_test_type *bdt)
{
	spin_lock(&blk_dev_test_list_lock);
	list_del_init(&bdt->list);
	spin_unlock(&blk_dev_test_list_lock);
}
EXPORT_SYMBOL_GPL(test_iosched_unregister);

/**
 * test_iosched_set_test_result() - Set the test
 * result(PASS/FAIL)
 * @test_result:	the test result
 */
void test_iosched_set_test_result(int test_result)
{
	if (!ptd)
		return;

	ptd->test_result = test_result;
}
EXPORT_SYMBOL(test_iosched_set_test_result);


/**
 * test_iosched_set_ignore_round() - Set the ignore_round flag
 * @ignore_round:	A flag to indicate if this test round
 * should be ignored and re-run
 */
void test_iosched_set_ignore_round(bool ignore_round)
{
	if (!ptd)
		return;

	ptd->ignore_round = ignore_round;
}
EXPORT_SYMBOL(test_iosched_set_ignore_round);

/**
 * test_iosched_get_debugfs_tests_root() - returns the root
 * debugfs directory for the test_iosched tests
 */
struct dentry *test_iosched_get_debugfs_tests_root(void)
{
	if (!ptd)
		return NULL;

	return ptd->debug.debug_tests_root;
}
EXPORT_SYMBOL(test_iosched_get_debugfs_tests_root);

/**
 * test_iosched_get_debugfs_utils_root() - returns the root
 * debugfs directory for the test_iosched utils
 */
struct dentry *test_iosched_get_debugfs_utils_root(void)
{
	if (!ptd)
		return NULL;

	return ptd->debug.debug_utils_root;
}
EXPORT_SYMBOL(test_iosched_get_debugfs_utils_root);

static int test_debugfs_init(struct test_data *td)
{
	td->debug.debug_root = debugfs_create_dir("test-iosched", NULL);
	if (!td->debug.debug_root)
		return -ENOENT;

	td->debug.debug_tests_root = debugfs_create_dir("tests",
							td->debug.debug_root);
	if (!td->debug.debug_tests_root)
		goto err;

	td->debug.debug_utils_root = debugfs_create_dir("utils",
							td->debug.debug_root);
	if (!td->debug.debug_utils_root)
		goto err;

	td->debug.debug_test_result = debugfs_create_u32(
					"test_result",
					S_IRUGO | S_IWUGO,
					td->debug.debug_utils_root,
					&td->test_result);
	if (!td->debug.debug_test_result)
		goto err;

	td->debug.start_sector = debugfs_create_u32(
					"start_sector",
					S_IRUGO | S_IWUGO,
					td->debug.debug_utils_root,
					&td->start_sector);
	if (!td->debug.start_sector)
		goto err;

	return 0;

err:
	debugfs_remove_recursive(td->debug.debug_root);
	return -ENOENT;
}

static void test_debugfs_cleanup(struct test_data *td)
{
	debugfs_remove_recursive(td->debug.debug_root);
}

static void print_req(struct request *req)
{
	struct bio *bio;
	struct test_request *test_rq;

	if (!req)
		return;

	test_rq = (struct test_request *)req->elv.priv[0];

	if (test_rq) {
		pr_debug("%s: Dispatch request %d: __sector=0x%lx",
		       __func__, test_rq->req_id, (unsigned long)req->__sector);
		pr_debug("%s: nr_phys_segments=%d, num_of_sectors=%d",
		       __func__, req->nr_phys_segments, blk_rq_sectors(req));
		bio = req->bio;
		pr_debug("%s: bio: bi_size=%d, bi_sector=0x%lx",
			      __func__, bio->bi_iter.bi_size,
			      (unsigned long)bio->bi_iter.bi_sector);
		while ((bio = bio->bi_next) != NULL) {
			pr_debug("%s: bio: bi_size=%d, bi_sector=0x%lx",
				      __func__, bio->bi_iter.bi_size,
				      (unsigned long)bio->bi_iter.bi_sector);
		}
	}
}

static void test_merged_requests(struct request_queue *q,
			 struct request *rq, struct request *next)
{
	list_del_init(&next->queuelist);
}
/*
 * test_dispatch_from(): Dispatch request from @queue to the @dispatched_queue.
 * Also update th dispatched_count counter.
 */
static int test_dispatch_from(struct request_queue *q,
		struct list_head *queue, unsigned int *count)
{
	struct test_request *test_rq;
	struct request *rq;
	int ret = 0;

	if (!ptd)
		goto err;

	spin_lock_irq(&ptd->lock);
	if (!list_empty(queue)) {
		test_rq = list_entry(queue->next, struct test_request,
				queuelist);
		rq = test_rq->rq;
		if (!rq) {
			pr_err("%s: null request,return", __func__);
			spin_unlock_irq(&ptd->lock);
			goto err;
		}
		list_move_tail(&test_rq->queuelist, &ptd->dispatched_queue);
		ptd->dispatched_count++;
		(*count)--;
		spin_unlock_irq(&ptd->lock);

		print_req(rq);
		elv_dispatch_sort(q, rq);
		ptd->test_info.test_byte_count += test_rq->buf_size;
		ret = 1;
		goto err;
	}
	spin_unlock_irq(&ptd->lock);

err:
	return ret;
}

/*
 * Dispatch a test request in case there is a running test Otherwise, dispatch
 * a request that was queued by the FS to keep the card functional.
 */
static int test_dispatch_requests(struct request_queue *q, int force)
{
	struct test_data *td = q->elevator->elevator_data;
	struct request *rq = NULL;
	int ret = 0;

	switch (td->test_state) {
	case TEST_IDLE:
		if (!list_empty(&td->queue)) {
			rq = list_entry(td->queue.next, struct request,
					queuelist);
			list_del_init(&rq->queuelist);
			elv_dispatch_sort(q, rq);
			ret = 1;
			goto exit;
		}
		break;
	case TEST_RUNNING:
		if (test_dispatch_from(q, &td->urgent_queue,
				       &td->urgent_count)) {
			pr_debug("%s: Dispatched from urgent_count=%d",
					__func__, ptd->urgent_count);
			ret = 1;
			goto exit;
		}
		if (test_dispatch_from(q, &td->reinsert_queue,
				       &td->reinsert_count)) {
			pr_debug("%s: Dispatched from reinsert_count=%d",
					__func__, ptd->reinsert_count);
			ret = 1;
			goto exit;
		}
		if (test_dispatch_from(q, &td->test_queue, &td->test_count)) {
			pr_debug("%s: Dispatched from test_count=%d",
					__func__, ptd->test_count);
			ret = 1;
			goto exit;
		}
		break;
	case TEST_COMPLETED:
	default:
		break;
	}

exit:
	return ret;
}

static void test_add_request(struct request_queue *q, struct request *rq)
{
	struct test_data *td = q->elevator->elevator_data;

	list_add_tail(&rq->queuelist, &td->queue);

	/*
	 * The write requests can be followed by a FLUSH request that might
	 * cause unexpected results of the test.
	 */
	if ((rq_data_dir(rq) == WRITE) && (td->test_state == TEST_RUNNING)) {
		pr_debug("%s: got WRITE req in the middle of the test",
			__func__);
		td->fs_wr_reqs_during_test = true;
	}
}

static struct request *
test_former_request(struct request_queue *q, struct request *rq)
{
	struct test_data *td = q->elevator->elevator_data;

	if (rq->queuelist.prev == &td->queue)
		return NULL;
	return list_entry(rq->queuelist.prev, struct request, queuelist);
}

static struct request *
test_latter_request(struct request_queue *q, struct request *rq)
{
	struct test_data *td = q->elevator->elevator_data;

	if (rq->queuelist.next == &td->queue)
		return NULL;
	return list_entry(rq->queuelist.next, struct request, queuelist);
}

static int test_init_queue(struct request_queue *q, struct elevator_type *e)
{
	struct blk_dev_test_type *__bdt;
	struct elevator_queue *eq;

	eq = elevator_alloc(q, e);
	if (!eq)
		return -ENOMEM;

	ptd = kmalloc_node(sizeof(struct test_data), GFP_KERNEL,
			     q->node);
	if (!ptd) {
		pr_err("%s: failed to allocate test data", __func__);
		return -ENOMEM;
	}
	memset((void *)ptd, 0, sizeof(struct test_data));
	INIT_LIST_HEAD(&ptd->queue);
	INIT_LIST_HEAD(&ptd->test_queue);
	INIT_LIST_HEAD(&ptd->dispatched_queue);
	INIT_LIST_HEAD(&ptd->reinsert_queue);
	INIT_LIST_HEAD(&ptd->urgent_queue);
	init_waitqueue_head(&ptd->wait_q);

	eq->elevator_data = ptd;
	ptd->req_q = q;
	spin_lock_irq(q->queue_lock);
	q->elevator = eq;
	spin_unlock_irq(q->queue_lock);

	setup_timer(&ptd->timeout_timer, test_timeout_handler,
		    (unsigned long)ptd);

	spin_lock_init(&ptd->lock);

	if (test_debugfs_init(ptd)) {
		pr_err("%s: Failed to create debugfs files", __func__);
		return -ENOMEM;
	}

	list_for_each_entry(__bdt, &blk_dev_test_list, list)
		__bdt->init_fn();

	return 0;
}

static void test_exit_queue(struct elevator_queue *e)
{
	struct test_data *td = e->elevator_data;
	struct blk_dev_test_type *__bdt;

	BUG_ON(!list_empty(&td->queue));

	list_for_each_entry(__bdt, &blk_dev_test_list, list)
		__bdt->exit_fn();

	test_debugfs_cleanup(td);

	kfree(td);
}

/**
 * test_iosched_add_urgent_req() - Add an urgent test_request.
 * First mark the request as urgent, then add it to the
 * urgent_queue test queue.
 * @test_rq:		pointer to the urgent test_request to be
 *			added.
 *
 */
void test_iosched_add_urgent_req(struct test_request *test_rq)
{
	spin_lock_irq(&ptd->lock);
	test_rq->rq->cmd_flags |= REQ_URGENT;
	list_add_tail(&test_rq->queuelist, &ptd->urgent_queue);
	ptd->urgent_count++;
	spin_unlock_irq(&ptd->lock);
}
EXPORT_SYMBOL(test_iosched_add_urgent_req);

static struct elevator_type elevator_test_iosched = {

	.ops = {
		.elevator_merge_req_fn = test_merged_requests,
		.elevator_dispatch_fn = test_dispatch_requests,
		.elevator_add_req_fn = test_add_request,
		.elevator_former_req_fn = test_former_request,
		.elevator_latter_req_fn = test_latter_request,
		.elevator_init_fn = test_init_queue,
		.elevator_exit_fn = test_exit_queue,
	},
	.elevator_name = "test-iosched",
	.elevator_owner = THIS_MODULE,
};

static int __init test_init(void)
{
	elv_register(&elevator_test_iosched);

	return 0;
}

static void __exit test_exit(void)
{
	elv_unregister(&elevator_test_iosched);
}

module_init(test_init);
module_exit(test_exit);

MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Test IO scheduler");