-
Notifications
You must be signed in to change notification settings - Fork 3
/
dblockmain.c
2411 lines (1991 loc) · 81.7 KB
/
dblockmain.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-only
/*
* Copyright (C) 2020-2021 Datto Inc.
*/
#include <linux/miscdevice.h>
#include <linux/types.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/crc32.h>
#include <linux/init.h>
#include <linux/wait.h>
#include <linux/gfp.h>
#include <linux/hdreg.h>
#include <linux/blkdev.h>
#include <linux/sched/signal.h>
#include <linux/delay.h>
#include <linux/ctype.h>
#include <linux/version.h>
#include <linux/kern_levels.h>
#include <linux/moduleparam.h>
#include <linux/stat.h>
#include <linux/blk_types.h>
#include <linux/genhd.h>
#include "dblocktools.h"
#include "dblock.h"
#define DBLOCK_VERSION BUILD_VERSION
#define DBLOCK_LICENSE "GPL"
#define DBLOCK_AUTHOR "Stu Mark <[email protected]>"
#define DBLOCK_DESC "kernel driver for dblock object store block device"
#define DBLOCK_CONTROL_DEVICE_NAME "dblockctl"
#define KERNEL_SECTOR_SIZE 512
#define STATE_RUNNING 0
#define STATE_SHUTDOWN 1
#define STATE_FORCE_SHUTDOWN 2
static int max_minors = 255;
static int debug_logs = 0;
static int data_interrogation = 0;
#define log_kern_debug(fmt, args...) do{ if(debug_logs) printk(KERN_DEBUG "dblock:[%d] debug kernel: " fmt "\n", current->pid, ## args); }while(0)
#define log_kern_info(fmt, args...) printk(KERN_INFO "dblock:[%d] info kernel: " fmt "\n", current->pid, ## args)
#define log_kern_error(error, fmt, args...) printk(KERN_ERR "dblock:[%d] error kernel: " fmt ": error %d\n", current->pid, ## args, error)
#define log_user_debug(fmt, args...) do{ if(debug_logs) printk(KERN_DEBUG "dblock:[%d] debug userspace: " fmt "\n", current->pid, ## args); }while(0)
#define log_user_info(fmt, args...) printk(KERN_INFO "dblock:[%d] info userspace: " fmt "\n", current->pid, ## args)
#define log_user_error(error, fmt, args...) printk(KERN_ERR "dblock:[%d] error userspace: " fmt ": %d\n", current->pid, ## args, error)
void hexdump(void *ptr, int buflen)
{
unsigned char *buf = (unsigned char*) ptr;
int i, j;
for (i = 0; i < buflen; i += 16) {
for (j = 0; j < 16; j++)
if (i + j < buflen)
printk("%02x ", buf[i + j]);
else
printk(" ");
printk(" ");
for (j = 0; j < 16; j++)
if (i + j < buflen)
printk("%c", isprint(buf[i + j]) ? buf[i + j] : '.');
printk("\n");
}
}
void hexdumpbuf(char *out , void *ptr)
{
int buflen = 16;
unsigned char *buf = (unsigned char *) ptr;
int i, j;
char *pos = out;
memset(out, 0xff, 119);
for (i = 0; i < buflen; i += 16) {
for (j = 0; j < 16; j++)
if (i + j < buflen)
pos += sprintf(pos, "%02x ", buf[i + j]);
else
pos += sprintf(pos, " ");
pos += sprintf(pos, " ");
for (j = 0; j < 16; j++)
if (i + j < buflen)
pos += sprintf(pos, "%c", isprint(buf[i + j]) ? buf[i + j] : '.');
}
}
u64 hashup (char *start, u64 length)
{
if (debug_logs)
return crc32(0x80000000, start, length);
else
return 0;
}
module_param(max_minors, int, 0000);
MODULE_PARM_DESC(max_minors, "the maximum number of concurrent dblock devices the kernel module will support");
module_param(debug_logs, int, S_IRUSR | S_IWUSR);
MODULE_PARM_DESC(debug_logs, "whether to show debug logs or not");
module_param(data_interrogation, int, S_IRUSR | S_IWUSR);
MODULE_PARM_DESC(data_interrogation, "whether to show data interrogation");
static struct master_control_state_t
{
pil_mutex_t control_lock_mutex;
u32 next_device_handle;
u32 userspace_waiting;
struct list_head active_device_list;
} master_control_state;
typedef struct device_state_t {
pil_mutex_t device_state_lock_mutex;
atomic_t in_use;
char *device_name;
u32 block_device_registered;
int major;
int dev_open_count;
struct request_queue *queue;
struct gendisk *gendisk;
char *io_dev_name;
struct miscdevice io_dev;
u64 size;
u32 kernel_block_size;
u64 number_of_blocks;
u32 max_segments_per_request;
u32 timeout_milliseconds;
atomic_t shutting_down;
u32 handle_id;
pil_mutex_t request_block_lock;
pil_cv_t request_block_cv;
u32 concurrent_list_unprocessed_count;
u32 next_operation_id;
struct list_head concurrent_operations_list;
struct list_head list_anchor;
} device_state;
static int device_getgeo(struct block_device * block_device, struct hd_geometry * geo);
static int device_open(struct block_device *blk_dev, fmode_t mode);
static void device_release(struct gendisk *gd, fmode_t mode);
static long device_ioctl(struct file *f, unsigned int cmd, unsigned long __user userspace);
static s32 create_concurrent_operation_entry(device_state *device, dblock_context **context);
static void delete_concurrent_operation_entry(device_state *device, dblock_context *context);
static s32 find_device_by_handle_id(u32 handle_id, device_state **deviceout);
static void block_device_cleanup(device_state *ending_device);
static void disk_stats_start(struct request_queue *q, struct bio *bio, struct gendisk *gd);
static void disk_stats_end(struct request_queue *q, struct bio *bio, struct gendisk *gd, unsigned long start_time);
#if HAVE_BLK_ALLOC_QUEUE_NODE == 1
static blk_qc_t bio_request_handler(struct bio *bio);
#endif
static void device_get(device_state *device)
{
atomic_inc(&device->in_use);
}
static void device_put(device_state *device)
{
if (device == NULL)
{
log_kern_error(-EINVAL, "sanity failure, somebody called device_put with a NULL device.");
return;
}
if (atomic_dec_and_test(&device->in_use))
{
log_kern_debug("last reference count to device_id %d, calling block_device_cleanup", device->handle_id);
log_kern_info("removing block device");
block_device_cleanup(device);
}
}
struct block_device_operations dblock_blk_dev_ops = {
.owner = THIS_MODULE,
.open = device_open,
#if HAVE_BLK_ALLOC_QUEUE_NODE == 1
.submit_bio = bio_request_handler,
#endif
.release = device_release,
.getgeo = device_getgeo,
};
static void find_unprocessed_item(device_state *device, dblock_context **something_to_do_context)
{
dblock_context *search_context = NULL;
*something_to_do_context = NULL;
log_user_debug("looking through concurrent operations list for unprocessed items.");
list_for_each_entry(search_context, &device->concurrent_operations_list, list_anchor)
{
if (search_context->processed != 0)
log_user_debug("operation_id %u already processed", search_context->op.operation_id);
else
{
log_user_debug("operation_id %u has not been processed yet, going to do that one", search_context->op.operation_id);
*something_to_do_context = search_context;
(*something_to_do_context)->processed = 1;
device->concurrent_list_unprocessed_count--;
log_user_debug("decrementing unprocessed count to %u", device->concurrent_list_unprocessed_count);
log_user_debug("locking response lock for operation_id %u", (*something_to_do_context)->op.operation_id);
pil_mutex_lock(&((*something_to_do_context)->response_lock));
break;
}
}
}
static s32 handle_kernel_request_special(device_state *device, unsigned long __user userspace, dblock_context *context_ptr)
{
unsigned long copyret;
u32 copyamount;
u32 count;
u32 ret;
dblock_operation *userspaceop = (dblock_operation *)userspace;
dblock_operation_state *bio_segment;
copyamount = 0;
copyret = 0;
ret = 0;
if (debug_logs)
{
dblock_context *count_ptr = NULL;
u32 unprocessed = 0;
count = 0;
list_for_each_entry(count_ptr, &device->concurrent_operations_list, list_anchor)
{
count++;
if (count_ptr->processed == 0)
unprocessed++;
}
log_user_debug("Number of items in concurrent operations list: %u, unprocessed %u", count, unprocessed);
}
log_user_debug("operation block for request found item on head of list operation_id: %u, cmd: %u ", context_ptr->op.operation_id, context_ptr->op.header.operation);
count = 0;
bio_segment = NULL;
list_for_each_entry(bio_segment, &context_ptr->bio_segments_op_state_list, list_anchor)
{
dblock_bio_segment_metadata *entry;
log_user_debug("copying metadata for bio_segment %lld to dblock_operation, start %lld, length %lld", bio_segment->id, bio_segment->request_start, bio_segment->request_length);
entry = &(context_ptr->op.metadata[count]);
count++;
if (count > context_ptr->number_of_segments_in_list)
{
log_user_error(-EOVERFLOW, "kernel side sent userspace more bio segments than we can handle %d/%d. Sanity failure.", count, context_ptr->number_of_segments_in_list);
ret = -EOVERFLOW;
goto error;
}
entry->start = bio_segment->request_start;
entry->length = bio_segment->request_length;
}
if (data_interrogation)
{
log_user_debug("copying request to userspace dblock->op before returning, copy from %p to %p", &context_ptr->op, userspaceop);
}
copyamount = sizeof(dblock_operation);
copyret = copy_to_user(userspaceop, &context_ptr->op, copyamount);
if (copyret != 0)
{
log_user_error(-EINVAL, "Unable to copy kernel block thread request to userspace, failed to copy %ld bytes", copyret);
ret = -EINVAL;
goto error;
}
if (context_ptr->op.header.operation == DEVICE_OPERATION_KERNEL_WRITE_REQUEST)
{
char *bio_segment_buffer;
char *page_buffer;
char *userspacedatabufferpos;
u32 pointerpositioncounter;
pointerpositioncounter = 0;
userspacedatabufferpos = userspaceop->userspacedatabuffer;
count = 0;
bio_segment = NULL;
list_for_each_entry(bio_segment, &context_ptr->bio_segments_op_state_list, list_anchor)
{
dblock_bio_segment_metadata *entry;
log_user_debug("copying write data for bio_segment %lld to dblock_operation userspacedatabuffer.", bio_segment->id);
entry = &(context_ptr->op.metadata[count]);
count++;
if (count > context_ptr->number_of_segments_in_list)
{
log_user_error(-EOVERFLOW, "kernel side sent userspace more bio segments than we can handle %d/%d. Sanity failure.", count, context_ptr->number_of_segments_in_list);
ret = -EOVERFLOW;
goto error;
}
if (pointerpositioncounter + entry->length > MAX_BIO_SEGMENTS_BUFFER_SIZE_PER_REQUEST)
{
log_user_error(-EOVERFLOW, "kernel side sent userspace more bio segment DATA than we can fit in the userspace data buffer %lld/%d. Sanity failure.",
pointerpositioncounter + entry->length, MAX_BIO_SEGMENTS_BUFFER_SIZE_PER_REQUEST);
ret = -EOVERFLOW;
goto error;
}
page_buffer = kmap(bio_segment->bv_page);
bio_segment_buffer = page_buffer + bio_segment->offset;
if (data_interrogation)
{
log_user_debug("copy databuffer from %p to %p", bio_segment_buffer, userspacedatabufferpos);
}
copyret = copy_to_user(userspacedatabufferpos, bio_segment_buffer, entry->length);
kunmap(bio_segment->bv_page);
if (copyret != 0)
{
log_user_error(EINVAL, "Unable to copy kernel block thread data to userspace, failed to copy %ld bytes", copyret);
ret = -ENOMEM;
goto error;
}
log_user_debug("Copied %lld bytes for write to userspace directly from kernel buffer %p to %p", entry->length,
bio_segment_buffer, userspacedatabufferpos);
userspacedatabufferpos += entry->length;
pointerpositioncounter += entry->length;
}
}
error:
return ret;
}
static void make_operation_return_signal(device_state *device, int signal_number, unsigned long __user userspace)
{
u32 copyamount;
dblock_operation *userspaceop = (dblock_operation *)userspace;
dblock_operation *dummy = NULL;
u32 op_id;
unsigned long copyret;
s32 ret = 0;
dummy = kmalloc(sizeof(dblock_operation), GFP_KERNEL);
if (dummy == NULL)
{
log_kern_error(ENOMEM, "Unable to allocate memory for dummy userspace response in make_operation_return_signal.");
log_kern_error(ENOMEM, "userspace is going to get an undefined response from us, good luck, godspeed.");
ret = -ENOMEM;
goto error;
}
memset(dummy, 0, sizeof(dblock_operation));
pil_mutex_lock(&device->device_state_lock_mutex);
op_id = ++device->next_operation_id;
dummy->operation_id = op_id;
pil_mutex_unlock(&device->device_state_lock_mutex);
log_user_debug("New operation_id for dummy: %u", dummy->operation_id);
dummy->header.operation = DEVICE_OPERATION_KERNEL_BLOCK_FOR_REQUEST;
dummy->header.size = 0;
dummy->header.signal_number = signal_number;
copyamount = 0;
copyret = 0;
copyamount = sizeof(dblock_operation);
log_user_debug("copy dummy op from %p to %p", userspaceop, dummy);
copyret = copy_to_user(userspaceop, dummy, copyamount);
if (copyret != 0)
{
log_user_error(EINVAL, "Unable to copy kernel block thread dummy block_for_request request to userspace, failed to copy %ld bytes", copyret);
ret = -EINVAL;
goto error;
}
error:
if (dummy != NULL)
kfree(dummy);
}
static void make_operation_block_for_request(device_state *device, unsigned long __user userspace)
{
make_operation_return_signal(device, 0, userspace);
}
static s32 dblock_operation_block_for_request(device_state *device, unsigned long __user userspace)
{
s32 ret;
dblock_context *process_item_context = NULL;
log_user_debug("start dblock_operation_block_for_request.");
log_user_debug("about to lock request_block_lock. start dblock_operation_block_for_request.");
pil_mutex_lock(&device->request_block_lock);
do
{
log_user_debug("concurrent_list_unprocessed_count: %u", device->concurrent_list_unprocessed_count);
if (device->concurrent_list_unprocessed_count == 0)
{
log_user_debug("waiting on request_block_cv");
pil_cv_wait(&device->request_block_cv, &device->request_block_lock);
if (signal_pending(current))
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,1)
int signal_number = kernel_dequeue_signal(NULL);
#else
int signal_number = kernel_dequeue_signal();
#endif
if (signal_number == SIGURG)
continue; // ignore sigurg because go.
if (signal_number > 0)
log_user_error(EINVAL, "Got signal %d waiting for cv", signal_number);
else
log_user_error(EINVAL, "Odd, the kernel said there's a signal pending, but there's no signal to be dequeued, dequeue returned = %d", signal_number);
log_user_error(-EINTR, "userspace cv_wait for block_cv exited because of signal, unlocking mutex, returning error to ioctl, signal_number %d", signal_number);
pil_mutex_unlock(&device->request_block_lock);
make_operation_return_signal(device, signal_number, userspace);
return -EINTR;
}
}
else
{
log_user_debug("unprocessed count > 0, not waiting for request_block_cv");
break;
}
}
while (device->concurrent_list_unprocessed_count == 0);
ret = 0;
find_unprocessed_item(device, &process_item_context);
if (process_item_context != NULL)
{
ret = handle_kernel_request_special(device, userspace, process_item_context);
if (ret != 0)
{
ret = -ENOMEM;
make_operation_block_for_request(device, userspace);
}
log_user_debug("unlocking response lock for operation_id %u", process_item_context->op.operation_id);
pil_mutex_unlock(&process_item_context->response_lock);
}
else
{
log_user_error(-ENOENT, "we were woken up/unprocessed count > 0 but there was nothing unprocessed on the concurrent item list to process.");
ret = -ENOENT;
log_user_error(-EFAULT, "resetting the unprocessed count to zero since we just counted it and it's zero even though we think it's %u", device->concurrent_list_unprocessed_count);
device->concurrent_list_unprocessed_count = 0;
make_operation_block_for_request(device, userspace);
}
log_user_debug("dblock_operation_block_for_request unblocked, about to unlock request_block_lock, new unprocessed count: %u", device->concurrent_list_unprocessed_count);
pil_mutex_unlock(&device->request_block_lock);
log_user_debug("end dblock_operation_block_for_request which includes the request to userspace, returning: %d", ret);
log_user_debug("----------------------- end of userspace processing.");
return ret;
}
static s32 find_context_by_operation_id(device_state *device, u32 operation_id, dblock_context **out)
{
dblock_context *context_ptr;
log_user_debug("find context by operation_id %d, locking request_block lock", operation_id);
pil_mutex_lock(&device->request_block_lock);
context_ptr = NULL;
list_for_each_entry(context_ptr, &device->concurrent_operations_list, list_anchor)
{
log_user_debug("searching operation_id = %d to match request for %d", context_ptr->op.operation_id, operation_id);
if (context_ptr->op.operation_id == operation_id)
{
*out = context_ptr;
log_user_debug("we found matching operation_id = %u, locking response lock", operation_id);
pil_mutex_lock(&context_ptr->response_lock);
pil_mutex_unlock(&device->request_block_lock);
return 0;
}
}
log_user_error(-ENOENT, "we didn't find operation_id = %d", operation_id);
pil_mutex_unlock(&device->request_block_lock);
return -ENOENT;
}
static s32 dblock_operation_read_response(device_state *device, dblock_operation *op, unsigned long __user userspace)
{
s32 respond_ret;
s32 ret;
unsigned long copyret;
dblock_context *context_ptr;
dblock_operation *userspaceop = (dblock_operation *)userspace;
char *bio_segment_buffer;
char *page_buffer;
char *userspacedatabufferpos;
u32 pointerpositioncounter;
dblock_operation_state *bio_segment;
u32 count;
s32 userspaceerr;
respond_ret = 0;
log_user_debug("start dblock_operation_read_response");
context_ptr = NULL;
ret = find_context_by_operation_id(device, op->operation_id, &context_ptr);
if (ret != 0)
{
log_user_error(-ENOENT, "Unable to find concurrent operation_id %u to broadcast to for read_response", op->operation_id);
log_user_error(-ENOENT, "Will not signal block device thread, because we can't find it.");
respond_ret = -ENOENT;
goto errornocontext;
}
userspaceerr = op->error;
if (userspaceerr != 0)
{
log_kern_error(-EINVAL, "Error returned from userspace in read response. sending error to kernel side: %d", userspaceerr);
respond_ret = -EINVAL;
goto error;
}
pointerpositioncounter = 0;
userspacedatabufferpos = userspaceop->userspacedatabuffer;
count = 0;
bio_segment = NULL;
list_for_each_entry(bio_segment, &context_ptr->bio_segments_op_state_list, list_anchor)
{
log_user_debug("copying data from userspace for bio_segment %lld to bio segment buffer.", bio_segment->id);
count++;
if (count > context_ptr->number_of_segments_in_list)
{
log_user_error(-EOVERFLOW, "we are somehow trying to copy more data than the kernel side said it asked us for %d/%d. Sanity failure.", count, context_ptr->number_of_segments_in_list);
respond_ret = -EOVERFLOW;
goto error;
}
if (pointerpositioncounter + bio_segment->request_length> MAX_BIO_SEGMENTS_BUFFER_SIZE_PER_REQUEST)
{
log_user_error(-EOVERFLOW, "kernel side sent userspace more bio segment DATA than we can fit in the userspace data buffer %lld/%d. Sanity failure.",
pointerpositioncounter + bio_segment->request_length, MAX_BIO_SEGMENTS_BUFFER_SIZE_PER_REQUEST);
respond_ret = -EOVERFLOW;
goto error;
}
page_buffer = kmap(bio_segment->bv_page);
bio_segment_buffer = page_buffer + bio_segment->offset;
log_user_debug("Copying data from userspace directly to bio buf: %llu bytes", bio_segment->request_length);
log_user_debug("copy data from userspace to bio buf %p to %p %lld bytes", userspacedatabufferpos, bio_segment_buffer, bio_segment->request_length);
copyret = copy_from_user(bio_segment_buffer, userspacedatabufferpos, bio_segment->request_length);
kunmap(bio_segment->bv_page);
if (copyret != 0)
{
log_user_error(-ENOMEM, "Unable to copy %llu bytes from userspace, %ld didn't copy", bio_segment->request_length, copyret);
respond_ret = -ENOMEM;
goto error;
}
log_user_debug("userspacedatabufferpos %p, op_state.bio_segment_buffer %p, context_ptr %p", userspacedatabufferpos, bio_segment_buffer, context_ptr);
userspacedatabufferpos += bio_segment->request_length;
pointerpositioncounter += bio_segment->request_length;
}
error:
context_ptr->op.error = respond_ret;
log_user_debug("broadcasting to response_cv to unblock block device thread, and setting triggered flag");
context_ptr->response_cv_triggered_flag = 1;
pil_cv_broadcast(&context_ptr->response_cv);
log_user_debug("about to unlock response_lock");
pil_mutex_unlock(&context_ptr->response_lock);
errornocontext:
log_user_debug("end dblock_operation_read_response, going back to block for another request");
return dblock_operation_block_for_request(device, userspace);
}
static s32 dblock_operation_write_response(device_state *device, dblock_operation *op, unsigned long __user userspace)
{
s32 respond_ret;
s32 ret;
dblock_context *context_ptr;
s32 userspaceerr;
respond_ret = 0;
log_user_debug("start dblock_operation_write_response");
context_ptr = NULL;
ret = find_context_by_operation_id(device, op->operation_id, &context_ptr);
if (ret != 0)
{
log_user_error(-ENOENT, "Unable to find concurrent operation_id %u to respond to for write_response", op->operation_id);
log_user_error(-ENOENT, "Will not signal block device thread, because we can't find it.");
goto errornocontext;
}
userspaceerr = op->error;
if (userspaceerr != 0)
{
log_kern_error(-EINVAL, "Error returned from userspace in write response. sending error to kernel side: %d", userspaceerr);
respond_ret = -EINVAL;
goto error;
}
log_user_debug("data successfully written by userspace: %llu bytes.", op->packet.write_response.response_total_length_of_all_segment_requests_written);
error:
context_ptr->op.error = respond_ret;
log_user_debug("broadcasting to response_cv to unblock device thread for write response, and setting triggered flag");
context_ptr->response_cv_triggered_flag = 1;
pil_cv_broadcast(&context_ptr->response_cv);
log_user_debug("about to unlock response_lock");
pil_mutex_unlock(&context_ptr->response_lock);
errornocontext:
log_user_debug("end dblock_operation_write_response, going back to block for another request");
return dblock_operation_block_for_request(device, userspace);
}
static s32 ioctl_dblock_device_status(unsigned long __user userspace)
{
return -ENODATA;
}
static s32 ioctl_dblock_operation(unsigned long __user userspace)
{
unsigned long copyret;
device_state *device = NULL;
dblock_operation *userspaceop = (dblock_operation *)userspace;
u32 operation;
dblock_operation *holding_area = NULL;
s32 ret = 0;
holding_area = kmalloc(sizeof(dblock_operation), GFP_KERNEL);
if (holding_area == NULL)
{
log_kern_error(ENOMEM, "Unable to allocate memory for holding area for ioctl_dblock_operation.");
ret = -ENOMEM;
goto error;
}
if (data_interrogation)
{
log_user_debug("copy userspace op without buffer to holding area %p to %p", userspaceop, holding_area);
}
copyret = copy_from_user(holding_area, userspaceop, sizeof(dblock_operation));
if (copyret != 0)
{
log_user_error(-ENOMEM, "Unable to copy %lu bytes from userspace", copyret);
ret = -ENOMEM;
goto error;
}
ret = find_device_by_handle_id(holding_area->handle_id, &device);
if (ret != 0)
{
log_kern_error(ret, "Unable to find handle_id %d in list of active devices, can't process ioctl.", holding_area->handle_id);
goto error;
}
operation = holding_area->header.operation;
ret = 0;
switch (operation)
{
case DEVICE_OPERATION_NO_RESPONSE_BLOCK_FOR_REQUEST:
ret = dblock_operation_block_for_request(device, userspace);
break;
case DEVICE_OPERATION_READ_RESPONSE:
ret = dblock_operation_read_response(device, holding_area, userspace);
break;
case DEVICE_OPERATION_WRITE_RESPONSE:
ret = dblock_operation_write_response(device, holding_area, userspace);
break;
default:
log_user_error(-EINVAL, "Unknown ioctl: %d", operation);
ret = -EINVAL;
break;
}
device_put(device);
error:
if (holding_area != NULL)
kfree(holding_area);
return ret;
}
static long device_ioctl(struct file *f, unsigned int cmd, unsigned long __user userspace)
{
s32 ret = 0;
u32 logvalue;
log_user_debug("start dblock_ioctl");
log_user_debug("Ioctl: type=%x number=%x direction=%x size=%x", _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_DIR(cmd), _IOC_SIZE(cmd));
pil_mutex_lock(&master_control_state.control_lock_mutex);
master_control_state.userspace_waiting++;
logvalue = master_control_state.userspace_waiting;
pil_mutex_unlock(&master_control_state.control_lock_mutex);
log_user_debug("entering ioctl, userspace waiting count: %d", logvalue);
if (cmd == IOCTL_DEVICE_PING)
{
log_user_debug("got ioctl_device_ping");
ret = 0;
}
else if (cmd == IOCTL_DEVICE_STATUS)
ret = ioctl_dblock_device_status(userspace);
else if (cmd == IOCTL_DEVICE_OPERATION)
ret = ioctl_dblock_operation(userspace);
else
{
log_user_error(-EINVAL, "Unknown ioctl: %d", cmd);
ret = -EINVAL;
}
pil_mutex_lock(&master_control_state.control_lock_mutex);
master_control_state.userspace_waiting--;
logvalue = master_control_state.userspace_waiting;
pil_mutex_unlock(&master_control_state.control_lock_mutex);
log_user_debug("exiting ioctl, userspace waiting count: %d", logvalue);
log_user_debug("Ioctl end: type=%x number=%x direction=%x size=%x", _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_DIR(cmd), _IOC_SIZE(cmd));
log_user_debug("end dblock_ioctl");
return ret;
}
static int device_getgeo(struct block_device *block_device, struct hd_geometry *geo)
{
u64 size;
s32 retval = 0;
u32 handle_id = 0;
device_state *device = NULL;
struct request_queue *queue = NULL;
log_kern_debug("starting dblock_getgeo");
if (block_device == NULL)
{
log_kern_error(-EINVAL, "block device is NULL");
retval = -EINVAL;
goto error;
}
queue = block_device->bd_disk->queue;
if (queue == NULL)
{
log_kern_error(-EINVAL, "no request queue in block device.");
retval = -EINVAL;
goto error;
}
handle_id = (u32)(size_t)queue->queuedata;
retval = find_device_by_handle_id(handle_id, &device);
if (retval != 0)
{
log_kern_error(-ENOENT, "no device_state found to process getgeo with.");
retval = -ENOENT;
goto error;
}
size = device->size;
do_div(size, KERNEL_SECTOR_SIZE);
geo->cylinders = (size & ~0x3f) >> 6;
geo->heads = 4;
geo->sectors = 16;
geo->start = 0;
device_put(device);
error:
log_kern_debug("end dblock_getgeo");
return retval;
}
static void free_pending_kernel_requests(device_state *device)
{
dblock_context *fail_context = NULL;
log_kern_debug("about to lock request_block_lock so we can mark all the queued items as failures.");
pil_mutex_lock(&device->request_block_lock);
log_kern_debug("looking through concurrent operations list for unprocessed items.");
list_for_each_entry(fail_context, &device->concurrent_operations_list, list_anchor)
{
if (fail_context->processed != 0)
log_kern_debug("operation_id %u already processed, still marking failure", fail_context->op.operation_id);
else
{
log_kern_debug("operation_id %u has not been processed yet, marking it as failure", fail_context->op.operation_id);
fail_context->processed = 1;
device->concurrent_list_unprocessed_count--;
log_kern_debug("decrementing unprocessed count to %u", device->concurrent_list_unprocessed_count);
}
log_kern_debug("locking response lock for operation_id %u", fail_context->op.operation_id);
pil_mutex_lock(&fail_context->response_lock);
fail_context->op.error = -EBADF;
log_kern_debug("broadcasting to response_cv to unblock block device thread, and setting triggered flag because of exit");
fail_context->response_cv_triggered_flag = 1;
pil_cv_broadcast(&fail_context->response_cv);
log_kern_debug("about to unlock response_lock for operation_id %u", fail_context->op.operation_id);
pil_mutex_unlock(&fail_context->response_lock);
}
log_kern_debug("done marking queued items as failures and broadcasting to kernel threads, unlocking request_block_lock");
pil_mutex_unlock(&device->request_block_lock);
}
static u32 unblock_userspace(device_state *device)
{
dblock_context *context;
s32 ret;
u32 sleep_counter;
u32 exit_processed;
u32 userspace_waiting;
context = NULL;
log_kern_debug("starting unblock userspace, failing all pending kernel requests...");
free_pending_kernel_requests(device);
pil_mutex_lock(&master_control_state.control_lock_mutex);
userspace_waiting = master_control_state.userspace_waiting;
pil_mutex_unlock(&master_control_state.control_lock_mutex);
if (userspace_waiting == 0)
userspace_waiting++; // always do at least one.
log_user_debug("unblock userspace, unblocking %d threads", userspace_waiting);
while (userspace_waiting > 0)
{
log_user_debug("unblock userspace, blocked threads remaining: %d", userspace_waiting);
userspace_waiting--;
context = NULL;
log_kern_debug("creating concurrent entry to tell userspace to exit.");
ret = create_concurrent_operation_entry(device, &context);
if (ret != 0)
{
log_kern_error(ret, "unable to create a concurrent operation entry in unblock_userspace.");
return ret;
}
context->op.header.operation = DEVICE_OPERATION_KERNEL_USERSPACE_EXIT;
context->op.header.size = 0;
context->op.header.signal_number = 0;
log_kern_debug("about to lock request_block_lock for unblock userspace adding %u to unprocessed list", context->op.operation_id);
pil_mutex_lock(&device->request_block_lock);
device->concurrent_list_unprocessed_count++;
log_kern_debug("adding userspace exit concurrent entry unprocessed queue.");
list_add_tail(&(context->list_anchor), &device->concurrent_operations_list);
log_kern_debug("broadcasting request_block_cv to ioctl thread and setting concurrent_list_unprocessed_count to %u", device->concurrent_list_unprocessed_count);
pil_cv_broadcast(&device->request_block_cv);
log_kern_debug("about to unlock request_block_lock");
pil_mutex_unlock(&device->request_block_lock);
log_kern_debug("Waiting for userspace to pick up exit queue item.");
exit_processed = 0;
for (sleep_counter = 0; sleep_counter < 5; sleep_counter++)
{
u32 processed = 0;
msleep(1);
pil_mutex_lock(&device->request_block_lock);
pil_mutex_lock(&context->response_lock);
processed = context->processed;
pil_mutex_unlock(&context->response_lock);
pil_mutex_unlock(&device->request_block_lock);
if (processed)
{
exit_processed = 1;
log_kern_debug("We found the exit queue item was marked processed, exiting cleanly.");
break;
}
}
if (exit_processed == 0)
log_kern_info("we queued an exit item, but it was never picked up. Either userspace is already gone or it is too slow.");
delete_concurrent_operation_entry(device, context);
}
log_kern_debug("end of unblock_userspace.");
return ret;
}
static void init_context_item(dblock_context *context)
{
s32 structsize;
pil_mutex_init(&context->response_lock);
pil_cv_init(&context->response_cv);
context->response_cv_triggered_flag = 0;
context->processed = 0;
INIT_LIST_HEAD(&context->bio_segments_op_state_list);
memset(&context->list_anchor, 0, sizeof(struct list_head));
structsize = sizeof(dblock_operation);
memset(&(context->op), 0, structsize);
}
static s32 create_concurrent_operation_entry(device_state *device, dblock_context **context)
{
u32 op_id;
log_kern_debug("Creating concurrent operation entry");
*context = kmalloc(sizeof(dblock_context), GFP_KERNEL);
if (*context == NULL)
{
log_kern_error(ENOMEM, "Unable to allocate a new context for new block device thread operation.");
return -ENOMEM;
}
init_context_item(*context);
pil_mutex_lock(&device->device_state_lock_mutex);
op_id = ++device->next_operation_id;
(*context)->op.operation_id = op_id;
pil_mutex_unlock(&device->device_state_lock_mutex);
log_kern_debug("New operation_id: %u", (*context)->op.operation_id);
return 0;
}
static void delete_concurrent_operation_entry(device_state *device, dblock_context *context)
{
if (context == NULL)
{
log_kern_error(-EINVAL, "Delete concurrent operation entry was passed a null context.");
return;
}
log_kern_debug("Deleting concurrent operation entry for operation_id: %u", context->op.operation_id);
log_kern_debug("about to lock request_block_lock to remove the context item off the list for operation_id %u", context->op.operation_id);
pil_mutex_lock(&device->request_block_lock);
log_kern_debug("about to lock response lock");
pil_mutex_lock(&context->response_lock);
if (context->processed == 0)
{
device->concurrent_list_unprocessed_count--;
log_kern_debug("also decrementing the unprocessed count to %u because an unprocessed item is being removed from the list.", device->concurrent_list_unprocessed_count);
}
pil_mutex_unlock(&context->response_lock);
list_del(&context->list_anchor);
pil_mutex_unlock(&device->request_block_lock);