-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuse-directory.c
602 lines (423 loc) · 14.2 KB
/
fuse-directory.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
/*
2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Stef Bon <[email protected]>
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.
*/
#include "global-defines.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 <inttypes.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h>
#include <time.h>
#include "logging.h"
#include "utils.h"
#include "skiplist.h"
#include "skiplist-find.h"
#include "skiplist-delete.h"
#include "skiplist-insert.h"
#include "skiplist-seek.h"
#include "simple-locking.h"
#include "fuse-dentry.h"
#include "fuse-directory.h"
#ifndef SIZE_DIRECTORY_HASHTABLE
#define SIZE_DIRECTORY_HASHTABLE 1024
#endif
static struct directory_s *directory_hashtable[2048];
static pthread_mutex_t directory_hashtable_mutex=PTHREAD_MUTEX_INITIALIZER;
extern struct directory_s *get_dummy_directory();
extern void fs_get_inode_link(struct inode_s *inode, struct inode_link_s **link);
/*
callbacks for the skiplist
compare two elements to determine the right order
*/
static int compare_entry(void *a, void *b)
{
int result=0;
struct entry_s *entry=(struct entry_s *) a;
struct name_s *name=(struct name_s *) b;
if (entry->name.index > name->index) {
result=1; /* entry->name is bigger */
} else if (entry->name.index==name->index) {
if (name->len > 6) {
if (entry->name.len > 6) {
result=strcmp(entry->name.name + 6, name->name + 6);
} else {
result=-1; /* name is bigger */
}
} else if (name->len==6) {
if (entry->name.len>6) {
result=1;
} else {
result=0;
}
} else {
result=0;
}
} else {
result=-1;
}
return result;
}
static void *get_next_entry(void *data)
{
struct entry_s *entry=(struct entry_s *) data;
return (void *) entry->name_next;
}
static void *get_prev_entry(void *data)
{
struct entry_s *entry=(struct entry_s *) data;
return (void *) entry->name_prev;
}
/* insert an entry (a) before another (b) */
static void insert_before_entry(void *a, void *b, struct skiplist_struct *sl)
{
struct entry_s *entry=(struct entry_s *) a;
struct entry_s *before=(struct entry_s *) b;
struct directory_s *directory=(struct directory_s *) ( ((char *) sl) - offsetof(struct directory_s, skiplist));
if (before==directory->first) {
entry->name_next=before;
before->name_prev=entry;
directory->first=entry;
} else {
struct entry_s *prev=before->name_prev;
prev->name_next=entry;
entry->name_prev=prev;
entry->name_next=before;
before->name_prev=entry;
}
directory->count++;
}
/* insert an entry (a) after another (b) */
static void insert_after_entry(void *a, void *b, struct skiplist_struct *sl)
{
struct entry_s *entry=(struct entry_s *) a;
struct entry_s *after=(struct entry_s *) b;
struct directory_s *directory=(struct directory_s *) ( ((char *) sl) - offsetof(struct directory_s, skiplist));
if ( ! after) after=directory->last;
if (after==directory->last) {
if ( ! after) {
/* empty */
directory->last=entry;
directory->first=entry;
} else {
entry->name_prev=after;
after->name_next=entry;
directory->last=entry;
}
} else {
struct entry_s *next=after->name_next;
next->name_prev=entry;
entry->name_next=next;
entry->name_prev=after;
after->name_next=entry;
}
directory->count++;
}
/* delete an entry from the linked list */
static void delete_entry(void *a, struct skiplist_struct *sl)
{
struct entry_s *entry=(struct entry_s *) a;
struct directory_s *directory=(struct directory_s *) ( ((char *) sl) - offsetof(struct directory_s, skiplist));
if (entry==directory->first) {
if (entry==directory->last) {
directory->first=NULL;
directory->last=NULL;
} else {
directory->first=entry->name_next;
directory->first->name_prev=NULL;
}
} else if (entry==directory->last) {
directory->last=entry->name_prev;
directory->last->name_next=NULL;
} else {
struct entry_s *next=entry->name_next;
struct entry_s *prev=entry->name_prev;
prev->name_next=next;
next->name_prev=prev;
}
entry->name_next=NULL;
entry->name_prev=NULL;
directory->count--;
}
void init_directory_readlock(struct directory_s *directory, struct simple_lock_s *lock)
{
init_simple_readlock(&directory->locking, lock);
}
void init_directory_writelock(struct directory_s *directory, struct simple_lock_s *lock)
{
init_simple_writelock(&directory->locking, lock);
}
struct simple_lock_s *create_rlock_directory(struct directory_s *directory)
{
struct simple_lock_s *lock=malloc(sizeof(struct simple_lock_s));
// logoutput_info("_create_rlock_directory: directory %s", (directory) ? "defined" : "notdefined");
if (lock) {
init_directory_readlock(directory, lock);
lock->flags|=SIMPLE_LOCK_FLAG_ALLOCATED;
if (simple_lock(lock)==0) return lock;
free(lock);
}
return NULL;
}
struct simple_lock_s *create_wlock_directory(struct directory_s *directory)
{
struct simple_lock_s *lock=malloc(sizeof(struct simple_lock_s));
if (lock) {
init_directory_writelock(directory, lock);
lock->flags|=SIMPLE_LOCK_FLAG_ALLOCATED;
if (simple_lock(lock)==0) return lock;
free(lock);
}
return NULL;
}
int lock_directory(struct directory_s *directory, struct simple_lock_s *lock)
{
return simple_lock(lock);
}
int rlock_directory(struct directory_s *directory, struct simple_lock_s *lock)
{
init_directory_readlock(directory, lock);
return simple_lock(lock);
}
int wlock_directory(struct directory_s *directory, struct simple_lock_s *lock)
{
init_directory_writelock(directory, lock);
return simple_lock(lock);
}
int unlock_directory(struct directory_s *directory, struct simple_lock_s *lock)
{
int result=simple_unlock(lock);
// if (lock->flags & SIMPLE_LOCK_FLAG_ALLOCATED) free(lock);
return result;
}
int upgradelock_directory(struct directory_s *directory, struct simple_lock_s *lock)
{
return simple_upgradelock(lock);
}
int prelock_directory(struct directory_s *directory, struct simple_lock_s *lock)
{
if (directory->flags & _DIRECTORY_FLAG_REMOVE) return -1;
return simple_prelock(lock);
}
/* callbacks for the skiplist */
static void *create_rlock_skiplist(struct skiplist_struct *sl)
{
struct directory_s *directory=(struct directory_s *) ( ((char *) sl) - offsetof(struct directory_s, skiplist));
//logoutput("create_rlock_skiplist");
return (void *) create_rlock_directory(directory);
}
static void *create_wlock_skiplist(struct skiplist_struct *sl)
{
struct directory_s *directory=(struct directory_s *) ( ((char *) sl) - offsetof(struct directory_s, skiplist));
//logoutput_info("create_wlock_skiplist");
return (void *) create_wlock_directory(directory);
}
static int lock_skiplist(struct skiplist_struct *sl, void *ptr)
{
struct directory_s *directory=(struct directory_s *) ( ((char *) sl) - offsetof(struct directory_s, skiplist));
struct simple_lock_s *lock=(struct simple_lock_s *) ptr;
return lock_directory(directory, lock);
}
static int unlock_skiplist(struct skiplist_struct *sl, void *ptr)
{
struct directory_s *directory=(struct directory_s *) ( ((char *) sl) - offsetof(struct directory_s, skiplist));
struct simple_lock_s *lock=(struct simple_lock_s *) ptr;
return unlock_directory(directory, lock);
}
static int upgradelock_skiplist(struct skiplist_struct *sl, void *ptr)
{
struct directory_s *directory=(struct directory_s *) ( ((char *) sl) - offsetof(struct directory_s, skiplist));
struct simple_lock_s *lock=(struct simple_lock_s *) ptr;
return upgradelock_directory(directory, lock);
}
static int prelock_skiplist(struct skiplist_struct *sl, void *ptr)
{
struct directory_s *directory=(struct directory_s *) ( ((char *) sl) - offsetof(struct directory_s, skiplist));
struct simple_lock_s *lock=(struct simple_lock_s *) ptr;
return prelock_directory(directory, lock);
}
static unsigned int count_entries(struct skiplist_struct *sl)
{
struct directory_s *directory=(struct directory_s *) ( ((char *) sl) - offsetof(struct directory_s, skiplist));
return directory->count;
}
static void *first_entry(struct skiplist_struct *sl)
{
struct directory_s *directory=(struct directory_s *) ( ((char *) sl) - offsetof(struct directory_s, skiplist));
return (void *) directory->first;
}
static void *last_entry(struct skiplist_struct *sl)
{
struct directory_s *directory=(struct directory_s *) ( ((char *) sl) - offsetof(struct directory_s, skiplist));
return (void *) directory->last;
}
int init_directory(struct directory_s *directory, unsigned int *error)
{
int result=0;
logoutput("init_directory");
memset(directory, 0, sizeof(struct directory_s));
directory->flags=0;
directory->synctime.tv_sec=0;
directory->synctime.tv_nsec=0;
directory->inode=NULL;
directory->next=NULL;
directory->prev=NULL;
directory->count=0;
result=init_simple_locking(&directory->locking);
if (result==-1) {
logoutput_warning("init_directory: error initializing locking");
goto out;
}
directory->first=NULL;
directory->last=NULL;
directory->dops=NULL;
directory->link.type=0;
directory->link.link.ptr=NULL;
result=init_skiplist(&directory->skiplist, 4, get_next_entry, get_prev_entry,
compare_entry, insert_before_entry, insert_after_entry, delete_entry,
create_rlock_skiplist, create_wlock_skiplist,
lock_skiplist, unlock_skiplist, upgradelock_skiplist, prelock_skiplist, count_entries, first_entry, last_entry, error);
if (result==-1) {
logoutput_warning("init_directory: error %i initializing skiplist", *error);
} else {
logoutput("init_directory: directory intialized");
}
out:
return result;
}
/* search the directory using a hash table
search is misleading a bit here, since it's always defined: a default value is taken (&dummy_directory)*/
struct directory_s *search_directory(struct inode_s *inode)
{
unsigned int hashvalue = inode->st.st_ino % 2048;
struct directory_s *directory=get_dummy_directory(); /* default value */
struct directory_s *search=NULL;
pthread_mutex_lock(&directory_hashtable_mutex);
search=directory_hashtable[hashvalue];
while (search) {
if (search->inode==inode) {
directory=search;
break;
}
search=search->next;
}
pthread_mutex_unlock(&directory_hashtable_mutex);
return directory;
}
struct directory_s *get_directory_dump(struct inode_s *inode)
{
return (struct directory_s *) inode->link.link.ptr;
}
void set_directory_dump(struct inode_s *inode, struct directory_s *d)
{
inode->link.type=INODE_LINK_TYPE_DIRECTORY;
inode->link.link.ptr=(void *) d;
}
static void _add_directory_hashtable(struct directory_s *directory)
{
unsigned int hashvalue = directory->inode->st.st_ino % 2048;
pthread_mutex_lock(&directory_hashtable_mutex);
directory->prev=NULL;
directory->next=directory_hashtable[hashvalue];
directory_hashtable[hashvalue]=directory;
pthread_mutex_unlock(&directory_hashtable_mutex);
}
void _remove_directory_hashtable(struct directory_s *directory)
{
unsigned int hashvalue = directory->inode->st.st_ino % 2048;
pthread_mutex_lock(&directory_hashtable_mutex);
if (directory==directory_hashtable[hashvalue]) {
struct directory_s *next=directory->next;
directory_hashtable[hashvalue]=next;
if (next) next->prev=NULL;
} else {
struct directory_s *next=directory->next;
struct directory_s *prev=directory->prev;
if (next) next->prev=directory->prev;
if (prev) prev->next=directory->next;
}
pthread_mutex_unlock(&directory_hashtable_mutex);
directory->prev=NULL;
directory->next=NULL;
}
struct directory_s *_create_directory(struct inode_s *inode, void (* init_cb)(struct directory_s *directory), unsigned int *error)
{
struct directory_s *directory=NULL;
logoutput("_create_directory: inode %li", inode->st.st_ino);
directory=malloc(sizeof(struct directory_s));
if (directory) {
int result=0;
memset(directory, 0, sizeof(struct directory_s));
result=init_directory(directory, error);
if (result==-1) {
destroy_directory(directory);
directory=NULL;
} else {
directory->inode=inode;
(* init_cb)(directory);
_add_directory_hashtable(directory);
}
}
return directory;
}
void free_pathcalls(struct pathcalls_s *p)
{
pthread_mutex_destroy(&p->mutex);
(* p->free)(p);
}
void free_directory(struct directory_s *directory)
{
_remove_directory_hashtable(directory);
clear_skiplist(&directory->skiplist);
destroy_lock_skiplist(&directory->skiplist);
clear_simple_locking(&directory->locking);
free_pathcalls(&directory->pathcalls);
}
void destroy_directory(struct directory_s *directory)
{
free_directory(directory);
free(directory);
}
int lock_pathcalls(struct pathcalls_s *p)
{
return pthread_mutex_lock(&p->mutex);
}
int unlock_pathcalls(struct pathcalls_s *p)
{
return pthread_mutex_unlock(&p->mutex);
}
unsigned int get_path_pathcalls(struct directory_s *directory, void *ptr)
{
return (* directory->pathcalls.get_path)(directory, ptr);
}
int init_directory_hashtable()
{
memset(directory_hashtable, 0, sizeof(directory_hashtable));
for (unsigned int i=0; i<2048; i++) directory_hashtable[i]=NULL;
pthread_mutex_init(&directory_hashtable_mutex, NULL);
return 0;
}
void free_directory_hashtable()
{
memset(directory_hashtable, 0, sizeof(directory_hashtable));
for (unsigned int i=0; i<2048; i++) directory_hashtable[i]=NULL;
pthread_mutex_destroy(&directory_hashtable_mutex);
}