File: ad_daos_hhash.c

package info (click to toggle)
openmpi 5.0.7-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 202,312 kB
  • sloc: ansic: 612,441; makefile: 42,495; sh: 11,230; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,154; python: 1,856; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (307 lines) | stat: -rw-r--r-- 7,833 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

#include "ad_daos.h"
#include "gurt/hash.h"
#include <gurt/common.h>

static struct d_hash_table *coh_hash;
static struct d_hash_table *poh_hash;

enum {
    DAOS_POOL,
    DAOS_CONT,
};

static inline struct adio_daos_hdl *hdl_obj(d_list_t * rlink)
{
    return container_of(rlink, struct adio_daos_hdl, entry);
}

static bool
key_cmp(struct d_hash_table *htable, d_list_t * rlink, const void *key, unsigned int ksize)
{
    struct adio_daos_hdl *hdl = hdl_obj(rlink);

    return (uuid_compare(hdl->uuid, key) == 0);
}

static void rec_addref(struct d_hash_table *htable, d_list_t * rlink)
{
    hdl_obj(rlink)->ref++;
}

static bool rec_decref(struct d_hash_table *htable, d_list_t * rlink)
{
    struct adio_daos_hdl *hdl = hdl_obj(rlink);

    assert(hdl->ref > 0);
    hdl->ref--;
    return (hdl->ref == 0);
}

static void rec_free(struct d_hash_table *htable, d_list_t * rlink)
{
    struct adio_daos_hdl *hdl = hdl_obj(rlink);

    assert(d_hash_rec_unlinked(&hdl->entry));
    assert(hdl->ref == 0);

    if (hdl->type == DAOS_POOL)
        daos_pool_disconnect(hdl->open_hdl, NULL);
    else if (hdl->type == DAOS_CONT) {
        dfs_umount(hdl->dfs);
        daos_cont_close(hdl->open_hdl, NULL);
    } else
        assert(0);
    ADIOI_Free(hdl);
}

static d_hash_table_ops_t hdl_hash_ops = {
    .hop_key_cmp = key_cmp,
    .hop_rec_addref = rec_addref,
    .hop_rec_decref = rec_decref,
    .hop_rec_free = rec_free
};

int adio_daos_hash_init(void)
{
    int rc;

    rc = d_hash_table_create(0, 16, NULL, &hdl_hash_ops, &poh_hash);
    if (rc)
        return rc;

    return d_hash_table_create(0, 16, NULL, &hdl_hash_ops, &coh_hash);
}

void adio_daos_hash_finalize(void)
{
    d_hash_table_destroy(coh_hash, true /* force */);
    d_hash_table_destroy(poh_hash, true /* force */);
}

struct adio_daos_hdl *adio_daos_poh_lookup(const uuid_t uuid)
{
    d_list_t *rlink;

    rlink = d_hash_rec_find(poh_hash, uuid, sizeof(uuid_t));
    if (rlink == NULL)
        return NULL;

    return hdl_obj(rlink);
}

void adio_daos_poh_release(struct adio_daos_hdl *hdl)
{
    d_hash_rec_decref(poh_hash, &hdl->entry);
}

int adio_daos_poh_insert(uuid_t uuid, daos_handle_t poh, struct adio_daos_hdl **hdl)
{
    struct adio_daos_hdl *phdl;
    int rc;

    phdl = (struct adio_daos_hdl *) ADIOI_Calloc(1, sizeof(struct adio_daos_hdl));
    if (phdl == NULL)
        return -1;

    phdl->type = DAOS_POOL;
    uuid_copy(phdl->uuid, uuid);
    phdl->open_hdl.cookie = poh.cookie;

    rc = d_hash_rec_insert(poh_hash, phdl->uuid, sizeof(uuid_t), &phdl->entry, true);
    if (rc) {
        PRINT_MSG(stderr, "Failed to add phdl to hashtable (%d)\n", rc);
        goto free_hdl;
    }

    d_hash_rec_addref(poh_hash, &phdl->entry);
    *hdl = phdl;

    return 0;

  free_hdl:
    ADIOI_Free(phdl);
    return rc;
}

int adio_daos_poh_lookup_connect(uuid_t uuid, struct adio_daos_hdl **hdl)
{
    struct adio_daos_hdl *phdl;
    int rc;

    phdl = adio_daos_poh_lookup(uuid);
    if (phdl != NULL) {
        *hdl = phdl;
        return 0;
    }

    phdl = (struct adio_daos_hdl *) ADIOI_Calloc(1, sizeof(struct adio_daos_hdl));
    if (phdl == NULL)
        return -1;

    phdl->type = DAOS_POOL;
    uuid_copy(phdl->uuid, uuid);

    /** Get the SVCL and Server group from env variables. This is temp as those
     * won't be needed later */
    char *svcl_str = NULL;
    char *group = NULL;
    daos_pool_info_t pool_info;
    d_rank_list_t *svcl = NULL;

    svcl_str = getenv("DAOS_SVCL");
    if (svcl_str != NULL) {
        svcl = daos_rank_list_parse(svcl_str, ":");
        if (svcl == NULL) {
            PRINT_MSG(stderr, "Failed to parse SVC list env\n");
            rc = -1;
            goto free_hdl;
        }
    }
    group = getenv("DAOS_GROUP");

    rc = daos_pool_connect(uuid, group, svcl, DAOS_PC_RW, &phdl->open_hdl, &pool_info, NULL);
    d_rank_list_free(svcl);
    if (rc < 0) {
        PRINT_MSG(stderr, "Failed to connect to pool (%d)\n", rc);
        goto free_hdl;
    }

    rc = d_hash_rec_insert(poh_hash, phdl->uuid, sizeof(uuid_t), &phdl->entry, true);
    if (rc) {
        PRINT_MSG(stderr, "Failed to add phdl to hashtable (%d)\n", rc);
        goto err_pool;
    }

    d_hash_rec_addref(poh_hash, &phdl->entry);
    *hdl = phdl;

    return 0;

  err_pool:
    daos_pool_disconnect(phdl->open_hdl, NULL);
  free_hdl:
    ADIOI_Free(phdl);
    return rc;
}

struct adio_daos_hdl *adio_daos_coh_lookup(const uuid_t uuid)
{
    d_list_t *rlink;

    rlink = d_hash_rec_find(coh_hash, uuid, sizeof(uuid_t));
    if (rlink == NULL)
        return NULL;

    return hdl_obj(rlink);
}

void adio_daos_coh_release(struct adio_daos_hdl *hdl)
{
    d_hash_rec_decref(coh_hash, &hdl->entry);
}

int adio_daos_coh_insert(uuid_t uuid, daos_handle_t coh, struct adio_daos_hdl **hdl)
{
    struct adio_daos_hdl *co_hdl;
    int rc;

    co_hdl = (struct adio_daos_hdl *) ADIOI_Calloc(1, sizeof(struct adio_daos_hdl));
    if (co_hdl == NULL)
        return -1;

    co_hdl->type = DAOS_CONT;
    uuid_copy(co_hdl->uuid, uuid);
    co_hdl->open_hdl.cookie = coh.cookie;

    rc = d_hash_rec_insert(coh_hash, co_hdl->uuid, sizeof(uuid_t), &co_hdl->entry, true);
    if (rc) {
        PRINT_MSG(stderr, "Failed to add co_hdl to hashtable (%d)\n", rc);
        goto err_coh;
    }

    d_hash_rec_addref(coh_hash, &co_hdl->entry);
    *hdl = co_hdl;

    return 0;

  err_coh:
    ADIOI_Free(co_hdl);
    return rc;
}

int
adio_daos_coh_lookup_create(daos_handle_t poh, uuid_t uuid, int amode,
                            bool create, struct adio_daos_hdl **hdl)
{
    struct adio_daos_hdl *co_hdl;
    int rc;

    co_hdl = adio_daos_coh_lookup(uuid);
    if (co_hdl != NULL) {
        *hdl = co_hdl;
        return 0;
    }

    co_hdl = (struct adio_daos_hdl *) ADIOI_Calloc(1, sizeof(struct adio_daos_hdl));
    if (co_hdl == NULL)
        return -1;

    co_hdl->type = DAOS_CONT;
    uuid_copy(co_hdl->uuid, uuid);

    /* Try to open the DAOS container first (the parent directory) */
    rc = daos_cont_open(poh, uuid, DAOS_COO_RW, &co_hdl->open_hdl, NULL, NULL);
    /* If fails with NOEXIST we can create it then reopen if create mode */
    if (rc == -DER_NONEXIST && create) {
        rc = dfs_cont_create(poh, uuid, NULL, &co_hdl->open_hdl, &co_hdl->dfs);
        /** if someone got there first, re-open*/
        if (rc == EEXIST) {
            rc = daos_cont_open(poh, uuid, DAOS_COO_RW, &co_hdl->open_hdl, NULL, NULL);
            if (rc) {
                PRINT_MSG(stderr, "Failed to create DFS container (%d)\n", rc);
                goto free_coh;
            }
            rc = dfs_mount(poh, co_hdl->open_hdl, amode, &co_hdl->dfs);
            if (rc) {
                PRINT_MSG(stderr, "Failed to mount DFS namesapce (%d)\n", rc);
                goto err_cont;
            }
        } else if (rc) {
            PRINT_MSG(stderr, "Failed to create DFS container (%d)\n", rc);
            goto free_coh;
        }
    } else if (rc == 0) {
        /* Mount a DFS namespace on the container */
        rc = dfs_mount(poh, co_hdl->open_hdl, amode, &co_hdl->dfs);
        if (rc) {
            PRINT_MSG(stderr, "Failed to mount DFS namespace (%d)\n", rc);
            goto err_cont;
        }
    } else {
        goto free_coh;
    }

    rc = d_hash_rec_insert(coh_hash, co_hdl->uuid, sizeof(uuid_t), &co_hdl->entry, true);
    if (rc) {
        PRINT_MSG(stderr, "Failed to add co_hdl to hashtable (%d)\n", rc);
        goto err_dfs;
    }

    d_hash_rec_addref(coh_hash, &co_hdl->entry);
    *hdl = co_hdl;

    return 0;

  err_dfs:
    dfs_umount(co_hdl->dfs);
  err_cont:
    daos_cont_close(co_hdl->open_hdl, NULL);
  free_coh:
    ADIOI_Free(co_hdl);
    return rc;
}