-
Notifications
You must be signed in to change notification settings - Fork 310
/
Copy pathdfuse_core.c
1564 lines (1278 loc) · 40 KB
/
dfuse_core.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
/**
* (C) Copyright 2016-2023 Intel Corporation.
*
* SPDX-License-Identifier: BSD-2-Clause-Patent
*/
#include <pthread.h>
#include "dfuse_common.h"
#include "dfuse.h"
/* Async progress thread.
*
* A number of threads are created at launch, each thread having its own event queue with a
* semaphore to wakeup, posted for each entry added to the event queue and once for shutdown.
* When there are no entries on the eq then the thread will yield in the semaphore, when there
* are pending events it'll spin in eq_poll() for completion. All pending events should be
* completed before thread exit, should exit be called with pending events.
*/
static void *
dfuse_progress_thread(void *arg)
{
struct dfuse_eq *eqt = arg;
daos_event_t *dev[128];
int to_consume = 1;
while (1) {
int rc;
int i;
for (i = 0; i < to_consume; i++) {
cont:
errno = 0;
rc = sem_wait(&eqt->de_sem);
if (rc != 0) {
rc = errno;
if (rc == EINTR)
D_GOTO(cont, 0);
DFUSE_TRA_ERROR(eqt, "Error from sem_wait: %d", rc);
}
}
if (eqt->de_handle->di_shutdown) {
int pending;
pending = daos_eq_query(eqt->de_eq, DAOS_EQR_ALL, 0, NULL);
DFUSE_TRA_INFO(eqt, "There are %d events pending", pending);
if (pending == 0)
return NULL;
}
rc = daos_eq_poll(eqt->de_eq, 1, DAOS_EQ_WAIT, 128, &dev[0]);
if (rc >= 1) {
for (i = 0; i < rc; i++) {
struct dfuse_event *ev;
ev = container_of(dev[i], struct dfuse_event, de_ev);
ev->de_complete_cb(ev);
}
to_consume = rc;
} else if (rc < 0) {
DFUSE_TRA_WARNING(eqt, "Error from daos_eq_poll, " DF_RC, DP_RC(rc));
to_consume = 0;
} else {
to_consume = 0;
}
}
return NULL;
}
/* Parse a string to a time, used for reading container attributes info
* timeouts.
*/
static int
dfuse_parse_time(char *buff, size_t len, unsigned int *_out)
{
int matched;
unsigned int out = 0;
int count0 = 0;
int count1 = 0;
char c = '\0';
matched = sscanf(buff, "%u%n%c%n", &out, &count0, &c, &count1);
if (matched == 0)
return EINVAL;
if (matched == 1 && len != count0)
return EINVAL;
if (matched == 2 && len != count1)
return EINVAL;
if (matched == 2) {
if (c == 'd' || c == 'D')
out *= 60 * 60 * 24;
else if (c == 'h' || c == 'H')
out *= 60 * 60;
else if (c == 'm' || c == 'M')
out *= 60;
else if (c == 's' || c == 'S')
true;
else
return EINVAL;
}
*_out = out;
return 0;
}
/* Inode entry hash table operations */
/* Shrink a 64 bit value into 32 bits to avoid hash collisions */
static uint32_t
ih_key_hash(struct d_hash_table *htable, const void *key, unsigned int ksize)
{
const ino_t *_ino = key;
ino_t ino = *_ino;
uint32_t hash = ino ^ (ino >> 32);
return hash;
}
static bool
ih_key_cmp(struct d_hash_table *htable, d_list_t *rlink, const void *key, unsigned int ksize)
{
const struct dfuse_inode_entry *ie;
const ino_t *ino = key;
ie = container_of(rlink, struct dfuse_inode_entry, ie_htl);
return *ino == ie->ie_stat.st_ino;
}
static uint32_t
ih_rec_hash(struct d_hash_table *htable, d_list_t *rlink)
{
const struct dfuse_inode_entry *ie;
ie = container_of(rlink, struct dfuse_inode_entry, ie_htl);
return ih_key_hash(NULL, &ie->ie_stat.st_ino, sizeof(ie->ie_stat.st_ino));
}
static void
ih_addref(struct d_hash_table *htable, d_list_t *rlink)
{
struct dfuse_inode_entry *ie;
ie = container_of(rlink, struct dfuse_inode_entry, ie_htl);
atomic_fetch_add_relaxed(&ie->ie_ref, 1);
}
static bool
ih_decref(struct d_hash_table *htable, d_list_t *rlink)
{
struct dfuse_inode_entry *ie;
ie = container_of(rlink, struct dfuse_inode_entry, ie_htl);
return (atomic_fetch_sub_relaxed(&ie->ie_ref, 1) == 1);
}
static int
ih_ndecref(struct d_hash_table *htable, d_list_t *rlink, int count)
{
struct dfuse_inode_entry *ie;
uint32_t oldref = 0;
uint32_t newref = 0;
ie = container_of(rlink, struct dfuse_inode_entry, ie_htl);
do {
oldref = atomic_load_relaxed(&ie->ie_ref);
if (oldref < count)
break;
newref = oldref - count;
} while (!atomic_compare_exchange(&ie->ie_ref, oldref, newref));
if (oldref < count) {
DFUSE_TRA_ERROR(ie, "unable to decref %u from %u", count, oldref);
return -DER_INVAL;
}
if (newref == 0)
return 1;
return 0;
}
static void
ih_free(struct d_hash_table *htable, d_list_t *rlink)
{
struct dfuse_inode_entry *ie;
ie = container_of(rlink, struct dfuse_inode_entry, ie_htl);
DFUSE_TRA_DEBUG(ie, "parent %#lx", ie->ie_parent);
dfuse_ie_close(htable->ht_priv, ie);
}
static d_hash_table_ops_t ie_hops = {
.hop_key_cmp = ih_key_cmp,
.hop_key_hash = ih_key_hash,
.hop_rec_hash = ih_rec_hash,
.hop_rec_addref = ih_addref,
.hop_rec_decref = ih_decref,
.hop_rec_ndecref = ih_ndecref,
.hop_rec_free = ih_free,
};
static uint32_t
ph_key_hash(struct d_hash_table *htable, const void *key, unsigned int ksize)
{
return *((const uint32_t *)key);
}
static uint32_t
ph_rec_hash(struct d_hash_table *htable, d_list_t *link)
{
struct dfuse_pool *dfp;
dfp = container_of(link, struct dfuse_pool, dfp_entry);
return ph_key_hash(NULL, &dfp->dfp_pool, sizeof(dfp->dfp_pool));
}
static bool
ph_key_cmp(struct d_hash_table *htable, d_list_t *link, const void *key, unsigned int ksize)
{
struct dfuse_pool *dfp;
dfp = container_of(link, struct dfuse_pool, dfp_entry);
return uuid_compare(dfp->dfp_pool, key) == 0;
}
static void
ph_addref(struct d_hash_table *htable, d_list_t *link)
{
struct dfuse_pool *dfp;
uint32_t oldref;
dfp = container_of(link, struct dfuse_pool, dfp_entry);
oldref = atomic_fetch_add_relaxed(&dfp->dfp_ref, 1);
DFUSE_TRA_DEBUG(dfp, "addref to %u", oldref + 1);
}
static bool
ph_decref(struct d_hash_table *htable, d_list_t *link)
{
struct dfuse_pool *dfp;
uint32_t oldref;
dfp = container_of(link, struct dfuse_pool, dfp_entry);
oldref = atomic_fetch_sub_relaxed(&dfp->dfp_ref, 1);
DFUSE_TRA_DEBUG(dfp, "decref to %u", oldref - 1);
return oldref == 1;
}
static void
_ph_free(struct dfuse_info *dfuse_info, struct dfuse_pool *dfp)
{
int rc;
if (daos_handle_is_valid(dfp->dfp_poh)) {
rc = daos_pool_disconnect(dfp->dfp_poh, NULL);
/* Hook for fault injection testing, if the disconnect fails with out of memory
* then simply try it again.
*/
if (rc == -DER_NOMEM)
rc = daos_pool_disconnect(dfp->dfp_poh, NULL);
if (rc != -DER_SUCCESS)
DFUSE_TRA_ERROR(dfp, "daos_pool_disconnect() failed: " DF_RC, DP_RC(rc));
}
rc = d_hash_table_destroy_inplace(&dfp->dfp_cont_table, false);
if (rc != -DER_SUCCESS)
DFUSE_TRA_ERROR(dfp, "Failed to destroy pool hash table: " DF_RC, DP_RC(rc));
atomic_fetch_sub_relaxed(&dfuse_info->di_pool_count, 1);
D_FREE(dfp);
}
static void
ph_free(struct d_hash_table *htable, d_list_t *link)
{
_ph_free(htable->ht_priv, container_of(link, struct dfuse_pool, dfp_entry));
}
static d_hash_table_ops_t pool_hops = {
.hop_key_cmp = ph_key_cmp,
.hop_key_hash = ph_key_hash,
.hop_rec_hash = ph_rec_hash,
.hop_rec_addref = ph_addref,
.hop_rec_decref = ph_decref,
.hop_rec_free = ph_free,
};
static uint32_t
ch_key_hash(struct d_hash_table *htable, const void *key, unsigned int ksize)
{
return *((const uint32_t *)key);
}
static uint32_t
ch_rec_hash(struct d_hash_table *htable, d_list_t *link)
{
struct dfuse_cont *dfc;
dfc = container_of(link, struct dfuse_cont, dfs_entry);
return ch_key_hash(NULL, &dfc->dfs_cont, sizeof(dfc->dfs_cont));
}
static bool
ch_key_cmp(struct d_hash_table *htable, d_list_t *link, const void *key, unsigned int ksize)
{
struct dfuse_cont *dfc;
dfc = container_of(link, struct dfuse_cont, dfs_entry);
return uuid_compare(dfc->dfs_cont, key) == 0;
}
static void
ch_addref(struct d_hash_table *htable, d_list_t *link)
{
struct dfuse_cont *dfc;
uint32_t oldref;
dfc = container_of(link, struct dfuse_cont, dfs_entry);
oldref = atomic_fetch_add_relaxed(&dfc->dfs_ref, 1);
DFUSE_TRA_DEBUG(dfc, "addref to %u", oldref + 1);
}
static bool
ch_decref(struct d_hash_table *htable, d_list_t *link)
{
struct dfuse_cont *dfc;
uint32_t oldref;
dfc = container_of(link, struct dfuse_cont, dfs_entry);
oldref = atomic_fetch_sub_relaxed(&dfc->dfs_ref, 1);
DFUSE_TRA_DEBUG(dfc, "decref to %u", oldref - 1);
return oldref == 1;
}
static void
_ch_free(struct dfuse_info *dfuse_info, struct dfuse_cont *dfc)
{
if (daos_handle_is_valid(dfc->dfs_coh)) {
int rc;
rc = dfs_umount(dfc->dfs_ns);
if (rc != 0)
DFUSE_TRA_ERROR(dfc, "dfs_umount() failed: %d (%s)", rc, strerror(rc));
rc = daos_cont_close(dfc->dfs_coh, NULL);
if (rc == -DER_NOMEM)
rc = daos_cont_close(dfc->dfs_coh, NULL);
if (rc != 0)
DFUSE_TRA_ERROR(dfc, "daos_cont_close() failed, " DF_RC, DP_RC(rc));
}
atomic_fetch_sub_relaxed(&dfuse_info->di_container_count, 1);
d_hash_rec_decref(&dfuse_info->di_pool_table, &dfc->dfs_dfp->dfp_entry);
D_FREE(dfc);
}
static void
ch_free(struct d_hash_table *htable, d_list_t *link)
{
_ch_free(htable->ht_priv, container_of(link, struct dfuse_cont, dfs_entry));
}
d_hash_table_ops_t cont_hops = {
.hop_key_cmp = ch_key_cmp,
.hop_key_hash = ch_key_hash,
.hop_rec_hash = ch_rec_hash,
.hop_rec_addref = ch_addref,
.hop_rec_decref = ch_decref,
.hop_rec_free = ch_free,
};
/* Connect to a pool.
*
* Daos accepts labels and uuids via the same function so simply call that, connect to a pool
* and setup a descriptor, then enter into the hash table and verify that it's unique. If there
* is likely already a connection for this pool then use dfuse_pool_get_handle() instead
* which will do a hash-table lookup in advance.
*
* Return code is a system errno.
*/
int
dfuse_pool_connect(struct dfuse_info *fs_handle, const char *label, struct dfuse_pool **_dfp)
{
struct dfuse_pool *dfp;
d_list_t *rlink;
int rc;
int ret;
D_ALLOC_PTR(dfp);
if (dfp == NULL)
D_GOTO(err, rc = ENOMEM);
atomic_init(&dfp->dfp_ref, 1);
DFUSE_TRA_UP(dfp, fs_handle, "dfp");
/* Handle the case where no identifier is supplied, this is for when dfuse
* is started without any pool on the command line.
*/
if (label[0]) {
daos_pool_info_t p_info = {};
rc = daos_pool_connect(label, fs_handle->di_group, DAOS_PC_RO, &dfp->dfp_poh,
&p_info, NULL);
if (rc) {
if (rc == -DER_NO_PERM || rc == -DER_NONEXIST)
DFUSE_TRA_INFO(dfp, "daos_pool_connect() failed, " DF_RC,
DP_RC(rc));
else
DFUSE_TRA_ERROR(dfp, "daos_pool_connect() '%s' failed, " DF_RC,
label, DP_RC(rc));
D_GOTO(err_free, rc = daos_der2errno(rc));
}
uuid_copy(dfp->dfp_pool, p_info.pi_uuid);
}
rc = d_hash_table_create_inplace(D_HASH_FT_LRU | D_HASH_FT_EPHEMERAL, 3, fs_handle,
&cont_hops, &dfp->dfp_cont_table);
if (rc != -DER_SUCCESS) {
DFUSE_TRA_ERROR(dfp, "Failed to create hash table: " DF_RC, DP_RC(rc));
D_GOTO(err_disconnect, rc = daos_der2errno(rc));
}
atomic_fetch_add_relaxed(&fs_handle->di_pool_count, 1);
rlink = d_hash_rec_find_insert(&fs_handle->di_pool_table, &dfp->dfp_pool,
sizeof(dfp->dfp_pool), &dfp->dfp_entry);
if (rlink != &dfp->dfp_entry) {
DFUSE_TRA_DEBUG(dfp, "Found existing pool, reusing");
_ph_free(fs_handle, dfp);
dfp = container_of(rlink, struct dfuse_pool, dfp_entry);
}
DFUSE_TRA_DEBUG(dfp, "Returning dfp for " DF_UUID, DP_UUID(dfp->dfp_pool));
*_dfp = dfp;
return rc;
err_disconnect:
ret = daos_pool_disconnect(dfp->dfp_poh, NULL);
if (ret)
DFUSE_TRA_WARNING(dfp, "Failed to disconnect pool: "DF_RC, DP_RC(ret));
err_free:
D_FREE(dfp);
err:
return rc;
}
int
dfuse_pool_get_handle(struct dfuse_info *dfuse_info, uuid_t pool, struct dfuse_pool **_dfp)
{
d_list_t *rlink;
char uuid_str[37];
rlink = d_hash_rec_find(&dfuse_info->di_pool_table, pool, sizeof(*pool));
if (rlink) {
*_dfp = container_of(rlink, struct dfuse_pool, dfp_entry);
return 0;
}
uuid_unparse(pool, uuid_str);
return dfuse_pool_connect(dfuse_info, uuid_str, _dfp);
}
#define ATTR_COUNT 6
char const *const cont_attr_names[ATTR_COUNT] = {
"dfuse-attr-time", "dfuse-dentry-time", "dfuse-dentry-dir-time",
"dfuse-ndentry-time", "dfuse-data-cache", "dfuse-direct-io-disable"};
#define ATTR_TIME_INDEX 0
#define ATTR_DENTRY_INDEX 1
#define ATTR_DENTRY_DIR_INDEX 2
#define ATTR_NDENTRY_INDEX 3
#define ATTR_DATA_CACHE_INDEX 4
#define ATTR_DIRECT_IO_DISABLE_INDEX 5
/* Attribute values are of the form "120M", so the buffer does not need to be
* large.
*/
#define ATTR_VALUE_LEN 128
static bool
dfuse_char_enabled(char *addr, size_t len)
{
if (strncasecmp(addr, "on", len) == 0)
return true;
if (strncasecmp(addr, "true", len) == 0)
return true;
return false;
}
static bool
dfuse_char_disabled(char *addr, size_t len)
{
if (strncasecmp(addr, "off", len) == 0)
return true;
if (strncasecmp(addr, "false", len) == 0)
return true;
return false;
}
/* Setup caching attributes for a container.
*
* These are read from pool attributes, or can be overwritten on the command
* line, but only for the root dfc in that case, so to use caching with
* multiple containers it needs to be set via attributes.
*
* Returns a error code on error, or ENODATA if no attributes are
* set.
*/
static int
dfuse_cont_get_cache(struct dfuse_cont *dfc)
{
size_t sizes[ATTR_COUNT];
char *buff;
char *buff_addrs[ATTR_COUNT];
int rc;
int i;
unsigned int value;
bool have_dentry = false;
bool have_dentry_dir = false;
bool have_dio = false;
bool have_cache_off = false;
D_ALLOC(buff, ATTR_VALUE_LEN * ATTR_COUNT);
if (buff == NULL)
return ENOMEM;
for (i = 0; i < ATTR_COUNT; i++) {
sizes[i] = ATTR_VALUE_LEN - 1;
buff_addrs[i] = buff + i * ATTR_VALUE_LEN;
}
rc = daos_cont_get_attr(dfc->dfs_coh, ATTR_COUNT, cont_attr_names,
(void *const *)buff_addrs, sizes, NULL);
if (rc == -DER_NONEXIST) {
/* none of the cache related attrs are present */
D_GOTO(out, rc = ENODATA);
} else if (rc != -DER_SUCCESS) {
DFUSE_TRA_WARNING(dfc, "Failed to load values for all cache related attrs" DF_RC,
DP_RC(rc));
D_GOTO(out, rc = daos_der2errno(rc));
}
for (i = 0; i < ATTR_COUNT; i++) {
if (sizes[i] == 0) {
/* attr is not present */
continue;
}
/* Ensure the character after the fetched string is zero in case
* of non-null terminated strings. size always refers to the
* number of non-null characters in this case, regardless of if
* the attribute is null terminated or not.
*/
if (*(buff_addrs[i] + sizes[i] - 1) == '\0')
sizes[i]--;
else
*(buff_addrs[i] + sizes[i]) = '\0';
if (i == ATTR_DATA_CACHE_INDEX) {
if (dfuse_char_enabled(buff_addrs[i], sizes[i])) {
dfc->dfc_data_timeout = -1;
DFUSE_TRA_INFO(dfc, "setting '%s' is enabled", cont_attr_names[i]);
} else if (dfuse_char_disabled(buff_addrs[i], sizes[i])) {
have_cache_off = true;
dfc->dfc_data_timeout = 0;
DFUSE_TRA_INFO(dfc, "setting '%s' is disabled", cont_attr_names[i]);
} else if (dfuse_parse_time(buff_addrs[i], sizes[i], &value) == 0) {
DFUSE_TRA_INFO(dfc, "setting '%s' is %u seconds",
cont_attr_names[i], value);
dfc->dfc_data_timeout = value;
} else {
DFUSE_TRA_WARNING(dfc, "Failed to parse '%s' for '%s'",
buff_addrs[i], cont_attr_names[i]);
dfc->dfc_data_timeout = 0;
}
continue;
}
if (i == ATTR_DIRECT_IO_DISABLE_INDEX) {
if (dfuse_char_enabled(buff_addrs[i], sizes[i])) {
have_dio = true;
dfc->dfc_direct_io_disable = true;
DFUSE_TRA_INFO(dfc, "setting '%s' is enabled", cont_attr_names[i]);
} else if (dfuse_char_disabled(buff_addrs[i], sizes[i])) {
dfc->dfc_direct_io_disable = false;
DFUSE_TRA_INFO(dfc, "setting '%s' is disabled", cont_attr_names[i]);
} else {
DFUSE_TRA_WARNING(dfc, "Failed to parse '%s' for '%s'",
buff_addrs[i], cont_attr_names[i]);
dfc->dfc_direct_io_disable = false;
}
continue;
}
rc = dfuse_parse_time(buff_addrs[i], sizes[i], &value);
if (rc != 0) {
DFUSE_TRA_WARNING(dfc, "Failed to parse '%s' for '%s'", buff_addrs[i],
cont_attr_names[i]);
continue;
}
DFUSE_TRA_INFO(dfc, "setting '%s' is %u seconds", cont_attr_names[i], value);
if (i == ATTR_TIME_INDEX) {
dfc->dfc_attr_timeout = value;
} else if (i == ATTR_DENTRY_INDEX) {
have_dentry = true;
dfc->dfc_dentry_timeout = value;
} else if (i == ATTR_DENTRY_DIR_INDEX) {
have_dentry_dir = true;
dfc->dfc_dentry_dir_timeout = value;
} else if (i == ATTR_NDENTRY_INDEX) {
dfc->dfc_ndentry_timeout = value;
}
}
/* Check if dfuse-direct-io-disable is set to on but dfuse-data-cache is set to off.
* This combination does not make sense, so warn in this case and set caching to on.
*/
if (have_dio) {
if (have_cache_off)
DFUSE_TRA_WARNING(dfc, "Caching enabled because of %s",
cont_attr_names[ATTR_DIRECT_IO_DISABLE_INDEX]);
dfc->dfc_data_timeout = -1;
}
if (have_dentry && !have_dentry_dir)
dfc->dfc_dentry_dir_timeout = dfc->dfc_dentry_timeout;
rc = 0;
out:
D_FREE(buff);
return rc;
}
/* Set default cache values for a container.
*
* These are used by default if the container does not set any attributes
* itself, and there are no command-line settings to overrule them.
*
* It is intended to improve performance and usability on interactive
* nodes without preventing use across nodes, as such data cache is enabled
* and metadata cache is on but with relatively short timeouts.
*
* One second is used for attributes, dentries and negative dentries, however
* dentries which represent directories and are therefore referenced much
* more often during path-walk activities are set to five seconds.
*/
void
dfuse_set_default_cont_cache_values(struct dfuse_cont *dfc)
{
dfc->dfc_attr_timeout = 1;
dfc->dfc_dentry_timeout = 1;
dfc->dfc_dentry_dir_timeout = 5;
dfc->dfc_ndentry_timeout = 1;
dfc->dfc_data_timeout = 60 * 10;
dfc->dfc_direct_io_disable = false;
}
/* Open a cont by label.
*
* Only used for command line labels, not for paths in dfuse.
*/
int
dfuse_cont_open_by_label(struct dfuse_info *dfuse_info, struct dfuse_pool *dfp, const char *label,
struct dfuse_cont **_dfc)
{
struct dfuse_cont *dfc;
daos_cont_info_t c_info = {};
int dfs_flags = O_RDWR;
int rc;
int ret;
D_ALLOC_PTR(dfc);
if (dfc == NULL)
D_GOTO(err_free, rc = ENOMEM);
DFUSE_TRA_UP(dfc, dfp, "dfc");
rc = daos_cont_open(dfp->dfp_poh, label, DAOS_COO_RW, &dfc->dfs_coh, &c_info, NULL);
if (rc == -DER_NO_PERM) {
dfs_flags = O_RDONLY;
rc = daos_cont_open(dfp->dfp_poh, label, DAOS_COO_RO, &dfc->dfs_coh, &c_info, NULL);
}
if (rc == -DER_NONEXIST) {
DFUSE_TRA_INFO(dfc, "daos_cont_open() failed: " DF_RC, DP_RC(rc));
D_GOTO(err_free, rc = daos_der2errno(rc));
} else if (rc != -DER_SUCCESS) {
DFUSE_TRA_ERROR(dfc, "daos_cont_open() failed: " DF_RC, DP_RC(rc));
D_GOTO(err_free, rc = daos_der2errno(rc));
}
uuid_copy(dfc->dfs_cont, c_info.ci_uuid);
rc = dfs_mount(dfp->dfp_poh, dfc->dfs_coh, dfs_flags, &dfc->dfs_ns);
if (rc) {
DFUSE_TRA_ERROR(dfc, "dfs_mount() failed: %d (%s)", rc, strerror(rc));
D_GOTO(err_close, rc);
}
if (dfuse_info->di_caching) {
rc = dfuse_cont_get_cache(dfc);
if (rc == ENODATA) {
/* If there is no container specific
* attributes then use defaults
*/
DFUSE_TRA_INFO(dfc, "Using default caching values");
dfuse_set_default_cont_cache_values(dfc);
} else if (rc != 0) {
D_GOTO(err_close, rc);
}
} else {
DFUSE_TRA_INFO(dfc, "Caching disabled");
}
rc = dfuse_cont_open(dfuse_info, dfp, &c_info.ci_uuid, &dfc);
if (rc) {
D_FREE(dfc);
return rc;
}
*_dfc = dfc;
return 0;
err_close:
ret = daos_cont_close(dfc->dfs_coh, NULL);
if (ret)
DFUSE_TRA_WARNING(dfc, "daos_cont_close() failed: " DF_RC, DP_RC(ret));
err_free:
D_FREE(dfc);
return rc;
}
/*
* Return a container connection by uuid.
*
* Re-use an existing connection if possible, otherwise open new connection
* and setup dfs.
*
* In the case of a container which has been created by mkdir _dfs will be a
* valid pointer, with dfs_ns and dfs_coh set already. Failure in this case
* will result in the memory being freed.
*
* If successful will pass out a dfs pointer, with one reference held.
*
* Return code is a system errno.
*/
int
dfuse_cont_open(struct dfuse_info *dfuse_info, struct dfuse_pool *dfp, uuid_t *cont,
struct dfuse_cont **_dfc)
{
struct dfuse_cont *dfc = NULL;
d_list_t *rlink;
int rc = -DER_SUCCESS;
if (*_dfc) {
dfc = *_dfc;
} else {
/* Check if there is already a open container connection, and
* just use it if there is. The rec_find() will take the
* additional reference for us.
*/
rlink = d_hash_rec_find(&dfp->dfp_cont_table, cont, sizeof(*cont));
if (rlink) {
*_dfc = container_of(rlink, struct dfuse_cont, dfs_entry);
return 0;
}
D_ALLOC_PTR(dfc);
if (!dfc)
D_GOTO(err, rc = ENOMEM);
DFUSE_TRA_UP(dfc, dfp, "dfc");
}
/* No existing container found, so setup dfs and connect to one */
atomic_init(&dfc->dfs_ref, 1);
DFUSE_TRA_DEBUG(dfp, "New cont "DF_UUIDF" in pool "DF_UUIDF,
DP_UUID(cont), DP_UUID(dfp->dfp_pool));
dfc->dfs_dfp = dfp;
/* Allow for uuid to be NULL, in which case this represents a pool */
if (uuid_is_null(*cont)) {
if (uuid_is_null(dfp->dfp_pool))
dfc->dfs_ops = &dfuse_pool_ops;
else
dfc->dfs_ops = &dfuse_cont_ops;
/* Turn on some caching of metadata, otherwise container
* operations will be very frequent
*/
dfc->dfc_attr_timeout = 60;
dfc->dfc_dentry_dir_timeout = 60;
dfc->dfc_ndentry_timeout = 60;
} else if (*_dfc == NULL) {
char str[37];
int dfs_flags = O_RDWR;
dfc->dfs_ops = &dfuse_dfs_ops;
uuid_copy(dfc->dfs_cont, *cont);
uuid_unparse(dfc->dfs_cont, str);
rc = daos_cont_open(dfp->dfp_poh, str, DAOS_COO_RW, &dfc->dfs_coh, NULL, NULL);
if (rc == -DER_NO_PERM) {
dfs_flags = O_RDONLY;
rc = daos_cont_open(dfp->dfp_poh, str, DAOS_COO_RO, &dfc->dfs_coh, NULL,
NULL);
}
if (rc == -DER_NONEXIST) {
DFUSE_TRA_INFO(dfc, "daos_cont_open() failed: " DF_RC, DP_RC(rc));
D_GOTO(err_free, rc = daos_der2errno(rc));
} else if (rc != -DER_SUCCESS) {
DFUSE_TRA_ERROR(dfc, "daos_cont_open() failed: " DF_RC, DP_RC(rc));
D_GOTO(err_free, rc = daos_der2errno(rc));
}
rc = dfs_mount(dfp->dfp_poh, dfc->dfs_coh, dfs_flags, &dfc->dfs_ns);
if (rc) {
DFUSE_TRA_ERROR(dfc, "dfs_mount() failed: %d (%s)", rc, strerror(rc));
D_GOTO(err_close, rc);
}
if (dfuse_info->di_caching) {
rc = dfuse_cont_get_cache(dfc);
if (rc == ENODATA) {
/* If there are no container attributes then use defaults */
DFUSE_TRA_INFO(dfc, "Using default caching values");
dfuse_set_default_cont_cache_values(dfc);
rc = 0;
} else if (rc != 0) {
D_GOTO(err_umount, rc);
}
} else {
DFUSE_TRA_INFO(dfc, "Caching disabled");
}
} else {
/* This is either a container where a label is set on the
* command line, or one created through mkdir, in either case
* the container will be mounted, and caching etc already
* setup.
*/
dfc->dfs_ops = &dfuse_dfs_ops;
}
dfc->dfs_ino = atomic_fetch_add_relaxed(&dfuse_info->di_ino_next, 1);
/* Take a reference on the pool */
d_hash_rec_addref(&dfuse_info->di_pool_table, &dfp->dfp_entry);
atomic_fetch_add_relaxed(&dfuse_info->di_container_count, 1);
/* Finally insert into the hash table. This may return an existing
* container if there is a race to insert, so if that happens
* just use that one.
*/
rlink = d_hash_rec_find_insert(&dfp->dfp_cont_table,
&dfc->dfs_cont, sizeof(dfc->dfs_cont),
&dfc->dfs_entry);
if (rlink != &dfc->dfs_entry) {
DFUSE_TRA_DEBUG(dfp, "Found existing container, reusing");
_ch_free(dfuse_info, dfc);
dfc = container_of(rlink, struct dfuse_cont, dfs_entry);
}
DFUSE_TRA_DEBUG(dfc, "Returning dfs for " DF_UUID " ref %d", DP_UUID(dfc->dfs_cont),
dfc->dfs_ref);
*_dfc = dfc;
return rc;
err_umount:
dfs_umount(dfc->dfs_ns);
err_close:
daos_cont_close(dfc->dfs_coh, NULL);
err_free:
D_FREE(dfc);
err:
return rc;
}
/* Set a timer to mark cache entry as valid */
void
dfuse_mcache_set_time(struct dfuse_inode_entry *ie)
{
struct timespec now;
clock_gettime(CLOCK_MONOTONIC_COARSE, &now);
ie->ie_mcache_last_update = now;
}
void
dfuse_mcache_evict(struct dfuse_inode_entry *ie)
{
ie->ie_mcache_last_update.tv_sec = 0;
ie->ie_mcache_last_update.tv_nsec = 0;
}
bool
dfuse_mcache_get_valid(struct dfuse_inode_entry *ie, double max_age, double *timeout)
{
bool use = false;
struct timespec now;
struct timespec left;
double time_left;
D_ASSERT(max_age != -1);
D_ASSERT(max_age >= 0);
if (ie->ie_mcache_last_update.tv_sec == 0)
return false;
clock_gettime(CLOCK_MONOTONIC_COARSE, &now);
left.tv_sec = now.tv_sec - ie->ie_mcache_last_update.tv_sec;
left.tv_nsec = now.tv_nsec - ie->ie_mcache_last_update.tv_nsec;
if (left.tv_nsec < 0) {
left.tv_sec--;
left.tv_nsec += 1000000000;
}
time_left = max_age - (left.tv_sec + ((double)left.tv_nsec / 1000000000));
if (time_left > 0) {
use = true;
DFUSE_TRA_DEBUG(ie, "Allowing cache use, time remaining: %lf", time_left);
if (timeout)
*timeout = time_left;
}
return use;
}
/* Set a timer to mark cache entry as valid */
void
dfuse_dcache_set_time(struct dfuse_inode_entry *ie)
{
struct timespec now;
clock_gettime(CLOCK_MONOTONIC_COARSE, &now);
ie->ie_dcache_last_update = now;
}
void
dfuse_dcache_evict(struct dfuse_inode_entry *ie)
{
ie->ie_dcache_last_update.tv_sec = 0;
ie->ie_dcache_last_update.tv_nsec = 0;
}
bool
dfuse_dcache_get_valid(struct dfuse_inode_entry *ie, double max_age)
{
bool use = false;
struct timespec now;
struct timespec left;
double time_left;
if (max_age == -1)
return true;
if (ie->ie_dcache_last_update.tv_sec == 0)
return false;
clock_gettime(CLOCK_MONOTONIC_COARSE, &now);
left.tv_sec = now.tv_sec - ie->ie_dcache_last_update.tv_sec;
left.tv_nsec = now.tv_nsec - ie->ie_dcache_last_update.tv_nsec;
if (left.tv_nsec < 0) {
left.tv_sec--;
left.tv_nsec += 1000000000;
}
time_left = max_age - (left.tv_sec + ((double)left.tv_nsec / 1000000000));
if (time_left > 0) {
use = true;