-
Notifications
You must be signed in to change notification settings - Fork 0
/
ramcachefs.c
2270 lines (2016 loc) · 73.4 KB
/
ramcachefs.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
/*
MIT License
Copyright (c) 2021 Sven Willner <sven.willner@yfx.de>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#define _GNU_SOURCE
#define FUSE_USE_VERSION 34
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fuse_lowlevel.h>
#include <inttypes.h>
#include <limits.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/statvfs.h>
#include <unistd.h>
#ifndef DONT_USE_MMAP
#include <sys/mman.h>
#endif
typedef char __assert_ino_t_large_enough[sizeof(fuse_ino_t) >= sizeof(uintptr_t) ? 1 : -1]; /* fuse_ino_t needs to hold a pointer */
#ifdef DEBUG
#define debug(...) fuse_log(FUSE_LOG_DEBUG, __VA_ARGS__)
#else
#define debug(...)
#endif
static const double RAMCACHEFS_TIMEOUT = 86400.0;
enum {
RAMCACHEFS_TRIGGER_PERSIST = _IO('E', 0),
};
struct ramcachefs_inode {
/* the following are all protected by data->node_mutex: */
uint64_t nlookup;
struct ramcachefs_inode* parent;
struct ramcachefs_inode* prev;
struct ramcachefs_inode* next;
struct ramcachefs_inode* first_child;
struct ramcachefs_inode* last_child;
struct ramcachefs_inode* origin; /* which original file is represented by this content+info */
struct ramcachefs_inode* orig_moved_to; /* where the original content+info of orig_name has been moved to */
/* ALWAYS inode->origin->orig_moved_to == inode && inode->orig_moved_to->origin == inode */
char* orig_name; /* NULL if only in memory yet */
char* name; /* NULL if orig_name was deleted or moved to orig_moved_to */
/* the following do not need to be protected: */
fuse_ino_t ino; /* does not change */
off_t dir_offset; /* offset in parent dir */
size_t size; /* size of content (or strlen(content) for links) */
void* content; /* file content or (null-terminated) link target */
struct timespec times[2]; /* as for utimensat: 0: atime, 1: mtime */
uid_t uid;
gid_t gid;
mode_t mode;
dev_t dev;
unsigned int changed_content : 1;
unsigned int changed_mode : 1;
unsigned int changed_name : 1;
unsigned int changed_owner : 1;
unsigned int changed_time : 1;
#ifndef DONT_USE_MMAP
unsigned int file_backed : 1; /* mmap is still backed by file */
#endif
};
struct ramcachefs_data {
struct ramcachefs_inode* root;
int direct_io;
int prepopulate;
int orig_root_fd;
int opendir_result;
unsigned long block_size;
fsblkcnt_t max_blocks;
fsfilcnt_t max_inodes;
fsblkcnt_t free_blocks; /* protected by node_mutex */
fsfilcnt_t free_inodes; /* protected by node_mutex */
int writers; /* number of current writing threads, protected by writers_mutex */
pthread_mutex_t writers_mutex; /* protects writers (number of current writing threads */
pthread_mutex_t persist_mutex; /* looks writing operations while persisting is in progress */
pthread_mutex_t node_mutex; /* protects members in ramcachefs_inode that are accessible "from the outside", e.g. from their parent directory */
#ifdef DEBUG
int debug;
#endif
};
struct ramcachefs_file_info {
size_t max_written;
int orig_flags;
};
struct ramcachefs_opts {
char* size;
int direct_io;
int noautopersist;
int prepopulate;
int trigger_persist;
unsigned long max_inodes;
};
static const struct fuse_opt ramcachefs_opts[] = {
{"--trigger-persist", offsetof(struct ramcachefs_opts, trigger_persist), 1},
{"-p", offsetof(struct ramcachefs_opts, trigger_persist), 1},
{"direct_io", offsetof(struct ramcachefs_opts, direct_io), 1},
{"maxinodes=%u", offsetof(struct ramcachefs_opts, max_inodes), 1},
{"noautopersist", offsetof(struct ramcachefs_opts, noautopersist), 1},
{"prepopulate", offsetof(struct ramcachefs_opts, prepopulate), 1},
{"size=%s", offsetof(struct ramcachefs_opts, size), 1},
FUSE_OPT_END //
};
#ifdef DEBUG
static void print_inode_simple(const char* prefix, struct ramcachefs_inode* inode) {
if (prefix) {
fuse_log(FUSE_LOG_DEBUG, "%s", prefix);
}
if (inode) {
fuse_log(FUSE_LOG_DEBUG, "%lu %s", inode->ino, inode->name);
if (inode->orig_name) {
fuse_log(FUSE_LOG_DEBUG, " [orig_name: %s]", inode->orig_name);
}
} else {
fuse_log(FUSE_LOG_DEBUG, "(null)");
}
fuse_log(FUSE_LOG_DEBUG, "\n");
}
static void print_inode(struct ramcachefs_inode* inode) {
fuse_log(FUSE_LOG_DEBUG, " ino: %lu\n", inode->ino);
fuse_log(FUSE_LOG_DEBUG, " name: %s\n", inode->name);
fuse_log(FUSE_LOG_DEBUG, " orig_name: %s\n", inode->orig_name);
fuse_log(FUSE_LOG_DEBUG, " ch_content: %d\n", inode->changed_content);
fuse_log(FUSE_LOG_DEBUG, " changed_mode: %d\n", inode->changed_mode);
fuse_log(FUSE_LOG_DEBUG, " changed_name: %d\n", inode->changed_name);
fuse_log(FUSE_LOG_DEBUG, " changed_owner: %d\n", inode->changed_owner);
fuse_log(FUSE_LOG_DEBUG, " changed_time: %d\n", inode->changed_time);
fuse_log(FUSE_LOG_DEBUG, " nlookup: %lu\n", inode->nlookup);
print_inode_simple(" parent: ", inode->parent);
print_inode_simple(" prev: ", inode->prev);
print_inode_simple(" next: ", inode->next);
print_inode_simple(" first_child: ", inode->first_child);
print_inode_simple(" last_child: ", inode->last_child);
print_inode_simple(" origin: ", inode->origin);
print_inode_simple(" orig_moved_to: ", inode->orig_moved_to);
fuse_log(FUSE_LOG_DEBUG, " dir_offset: %lu\n", inode->dir_offset);
fuse_log(FUSE_LOG_DEBUG, " size: %lu\n", inode->size);
fuse_log(FUSE_LOG_DEBUG, " atime: %lu\n", inode->times[0]);
fuse_log(FUSE_LOG_DEBUG, " mtime: %lu\n", inode->times[1]);
fuse_log(FUSE_LOG_DEBUG, " uid: %d\n", inode->uid);
fuse_log(FUSE_LOG_DEBUG, " gid: %d\n", inode->gid);
fuse_log(FUSE_LOG_DEBUG, " mode: %d\n", inode->mode);
}
static void print_parent_path(struct ramcachefs_inode* inode) {
if (inode->parent) {
print_parent_path(inode->parent);
fuse_log(FUSE_LOG_DEBUG, "%s/", inode->parent->name);
}
}
static void print_path(struct ramcachefs_inode* inode, struct ramcachefs_inode* fallbackparent, const char* fallbackname) {
if (inode) {
print_parent_path(inode);
fuse_log(FUSE_LOG_DEBUG, "%s(%ld)", inode->name, inode->ino);
} else if (fallbackparent) {
print_parent_path(fallbackparent);
fuse_log(FUSE_LOG_DEBUG, "%s/%s(not found)", fallbackparent->name, fallbackname);
} else {
fuse_log(FUSE_LOG_DEBUG, "?/%s(not found)", fallbackname);
}
}
static void print_tree(struct ramcachefs_inode* inode, int depth) {
static const int indentwidth = 4;
const int indent = depth * indentwidth;
fuse_log(FUSE_LOG_DEBUG, "%*s", indent, "");
print_inode_simple("", inode);
if (inode->orig_moved_to) {
fuse_log(FUSE_LOG_DEBUG, "%*s orig_moved_to=", indent, "");
print_inode_simple("", inode->orig_moved_to);
if (inode != inode->orig_moved_to->origin) {
fuse_log(FUSE_LOG_DEBUG, "%*s ERROR orig_moved_to->origin=", indent, "");
print_inode_simple("", inode->orig_moved_to->origin);
}
}
if (inode->origin) {
fuse_log(FUSE_LOG_DEBUG, "%*s origin=", indent, "");
print_inode_simple("", inode->origin);
if (inode != inode->origin->orig_moved_to) {
fuse_log(FUSE_LOG_DEBUG, "%*s ERROR origin->orig_moved_to=", indent, "");
print_inode_simple("", inode->origin->orig_moved_to);
}
}
struct ramcachefs_inode* child = inode->first_child;
struct ramcachefs_inode* prev = NULL;
while (child) {
print_tree(child, depth + 1);
if (prev != child->prev) {
fuse_log(FUSE_LOG_DEBUG, "%*sERROR prev=", indent + indentwidth, "");
print_inode_simple("", child->prev);
}
prev = child;
child = child->next;
}
if (prev && prev->next) {
fuse_log(FUSE_LOG_DEBUG, "%*sERROR next=", indent + indentwidth, "");
print_inode_simple("", prev->next);
}
if (prev != inode->last_child) {
fuse_log(FUSE_LOG_DEBUG, "%*sERROR last_child=", indent, "");
print_inode_simple("", inode->last_child);
}
}
#endif
static struct ramcachefs_data* get_data(fuse_req_t req) { return (struct ramcachefs_data*)fuse_req_userdata(req); }
static struct ramcachefs_inode* get_inode(fuse_req_t req, fuse_ino_t ino) {
if (ino == FUSE_ROOT_ID) {
return get_data(req)->root;
}
return (struct ramcachefs_inode*)(uintptr_t)ino;
}
static void start_writing(struct ramcachefs_data* data) {
pthread_mutex_lock(&data->writers_mutex);
++data->writers;
if (data->writers == 1) {
pthread_mutex_lock(&data->persist_mutex);
}
pthread_mutex_unlock(&data->writers_mutex);
}
static void stop_writing(struct ramcachefs_data* data) {
pthread_mutex_lock(&data->writers_mutex);
--data->writers;
if (data->writers == 0) {
pthread_mutex_unlock(&data->persist_mutex);
}
pthread_mutex_unlock(&data->writers_mutex);
}
static struct ramcachefs_inode* alloc_inode(const char* name, struct stat* stbuf) {
struct ramcachefs_inode* inode = malloc(sizeof(struct ramcachefs_inode));
if (inode) {
memset(inode, 0, sizeof(struct ramcachefs_inode));
inode->ino = (fuse_ino_t)inode;
inode->nlookup = 1;
if (name) {
inode->name = strdup(name);
if (!inode->name) {
free(inode);
return NULL;
}
}
if (stbuf) {
inode->mode = stbuf->st_mode;
inode->uid = stbuf->st_uid;
inode->gid = stbuf->st_gid;
inode->size = stbuf->st_size;
inode->times[0] = stbuf->st_atim;
inode->times[1] = stbuf->st_mtim;
inode->dev = stbuf->st_rdev;
}
}
return inode;
}
static void set_parent_unsafe(struct ramcachefs_inode* inode, struct ramcachefs_inode* parent) { /* data->node_mutex must be locked */
inode->parent = parent;
if (parent->first_child) {
parent->last_child->next = inode;
inode->prev = parent->last_child;
inode->dir_offset = parent->last_child->dir_offset + 1;
parent->last_child = inode;
} else {
inode->dir_offset = 1;
parent->first_child = inode;
parent->last_child = inode;
}
}
static void remove_inode_from_parent_unsafe(struct ramcachefs_inode* child) { /* data->node_mutex must be locked */
if (child == child->parent->first_child) {
child->parent->first_child = child->next;
}
if (child == child->parent->last_child) {
child->parent->last_child = child->prev;
}
if (child->next) {
child->next->prev = child->prev;
}
if (child->prev) {
child->prev->next = child->next;
}
child->parent = NULL;
child->prev = NULL;
child->next = NULL;
}
static void free_inode(struct ramcachefs_inode* inode) {
struct ramcachefs_inode* child = inode->first_child;
struct ramcachefs_inode* next_child;
while (child) {
next_child = child->next;
free_inode(child);
child = next_child;
}
free(inode->orig_name);
free(inode->name);
#ifdef DONT_USE_MMAP
free(inode->content);
#else
if (S_ISREG(inode->mode)) {
if (inode->content && munmap(inode->content, inode->size)) {
fuse_log(FUSE_LOG_ERR, "munmap failed: %m\n");
}
} else {
free(inode->content);
}
#endif
free(inode);
}
static int is_dot_or_dotdot(const char* name) { return name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')); }
static int cache_dir_unsafe(struct ramcachefs_data* data, struct ramcachefs_inode* dirnode, DIR* dir) { /* not thread safe */
struct ramcachefs_inode* inode;
struct stat stbuf;
DIR* subdir;
int fd;
errno = 0;
struct dirent* entry;
for (entry = readdir(dir); entry; entry = readdir(dir)) {
if (is_dot_or_dotdot(entry->d_name)) {
continue;
}
if (fstatat(dirfd(dir), entry->d_name, &stbuf, AT_SYMLINK_NOFOLLOW)) {
fuse_log(FUSE_LOG_ERR, "can't fstatat `%s': %m\n", entry->d_name);
return 1;
}
if (data->free_inodes == 0) {
fuse_log(FUSE_LOG_ERR, "no space left\n");
return 1;
}
inode = alloc_inode(entry->d_name, &stbuf);
if (!inode) {
fuse_log(FUSE_LOG_ERR, "can't allocate memory: %m\n");
return 1;
}
--data->free_inodes;
inode->orig_name = strdup(entry->d_name);
if (!inode->orig_name) {
fuse_log(FUSE_LOG_ERR, "can't allocate memory: %m\n");
goto err_out;
}
switch (stbuf.st_mode & S_IFMT) {
case S_IFDIR: /* directory */
fd = openat(dirfd(dir), entry->d_name, O_DIRECTORY | O_NOATIME | O_RDONLY);
if (fd < 0) {
fuse_log(FUSE_LOG_ERR, "can't open `%s': %m\n", entry->d_name);
goto err_out;
}
subdir = fdopendir(fd);
if (!subdir) {
fuse_log(FUSE_LOG_ERR, "can't open `%s' using fdopendir: %m\n", entry->d_name);
goto err_out;
}
if (cache_dir_unsafe(data, inode, subdir)) {
closedir(subdir);
goto err_out;
}
closedir(subdir);
break;
case S_IFLNK: /* symbolic link */
if (inode->size > 0) {
inode->content = calloc(inode->size + 1, sizeof(char));
if (!inode->content) {
fuse_log(FUSE_LOG_ERR, "can't allocate memory: %m\n");
goto err_out;
}
ssize_t res = readlinkat(dirfd(dir), entry->d_name, inode->content, inode->size);
if (res < 0) {
fuse_log(FUSE_LOG_ERR, "can't read link `%s': %m\n", entry->d_name);
free(inode->content);
inode->content = NULL;
goto err_out;
}
}
break;
case S_IFREG: /* regular file */
if (inode->size > 0) {
size_t needed_size = (inode->size + data->block_size - 1) / data->block_size;
if (needed_size > data->free_blocks) {
fuse_log(FUSE_LOG_ERR, "no space left\n");
goto err_out;
}
fd = openat(dirfd(dir), entry->d_name, O_NOATIME | O_RDONLY);
if (fd < 0) {
fuse_log(FUSE_LOG_ERR, "can't open `%s': %m\n", entry->d_name);
goto err_out;
}
#ifdef DONT_USE_MMAP
inode->content = malloc(inode->size);
if (!inode->content) {
fuse_log(FUSE_LOG_ERR, "can't allocate memory: %m\n");
close(fd);
goto err_out;
}
ssize_t res;
ssize_t size = inode->size;
char* buf = inode->content;
while (1) {
res = read(fd, buf, size);
if (res < 0) {
fuse_log(FUSE_LOG_ERR, "can't read from `%s': %m\n", entry->d_name);
goto err_out;
}
if (res == size) {
break;
}
size -= res;
buf += res;
}
#else
inode->content = mmap(NULL, inode->size, PROT_READ | PROT_WRITE, MAP_PRIVATE | (data->prepopulate ? MAP_POPULATE : 0), fd, 0);
if (inode->content == MAP_FAILED) {
fuse_log(FUSE_LOG_ERR, "mmap failed: %m\n");
close(fd);
goto err_out;
}
inode->file_backed = 1;
#endif
close(fd);
data->free_blocks -= needed_size;
}
break;
case S_IFBLK: /* block device */
case S_IFCHR: /* character device */
case S_IFIFO: /* FIFO */
case S_IFSOCK: /* socket */
/* these are handled by stat already */
break;
default:
fuse_log(FUSE_LOG_ERR, "unsupported file type for `%s'\n", entry->d_name);
goto err_out;
}
set_parent_unsafe(inode, dirnode);
}
if (errno) {
fuse_log(FUSE_LOG_ERR, "readdir failed: %m\n");
return 1;
}
return 0;
err_out:
++data->free_inodes;
free_inode(inode);
return 1;
}
static int resize_file_unsafe(struct ramcachefs_data* data,
struct ramcachefs_inode* inode,
size_t newsize) { /* must be protected by start/stop_writing(data) */
if (newsize == inode->size) {
return 0;
}
if (newsize == 0) {
#ifdef DONT_USE_MMAP
free(inode->content);
#else
if (munmap(inode->content, inode->size)) {
fuse_log(FUSE_LOG_ERR, "munmap failed: %m\n");
}
inode->file_backed = 0;
#endif
data->free_blocks += (inode->size + data->block_size - 1) / data->block_size;
inode->content = NULL;
inode->size = 0;
return 0;
}
if (newsize > inode->size
&& data->free_blocks < (newsize + data->block_size - 1) / data->block_size - (inode->size + data->block_size - 1) / data->block_size) {
return ENOSPC;
}
void* newbuf;
#ifdef DONT_USE_MMAP
newbuf = realloc(inode->content, newsize);
if (!newbuf) {
return ENOMEM;
}
if (newsize > inode->size) {
memset(newbuf + inode->size, 0, newsize - inode->size);
}
#else
if (inode->file_backed || !inode->content) {
newbuf = mmap(NULL, newsize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
} else {
newbuf = mremap(inode->content, inode->size, newsize, MREMAP_MAYMOVE);
}
if (newbuf == MAP_FAILED) {
fuse_log(FUSE_LOG_ERR, "mmap or mremap failed: %m\n");
return errno;
}
if (inode->file_backed) {
memcpy(newbuf, inode->content, inode->size < newsize ? inode->size : newsize);
if (munmap(inode->content, inode->size)) {
fuse_log(FUSE_LOG_ERR, "munmap failed: %m\n");
}
inode->file_backed = 0;
}
#endif
data->free_blocks += (inode->size + data->block_size - 1) / data->block_size;
data->free_blocks -= (newsize + data->block_size - 1) / data->block_size;
inode->content = newbuf;
inode->size = newsize;
return 0;
}
static void fill_stat(struct stat* stbuf, struct ramcachefs_inode* inode) {
stbuf->st_dev = 0; /* ignored by libfuse */
stbuf->st_ino = inode->ino;
stbuf->st_mode = inode->mode;
stbuf->st_nlink = S_ISDIR(inode->mode) ? 2 : 1;
stbuf->st_uid = inode->uid;
stbuf->st_gid = inode->gid;
stbuf->st_rdev = inode->dev;
stbuf->st_size = inode->size;
stbuf->st_blksize = 0; /* ignored by libfuse */
stbuf->st_blocks = (inode->size + 511) / 512;
stbuf->st_atim = inode->times[0];
stbuf->st_mtim = inode->times[1];
}
static struct ramcachefs_inode* find_child(struct ramcachefs_data* data, struct ramcachefs_inode* inode, const char* name) {
pthread_mutex_lock(&data->node_mutex);
struct ramcachefs_inode* child = inode->first_child;
while (child) {
if (child->name && strcmp(name, child->name) == 0) {
goto out;
}
child = child->next;
}
out:
pthread_mutex_unlock(&data->node_mutex);
return child;
}
static int get_original_path_internal_unsafe(struct ramcachefs_inode* inode, char** out, int is_last, int came_from_origin) {
if (!inode->parent) {
return 0;
}
if (inode->origin && !came_from_origin) { /* make sure to not jump twice using origin (for swapped inodes) */
return get_original_path_internal_unsafe(inode->origin, out, is_last, 1);
}
int offset = get_original_path_internal_unsafe(inode->parent, out, 0, 0);
int len = strlen(inode->orig_name);
*out = realloc(*out, offset + len + (is_last ? 1 : 2));
memcpy(*out + offset, inode->orig_name, len + 1);
if (!is_last) {
(*out)[offset + len] = '/';
(*out)[offset + len + 1] = '\0';
return offset + len + 1;
}
return offset + len;
}
static char* get_original_path_unsafe(struct ramcachefs_inode* inode) {
char* out = NULL;
get_original_path_internal_unsafe(inode, &out, 1, 0);
return out;
}
static int persist_internal_unsafe(struct ramcachefs_data* data, int parentfd, struct ramcachefs_inode* inode, int depth) {
#ifdef DEBUG
const int indent = depth * 4;
fuse_log(FUSE_LOG_DEBUG, "%*s", indent, "");
print_inode_simple("", inode);
#endif
#ifdef DEBUG_DETAILS
if (inode->orig_moved_to) {
fuse_log(FUSE_LOG_DEBUG, "%*s orig_moved_to=", indent, "");
print_inode_simple("", inode->orig_moved_to);
if (inode != inode->orig_moved_to->origin) {
fuse_log(FUSE_LOG_DEBUG, "%*s ERROR orig_moved_to->origin=", indent, "");
print_inode_simple("", inode->orig_moved_to->origin);
}
}
if (inode->origin) {
fuse_log(FUSE_LOG_DEBUG, "%*s origin=", indent, "");
print_inode_simple("", inode->origin);
if (inode != inode->origin->orig_moved_to) {
fuse_log(FUSE_LOG_DEBUG, "%*s ERROR origin->orig_moved_to=", indent, "");
print_inode_simple("", inode->origin->orig_moved_to);
}
}
#endif
if (inode->orig_moved_to && inode->orig_moved_to->orig_moved_to != inode) {
return 1;
}
if (inode->origin) {
char* original_path = get_original_path_unsafe(inode);
debug("%*s move %s -> %s\n", indent, "", original_path, inode->name);
int exchange = inode->origin->origin == inode;
if (renameat2(data->orig_root_fd, original_path, parentfd, inode->name, exchange ? RENAME_EXCHANGE : 0)) {
fuse_log(FUSE_LOG_ERR, "can't move `%s' to `%s': %m\n", original_path, inode->origin->orig_name);
return -1;
}
if (exchange) {
char* tmpchar = inode->orig_name;
inode->orig_name = inode->origin->orig_name;
inode->origin->orig_name = tmpchar;
} else {
free(inode->origin->orig_name);
inode->origin->orig_name = NULL;
free(inode->orig_name);
inode->orig_name = strdup(inode->name);
}
inode->changed_name = 0;
free(original_path);
if (!inode->origin->orig_moved_to->name) {
free(inode->origin->orig_moved_to->orig_name);
inode->origin->orig_moved_to->orig_name = NULL;
}
inode->origin->orig_moved_to = NULL;
inode->origin = NULL;
} else if (inode->name) {
if (inode->changed_name) {
debug("%*s rename %s -> %s\n", indent, "", inode->orig_name, inode->name);
if (renameat2(parentfd, inode->orig_name, parentfd, inode->name, RENAME_NOREPLACE)) {
fuse_log(FUSE_LOG_ERR, "can't rename `%s' to `%s': %m\n", inode->orig_name, inode->name);
return -1;
}
free(inode->orig_name);
inode->orig_name = strdup(inode->name);
inode->changed_name = 0;
}
if (!inode->orig_name) {
debug("%*s create\n", indent, "");
switch (inode->mode & S_IFMT) {
case S_IFDIR: /* directory */
if (mkdirat(parentfd, inode->name, inode->mode)) {
fuse_log(FUSE_LOG_ERR, "can't mkdir `%s': %m\n", inode->name);
return -1;
}
break;
case S_IFLNK: /* symbolic link */
if (symlinkat(inode->content, parentfd, inode->name)) {
fuse_log(FUSE_LOG_ERR, "can't symlink `%s': %m\n", inode->name);
return -1;
}
break;
case S_IFREG: /* regular file */
inode->changed_content = 1;
break;
case S_IFBLK: /* block device */
case S_IFCHR: /* character device */
case S_IFSOCK: /* socket */
if (mknodat(parentfd, inode->name, inode->mode, inode->dev)) {
fuse_log(FUSE_LOG_ERR, "can't mknod `%s': %m\n", inode->name);
return -1;
}
break;
case S_IFIFO: /* FIFO */
if (mkfifoat(parentfd, inode->name, inode->mode)) {
fuse_log(FUSE_LOG_ERR, "can't mkfifo `%s': %m\n", inode->name);
return -1;
}
break;
fuse_log(FUSE_LOG_ERR, "can't persist socket file `%s'\n", inode->name);
break;
default:
fuse_log(FUSE_LOG_ERR, "unsupported file type for `%s'\n", inode->name);
return -1;
}
inode->changed_name = 0;
inode->changed_mode = 0; /* either already handled my mkdirat/symlinkat/mknodat/mkfifoat or later by openat */
inode->changed_owner = 1;
inode->changed_time = 1;
inode->orig_name = strdup(inode->name);
}
#ifdef DEBUG_DETAILS
if (strcmp(inode->name, inode->orig_name)) {
print_inode(inode);
return -1;
}
#endif
if (inode->changed_content) {
if (S_ISREG(inode->mode)) {
debug("%*s write\n", indent, "");
#ifdef DONT_USE_MMAP
int fd = openat(parentfd, inode->name, O_CREAT | O_TRUNC | O_WRONLY, inode->mode);
if (fd >= 0) {
ssize_t written;
ssize_t size = inode->size;
void* buf = inode->content;
while (1) {
errno = 0;
written = write(fd, buf, size);
if (written < 0) {
if (errno == EINTR) {
continue;
}
fuse_log(FUSE_LOG_ERR, "can't write to `%s': %m\n", inode->name);
close(fd);
return -1;
}
if (written == size) {
break;
}
size -= written;
buf += written;
}
#else
int fd = openat(parentfd, inode->name, O_CREAT | O_RDWR, inode->mode);
if (fd >= 0) {
if (ftruncate(fd, inode->size)) {
fuse_log(FUSE_LOG_ERR, "ftruncate `%s' failed: %m\n", inode->name);
close(fd);
return -1;
}
if (inode->size > 0) {
void* buf = mmap(NULL, inode->size, PROT_WRITE, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED) {
fuse_log(FUSE_LOG_ERR, "mmap `%s' failed: %m\n", inode->name);
close(fd);
return -1;
}
memcpy(buf, inode->content, inode->size);
if (munmap(buf, inode->size)) {
fuse_log(FUSE_LOG_ERR, "munmap `%s' failed: %m\n", inode->name);
close(fd);
return -1;
}
if (munmap(inode->content, inode->size)) {
fuse_log(FUSE_LOG_ERR, "munmap `%s' failed: %m\n", inode->name);
close(fd);
return -1;
}
inode->content = mmap(NULL, inode->size, PROT_READ | PROT_WRITE, MAP_PRIVATE | (data->prepopulate ? MAP_POPULATE : 0), fd, 0);
if (inode->content == MAP_FAILED) {
fuse_log(FUSE_LOG_ERR, "mmap `%s' failed: %m\n", inode->name);
close(fd);
return -1;
}
inode->file_backed = 1;
}
#endif
close(fd);
} else {
fuse_log(FUSE_LOG_ERR, "can't open `%s': %m\n", inode->name);
return -1;
}
}
inode->changed_content = 0;
}
if (inode->changed_mode) {
debug("%*s chmod\n", indent, "");
if (fchmodat(parentfd, inode->name, inode->mode, AT_SYMLINK_NOFOLLOW)) {
if (errno != ENOTSUP || fchmodat(parentfd, inode->name, inode->mode, 0)) {
fuse_log(FUSE_LOG_ERR, "can't chmod `%s': %m\n", inode->name);
return -1;
}
}
inode->changed_mode = 0;
}
if (inode->changed_owner) {
debug("%*s chown\n", indent, "");
if (fchownat(parentfd, inode->name, inode->uid, inode->gid, AT_SYMLINK_NOFOLLOW)) {
if (errno != ENOTSUP || fchownat(parentfd, inode->name, inode->uid, inode->gid, 0)) {
fuse_log(FUSE_LOG_ERR, "can't chown `%s': %m\n", inode->name);
return -1;
}
}
inode->changed_owner = 0;
}
if (inode->changed_time) {
debug("%*s change time\n", indent, "");
if (utimensat(parentfd, inode->name, inode->times, AT_SYMLINK_NOFOLLOW)) {
fuse_log(FUSE_LOG_ERR, "can't utimensat `%s': %m\n", inode->name);
return -1;
}
inode->changed_time = 0;
}
}
if (inode->orig_name) {
if (S_ISDIR(inode->mode)) {
int res = 0;
int fd = openat(parentfd, inode->orig_name, O_DIRECTORY);
if (fd < 0) {
fuse_log(FUSE_LOG_ERR, "can't open `%s': %m\n", inode->orig_name);
return -1;
}
struct ramcachefs_inode* next;
struct ramcachefs_inode* child = inode->first_child;
while (child) {
res |= persist_internal_unsafe(data, fd, child, depth + 1);
if (res < 0) {
close(fd);
return res;
}
next = child->next;
if (!child->name && !child->orig_name && !child->orig_moved_to && !child->origin) {
debug("%*s removing inode\n", indent, "");
remove_inode_from_parent_unsafe(child);
++data->free_inodes;
free_inode(child);
}
child = next;
}
close(fd);
}
if (!inode->name) {
debug("%*s delete\n", indent, "");
if (unlinkat(parentfd, inode->orig_name, S_ISDIR(inode->mode) ? AT_REMOVEDIR : 0)) {
fuse_log(FUSE_LOG_ERR, "can't rename `%s' to `%s': %m\n", inode->orig_name, inode->name);
return -1;
}
free(inode->orig_name);
inode->orig_name = NULL;
}
}
return 0;
}
static int persist(struct ramcachefs_data* data) {
debug("persisting...\n");
pthread_mutex_lock(&data->persist_mutex);
pthread_mutex_lock(&data->node_mutex);
#ifdef DEBUG_DETAILS
fuse_log(FUSE_LOG_DEBUG, "\nbefore persisting:\n");
print_tree(data->root, 0);
#endif
int res = 1;
int dir = openat(data->orig_root_fd, ".", O_DIRECTORY);
if (dir < 0) {
fuse_log(FUSE_LOG_ERR, "can't open `%s': %m\n", data->root->orig_name);
res = -1;
goto out1;
}
struct ramcachefs_inode* next;
struct ramcachefs_inode* child;
while (res) {
#ifdef DEBUG_DETAILS
fuse_log(FUSE_LOG_DEBUG, "\npersisting round:\n");
#endif
res = 0;
child = data->root->first_child;
while (child) {
res |= persist_internal_unsafe(data, dir, child, 1);
if (res < 0) {
goto out2;
}
next = child->next;
if (!child->name && !child->orig_name && !child->orig_moved_to && !child->origin) {
debug(" removing inode\n");
remove_inode_from_parent_unsafe(child);
++data->free_inodes;
free_inode(child);
}
child = next;
}
}
if (data->root->changed_mode) {
debug(" chmod\n");
if (fchmod(dir, data->root->mode)) {
fuse_log(FUSE_LOG_ERR, "can't chmod `%s': %m\n", data->root->orig_name);
res = -1;
goto out2;
}
data->root->changed_mode = 0;
}
if (data->root->changed_owner) {
debug(" chown\n");
if (fchown(dir, data->root->uid, data->root->gid)) {
fuse_log(FUSE_LOG_ERR, "can't chown `%s': %m\n", data->root->orig_name);
res = -1;
goto out2;
}
data->root->changed_owner = 0;
}
if (data->root->changed_time) {
debug(" change time\n");
if (futimens(dir, data->root->times)) {
fuse_log(FUSE_LOG_ERR, "can't utimensat `%s': %m\n", data->root->orig_name);
res = -1;
goto out2;
}
data->root->changed_time = 0;
}
out2:
close(dir);
out1:
#ifdef DEBUG_DETAILS
fuse_log(FUSE_LOG_DEBUG, "\nafter persisting:\n");
print_tree(data->root, 0);
fuse_log(FUSE_LOG_DEBUG, "\n");
#endif
pthread_mutex_unlock(&data->node_mutex);
pthread_mutex_unlock(&data->persist_mutex);
return res < 0 ? 1 : 0;
}
static void forget_inode_unsafe(struct ramcachefs_data* data, struct ramcachefs_inode* inode, uint64_t nlookup) { /* data->node_mutex must be locked */
if (nlookup < inode->nlookup) {
inode->nlookup -= nlookup;
} else {
if (inode->origin) {
/* this inode represents an original file, mark this as deleted as well */
inode->origin->orig_moved_to = NULL;
inode->origin = NULL;
}
if (inode->nlookup) {
inode->nlookup = 0;
free(inode->name);
inode->name = NULL;
if (S_ISREG(inode->mode)) {
data->free_blocks += (inode->size + data->block_size - 1) / data->block_size;
}
#ifdef DONT_USE_MMAP
free(inode->content);
#else
if (S_ISREG(inode->mode)) {
if (inode->content && munmap(inode->content, inode->size)) {
fuse_log(FUSE_LOG_ERR, "munmap failed: %m\n");
}