forked from portworx/px-fuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev.c
executable file
·1396 lines (1181 loc) · 33.6 KB
/
dev.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
/*
FUSE: Filesystem in Userspace
Copyright (C) 2001-2008 Miklos Szeredi <[email protected]>
This program can be distributed under the terms of the GNU GPL.
See the file COPYING.
*/
#include "fuse_i.h"
#include <linux/init.h>
#include <linux/module.h>
#include <linux/poll.h>
#include <linux/uio.h>
#include <linux/miscdevice.h>
#include <linux/pagemap.h>
#include <linux/file.h>
#include <linux/slab.h>
#include <linux/pipe_fs_i.h>
#include <linux/swap.h>
#include <linux/splice.h>
#include <linux/aio.h>
#include <linux/random.h>
#include <linux/version.h>
#include <linux/blkdev.h>
#include <linux/sort.h>
#include <linux/vmalloc.h>
#include "pxd_compat.h"
#include "pxd_core.h"
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0)
#define PAGE_CACHE_GET(page) get_page(page)
#define PAGE_CACHE_RELEASE(page) put_page(page)
#else
#define PAGE_CACHE_GET(page) page_cache_get(page)
#define PAGE_CACHE_RELEASE(page) page_cache_release(page)
#endif
#define FUSE_MAX_REQUEST_IDS (2 * FUSE_DEFAULT_MAX_BACKGROUND)
static struct kmem_cache *fuse_req_cachep;
static struct fuse_conn *fuse_get_conn(struct file *file)
{
/*
* Lockless access is OK, because file->private data is set
* once during mount and is valid until the file is released.
*/
return file->private_data;
}
void fuse_request_init(struct fuse_req *req)
{
memset(req, 0, sizeof(*req));
}
static struct fuse_req *__fuse_request_alloc(gfp_t flags)
{
struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, flags);
if (req) {
fuse_request_init(req);
}
return req;
}
struct fuse_req *fuse_request_alloc()
{
return __fuse_request_alloc(GFP_NOIO);
}
void fuse_request_free(struct fuse_req *req)
{
kmem_cache_free(fuse_req_cachep, req);
}
static struct fuse_req *__fuse_get_req(struct fuse_conn *fc)
{
struct fuse_req *req;
int err;
req = fuse_request_alloc();
if (!req) {
err = -ENOMEM;
goto out;
}
return req;
out:
return ERR_PTR(err);
}
struct fuse_req *fuse_get_req(struct fuse_conn *fc)
{
return __fuse_get_req(fc);
}
struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc)
{
return __fuse_get_req(fc);
}
static u64 fuse_get_unique(struct fuse_conn *fc)
{
struct fuse_per_cpu_ids *my_ids;
u64 uid;
int num_alloc;
int cpu = get_cpu();
my_ids = per_cpu_ptr(fc->per_cpu_ids, cpu);
if (unlikely(my_ids->num_free_ids == 0)) {
spin_lock(&fc->lock);
BUG_ON(fc->num_free_ids == 0);
num_alloc = min(fc->num_free_ids, (u32)FUSE_MAX_PER_CPU_IDS / 2);
memcpy(my_ids->free_ids, &fc->free_ids[fc->num_free_ids - num_alloc],
num_alloc * sizeof(u64));
fc->num_free_ids -= num_alloc;
spin_unlock(&fc->lock);
my_ids->num_free_ids = num_alloc;
}
uid = my_ids->free_ids[--my_ids->num_free_ids];
put_cpu();
uid += FUSE_MAX_REQUEST_IDS;
/* zero is special */
if (uid == 0)
uid += FUSE_MAX_REQUEST_IDS;
return uid;
}
static void fuse_put_unique(struct fuse_conn *fc, u64 uid)
{
struct fuse_per_cpu_ids *my_ids;
int num_free;
int cpu;
if (uid == 0) {
return;
}
cpu = get_cpu();
my_ids = per_cpu_ptr(fc->per_cpu_ids, cpu);
if (unlikely(my_ids->num_free_ids == FUSE_MAX_PER_CPU_IDS)) {
num_free = FUSE_MAX_PER_CPU_IDS / 2;
spin_lock(&fc->lock);
BUG_ON(fc->num_free_ids + num_free > FUSE_MAX_REQUEST_IDS);
memcpy(&fc->free_ids[fc->num_free_ids],
&my_ids->free_ids[my_ids->num_free_ids - num_free],
num_free * sizeof(u64));
fc->num_free_ids += num_free;
spin_unlock(&fc->lock);
my_ids->num_free_ids -= num_free;
}
my_ids->free_ids[my_ids->num_free_ids++] = uid;
fc->request_map[uid & (FUSE_MAX_REQUEST_IDS - 1)] = NULL;
put_cpu();
}
static struct rdwr_in *fuse_rdwr(struct fuse_conn *fc, uint32_t read)
{
return &fc->queue->requests[read & (FUSE_REQUEST_QUEUE_SIZE - 1)];
}
static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
{
u32 write;
struct rdwr_in *rdwr;
struct fuse_queue_cb *cb = &fc->queue->requests_cb;
req->in.unique = fuse_get_unique(fc);
fc->request_map[req->in.unique & (FUSE_MAX_REQUEST_IDS - 1)] = req;
spin_lock(&cb->w.lock);
write = cb->w.write;
if (write - cb->w.read >= FUSE_REQUEST_QUEUE_SIZE) {
cb->w.read = cb->r.read;
BUG_ON(write - cb->w.read >= FUSE_REQUEST_QUEUE_SIZE);
}
rdwr = fuse_rdwr(fc, write);
rdwr->in = req->in;
rdwr->rdwr = req->pxd_rdwr_in;
req->sequence = cb->w.sequence++;
cb->w.write = write + 1;
smp_store_release(&cb->r.write, write + 1);
spin_unlock(&cb->w.lock);
}
static void fuse_conn_wakeup(struct fuse_conn *fc)
{
wake_up(&fc->waitq);
kill_fasync(&fc->fasync, SIGIO, POLL_IN);
}
/*
* This function is called when a request is finished. Either a reply
* has arrived or it was aborted (and not yet sent) or some error
* occurred during communication with userspace, or the device file
* was closed. The requester thread is woken up (if still waiting),
* the 'end' callback is called if given, else the reference to the
* request is released
*/
static void request_end(struct fuse_conn *fc, struct fuse_req *req,
int status)
{
u64 uid = req->in.unique;
bool shouldfree = false;
/* 'req->end' is always set if the context gets used.
* if error happens during processing, error path handling does free request.
*/
if (req->end)
shouldfree = req->end(fc, req, status);
fuse_put_unique(fc, uid);
if (shouldfree) fuse_request_free(req);
}
void fuse_request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
{
/*
* Ensures checking the value of allow_disconnected and adding request to
* queue is done atomically.
*/
rcu_read_lock();
// 'allow_disconnected' check subsumes 'connected' as well
if (READ_ONCE(fc->allow_disconnected)) {
queue_request(fc, req);
rcu_read_unlock();
fuse_conn_wakeup(fc);
} else {
rcu_read_unlock();
request_end(fc, req, -ENOTCONN);
}
}
static bool request_pending(struct fuse_conn *fc)
{
struct fuse_queue_cb *cb = &fc->queue->requests_cb;
return cb->r.read != cb->r.write;
}
/* Wait until a request is available on the pending list */
static void request_wait(struct fuse_conn *fc)
{
DECLARE_WAITQUEUE(wait, current);
add_wait_queue_exclusive(&fc->waitq, &wait);
while (!request_pending(fc)) {
set_current_state(TASK_INTERRUPTIBLE);
if (signal_pending(current))
break;
schedule();
}
set_current_state(TASK_RUNNING);
remove_wait_queue(&fc->waitq, &wait);
}
extern uint32_t pxd_detect_zero_writes;
static bool __check_zero_page_write(char *base, size_t len) {
uint8_t wsize = sizeof(uint64_t);
char *p;
size_t i;
uint64_t *q;
p = base;
q = (uint64_t *)p;
for (i = 0; i < (len / wsize); i++) {
if (q[i]) {
return false;
}
}
for (i = len - (len % wsize); i < len; i++) {
if (p[i]) {
return false;
}
}
return true;
}
/* Check if the request is writing zeroes and if so, convert it as a discard
* request.
*/
#ifndef __PXD_BIO_MAKEREQ__
static void __fuse_convert_zero_writes(struct fuse_req *req)
{
struct req_iterator breq_iter;
#ifdef HAVE_BVEC_ITER
struct bio_vec bvec;
#else
struct bio_vec *bvec = NULL;
#endif
char *kaddr, *p;
size_t len;
rq_for_each_segment(bvec, req->rq, breq_iter) {
kaddr = kmap_atomic(BVEC(bvec).bv_page);
p = kaddr + BVEC(bvec).bv_offset;
len = BVEC(bvec).bv_len;
if (!__check_zero_page_write(p, len)) {
kunmap_atomic(kaddr);
return;
}
kunmap_atomic(kaddr);
}
req->in.opcode = PXD_DISCARD;
}
#else
static void __fuse_convert_zero_writes(struct fuse_req *req)
{
#if defined(HAVE_BVEC_ITER)
struct bvec_iter bvec_iter;
struct bio_vec bvec;
#else
int bvec_iter;
struct bio_vec *bvec = NULL;
#endif
char *kaddr, *p;
size_t len;
bio_for_each_segment(bvec, req->bio, bvec_iter) {
kaddr = kmap_atomic(BVEC(bvec).bv_page);
p = kaddr + BVEC(bvec).bv_offset;
len = BVEC(bvec).bv_len;
if (!__check_zero_page_write(p, len)) {
kunmap_atomic(kaddr);
return;
}
kunmap_atomic(kaddr);
}
req->in.opcode = PXD_DISCARD;
}
#endif
void fuse_convert_zero_writes(struct fuse_req *req)
{
__fuse_convert_zero_writes(req);
}
/*
* Read a single request into the userspace filesystem's buffer. This
* function waits until a request is available, then removes it from
* the pending list and copies request data to userspace buffer. If
* no reply is needed (FORGET) or request has been aborted or there
* was an error during the copying then it's finished by calling
* request_end(). Otherwise add it to the processing list, and set
* the 'sent' flag.
*/
static ssize_t fuse_dev_do_read(struct fuse_conn *fc, struct file *file,
struct iov_iter *iter)
{
ssize_t copied = 0, copied_this_time;
ssize_t remain = iter->count;
u32 read, write, idx;
struct fuse_queue_cb *cb = &fc->queue->requests_cb;
if (!request_pending(fc)) {
if ((file->f_flags & O_NONBLOCK))
return -EAGAIN;
request_wait(fc);
if (!request_pending(fc))
return -ERESTARTSYS;
}
retry:
read = cb->r.read;
write = smp_load_acquire(&cb->r.write);
while (read != write && remain >= sizeof(struct rdwr_in)) {
idx = read & (FUSE_REQUEST_QUEUE_SIZE - 1);
/* copy as many contiguous elements as possible */
copied_this_time = min(FUSE_REQUEST_QUEUE_SIZE - idx,
min(write - read, (u32)(remain / sizeof(struct rdwr_in)))) *
sizeof(struct rdwr_in);
if (copy_to_iter(fuse_rdwr(fc, read), copied_this_time, iter)
!= copied_this_time) {
printk(KERN_ERR "%s: copy error\n", __func__);
return -EFAULT;
}
read += copied_this_time / sizeof(struct rdwr_in);
copied += copied_this_time;
remain -= copied_this_time;
}
cb->r.read = read;
/* Check if more requests could be picked up */
if (remain && request_pending(fc))
goto retry;
return copied;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,0,0)
static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
unsigned long nr_segs, loff_t pos)
{
struct file *file = iocb->ki_filp;
struct fuse_conn *fc = fuse_get_conn(file);
struct iov_iter iter;
if (!fc)
return -EPERM;
iov_iter_init(&iter, READ, iov, nr_segs, iov_length(iov, nr_segs));
return fuse_dev_do_read(fc, file, &iter);
}
#else
static ssize_t fuse_dev_read_iter(struct kiocb *iocb, struct iov_iter *to)
{
struct file *file = iocb->ki_filp;
struct fuse_conn *fc = fuse_get_conn(file);
if (!fc)
return -EPERM;
return fuse_dev_do_read(fc, file, to);
}
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,14,0)
static int fuse_dev_pipe_buf_steal(struct pipe_inode_info *pipe,
struct pipe_buffer *buf)
{
return 1;
}
static const struct pipe_buf_operations fuse_dev_pipe_buf_ops = {
.can_merge = 0,
.map = generic_pipe_buf_map,
.unmap = generic_pipe_buf_unmap,
.confirm = generic_pipe_buf_confirm,
.release = generic_pipe_buf_release,
.steal = fuse_dev_pipe_buf_steal,
.get = generic_pipe_buf_get,
};
#endif
static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
struct pipe_inode_info *pipe,
size_t len, unsigned int flags)
{
return -EINVAL;
}
static int fuse_notify_add(struct fuse_conn *conn, unsigned int size,
struct iov_iter *iter)
{
struct pxd_add_out add;
struct pxd_add_ext_out add_ext;
size_t len = sizeof(add);
if (copy_from_iter(&add, len, iter) != len) {
printk(KERN_ERR "%s: can't copy arg\n", __func__);
return -EFAULT;
}
memset(&add_ext, 0, sizeof(add_ext));
memcpy(&add_ext, &add, sizeof(add));
add_ext.open_mode = O_LARGEFILE | O_RDWR | O_NOATIME; // default flags
return pxd_add(conn, &add_ext);
}
static int fuse_notify_add_ext(struct fuse_conn *conn, unsigned int size,
struct iov_iter *iter)
{
struct pxd_add_ext_out add;
size_t len = sizeof(add);
if (copy_from_iter(&add, len, iter) != len) {
printk(KERN_ERR "%s: can't copy arg\n", __func__);
return -EFAULT;
}
return pxd_add(conn, &add);
}
/* Look up request on processing list by unique ID */
static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
{
u32 index = unique & (FUSE_MAX_REQUEST_IDS - 1);
struct fuse_req *req = fc->request_map[index];
if (req == NULL) {
printk(KERN_ERR "no request unique %llx", unique);
return req;
}
if (req->in.unique != unique) {
printk(KERN_ERR "id mismatch got %llx need %llx", req->in.unique, unique);
return NULL;
}
return req;
}
struct fuse_req* request_find_in_ctx(unsigned ctx, u64 unique)
{
struct pxd_context *pctx = find_context(ctx);
if (!pctx) return NULL;
return request_find(&pctx->fc, unique);
}
#define IOV_BUF_SIZE 64
static int copy_in_read_data_iovec(struct iov_iter *iter,
struct pxd_read_data_out *read_data, struct iovec *iov,
struct iov_iter *data_iter)
{
int iovcnt;
size_t len;
if (!read_data->iovcnt)
return -EFAULT;
iovcnt = min(read_data->iovcnt, IOV_BUF_SIZE);
len = iovcnt * sizeof(struct iovec);
if (copy_from_iter(iov, len, iter) != len) {
printk(KERN_ERR "%s: can't copy iovec\n", __func__);
return -EFAULT;
}
read_data->iovcnt -= iovcnt;
iov_iter_init(data_iter, READ, iov, iovcnt, iov_length(iov, iovcnt));
return 0;
}
#ifndef __PXD_BIO_MAKEREQ__
static int __fuse_notify_read_data(struct fuse_conn *conn,
struct fuse_req *req,
struct pxd_read_data_out *read_data_p, struct iov_iter *iter)
{
struct iovec iov[IOV_BUF_SIZE];
struct iov_iter data_iter;
#ifdef HAVE_BVEC_ITER
struct bio_vec bvec;
#else
struct bio_vec *bvec = NULL;
#endif
struct req_iterator breq_iter;
size_t copied, skipped = 0;
int ret;
ret = copy_in_read_data_iovec(iter, read_data_p, iov, &data_iter);
if (ret)
return ret;
/* advance the iterator if data is unaligned */
if (unlikely(req->pxd_rdwr_in.offset & PXD_LBS_MASK))
iov_iter_advance(&data_iter,
req->pxd_rdwr_in.offset & PXD_LBS_MASK);
rq_for_each_segment(bvec, req->rq, breq_iter) {
ssize_t len = BVEC(bvec).bv_len;
copied = 0;
if (skipped < read_data_p->offset) {
if (read_data_p->offset - skipped >= len) {
skipped += len;
copied = len;
} else {
copied = read_data_p->offset - skipped;
skipped = read_data_p->offset;
}
}
if (copied < len) {
size_t copy_this = copy_page_to_iter(BVEC(bvec).bv_page,
BVEC(bvec).bv_offset + copied,
len - copied, &data_iter);
if (copy_this != len - copied) {
if (!iter->count)
return 0;
/* out of space in destination, copy more iovec */
ret = copy_in_read_data_iovec(iter, read_data_p,
iov, &data_iter);
if (ret)
return ret;
len -= copied;
copied = copy_page_to_iter(BVEC(bvec).bv_page,
BVEC(bvec).bv_offset + copied + copy_this,
len, &data_iter);
if (copied != len) {
printk(KERN_ERR "%s: copy failed new iovec\n",
__func__);
return -EFAULT;
}
}
}
}
return 0;
}
#else
static int __fuse_notify_read_data(struct fuse_conn *conn,
struct fuse_req *req,
struct pxd_read_data_out *read_data_p, struct iov_iter *iter)
{
struct iovec iov[IOV_BUF_SIZE];
struct iov_iter data_iter;
#ifdef HAVE_BVEC_ITER
struct bio_vec bvec;
struct bvec_iter bvec_iter;
#else
struct bio_vec *bvec = NULL;
int bvec_iter;
#endif
size_t copied, skipped = 0;
int ret;
ret = copy_in_read_data_iovec(iter, read_data_p, iov, &data_iter);
if (ret)
return ret;
/* advance the iterator if data is unaligned */
if (unlikely(req->pxd_rdwr_in.offset & PXD_LBS_MASK))
iov_iter_advance(&data_iter,
req->pxd_rdwr_in.offset & PXD_LBS_MASK);
bio_for_each_segment(bvec, req->bio, bvec_iter) {
ssize_t len = BVEC(bvec).bv_len;
copied = 0;
if (skipped < read_data_p->offset) {
if (read_data_p->offset - skipped >= len) {
skipped += len;
copied = len;
} else {
copied = read_data_p->offset - skipped;
skipped = read_data_p->offset;
}
}
if (copied < len) {
size_t copy_this = copy_page_to_iter(BVEC(bvec).bv_page,
BVEC(bvec).bv_offset + copied,
len - copied, &data_iter);
if (copy_this != len - copied) {
if (!iter->count)
return 0;
/* out of space in destination, copy more iovec */
ret = copy_in_read_data_iovec(iter, read_data_p,
iov, &data_iter);
if (ret)
return ret;
len -= copied;
copied = copy_page_to_iter(BVEC(bvec).bv_page,
BVEC(bvec).bv_offset + copied + copy_this,
len, &data_iter);
if (copied != len) {
printk(KERN_ERR "%s: copy failed new iovec\n",
__func__);
return -EFAULT;
}
}
}
}
return 0;
}
#endif
static int fuse_notify_read_data(struct fuse_conn *conn, unsigned int size,
struct iov_iter *iter)
{
struct pxd_read_data_out read_data;
size_t len = sizeof(read_data);
struct fuse_req *req;
if (copy_from_iter(&read_data, len, iter) != len) {
printk(KERN_ERR "%s: can't copy read_data arg\n", __func__);
return -EFAULT;
}
req = request_find(conn, read_data.unique);
if (!req) {
printk(KERN_ERR "%s: request %lld not found\n", __func__,
read_data.unique);
return -ENOENT;
}
if (req->in.opcode != PXD_WRITE &&
req->in.opcode != PXD_WRITE_SAME) {
printk(KERN_ERR "%s: request is not a write\n", __func__);
return -EINVAL;
}
return __fuse_notify_read_data(conn, req, &read_data, iter);
}
static int fuse_notify_remove(struct fuse_conn *conn, unsigned int size,
struct iov_iter *iter)
{
struct pxd_remove_out remove;
size_t len = sizeof(remove);
if (copy_from_iter(&remove, len, iter) != len) {
printk(KERN_ERR "%s: can't copy arg\n", __func__);
return -EFAULT;
}
return pxd_remove(conn, &remove);
}
static int fuse_notify_update_size(struct fuse_conn *conn, unsigned int size,
struct iov_iter *iter)
{
struct pxd_update_size update_size;
size_t len = sizeof(update_size);
if (copy_from_iter(&update_size, len, iter) != len) {
printk(KERN_ERR "%s: can't copy arg\n", __func__);
return -EFAULT;
}
return pxd_update_size(conn, &update_size);
}
static int fuse_notify_get_features(struct fuse_conn *conn, unsigned int size,
struct iov_iter *iter) {
return pxd_supported_features();
}
static int fuse_notify_suspend(struct fuse_conn *conn, unsigned int size,
struct iov_iter *iter) {
struct pxd_context *ctx = container_of(conn, struct pxd_context, fc);
struct pxd_suspend req;
size_t len = sizeof(req);
struct pxd_device *pxd_dev;
if (copy_from_iter(&req, len, iter) != len) {
printk(KERN_ERR "%s: can't copy arg\n", __func__);
return -EFAULT;
}
pxd_dev = find_pxd_device(ctx, req.dev_id);
if (!pxd_dev) {
printk(KERN_ERR "device %llu not found\n", req.dev_id);
return -EINVAL;
}
return pxd_request_suspend(pxd_dev, req.skip_flush, req.coe);
}
static int fuse_notify_resume(struct fuse_conn *conn, unsigned int size,
struct iov_iter *iter) {
struct pxd_context *ctx = container_of(conn, struct pxd_context, fc);
struct pxd_resume req;
size_t len = sizeof(req);
struct pxd_device *pxd_dev;
if (copy_from_iter(&req, len, iter) != len) {
printk(KERN_ERR "%s: can't copy arg\n", __func__);
return -EFAULT;
}
pxd_dev = find_pxd_device(ctx, req.dev_id);
if (!pxd_dev) {
printk(KERN_ERR "device %llu not found\n", req.dev_id);
return -EINVAL;
}
return pxd_request_resume(pxd_dev);
}
static int fuse_notify_ioswitch_event(struct fuse_conn *conn, unsigned int size,
struct iov_iter *iter, bool failover) {
struct pxd_context *ctx = container_of(conn, struct pxd_context, fc);
struct pxd_ioswitch req;
size_t len = sizeof(req);
struct pxd_device *pxd_dev;
if (copy_from_iter(&req, len, iter) != len) {
printk(KERN_ERR "%s: can't copy arg\n", __func__);
return -EFAULT;
}
pxd_dev = find_pxd_device(ctx, req.dev_id);
if (!pxd_dev) {
printk(KERN_ERR "device %llu not found\n", req.dev_id);
return -EINVAL;
}
return pxd_request_ioswitch(pxd_dev,
failover ? PXD_FAILOVER_TO_USERSPACE : PXD_FALLBACK_TO_KERNEL);
}
static int fuse_notify_export(struct fuse_conn *conn, unsigned int size,
struct iov_iter *iter)
{
uint64_t dev_id;
size_t len = sizeof(dev_id);
if (copy_from_iter(&dev_id, len, iter) != len) {
printk(KERN_ERR "%s: can't copy arg\n", __func__);
return -EFAULT;
}
return pxd_export(conn, dev_id);
}
static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
unsigned int size, struct iov_iter *iter)
{
switch ((int)code) {
case PXD_READ_DATA:
return fuse_notify_read_data(fc, size, iter);
case PXD_ADD:
return fuse_notify_add(fc, size, iter);
case PXD_REMOVE:
return fuse_notify_remove(fc, size, iter);
case PXD_UPDATE_SIZE:
return fuse_notify_update_size(fc, size, iter);
case PXD_ADD_EXT:
return fuse_notify_add_ext(fc, size, iter);
case PXD_GET_FEATURES:
return fuse_notify_get_features(fc, size, iter);
case PXD_SUSPEND:
return fuse_notify_suspend(fc, size, iter);
case PXD_RESUME:
return fuse_notify_resume(fc, size, iter);
case PXD_FAILOVER_TO_USERSPACE:
return fuse_notify_ioswitch_event(fc, size, iter, true);
case PXD_FALLBACK_TO_KERNEL:
return fuse_notify_ioswitch_event(fc, size, iter, false);
case PXD_EXPORT_DEV:
return fuse_notify_export(fc, size, iter);
default:
return -EINVAL;
}
}
/*
* Write a single reply to a request. First the header is copied from
* the write buffer. The request is then searched on the processing
* list by the unique ID found in the header. If found, then remove
* it from the list and copy the rest of the buffer to the request.
* The request is finished by calling request_end()
*/
#ifndef __PXD_BIO_MAKEREQ__
static int __fuse_dev_do_write(struct fuse_conn *fc,
struct fuse_req *req, struct iov_iter *iter)
{
if (req->in.opcode == PXD_READ && iter->count > 0) {
#ifdef HAVE_BVEC_ITER
struct bio_vec bvec;
#else
struct bio_vec *bvec = NULL;
#endif
struct request *breq = req->rq;
struct req_iterator breq_iter;
int nsegs = breq->nr_phys_segments;
if (nsegs) {
int i = 0;
rq_for_each_segment(bvec, breq, breq_iter) {
ssize_t len = BVEC(bvec).bv_len;
if (copy_page_from_iter(BVEC(bvec).bv_page,
BVEC(bvec).bv_offset,
len, iter) != len) {
printk(KERN_ERR "%s: copy page %d of %d error\n",
__func__, i, nsegs);
return -EFAULT;
}
i++;
}
}
}
return 0;
}
#else
static int __fuse_dev_do_write(struct fuse_conn *fc,
struct fuse_req *req, struct iov_iter *iter)
{
#if defined(HAVE_BVEC_ITER)
struct bio_vec bvec;
struct bio *breq = req->bio;
int nsegs = bio_segments(breq);
struct bvec_iter bvec_iter;
#else
struct bio_vec *bvec = NULL;
struct bio *breq = req->bio;
int nsegs = bio_segments(breq);
int bvec_iter;
#endif
if (req->in.opcode == PXD_READ && iter->count > 0) {
if (nsegs) {
int i = 0;
bio_for_each_segment(bvec, breq, bvec_iter) {
ssize_t len = BVEC(bvec).bv_len;
if (copy_page_from_iter(BVEC(bvec).bv_page,
BVEC(bvec).bv_offset,
len, iter) != len) {
printk(KERN_ERR "%s: copy page %d of %d error\n",
__func__, i, nsegs);
return -EFAULT;
}
i++;
}
}
}
return 0;
}
#endif
static ssize_t fuse_dev_do_write(struct fuse_conn *fc, struct iov_iter *iter)
{
int err;
struct fuse_req *req;
struct fuse_out_header oh;
size_t len;
size_t nbytes = iter->count;
if (iter->count < sizeof(struct fuse_out_header))
return -EINVAL;
len = sizeof(oh);
if (copy_from_iter(&oh, len, iter) != len) {
printk(KERN_ERR "%s: can't copy header\n", __func__);
return -EFAULT;
}
if (oh.len != nbytes)
return -EINVAL;
/*
* Zero oh.unique indicates unsolicited notification message
* and error contains notification code.
*/
if (!oh.unique) {
err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), iter);
return err ? err : nbytes;
}
if (oh.error <= -1000 || oh.error > 0)
return -EINVAL;
req = request_find(fc, oh.unique);
if (!req) {
printk(KERN_ERR "%s: request %lld not found\n", __func__, oh.unique);
return -ENOENT;
}
err = __fuse_dev_do_write(fc, req, iter);
if (err)
return err;
request_end(fc, req, oh.error);
return nbytes;
}
void fuse_user_complete(struct fuse_conn *fc, uint64_t unique, uint64_t user_data,
int res)
{
struct fuse_queue_cb *cb = &fc->queue->requests_cb;
uint32_t write;
struct rdwr_in *rdwr;
spin_lock(&cb->w.lock);
write = cb->w.write;
if (write - cb->w.read >= FUSE_REQUEST_QUEUE_SIZE) {
cb->w.read = cb->r.read;
BUG_ON(write - cb->w.read >= FUSE_REQUEST_QUEUE_SIZE);
}
rdwr = fuse_rdwr(fc, write);
rdwr->in.opcode = PXD_COMPLETE;
rdwr->in.unique = unique;
rdwr->completion.res = res;
rdwr->completion.user_data = user_data;
smp_store_release(&cb->r.write, write + 1);
cb->w.write = write + 1;
spin_unlock(&cb->w.lock);
fuse_conn_wakeup(fc);
}