forked from containers/fuse-overlayfs
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.c
4027 lines (3383 loc) · 101 KB
/
main.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
/* fuse-migratefs: Migration Filesystem in Userspace
Copyright (C) 2018-2019 Stephane Thiell <[email protected]>
Forked from fuse-overlayfs: Overlay Filesystem in Userspace
Copyright (C) 2018 Giuseppe Scrivano <[email protected]>
Copyright (C) 2018-2019 Red Hat Inc.
Copyright (C) 2001-2007 Miklos Szeredi <[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 3 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, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#define FUSE_USE_VERSION 32
#define _FILE_OFFSET_BITS 64
#define ENABLE_IOCTL 0
#define MIGRATEFS_MT 1
#define MIGRATEFS_MT_MAX_IDLE_THREADS 10
#define MIGRATEFS_MT_CLONE_FD false
// VERB_LEVEL:
// 0 = quiet
// 1 = verbose (copyup and errors only)
// 2 = debug
#define VERB_LEVEL 1
#define COPYUP_ON_SETXATTR 0 // setxattr and removexattr
#define DELETE_FILE_ON_COPYUP 1
#include <config.h>
#include <fuse.h>
#include <fuse_lowlevel.h>
#include <unistd.h>
#include <execinfo.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include <limits.h>
#include <dirent.h>
#include <assert.h>
#include <errno.h>
#include <err.h>
#include <error.h>
#include <inttypes.h>
#include <fcntl.h>
#include <grp.h>
#include <hash.h>
#include <sys/statvfs.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/sysmacros.h>
#include <sys/xattr.h>
#include <linux/fs.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <pthread.h>
/* We consider short caching of ATTR safe because we check attributes
* on lookup. This allows to reduce the number of ovl_getattr() calls,
* that would otherwise follow ovl_lookup() to get the attributes in
* case of stat() for example.
*/
#define ATTR_TIMEOUT 1
/* However, we don't cache entries because layers may be modified
* directly and/or remotely.
*/
#define ENTRY_TIMEOUT 0
#define NODE_TO_INODE(x) ((fuse_ino_t) x)
/* Check GCC version, just to be safe */
#if !defined(__GNUC__) || (__GNUC__ < 4) || (__GNUC_MINOR__ < 1)
# error atomic ops work only with GCC newer than version 4.1
#endif /* GNUC >= 4.1 */
#if defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 6) && !defined __cplusplus
_Static_assert (sizeof (fuse_ino_t) >= sizeof (uintptr_t),
"fuse_ino_t too small to hold uintptr_t values!");
#else
struct _uintptr_to_must_hold_fuse_ino_t_dummy_struct
{
unsigned _uintptr_to_must_hold_fuse_ino_t:
((sizeof (fuse_ino_t) >= sizeof (uintptr_t)) ? 1 : -1);
};
#endif
static int ngroups; // sysconf(_SC_NGROUPS_MAX)
static pthread_key_t key_suppl_gids;
static pthread_key_t key_ctx_uid;
static pthread_once_t key_once = PTHREAD_ONCE_INIT;
static pthread_mutex_t ovl_node_global_lock;
#if VERB_LEVEL > 0
#define verb_print(fmt, ...) \
do { fprintf(stderr, "version=" VERSION " " fmt, __VA_ARGS__); } while (0)
#else
#define verb_print(fmt, ...) do {} while (0)
#endif
#if VERB_LEVEL > 1
#define debug_print(fmt, ...) \
do { fprintf(stderr, fmt, __VA_ARGS__); } while (0)
#else
#define debug_print(fmt, ...) do {} while (0)
#endif
static void
make_keys()
{
(void) pthread_key_create(&key_suppl_gids, &free);
(void) pthread_key_create(&key_ctx_uid, &free);
}
static void FUSE_ENTER_NOSETGROUPS(fuse_req_t req)
{
const struct fuse_ctx *ctx = fuse_req_ctx (req);
uid_t *ctx_uid_ptr;
// use direct syscall as the libc function will synchronize it for all threads
if (syscall(SYS_setresgid, -1, ctx->gid, -1) < 0)
debug_print ("FUSE_EXIT: setresgid failed with errno=%d\n", errno);
if ((ctx_uid_ptr = pthread_getspecific(key_ctx_uid)) == NULL) {
ctx_uid_ptr = malloc(sizeof(*ctx_uid_ptr));
if (ctx_uid_ptr == NULL)
error (EXIT_FAILURE, ENOMEM, "cannot allocate memory");
(void) pthread_setspecific(key_ctx_uid, ctx_uid_ptr);
}
*ctx_uid_ptr = ctx->uid;
if (syscall(SYS_setresuid, -1, ctx->uid, -1) < 0)
debug_print ("FUSE_EXIT: setresuid failed with errno=%d\n", errno);
}
static void FUSE_ENTER(fuse_req_t req)
{
int ret;
gid_t *suppl_gids;
(void) pthread_once(&key_once, make_keys);
if ((suppl_gids = pthread_getspecific(key_suppl_gids)) == NULL) {
suppl_gids = malloc(sizeof(*suppl_gids) * ngroups);
if (suppl_gids == NULL)
error (EXIT_FAILURE, ENOMEM, "cannot allocate memory");
(void) pthread_setspecific(key_suppl_gids, suppl_gids);
}
ret = fuse_req_getgroups(req, sizeof(*suppl_gids) * ngroups, suppl_gids);
if (ret < 0)
{
debug_print ("fuse_req_getgroups failed with errno=%d\n", -ret);
}
else
{
ret = syscall(SYS_setgroups, ret, suppl_gids);
if (ret < 0)
{
debug_print ("setgroups failed with errno=%d\n", errno);
}
}
FUSE_ENTER_NOSETGROUPS(req); // UID and primary GID
}
static void FUSE_EXIT_NOSETGROUPS()
{
// use direct syscalls as the libc functions will synchronize all threads
if (syscall(SYS_setresuid, -1, 0, -1) < 0)
debug_print ("FUSE_EXIT: setresuid failed with errno=%d\n", errno);
if (syscall(SYS_setresgid, -1, 0, -1) < 0)
debug_print ("FUSE_EXIT: setresgid failed with errno=%d\n", errno);
}
static void FUSE_EXIT()
{
gid_t gid = 0;
FUSE_EXIT_NOSETGROUPS(); // UID and primary GID
if (syscall(SYS_setgroups, 1, &gid) < 0)
debug_print ("FUSE_EXIT: setgroups failed with errno=%d\n", errno);
}
static uid_t FUSE_GETCURRENTUID()
{
uid_t *ctx_uid_ptr = pthread_getspecific(key_ctx_uid);
assert (ctx_uid_ptr != NULL);
return *ctx_uid_ptr;
}
// temporary priv elevation helpers for copyup
static void FUSE_ENTER_ROOTPRIV()
{
if (syscall(SYS_setresuid, -1, 0, -1) < 0)
verb_print ("FUSE_ENTER_ROOTPRIV: setresuid failed with errno=%d\n", errno);
}
static void FUSE_EXIT_ROOTPRIV()
{
uid_t saved_ctx_uid = FUSE_GETCURRENTUID();
if (syscall(SYS_setresuid, -1, saved_ctx_uid, -1) < 0)
verb_print ("FUSE_EXIT_ROOTPRIV: setresuid uid=%u failed with errno=%d\n",
saved_ctx_uid, errno);
}
/* Obtain a backtrace and print it to stdout. */
static void
print_trace (void)
{
void *array[10];
size_t size;
char **strings;
size_t i;
size = backtrace (array, 10);
strings = backtrace_symbols (array, size);
verb_print ("Obtained %zd stack frames.\n", size);
for (i = 0; i < size; i++)
verb_print ("%s\n", strings[i]);
free (strings);
}
struct ovl_layer
{
struct ovl_layer *next;
char *path;
int fd;
bool low;
};
struct ovl_node
{
struct ovl_node *parent;
Hash_table *children;
struct ovl_layer *layer;
char *path;
char *name;
uint64_t lookups;
uint64_t dircnt;
ino_t ino;
pthread_mutex_t dirlock;
unsigned int loaded : 1; // for load_dir()
unsigned int multilayer : 1; // node in multiple layers? to enable some optimization
};
struct ovl_data
{
struct fuse_session *se;
int debug;
char *mountpoint;
char *lowerdir;
char *upperdir;
unsigned int max_idle_threads;
int mt;
int omitxattr;
struct ovl_layer *layers;
struct ovl_node *root;
};
static const struct fuse_opt ovl_opts[] = {
{"max_idle_threads=%u",
offsetof (struct ovl_data, max_idle_threads), 0},
{"mt=%d",
offsetof (struct ovl_data, mt), 0},
{"omitxattr",
offsetof (struct ovl_data, omitxattr), 1},
{"lowerdir=%s",
offsetof (struct ovl_data, lowerdir), 0},
{"upperdir=%s",
offsetof (struct ovl_data, upperdir), 0},
FUSE_OPT_END
};
/* Kernel definitions. */
typedef unsigned char u8;
typedef unsigned char uuid_t[16];
static struct ovl_data *
ovl_data (fuse_req_t req)
{
return (struct ovl_data *) fuse_req_userdata (req);
}
static int
get_next_wd_counter ()
{
static volatile int wd_counter = 1;
return __sync_fetch_and_add(&wd_counter, 1);
}
/* Useful in a gdb session. */
void
dump_directory (struct ovl_node *node)
{
struct ovl_node *it;
if (node->children == NULL)
return;
for (it = hash_get_first (node->children); it; it = hash_get_next (node->children, it))
printf ("ENTRY: %s (%s)\n", it->name, it->path);
}
static bool
ovl_debug (fuse_req_t req)
{
return ovl_data (req)->debug != 0;
}
static void
ovl_init (void *userdata, struct fuse_conn_info *conn)
{
conn->want |= FUSE_CAP_DONT_MASK | FUSE_CAP_SPLICE_READ | FUSE_CAP_SPLICE_MOVE;
conn->want &= ~FUSE_CAP_PARALLEL_DIROPS;
verb_print ("ovl_init: conn->want=0x%x\n", conn->want);
// conn->want |= FUSE_IOCTL_UNRESTRICTED;
}
static struct ovl_layer *
get_upper_layer (struct ovl_data *lo)
{
return lo->layers;
}
static struct ovl_layer *
get_lower_layers (struct ovl_data *lo)
{
return lo->layers->next;
}
static inline bool
node_dirp (struct ovl_node *n)
{
return n->children != NULL;
}
static int
node_dirfd (struct ovl_node *n)
{
return n->layer->fd;
}
static bool
has_prefix (const char *str, const char *pref)
{
while (1)
{
if (*pref == '\0')
return true;
if (*str == '\0')
return false;
if (*pref != *str)
return false;
str++;
pref++;
}
return false;
}
static ino_t
masked_inode(ino_t ino, struct ovl_layer *layer)
{
/* Use high 4-bits for layer ID */
uint64_t layerid = 1ULL; // upper uses 0x1
// lowers use higher ids
if (layer->low) // TODO: change boolean to layer index to support multiple lowers
layerid = 2ULL;
return (layerid << 60ULL) + (ino & ~(layerid << 60ULL));
}
static int
rpl_stat (fuse_req_t req, struct ovl_node *node, struct stat *st)
{
int ret;
struct ovl_data *data = ovl_data (req);
ret = TEMP_FAILURE_RETRY (fstatat (node_dirfd (node), node->path, st, AT_SYMLINK_NOFOLLOW));
if (ret < 0)
{
debug_print ("rpl_stat: fstatat path=%s failed with errno=%d\n", node->path, errno);
return ret;
}
st->st_ino = masked_inode(st->st_ino, node->layer);
if (ret == 0 && node_dirp (node) && node->multilayer)
{
struct ovl_node *it;
pthread_mutex_lock (&ovl_node_global_lock);
// node->dircnt provides a cached value for st_nlink or is set to 0 if not available
if (node->dircnt > 0)
{
debug_print ("rpl_stat: using dircnt=%d for path=%s\n", node->dircnt, node->path);
st->st_nlink = node->dircnt;
}
else
{
st->st_nlink = 2;
for (it = hash_get_first (node->children); it; it = hash_get_next (node->children, it))
{
if (node_dirp (it))
st->st_nlink++;
}
node->dircnt = st->st_nlink; // set dircnt cache
debug_print ("rpl_stat: computed st_nlink=%d for path=%s\n", node->dircnt, node->path);
}
pthread_mutex_unlock (&ovl_node_global_lock);
}
return ret;
}
static void
node_mark_all_free (void *p)
{
struct ovl_node *it, *n = (struct ovl_node *) p;
n->lookups = 0;
if (n->children)
{
for (it = hash_get_first (n->children); it; it = hash_get_next (n->children, it))
node_mark_all_free (it);
}
}
// must be called with mutex locked
static void
node_free (void *p)
{
struct ovl_node *n = (struct ovl_node *) p;
if (n == NULL)
return;
if (n->parent)
{
if (n->parent->children == NULL)
{
// should never happen
verb_print ("node_free: ERROR parent->children==NULL lookups=%lu path=%s name=%s\n",
n->lookups, n->path, n->name);
print_trace ();
n->parent = NULL; // workaround
goto destroy;
}
if (hash_lookup (n->parent->children, n) == n)
hash_delete (n->parent->children, n);
n->parent = NULL;
if (n->lookups > 0)
{
debug_print ("node_free: unlink parent but NO FREE ino=%" PRIu64 " lookups=%lu path=%s\n", n,
n->lookups, n->path);
}
}
debug_print ("do_forget: calling node_free lookups=%lu path=%s name=%s\n",
n->lookups, n->path, n->name);
if (n->lookups > 0)
return;
debug_print ("node_free: FREE ino=%" PRIu64 " path=%s name=%s\n", n, n->path, n->name);
if (n->children)
{
struct ovl_node *it, *next;
for (it = hash_get_first (n->children); it; it = next)
{
next = hash_get_next (n->children, it);
node_free (it);
}
hash_free (n->children);
n->children = NULL;
pthread_mutex_destroy (&n->dirlock);
}
destroy:
free (n->name);
free (n->path);
free (n);
return;
}
// must be called with mutex locked
static void
do_forget (fuse_ino_t ino, uint64_t nlookup)
{
struct ovl_node *n;
if (ino == FUSE_ROOT_ID)
return;
n = (struct ovl_node *) ino;
debug_print ("do_forget: path=%s name=%s lookups=%d nlookup=%d\n",
n->path, n->name, n->lookups, nlookup);
if (n->lookups < nlookup)
{
verb_print ("do_forget: ERROR path=%s name=%s lookups=%d nlookup=%d\n",
n->path, n->name, n->lookups, nlookup);
return;
}
assert (n->lookups >= nlookup);
n->lookups -= nlookup;
if (n->lookups == 0)
{
//verb_print ("do_forget: calling node_free path=%s name=%s\n",
// n->path, n->name);
node_free (n);
}
}
static void ref_node (fuse_ino_t ino)
{
int saved_errno = errno;
struct ovl_node *n = (struct ovl_node *) ino;
pthread_mutex_lock (&ovl_node_global_lock);
n->lookups++;
pthread_mutex_unlock (&ovl_node_global_lock);
errno = saved_errno;
}
static void unref_node (fuse_ino_t ino)
{
int saved_errno = errno;
struct ovl_node *n = (struct ovl_node *) ino;
pthread_mutex_lock (&ovl_node_global_lock);
do_forget (NODE_TO_INODE(n), 1);
pthread_mutex_unlock (&ovl_node_global_lock);
errno = saved_errno;
}
static void
ovl_forget (fuse_req_t req, fuse_ino_t ino, uint64_t nlookup)
{
// this is safe to proceed as root
debug_print ("ovl_forget(ino=%" PRIu64 ", nlookup=%lu)\n", ino, nlookup);
pthread_mutex_lock (&ovl_node_global_lock);
do_forget (ino, nlookup);
pthread_mutex_unlock (&ovl_node_global_lock);
fuse_reply_none (req);
}
static size_t
node_hasher (const void *p, size_t s)
{
struct ovl_node *n = (struct ovl_node *) p;
return hash_string (n->name, s);
}
static bool
node_compare (const void *n1, const void *n2)
{
struct ovl_node *node1 = (struct ovl_node *) n1;
struct ovl_node *node2 = (struct ovl_node *) n2;
return strcmp (node1->name, node2->name) == 0 ? true : false;
}
static struct ovl_node *
make_ovl_node (const char *path, struct ovl_layer *layer, const char *name, ino_t ino, bool dir_p, struct ovl_node *parent)
{
struct ovl_node *ret = malloc (sizeof (*ret));
if (ret == NULL)
{
errno = ENOMEM;
return NULL;
}
ret->parent = parent;
ret->lookups = 0;
ret->dircnt = 0;
if (dir_p)
pthread_mutex_init (&ret->dirlock, NULL);
ret->layer = layer;
ret->ino = ino;
ret->name = strdup (name);
if (ret->name == NULL)
{
free (ret);
errno = ENOMEM;
return NULL;
}
if (has_prefix (path, "./") && path[2])
path += 2;
ret->path = strdup (path);
if (ret->path == NULL)
{
free (ret->name);
free (ret);
errno = ENOMEM;
return NULL;
}
ret->loaded = 0;
ret->multilayer = 0;
if (!dir_p)
ret->children = NULL;
else
{
ret->children = hash_initialize (10, NULL, node_hasher, node_compare, node_free);
if (ret->children == NULL)
{
free (ret->path);
free (ret->name);
free (ret);
errno = ENOMEM;
return NULL;
}
}
if (ret->ino == 0)
{
struct stat st;
struct ovl_layer *it;
char path[PATH_MAX];
int layercnt = 0;
strncpy (path, ret->path, sizeof (path) - 1);
path[PATH_MAX - 1] = '\0';
for (it = layer; it; it = it->next)
{
int fd = TEMP_FAILURE_RETRY (openat (it->fd, path, O_RDONLY|O_NONBLOCK|O_NOFOLLOW|O_PATH));
if (fd < 0)
continue;
if (fstat (fd, &st) == 0)
{
ret->ino = masked_inode(st.st_ino, it);
layercnt++;
}
close (fd);
}
if (layercnt > 1)
ret->multilayer = 1;
}
//verb_print ("make_ovl_node: ALLOC path=%s name=%s\n", ret->path, ret->name);
return ret;
}
static struct ovl_node *
insert_node (struct ovl_node *parent, struct ovl_node *item, bool replace, bool retain)
{
struct ovl_node *old = NULL, *prev_parent;
int ret;
pthread_mutex_lock (&ovl_node_global_lock);
parent->dircnt = 0; // invalidate dircnt cache
prev_parent = item->parent;
if (prev_parent)
{
prev_parent->dircnt = 0; // invalidate dircnt cache
if (hash_lookup (prev_parent->children, item) == item)
hash_delete (prev_parent->children, item);
}
if (replace)
{
old = hash_delete (parent->children, item);
if (old)
node_free (old);
}
ret = hash_insert_if_absent (parent->children, item, (const void **) &old);
if (ret < 0)
{
node_free (item);
pthread_mutex_unlock (&ovl_node_global_lock);
errno = ENOMEM;
return NULL;
}
if (ret == 0)
{
node_free (item);
old->loaded = 1; // for load_dir()
if (retain)
old->lookups++;
if (old->parent == NULL)
verb_print ("insert_node: ERROR (old) parent==NULL path=%s\n", old->path);
if (old->parent != NULL && old->parent->children == NULL)
verb_print ("insert_node: ERROR (old) parent->children==NULL path=%s\n", old->path);
pthread_mutex_unlock (&ovl_node_global_lock);
return old;
}
item->parent = parent;
if (item->parent && item->parent->children == NULL)
verb_print ("insert_node: ERROR (new) parent->children==NULL path=%s\n", item->path);
item->loaded = 1; // for load_dir()
if (retain)
item->lookups++;
pthread_mutex_unlock (&ovl_node_global_lock);
return item;
}
static struct ovl_node *
load_dir (struct ovl_data *lo, struct ovl_node *n, struct ovl_layer *layer, char *path, char *name)
{
DIR *dp;
struct dirent *dent;
struct stat st;
struct ovl_layer *it, *upper_layer = get_upper_layer (lo);
struct ovl_node *nit, *next;
int dir_layercnt = 0;
if (!n)
{
n = make_ovl_node (path, layer, name, 0, true, NULL);
if (n == NULL)
{
errno = ENOMEM;
return NULL;
}
}
pthread_mutex_lock (&ovl_node_global_lock);
n->dircnt = 0; // invalidate dircnt cache
for (nit = hash_get_first (n->children); nit; nit = hash_get_next (n->children, nit))
nit->loaded = 0;
pthread_mutex_unlock (&ovl_node_global_lock);
for (it = lo->layers; it; it = it->next)
{
int fd = TEMP_FAILURE_RETRY (openat (it->fd, path, O_DIRECTORY));
if (fd < 0)
{
if (errno != ENOENT)
return NULL; // propagate error including EACCES
continue;
}
dir_layercnt++;
dp = fdopendir (fd);
if (dp == NULL)
{
close (fd);
continue;
}
for (;;)
{
struct ovl_node key;
struct ovl_node *child = NULL;
char node_path[PATH_MAX];
errno = 0;
dent = readdir (dp);
if (dent == NULL)
{
if (errno)
{
int saved_errno = errno;
closedir (dp);
errno = saved_errno;
return NULL;
}
break;
}
key.name = dent->d_name;
if ((strcmp (dent->d_name, ".") == 0) || strcmp (dent->d_name, "..") == 0)
continue;
debug_print ("load_dir: dent->d_name=%s\n", dent->d_name);
if (TEMP_FAILURE_RETRY (fstatat (fd, dent->d_name, &st, AT_SYMLINK_NOFOLLOW)) < 0)
{
// cannot stat a file - we ignore and log a message (when not ENOENT, which is ok),
// we don't really want to propagate the error because otherwise it might lead to
// weird error when opening the directory...
if (errno != ENOENT)
{
verb_print ("load_dir=warning call=fstatat path=%s d_name=%s errno=%d\n", path,
dent->d_name, errno);
}
continue;
}
pthread_mutex_lock (&ovl_node_global_lock);
child = hash_lookup (n->children, &key);
if (child)
{
if (!child->loaded)
{
child->layer = it; // adjust layer
child->multilayer = 0;
}
else
child->multilayer = 1;
child->loaded = 1;
pthread_mutex_unlock (&ovl_node_global_lock);
continue;
}
snprintf (node_path, sizeof(node_path), "%s/%s", n->path, dent->d_name);
pthread_mutex_unlock (&ovl_node_global_lock);
bool dirp = ((st.st_mode & S_IFMT) == S_IFDIR);
debug_print ("load_dir: make_ovl_node %s\n", dent->d_name);
child = make_ovl_node (node_path, it, dent->d_name, 0, dirp, n);
if (child == NULL)
{
closedir (dp);
errno = ENOMEM;
return NULL;
}
// insert without retaining child
if (insert_node (n, child, false, false) == NULL)
{
closedir (dp);
errno = ENOMEM;
return NULL;
}
// child->loaded set by insert_node w/ mutex locked
}
closedir (dp);
}
pthread_mutex_lock (&ovl_node_global_lock);
// refresh multilayer bit of directory
n->multilayer = (dir_layercnt > 1) ? 1 : 0;
for (nit = hash_get_first (n->children); nit; nit = next)
{
next = hash_get_next (n->children, nit);
if (!nit->loaded)
{
debug_print ("load_dir node_free orphan uid=%u path=%s name=%s\n",
FUSE_GETCURRENTUID(), nit->path, nit->name);
node_free(nit);
}
}
pthread_mutex_unlock (&ovl_node_global_lock);
return n;
}
static void
free_layers (struct ovl_layer *layers)
{
if (layers == NULL)
return;
free_layers (layers->next);
free (layers->path);
if (layers->fd >= 0)
close (layers->fd);
free (layers);
}
static struct ovl_layer *
read_dirs (char *path, bool low, struct ovl_layer *layers)
{
char *buf = NULL, *saveptr = NULL, *it;
struct ovl_layer *last;
if (path == NULL)
return NULL;
buf = strdup (path);
if (buf == NULL)
return NULL;
last = layers;
while (last && last->next)
last = last->next;
for (it = strtok_r (path, ":", &saveptr); it; it = strtok_r (NULL, ":", &saveptr))
{
char full_path[PATH_MAX];
struct ovl_layer *l = NULL;
if (realpath (it, full_path) < 0)
return NULL;
l = malloc (sizeof (*l));
if (l == NULL)
{
free_layers (layers);
return NULL;
}
l->path = strdup (full_path);
if (l->path == NULL)
{
free (l);
free_layers (layers);
return NULL;
}
l->fd = open (l->path, O_DIRECTORY);
if (l->fd < 0)
{
free (l->path);
free (l);
free_layers (layers);
return NULL;
}
l->low = low;
if (low)
{
l->next = NULL;
if (last == NULL)
last = layers = l;
else
{
last->next = l;
last = l;
}
}
else
{
l->next = layers;
layers = l;
}
}
free (buf);
return layers;
}
static struct ovl_node *
do_lookup_file (struct ovl_data *lo, fuse_ino_t parent, const char *name)
{
struct ovl_node key, *rm;
struct ovl_node *node, *pnode;
struct stat st;
int ret;
char path[PATH_MAX];
pthread_mutex_lock (&ovl_node_global_lock);