summaryrefslogtreecommitdiffstats
path: root/src/server/fuse.c
blob: 48e3e5ee38a7bc594d9afac8ec08a766f5dd690b (plain) (blame)
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
#include "fuse.h"
#include "../types.h"
#include "../shared/log.h"

#ifndef BUILD_SERVER_FUSE

//
bool dfuse_init(const char *opts UNUSED, const char *dir UNUSED)
{
	logadd( LOG_ERROR, "FUSE: Not compiled in" );
	return false;
}

void dfuse_shutdown()
{
}

#else

#define PATHLEN (2000)
static char nullbytes[DNBD3_BLOCK_SIZE];

// FUSE ENABLED
#define FUSE_USE_VERSION 30
//
#include "../config.h"
#include "locks.h"
#include "threadpool.h"
#include "image.h"
#include "uplink.h"
#include "reference.h"

#include <fuse_lowlevel.h>
#include <ctype.h>
#include <assert.h>
#include <string.h>
#include <signal.h>

#define INO_ROOT (1)
#define INO_CTRL (2)
#define INO_DIR  (3)
static const char *NAME_CTRL = "control";
static const char *NAME_DIR = "images";

typedef struct {
	fuse_req_t req;
	uint16_t rid;
	char name[PATHLEN];
} lookup_t;

static fuse_ino_t inoCounter = 10;
typedef struct _dfuse_dir {
	struct _dfuse_dir *next;
	struct _dfuse_dir *child;
	const char *name;
	uint64_t size;
	fuse_ino_t ino;
	int refcount;
	lookup_t *img;
} dfuse_entry_t;

typedef struct {
	dfuse_entry_t *entry;
	dnbd3_image_t *image;
} cmdopen_t;

static dfuse_entry_t sroot = {
	.name = "images",
	.ino = INO_DIR,
	.refcount = 2,
}, *root = &sroot;
static pthread_mutex_t dirLock;

#define INIT_NONE (0)
#define INIT_DONE (1)
#define INIT_SHUTDOWN (2)
#define INIT_INPROGRESS (3)

static struct fuse_session *fuseSession = NULL;
static struct fuse_chan *fuseChannel = NULL;
static char *fuseMountPoint = NULL;
static pthread_t fuseThreadId;
static bool haveThread = false;
static _Atomic(int) initState = INIT_NONE;
static pthread_mutex_t initLock;
static struct timespec startupTime;

static dfuse_entry_t* dirLookup(dfuse_entry_t *dir, const char *name);
static dfuse_entry_t* inoRecursive(dfuse_entry_t *dir, fuse_ino_t ino);

static void uplinkCallback(void *data, uint64_t handle, uint64_t start UNUSED, uint32_t length, const char *buffer);
static void cleanupFuse();
static void* fuseMainLoop(void *data);

static void ll_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
{
	fi->fh = 0;
	if ( ino == INO_CTRL ) {
		if ( ( fi->flags & 3 ) != O_WRONLY ) {
			fuse_reply_err( req, EINVAL );
		} else {
			fi->nonseekable = 1;
			fuse_reply_open( req, fi );
		}
	} else if ( ino == INO_ROOT ) {
		fuse_reply_err( req, EISDIR );
	} else {
		if ( ( fi->flags & 3 ) != O_RDONLY ) {
			fuse_reply_err( req, EINVAL );
			return;
		}
		mutex_lock( &dirLock );
		dfuse_entry_t *entry = inoRecursive( root, ino );
		if ( entry == NULL ) {
			mutex_unlock( &dirLock );
			fuse_reply_err( req, ENOENT );
		} else if ( entry->img == NULL ) {
			mutex_unlock( &dirLock );
			fuse_reply_err( req, EISDIR );
		} else if ( entry->img->rid == 0 ) {
			mutex_unlock( &dirLock );
			fuse_reply_err( req, ENOENT );
		} else {
			entry->refcount++;
			mutex_unlock( &dirLock );
			dnbd3_image_t *image = image_get( entry->img->name, entry->img->rid, true );
			if ( image == NULL ) {
				fuse_reply_err( req, ENOENT );
				mutex_lock( &dirLock );
				entry->refcount--;
				mutex_unlock( &dirLock );
			} else {
				cmdopen_t *handle = malloc( sizeof(cmdopen_t) );
				handle->entry = entry;
				handle->image = image;
				fi->fh = (uintptr_t)handle;
				fi->keep_cache = 1;
				fuse_reply_open( req, fi );
			}
		}
	}
}

static dfuse_entry_t* addImage(dfuse_entry_t **dir, const char *name, lookup_t *img)
{
	const char *slash = strchr( name, '/' );
	if ( slash == NULL ) {
		// Name portion at the end
		char *path = NULL;
		if ( asprintf( &path, "%s:%d", name, (int)img->rid ) == -1 )
			abort();
		dfuse_entry_t *entry = dirLookup( *dir, path );
		if ( entry == NULL ) {
			entry = calloc( 1, sizeof( *entry ) );
			entry->next = *dir;
			*dir = entry;
			entry->name = path;
			entry->ino = inoCounter++;
			entry->img = img;
		} else {
			free( path );
			if ( entry->img == NULL ) {
				return NULL;
			}
		}
		return entry;
	} else {
		// Dirname
		char *path = NULL;
		if ( asprintf( &path, "%.*s", (int)( slash - name ), name ) == -1 )
			abort();
		dfuse_entry_t *entry = dirLookup( *dir, path );
		if ( entry == NULL ) {
			entry = calloc( 1, sizeof( *entry ) );
			entry->next = *dir;
			*dir = entry;
			entry->name = path;
			entry->ino = inoCounter++;
		} else {
			free( path );
		}
		return addImage( &entry->child, slash + 1, img );
	}
}

static void ll_write(fuse_req_t req, fuse_ino_t ino, const char *buf, size_t size, off_t off, struct fuse_file_info *fi UNUSED)
{
	if ( ino != INO_CTRL ) {
		fuse_reply_err( req, EROFS );
		return;
	}
	if ( off != 0 ) {
		fuse_reply_err( req, ESPIPE );
		return;
	}
	if ( size >= PATHLEN ) {
		fuse_reply_err( req, ENOSPC );
		return;
	}
	size_t colon = 0;
	int rid = 0;
	for ( size_t i = 0; i < size; ++i ) {
		if ( buf[i] == '\0' || buf[i] == '\n' ) {
			if ( colon == 0 ) {
				colon = i;
			}
			break;
		}
		if ( colon != 0 ) {
			if ( !isdigit( buf[i] ) ) {
				logadd( LOG_WARNING, "FUSE: Malformed rid" );
				fuse_reply_err( req, EINVAL );
				return;
			}
			rid = rid * 10 + ( buf[i] - '0' ); // Can overflow but who cares
		} else if ( buf[i] == ':' ) {
			colon = i; // Image name starting with ':' would be broken...
		}
	}
	if ( rid < 0 || rid > 65535 ) {
		logadd( LOG_WARNING, "FUSE: Invalid rid '%d'", rid );
		fuse_reply_err( req, EINVAL );
		return;
	}
	if ( colon == 0 ) {
		colon = size;
	}
	lookup_t *lu = malloc( sizeof(lookup_t) );
	lu->rid = (uint16_t)rid;
	lu->req = req;
	if ( snprintf( lu->name, PATHLEN, "%.*s", (int)colon, buf ) == -1 ) {
		free( lu );
		fuse_reply_err( req, ENOSPC );
		return;
	}
	logadd( LOG_DEBUG1, "FUSE: Request for '%s:%d'", lu->name, (int)lu->rid );
	dnbd3_image_t *image = image_getOrLoad( lu->name, lu->rid );
	if ( image == NULL ) {
		fuse_reply_err( lu->req, ENOENT );
		free( lu );
	} else {
		mutex_lock( &dirLock );
		dfuse_entry_t *entry = addImage( &root->child, lu->name, lu );
		if ( entry != NULL ) {
			entry->size = image->virtualFilesize;
		}
		lu->rid = image->rid; // In case it was 0
		mutex_unlock( &dirLock );
		image_release( image );
		if ( entry == NULL ) {
			fuse_reply_err( lu->req, EINVAL );
			free( lu );
		} else {
			fuse_reply_write( lu->req, size );
		}
	}
}

static void ll_read( fuse_req_t req, fuse_ino_t ino UNUSED, size_t size, off_t off, struct fuse_file_info *fi )
{
	if ( fi->fh == 0 ) {
		fuse_reply_err( req, 0 );
		return;
	}
	cmdopen_t *handle = (cmdopen_t*)fi->fh;
	dnbd3_image_t *image = handle->image;
	if ( off < 0 || (uint64_t)off >= image->virtualFilesize ) {
		fuse_reply_err( req, 0 );
		return;
	}
	if ( off + size > image->virtualFilesize ) {
		size = image->virtualFilesize - off;
	}

	// Check if cached locally
	dnbd3_cache_map_t *cache = ref_get_cachemap( image );
	if ( cache != NULL ) {
		// This is a proxyed image, check if we need to relay the request...
		const uint64_t start = (uint64_t)off & ~(uint64_t)(DNBD3_BLOCK_SIZE - 1);
		const uint64_t end = (off + size + DNBD3_BLOCK_SIZE - 1) & ~(uint64_t)(DNBD3_BLOCK_SIZE - 1);
		if ( !image_isRangeCachedUnsafe( cache, start, end ) ) {
			ref_put( &cache->reference );
			if ( size > (uint32_t)_maxPayload ) {
				size = (uint32_t)_maxPayload;
			}
			if ( !uplink_request( image, req, &uplinkCallback, 0, off, (uint32_t)size ) ) {
				logadd( LOG_DEBUG1, "FUSE: Could not relay uncached request to upstream proxy for image %s:%d",
						image->name, image->rid );
				fuse_reply_err( req, EIO );
			}
			return; // ASYNC
		}
		ref_put( &cache->reference );
	}

	// Is cached
	size_t readSize = size;
	if ( off + readSize > image->realFilesize ) {
		if ( (uint64_t)off >= image->realFilesize ) {
			readSize = 0;
		} else {
			readSize = image->realFilesize - off;
		}
	}
	struct fuse_bufvec *vec = calloc( 1, sizeof(*vec) + sizeof(struct fuse_buf) );
	if ( readSize != 0 ) {
		// Real data from file
		vec->buf[vec->count++] = (struct fuse_buf){
			.size = readSize,
			.flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_RETRY | FUSE_BUF_FD_SEEK,
			.fd = image->readFd,
			.pos = off,
		};
	}
	if ( readSize != size ) {
		vec->buf[vec->count++] = (struct fuse_buf){
			.size = size - readSize,
			.mem = nullbytes,
			.fd = -1,
		};
	}
	fuse_reply_data( req, vec, 0 );
	free( vec );
}

static bool statInternal(fuse_ino_t ino, struct stat *stbuf)
{
	switch ( ino ) {
	case INO_ROOT:
	case INO_DIR:
		stbuf->st_mode = S_IFDIR | 0555;
		stbuf->st_nlink = 2;
		stbuf->st_mtim = startupTime;
		break;
	case INO_CTRL:
		stbuf->st_mode = S_IFREG | 0222;
		stbuf->st_nlink = 1;
		stbuf->st_size = 0;
		clock_gettime( CLOCK_REALTIME, &stbuf->st_mtim );
		break;
	default:
		return false;
	}
	stbuf->st_ctim = stbuf->st_atim = startupTime;
	stbuf->st_uid = 0;
	stbuf->st_ino = ino;
	return true;
}

/**
 * HOLD LOCK
 */
static dfuse_entry_t* dirLookup(dfuse_entry_t *dir, const char *name)
{
	if ( dir == NULL )
		return NULL;
	for ( dfuse_entry_t *it = dir; it != NULL; it = it->next ) {
		if ( strcmp( it->name, name ) == 0 )
			return it;
	}
	return NULL;
}

static dfuse_entry_t* inoRecursive(dfuse_entry_t *dir, fuse_ino_t ino)
{
	for ( dfuse_entry_t *it = dir; it != NULL; it = it->next ) {
		logadd( LOG_DEBUG1, "ino %d is %s", (int)it->ino, it->name );
		if ( it->ino == ino )
			return it;
		if ( it->img == NULL ) {
			dir = inoRecursive( it->child, ino );
			if ( dir != NULL )
				return dir;
		}
	}
	return NULL;
}

/**
 * HOLD LOCK
 */
static void entryToStat(dfuse_entry_t *entry, struct stat *stbuf)
{
	if ( entry->img == NULL ) {
		stbuf->st_mode = S_IFDIR | 0555;
		stbuf->st_nlink = 2;
	} else {
		stbuf->st_mode = S_IFREG | 0444;
		stbuf->st_nlink = 1;
		stbuf->st_size = entry->size;
	}
	stbuf->st_ino = entry->ino;
	stbuf->st_uid = 0;
	stbuf->st_ctim = stbuf->st_atim = stbuf->st_mtim = startupTime;
}

static void ll_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
{
	logadd( LOG_DEBUG2, "Lookup at ino %d for '%s'", (int)parent, name );
	if ( parent == INO_ROOT ) {
		struct fuse_entry_param e = { 0 };
		if ( strcmp( name, NAME_DIR ) == 0 ) {
			e.ino = INO_DIR;
		} else if ( strcmp( name, NAME_CTRL ) == 0 ) {
			e.ino = INO_CTRL;
			e.attr_timeout = e.entry_timeout = 3600;
		}
		if ( e.ino != 0 && statInternal( e.ino, &e.attr ) ) {
			fuse_reply_entry( req, &e );
			return;
		}
	} else {
		mutex_lock( &dirLock );
		dfuse_entry_t *dir = inoRecursive( root, parent );
		if ( dir != NULL ) {
			if ( dir->img != NULL ) {
				mutex_unlock( &dirLock );
				fuse_reply_err( req, ENOTDIR );
				return;
			}
			dfuse_entry_t *entry = dirLookup( dir->child, name );
			if ( entry != NULL ) {
				struct fuse_entry_param e = { .ino = entry->ino };
				entryToStat( entry, &e.attr );
				mutex_unlock( &dirLock );
				fuse_reply_entry( req, &e );
				return;
			}
		}
		mutex_unlock( &dirLock );
	}
	fuse_reply_err( req, ENOENT );
}

struct dirbuf {
	char *p;
	size_t size;
};

static void dirbuf_add( fuse_req_t req, struct dirbuf *b, const char *name, fuse_ino_t ino )
{
	struct stat stbuf = { .st_ino = ino };
	size_t oldsize = b->size;
	b->size += fuse_add_direntry( req, NULL, 0, name, NULL, 0 );
	b->p = ( char * ) realloc( b->p, b->size );
	fuse_add_direntry( req, b->p + oldsize, b->size - oldsize, name, &stbuf, b->size );
	return;
}

static int reply_buf_limited( fuse_req_t req, const char *buf, size_t bufsize, off_t off, size_t maxsize )
{
	if ( off >= 0 && off < (off_t)bufsize ) {
		return fuse_reply_buf( req, buf + off, MIN( bufsize - off, maxsize ) );
	}
	return fuse_reply_buf( req, NULL, 0 );
}

static void ll_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, struct fuse_file_info *fi UNUSED)
{
	if ( ino != INO_ROOT ) {
		fuse_reply_err( req, EACCES );
	} else {
		struct dirbuf b;
		memset( &b, 0, sizeof( b ) );
		dirbuf_add( req, &b, ".", INO_ROOT );
		dirbuf_add( req, &b, "..", INO_ROOT );
		dirbuf_add( req, &b, NAME_CTRL, INO_CTRL );
		dirbuf_add( req, &b, NAME_DIR, INO_DIR );
		reply_buf_limited( req, b.p, b.size, off, size );
		free( b.p );
	}
}

static void ll_getattr(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi UNUSED)
{
	struct stat stbuf = { .st_ino = 0 };
	if ( !statInternal( ino, &stbuf ) ) {
		mutex_lock( &dirLock );
		dfuse_entry_t *entry = inoRecursive( root, ino );
		if ( entry != NULL ) {
			entryToStat( entry, &stbuf );
		}
		mutex_unlock( &dirLock );
	}
	if ( stbuf.st_ino == 0 ) {
		fuse_reply_err( req, ENOENT );
	} else {
		fuse_reply_attr( req, &stbuf, 0 );
	}
}

void ll_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr UNUSED, int to_set UNUSED, struct fuse_file_info *fi)
{
	ll_getattr( req, ino, fi );
}

void ll_release(fuse_req_t req, fuse_ino_t ino UNUSED, struct fuse_file_info *fi)
{
	if ( fi->fh != 0 ) {
		cmdopen_t *handle = (cmdopen_t*)fi->fh;
		image_release( handle->image );
		mutex_lock( &dirLock );
		handle->entry->refcount--;
		mutex_unlock( &dirLock );
		free( handle );
	}
	fuse_reply_err( req, 0 );
}

static void uplinkCallback(void *data, uint64_t handle UNUSED, uint64_t start UNUSED, uint32_t length, const char *buffer)
{
	fuse_req_t req = (fuse_req_t)data;
	if ( buffer == NULL ) {
		fuse_reply_err( req, EIO );
	} else {
		fuse_reply_buf( req, buffer, length );
	}
}

/* map the implemented fuse operations */
static struct fuse_lowlevel_ops fuseOps = {
	.lookup = ll_lookup,
	.getattr = ll_getattr,
	.setattr = ll_setattr,
	.readdir = ll_readdir,
	.open = ll_open,
	.release = ll_release,
	.read = ll_read,
	.write = ll_write,
	//.init = ll_init,
	//.destroy = ll_destroy,
};

bool dfuse_init(const char *opts, const char *dir)
{
	int ex = INIT_NONE;
	if ( !atomic_compare_exchange_strong( &initState, &ex, INIT_INPROGRESS ) ) {
		logadd( LOG_ERROR, "Calling dfuse_init twice" );
		exit( 1 );
	}
	mutex_init( &initLock, LOCK_FUSE_INIT );
	mutex_lock( &initLock );
	mutex_init( &dirLock, LOCK_FUSE_DIR );
	clock_gettime( CLOCK_REALTIME, &startupTime );
	struct fuse_args args = FUSE_ARGS_INIT( 0, NULL );
	fuse_opt_add_arg( &args, "dnbd3fs" ); // argv[0]
	if ( opts != NULL ) {
		fuse_opt_add_arg( &args, opts );
	}
	fuse_opt_add_arg( &args, "-odefault_permissions" );
	fuse_opt_add_arg( &args, dir ); // last param is mount point
	//
	if ( fuse_parse_cmdline( &args, &fuseMountPoint, NULL, NULL ) == -1 ) {
		logadd( LOG_ERROR, "FUSE: Error parsing command line" );
		goto fail;
	}
	fuseChannel = fuse_mount( fuseMountPoint, &args );
	if ( fuseChannel == NULL ) {
		logadd( LOG_ERROR, "FUSE: Cannot mount to %s", dir );
		goto fail;
	}
	fuseSession = fuse_lowlevel_new( &args, &fuseOps, sizeof( fuseOps ), NULL );
	if ( fuseSession == NULL ) {
		logadd( LOG_ERROR, "FUSE: Error initializing fuse session" );
		goto fail;
	}
	fuse_session_add_chan( fuseSession, fuseChannel );
	if ( 0 != thread_create( &fuseThreadId, NULL, &fuseMainLoop, (void *)NULL ) ) {
		logadd( LOG_ERROR, "FUSE: Could not start thread" );
		goto fail;
	}
	haveThread = true;
	// Init OK
	mutex_unlock( &initLock );
	return true;
fail:
	cleanupFuse();
	fuse_opt_free_args( &args );
	initState = INIT_SHUTDOWN;
	mutex_unlock( &initLock );
	return false;
}

void dfuse_shutdown()
{
	if ( initState == INIT_NONE )
		return;
	for ( ;; ) {
		int ex = INIT_DONE;
		if ( atomic_compare_exchange_strong( &initState, &ex, INIT_SHUTDOWN ) )
			break; // OK, do the shutdown
		if ( ex == INIT_INPROGRESS )
			continue; // dfuse_init in progress, wait for mutex
		// Wrong state
		logadd( LOG_WARNING, "Called dfuse_shutdown without dfuse_init first" );
		return;
	}
	logadd( LOG_INFO, "Shutting down fuse mainloop..." );
	mutex_lock( &initLock );
	if ( fuseSession != NULL ) {
		fuse_session_exit( fuseSession );
	}
	if ( !haveThread ) {
		cleanupFuse();
	}
	mutex_unlock( &initLock );
	if ( haveThread ) {
		logadd( LOG_DEBUG1, "FUSE: Sending USR1 to mainloop thread" );
		pthread_kill( fuseThreadId, SIGUSR1 );
		pthread_join( fuseThreadId, NULL );
	}
}

static void* fuseMainLoop(void *data UNUSED)
{
	int ex = INIT_INPROGRESS;
	if ( !atomic_compare_exchange_strong( &initState, &ex, INIT_DONE ) ) {
		logadd( LOG_WARNING, "FUSE: Unexpected state in fuseMainLoop: %d", ex );
		return NULL;
	}
	logadd( LOG_INFO, "FUSE: Starting mainloop" );
	fuse_session_loop_mt( fuseSession );
	logadd( LOG_INFO, "FUSE: Left mainloop" );
	mutex_lock( &initLock );
	cleanupFuse();
	mutex_unlock( &initLock );
	return NULL;
}

static void cleanupFuse()
{
	if ( fuseChannel != NULL ) {
		fuse_session_remove_chan( fuseChannel );
	}
	if ( fuseSession != NULL ) {
		fuse_session_destroy( fuseSession );
		fuseSession = NULL;
	}
	if ( fuseMountPoint != NULL && fuseChannel != NULL ) {
		fuse_unmount( fuseMountPoint, fuseChannel );
	}
	fuseChannel = NULL;
}

#endif