-
Notifications
You must be signed in to change notification settings - Fork 4
/
fuse-loop-epoll-mt.c
701 lines (404 loc) · 14.7 KB
/
fuse-loop-epoll-mt.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
/*
2010, 2011 Stef Bon <stefbon@gmail.com>
fuse-loop-epoll-mt.c
This is an alternative main eventloop for the fuse filesystem using epoll and threads.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#define FUSE_USE_VERSION 26
#define _REENTRANT
#define _GNU_SOURCE
#define PACKAGE_VERSION "1.2"
#include <fuse/fuse_lowlevel.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <err.h>
#include <assert.h>
#include <inttypes.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/epoll.h>
#include <sys/signalfd.h>
#include <pthread.h>
#include <semaphore.h>
#define LOGGING
#include "logging.h"
#include "fuse-loop-epoll-mt.h"
//
// a thread in the pool of active threads
// to process the fuse event
// the mainloop has noticed there is data available, and read in a buffer
// but is not processed yet
//
static void *thread_to_process_fuse_event(void *threadarg)
{
struct fuse_worker_thread_data_struct *worker_data;
struct fuse_event_data_struct *fuse_event_data;
bool permanentthread;
struct global_data_struct *global_data;
unsigned char loglevel;
logoutput("thread started..");
worker_data=(struct fuse_worker_thread_data_struct *) threadarg;
if ( worker_data ) {
permanentthread=false;
loglevel=worker_data->global_data->loglevel;
if ( worker_data->typeworker==TYPE_WORKER_PERMANENT ) permanentthread=true;
worker_data->threadid=pthread_self();
if ( permanentthread ) {
writelog(loglevel, 1, "perm thread %i: starting for loop..", worker_data->nr);
} else {
writelog(loglevel, 1, "temp thread started..");
}
while (1) {
// if permanent : wait for the condition
// this is set by the mainloop to inform this thread there is data ready to process
if ( permanentthread ) {
// wait for semaphore to "release"
sem_wait(&(worker_data->busy_sem));
writelog(loglevel, 1, "thread %i waking up, setting state to busy", worker_data->nr);
worker_data->busy=2;
}
fuse_event_data=worker_data->fuse_event_data;
if ( fuse_event_data && fuse_event_data->buff ) {
writelog(loglevel, 1, "data found, start fuse_session_process");
fuse_session_process(fuse_event_data->se, fuse_event_data->buff, fuse_event_data->res, fuse_event_data->ch);
// reset the various data
free(fuse_event_data->buff);
free(fuse_event_data);
worker_data->fuse_event_data=NULL;
}
if ( permanentthread ) {
writelog(loglevel, 1, "thread %i: set state to not busy", worker_data->nr);
worker_data->busy=0;
} else {
writelog(loglevel, 1, "temp thread ready, exit");
break;
}
}
if ( ! permanentthread ) {
// freeing the worker data when temp thread
// the buff is already freed..
// free
// remove itself from list of temp threads
global_data=worker_data->global_data;
if ( worker_data->prev ) worker_data->prev->next=worker_data->next;
if ( worker_data->next ) worker_data->next->prev=worker_data->prev;
if ( global_data->temp_worker_data==worker_data) global_data->temp_worker_data=worker_data->next;
free(worker_data);
}
}
logoutput("thread ending");
pthread_exit(NULL);
}
static struct fuse_event_data_struct *create_fuse_event_data(struct fuse_epoll_data_struct *fuse_epoll_data)
{
struct fuse_event_data_struct *fuse_event_data;
unsigned ncount=0;
while (ncount<4) {
fuse_event_data=malloc(sizeof(struct fuse_event_data_struct));
if ( fuse_event_data ) break;
ncount++;
}
if ( fuse_event_data ) {
ncount=0;
fuse_event_data->buffsize=fuse_epoll_data->buffsize;
while (ncount<4) {
fuse_event_data->buff=malloc(fuse_event_data->buffsize);
if (fuse_event_data->buff) break;
ncount++;
}
if ( ! fuse_event_data->buff ) {
free(fuse_event_data);
fuse_event_data=NULL;
} else {
memcpy(fuse_event_data->buff, fuse_epoll_data->buff, fuse_event_data->buffsize);
fuse_event_data->ch=fuse_epoll_data->ch;
fuse_event_data->se=fuse_epoll_data->se;
fuse_event_data->res=fuse_epoll_data->res;
}
}
return fuse_event_data;
}
int fuse_session_loop_epoll_mt(struct fuse_session *se, unsigned char loglevel)
{
int epoll_fd, signal_fd, fuse_fd;
struct epoll_event epoll_events[MAX_EPOLL_NREVENTS];
int i, res, nextfreethread=0, ncount;
struct fuse_chan *ch = NULL;
ssize_t readlen;
struct signalfd_siginfo fdsi;
int signo, nreturn, nerror;
sigset_t fuse_sigset;
pid_t cpid;
struct fuse_epoll_data_struct *fuse_epoll_data;
struct fuse_worker_thread_data_struct fuse_worker_data[NUM_WORKER_THREADS];
struct fuse_worker_thread_data_struct *tmp_worker_data;
struct fuse_event_data_struct *fuse_event_data;
pthread_t pthreadid;
bool threadfound=false;
struct global_data_struct global_data;
// init mainloop data
global_data.epoll_data=NULL;
global_data.temp_worker_data=NULL;
global_data.loglevel=loglevel;
// create an epoll instance
epoll_fd=epoll_create(MAX_EPOLL_NRFDS);
// walk through all the channels and add the assiciated fd to the epoll
ch = fuse_session_next_chan(se, NULL);
while (ch) {
// determine the fd of the channel
fuse_fd=fuse_chan_fd(ch);
fuse_epoll_data=malloc(sizeof(struct fuse_epoll_data_struct));
if ( fuse_epoll_data ) {
memset(fuse_epoll_data, 0, sizeof(struct fuse_epoll_data_struct));
fuse_epoll_data->type_fd=TYPE_FD_FUSE;
fuse_epoll_data->fd=fuse_fd;
fuse_epoll_data->ch=ch;
fuse_epoll_data->se=se;
fuse_epoll_data->buffsize=fuse_chan_bufsize(fuse_epoll_data->ch);
fuse_epoll_data->buff=(char *) malloc(fuse_epoll_data->buffsize);
// add epoll_data to list (insert at begin)
fuse_epoll_data->next=global_data.epoll_data;
global_data.epoll_data=fuse_epoll_data;
if ( ! fuse_epoll_data->buff ) {
// here some freeing of data?
nreturn=-ENOMEM;
goto out;
}
} else {
// here free data??
res=-ENOMEM;
goto out;
}
// add this fd to the epoll instance
static struct epoll_event epoll_fuse_instance;
epoll_fuse_instance.events=EPOLLIN;
epoll_fuse_instance.data.ptr=(void *) fuse_epoll_data;
res=epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fuse_fd, &epoll_fuse_instance);
if ( res==-1 ) {
nreturn=-errno;
goto out;
}
ch = fuse_session_next_chan(se, ch);
}
// set the set of signals for signalfd to listen to
sigemptyset(&fuse_sigset);
sigaddset(&fuse_sigset, SIGINT);
sigaddset(&fuse_sigset, SIGIO);
sigaddset(&fuse_sigset, SIGHUP);
sigaddset(&fuse_sigset, SIGTERM);
sigaddset(&fuse_sigset, SIGPIPE);
sigaddset(&fuse_sigset, SIGCHLD);
sigaddset(&fuse_sigset, SIGUSR1);
signal_fd = signalfd(-1, &fuse_sigset, SFD_NONBLOCK);
if (signal_fd == -1) {
nreturn=-errno;
logoutput("mainloop: unable to create signalfd, error: %i", nreturn);
goto out;
}
if (sigprocmask(SIG_BLOCK, &fuse_sigset, NULL) == -1) {
logoutput("mainloop: error sigprocmask");
goto out;
}
writelog(loglevel, 1, "mainloop: adding signalfd %i to epoll", signal_fd);
fuse_epoll_data=malloc(sizeof(struct fuse_epoll_data_struct));
if ( fuse_epoll_data ) {
memset(fuse_epoll_data, 0, sizeof(struct fuse_epoll_data_struct));
fuse_epoll_data->type_fd=TYPE_FD_SIGNAL;
fuse_epoll_data->fd=signal_fd;
} else {
res=-ENOMEM;
goto out;
}
static struct epoll_event epoll_signalfd_instance;
epoll_signalfd_instance.events=EPOLLIN;
epoll_signalfd_instance.data.ptr=( void *) fuse_epoll_data;
res=epoll_ctl(epoll_fd, EPOLL_CTL_ADD, signal_fd, &epoll_signalfd_instance);
if ( res==-1 ) {
nreturn=-errno;
goto out;
}
// init and fire up the different worker threads
for (i=0; i<NUM_WORKER_THREADS; i++) {
res=sem_init(&(fuse_worker_data[i].busy_sem), 0, 0);
if ( res!=0 ) {
// error
writelog(loglevel, 0, "Error creating a semaphore for thread(nr: %i, error: %i).", i, res);
goto out;
}
fuse_worker_data[i].busy=0;
fuse_worker_data[i].nr=i;
fuse_worker_data[i].typeworker=TYPE_WORKER_PERMANENT;
fuse_worker_data[i].fuse_event_data=NULL;
fuse_worker_data[i].global_data=&global_data;
writelog(loglevel, 1, "creating a new thread: %i.", i);
res=pthread_create(&pthreadid, NULL, thread_to_process_fuse_event, (void *) &fuse_worker_data[i]);
if ( res!=0 ) {
// error
logoutput("Error creating a new thread (nr: %i, error: %i).", i, res);
goto out;
}
}
writelog(loglevel, 0, "mainloop: starting epoll wait loop");
while (1) {
int number_of_fds=epoll_wait(epoll_fd, epoll_events, MAX_EPOLL_NREVENTS, -1);
if (number_of_fds < 0) {
nreturn=-errno;
writelog(loglevel, 0, "mainloop: epoll_wait error");
goto out;
}
for (i=0; i<number_of_fds; i++) {
fuse_epoll_data=(struct fuse_epoll_data_struct *) epoll_events[i].data.ptr;
// look what kind of fd this is
if ( fuse_epoll_data->type_fd==TYPE_FD_FUSE ) {
// it a fuse thing
// create a new request to handle this event the first free thread
// check first it;s not exit
if ( fuse_session_exited(se) ) goto out;
memset(fuse_epoll_data->buff, 0, fuse_epoll_data->buffsize);
res=fuse_chan_recv(&fuse_epoll_data->ch, fuse_epoll_data->buff, fuse_epoll_data->buffsize);
if ( res>0 ) {
fuse_epoll_data->res=res;
// copy the data to a new event data
fuse_event_data=create_fuse_event_data(fuse_epoll_data);
if ( ! fuse_event_data ) {
nreturn=-ENOMEM;
goto out;
}
threadfound=false;
ncount=0;
while ( ! threadfound ) {
if ( fuse_worker_data[nextfreethread].busy==0 ) {
fuse_worker_data[nextfreethread].fuse_event_data=fuse_event_data;
// not busy, activate ("verhogen") the sem
writelog(loglevel, 0, "mainloop: sem post for thread %i", nextfreethread);
res=sem_post(&(fuse_worker_data[nextfreethread].busy_sem));
if ( res==0 ) {
threadfound=true;
} else {
// some error, just try the next worker thread
writelog(loglevel, 0, "mainloop: sem post failed");
fuse_worker_data[nextfreethread].fuse_event_data=NULL;
}
}
nextfreethread++;
ncount++;
if ( nextfreethread>=NUM_WORKER_THREADS ) {
nextfreethread=0;
}
// stop if found or all the threads are tried
if ( threadfound || ncount>=NUM_WORKER_THREADS) break;
}
if ( ! threadfound) {
// no free thread found in the pool
// start a temp thread especially for this purpose
writelog(loglevel, 0, "mainloop: no free thread found, starting a temp thread");
tmp_worker_data=malloc(sizeof(struct fuse_worker_thread_data_struct));
if ( tmp_worker_data ) {
tmp_worker_data->busy=0;
tmp_worker_data->nr=0;
tmp_worker_data->typeworker=TYPE_WORKER_TEMPORARY;
tmp_worker_data->fuse_event_data=fuse_event_data;
tmp_worker_data->global_data=&global_data;
// insert at beginning
tmp_worker_data->prev=NULL;
if ( global_data.temp_worker_data ) {
global_data.temp_worker_data->prev=tmp_worker_data;
tmp_worker_data->next=global_data.temp_worker_data;
} else {
tmp_worker_data->next=NULL;
}
global_data.temp_worker_data=tmp_worker_data;
res=pthread_create(&pthreadid, NULL, thread_to_process_fuse_event, (void *) tmp_worker_data);
if ( res!=0 ) {
free(fuse_event_data->buff);
free(fuse_event_data);
free(tmp_worker_data);
writelog(loglevel, 0, "mainloop: cannot start temp thread");
}
} else {
writelog(loglevel, 0, "mainloop: cannot allocate space for temp thread");
}
}
} else {
writelog(loglevel, 0, "mainloop: error reading fuse event, %i", res);
}
if ( fuse_session_exited(se) ) goto out;
} else if ( fuse_epoll_data->type_fd==TYPE_FD_SIGNAL ) {
//
// some data on signalfd
//
writelog(loglevel, 0, "mainloop: in signal loop");
readlen=read(signal_fd, &fdsi, sizeof(struct signalfd_siginfo));
nerror=errno;
if ( readlen==-1 ) {
if ( nerror==EAGAIN ) {
// blocking error: back to the mainloop
continue;
}
writelog(loglevel, 0, "error %i reading from signalfd......", nerror);
} else {
if ( readlen == sizeof(struct signalfd_siginfo)) {
// check the signal
signo=fdsi.ssi_signo;
if ( signo==SIGHUP || signo==SIGINT || signo==SIGTERM ) {
writelog(loglevel, 0, "mainloop: caught signal %i, exit session", signo);
fuse_session_exit(se);
} else if ( signo==SIGPIPE ) {
signo=0;
writelog(loglevel, 0, "mainloop: caught signal SIGPIPE, ignoring");
} else if ( signo == SIGCHLD) {
writelog(loglevel, 0, "Got SIGCHLD, from pid: %d", fdsi.ssi_pid);
// look at the pid of the child with waitpid for preventing zombies
cpid=waitpid(fdsi.ssi_pid, NULL, WNOHANG);
} else if ( signo == SIGIO) {
writelog(loglevel, 0, "Got SIGIO.....");
} else {
writelog(loglevel, 0, "got unknown signal %i", signo);
}
}
}
}
}
}
out:
// stop any thread here
for (i=0; i<NUM_WORKER_THREADS; i++) {
pthread_cancel(fuse_worker_data[i].threadid);
sem_destroy(&(fuse_worker_data[i].busy_sem));
}
// the epoll data associated with every channel
fuse_epoll_data=global_data.epoll_data;
while (fuse_epoll_data) {
fuse_epoll_data=fuse_epoll_data->next;
if (global_data.epoll_data->buff) free(global_data.epoll_data->buff);
free(global_data.epoll_data);
global_data.epoll_data=fuse_epoll_data;
}
// what to do with temp threads??
tmp_worker_data=global_data.temp_worker_data;
while (tmp_worker_data) {
// this is safe?
pthread_cancel(tmp_worker_data->threadid);
tmp_worker_data=tmp_worker_data->next;
free(global_data.temp_worker_data);
global_data.temp_worker_data=tmp_worker_data;
}
fuse_session_reset(se);
return nreturn < 0 ? -1 : 0;
}