Debian Codegraph @

src/mpi/romio/adio/ad_daos/ad_daos_open.c

raw
1 /*
2 * Copyright (C) by Argonne National Laboratory
3 * See COPYRIGHT in top-level directory
4 */
5
6 #include "ad_daos.h"
7 #include <libgen.h>
8 #include <uuid/uuid.h>
9 #include <gurt/common.h>
10
11 static int parse_filename(const char *path, char **_obj_name, char **_cont_name)
12 {
13 char *f1;
14 char *f2;
15 char *fname;
16 char *cont_name;
17 int rc = 0;
18
19 f1 = ADIOI_Strdup(path);
20 if (f1 == NULL)
21 return ENOMEM;
22
23 f2 = ADIOI_Strdup(path);
24 if (f2 == NULL) {
25 ADIOI_Free(f1);
26 return ENOMEM;
27 }
28
29 fname = basename(f1);
30 cont_name = dirname(f2);
31
32 if (cont_name[0] != '/') {
33 char *ptr;
34 char buf[PATH_MAX];
35
36 ptr = realpath(cont_name, buf);
37 if (ptr == NULL) {
38 rc = errno;
39 goto out;
40 }
41
42 cont_name = ADIOI_Strdup(ptr);
43 if (cont_name == NULL) {
44 rc = ENOMEM;
45 goto out;
46 }
47 *_cont_name = cont_name;
48 } else {
49 *_cont_name = ADIOI_Strdup(cont_name);
50 if (*_cont_name == NULL) {
51 rc = ENOMEM;
52 goto out;
53 }
54 }
55
56 *_obj_name = ADIOI_Strdup(fname);
57 if (*_obj_name == NULL) {
58 rc = ENOMEM;
59 goto out;
60 }
61
62 out:
63 ADIOI_Free(f1);
64 ADIOI_Free(f2);
65 return rc;
66 }
67
68
69 static int cache_handles(struct ADIO_DAOS_cont *cont)
70 {
71 int rc;
72
73 cont->c = adio_daos_coh_lookup(&cont->attr);
74 if (cont->c == NULL) {
75 /** insert handle into container hashtable */
76 rc = adio_daos_coh_insert(&cont->attr, cont->coh, NULL, &cont->c);
77 } else {
78 /** g2l handle not needed, already cached */
79 rc = daos_cont_close(cont->coh, NULL);
80 cont->coh = cont->c->open_hdl;
81 }
82 if (rc)
83 return rc;
84
85 cont->p = adio_daos_poh_lookup(&cont->attr);
86 if (cont->p == NULL) {
87 /** insert handle into pool hashtable */
88 rc = adio_daos_poh_insert(&cont->attr, cont->poh, &cont->p);
89 } else {
90 /** g2l handle not needed, already cached */
91 rc = daos_pool_disconnect(cont->poh, NULL);
92 cont->poh = cont->p->open_hdl;
93 }
94
95 return rc;
96 }
97
98 static int share_cont_info(struct ADIO_DAOS_cont *cont, int rank, MPI_Comm comm)
99 {
100 d_iov_t pool_hdl = { NULL, 0, 0 };
101 d_iov_t cont_hdl = { NULL, 0, 0 };
102 d_iov_t dfs_hdl = { NULL, 0, 0 };
103 d_iov_t file_hdl = { NULL, 0, 0 };
104 char *buf = NULL;
105 uint64_t total_size;
106 daos_size_t buf_size;
107 #if CHECK_DAOS_API_VERSION(1, 4)
108 daos_size_t pool_len = 0, cont_len = 0;
109 #else
110 daos_size_t pool_len = 37, cont_len = 37;
111 #endif
112 int rc = 0;
113
114 if (rank == 0) {
115 rc = daos_pool_local2global(cont->poh, &pool_hdl);
116 if (rc)
117 return rc;
118 rc = daos_cont_local2global(cont->coh, &cont_hdl);
119 if (rc)
120 return rc;
121 rc = dfs_local2global(cont->dfs, &dfs_hdl);
122 if (rc)
123 return rc;
124 rc = dfs_obj_local2global(cont->dfs, cont->obj, &file_hdl);
125 if (rc)
126 return rc;
127
128 buf_size = 0;
129 #if CHECK_DAOS_API_VERSION(1, 4)
130 pool_len = strlen(cont->attr.da_pool) + 1;
131 #endif
132 buf_size += pool_len;
133
134 #if CHECK_DAOS_API_VERSION(1, 4)
135 cont_len = strlen(cont->attr.da_cont) + 1;
136 #endif
137 buf_size += cont_len;
138
139 total_size = buf_size + pool_hdl.iov_buf_len + cont_hdl.iov_buf_len +
140 dfs_hdl.iov_buf_len + file_hdl.iov_buf_len + sizeof(daos_size_t) * 4;
141 }
142
143 /** broadcast size to all peers */
144 rc = MPI_Bcast(&total_size, 1, MPI_UINT64_T, 0, comm);
145 if (rc != MPI_SUCCESS)
146 return -1;
147
148 /** allocate buffers */
149 buf = ADIOI_Malloc(total_size);
150 if (buf == NULL)
151 return -1;
152
153 if (rank == 0) {
154 char *ptr = buf;
155
156 #if CHECK_DAOS_API_VERSION(1, 4)
157 strcpy(ptr, cont->attr.da_pool);
158 #else
159 uuid_unparse(cont->attr.da_puuid, ptr);
160 #endif
161 ptr += pool_len;
162
163 #if CHECK_DAOS_API_VERSION(1, 4)
164 strcpy(ptr, cont->attr.da_cont);
165 #else
166 uuid_unparse(cont->attr.da_cuuid, ptr);
167 #endif
168 ptr += cont_len;
169
170 *((daos_size_t *) ptr) = pool_hdl.iov_buf_len;
171 ptr += sizeof(daos_size_t);
172 pool_hdl.iov_buf = ptr;
173 pool_hdl.iov_len = pool_hdl.iov_buf_len;
174 rc = daos_pool_local2global(cont->poh, &pool_hdl);
175 if (rc)
176 goto out;
177 ptr += pool_hdl.iov_buf_len;
178
179 *((daos_size_t *) ptr) = cont_hdl.iov_buf_len;
180 ptr += sizeof(daos_size_t);
181 cont_hdl.iov_buf = ptr;
182 cont_hdl.iov_len = cont_hdl.iov_buf_len;
183 rc = daos_cont_local2global(cont->coh, &cont_hdl);
184 if (rc)
185 goto out;
186 ptr += cont_hdl.iov_buf_len;
187
188 *((daos_size_t *) ptr) = dfs_hdl.iov_buf_len;
189 ptr += sizeof(daos_size_t);
190 dfs_hdl.iov_buf = ptr;
191 dfs_hdl.iov_len = dfs_hdl.iov_buf_len;
192 rc = dfs_local2global(cont->dfs, &dfs_hdl);
193 if (rc)
194 goto out;
195 ptr += dfs_hdl.iov_buf_len;
196
197 *((daos_size_t *) ptr) = file_hdl.iov_buf_len;
198 ptr += sizeof(daos_size_t);
199 file_hdl.iov_buf = ptr;
200 file_hdl.iov_len = file_hdl.iov_buf_len;
201 rc = dfs_obj_local2global(cont->dfs, cont->obj, &file_hdl);
202 if (rc)
203 goto out;
204 }
205
206 rc = MPI_Bcast_c(buf, total_size, MPI_BYTE, 0, comm);
207 if (rc != MPI_SUCCESS)
208 goto out;
209
210 if (rank != 0) {
211 char *ptr = buf;
212
213 #if CHECK_DAOS_API_VERSION(1, 4)
214 strncpy(cont->attr.da_pool, ptr, DAOS_PROP_LABEL_MAX_LEN + 1);
215 cont->attr.da_pool[DAOS_PROP_LABEL_MAX_LEN] = 0;
216 pool_len = strlen(cont->attr.da_pool) + 1;
217 #else
218 rc = uuid_parse(ptr, cont->attr.da_puuid);
219 if (rc)
220 goto out;
221 #endif
222 ptr += pool_len;
223
224 #if CHECK_DAOS_API_VERSION(1, 4)
225 strncpy(cont->attr.da_cont, ptr, DAOS_PROP_LABEL_MAX_LEN + 1);
226 cont->attr.da_cont[DAOS_PROP_LABEL_MAX_LEN] = 0;
227 cont_len = strlen(cont->attr.da_cont) + 1;
228 #else
229 rc = uuid_parse(ptr, cont->attr.da_cuuid);
230 if (rc)
231 goto out;
232 #endif
233 ptr += cont_len;
234
235 pool_hdl.iov_buf_len = *((daos_size_t *) ptr);
236 ptr += sizeof(daos_size_t);
237 pool_hdl.iov_buf = ptr;
238 pool_hdl.iov_len = pool_hdl.iov_buf_len;
239 rc = daos_pool_global2local(pool_hdl, &cont->poh);
240 if (rc)
241 goto out;
242 ptr += pool_hdl.iov_buf_len;
243
244 cont_hdl.iov_buf_len = *((daos_size_t *) ptr);
245 ptr += sizeof(daos_size_t);
246 cont_hdl.iov_buf = ptr;
247 cont_hdl.iov_len = cont_hdl.iov_buf_len;
248 rc = daos_cont_global2local(cont->poh, cont_hdl, &cont->coh);
249 if (rc)
250 goto out;
251 ptr += cont_hdl.iov_buf_len;
252
253 rc = cache_handles(cont);
254 if (rc)
255 goto out;
256
257 dfs_hdl.iov_buf_len = *((daos_size_t *) ptr);
258 ptr += sizeof(daos_size_t);
259 dfs_hdl.iov_buf = ptr;
260 dfs_hdl.iov_len = dfs_hdl.iov_buf_len;
261 rc = dfs_global2local(cont->poh, cont->coh, O_RDWR, dfs_hdl, &cont->dfs);
262 if (rc)
263 goto out;
264 ptr += dfs_hdl.iov_buf_len;
265
266 if (rank != 0) {
267 if (cont->c->dfs == NULL) {
268 cont->c->dfs = cont->dfs;
269 } else {
270 dfs_umount(cont->dfs);
271 cont->dfs = cont->c->dfs;
272 }
273 }
274
275 file_hdl.iov_buf_len = *((daos_size_t *) ptr);
276 ptr += sizeof(daos_size_t);
277 file_hdl.iov_buf = ptr;
278 file_hdl.iov_len = file_hdl.iov_buf_len;
279 rc = dfs_obj_global2local(cont->dfs, 0, file_hdl, &cont->obj);
280 if (rc)
281 goto out;
282 }
283
284 out:
285 ADIOI_Free(buf);
286 return rc;
287 }
288
289 static int get_pool_cont_uuids(const char *path, struct duns_attr_t *attr)
290 {
291 char *str;
292 int rc;
293
294 if (adio_daos_bypass_duns)
295 goto resolve_with_env;
296
297 if (adio_daos_path_prefix) {
298 char *new_path;
299
300 new_path = ADIOI_Malloc(strlen(path) + strlen(adio_daos_path_prefix) + 2);
301 if (new_path == NULL)
302 return ENOMEM;
303 snprintf(new_path, PATH_MAX, "%s/%s", adio_daos_path_prefix, path);
304 rc = duns_resolve_path(new_path, attr);
305 ADIOI_Free(new_path);
306 } else {
307 #if CHECK_DAOS_API_VERSION(1, 4)
308 attr->da_flags = DUNS_NO_PREFIX;
309 #else
310 attr->da_no_prefix = true;
311 #endif
312 rc = duns_resolve_path(path, attr);
313 }
314
315 if (rc)
316 PRINT_MSG(stderr, "duns_resolve_path() failed on path %s (%d)\n", path, rc);
317 return rc;
318
319 resolve_with_env:
320 /* use env variables to retrieve the pool and container */
321
322 str = getenv("DAOS_POOL");
323 if (str == NULL) {
324 PRINT_MSG(stderr, "Can't retrieve DAOS pool uuid/label\n");
325 return EINVAL;
326 }
327 #if CHECK_DAOS_API_VERSION(1, 4)
328 strncpy(attr->da_pool, str, DAOS_PROP_LABEL_MAX_LEN + 1);
329 attr->da_pool[DAOS_PROP_LABEL_MAX_LEN] = 0;
330 #else
331 if (uuid_parse(str, attr->da_puuid) < 0) {
332 PRINT_MSG(stderr, "Failed to parse pool uuid\n");
333 return EINVAL;
334 }
335 #endif
336
337 str = getenv("DAOS_CONT");
338 if (str == NULL) {
339 PRINT_MSG(stderr, "Can't retrieve DAOS cont uuid/label\n");
340 return EINVAL;
341 }
342 #if CHECK_DAOS_API_VERSION(1, 4)
343 strncpy(attr->da_cont, str, DAOS_PROP_LABEL_MAX_LEN + 1);
344 attr->da_cont[DAOS_PROP_LABEL_MAX_LEN] = 0;
345 #else
346 if (uuid_parse(str, attr->da_cuuid) < 0) {
347 PRINT_MSG(stderr, "Failed to parse cont uuid\n");
348 return EINVAL;
349 }
350 #endif
351
352 attr->da_oclass_id = OC_UNKNOWN;
353 attr->da_chunk_size = 0;
354
355 return 0;
356 }
357
358 void ADIOI_DAOS_Open(ADIO_File fd, int *error_code)
359 {
360 struct ADIO_DAOS_cont *cont = fd->fs_ptr;
361 static char myname[] = "ADIOI_DAOS_OPEN";
362 dfs_obj_t *parent = NULL;
363 int perm, old_mask, amode;
364 int rc;
365
366 *error_code = MPI_SUCCESS;
367
368 rc = parse_filename(fd->filename, &cont->obj_name, &cont->cont_name);
369 if (rc) {
370 *error_code = ADIOI_DAOS_err(myname, cont->cont_name, __LINE__, rc);
371 return;
372 }
373
374 rc = get_pool_cont_uuids(cont->cont_name, &cont->attr);
375 if (rc) {
376 *error_code = ADIOI_DAOS_err(myname, cont->cont_name, __LINE__, rc);
377 goto err_free;
378 }
379
380 /** Info object setting should override */
381 if (fd->hints->fs_hints.daos.obj_class != OC_UNKNOWN)
382 cont->attr.da_oclass_id = fd->hints->fs_hints.daos.obj_class;
383 if (fd->hints->fs_hints.daos.chunk_size != 0)
384 cont->attr.da_chunk_size = fd->hints->fs_hints.daos.chunk_size;
385
386 #if 0
387 {
388 char uuid_str[37];
389 uuid_unparse(cont->attr.da_cuuid, uuid_str);
390
391 fprintf(stderr, "Container Open %s %s\n", cont->cont_name, uuid_str);
392 fprintf(stderr, "File %s\n", cont->obj_name);
393 }
394 fprintf(stderr, "chunk_size = %d\n", cont->attr.da_chunk_size);
395 fprintf(stderr, "OCLASS = %d\n", cont->attr.da_oclass_id);
396 #endif
397
398 rc = adio_daos_poh_lookup_connect(&cont->attr, &cont->p);
399 if (rc) {
400 PRINT_MSG(stderr, "Failed to connect to DAOS Pool (%d)\n", rc);
401 *error_code = ADIOI_DAOS_err(myname, cont->cont_name, __LINE__, rc);
402 goto err_free;
403 }
404
405 cont->poh = cont->p->open_hdl;
406
407 rc = adio_daos_coh_lookup_create(cont->poh, &cont->attr, O_RDWR,
408 (fd->access_mode & ADIO_CREATE), &cont->c);
409 if (rc) {
410 *error_code = ADIOI_DAOS_err(myname, cont->cont_name, __LINE__, rc);
411 goto err_pool;
412 }
413
414 cont->coh = cont->c->open_hdl;
415
416 assert(cont->c->dfs);
417 cont->dfs = cont->c->dfs;
418
419 /* Set file access flags */
420 amode = 0;
421 if (fd->access_mode & ADIO_CREATE)
422 amode = amode | O_CREAT;
423 if (fd->access_mode & ADIO_RDONLY)
424 amode = amode | O_RDONLY;
425 if (fd->access_mode & ADIO_WRONLY)
426 amode = amode | O_WRONLY;
427 if (fd->access_mode & ADIO_RDWR)
428 amode = amode | O_RDWR;
429 if (fd->access_mode & ADIO_EXCL)
430 amode = amode | O_EXCL;
431
432 /* Set DFS permission mode + object type */
433 if (fd->perm == ADIO_PERM_NULL) {
434 old_mask = umask(022);
435 umask(old_mask);
436 perm = old_mask ^ 0666;
437 } else {
438 perm = fd->perm;
439 }
440 perm = S_IFREG | perm;
441
442 /* Lookup the parent directory. this will be NULL in case of root */
443 if (cont->attr.da_rel_path) {
444 rc = dfs_lookup(cont->dfs, cont->attr.da_rel_path, amode, &parent, NULL, NULL);
445 if (rc) {
446 *error_code = ADIOI_DAOS_err(myname, cont->obj_name, __LINE__, rc);
447 goto err_cont;
448 }
449 }
450
451 rc = dfs_open(cont->dfs, parent, cont->obj_name, perm, amode,
452 cont->attr.da_oclass_id, cont->attr.da_chunk_size, NULL, &cont->obj);
453
454 if (parent)
455 dfs_release(parent);
456
457 if (rc) {
458 *error_code = ADIOI_DAOS_err(myname, cont->obj_name, __LINE__, rc);
459 goto err_cont;
460 }
461
462 out:
463 return;
464 err_cont:
465 adio_daos_coh_release(cont->c);
466 cont->c = NULL;
467 err_pool:
468 adio_daos_poh_release(cont->p);
469 cont->p = NULL;
470 err_free:
471 #if CHECK_DAOS_API_VERSION(1, 4)
472 duns_destroy_attr(&cont->attr);
473 #else
474 if (cont->attr.da_rel_path) {
475 MPL_direct_free(cont->attr.da_rel_path);
476 cont->attr.da_rel_path = NULL;
477 }
478 #endif
479 ADIOI_Free(cont->obj_name);
480 ADIOI_Free(cont->cont_name);
481 goto out;
482 }
483
484 void ADIOI_DAOS_OpenColl(ADIO_File fd, int rank, int access_mode, int *error_code)
485 {
486 struct ADIO_DAOS_cont *cont;
487 int amode, orig_amode_wronly;
488 MPI_Comm comm = fd->comm;
489 int mpi_size;
490 int rc;
491 static char myname[] = "ADIOI_DAOS_OPENCOLL";
492
493 ADIOI_DAOS_Init(error_code);
494 if (*error_code != MPI_SUCCESS)
495 return;
496
497 MPI_Comm_size(comm, &mpi_size);
498
499 orig_amode_wronly = access_mode;
500 if (access_mode & ADIO_WRONLY) {
501 access_mode = access_mode ^ ADIO_WRONLY;
502 access_mode = access_mode | ADIO_RDWR;
503 }
504 fd->access_mode = access_mode;
505
506 amode = 0;
507 if (access_mode & ADIO_RDONLY)
508 amode = DAOS_COO_RO;
509 else
510 amode = DAOS_COO_RW;
511
512 cont = (struct ADIO_DAOS_cont *) ADIOI_Calloc(1, sizeof(struct ADIO_DAOS_cont));
513 if (cont == NULL) {
514 *error_code = MPI_ERR_NO_MEM;
515 return;
516 }
517
518 fd->access_mode = access_mode;
519 cont->amode = amode;
520 fd->fs_ptr = cont;
521
522 if (rank == 0) {
523 (*(fd->fns->ADIOI_xxx_Open)) (fd, error_code);
524 MPI_Error_class(*error_code, &rc);
525 }
526
527 if (mpi_size > 1) {
528 MPI_Bcast(&rc, 1, MPI_INT, 0, comm);
529
530 if (rank != 0) {
531 if (rc)
532 *error_code = MPIO_Err_create_code(MPI_SUCCESS,
533 MPIR_ERR_RECOVERABLE, myname,
534 __LINE__, rc, "File Open error", 0);
535 else
536 *error_code = MPI_SUCCESS;
537 }
538 }
539 if (*error_code != MPI_SUCCESS)
540 goto err_free;
541
542 if (mpi_size > 1) {
543 rc = share_cont_info(cont, rank, comm);
544 if (rc) {
545 *error_code = MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE, myname,
546 __LINE__, rc, "File Open error", 0);
547 goto err_free;
548 }
549 }
550
551 fd->is_open = 1;
552 fd->access_mode = orig_amode_wronly;
553
554 #ifdef ADIOI_MPE_LOGGING
555 MPE_Log_event(ADIOI_MPE_open_b, 0, NULL);
556 #endif
557
558 return;
559
560 err_free:
561 ADIOI_Free(cont);
562 return;
563 }
564
565 void ADIOI_DAOS_Flush(ADIO_File fd, int *error_code)
566 {
567 MPI_Barrier(fd->comm);
568 *error_code = MPI_SUCCESS;
569 }
570
571 void ADIOI_DAOS_Delete(const char *filename, int *error_code)
572 {
573 struct adio_daos_hdl *p, *c;
574 dfs_t *dfs;
575 char *obj_name, *cont_name;
576 dfs_obj_t *parent = NULL;
577 struct duns_attr_t attr = { };
578 static char myname[] = "ADIOI_DAOS_DELETE";
579 int rc;
580
581 ADIOI_DAOS_Init(error_code);
582 if (*error_code != MPI_SUCCESS)
583 return;
584
585 rc = parse_filename(filename, &obj_name, &cont_name);
586 if (rc) {
587 *error_code = MPI_ERR_NO_MEM;
588 return;
589 }
590
591 rc = get_pool_cont_uuids(cont_name, &attr);
592 if (rc) {
593 *error_code = ADIOI_DAOS_err(myname, cont_name, __LINE__, rc);
594 goto out_free;
595 }
596
597 rc = adio_daos_poh_lookup_connect(&attr, &p);
598 if (rc || p == NULL) {
599 PRINT_MSG(stderr, "Failed to connect to pool\n");
600 *error_code = ADIOI_DAOS_err(myname, cont_name, __LINE__, rc);
601 goto out_free;
602 }
603
604 rc = adio_daos_coh_lookup_create(p->open_hdl, &attr, O_RDWR, false, &c);
605 if (rc || c == NULL) {
606 *error_code = ADIOI_DAOS_err(myname, cont_name, __LINE__, rc);
607 goto out_pool;
608 }
609
610 if (c->dfs == NULL) {
611 /* Mount DFS namespace on the container */
612 rc = dfs_mount(p->open_hdl, c->open_hdl, O_RDWR, &dfs);
613 if (rc) {
614 PRINT_MSG(stderr, "Failed to mount flat namespace (%d)\n", rc);
615 *error_code = ADIOI_DAOS_err(myname, obj_name, __LINE__, rc);
616 goto out_cont;
617 }
618 /* Track the DFS handle with the container one */
619 c->dfs = dfs;
620 }
621
622 /* Lookup the parent directory. this will be NULL in case of root */
623 if (attr.da_rel_path) {
624 rc = dfs_lookup(c->dfs, attr.da_rel_path, O_RDWR, &parent, NULL, NULL);
625 if (rc) {
626 *error_code = ADIOI_DAOS_err(myname, obj_name, __LINE__, rc);
627 goto out_cont;
628 }
629 }
630
631 /* Remove the file */
632 rc = dfs_remove(c->dfs, parent, obj_name, false, NULL);
633 if (rc) {
634 *error_code = ADIOI_DAOS_err(myname, obj_name, __LINE__, rc);
635 goto out_dfs;
636 }
637
638 *error_code = MPI_SUCCESS;
639
640 out_dfs:
641 if (parent)
642 dfs_release(parent);
643 out_cont:
644 adio_daos_coh_release(c);
645 out_pool:
646 adio_daos_poh_release(p);
647 out_free:
648 #if CHECK_DAOS_API_VERSION(1, 4)
649 duns_destroy_attr(&attr);
650 #else
651 if (attr.da_rel_path)
652 MPL_direct_free(attr.da_rel_path);
653 #endif
654 ADIOI_Free(obj_name);
655 ADIOI_Free(cont_name);
656 return;
657 }