Repository navigation
Expand file tree
/
Copy pathdev.c
More file actions
2355 lines (1979 loc) · 54.3 KB
/
Copy pathdev.c
File metadata and controls
2355 lines (1979 loc) · 54.3 KB
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
/*
FUSE: Filesystem in Userspace
Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
*/
#include "dev.h"
#include "args.h"
#include "dev_uring_i.h"
#include <linux/init.h>
#include <linux/module.h>
#include <linux/poll.h>
#include <linux/sched/signal.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/sched.h>
#include <linux/seq_file.h>
#include "fuse_trace.h"
MODULE_ALIAS_MISCDEV(FUSE_MINOR);
MODULE_ALIAS("devname:fuse");
static DECLARE_WAIT_QUEUE_HEAD(fuse_dev_waitq);
static struct kmem_cache *fuse_req_cachep;
static void fuse_request_init(struct fuse_chan *fch, struct fuse_req *req)
{
INIT_LIST_HEAD(&req->list);
INIT_LIST_HEAD(&req->intr_entry);
init_waitqueue_head(&req->waitq);
refcount_set(&req->count, 1);
__set_bit(FR_PENDING, &req->flags);
req->chan = fch;
req->create_time = jiffies;
}
static struct fuse_req *fuse_request_alloc(struct fuse_chan *fch, gfp_t flags)
{
struct fuse_req *req = kmem_cache_zalloc(fuse_req_cachep, flags);
if (req)
fuse_request_init(fch, req);
return req;
}
static void fuse_request_free(struct fuse_req *req)
{
WARN_ON(!list_empty(&req->intr_entry));
kmem_cache_free(fuse_req_cachep, req);
}
static void __fuse_get_request(struct fuse_req *req)
{
refcount_inc(&req->count);
}
/* Must be called with > 1 refcount */
static void __fuse_put_request(struct fuse_req *req)
{
refcount_dec(&req->count);
}
void fuse_chan_set_initialized(struct fuse_chan *fch, struct fuse_chan_param *param)
{
if (param) {
fch->minor = param->minor;
fch->max_write = param->max_write;
fch->max_pages = param->max_pages;
if (param->io_uring_enabled)
fuse_uring_conn_init(fch);
}
/* Pairs with smp_load_acquire() readers of fch->initialized */
smp_store_release(&fch->initialized, 1);
wake_up_all(&fch->blocked_waitq);
}
static bool fuse_block_alloc(struct fuse_chan *fch, bool for_background)
{
/* Pairs with smp_store_release() in fuse_chan_set_initialized() */
if (!smp_load_acquire(&fch->initialized))
return true;
return (for_background && fch->blocked) ||
(fch->io_uring && fch->connected && !fuse_uring_ready(fch));
}
static void fuse_drop_waiting(struct fuse_chan *fch)
{
/*
* lockess check of fch->connected is okay, because atomic_dec_and_test()
* provides a memory barrier matched with the one in fuse_chan_wait_aborted()
* to ensure no wake-up is missed.
*/
if (atomic_dec_and_test(&fch->num_waiting) &&
!READ_ONCE(fch->connected)) {
/* wake up aborters */
wake_up_all(&fch->blocked_waitq);
}
}
static void fuse_put_request(struct fuse_req *req);
static struct fuse_req *fuse_get_req(struct fuse_chan *fch, bool for_background)
{
struct fuse_req *req;
int err;
atomic_inc(&fch->num_waiting);
if (fuse_block_alloc(fch, for_background)) {
err = -EINTR;
if (wait_event_state_exclusive(fch->blocked_waitq,
!fuse_block_alloc(fch, for_background),
(TASK_KILLABLE | TASK_FREEZABLE)))
goto out;
}
err = -ENOTCONN;
if (!fch->connected)
goto out;
req = fuse_request_alloc(fch, GFP_KERNEL);
err = -ENOMEM;
if (!req) {
if (for_background)
wake_up(&fch->blocked_waitq);
goto out;
}
__set_bit(FR_WAITING, &req->flags);
if (for_background)
__set_bit(FR_BACKGROUND, &req->flags);
return req;
out:
fuse_drop_waiting(fch);
return ERR_PTR(err);
}
static void fuse_put_request(struct fuse_req *req)
{
struct fuse_chan *fch = req->chan;
if (refcount_dec_and_test(&req->count)) {
if (test_bit(FR_BACKGROUND, &req->flags)) {
/*
* We get here in the unlikely case that a background
* request was allocated but not sent
*/
spin_lock(&fch->bg_lock);
if (!fch->blocked)
wake_up(&fch->blocked_waitq);
spin_unlock(&fch->bg_lock);
}
if (test_bit(FR_WAITING, &req->flags)) {
__clear_bit(FR_WAITING, &req->flags);
fuse_drop_waiting(fch);
}
fuse_request_free(req);
}
}
unsigned int fuse_len_args(unsigned int numargs, struct fuse_arg *args)
{
unsigned nbytes = 0;
unsigned i;
for (i = 0; i < numargs; i++)
nbytes += args[i].size;
return nbytes;
}
EXPORT_SYMBOL_GPL(fuse_len_args);
static u64 fuse_get_unique_locked(struct fuse_iqueue *fiq)
{
fiq->reqctr += FUSE_REQ_ID_STEP;
return fiq->reqctr;
}
u64 fuse_get_unique(struct fuse_iqueue *fiq)
{
u64 ret;
spin_lock(&fiq->lock);
ret = fuse_get_unique_locked(fiq);
spin_unlock(&fiq->lock);
return ret;
}
EXPORT_SYMBOL_GPL(fuse_get_unique);
unsigned int fuse_req_hash(u64 unique)
{
return hash_long(unique & ~FUSE_INT_REQ_BIT, FUSE_PQ_HASH_BITS);
}
EXPORT_SYMBOL_GPL(fuse_req_hash);
/*
* A new request is available, wake fiq->waitq
*/
static void fuse_dev_wake_and_unlock(struct fuse_iqueue *fiq, bool sync)
__releases(fiq->lock)
{
if (sync)
wake_up_sync(&fiq->waitq);
else
wake_up(&fiq->waitq);
kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
spin_unlock(&fiq->lock);
}
struct fuse_forget_link *fuse_alloc_forget(void)
{
return kzalloc_obj(struct fuse_forget_link, GFP_KERNEL_ACCOUNT);
}
void fuse_dev_queue_forget(struct fuse_iqueue *fiq,
struct fuse_forget_link *forget)
{
spin_lock(&fiq->lock);
if (fiq->connected) {
fiq->forget_list_tail->next = forget;
fiq->forget_list_tail = forget;
fuse_dev_wake_and_unlock(fiq, false);
} else {
kfree(forget);
spin_unlock(&fiq->lock);
}
}
void fuse_dev_queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
{
spin_lock(&fiq->lock);
/* Repeat FR_SENT test after obtaining the lock to prevent race with fuse_resend() */
if (list_empty(&req->intr_entry) && test_bit(FR_SENT, &req->flags)) {
list_add_tail(&req->intr_entry, &fiq->interrupts);
/*
* Pairs with smp_mb() implied by test_and_set_bit()
* from fuse_request_end().
*/
smp_mb();
if (test_bit(FR_FINISHED, &req->flags)) {
list_del_init(&req->intr_entry);
spin_unlock(&fiq->lock);
} else {
fuse_dev_wake_and_unlock(fiq, false);
}
} else {
spin_unlock(&fiq->lock);
}
}
static inline void fuse_request_assign_unique_locked(struct fuse_iqueue *fiq,
struct fuse_req *req)
{
if (req->in.h.opcode != FUSE_NOTIFY_REPLY)
req->in.h.unique = fuse_get_unique_locked(fiq);
/* tracepoint captures in.h.unique and in.h.len */
trace_fuse_request_send(req);
}
inline void fuse_request_assign_unique(struct fuse_iqueue *fiq,
struct fuse_req *req)
{
if (req->in.h.opcode != FUSE_NOTIFY_REPLY)
req->in.h.unique = fuse_get_unique(fiq);
/* tracepoint captures in.h.unique and in.h.len */
trace_fuse_request_send(req);
}
EXPORT_SYMBOL_GPL(fuse_request_assign_unique);
static void fuse_dev_queue_req(struct fuse_iqueue *fiq, struct fuse_req *req)
{
bool sync = test_and_clear_bit(FR_SYNC_WAKEUP, &req->flags);
spin_lock(&fiq->lock);
if (fiq->connected) {
fuse_request_assign_unique_locked(fiq, req);
list_add_tail(&req->list, &fiq->pending);
fuse_dev_wake_and_unlock(fiq, sync);
} else {
spin_unlock(&fiq->lock);
req->out.h.error = -ENOTCONN;
clear_bit(FR_PENDING, &req->flags);
fuse_request_end(req);
}
}
static const struct fuse_iqueue_ops fuse_dev_fiq_ops = {
.send_forget = fuse_dev_queue_forget,
.send_interrupt = fuse_dev_queue_interrupt,
.send_req = fuse_dev_queue_req,
};
void fuse_iqueue_init(struct fuse_iqueue *fiq, const struct fuse_iqueue_ops *ops, void *priv)
{
spin_lock_init(&fiq->lock);
init_waitqueue_head(&fiq->waitq);
INIT_LIST_HEAD(&fiq->pending);
INIT_LIST_HEAD(&fiq->interrupts);
fiq->forget_list_tail = &fiq->forget_list_head;
fiq->connected = 1;
fiq->ops = ops;
fiq->priv = priv;
}
EXPORT_SYMBOL_GPL(fuse_iqueue_init);
void fuse_chan_release(struct fuse_chan *fch)
{
struct fuse_iqueue *fiq = &fch->iq;
if (fiq->ops->release)
fiq->ops->release(fiq);
if (fch->timeout.req_timeout)
cancel_delayed_work_sync(&fch->timeout.work);
}
void fuse_chan_free(struct fuse_chan *fch)
{
WARN_ON(!list_empty(&fch->devices));
kfree(fch->pq_prealloc);
kfree(fch);
}
EXPORT_SYMBOL_GPL(fuse_chan_free);
struct fuse_chan *fuse_chan_new(void)
{
struct fuse_chan *fch = kzalloc_obj(struct fuse_chan);
if (!fch)
return NULL;
spin_lock_init(&fch->lock);
INIT_LIST_HEAD(&fch->devices);
spin_lock_init(&fch->bg_lock);
INIT_LIST_HEAD(&fch->bg_queue);
init_waitqueue_head(&fch->blocked_waitq);
atomic_set(&fch->num_waiting, 0);
fch->max_background = FUSE_DEFAULT_MAX_BACKGROUND;
fch->initialized = 0;
fch->blocked = 0;
fch->connected = 1;
fch->timeout.req_timeout = 0;
return fch;
}
EXPORT_SYMBOL_GPL(fuse_chan_new);
struct list_head *fuse_pqueue_alloc(void)
{
struct list_head *pq = kzalloc_objs(struct list_head, FUSE_PQ_HASH_SIZE);
if (pq) {
for (int i = 0; i < FUSE_PQ_HASH_SIZE; i++)
INIT_LIST_HEAD(&pq[i]);
}
return pq;
}
struct fuse_chan *fuse_dev_chan_new(void)
{
struct fuse_chan *fch __free(kfree) = fuse_chan_new();
if (!fch)
return NULL;
fch->pq_prealloc = fuse_pqueue_alloc();
if (!fch->pq_prealloc)
return NULL;
fuse_iqueue_init(&fch->iq, &fuse_dev_fiq_ops, NULL);
return no_free_ptr(fch);
}
EXPORT_SYMBOL_GPL(fuse_dev_chan_new);
unsigned int fuse_chan_num_background(struct fuse_chan *fch)
{
return READ_ONCE(fch->num_background);
}
unsigned int fuse_chan_max_background(struct fuse_chan *fch)
{
return READ_ONCE(fch->max_background);
}
void fuse_chan_max_background_set(struct fuse_chan *fch, unsigned int val)
{
spin_lock(&fch->bg_lock);
fch->max_background = val;
fch->blocked = fch->num_background >= fch->max_background;
if (!fch->blocked)
wake_up_nr(&fch->blocked_waitq,
fch->max_background - fch->num_background);
spin_unlock(&fch->bg_lock);
}
unsigned int fuse_chan_num_waiting(struct fuse_chan *fch)
{
return atomic_read(&fch->num_waiting);
}
void fuse_chan_set_fc(struct fuse_chan *fch, struct fuse_conn *fc)
{
fch->conn = fc;
}
void fuse_pqueue_init(struct fuse_pqueue *fpq)
{
spin_lock_init(&fpq->lock);
INIT_LIST_HEAD(&fpq->io);
fpq->connected = 1;
fpq->processing = NULL;
}
static struct fuse_dev *fuse_dev_alloc_no_pq(void)
{
struct fuse_dev *fud;
fud = kzalloc_obj(struct fuse_dev);
if (!fud)
return NULL;
refcount_set(&fud->ref, 1);
fuse_pqueue_init(&fud->pq);
return fud;
}
struct fuse_dev *fuse_dev_alloc(void)
{
struct fuse_dev *fud __free(kfree) = fuse_dev_alloc_no_pq();
if (!fud)
return NULL;
fud->pq.processing = fuse_pqueue_alloc();
if (!fud->pq.processing)
return NULL;
return no_free_ptr(fud);
}
EXPORT_SYMBOL_GPL(fuse_dev_alloc);
/*
* Installs @fch into @fud, return true on success. "Consumes" @pq in either case.
*/
static bool fuse_dev_install_with_pq(struct fuse_dev *fud, struct fuse_chan *fch,
struct list_head *pq)
{
struct fuse_chan *old_fch;
guard(spinlock)(&fch->lock);
/* protect fud->pq.processing assignment */
scoped_guard(spinlock, &fud->pq.lock) {
/*
* Pairs with:
* - xchg() in fuse_dev_release()
* - smp_load_acquire() in fuse_dev_fc_get()
*/
old_fch = cmpxchg(&fud->chan, NULL, fch);
if (old_fch) {
/*
* failed to set fud->chan because
* - it was already set to a different fc
* - it was set to disconneted
*/
kfree(pq);
return false;
}
if (pq) {
WARN_ON(fud->pq.processing);
fud->pq.processing = pq;
}
}
list_add_tail(&fud->entry, &fch->devices);
fuse_conn_get(fch->conn);
wake_up_all(&fuse_dev_waitq);
return true;
}
void fuse_dev_install(struct fuse_dev *fud, struct fuse_chan *fch)
{
struct list_head *pq = fch->pq_prealloc;
fch->pq_prealloc = NULL;
if (!fuse_dev_install_with_pq(fud, fch, pq)) {
/* Channel is not usable without a dev */
fuse_chan_abort(fch, false);
}
}
EXPORT_SYMBOL_GPL(fuse_dev_install);
struct fuse_dev *fuse_dev_alloc_install(struct fuse_chan *fch)
{
struct fuse_dev *fud;
fud = fuse_dev_alloc_no_pq();
if (!fud)
return NULL;
fuse_dev_install(fud, fch);
return fud;
}
EXPORT_SYMBOL_GPL(fuse_dev_alloc_install);
void fuse_dev_put(struct fuse_dev *fud)
{
struct fuse_chan *fch;
if (!refcount_dec_and_test(&fud->ref))
return;
fch = fuse_dev_chan_get(fud);
if (fch && fch != FUSE_DEV_CHAN_DISCONNECTED) {
/* This is the virtiofs case (fuse_dev_release() not called) */
spin_lock(&fch->lock);
list_del(&fud->entry);
spin_unlock(&fch->lock);
fuse_conn_put(fch->conn);
}
kfree(fud->pq.processing);
kfree(fud);
}
EXPORT_SYMBOL_GPL(fuse_dev_put);
bool fuse_dev_is_installed(struct fuse_dev *fud)
{
struct fuse_chan *fch = fuse_dev_chan_get(fud);
return fch != NULL && fch != FUSE_DEV_CHAN_DISCONNECTED;
}
/*
* Checks if @fc matches the one installed in @fud
*/
bool fuse_dev_verify(struct fuse_dev *fud, struct fuse_chan *fch)
{
return fuse_dev_chan_get(fud) == fch;
}
bool fuse_dev_is_sync_init(struct fuse_dev *fud)
{
return fud->sync_init;
}
struct fuse_dev *fuse_dev_grab(struct file *file)
{
struct fuse_dev *fud = fuse_file_to_fud(file);
refcount_inc(&fud->ref);
return fud;
}
static void fuse_send_one(struct fuse_iqueue *fiq, struct fuse_req *req)
{
req->in.h.len = sizeof(struct fuse_in_header) +
fuse_len_args(req->args->in_numargs,
(struct fuse_arg *) req->args->in_args);
fiq->ops->send_req(fiq, req);
}
void fuse_chan_queue_forget(struct fuse_chan *fch, struct fuse_forget_link *forget,
u64 nodeid, u64 nlookup)
{
struct fuse_iqueue *fiq = &fch->iq;
forget->forget_one.nodeid = nodeid;
forget->forget_one.nlookup = nlookup;
fiq->ops->send_forget(fiq, forget);
}
static void flush_bg_queue(struct fuse_chan *fch)
{
struct fuse_iqueue *fiq = &fch->iq;
while (fch->active_background < fch->max_background &&
!list_empty(&fch->bg_queue)) {
struct fuse_req *req;
req = list_first_entry(&fch->bg_queue, struct fuse_req, list);
list_del(&req->list);
fch->active_background++;
fuse_send_one(fiq, req);
}
}
void fuse_request_bg_finish(struct fuse_chan *fch, struct fuse_req *req)
{
lockdep_assert_held(&fch->bg_lock);
clear_bit(FR_BACKGROUND, &req->flags);
if (fch->num_background == fch->max_background) {
fch->blocked = 0;
wake_up(&fch->blocked_waitq);
} else if (!fch->blocked) {
/*
* Wake up next waiter, if any. It's okay to use
* waitqueue_active(), as we've already synced up
* fch->blocked with waiters with the wake_up() call
* above.
*/
if (waitqueue_active(&fch->blocked_waitq))
wake_up(&fch->blocked_waitq);
}
fch->num_background--;
fch->active_background--;
}
/*
* 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
*/
void fuse_request_end(struct fuse_req *req)
{
struct fuse_chan *fch = req->chan;
struct fuse_iqueue *fiq = &fch->iq;
if (test_and_set_bit(FR_FINISHED, &req->flags))
goto put_request;
trace_fuse_request_end(req);
/*
* test_and_set_bit() implies smp_mb() between bit
* changing and below FR_INTERRUPTED check. Pairs with
* smp_mb() from queue_interrupt().
*/
if (test_bit(FR_INTERRUPTED, &req->flags)) {
spin_lock(&fiq->lock);
list_del_init(&req->intr_entry);
spin_unlock(&fiq->lock);
}
WARN_ON(test_bit(FR_PENDING, &req->flags));
WARN_ON(test_bit(FR_SENT, &req->flags));
if (test_bit(FR_BACKGROUND, &req->flags)) {
spin_lock(&fch->bg_lock);
fuse_request_bg_finish(fch, req);
flush_bg_queue(fch);
spin_unlock(&fch->bg_lock);
} else {
/* Wake up waiter sleeping in request_wait_answer() */
wake_up(&req->waitq);
}
if (test_bit(FR_ASYNC, &req->flags))
req->args->end(req->args, req->out.h.error);
put_request:
fuse_put_request(req);
}
EXPORT_SYMBOL_GPL(fuse_request_end);
static int queue_interrupt(struct fuse_req *req)
{
struct fuse_iqueue *fiq = &req->chan->iq;
/* Check for we've sent request to interrupt this req */
if (unlikely(!test_bit(FR_INTERRUPTED, &req->flags)))
return -EINVAL;
fiq->ops->send_interrupt(fiq, req);
return 0;
}
bool fuse_remove_pending_req(struct fuse_req *req, spinlock_t *lock)
{
spin_lock(lock);
if (test_bit(FR_PENDING, &req->flags)) {
/*
* FR_PENDING does not get cleared as the request will end
* up in destruction anyway.
*/
list_del(&req->list);
spin_unlock(lock);
__fuse_put_request(req);
req->out.h.error = -EINTR;
return true;
}
spin_unlock(lock);
return false;
}
static void request_wait_answer(struct fuse_req *req)
{
struct fuse_chan *fch = req->chan;
struct fuse_iqueue *fiq = &fch->iq;
int err;
if (!fch->no_interrupt) {
/* Any signal may interrupt this */
err = wait_event_interruptible(req->waitq,
test_bit(FR_FINISHED, &req->flags));
if (!err)
return;
set_bit(FR_INTERRUPTED, &req->flags);
/* matches barrier in fuse_dev_do_read() */
smp_mb__after_atomic();
if (test_bit(FR_SENT, &req->flags))
queue_interrupt(req);
}
if (!test_bit(FR_FORCE, &req->flags)) {
bool removed;
/* Only fatal signals may interrupt this */
err = wait_event_killable(req->waitq,
test_bit(FR_FINISHED, &req->flags));
if (!err)
return;
if (req->args->abort_on_kill) {
fuse_chan_abort(fch, false);
goto wait_for_finish;
}
if (test_bit(FR_URING, &req->flags))
removed = fuse_uring_remove_pending_req(req);
else
removed = fuse_remove_pending_req(req, &fiq->lock);
if (removed)
return;
}
wait_for_finish:
/*
* Either request is already in userspace, or it was forced.
* Wait it out.
*/
wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags));
}
static void __fuse_request_send(struct fuse_req *req)
{
struct fuse_iqueue *fiq = &req->chan->iq;
BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
/* acquire extra reference, since request is still needed after
fuse_request_end() */
__fuse_get_request(req);
/*
* This is a synchronous request: the caller will block waiting for
* the answer. Hint the scheduler via wake_up_sync().
*/
set_bit(FR_SYNC_WAKEUP, &req->flags);
fuse_send_one(fiq, req);
request_wait_answer(req);
/* Pairs with smp_wmb() in fuse_request_end() */
smp_rmb();
}
static void fuse_adjust_compat(struct fuse_chan *fch, struct fuse_args *args)
{
if (fch->minor < 4 && args->opcode == FUSE_STATFS)
args->out_args[0].size = FUSE_COMPAT_STATFS_SIZE;
if (fch->minor < 9) {
switch (args->opcode) {
case FUSE_LOOKUP:
case FUSE_CREATE:
case FUSE_MKNOD:
case FUSE_MKDIR:
case FUSE_SYMLINK:
case FUSE_LINK:
args->out_args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
break;
case FUSE_GETATTR:
case FUSE_SETATTR:
args->out_args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
break;
}
}
if (fch->minor < 12) {
switch (args->opcode) {
case FUSE_CREATE:
args->in_args[0].size = sizeof(struct fuse_open_in);
break;
case FUSE_MKNOD:
args->in_args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
break;
}
}
}
static void fuse_args_to_req(struct fuse_req *req, struct fuse_args *args)
{
req->in.h.opcode = args->opcode;
req->in.h.nodeid = args->nodeid;
req->in.h.uid = args->uid;
req->in.h.gid = args->gid;
req->in.h.pid = args->pid;
req->args = args;
if (args->is_ext)
req->in.h.total_extlen = args->in_args[args->ext_idx].size / 8;
if (args->end)
__set_bit(FR_ASYNC, &req->flags);
}
ssize_t fuse_chan_send(struct fuse_chan *fch, struct fuse_args *args)
{
struct fuse_req *req;
ssize_t ret;
if (args->force) {
atomic_inc(&fch->num_waiting);
req = fuse_request_alloc(fch, GFP_KERNEL | __GFP_NOFAIL);
__set_bit(FR_WAITING, &req->flags);
if (!args->abort_on_kill)
__set_bit(FR_FORCE, &req->flags);
} else {
req = fuse_get_req(fch, false);
if (IS_ERR(req))
return PTR_ERR(req);
}
/* Needs to be done after fuse_get_req() so that fch->minor is valid */
fuse_adjust_compat(fch, args);
fuse_args_to_req(req, args);
if (!args->noreply)
__set_bit(FR_ISREPLY, &req->flags);
__fuse_request_send(req);
ret = req->out.h.error;
if (!ret && args->out_argvar) {
BUG_ON(args->out_numargs == 0);
ret = args->out_args[args->out_numargs - 1].size;
}
fuse_put_request(req);
return ret;
}
#ifdef CONFIG_FUSE_IO_URING
static bool fuse_request_queue_background_uring(struct fuse_req *req)
{
struct fuse_iqueue *fiq = &req->chan->iq;
req->in.h.len = sizeof(struct fuse_in_header) +
fuse_len_args(req->args->in_numargs,
(struct fuse_arg *) req->args->in_args);
fuse_request_assign_unique(fiq, req);
return fuse_uring_queue_bq_req(req);
}
#endif
/*
* @return true if queued
*/
static int fuse_request_queue_background(struct fuse_req *req)
{
struct fuse_chan *fch = req->chan;
bool queued = false;
WARN_ON(!test_bit(FR_BACKGROUND, &req->flags));
if (!test_bit(FR_WAITING, &req->flags)) {
__set_bit(FR_WAITING, &req->flags);
atomic_inc(&fch->num_waiting);
}
__set_bit(FR_ISREPLY, &req->flags);
#ifdef CONFIG_FUSE_IO_URING
if (fuse_uring_ready(fch))
return fuse_request_queue_background_uring(req);
#endif
spin_lock(&fch->bg_lock);
if (likely(fch->connected)) {
fch->num_background++;
if (fch->num_background == fch->max_background)
fch->blocked = 1;
list_add_tail(&req->list, &fch->bg_queue);
flush_bg_queue(fch);
queued = true;
}
spin_unlock(&fch->bg_lock);
return queued;
}
int fuse_chan_send_bg(struct fuse_chan *fch, struct fuse_args *args, gfp_t gfp_flags)
{
struct fuse_req *req;
if (args->force) {
req = fuse_request_alloc(fch, gfp_flags);
if (!req)
return -ENOMEM;
__set_bit(FR_BACKGROUND, &req->flags);
} else {
req = fuse_get_req(fch, true);
if (IS_ERR(req))
return PTR_ERR(req);
}
fuse_args_to_req(req, args);
if (!fuse_request_queue_background(req)) {
fuse_put_request(req);
return -ENOTCONN;
}
return 0;
}
int fuse_chan_send_notify_reply(struct fuse_chan *fch, struct fuse_args *args, u64 unique)
{
struct fuse_req *req;
struct fuse_iqueue *fiq = &fch->iq;
req = fuse_get_req(fch, false);
if (IS_ERR(req))
return PTR_ERR(req);
__clear_bit(FR_ISREPLY, &req->flags);
req->in.h.unique = unique;
fuse_args_to_req(req, args);
fuse_send_one(fiq, req);
return 0;
}
/*
* Lock the request. Up to the next unlock_request() there mustn't be
* anything that could cause a page-fault. If the request was already
* aborted bail out.
*/
static int lock_request(struct fuse_req *req)
{
int err = 0;
if (req) {
spin_lock(&req->waitq.lock);
if (test_bit(FR_ABORTED, &req->flags))
err = -ENOENT;
else
set_bit(FR_LOCKED, &req->flags);
spin_unlock(&req->waitq.lock);
}
return err;
}
/*
* Unlock request. If it was aborted while locked, caller is responsible
* for unlocking and ending the request.
*/
static int unlock_request(struct fuse_req *req)
{
int err = 0;
if (req) {
spin_lock(&req->waitq.lock);
if (test_bit(FR_ABORTED, &req->flags))
err = -ENOENT;
else
clear_bit(FR_LOCKED, &req->flags);
spin_unlock(&req->waitq.lock);
}
return err;
}
void fuse_copy_init(struct fuse_copy_state *cs, bool write,
struct iov_iter *iter)
{
memset(cs, 0, sizeof(*cs));
cs->write = write;
cs->iter = iter;
}
/* Unmap and put previous page of userspace buffer */
void fuse_copy_finish(struct fuse_copy_state *cs)
{
if (cs->currbuf) {
struct pipe_buffer *buf = cs->currbuf;