-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcachefs.c
1749 lines (1418 loc) · 48.9 KB
/
cachefs.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
/*
* Copyright 2023 Regents of Nanjing University of Aeronautics and Astronautics and
* Hohai University, Miao Cai <[email protected]> and Junru Shen <[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 <https://www.gnu.org/licenses/>.
*/
/*
* CacheFS is a coherent, scalable compute-node-side cache layer
* for ETHANE. For every FS client, it provides a partial FS view
* of the global namespace. It caches the latest / hottest FS data.
* The coherence between CacheFSes of different compute-nodes is
* guaranteed via the shared log.
*
* CacheFS contains 2 parts:
* (1) namespace cache: It maps full path/remote dentry pointer
* to file's remote pointer and attributes. "Tombstone" items
* are used to indicate the negative lookup result (removed file).
* The namespace cache is organized as a hash table. (ns cache)
* (3) block mapping cache: It maps <file remote pointer, offset> to
* its corresponding data block's remote pointer and block size.
* The file mapping cache is organized as an AVL-based interval
* tree. (bm cache)
*/
#include <sys/stat.h>
#include <errno.h>
#include <math.h>
#include "sharedfs.h"
#include "cachefs.h"
#include "debug.h"
#include <unistd.h>
#include <asm/unistd_64.h>
#include "oplogger.h"
#include "tabhash.h"
#include "list.h"
#include "avl.h"
#define NSC_BUCKET_MAX_LEN 4
struct lru_bucket {
struct list_head head;
int count;
};
struct ns_cache {
struct lru_bucket *buckets;
TAB_hash hf;
int count;
size_t version;
int nr_ent_max, nr_ent_high_watermark;
int nr_buckets, bucket_len_high_watermark;
};
struct ns_entry {
struct list_head node;
struct ethane_dentry dentry;
size_t version;
bool is_create;
char full_path[];
};
struct bm_cache {
struct avl_tree tree;
struct bm_entry *clock_hand;
size_t version;
int nr_ent_max, nr_ent_high_watermark;
};
struct bm_entry {
struct avl_node node;
dmptr_t remote_dentry;
size_t off, size;
dmptr_t blk_remote_addr;
size_t version;
/* We use CLOCK psuedo-LRU cache eviction algorithm for BMC. */
bool ref;
};
struct cachefs {
sharedfs_t *rfs;
struct ns_cache nsc;
struct bm_cache bmc;
/*
* Entries BEFORE this version are considered as "stale"
* and can be evicted.
*/
size_t version;
};
void cachefs_set_version(cachefs_t *cfs, size_t version) {
cfs->version = version;
cfs->nsc.version = cfs->bmc.version = version;
if (cfs->version != version) {
pr_debug("cachefs set version to %lu", version);
}
}
/*
* Cache maintenance functions
* (Data structure level)
*/
/* Namespace Cache */
static inline bool nsc_entry_evictable(struct ns_cache *cache, struct ns_entry *entry) {
return entry->version < cache->version;
}
static inline int nsc_remove(struct ns_cache *cache, struct ns_entry *entry);
static inline int nsc_entry_evict(struct ns_cache *cache, struct lru_bucket *bucket, int nr) {
struct ns_entry *entry, *tmp;
int nr_evicted = 0;
list_for_each_entry_safe_reverse(entry, tmp, &bucket->head, node) {
if (!nsc_entry_evictable(cache, entry)) {
continue;
}
nsc_remove(cache, entry);
free(entry);
if (++nr_evicted == nr) {
goto out;
}
}
out:
return nr_evicted;
}
static inline size_t full_path_hash(struct ns_cache *cache, const char *full_path, size_t len) {
return TAB_finalize(&cache->hf, TAB_process(&cache->hf, (const uint8_t *) full_path, len, 0));
}
static inline void nsc_lru_update(struct lru_bucket *bucket, struct ns_entry *entry) {
/* move to LRU list head */
list_move(&entry->node, &bucket->head);
}
static inline void nsc_lru_add(struct ns_cache *cache, struct lru_bucket *bucket, struct ns_entry *entry) {
int nr_evict;
if (bucket->count >= cache->bucket_len_high_watermark) {
/* evict entries from LRU list */
nr_evict = bucket->count + 1 - cache->bucket_len_high_watermark;
nr_evict = nsc_entry_evict(cache, bucket, nr_evict);
bucket->count -= nr_evict;
cache->count -= nr_evict;
}
/* add to LRU list head */
INIT_LIST_HEAD(&entry->node);
list_add(&entry->node, &bucket->head);
bucket->count++;
cache->count++;
}
static inline void nsc_lru_del(struct ns_cache *cache, struct lru_bucket *bucket, struct ns_entry *entry) {
list_del(&entry->node);
bucket->count--;
cache->count--;
}
static inline struct ns_entry *nsc_lookup(struct ns_cache *cache, const char *full_path, size_t len) {
struct lru_bucket *bucket;
struct ns_entry *entry;
bucket = &cache->buckets[full_path_hash(cache, full_path, len) % cache->nr_buckets];
list_for_each_entry(entry, &bucket->head, node) {
if (strlen(entry->full_path) == len && strncmp(entry->full_path, full_path, len) == 0) {
nsc_lru_update(bucket, entry);
goto out;
}
}
entry = NULL;
out:
return entry;
}
static inline int nsc_insert(struct ns_cache *cache, struct ns_entry *entry) {
int bucketn = full_path_hash(cache, entry->full_path, strlen(entry->full_path)) % cache->nr_buckets;
struct lru_bucket *bucket;
bucket = &cache->buckets[bucketn];
nsc_lru_add(cache, bucket, entry);
pr_debug("bucket: %d; dentry: %lx(%s); path: %s; create: %d",
bucketn, entry->dentry.remote_addr, get_de_ty_str(entry->dentry.type),
entry->full_path, entry->is_create);
return 0;
}
static inline int nsc_remove(struct ns_cache *cache, struct ns_entry *entry) {
int bucketn = full_path_hash(cache, entry->full_path, strlen(entry->full_path)) % cache->nr_buckets;
struct lru_bucket *bucket;
bucket = &cache->buckets[bucketn];
nsc_lru_del(cache, bucket, entry);
pr_debug("bucket: %d; dentry: %lx(%s); path: %s; create: %d",
bucketn, entry->dentry.remote_addr, get_de_ty_str(entry->dentry.type),
entry->full_path, entry->is_create);
return 0;
}
/* Block Mapping Cache */
static inline bool bmc_entry_evictable(struct bm_cache *cache, struct bm_entry *entry) {
return entry->version < cache->version;
}
static inline struct bm_entry *bmc_lookup(struct bm_cache *cache, dmptr_t remote_dentry, size_t off) {
struct bm_entry query = { .remote_dentry = remote_dentry, .off = off }, *entry;
pr_debug("lookup extent: dentry=%lx off=%lu", remote_dentry, off);
/* find the last interval that <= off */
entry = avl_tree_nearest(&cache->tree, &query);
if (unlikely(entry && entry->off > off)) {
entry = avl_tree_prev(&cache->tree, entry);
}
/* ensure @off is within range */
if (likely(entry)) {
pr_debug("found entry: dentry=%lx off=%lu size=%lu, check validity",
entry->remote_dentry, entry->off, entry->size);
if (unlikely(off >= entry->off + entry->size)) {
entry = NULL;
} else {
/* set the reference flag for LRU */
entry->ref = true;
}
}
return entry;
}
static inline int bmc_remove(struct bm_cache *cache, struct bm_entry *entry);
static inline bool bmc_evict(struct bm_cache *bmc, int nr) {
struct bm_entry *victim, *start;
int i;
for (i = 0; i < nr; i++) {
if (unlikely(!bmc->clock_hand)) {
bmc->clock_hand = avl_tree_first(&bmc->tree);
}
start = bmc->clock_hand;
while (bmc->clock_hand->ref || !bmc_entry_evictable(bmc, bmc->clock_hand)) {
bmc->clock_hand->ref = false;
bmc->clock_hand = avl_tree_next(&bmc->tree, bmc->clock_hand);
if (unlikely(!bmc->clock_hand)) {
bmc->clock_hand = avl_tree_first(&bmc->tree);
}
if (bmc->clock_hand == start) {
/* we have tried our best, but no entries can be evicted so far */
return false;
}
}
victim = bmc->clock_hand;
bmc->clock_hand = avl_tree_next(&bmc->tree, bmc->clock_hand);
bmc_remove(bmc, victim);
pr_debug("evicted entry: dentry=%lx off=%lu size=%lu", victim->remote_dentry, victim->off, victim->size);
}
return true;
}
/*
* adjust entry's prev and next entry to remove intersection part with entry
*/
static inline int bmc_insert_new(struct bm_cache *cache, struct bm_entry *entry) {
struct bm_entry *nearest, *prev = NULL, *next = NULL, *add;
int ret = 0;
pr_debug("insert new block: dentry=%lx off=%lu size=%lu", entry->remote_dentry, entry->off, entry->size);
/* do eviction if necessary */
if (cache->tree.count >= cache->nr_ent_high_watermark) {
bmc_evict(cache, (int) (cache->tree.count - cache->nr_ent_high_watermark + 1));
}
/* get pred and succ */
nearest = avl_tree_nearest(&cache->tree, entry);
if (nearest) {
if (unlikely(nearest->off > entry->off)) {
prev = avl_tree_prev(&cache->tree, nearest);
next = nearest;
} else {
prev = nearest;
next = avl_tree_next(&cache->tree, nearest);
}
}
if (prev && prev->remote_dentry != entry->remote_dentry) {
prev = NULL;
}
if (next && next->remote_dentry != entry->remote_dentry) {
next = NULL;
}
if (prev) {
pr_debug("has prev: dentry=%lx off=%lu size=%lu", prev->remote_dentry, prev->off, prev->size);
}
if (next) {
pr_debug("has next: dentry=%lx off=%lu size=%lu", next->remote_dentry, next->off, next->size);
}
/* remove prev's intersection part with entry */
if (prev && prev->off + prev->size > entry->off) {
/* prev completely covers entry */
if (prev->off + prev->size > entry->off + entry->size) {
add = calloc(1, sizeof(*add));
if (unlikely(!add)) {
ret = -ENOMEM;
goto out;
}
add->remote_dentry = prev->remote_dentry;
add->off = entry->off + entry->size;
add->size = prev->off + prev->size - add->off;
add->blk_remote_addr = prev->blk_remote_addr;
add->version = prev->version;
avl_tree_add(&cache->tree, add);
pr_debug("prev completely covers entry, add right part dentry=%lx off=%lu size=%lu",
add->remote_dentry, add->off, add->size);
}
prev->size = entry->off - prev->off;
pr_debug("prev -> dentry=%lx off=%lu size=%lu", prev->remote_dentry, prev->off, prev->size);
if (!prev->size) {
pr_debug("prev is completely covered by entry, remove prev");
avl_tree_remove(&cache->tree, prev);
free(prev);
}
}
/* remove next's intersection part with entry */
if (next && next->off < entry->off + entry->size) {
/* has intersection */
if (next->off + next->size > entry->off + entry->size) {
/* next is partially covered by entry */
next->off = entry->off + entry->size;
next->size -= entry->size;
pr_debug("next -> dentry=%lx off=%lu size=%lu", next->remote_dentry, next->off, next->size);
} else {
/* next is completely covered by entry */
avl_tree_remove(&cache->tree, next);
free(next);
pr_debug("next is completely covered by entry, remove next");
}
}
/* write interval */
entry->ref = true;
avl_tree_add(&cache->tree, entry);
out:
return ret;
}
/* adjust entry itself to remove intersection part with prev and next */
static inline int bmc_insert_old(struct bm_cache *cache, struct bm_entry *entry) {
struct bm_entry *nearest, *prev = NULL, *next = NULL, *add;
int ret = 0;
pr_debug("insert old block: dentry=%lx off=%lu size=%lu", entry->remote_dentry, entry->off, entry->size);
/* do eviction if necessary */
if (cache->tree.count >= cache->nr_ent_max) {
bmc_evict(cache, (int) (cache->tree.count - cache->nr_ent_max + 1));
}
/* get pred and succ */
nearest = avl_tree_nearest(&cache->tree, entry);
if (nearest) {
if (unlikely(nearest->off > entry->off)) {
prev = avl_tree_prev(&cache->tree, nearest);
next = nearest;
} else {
prev = nearest;
next = avl_tree_next(&cache->tree, nearest);
}
}
if (prev && prev->remote_dentry != entry->remote_dentry) {
prev = NULL;
}
if (next && next->remote_dentry != entry->remote_dentry) {
next = NULL;
}
if (prev) {
pr_debug("has prev: dentry=%lx off=%lu size=%lu", prev->remote_dentry, prev->off, prev->size);
}
if (next) {
pr_debug("has next: dentry=%lx off=%lu size=%lu", next->remote_dentry, next->off, next->size);
}
/* remove entry's intersection part with prev */
if (prev && prev->off + prev->size > entry->off) {
/* has intersection */
if (prev->off + prev->size >= entry->off + entry->size) {
/* prev completely covers entry */
pr_debug("prev completely covers entry, remove entry");
free(entry);
goto out;
}
/* prev partially covers entry */
entry->size -= prev->off + prev->size - entry->off;
entry->off = prev->off + prev->size;
ethane_assert(entry->size > 0);
}
/* remove entry's intersection part with next */
if (next && next->off < entry->off + entry->size) {
if (next->off + next->size < entry->off + entry->size) {
/* next is completely covered by entry */
add = calloc(1, sizeof(*add));
if (unlikely(!add)) {
ret = -ENOMEM;
goto out;
}
add->remote_dentry = entry->remote_dentry;
add->off = next->off + next->size;
add->size = entry->off + entry->size - add->off;
add->blk_remote_addr = next->blk_remote_addr;
add->version = entry->version;
avl_tree_add(&cache->tree, add);
pr_debug("next completely covered by entry, add right part dentry=%lx off=%lu size=%lu",
add->remote_dentry, add->off, add->size);
}
entry->size = next->off - entry->off;
ethane_assert(entry->size > 0);
}
avl_tree_add(&cache->tree, entry);
pr_debug("entry -> dentry=%lx off=%lu size=%lu", entry->remote_dentry, entry->off, entry->size);
out:
return ret;
}
static inline int bmc_remove(struct bm_cache *cache, struct bm_entry *entry) {
avl_tree_remove(&cache->tree, entry);
free(entry);
return 0;
}
static int fetch_path_prefixes_to_cache(cachefs_t *cfs, const char *path,
struct ns_entry **parent_ent, struct ns_entry **ent) {
struct ns_cache *nsc = &cfs->nsc;
struct ethane_dentry **dentries;
int len, depth, ret = 0, i = 0;
const char *component, *next;
dmptr_t parent = DMPTR_NULL;
bool need_remote = false;
struct ns_entry *entry;
size_t prefix_len;
depth = ethane_get_dir_depth(path);
dentries = calloc(depth, sizeof(struct ethane_dentry *));
if (unlikely(!dentries)) {
ret = -ENOMEM;
goto out;
}
pr_debug("fetch path prefixes to cache, path=%s", path);
ETHANE_ITER_COMPONENTS(path, component, next, len) {
prefix_len = component + len - path;
if ((entry = nsc_lookup(nsc, path, prefix_len))) {
dentries[i] = &entry->dentry;
pr_debug("component %d (%.*s) found in cache", i, (int) prefix_len, path);
} else {
entry = calloc(1, sizeof(*entry) + prefix_len + 1);
if (unlikely(!entry)) {
ret = -ENOMEM;
goto out_free;
}
dentries[i] = &entry->dentry;
entry->dentry.type = ETHANE_DENTRY_TOMBSTONE;
entry->version = 0;
strncpy(entry->full_path, path, prefix_len);
entry->full_path[prefix_len] = '\0';
nsc_insert(nsc, entry);
need_remote = true;
pr_debug("component %d (%.*s) not present in cache, need_remote", i, (int) prefix_len, path);
}
i++;
}
if (need_remote) {
ret = sharedfs_ns_lookup_dentries(cfs->rfs, path, dentries);
if (unlikely(ret < 0)) {
goto out_free;
}
}
i = 0;
ETHANE_ITER_COMPONENTS(path, component, next, len) {
prefix_len = component + len - path;
entry = container_of(dentries[i], struct ns_entry, dentry);
if (dentries[i]->remote_addr == DMPTR_NULL) {
/* Non-existent file */
entry->dentry.type = ETHANE_DENTRY_TOMBSTONE;
pr_debug("component %d (%.*s) is a tombstone", i, (int) prefix_len, path);
}
ethane_assert(!dentries[i]->parent || dentries[i]->parent == parent);
dentries[i]->parent = parent;
parent = dentries[i]->remote_addr;
i++;
}
ethane_assert(depth > 0);
if (ent) {
*ent = container_of(dentries[depth - 1], struct ns_entry, dentry);
}
if (parent_ent) {
*parent_ent = depth > 1 ? container_of(dentries[depth - 2], struct ns_entry, dentry) : NULL;
}
out_free:
free(dentries);
out:
return ret;
}
enum perm_action {
PERM_R,
PERM_W,
PERM_EX
};
/*
* We assume that UID/GID are synced across CNs
*/
static int check_permission(cachefs_ctx_t *ctx, struct ethane_perm *perm, enum perm_action action) {
if (perm->owner.uid == ctx->uid) {
/* Owner */
switch (action) {
case PERM_R:
return (perm->mode & S_IRUSR) ? 0 : -EACCES;
case PERM_W:
return (perm->mode & S_IWUSR) ? 0 : -EACCES;
case PERM_EX:
return (perm->mode & S_IXUSR) ? 0 : -EACCES;
}
} else if (perm->owner.gid == ctx->gid) {
/* Group */
switch (action) {
case PERM_R:
return (perm->mode & S_IRGRP) ? 0 : -EACCES;
case PERM_W:
return (perm->mode & S_IWGRP) ? 0 : -EACCES;
case PERM_EX:
return (perm->mode & S_IXGRP) ? 0 : -EACCES;
}
} else {
/* Others */
switch (action) {
case PERM_R:
return (perm->mode & S_IROTH) ? 0 : -EACCES;
case PERM_W:
return (perm->mode & S_IWOTH) ? 0 : -EACCES;
case PERM_EX:
return (perm->mode & S_IXOTH) ? 0 : -EACCES;
}
}
ethane_assert(0);
}
static int check_prefix_components(cachefs_t *cfs, cachefs_ctx_t *ctx, const char *path, enum perm_action last_action) {
size_t prefix_len, path_len = strlen(path);
struct ns_entry *entry = NULL;
const char *component, *next;
int len, ret;
/* prefix components (except last one) should exist and be with lookup permission */
ETHANE_ITER_COMPONENTS(path, component, next, len) {
prefix_len = component + len - path;
if (prefix_len == path_len) {
break;
}
if (unlikely(!(entry = nsc_lookup(&cfs->nsc, path, prefix_len)) || entry->dentry.type == ETHANE_DENTRY_TOMBSTONE)) {
ret = -ENOENT;
goto out;
}
if (unlikely(entry->dentry.type != ETHANE_DENTRY_DIR)) {
ret = -ENOTDIR;
goto out;
}
ret = check_permission(ctx, &entry->dentry.perm, PERM_EX);
if (unlikely(ret < 0)) {
goto out;
}
}
ethane_assert(entry);
ret = check_permission(ctx, &entry->dentry.perm, last_action);
out:
return ret;
}
static int do_mkdir(cachefs_t *cfs, const char *path, mode_t mode,
dmptr_t remote_dentry, dmptr_t parent_remote_addr, size_t version) {
struct ethane_dentry *dentry;
bool need_insert = false;
struct ns_entry *entry;
int ret = 0;
entry = nsc_lookup(&cfs->nsc, path, strlen(path));
if (!entry) {
entry = calloc(1, sizeof(*entry) + strlen(path) + 1);
if (unlikely(!entry)) {
ret = -ENOMEM;
goto out;
}
need_insert = true;
}
dentry = &entry->dentry;
dentry->type = ETHANE_DENTRY_DIR;
dentry->remote_addr = remote_dentry;
dentry->parent = parent_remote_addr;
dentry->file_size = 0;
dentry->nr_children = 0;
dentry->perm.mode = mode;
/* FIXME: */
dentry->perm.owner.uid = 0;
dentry->perm.owner.gid = 0;
entry->version = version;
entry->is_create = true;
strcpy(entry->full_path, path);
if (need_insert) {
nsc_insert(&cfs->nsc, entry);
}
pr_debug("do_mkdir: %lx (parent: %lx)", remote_dentry, parent_remote_addr);
out:
return ret;
}
int cachefs_prefetch_metadata(cachefs_t *cfs, const char *path) {
return fetch_path_prefixes_to_cache(cfs, path, NULL, NULL);
}
static struct ns_entry *check_mkdir_and_get_parent(cachefs_t *cfs, cachefs_ctx_t *ctx, const char *path) {
size_t path_len = strlen(path);
struct bench_timer timer;
struct ns_entry *parent;
struct ns_entry *entry;
int ret;
bench_timer_start(&timer);
ret = fetch_path_prefixes_to_cache(cfs, path, &parent, NULL);
if (unlikely(IS_ERR(ret))) {
parent = ERR_PTR(ret);
goto out;
}
ret = check_prefix_components(cfs, ctx, path, PERM_W);
if (unlikely(IS_ERR(ret))) {
parent = ERR_PTR(ret);
goto out;
}
/* the last component must be non-existent */
entry = nsc_lookup(&cfs->nsc, path, path_len);
if (unlikely(entry && entry->dentry.type != ETHANE_DENTRY_TOMBSTONE)) {
parent = ERR_PTR(-EEXIST);
goto out;
}
ethane_assert(parent);
out:
return parent;
}
int cachefs_mkdir(cachefs_t *cfs, cachefs_ctx_t *ctx, const char *path, uint64_t *res,
mode_t mode, dmptr_t remote_dentry, size_t version) {
dmptr_t parent_remote_addr;
struct ns_entry *parent;
int ret;
if (*res == OP_RESULT_UNDETERMINED) {
parent = check_mkdir_and_get_parent(cfs, ctx, path);
if (unlikely(IS_ERR(parent))) {
*res = OP_RESULT_CANCELED;
ret = PTR_ERR(parent);
goto out;
}
*res = parent_remote_addr = parent->dentry.remote_addr;
} else {
ethane_assert(*res != OP_RESULT_CANCELED);
parent_remote_addr = *res;
}
ret = do_mkdir(cfs, path, mode, remote_dentry, parent_remote_addr, version);
out:
return ret;
}
static struct ns_entry *check_rmdir_and_get_ent(cachefs_t *cfs, cachefs_ctx_t *ctx, const char *path) {
size_t path_len = strlen(path);
struct ns_entry *entry;
int ret;
ret = fetch_path_prefixes_to_cache(cfs, path, NULL, NULL);
if (unlikely(ret < 0)) {
entry = ERR_PTR(ret);
goto out;
}
ret = check_prefix_components(cfs, ctx, path, PERM_W);
if (unlikely(ret < 0)) {
entry = ERR_PTR(ret);
goto out;
}
/* the last component must be existent */
entry = nsc_lookup(&cfs->nsc, path, path_len);
if (unlikely(!entry || entry->dentry.type == ETHANE_DENTRY_TOMBSTONE)) {
entry = ERR_PTR(-ENOENT);
goto out;
}
/* the last component must be directory */
if (unlikely(entry->dentry.type != ETHANE_DENTRY_DIR)) {
entry = ERR_PTR(-ENOTDIR);
goto out;
}
/* ... and it must be empty */
if (unlikely(entry->dentry.nr_children > 0)) {
entry = ERR_PTR(-ENOTEMPTY);
goto out;
}
out:
return entry;
}
static int do_rmdir(cachefs_t *cfs, const char *path, size_t version, dmptr_t dentry_remote_addr) {
bool need_insert = false;
struct ns_entry *entry;
int ret = 0;
entry = nsc_lookup(&cfs->nsc, path, strlen(path));
if (!entry) {
entry = calloc(1, sizeof(*entry) + strlen(path) + 1);
if (unlikely(!entry)) {
ret = -ENOMEM;
goto out;
}
need_insert = true;
}
entry->dentry.remote_addr = dentry_remote_addr;
entry->dentry.type = ETHANE_DENTRY_TOMBSTONE;
entry->is_create = false;
entry->version = version;
strcpy(entry->full_path, path);
if (need_insert) {
nsc_insert(&cfs->nsc, entry);
}
pr_debug("do_rmdir: %s %lx", path, dentry_remote_addr);
out:
return ret;
}
int cachefs_rmdir(cachefs_t *cfs, cachefs_ctx_t *ctx, const char *path, uint64_t *res, size_t version) {
dmptr_t dentry_remote_addr;
struct ns_entry *entry;
int ret;
if (*res == OP_RESULT_UNDETERMINED) {
entry = check_rmdir_and_get_ent(cfs, ctx, path);
if (unlikely(IS_ERR(entry))) {
*res = OP_RESULT_CANCELED;
ret = PTR_ERR(entry);
goto out;
}
*res = dentry_remote_addr = entry->dentry.remote_addr;
} else {
dentry_remote_addr = *res;
}
ethane_assert(*res != OP_RESULT_CANCELED);
ret = do_rmdir(cfs, path, version, dentry_remote_addr);
out:
return ret;
}
static struct ns_entry *check_unlink_and_get_ent(cachefs_t *cfs, cachefs_ctx_t *ctx, const char *path) {
size_t path_len = strlen(path);
struct ns_entry *entry;
int ret;
ret = fetch_path_prefixes_to_cache(cfs, path, NULL, NULL);
if (unlikely(ret < 0)) {
entry = ERR_PTR(ret);
goto out;
}
ret = check_prefix_components(cfs, ctx, path, PERM_W);
if (unlikely(ret < 0)) {
entry = ERR_PTR(ret);
goto out;
}
/* the last component must be existent */
entry = nsc_lookup(&cfs->nsc, path, path_len);
if (unlikely(!entry || entry->dentry.type == ETHANE_DENTRY_TOMBSTONE)) {
entry = ERR_PTR(-ENOENT);
goto out;
}
/* the last component must be file */
if (unlikely(entry->dentry.type != ETHANE_DENTRY_FILE)) {
entry = ERR_PTR(-EISDIR);
goto out;
}
out:
return entry;
}
static int do_unlink(cachefs_t *cfs, const char *path, size_t version, dmptr_t dentry_remote_addr) {
bool need_insert = false;
struct ns_entry *entry;
int ret = 0;
entry = nsc_lookup(&cfs->nsc, path, strlen(path));
if (!entry) {
entry = calloc(1, sizeof(*entry) + strlen(path) + 1);
if (unlikely(!entry)) {
ret = -ENOMEM;
goto out;
}
need_insert = true;
}
entry->dentry.remote_addr = dentry_remote_addr;
entry->dentry.type = ETHANE_DENTRY_TOMBSTONE;
entry->is_create = false;
entry->version = version;
strcpy(entry->full_path, path);
if (need_insert) {
nsc_insert(&cfs->nsc, entry);
}
pr_debug("do_unlink: %s %lx", path, dentry_remote_addr);
out:
return ret;
}
int cachefs_unlink(cachefs_t *cfs, cachefs_ctx_t *ctx, const char *path, uint64_t *res, size_t version) {
dmptr_t dentry_remote_addr;
struct ns_entry *entry;
int ret;
if (*res == OP_RESULT_UNDETERMINED) {
entry = check_unlink_and_get_ent(cfs, ctx, path);
if (unlikely(IS_ERR(entry))) {
ret = PTR_ERR(entry);
*res = OP_RESULT_CANCELED;
goto out;
}
*res = dentry_remote_addr = entry->dentry.remote_addr;
} else {
dentry_remote_addr = *res;
}
ethane_assert(*res != OP_RESULT_CANCELED);
ret = do_unlink(cfs, path, version, dentry_remote_addr);
out:
return ret;
}
static struct ns_entry *check_create_and_get_parent(cachefs_t *cfs, cachefs_ctx_t *ctx, const char *path) {
size_t path_len = strlen(path);
struct ns_entry *parent;
struct ns_entry *entry;
int ret;
ret = fetch_path_prefixes_to_cache(cfs, path, &parent, NULL);
if (unlikely(ret < 0)) {
parent = ERR_PTR(ret);
goto out;
}
ret = check_prefix_components(cfs, ctx, path, PERM_W);
if (unlikely(ret < 0)) {
parent = ERR_PTR(ret);
goto out;
}
/* the last component must be non-existent */
entry = nsc_lookup(&cfs->nsc, path, path_len);
if (unlikely(entry && entry->dentry.type != ETHANE_DENTRY_TOMBSTONE)) {
parent = ERR_PTR(-EEXIST);
goto out;
}
ethane_assert(parent);
out:
return parent;
}
static int do_create(cachefs_t *cfs, const char *path, mode_t mode, dmptr_t remote_dentry, dmptr_t parent_remote_addr,
struct ethane_open_file *file, size_t version) {
struct ethane_dentry *dentry;
bool need_insert = false;
struct ns_entry *entry;
int ret = 0;
entry = nsc_lookup(&cfs->nsc, path, strlen(path));
if (!entry) {
entry = calloc(1, sizeof(*entry) + strlen(path) + 1);
if (unlikely(!entry)) {
ret = -ENOMEM;
goto out;
}
need_insert = true;
}
dentry = &entry->dentry;
dentry->type = ETHANE_DENTRY_FILE;
dentry->remote_addr = remote_dentry;
dentry->parent = parent_remote_addr;