forked from portworx/px-fuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pxd.c
2509 lines (2116 loc) · 61.8 KB
/
pxd.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
#include <linux/module.h>
#include <linux/module.h>
#include <linux/blkdev.h>
#include <linux/sysfs.h>
#include <linux/crc32.h>
#include <linux/ctype.h>
#include <linux/sched.h>
#include "fuse_i.h"
#include "pxd.h"
#include <linux/uio.h>
#include <linux/bio.h>
#include <linux/pid_namespace.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,3,0)
#include <linux/part_stat.h>
#endif
#define CREATE_TRACE_POINTS
#undef TRACE_INCLUDE_PATH
#define TRACE_INCLUDE_PATH .
#define TRACE_INCLUDE_FILE pxd_trace
#include "pxd_trace.h"
#undef CREATE_TRACE_POINTS
#include "pxd_compat.h"
#include "pxd_core.h"
#ifdef __PX_BLKMQ__
#include <linux/blk-mq.h>
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
#include "io.h"
#include "pxd_io_uring.h"
#endif
/** enables time tracing */
//#define GD_TIME_LOG
#ifdef GD_TIME_LOG
#define KTIME_GET_TS(t) ktime_get_ts((t))
#else
#define KTIME_GET_TS(t)
#endif
#define PXD_TIMER_SECS_MIN 30
#define PXD_TIMER_SECS_DEFAULT 600
#define PXD_TIMER_SECS_MAX (U32_MAX)
#if defined(PF_LESS_THROTTLE)
#define PF_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LESS_THROTTLE)
#elif defined(PF_LOCAL_THROTTLE)
#define PF_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE)
#endif
#define IS_SET_IO_FLUSHER_FLAG(flg) ((flg & PF_IO_FLUSHER) == PF_IO_FLUSHER)
#define IS_SET_IO_FLUSHER(task) (IS_SET_IO_FLUSHER_FLAG((task->flags)))
#define SET_IO_FLUSHER(task) (task->flags |= PF_IO_FLUSHER)
#define CLEAR_IO_FLUSHER(task) (task->flags &= ~(PF_IO_FLUSHER))
#define TOSTRING_(x) #x
#define VERTOSTR(x) TOSTRING_(x)
extern const char *gitversion;
static dev_t pxd_major;
static DEFINE_IDA(pxd_minor_ida);
struct pxd_context *pxd_contexts;
uint32_t pxd_num_contexts = PXD_NUM_CONTEXTS;
uint32_t pxd_num_contexts_exported = PXD_NUM_CONTEXT_EXPORTED;
uint32_t pxd_timeout_secs = PXD_TIMER_SECS_DEFAULT;
uint32_t pxd_detect_zero_writes = 0;
module_param(pxd_num_contexts_exported, uint, 0644);
module_param(pxd_num_contexts, uint, 0644);
module_param(pxd_detect_zero_writes, uint, 0644);
static void pxd_abort_context(struct work_struct *work);
static int pxd_nodewipe_cleanup(struct pxd_context *ctx);
static int pxd_bus_add_dev(struct pxd_device *pxd_dev);
struct pxd_context* find_context(unsigned ctx)
{
if (ctx >= pxd_num_contexts) {
return NULL;
}
return &pxd_contexts[ctx];
}
static int pxd_open(struct block_device *bdev, fmode_t mode)
{
struct pxd_device *pxd_dev = bdev->bd_disk->private_data;
struct fuse_conn *fc = &pxd_dev->ctx->fc;
int err = 0;
spin_lock(&fc->lock);
if (!READ_ONCE(fc->connected)) {
err = -ENXIO;
} else {
spin_lock(&pxd_dev->lock);
if (pxd_dev->removing)
err = -EBUSY;
else
pxd_dev->open_count++;
spin_unlock(&pxd_dev->lock);
if (!err)
(void)get_device(&pxd_dev->dev);
}
spin_unlock(&fc->lock);
trace_pxd_open(pxd_dev->dev_id, pxd_dev->major, pxd_dev->minor, mode, err);
return err;
}
static void pxd_release(struct gendisk *disk, fmode_t mode)
{
struct pxd_device *pxd_dev = disk->private_data;
spin_lock(&pxd_dev->lock);
BUG_ON(pxd_dev->magic != PXD_DEV_MAGIC);
pxd_dev->open_count--;
spin_unlock(&pxd_dev->lock);
trace_pxd_release(pxd_dev->dev_id, pxd_dev->major, pxd_dev->minor, mode);
put_device(&pxd_dev->dev);
}
static long pxd_ioctl_dump_fc_info(void)
{
int i;
struct pxd_context *ctx;
for (i = 0; i < pxd_num_contexts; ++i) {
ctx = &pxd_contexts[i];
if (ctx->num_devices == 0) {
continue;
}
printk(KERN_INFO "%s: pxd_ctx: %s ndevices: %lu",
__func__, ctx->name, ctx->num_devices);
printk(KERN_INFO "\tFC: connected: %d", READ_ONCE(ctx->fc.connected));
}
return 0;
}
static long pxd_ioctl_get_version(void __user *argp)
{
char ver_data[64];
int ver_len = 0;
if (argp) {
ver_len = strlen(gitversion) < 64 ? strlen(gitversion) : 64;
strncpy(ver_data, gitversion, ver_len);
if (copy_to_user(argp +
offsetof(struct pxd_ioctl_version_args, piv_len),
&ver_len, sizeof(ver_len))) {
return -EFAULT;
}
if (copy_to_user(argp +
offsetof(struct pxd_ioctl_version_args, piv_data),
ver_data, ver_len)) {
return -EFAULT;
}
}
return 0;
}
static long pxd_ioctl_init(struct file *file, void __user *argp)
{
struct pxd_context *ctx = container_of(file->f_op, struct pxd_context, fops);
struct iov_iter iter;
struct iovec iov = {argp, sizeof(struct pxd_ioctl_init_args)};
iov_iter_init(&iter, WRITE, &iov, 1, sizeof(struct pxd_ioctl_init_args));
return pxd_read_init(&ctx->fc, &iter);
}
static long pxd_ioctl_resize(struct file *file, void __user *argp)
{
struct pxd_context *ctx = NULL;
struct pxd_update_size update_args;
long ret = 0;
if (copy_from_user(&update_args, argp, sizeof(update_args))) {
return -EFAULT;
}
if (update_args.context_id >= pxd_num_contexts_exported) {
printk("%s : invalid context: %d\n", __func__, update_args.context_id);
return -EFAULT;
}
ctx = &pxd_contexts[update_args.context_id];
if (!ctx || ctx->id >= pxd_num_contexts_exported) {
return -EFAULT;
}
ret = pxd_ioc_update_size(&ctx->fc, &update_args);
return ret;
}
static long pxd_ioctl_fp_cleanup(struct file *file, void __user *argp)
{
struct pxd_context *ctx = NULL;
struct pxd_fastpath_out cleanup_args;
long ret = 0;
struct pxd_device *pxd_dev;
if (copy_from_user(&cleanup_args, argp, sizeof(cleanup_args))) {
return -EFAULT;
}
if (cleanup_args.context_id >= pxd_num_contexts_exported) {
printk("%s : invalid context: %d\n", __func__, cleanup_args.context_id);
return -EFAULT;
}
ctx = &pxd_contexts[cleanup_args.context_id];
if (!ctx || ctx->id >= pxd_num_contexts_exported) {
return -EFAULT;
}
pxd_dev = find_pxd_device(ctx, cleanup_args.dev_id);
if (pxd_dev != NULL) {
(void)get_device(&pxd_dev->dev);
ret = pxd_fastpath_vol_cleanup(pxd_dev);
put_device(&pxd_dev->dev);
}
return ret;
}
static void print_io_flusher_state(unsigned int new_flags,
pid_t pid, pid_t ppid, char *comm)
{
if (IS_SET_IO_FLUSHER_FLAG(new_flags)) {
printk("Process %s pid %d parent pid %d IO_FLUSHER is set\n",
comm, pid, ppid);
} else {
printk("Process %s pid %d parent pid %d IO_FLUSHER not set\n", comm, pid,
ppid);
}
}
static int is_io_flusher_supported(void)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0) && defined(PF_MEMALLOC_NOIO)
#if defined(PF_LESS_THROTTLE) || defined(PF_LOCAL_THROTTLE)
return 1;
#else
return 0;
#endif
#else
return 0;
#endif
}
static long pxd_ioflusher_state(void __user *argp)
{
struct pxd_ioctl_io_flusher_args io_flusher_args;
struct task_struct *task = NULL;
struct pid_namespace *ns = NULL;
pid_t ppid = -1;
pid_t pid = -1;
char *comm = NULL;
unsigned int old_flags = 0;
unsigned int new_flags = 0;
char command[TASK_COMM_LEN] = {0};
int is_io_flusher_set = 0;
if (!is_io_flusher_supported()) {
return -EOPNOTSUPP;
}
if (argp == NULL) {
return -EINVAL;
}
if (copy_from_user(&io_flusher_args, argp, sizeof(io_flusher_args))) {
return -EFAULT;
}
rcu_read_lock();
task = current;
ns = task_active_pid_ns(task);
if (io_flusher_args.pid == 0) {
get_task_struct(task);
} else {
struct task_struct *temp_task;
struct pid *pid = NULL;
pid = find_pid_ns(io_flusher_args.pid, ns);
if (pid == NULL) {
rcu_read_unlock();
printk("Input pid %d does not exist", io_flusher_args.pid);
return -ENOENT;
}
temp_task = pid_task(pid, PIDTYPE_PID);
if (temp_task == NULL) {
rcu_read_unlock();
printk("Input pid %d does not exist", io_flusher_args.pid);
return -ENOENT;
}
task = temp_task;
get_task_struct(task);
}
rcu_read_unlock();
ppid = task_pgrp_nr(task);
pid = task_pid_nr(task);
comm = get_task_comm(command, task);
old_flags = task->flags;
switch (io_flusher_args.io_flusher_action) {
case PXD_IO_FLUSHER_GET:
break;
case PXD_IO_FLUSHER_SET:
SET_IO_FLUSHER(task);
break;
case PXD_IO_FLUSHER_CLEAR:
CLEAR_IO_FLUSHER(task);
break;
}
new_flags = task->flags;
put_task_struct(task);
is_io_flusher_set = IS_SET_IO_FLUSHER_FLAG(new_flags);
print_io_flusher_state(new_flags, pid, ppid, comm);
if (copy_to_user(argp + offsetof(struct pxd_ioctl_io_flusher_args, is_io_flusher_set),
&is_io_flusher_set, sizeof(is_io_flusher_set))) {
return -EFAULT;
}
return 0;
}
static long pxd_control_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case PXD_IOC_DUMP_FC_INFO:
return pxd_ioctl_dump_fc_info();
case PXD_IOC_GET_VERSION:
return pxd_ioctl_get_version((void __user *)arg);
case PXD_IOC_INIT:
return pxd_ioctl_init(file, (void __user *)arg);
case PXD_IOC_RUN_USER_QUEUE:
return -ENOTTY;
case PXD_IOC_RESIZE:
return pxd_ioctl_resize(file, (void __user *)arg);
case PXD_IOC_FPCLEANUP:
return pxd_ioctl_fp_cleanup(file, (void __user *)arg);
case PXD_IOC_IO_FLUSHER:
return pxd_ioflusher_state((void __user *)arg);
default:
return -ENOTTY;
}
}
static const struct block_device_operations pxd_bd_ops = {
.owner = THIS_MODULE,
.open = pxd_open,
.release = pxd_release,
};
#if defined(__PXD_BIO_MAKEREQ__) && LINUX_VERSION_CODE >= KERNEL_VERSION(5,9,0)
static const struct block_device_operations pxd_bd_fpops = {
.owner = THIS_MODULE,
.open = pxd_open,
.release = pxd_release,
.submit_bio = pxd_bio_make_request_entryfn,
};
#define get_bd_fpops() (&pxd_bd_fpops)
#else
#define get_bd_fpops() (&pxd_bd_ops)
#endif
static bool __pxd_device_qfull(struct pxd_device *pxd_dev)
{
int ncount = PXD_ACTIVE(pxd_dev);
// does not care about async or sync request.
if (ncount > pxd_dev->qdepth) {
if (atomic_cmpxchg(&pxd_dev->congested, 0, 1) == 0) {
pxd_dev->nr_congestion_on++;
}
return 1;
}
if (atomic_cmpxchg(&pxd_dev->congested, 1, 0) == 1) {
pxd_dev->nr_congestion_off++;
}
return 0;
}
// congestion callback from kernel writeback module
int pxd_device_congested(void *data, int bits)
{
struct pxd_device *pxd_dev = data;
// notify congested if device is suspended as well.
// modified under lock, read outside lock.
if (atomic_read(&pxd_dev->fp.suspend)) {
return 1;
}
return __pxd_device_qfull(pxd_dev);
}
void pxd_check_q_congested(struct pxd_device *pxd_dev)
{
if (pxd_device_congested(pxd_dev, 0)) {
wait_event_interruptible(pxd_dev->suspend_wq,
!pxd_device_congested(pxd_dev, 0));
}
}
void pxd_check_q_decongested(struct pxd_device *pxd_dev)
{
if (!pxd_device_congested(pxd_dev, 0)) {
wake_up(&pxd_dev->suspend_wq);
}
}
static void pxd_request_complete(struct fuse_conn *fc, struct fuse_req *req, int status)
{
atomic_dec(&req->pxd_dev->ncount);
pxd_check_q_decongested(req->pxd_dev);
pxd_printk("%s: receive reply to %px(%lld) at %lld err %d\n",
__func__, req, req->in.unique,
req->pxd_rdwr_in.offset, status);
}
#ifdef __PXD_BIO_MAKEREQ__
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,11,0)
static void pxd_update_stats(struct fuse_req *req, int sgrp, unsigned int count)
#else
static void pxd_update_stats(struct fuse_req *req, int rw, unsigned int count)
#endif
{
struct pxd_device *pxd_dev = req->queue->queuedata;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,11,0) || defined(__EL8__)
{
struct block_device *p = pxd_dev->disk->part0;
if (!p) return;
part_stat_lock();
part_stat_add(p, sectors[sgrp], count);
part_stat_inc(p, ios[sgrp]);
}
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(5,0,0) || defined(__EL8__)
part_stat_lock();
part_stat_inc(&pxd_dev->disk->part0, ios[rw]);
part_stat_add(&pxd_dev->disk->part0, sectors[rw], count);
#else
int cpu = part_stat_lock();
part_stat_inc(cpu, &pxd_dev->disk->part0, ios[rw]);
part_stat_add(cpu, &pxd_dev->disk->part0, sectors[rw], count);
#endif
part_stat_unlock();
}
static bool pxd_process_read_reply(struct fuse_conn *fc, struct fuse_req *req,
int status)
{
trace_pxd_reply(req->in.unique, 0u);
pxd_update_stats(req, 0, BIO_SIZE(req->bio) / SECTOR_SIZE);
BIO_ENDIO(req->bio, status);
pxd_request_complete(fc, req, status);
return true;
}
static bool pxd_process_write_reply(struct fuse_conn *fc, struct fuse_req *req,
int status)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,8,0) || defined(REQ_PREFLUSH)
trace_pxd_reply(req->in.unique, REQ_OP_WRITE);
#else
trace_pxd_reply(req->in.unique, REQ_WRITE);
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,11,0)
{
const struct bio* bio = req->bio;
int statgrp = STAT_WRITE;
size_t sz = BIO_SIZE(bio) / SECTOR_SIZE;
if (!bio) statgrp = STAT_FLUSH;
else if (!op_is_write(bio->bi_opf)) statgrp = STAT_READ;
else if (op_is_flush(bio->bi_opf) && (sz == 0)) statgrp = STAT_FLUSH;
else statgrp = STAT_WRITE;
pxd_update_stats(req, statgrp, sz);
}
#else
pxd_update_stats(req, 1, BIO_SIZE(req->bio) / SECTOR_SIZE);
#endif
BIO_ENDIO(req->bio, status);
pxd_request_complete(fc, req, status);
return true;
}
#else
/* only used by the USE_REQUESTQ_MODEL definition */
static bool pxd_process_read_reply_q(struct fuse_conn *fc, struct fuse_req *req,
int status)
{
pxd_request_complete(fc, req, status);
#ifndef __PX_BLKMQ__
blk_end_request(req->rq, status, blk_rq_bytes(req->rq));
return true;
#else
blk_mq_end_request(req->rq, errno_to_blk_status(status));
return false;
#endif
}
/* only used by the USE_REQUESTQ_MODEL definition */
static bool pxd_process_write_reply_q(struct fuse_conn *fc, struct fuse_req *req,
int status)
{
pxd_request_complete(fc, req, status);
#ifndef __PX_BLKMQ__
blk_end_request(req->rq, status, blk_rq_bytes(req->rq));
return true;
#else
blk_mq_end_request(req->rq, errno_to_blk_status(status));
return false;
#endif
}
#endif
static struct fuse_req *pxd_fuse_req(struct pxd_device *pxd_dev)
{
int eintr = 0;
struct fuse_req *req = NULL;
struct fuse_conn *fc = &pxd_dev->ctx->fc;
int status;
while (req == NULL) {
req = fuse_get_req_for_background(fc);
if (IS_ERR(req) && PTR_ERR(req) == -EINTR) {
req = NULL;
++eintr;
}
}
if (eintr > 0) {
printk_ratelimited(KERN_INFO "%s: alloc EINTR retries %d",
__func__, eintr);
}
status = IS_ERR(req) ? PTR_ERR(req) : 0;
if (status != 0) {
printk_ratelimited(KERN_ERR "%s: request alloc failed: %d",
__func__, status);
}
return req;
}
static void pxd_req_misc(struct fuse_req *req, uint32_t size, uint64_t off,
uint32_t minor, uint32_t flags)
{
req->pxd_rdwr_in.dev_minor = minor;
req->pxd_rdwr_in.offset = off;
req->pxd_rdwr_in.size = size;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,8,0) || defined(REQ_PREFLUSH)
req->pxd_rdwr_in.flags =
((flags & REQ_FUA) ? PXD_FLAGS_FUA : 0) |
((flags & REQ_PREFLUSH) ? PXD_FLAGS_PREFLUSH : 0) |
((flags & REQ_META) ? PXD_FLAGS_META : 0);
#else
req->pxd_rdwr_in.flags = ((flags & REQ_FLUSH) ? PXD_FLAGS_PREFLUSH : 0) |
((flags & REQ_FUA) ? PXD_FLAGS_FUA : 0) |
((flags & REQ_META) ? PXD_FLAGS_META : 0);
#endif
}
/*
* when block device is registered in non blk mq mode, device limits are not
* honoured. Handle it appropriately.
*/
#ifdef __PXD_BIO_MAKEREQ__
static
int pxd_handle_device_limits(struct fuse_req *req, uint32_t *size, uint64_t *off,
unsigned int op)
{
struct request_queue *q = req->pxd_dev->disk->queue;
sector_t max_sectors, rq_sectors;
if (!fastpath_enabled(req->pxd_dev)) {
return 0;
}
rq_sectors = *size >> SECTOR_SHIFT;
BUG_ON(rq_sectors != bio_sectors(req->bio));
max_sectors = blk_queue_get_max_sectors(q, op);
if (!max_sectors) {
return -EOPNOTSUPP;
}
while (rq_sectors > max_sectors) {
struct bio *b;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,18,0) || \
(LINUX_VERSION_CODE >= KERNEL_VERSION(4,12,0) && \
defined(bvec_iter_sectors))
b = bio_split(req->bio, max_sectors, GFP_NOIO, &fs_bio_set);
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)
b = bio_split(req->bio, max_sectors, GFP_NOIO, fs_bio_set);
#else
// This issue has so far been seen only with 4.20 and 5.x kernels
// bio split signature way too different to be handled.
printk_ratelimited(KERN_ERR"device %llu IO queue limits (rq/max %lu/%lu sectors) exceeded\n",
req->pxd_dev->dev_id, rq_sectors, max_sectors);
return -EIO;
#endif
if (!b) {
return -ENOMEM;
}
bio_chain(b, req->bio);
#if LINUX_VERSION_CODE < KERNEL_VERSION(5,9,0)
generic_make_request(b);
#else
submit_bio_noacct(b);
#endif
rq_sectors -= max_sectors;
*off += (max_sectors << SECTOR_SHIFT);
}
*size = rq_sectors << SECTOR_SHIFT;
return 0;
}
#else
static inline
int pxd_handle_device_limits(struct fuse_req *req, uint32_t *size, uint64_t *off,
unsigned int op)
{
return 0;
}
#endif
static int pxd_read_request(struct fuse_req *req, uint32_t size, uint64_t off,
uint32_t minor, uint32_t flags)
{
int rc;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,8,0) || defined(REQ_PREFLUSH)
rc = pxd_handle_device_limits(req, &size, &off, REQ_OP_READ);
#else
rc = pxd_handle_device_limits(req, &size, &off, 0);
#endif
if (rc) {
return rc;
}
req->in.opcode = PXD_READ;
#ifdef __PXD_BIO_MAKEREQ__
req->end = pxd_process_read_reply;
#else
req->end = pxd_process_read_reply_q;
#endif
pxd_req_misc(req, size, off, minor, flags);
return 0;
}
static int pxd_write_request(struct fuse_req *req, uint32_t size, uint64_t off,
uint32_t minor, uint32_t flags)
{
int rc;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,8,0) || defined(REQ_PREFLUSH)
rc = pxd_handle_device_limits(req, &size, &off, REQ_OP_WRITE);
#else
rc = pxd_handle_device_limits(req, &size, &off, REQ_WRITE);
#endif
if (rc) {
return rc;
}
req->in.opcode = PXD_WRITE;
#ifdef __PXD_BIO_MAKEREQ__
req->end = pxd_process_write_reply;
#else
req->end = pxd_process_write_reply_q;
#endif
pxd_req_misc(req, size, off, minor, flags);
if (pxd_detect_zero_writes && req->pxd_rdwr_in.size != 0)
fuse_convert_zero_writes(req);
return 0;
}
static int pxd_discard_request(struct fuse_req *req, uint32_t size, uint64_t off,
uint32_t minor, uint32_t flags)
{
int rc;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,8,0) || defined(REQ_PREFLUSH)
rc = pxd_handle_device_limits(req, &size, &off, REQ_OP_DISCARD);
#else
rc = pxd_handle_device_limits(req, &size, &off, REQ_DISCARD);
#endif
if (rc) {
return rc;
}
req->in.opcode = PXD_DISCARD;
#ifdef __PXD_BIO_MAKEREQ__
req->end = pxd_process_write_reply;
#else
req->end = pxd_process_write_reply_q;
#endif
pxd_req_misc(req, size, off, minor, flags);
return 0;
}
static int pxd_write_same_request(struct fuse_req *req, uint32_t size, uint64_t off,
uint32_t minor, uint32_t flags)
{
int rc;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,8,0) || defined(REQ_PREFLUSH)
rc = pxd_handle_device_limits(req, &size, &off, REQ_OP_WRITE_SAME);
#else
rc = pxd_handle_device_limits(req, &size, &off, REQ_WRITE_SAME);
#endif
if (rc) {
return rc;
}
req->in.opcode = PXD_WRITE_SAME;
#ifdef __PXD_BIO_MAKEREQ__
req->end = pxd_process_write_reply;
#else
req->end = pxd_process_write_reply_q;
#endif
pxd_req_misc(req, size, off, minor, flags);
return 0;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,8,0) || defined(REQ_PREFLUSH)
static int pxd_request(struct fuse_req *req, uint32_t size, uint64_t off,
uint32_t minor, uint32_t op, uint32_t flags)
{
int rc;
trace_pxd_request(req->in.unique, size, off, minor, flags);
switch (op) {
case REQ_OP_WRITE_SAME:
rc = pxd_write_same_request(req, size, off, minor, flags);
break;
case REQ_OP_WRITE:
rc = pxd_write_request(req, size, off, minor, flags);
break;
case REQ_OP_READ:
rc = pxd_read_request(req, size, off, minor, flags);
break;
case REQ_OP_DISCARD:
rc = pxd_discard_request(req, size, off, minor, flags);
break;
case REQ_OP_FLUSH:
rc = pxd_write_request(req, 0, 0, minor, REQ_FUA);
break;
default:
printk(KERN_ERR"[%llu] REQ_OP_UNKNOWN(%#x): size=%d, off=%lld, minor=%d, flags=%#x\n",
req->in.unique, op, size, off, minor, flags);
return -1;
}
if (rc == 0) atomic_inc(&req->pxd_dev->ncount);
return rc;
}
#else
static int pxd_request(struct fuse_req *req, uint32_t size, uint64_t off,
uint32_t minor, uint32_t flags)
{
int rc;
trace_pxd_request(req->in.unique, size, off, minor, flags);
switch (flags & (REQ_WRITE | REQ_DISCARD | REQ_WRITE_SAME)) {
case REQ_WRITE:
/* FALLTHROUGH */
case (REQ_WRITE | REQ_WRITE_SAME):
if (flags & REQ_WRITE_SAME)
rc = pxd_write_same_request(req, size, off, minor, flags);
else
rc = pxd_write_request(req, size, off, minor, flags);
break;
case 0:
rc = pxd_read_request(req, size, off, minor, flags);
break;
case REQ_DISCARD:
/* FALLTHROUGH */
case REQ_WRITE | REQ_DISCARD:
rc = pxd_discard_request(req, size, off, minor, flags);
break;
default:
printk(KERN_ERR"[%llu] REQ_OP_UNKNOWN(%#x): size=%d, off=%lld, minor=%d, flags=%#x\n",
req->in.unique, flags, size, off, minor, flags);
return -1;
}
if (rc == 0) atomic_inc(&req->pxd_dev->ncount);
return rc;
}
#endif
static
bool pxd_process_ioswitch_complete(struct fuse_conn *fc, struct fuse_req *req,
int status)
{
struct pxd_device *pxd_dev = req->pxd_dev;
struct list_head ios;
unsigned long flags;
INIT_LIST_HEAD(&ios);
/// io path switch event completes with status.
printk("device %llu completed ioswitch %d with status %d\n",
pxd_dev->dev_id, req->in.opcode, status);
if (req->in.opcode == PXD_FAILOVER_TO_USERSPACE) {
// if the status is successful, then reissue IO to userspace
// else fail IO to complete.
disableFastPath(pxd_dev, true);
spin_lock_irqsave(&pxd_dev->fp.fail_lock, flags);
list_splice(&pxd_dev->fp.failQ, &ios);
INIT_LIST_HEAD(&pxd_dev->fp.failQ);
pxd_dev->fp.active_failover = false;
spin_unlock_irqrestore(&pxd_dev->fp.fail_lock, flags);
}
// reopen the suspended device
pxd_request_resume_internal(pxd_dev);
BUG_ON(atomic_read(&pxd_dev->fp.ioswitch_active) == 0);
atomic_set(&pxd_dev->fp.ioswitch_active, 0);
// reissue any failed IOs from local list
pxd_reissuefailQ(pxd_dev, &ios, status);
return true;
}
static
int pxd_initiate_ioswitch(struct pxd_device *pxd_dev, int code)
{
struct fuse_req *req;
if (!fastpath_enabled(pxd_dev)) {
return -EINVAL;
}
req = pxd_fuse_req(pxd_dev);
if (IS_ERR_OR_NULL(req)) {
return -ENOMEM;
}
req->pxd_dev = pxd_dev;
req->bio = NULL;
req->queue = pxd_dev->disk->queue;
req->in.opcode = code;
req->end = pxd_process_ioswitch_complete;
pxd_req_misc(req, 0, 0, pxd_dev->minor, PXD_FLAGS_SYNC);
fuse_request_send_nowait(&pxd_dev->ctx->fc, req);
return 0;
}
int pxd_initiate_failover(struct pxd_device *pxd_dev)
{
int rc;
if (!fastpath_active(pxd_dev)) {
// already in native path
return -EINVAL;
}
if (atomic_cmpxchg(&pxd_dev->fp.ioswitch_active, 0, 1) != 0) {
return 0; // already initiated, skip it.
}
rc = pxd_request_suspend_internal(pxd_dev, false, true);
if (rc) {
atomic_set(&pxd_dev->fp.ioswitch_active, 0);
return rc;
}
rc = pxd_initiate_ioswitch(pxd_dev, PXD_FAILOVER_TO_USERSPACE);
if (rc) {
pxd_request_resume(pxd_dev);
atomic_set(&pxd_dev->fp.ioswitch_active, 0);
}
return rc;
}
int pxd_initiate_fallback(struct pxd_device *pxd_dev)
{
int rc;
if (fastpath_active(pxd_dev)) {
// already in fast path
return -EINVAL;
}
if (atomic_cmpxchg(&pxd_dev->fp.ioswitch_active, 0, 1) != 0) {
return -EBUSY;
}
rc = pxd_request_suspend_internal(pxd_dev, true, false);
if (rc) {
atomic_set(&pxd_dev->fp.ioswitch_active, 0);
return rc;
}
rc = pxd_initiate_ioswitch(pxd_dev, PXD_FALLBACK_TO_KERNEL);
if (rc) {
pxd_request_resume_internal(pxd_dev);
atomic_set(&pxd_dev->fp.ioswitch_active, 0);
}
return rc;
}
// similar function to make_request_slowpath only optimized to ensure its a reroute
// from fastpath on IO fail.
void pxd_reroute_slowpath(struct request_queue *q, struct bio *bio)
{
struct pxd_device *pxd_dev = q->queuedata;
struct fuse_req *req;
req = pxd_fuse_req(pxd_dev);
if (IS_ERR_OR_NULL(req)) {
bio_io_error(bio);
return;
}
req->pxd_dev = pxd_dev;
req->bio = bio;
req->queue = q;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,8,0) || defined(REQ_PREFLUSH)
if (pxd_request(req, BIO_SIZE(bio), BIO_SECTOR(bio) * SECTOR_SIZE,
pxd_dev->minor, bio_op(bio), bio->bi_opf)) {
#else
if (pxd_request(req, BIO_SIZE(bio), BIO_SECTOR(bio) * SECTOR_SIZE,
pxd_dev->minor, bio->bi_rw)) {
#endif
fuse_request_free(req);
bio_io_error(bio);
return;
}
fuse_request_send_nowait(&pxd_dev->ctx->fc, req);
}
#ifdef __PXD_BIO_BLKMQ__
#if !defined(__PX_BLKMQ__)
void pxdmq_reroute_slowpath(struct fuse_req *req)
{
struct pxd_device *pxd_dev = req->pxd_dev;
struct request *rq = req->rq;
BUG_ON(pxd_dev->fp.fastpath);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0) || defined(REQ_PREFLUSH)
if (pxd_request(req, blk_rq_bytes(rq), blk_rq_pos(rq) * SECTOR_SIZE,
pxd_dev->minor, req_op(rq), rq->cmd_flags)) {
#else
if (pxd_request(req, blk_rq_bytes(rq), blk_rq_pos(rq) * SECTOR_SIZE,
pxd_dev->minor, rq->cmd_flags)) {
#endif
fuse_request_free(req);
blk_end_request(rq, -EIO, blk_rq_bytes(rq));
return;
}
fuse_request_send_nowait(&pxd_dev->ctx->fc, req);
}
static void pxd_rq_fn(struct request_queue *q)
{
struct pxd_device *pxd_dev = q->queuedata;
struct fuse_req *req;
struct fuse_conn *fc = &pxd_dev->ctx->fc;
for (;;) {
struct request *rq;