-
Notifications
You must be signed in to change notification settings - Fork 10
/
io.c
3152 lines (2685 loc) · 73.4 KB
/
io.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
/*
* Shared application/kernel submission and completion ring pairs, for
* supporting fast/efficient IO.
*
* A note on the read/write ordering memory barriers that are matched between
* the application and kernel side.
*
* After the application reads the CQ ring tail, it must use an
* appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
* before writing the tail (using smp_load_acquire to read the tail will
* do). It also needs a smp_mb() before updating CQ head (ordering the
* entry load(s) with the head store), pairing with an implicit barrier
* through a control-dependency in io_get_cqring (smp_store_release to
* store head will do). Failure to do so could lead to reading invalid
* CQ entries.
*
* Likewise, the application must use an appropriate smp_wmb() before
* writing the SQ tail (ordering SQ entry stores with the tail store),
* which pairs with smp_load_acquire in io_get_sqring (smp_store_release
* to store the tail will do). And it needs a barrier ordering the SQ
* head load before writing new SQ entries (smp_load_acquire to read
* head will do).
*
* When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
* needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
* updating the SQ tail; a full memory barrier smp_mb() is needed
* between.
*
* Also see the examples in the liburing library:
*
* git://git.kernel.dk/liburing
*
* io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
* from data shared between the kernel and application. This is done both
* for ordering purposes, but also to ensure that once a value is loaded from
* data that the application could potentially modify, it remains stable.
*
* Copyright (C) 2018-2019 Jens Axboe
* Copyright (c) 2018-2019 Christoph Hellwig
*/
#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0)
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/syscalls.h>
#include <linux/compat.h>
#include <linux/refcount.h>
#include <linux/uio.h>
#include <linux/sched/signal.h>
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/fdtable.h>
#include <linux/mm.h>
#include <linux/mman.h>
#include <linux/mmu_context.h>
#include <linux/percpu.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
#include <linux/kthread.h>
#include <linux/blkdev.h>
#include <linux/bvec.h>
#include <linux/net.h>
#include <net/sock.h>
#include <net/af_unix.h>
#include <net/scm.h>
#include <linux/anon_inodes.h>
#include <linux/sched/mm.h>
#include <linux/uaccess.h>
#include <linux/nospec.h>
#include <linux/sizes.h>
#include <linux/hugetlb.h>
#include <linux/blkdev.h>
#include "pxd_io_uring.h"
#include "io.h"
#include "fuse_i.h"
#include "pxd_core.h"
#include "pxd_compat.h"
#include <uapi/linux/eventpoll.h>
#include <linux/net.h>
#define IORING_MAX_ENTRIES 4096
#define IORING_MAX_FIXED_FILES 4096
#ifndef SZ_4G
#define SZ_4G (1ULL << 32)
#endif
struct io_uring {
u32 head ____cacheline_aligned_in_smp;
u32 tail ____cacheline_aligned_in_smp;
};
/*
* This data is shared with the application through the mmap at offset
* IORING_OFF_SQ_RING.
*
* The offsets to the member fields are published through struct
* io_sqring_offsets when calling io_uring_setup.
*/
struct io_sq_ring {
/*
* Head and tail offsets into the ring; the offsets need to be
* masked to get valid indices.
*
* The kernel controls head and the application controls tail.
*/
struct io_uring r;
/*
* Bitmask to apply to head and tail offsets (constant, equals
* ring_entries - 1)
*/
u32 ring_mask;
/* Ring size (constant, power of 2) */
u32 ring_entries;
/*
* Number of invalid entries dropped by the kernel due to
* invalid index stored in array
*
* Written by the kernel, shouldn't be modified by the
* application (i.e. get number of "new events" by comparing to
* cached value).
*
* After a new SQ head value was read by the application this
* counter includes all submissions that were dropped reaching
* the new SQ head (and possibly more).
*/
u32 dropped;
/*
* Runtime flags
*
* Written by the kernel, shouldn't be modified by the
* application.
*
* The application needs a full memory barrier before checking
* for IORING_SQ_NEED_WAKEUP after updating the sq tail.
*/
u32 flags;
/*
* Ring buffer of indices into array of io_uring_sqe, which is
* mmapped by the application using the IORING_OFF_SQES offset.
*
* This indirection could e.g. be used to assign fixed
* io_uring_sqe entries to operations and only submit them to
* the queue when needed.
*
* The kernel modifies neither the indices array nor the entries
* array.
*/
u32 array[];
};
/*
* This data is shared with the application through the mmap at offset
* IORING_OFF_CQ_RING.
*
* The offsets to the member fields are published through struct
* io_cqring_offsets when calling io_uring_setup.
*/
struct io_cq_ring {
/*
* Head and tail offsets into the ring; the offsets need to be
* masked to get valid indices.
*
* The application controls head and the kernel tail.
*/
struct io_uring r;
/*
* Bitmask to apply to head and tail offsets (constant, equals
* ring_entries - 1)
*/
u32 ring_mask;
/* Ring size (constant, power of 2) */
u32 ring_entries;
/*
* Number of completion events lost because the queue was full;
* this should be avoided by the application by making sure
* there are not more requests pending thatn there is space in
* the completion queue.
*
* Written by the kernel, shouldn't be modified by the
* application (i.e. get number of "new events" by comparing to
* cached value).
*
* As completion events come in out of order this counter is not
* ordered with any other data.
*/
u32 overflow;
/*
* Ring buffer of completion events.
*
* The kernel writes completion events fresh every time they are
* produced, so the application is allowed to modify pending
* entries.
*/
struct io_uring_cqe cqes[];
};
#define IO_PLUG_THRESHOLD 2
#define IO_IOPOLL_BATCH 8
struct io_submit_state {
struct blk_plug plug;
/*
* io_kiocb alloc cache
*/
void *reqs[IO_IOPOLL_BATCH];
unsigned int free_reqs;
unsigned int cur_req;
/*
* File reference cache
*/
struct file *file;
unsigned int fd;
unsigned int has_refs;
unsigned int used_refs;
unsigned int ios_left;
};
static void io_sq_wq_submit_work(struct work_struct *work);
struct kmem_cache *req_cachep;
static void *io_alloc_msg_buf(struct io_ring_ctx *ctx)
{
if (ctx->nr_msg_bufs == 0) {
return NULL;
}
--ctx->nr_msg_bufs;
return ctx->msg_bufs[ctx->nr_msg_bufs];
}
static void io_ring_ctx_ref_free(struct percpu_ref *ref)
{
struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
complete(&ctx->ctx_done);
}
static size_t queue_size(struct io_ring_ctx *ctx)
{
return sizeof(struct fuse_queue_cb) + sizeof(struct fuse_queue_cb) +
ctx->sq_entries * sizeof(struct io_uring_sqe) +
ctx->cq_entries * sizeof(struct io_uring_cqe);
}
static int io_ring_ctx_init(struct io_ring_ctx *ctx, struct io_uring_params *params)
{
memset(ctx, 0, sizeof(*ctx));
ctx->flags = params->flags;
ctx->sq_thread_idle = params->sq_thread_idle;
params->sq_entries = params->sq_entries == 0 ?
FUSE_REQUEST_QUEUE_SIZE : roundup_pow_of_two(
params->sq_entries);
params->cq_entries = params->cq_entries == 0 ?
FUSE_REQUEST_QUEUE_SIZE : roundup_pow_of_two(
params->cq_entries);
ctx->sq_entries = params->sq_entries;
ctx->sq_mask = ctx->sq_entries - 1;
ctx->cq_entries = params->cq_entries;
ctx->cq_mask = ctx->cq_entries - 1;
ctx->queue = vmalloc((queue_size(ctx) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1));
if (!ctx->queue) {
printk(KERN_ERR "failed to allocate request queue");
return -ENOMEM;
}
ctx->requests_cb = ctx->queue;
ctx->requests = (void *)(ctx->requests_cb + 1);
ctx->responses_cb = (void *)(ctx->requests + ctx->sq_entries);
ctx->responses = (void *)(ctx->responses_cb + 1);
pr_info("queue size %ld requests_cb %ld requests %ld responses_cb %ld responses %ld",
queue_size(ctx), (void *)ctx->requests_cb - ctx->queue,
(void *)ctx->requests - ctx->queue,
(void *)ctx->responses_cb - ctx->queue,
(void *)ctx->responses - ctx->queue);
fuse_queue_init_cb(ctx->requests_cb);
fuse_queue_init_cb(ctx->responses_cb);
ctx->user_files = kcalloc(IORING_MAX_FIXED_FILES, sizeof(struct file *),
GFP_KERNEL);
if (ctx->user_files == NULL) {
vfree(ctx->queue);
return -ENOMEM;
}
ctx->nr_user_files = IORING_MAX_FIXED_FILES;
ctx->nr_msg_bufs = 0;
ctx->msg_bufs = kcalloc(PXD_IO_MAX_MSG_BUFS, sizeof(void *), GFP_KERNEL);
if (ctx->msg_bufs == NULL) {
vfree(ctx->queue);
kfree(ctx->user_files);
}
if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, 0, GFP_KERNEL)) {
vfree(ctx->queue);
kfree(ctx->user_files);
kfree(ctx->msg_bufs);
return -ENOMEM;
}
init_waitqueue_head(&ctx->wait);
init_completion(&ctx->ctx_done);
mutex_init(&ctx->uring_lock);
init_waitqueue_head(&ctx->cq_wait);
spin_lock_init(&ctx->completion_lock);
INIT_LIST_HEAD(&ctx->poll_list);
INIT_LIST_HEAD(&ctx->cancel_list);
INIT_LIST_HEAD(&ctx->defer_list);
INIT_LIST_HEAD(&ctx->sock_poll_list);
return 0;
}
static inline bool io_sequence_defer(struct io_ring_ctx *ctx,
struct io_kiocb *req)
{
if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
return false;
return req->sequence > ctx->cached_cq_tail;
}
static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
{
struct io_kiocb *req;
if (list_empty(&ctx->defer_list))
return NULL;
req = list_first_entry(&ctx->defer_list, struct io_kiocb, list);
if (!io_sequence_defer(ctx, req)) {
list_del_init(&req->list);
return req;
}
return NULL;
}
static void __io_commit_cqring(struct io_ring_ctx *ctx)
{
struct fuse_queue_cb *cb = ctx->responses_cb;
if (ctx->cached_cq_tail != READ_ONCE(cb->r.write)) {
/* order cqe stores with ring update */
smp_store_release(&cb->r.write, ctx->cached_cq_tail);
if (wq_has_sleeper(&ctx->cq_wait)) {
wake_up_interruptible(&ctx->cq_wait);
kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
}
}
}
static void io_commit_cqring(struct io_ring_ctx *ctx)
{
struct io_kiocb *req;
__io_commit_cqring(ctx);
while ((req = io_get_deferred_req(ctx)) != NULL) {
req->flags |= REQ_F_IO_DRAINED;
queue_work(ctx->sqo_wq, &req->work);
}
}
static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
{
struct fuse_queue_cb *cb = ctx->responses_cb;
unsigned tail;
tail = ctx->cached_cq_tail;
/*
* writes to the cq entry need to come after reading head; the
* control dependency is enough as we're using WRITE_ONCE to
* fill the cq entry
*/
if (tail - READ_ONCE(cb->r.read) == ctx->cq_entries)
return NULL;
ctx->cached_cq_tail++;
return &ctx->responses[tail & ctx->cq_mask];
}
static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
long res)
{
struct io_uring_cqe *cqe;
cqe = io_get_cqring(ctx);
BUG_ON(!cqe);
WRITE_ONCE(cqe->user_data, ki_user_data);
WRITE_ONCE(cqe->res, res);
WRITE_ONCE(cqe->flags, 0);
}
static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
long res)
{
struct fuse_queue_cb *cb = ctx->responses_cb;
unsigned long flags;
spin_lock_irqsave(&cb->w.lock, flags);
io_cqring_fill_event(ctx, user_data, res);
io_commit_cqring(ctx);
spin_unlock_irqrestore(&cb->w.lock, flags);
}
static void io_ring_drop_ctx_refs(struct io_ring_ctx *ctx, unsigned refs)
{
percpu_ref_put_many(&ctx->refs, refs);
if (waitqueue_active(&ctx->wait))
wake_up(&ctx->wait);
}
static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
struct io_submit_state *state)
{
gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
struct io_kiocb *req;
if (!percpu_ref_tryget(&ctx->refs))
return NULL;
if (!state) {
req = kmem_cache_alloc(req_cachep, gfp);
if (unlikely(!req))
goto out;
} else if (!state->free_reqs) {
size_t sz;
int ret;
sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
/*
* Bulk alloc is all-or-nothing. If we fail to get a batch,
* retry single alloc to be on the safe side.
*/
if (unlikely(ret <= 0)) {
state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
if (!state->reqs[0])
goto out;
ret = 1;
}
state->free_reqs = ret - 1;
state->cur_req = 1;
req = state->reqs[0];
} else {
req = state->reqs[state->cur_req];
state->free_reqs--;
state->cur_req++;
}
req->file = NULL;
req->ctx = ctx;
req->flags = 0;
/* one is dropped after submission, the other at completion */
refcount_set(&req->refs, 2);
return req;
out:
io_ring_drop_ctx_refs(ctx, 1);
return NULL;
}
static void io_free_req(struct io_kiocb *req)
{
if (req->file && !(req->flags & REQ_F_FIXED_FILE))
fput(req->file);
io_ring_drop_ctx_refs(req->ctx, 1);
kmem_cache_free(req_cachep, req);
}
static void io_put_req(struct io_kiocb *req)
{
if (refcount_dec_and_test(&req->refs))
io_free_req(req);
}
static void kiocb_end_write(struct kiocb *kiocb)
{
if (kiocb->ki_flags & IOCB_WRITE) {
struct inode *inode = file_inode(kiocb->ki_filp);
/*
* Tell lockdep we inherited freeze protection from submission
* thread.
*/
if (S_ISREG(inode->i_mode))
__sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
file_end_write(kiocb->ki_filp);
}
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,16,0)
static void io_complete_rw(struct kiocb *kiocb, long res)
#else
static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
#endif
{
struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
kiocb_end_write(kiocb);
io_cqring_add_event(req->ctx, req->user_data, res);
io_put_req(req);
}
/*
* If we tracked the file through the SCM inflight mechanism, we could support
* any file. For now, just ensure that anything potentially problematic is done
* inline.
*/
static bool io_file_supports_async(struct file *file)
{
umode_t mode = file_inode(file)->i_mode;
if (S_ISBLK(mode) || S_ISCHR(mode))
return true;
if (S_ISREG(mode) && file->f_op != &fuse_dev_operations)
return true;
return false;
}
static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
bool force_nonblock)
{
const struct io_uring_sqe *sqe = s->sqe;
struct kiocb *kiocb = &req->rw;
int ret;
if (!req->file) {
return -EBADF;
}
if (force_nonblock && !io_file_supports_async(req->file))
force_nonblock = false;
kiocb->ki_pos = READ_ONCE(sqe->off);
kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
kiocb->ki_hint = file_write_hint(kiocb->ki_filp);
ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
if (unlikely(ret)) {
pr_info("%s: kiocb_set_rw_flags %d", __func__, ret);
return ret;
}
/* don't allow async punt if RWF_NOWAIT was requested */
if (kiocb->ki_flags & IOCB_NOWAIT)
req->flags |= REQ_F_NOWAIT;
if (force_nonblock)
kiocb->ki_flags |= IOCB_NOWAIT;
if (kiocb->ki_flags & IOCB_HIPRI) {
pr_info("%s: ki_flags has HIPRI", __func__);
return -EINVAL;
}
kiocb->ki_complete = io_complete_rw;
return 0;
}
static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
{
switch (ret) {
case -EIOCBQUEUED:
return;
case -ERESTARTSYS:
case -ERESTARTNOINTR:
case -ERESTARTNOHAND:
case -ERESTART_RESTARTBLOCK:
/*
* We can't just restart the syscall, since previously
* submitted sqes may already be in progress. Just fail this
* IO with EINTR.
*/
ret = -EINTR;
break;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,16,0)
kiocb->ki_complete(kiocb, ret);
#else
kiocb->ki_complete(kiocb, ret, 0);
#endif
}
static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
const struct io_uring_sqe *sqe,
struct iov_iter *iter)
{
size_t len = READ_ONCE(sqe->len);
struct io_mapped_ubuf *imu;
unsigned index, buf_index;
size_t offset;
u64 buf_addr;
/* attempt to use fixed buffers without having provided iovecs */
if (unlikely(!ctx->user_bufs))
return -EFAULT;
buf_index = READ_ONCE(sqe->buf_index);
if (unlikely(buf_index >= ctx->nr_user_bufs))
return -EFAULT;
index = array_index_nospec(buf_index, ctx->nr_user_bufs);
imu = &ctx->user_bufs[index];
buf_addr = READ_ONCE(sqe->addr);
/* overflow */
if (buf_addr + len < buf_addr)
return -EFAULT;
/* not inside the mapped region */
if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
return -EFAULT;
/*
* May not be a start of buffer, set size appropriately
* and advance us to the beginning.
*/
offset = buf_addr - imu->ubuf;
iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
if (offset)
iov_iter_advance(iter, offset);
return 0;
}
static int io_import_iovec(struct io_ring_ctx *ctx, int rw,
const struct sqe_submit *s, struct iovec **iovec,
struct iov_iter *iter)
{
const struct io_uring_sqe *sqe = s->sqe;
void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
size_t sqe_len = READ_ONCE(sqe->len);
u8 opcode;
/*
* We're reading ->opcode for the second time, but the first read
* doesn't care whether it's _FIXED or not, so it doesn't matter
* whether ->opcode changes concurrently. The first read does care
* about whether it is a READ or a WRITE, so we don't trust this read
* for that purpose and instead let the caller pass in the read/write
* flag.
*/
opcode = READ_ONCE(sqe->opcode);
if (opcode == IORING_OP_READ_FIXED ||
opcode == IORING_OP_WRITE_FIXED) {
int ret = io_import_fixed(ctx, rw, sqe, iter);
*iovec = NULL;
return ret;
}
if (!s->has_user)
return -EFAULT;
return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
}
static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
bool force_nonblock)
{
struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
struct kiocb *kiocb = &req->rw;
struct iov_iter iter;
struct file *file;
size_t iov_count;
int ret;
ssize_t ret2;
if (force_nonblock && (s->sqe->flags & IOSQE_FORCE_ASYNC)) {
return -EAGAIN;
}
ret = io_prep_rw(req, s, force_nonblock);
if (ret)
return ret;
file = kiocb->ki_filp;
if (unlikely(!(file->f_mode & FMODE_READ)))
return -EBADF;
if (unlikely(!file->f_op->read_iter))
return -EINVAL;
ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
if (ret < 0)
return ret;
iov_count = iov_iter_count(&iter);
/* Catch -EAGAIN return for forced non-blocking submission */
ret2 = call_read_iter(file, kiocb, &iter);
if (!force_nonblock || ret2 != -EAGAIN) {
io_rw_done(kiocb, ret2);
ret = 0;
} else {
ret = -EAGAIN;
}
kfree(iovec);
return ret;
}
static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
bool force_nonblock)
{
struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
struct kiocb *kiocb = &req->rw;
struct iov_iter iter;
struct file *file;
size_t iov_count;
int ret;
ssize_t ret2;
ret = io_prep_rw(req, s, force_nonblock);
if (ret) {
pr_info("%s: io prep rw fail: %d", __func__, ret);
return ret;
}
file = kiocb->ki_filp;
if (unlikely(!(file->f_mode & FMODE_WRITE)))
return -EBADF;
if (unlikely(!file->f_op->write_iter)) {
pr_info("%s: write iter is NULL", __func__);
return -EINVAL;
}
ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
if (ret < 0)
return ret;
iov_count = iov_iter_count(&iter);
ret = -EAGAIN;
if (force_nonblock &&
((s->sqe->flags & IOSQE_FORCE_ASYNC) || !(kiocb->ki_flags & IOCB_DIRECT))) {
goto out_free;
}
/*
* Open-code file_start_write here to grab freeze protection,
* which will be released by another thread in
* io_complete_rw(). Fool lockdep by telling it the lock got
* released so that it doesn't complain about the held lock when
* we return to userspace.
*/
if (S_ISREG(file_inode(file)->i_mode)) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,10,0)
__sb_start_write(file_inode(file)->i_sb,
SB_FREEZE_WRITE);
#else
__sb_start_write(file_inode(file)->i_sb,
SB_FREEZE_WRITE, true);
#endif
__sb_writers_release(file_inode(file)->i_sb,
SB_FREEZE_WRITE);
}
kiocb->ki_flags |= IOCB_WRITE;
ret2 = call_write_iter(file, kiocb, &iter);
if (!force_nonblock || ret2 != -EAGAIN) {
io_rw_done(kiocb, ret2);
ret = 0;
} else {
ret = -EAGAIN;
}
out_free:
kfree(iovec);
return ret;
}
// return nr_bytes in iovec if successful
// < 0 for failure
#ifndef __PXD_BIO_MAKEREQ__
static int build_bvec(struct fuse_req *req, int *rw, size_t off, size_t len,
struct bio_vec **iovec, struct iov_iter *iter)
{
struct request *rq = req->rq;
int nr_bvec = 0;
struct bio_vec *bvec = NULL;
struct bio_vec *alloc_bvec = NULL;
struct bio *bio = rq->bio;
struct req_iterator rq_iter;
struct bio_vec tmp;
size_t offset = 0;
size_t skip;
bool map_end = false;
size_t map_len = len;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,4,0)
rq_for_each_bvec(tmp, rq, rq_iter) {
#else
rq_for_each_segment(tmp, rq, rq_iter) {
#endif
nr_bvec++;
}
if (nr_bvec > UIO_FASTIOV) {
alloc_bvec = bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec),
GFP_NOIO);
} else {
alloc_bvec = bvec = *iovec;
}
if (!bvec)
return -EIO;
skip = off - (BIO_SECTOR(bio) << SECTOR_SHIFT);
nr_bvec = 0;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,4,0)
rq_for_each_bvec(tmp, rq, rq_iter) {
#else
rq_for_each_segment(tmp, rq, rq_iter) {
#endif
if (!tmp.bv_len) continue;
if (skip >= tmp.bv_len) {
skip -= tmp.bv_len;
continue;
} else if (!map_end) {
size_t bvlen = tmp.bv_len - skip;
map_end = true;
nr_bvec++;
offset = skip;
if (bvlen >= map_len) {
tmp.bv_len = map_len;
*bvec = tmp;
break;
}
*bvec = tmp;
bvec++;
map_len -= bvlen;
skip = 0;
} else {
nr_bvec++;
if (map_len <= tmp.bv_len) {
tmp.bv_len = map_len;
*bvec = tmp;
break;
}
*bvec = tmp;
bvec++;
map_len -= tmp.bv_len;
}
}
bvec = alloc_bvec;
*rw = bio_data_dir(bio);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,20,0)
iov_iter_bvec(iter, bio_data_dir(bio), bvec, nr_bvec, len);
#else
iov_iter_bvec(iter, ITER_BVEC | bio_data_dir(bio), bvec, nr_bvec, len);
#endif
iter->iov_offset = offset;
return len;
}
#else
static int build_bvec(struct fuse_req *req, int *rw, size_t off, size_t len,
struct bio_vec **iovec, struct iov_iter *iter)
{
struct bio *bio = req->bio;
int nr_bvec = bio->bi_vcnt;
struct bio_vec *bvec = NULL;
struct bio_vec *alloc_bvec = NULL;
struct bvec_iter bv_iter;
struct bio_vec bv;
size_t offset = 0;
size_t skip;
bool map_end = false;
size_t map_len = len;
if (nr_bvec > UIO_FASTIOV) {
alloc_bvec = bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec),
GFP_NOIO);
} else {
alloc_bvec = bvec = *iovec;
}
if (!bvec)
return -EIO;
nr_bvec = 0;
skip = off - (BIO_SECTOR(bio) << SECTOR_SHIFT);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,1,0)
bio_for_each_bvec(bv, bio, bv_iter) {
#else
bio_for_each_segment(bv, bio, bv_iter) {
#endif
if (!bv.bv_len) continue;
if (skip >= bv.bv_len) {
skip -= bv.bv_len;
continue;
} else if (!map_end) {
size_t bvlen = bv.bv_len - skip;
map_end = true;
nr_bvec++;
offset = skip;
if (bvlen >= map_len) {
bv.bv_len = map_len;
*bvec = bv;
break;
}
*bvec = bv;
bvec++;
map_len -= bvlen;
skip = 0;
} else {
nr_bvec++;
if (map_len <= bv.bv_len) {
bv.bv_len = map_len;
*bvec = bv;
break;
}
*bvec = bv;
bvec++;
map_len -= bv.bv_len;
}
}
bvec = alloc_bvec;
*rw = bio_data_dir(bio);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,20,0)
iov_iter_bvec(iter, bio_data_dir(bio), bvec, nr_bvec, len);
#else
iov_iter_bvec(iter, ITER_BVEC | bio_data_dir(bio), bvec, nr_bvec, len);
#endif
iter->iov_offset = offset;
return len;
}
#endif
static int io_import_bvec(struct io_kiocb *req, int *rw,
const struct sqe_submit *s, struct bio_vec **iovec,
struct iov_iter *iter)
{
const struct io_uring_sqe *sqe = s->sqe;
size_t sqe_off = req->rw.ki_pos;
size_t sqe_len = READ_ONCE(sqe->len);
uint64_t unique_id = READ_ONCE(sqe->addr);
uint32_t conn_id = READ_ONCE(sqe->buf_index);
struct fuse_req *freq;
if (!s->has_user)
return -EFAULT;
freq = request_find_in_ctx(conn_id, unique_id);
if (!freq) {
printk(KERN_ERR "%s: request %u:%lld not found\n", __func__, conn_id, unique_id);
return -ENOENT;
}
return build_bvec(freq, rw, sqe_off, sqe_len, iovec, iter);
}
static int io_switch(struct io_kiocb *req, const struct sqe_submit *s,
int dir, bool force_nonblock)
{
struct bio_vec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
struct kiocb *kiocb = &req->rw;
struct iov_iter iter;
struct file *file;
size_t iov_count;
int ret;
ssize_t ret2;
int rw;
ret = io_prep_rw(req, s, force_nonblock);
if (ret) {
pr_info("%s: io prep rw fail: %d", __func__, ret);
return ret;
}
file = kiocb->ki_filp;
if (unlikely(!(file->f_mode & FMODE_WRITE)))
return -EBADF;
if (unlikely(!file->f_op->write_iter)) {
pr_info("%s: write iter is NULL", __func__);
return -EINVAL;
}
ret = io_import_bvec(req, &rw, s, &iovec, &iter);
if (ret < 0)
goto out_free;
if (rw != dir) {
ret = -EINVAL;
pr_info("%s: invalid direction", __func__);