Coverage Report

Created: 2026-11-23 00:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hdf5/src/H5FDint.c
Line
Count
Source
1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2
 * Copyright by The HDF Group.                                               *
3
 * All rights reserved.                                                      *
4
 *                                                                           *
5
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
6
 * terms governing use, modification, and redistribution, is contained in    *
7
 * the LICENSE file, which can be found at the root of the source code       *
8
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
9
 * If you do not have access to either file, you may request a copy from     *
10
 * help@hdfgroup.org.                                                        *
11
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13
/*-------------------------------------------------------------------------
14
 *
15
 * Created:     H5FDint.c
16
 *
17
 * Purpose:     Internal routine for VFD operations
18
 *
19
 *-------------------------------------------------------------------------
20
 */
21
22
/****************/
23
/* Module Setup */
24
/****************/
25
26
#include "H5FDmodule.h" /* This source code file is part of the H5FD module */
27
28
/***********/
29
/* Headers */
30
/***********/
31
#include "H5private.h"   /* Generic Functions                        */
32
#include "H5CXprivate.h" /* API Contexts                             */
33
#include "H5Eprivate.h"  /* Error handling                           */
34
#include "H5Fprivate.h"  /* File access                              */
35
#include "H5FDpkg.h"     /* File Drivers                             */
36
#include "H5FLprivate.h" /* Free Lists                               */
37
#include "H5Iprivate.h"  /* IDs                                      */
38
#include "H5MMprivate.h" /* Memory management                        */
39
#include "H5PLprivate.h" /* Plugins                                  */
40
41
/****************/
42
/* Local Macros */
43
/****************/
44
45
/* Length of sequence lists requested from dataspace selections */
46
0
#define H5FD_SEQ_LIST_LEN 128
47
48
/* Length of stack allocated arrays for building vector I/O operations.
49
 * Corresponds to the number of contiguous blocks in a selection I/O operation.
50
 * If more space is needed dynamic allocation will be used instead. */
51
0
#define H5FD_LOCAL_VECTOR_LEN 8
52
53
/* Length of stack allocated arrays for dataspace IDs/structs for selection I/O
54
 * operations. Corresponds to the number of file selection/memory selection
55
 * pairs (along with addresses, etc.) in a selection I/O operation. If more
56
 * space is needed dynamic allocation will be used instead */
57
#define H5FD_LOCAL_SEL_ARR_LEN 8
58
59
/******************/
60
/* Local Typedefs */
61
/******************/
62
63
/*************************************************************************
64
 *
65
 * H5FD_srt_tmp_t
66
 *
67
 * Structure used to store I/O request addresses and the associated
68
 * indexes in the addrs[] array for the purpose of determine the sorted
69
 * order.
70
 *
71
 * This is done by allocating an array of H5FD_srt_tmp_t of length
72
 * count, loading it with the contents of the addrs[] array and the
73
 * associated indices, and then sorting it.
74
 *
75
 * This sorted array of H5FD_srt_tmp_t is then used to populate sorted
76
 * versions of the types[], addrs[], sizes[] and bufs[] vectors.
77
 *
78
 * addr:        haddr_t containing the value of addrs[i],
79
 *
80
 * index:       integer containing the value of i used to obtain the
81
 *              value of the addr field from the addrs[] vector.
82
 *
83
 *************************************************************************/
84
85
typedef struct H5FD_srt_tmp_t {
86
    haddr_t addr;
87
    size_t  index;
88
} H5FD_srt_tmp_t;
89
90
/* Information needed for iterating over the registered VFD hid_t IDs.
91
 * The name or value of the new VFD that is being registered is stored
92
 * in the name (or value) field and the found_id field is initialized to
93
 * H5I_INVALID_HID (-1).  If we find a VFD with the same name / value,
94
 * we set the found_id field to the existing ID for return to the function.
95
 */
96
typedef struct H5FD_get_driver_ud_t {
97
    /* IN */
98
    H5PL_vfd_key_t key;
99
100
    /* OUT */
101
    hid_t found_id; /* The driver ID, if we found a match */
102
} H5FD_get_driver_ud_t;
103
104
/********************/
105
/* Package Typedefs */
106
/********************/
107
108
/********************/
109
/* Local Prototypes */
110
/********************/
111
static int    H5FD__get_driver_cb(void *obj, hid_t id, void *_op_data);
112
static herr_t H5FD__read_selection_translate(uint32_t skip_vector_cb, H5FD_t *file, H5FD_mem_t type,
113
                                             hid_t dxpl_id, uint32_t count, H5S_t **mem_spaces,
114
                                             H5S_t **file_spaces, haddr_t offsets[], size_t element_sizes[],
115
                                             void *bufs[] /* out */);
116
static herr_t H5FD__write_selection_translate(uint32_t skip_vector_cb, H5FD_t *file, H5FD_mem_t type,
117
                                              hid_t dxpl_id, uint32_t count, H5S_t **mem_spaces,
118
                                              H5S_t **file_spaces, haddr_t offsets[], size_t element_sizes[],
119
                                              const void *bufs[]);
120
121
/*********************/
122
/* Package Variables */
123
/*********************/
124
125
/*****************************/
126
/* Library Private Variables */
127
/*****************************/
128
129
/*******************/
130
/* Local Variables */
131
/*******************/
132
133
/* Declare extern free list to manage the H5S_sel_iter_t struct */
134
H5FL_EXTERN(H5S_sel_iter_t);
135
136
/*-------------------------------------------------------------------------
137
 * Function:    H5FD_locate_signature
138
 *
139
 * Purpose:     Finds the HDF5 superblock signature in a file.  The
140
 *              signature can appear at address 0, or any power of two
141
 *              beginning with 512.
142
 *
143
 * Return:      SUCCEED/FAIL
144
 *
145
 *-------------------------------------------------------------------------
146
 */
147
herr_t
148
H5FD_locate_signature(H5FD_t *file, haddr_t *sig_addr)
149
264
{
150
264
    haddr_t  addr = HADDR_UNDEF;
151
264
    haddr_t  eoa  = HADDR_UNDEF;
152
264
    haddr_t  eof  = HADDR_UNDEF;
153
264
    uint8_t  buf[H5F_SIGNATURE_LEN];
154
264
    unsigned n;
155
264
    unsigned maxpow;
156
264
    herr_t   ret_value = SUCCEED; /* Return value */
157
158
264
    FUNC_ENTER_NOAPI_NOINIT
159
160
    /* Sanity checks */
161
264
    assert(file);
162
264
    assert(sig_addr);
163
164
    /* Find the least N such that 2^N is larger than the file size */
165
264
    eof  = H5FD_get_eof(file, H5FD_MEM_SUPER);
166
264
    eoa  = H5FD_get_eoa(file, H5FD_MEM_SUPER);
167
264
    addr = MAX(eof, eoa);
168
264
    if (HADDR_UNDEF == addr)
169
0
        HGOTO_ERROR(H5E_IO, H5E_CANTINIT, FAIL, "unable to obtain EOF/EOA value");
170
4.65k
    for (maxpow = 0; addr; maxpow++)
171
4.39k
        addr >>= 1;
172
264
    maxpow = MAX(maxpow, 9);
173
174
    /* Search for the file signature at format address zero followed by
175
     * powers of two larger than 9.
176
     */
177
548
    for (n = 8; n < maxpow; n++) {
178
546
        addr = (8 == n) ? 0 : (haddr_t)1 << n;
179
546
        if (H5FD_set_eoa(file, H5FD_MEM_SUPER, addr + H5F_SIGNATURE_LEN) < 0)
180
0
            HGOTO_ERROR(H5E_IO, H5E_CANTINIT, FAIL, "unable to set EOA value for file signature");
181
546
        if (H5FD_read(file, H5FD_MEM_SUPER, addr, (size_t)H5F_SIGNATURE_LEN, buf) < 0)
182
0
            HGOTO_ERROR(H5E_IO, H5E_CANTINIT, FAIL, "unable to read file signature");
183
546
        if (!memcmp(buf, H5F_SIGNATURE, (size_t)H5F_SIGNATURE_LEN))
184
262
            break;
185
546
    }
186
187
    /* If the signature was not found then reset the EOA value and return
188
     * HADDR_UNDEF.
189
     */
190
264
    if (n >= maxpow) {
191
2
        if (H5FD_set_eoa(file, H5FD_MEM_SUPER, eoa) < 0)
192
0
            HGOTO_ERROR(H5E_IO, H5E_CANTINIT, FAIL, "unable to reset EOA value");
193
2
        *sig_addr = HADDR_UNDEF;
194
2
    }
195
262
    else
196
        /* Set return value */
197
262
        *sig_addr = addr;
198
199
264
done:
200
264
    FUNC_LEAVE_NOAPI(ret_value)
201
264
} /* end H5FD_locate_signature() */
202
203
/*-------------------------------------------------------------------------
204
 * Function:    H5FD_read
205
 *
206
 * Purpose:     Private version of H5FDread()
207
 *
208
 * Return:      SUCCEED/FAIL
209
 *
210
 *-------------------------------------------------------------------------
211
 */
212
herr_t
213
H5FD_read(H5FD_t *file, H5FD_mem_t type, haddr_t addr, size_t size, void *buf /*out*/)
214
14.7k
{
215
14.7k
    hid_t    dxpl_id = H5I_INVALID_HID; /* DXPL for operation */
216
14.7k
    uint32_t actual_selection_io_mode;
217
14.7k
    herr_t   ret_value = SUCCEED; /* Return value */
218
219
14.7k
    FUNC_ENTER_NOAPI(FAIL)
220
221
    /* Sanity checks */
222
14.7k
    assert(file);
223
14.7k
    assert(file->cls);
224
14.7k
    assert(buf);
225
226
    /* Get proper DXPL for I/O */
227
14.7k
    dxpl_id = H5CX_get_dxpl();
228
229
14.7k
#ifndef H5_HAVE_PARALLEL
230
    /* The no-op case
231
     *
232
     * Do not return early for Parallel mode since the I/O could be a
233
     * collective transfer.
234
     */
235
14.7k
    if (0 == size)
236
0
        HGOTO_DONE(SUCCEED);
237
14.7k
#endif /* H5_HAVE_PARALLEL */
238
239
    /* If the file is open for SWMR read access, allow access to data past
240
     * the end of the allocated space (the 'eoa').  This is done because the
241
     * eoa stored in the file's superblock might be out of sync with the
242
     * objects being written within the file by the application performing
243
     * SWMR write operations.
244
     */
245
14.7k
    if (!(file->access_flags & H5F_ACC_SWMR_READ)) {
246
14.7k
        haddr_t eoa;
247
248
        /* Prepare & restore library for user callback */
249
14.7k
        H5_BEFORE_USER_CB(FAIL)
250
14.7k
            {
251
14.7k
                eoa = (file->cls->get_eoa)(file, type);
252
14.7k
            }
253
14.7k
        H5_AFTER_USER_CB(FAIL)
254
14.7k
        if (!H5_addr_defined(eoa))
255
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTINIT, FAIL, "driver get_eoa request failed");
256
257
14.7k
        if ((addr + file->base_addr + size) > eoa)
258
22
            HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "addr overflow, addr = %llu, size = %llu, eoa = %llu",
259
14.7k
                        (unsigned long long)(addr + file->base_addr), (unsigned long long)size,
260
14.7k
                        (unsigned long long)eoa);
261
14.7k
    }
262
263
    /* Prepare & restore library for user callback */
264
14.7k
    H5_BEFORE_USER_CB(FAIL)
265
14.7k
        {
266
            /* Dispatch to driver */
267
14.7k
            ret_value = (file->cls->read)(file, type, dxpl_id, addr + file->base_addr, size, buf);
268
14.7k
        }
269
14.7k
    H5_AFTER_USER_CB(FAIL)
270
14.7k
    if (ret_value < 0)
271
0
        HGOTO_ERROR(H5E_VFL, H5E_READERROR, FAIL, "driver read request failed");
272
273
    /* Set actual selection I/O, if this is a raw data operation */
274
14.7k
    if (type == H5FD_MEM_DRAW) {
275
313
        H5CX_get_actual_selection_io_mode(&actual_selection_io_mode);
276
313
        actual_selection_io_mode |= H5D_SCALAR_IO;
277
313
        H5CX_set_actual_selection_io_mode(actual_selection_io_mode);
278
313
    }
279
280
14.7k
done:
281
14.7k
    FUNC_LEAVE_NOAPI(ret_value)
282
14.7k
} /* end H5FD_read() */
283
284
/*-------------------------------------------------------------------------
285
 * Function:    H5FD_write
286
 *
287
 * Purpose:     Private version of H5FDwrite()
288
 *
289
 * Return:      SUCCEED/FAIL
290
 *
291
 *-------------------------------------------------------------------------
292
 */
293
herr_t
294
H5FD_write(H5FD_t *file, H5FD_mem_t type, haddr_t addr, size_t size, const void *buf)
295
0
{
296
0
    hid_t    dxpl_id;           /* DXPL for operation */
297
0
    haddr_t  eoa = HADDR_UNDEF; /* EOA for file */
298
0
    uint32_t actual_selection_io_mode;
299
0
    herr_t   ret_value = SUCCEED; /* Return value */
300
301
0
    FUNC_ENTER_NOAPI(FAIL)
302
303
    /* Sanity checks */
304
0
    assert(file);
305
0
    assert(file->cls);
306
0
    assert(buf);
307
308
    /* Get proper DXPL for I/O */
309
0
    dxpl_id = H5CX_get_dxpl();
310
311
0
#ifndef H5_HAVE_PARALLEL
312
    /* The no-op case
313
     *
314
     * Do not return early for Parallel mode since the I/O could be a
315
     * collective transfer.
316
     */
317
0
    if (0 == size)
318
0
        HGOTO_DONE(SUCCEED);
319
0
#endif /* H5_HAVE_PARALLEL */
320
321
    /* Prepare & restore library for user callback */
322
0
    H5_BEFORE_USER_CB(FAIL)
323
0
        {
324
0
            eoa = (file->cls->get_eoa)(file, type);
325
0
        }
326
0
    H5_AFTER_USER_CB(FAIL)
327
0
    if (!H5_addr_defined(eoa))
328
0
        HGOTO_ERROR(H5E_VFL, H5E_CANTINIT, FAIL, "driver get_eoa request failed");
329
0
    if ((addr + file->base_addr + size) > eoa)
330
0
        HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "addr overflow, addr = %llu, size=%llu, eoa=%llu",
331
0
                    (unsigned long long)(addr + file->base_addr), (unsigned long long)size,
332
0
                    (unsigned long long)eoa);
333
334
    /* Prepare & restore library for user callback */
335
0
    H5_BEFORE_USER_CB(FAIL)
336
0
        {
337
            /* Dispatch to driver */
338
0
            ret_value = (file->cls->write)(file, type, dxpl_id, addr + file->base_addr, size, buf);
339
0
        }
340
0
    H5_AFTER_USER_CB(FAIL)
341
0
    if (ret_value < 0)
342
0
        HGOTO_ERROR(H5E_VFL, H5E_WRITEERROR, FAIL, "driver write request failed");
343
344
    /* Set actual selection I/O, if this is a raw data operation */
345
0
    if (type == H5FD_MEM_DRAW) {
346
0
        H5CX_get_actual_selection_io_mode(&actual_selection_io_mode);
347
0
        actual_selection_io_mode |= H5D_SCALAR_IO;
348
0
        H5CX_set_actual_selection_io_mode(actual_selection_io_mode);
349
0
    }
350
351
0
done:
352
0
    FUNC_LEAVE_NOAPI(ret_value)
353
0
} /* end H5FD_write() */
354
355
/*-------------------------------------------------------------------------
356
 * Function:    H5FD_read_vector
357
 *
358
 * Purpose:     Private version of H5FDread_vector()
359
 *
360
 *              Perform count reads from the specified file at the offsets
361
 *              provided in the addrs array, with the lengths and memory
362
 *              types provided in the sizes and types arrays.  Data read
363
 *              is returned in the buffers provided in the bufs array.
364
 *
365
 *              If i > 0 and sizes[i] == 0, presume sizes[n] = sizes[i-1]
366
 *              for all n >= i and < count.
367
 *
368
 *              Similarly, if i > 0 and types[i] == H5FD_MEM_NOLIST,
369
 *              presume types[n] = types[i-1] for all n >= i and < count.
370
 *
371
 *              If the underlying VFD supports vector reads, pass the
372
 *              call through directly.
373
 *
374
 *              If it doesn't, convert the vector read into a sequence
375
 *              of individual reads.
376
 *
377
 *              Note that it is not in general possible to convert a
378
 *              vector read into a selection read, because each element
379
 *              in the vector read may have a different memory type.
380
 *              In contrast, selection reads are of a single type.
381
 *
382
 * Return:      Success:    SUCCEED
383
 *                          All reads have completed successfully, and
384
 *                          the results havce been into the supplied
385
 *                          buffers.
386
 *
387
 *              Failure:    FAIL
388
 *                          The contents of supplied buffers are undefined.
389
 *
390
 *-------------------------------------------------------------------------
391
 */
392
herr_t
393
H5FD_read_vector(H5FD_t *file, uint32_t count, H5FD_mem_t types[], haddr_t addrs[], size_t sizes[],
394
                 void *bufs[] /* out */)
395
0
{
396
0
    bool       addrs_cooked = false;
397
0
    bool       extend_sizes = false;
398
0
    bool       extend_types = false;
399
0
    uint32_t   i;
400
0
    size_t     size      = 0;
401
0
    H5FD_mem_t type      = H5FD_MEM_DEFAULT;
402
0
    hid_t      dxpl_id   = H5I_INVALID_HID; /* DXPL for operation */
403
0
    bool       is_raw    = false;           /* Does this include raw data */
404
0
    herr_t     ret_value = SUCCEED;         /* Return value */
405
406
0
    FUNC_ENTER_NOAPI(FAIL)
407
408
    /* Sanity checks */
409
0
    assert(file);
410
0
    assert(file->cls);
411
0
    assert((types) || (count == 0));
412
0
    assert((addrs) || (count == 0));
413
0
    assert((sizes) || (count == 0));
414
0
    assert((bufs) || (count == 0));
415
416
    /* verify that the first elements of the sizes and types arrays are
417
     * valid.
418
     */
419
0
    assert((count == 0) || (sizes[0] != 0));
420
0
    assert((count == 0) || (types[0] != H5FD_MEM_NOLIST));
421
422
    /* Get proper DXPL for I/O */
423
0
    dxpl_id = H5CX_get_dxpl();
424
425
0
#ifndef H5_HAVE_PARALLEL
426
    /* The no-op case
427
     *
428
     * Do not return early for Parallel mode since the I/O could be a
429
     * collective transfer.
430
     */
431
0
    if (0 == count)
432
0
        HGOTO_DONE(SUCCEED);
433
0
#endif /* H5_HAVE_PARALLEL */
434
435
0
    if (file->base_addr > 0) {
436
        /* apply the base_addr offset to the addrs array.  Must undo before
437
         * we return.
438
         */
439
0
        for (i = 0; i < count; i++)
440
0
            addrs[i] += file->base_addr;
441
0
        addrs_cooked = true;
442
0
    }
443
444
    /* If the file is open for SWMR read access, allow access to data past
445
     * the end of the allocated space (the 'eoa').  This is done because the
446
     * eoa stored in the file's superblock might be out of sync with the
447
     * objects being written within the file by the application performing
448
     * SWMR write operations.
449
     */
450
0
    if ((!(file->access_flags & H5F_ACC_SWMR_READ)) && (count > 0)) {
451
0
        haddr_t eoa;
452
453
0
        extend_sizes = false;
454
0
        extend_types = false;
455
456
0
        for (i = 0; i < count; i++) {
457
0
            if (!extend_sizes) {
458
0
                if (sizes[i] == 0) {
459
0
                    extend_sizes = true;
460
0
                    size         = sizes[i - 1];
461
0
                }
462
0
                else
463
0
                    size = sizes[i];
464
0
            }
465
466
0
            if (!extend_types) {
467
0
                if (types[i] == H5FD_MEM_NOLIST) {
468
0
                    extend_types = true;
469
0
                    type         = types[i - 1];
470
0
                }
471
0
                else {
472
0
                    type = types[i];
473
474
                    /* Check for raw data operation */
475
0
                    if (type == H5FD_MEM_DRAW)
476
0
                        is_raw = true;
477
0
                }
478
0
            }
479
480
            /* Prepare & restore library for user callback */
481
0
            H5_BEFORE_USER_CB(FAIL)
482
0
                {
483
0
                    eoa = (file->cls->get_eoa)(file, type);
484
0
                }
485
0
            H5_AFTER_USER_CB(FAIL)
486
0
            if (!H5_addr_defined(eoa))
487
0
                HGOTO_ERROR(H5E_VFL, H5E_CANTINIT, FAIL, "driver get_eoa request failed");
488
489
0
            if ((addrs[i] + size) > eoa)
490
0
                HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL,
491
0
                            "addr overflow, addrs[%d] = %llu, sizes[%d] = %llu, eoa = %llu", (int)i,
492
0
                            (unsigned long long)(addrs[i]), (int)i, (unsigned long long)size,
493
0
                            (unsigned long long)eoa);
494
0
        }
495
0
    }
496
0
    else
497
        /* We must still check if this is a raw data read */
498
0
        for (i = 0; i < count && types[i] != H5FD_MEM_NOLIST; i++)
499
0
            if (types[i] == H5FD_MEM_DRAW) {
500
0
                is_raw = true;
501
0
                break;
502
0
            }
503
504
    /* if the underlying VFD supports vector read, make the call */
505
0
    if (file->cls->read_vector) {
506
        /* Prepare & restore library for user callback */
507
0
        H5_BEFORE_USER_CB(FAIL)
508
0
            {
509
0
                ret_value = (file->cls->read_vector)(file, dxpl_id, count, types, addrs, sizes, bufs);
510
0
            }
511
0
        H5_AFTER_USER_CB(FAIL)
512
0
        if (ret_value < 0)
513
0
            HGOTO_ERROR(H5E_VFL, H5E_READERROR, FAIL, "driver read vector request failed");
514
515
        /* Set actual selection I/O mode, if this is a raw data operation */
516
0
        if (is_raw) {
517
0
            uint32_t actual_selection_io_mode;
518
519
0
            H5CX_get_actual_selection_io_mode(&actual_selection_io_mode);
520
0
            actual_selection_io_mode |= H5D_VECTOR_IO;
521
0
            H5CX_set_actual_selection_io_mode(actual_selection_io_mode);
522
0
        }
523
0
    }
524
0
    else {
525
        /* otherwise, implement the vector read as a sequence of regular
526
         * read calls.
527
         */
528
0
        extend_sizes = false;
529
0
        extend_types = false;
530
0
        uint32_t no_selection_io_cause;
531
0
        uint32_t actual_selection_io_mode;
532
533
0
        for (i = 0; i < count; i++) {
534
            /* we have already verified that sizes[0] != 0 and
535
             * types[0] != H5FD_MEM_NOLIST
536
             */
537
0
            if (!extend_sizes) {
538
0
                if (sizes[i] == 0) {
539
0
                    extend_sizes = true;
540
0
                    size         = sizes[i - 1];
541
0
                }
542
0
                else
543
0
                    size = sizes[i];
544
0
            }
545
546
0
            if (!extend_types) {
547
0
                if (types[i] == H5FD_MEM_NOLIST) {
548
0
                    extend_types = true;
549
0
                    type         = types[i - 1];
550
0
                }
551
0
                else
552
0
                    type = types[i];
553
0
            }
554
555
            /* Prepare & restore library for user callback */
556
0
            H5_BEFORE_USER_CB(FAIL)
557
0
                {
558
0
                    ret_value = (file->cls->read)(file, type, dxpl_id, addrs[i], size, bufs[i]);
559
0
                }
560
0
            H5_AFTER_USER_CB(FAIL)
561
0
            if (ret_value < 0)
562
0
                HGOTO_ERROR(H5E_VFL, H5E_READERROR, FAIL, "driver read request failed");
563
0
        }
564
565
        /* Add H5D_SEL_IO_NO_VECTOR_OR_SELECTION_IO_CB to no selection I/O cause */
566
0
        H5CX_get_no_selection_io_cause(&no_selection_io_cause);
567
0
        no_selection_io_cause |= H5D_SEL_IO_NO_VECTOR_OR_SELECTION_IO_CB;
568
0
        H5CX_set_no_selection_io_cause(no_selection_io_cause);
569
570
        /* Set actual selection I/O mode, if this is a raw data operation */
571
0
        if (is_raw) {
572
0
            H5CX_get_actual_selection_io_mode(&actual_selection_io_mode);
573
0
            actual_selection_io_mode |= H5D_SCALAR_IO;
574
0
            H5CX_set_actual_selection_io_mode(actual_selection_io_mode);
575
0
        }
576
0
    }
577
578
0
done:
579
    /* undo the base addr offset to the addrs array if necessary */
580
0
    if (addrs_cooked) {
581
0
        assert(file->base_addr > 0);
582
0
        for (i = 0; i < count; i++)
583
0
            addrs[i] -= file->base_addr;
584
0
    }
585
586
0
    FUNC_LEAVE_NOAPI(ret_value)
587
0
} /* end H5FD_read_vector() */
588
589
/*-------------------------------------------------------------------------
590
 * Function:    H5FD_write_vector
591
 *
592
 * Purpose:     Private version of H5FDwrite_vector()
593
 *
594
 *              Perform count writes to the specified file at the offsets
595
 *              provided in the addrs array, with the lengths and memory
596
 *              types provided in the sizes and types arrays.  Data written
597
 *              is taken from the buffers provided in the bufs array.
598
 *
599
 *              If i > 0 and sizes[i] == 0, presume sizes[n] = sizes[i-1]
600
 *              for all n >= i and < count.
601
 *
602
 *              Similarly, if i > 0 and types[i] == H5FD_MEM_NOLIST,
603
 *              presume types[n] = types[i-1] for all n >= i and < count.
604
 *
605
 *              If the underlying VFD supports vector writes, pass the
606
 *              call through directly.
607
 *
608
 *              If it doesn't, convert the vector write into a sequence
609
 *              of individual writes.
610
 *
611
 *              Note that it is not in general possible to convert a
612
 *              vector write into a selection write, because each element
613
 *              in the vector write may have a different memory type.
614
 *              In contrast, selection writes are of a single type.
615
 *
616
 * Return:      Success:    SUCCEED
617
 *                          All writes have completed successfully.
618
 *
619
 *              Failure:    FAIL
620
 *                          One or more writes failed.
621
 *
622
 *-------------------------------------------------------------------------
623
 */
624
herr_t
625
H5FD_write_vector(H5FD_t *file, uint32_t count, H5FD_mem_t types[], haddr_t addrs[], size_t sizes[],
626
                  const void *bufs[])
627
0
{
628
0
    bool       addrs_cooked = false;
629
0
    bool       extend_sizes = false;
630
0
    bool       extend_types = false;
631
0
    uint32_t   i;
632
0
    size_t     size = 0;
633
0
    H5FD_mem_t type = H5FD_MEM_DEFAULT;
634
0
    hid_t      dxpl_id;                 /* DXPL for operation */
635
0
    haddr_t    eoa       = HADDR_UNDEF; /* EOA for file */
636
0
    bool       is_raw    = false;       /* Does this include raw data */
637
0
    herr_t     ret_value = SUCCEED;     /* Return value */
638
639
0
    FUNC_ENTER_NOAPI(FAIL)
640
641
    /* Sanity checks */
642
0
    assert(file);
643
0
    assert(file->cls);
644
0
    assert((types) || (count == 0));
645
0
    assert((addrs) || (count == 0));
646
0
    assert((sizes) || (count == 0));
647
0
    assert((bufs) || (count == 0));
648
649
    /* verify that the first elements of the sizes and types arrays are
650
     * valid.
651
     */
652
0
    assert((count == 0) || (sizes[0] != 0));
653
0
    assert((count == 0) || (types[0] != H5FD_MEM_NOLIST));
654
655
    /* Get proper DXPL for I/O */
656
0
    dxpl_id = H5CX_get_dxpl();
657
658
0
#ifndef H5_HAVE_PARALLEL
659
    /* The no-op case
660
     *
661
     * Do not return early for Parallel mode since the I/O could be a
662
     * collective transfer.
663
     */
664
0
    if (0 == count)
665
0
        HGOTO_DONE(SUCCEED);
666
0
#endif /* H5_HAVE_PARALLEL */
667
668
0
    if (file->base_addr > 0) {
669
        /* apply the base_addr offset to the addrs array.  Must undo before
670
         * we return.
671
         */
672
0
        for (i = 0; i < count; i++)
673
0
            addrs[i] += file->base_addr;
674
0
        addrs_cooked = true;
675
0
    }
676
677
0
    extend_sizes = false;
678
0
    extend_types = false;
679
680
0
    for (i = 0; i < count; i++) {
681
0
        if (!extend_sizes) {
682
0
            if (sizes[i] == 0) {
683
0
                extend_sizes = true;
684
0
                size         = sizes[i - 1];
685
0
            }
686
0
            else
687
0
                size = sizes[i];
688
0
        }
689
690
0
        if (!extend_types) {
691
0
            if (types[i] == H5FD_MEM_NOLIST) {
692
0
                extend_types = true;
693
0
                type         = types[i - 1];
694
0
            }
695
0
            else {
696
0
                type = types[i];
697
698
                /* Check for raw data operation */
699
0
                if (type == H5FD_MEM_DRAW)
700
0
                    is_raw = true;
701
0
            }
702
0
        }
703
704
        /* Prepare & restore library for user callback */
705
0
        H5_BEFORE_USER_CB(FAIL)
706
0
            {
707
0
                eoa = (file->cls->get_eoa)(file, type);
708
0
            }
709
0
        H5_AFTER_USER_CB(FAIL)
710
0
        if (!H5_addr_defined(eoa))
711
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTINIT, FAIL, "driver get_eoa request failed");
712
713
0
        if ((addrs[i] + size) > eoa)
714
0
            HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL,
715
0
                        "addr overflow, addrs[%d] = %llu, sizes[%d] = %llu, eoa = %llu", (int)i,
716
0
                        (unsigned long long)(addrs[i]), (int)i, (unsigned long long)size,
717
0
                        (unsigned long long)eoa);
718
0
    }
719
720
    /* if the underlying VFD supports vector write, make the call */
721
0
    if (file->cls->write_vector) {
722
        /* Prepare & restore library for user callback */
723
0
        H5_BEFORE_USER_CB(FAIL)
724
0
            {
725
0
                ret_value = (file->cls->write_vector)(file, dxpl_id, count, types, addrs, sizes, bufs);
726
0
            }
727
0
        H5_AFTER_USER_CB(FAIL)
728
0
        if (ret_value < 0)
729
0
            HGOTO_ERROR(H5E_VFL, H5E_WRITEERROR, FAIL, "driver write vector request failed");
730
731
        /* Set actual selection I/O mode, if this is a raw data operation */
732
0
        if (is_raw) {
733
0
            uint32_t actual_selection_io_mode;
734
735
0
            H5CX_get_actual_selection_io_mode(&actual_selection_io_mode);
736
0
            actual_selection_io_mode |= H5D_VECTOR_IO;
737
0
            H5CX_set_actual_selection_io_mode(actual_selection_io_mode);
738
0
        }
739
0
    }
740
0
    else {
741
        /* otherwise, implement the vector write as a sequence of regular
742
         * write calls.
743
         */
744
0
        extend_sizes = false;
745
0
        extend_types = false;
746
0
        uint32_t no_selection_io_cause;
747
0
        uint32_t actual_selection_io_mode;
748
749
0
        for (i = 0; i < count; i++) {
750
            /* we have already verified that sizes[0] != 0 and
751
             * types[0] != H5FD_MEM_NOLIST
752
             */
753
0
            if (!extend_sizes) {
754
0
                if (sizes[i] == 0) {
755
0
                    extend_sizes = true;
756
0
                    size         = sizes[i - 1];
757
0
                }
758
0
                else
759
0
                    size = sizes[i];
760
0
            }
761
762
0
            if (!extend_types) {
763
0
                if (types[i] == H5FD_MEM_NOLIST) {
764
0
                    extend_types = true;
765
0
                    type         = types[i - 1];
766
0
                }
767
0
                else
768
0
                    type = types[i];
769
0
            }
770
771
            /* Prepare & restore library for user callback */
772
0
            H5_BEFORE_USER_CB(FAIL)
773
0
                {
774
0
                    ret_value = (file->cls->write)(file, type, dxpl_id, addrs[i], size, bufs[i]);
775
0
                }
776
0
            H5_AFTER_USER_CB(FAIL)
777
0
            if (ret_value < 0)
778
0
                HGOTO_ERROR(H5E_VFL, H5E_READERROR, FAIL, "driver write request failed");
779
0
        }
780
781
        /* Add H5D_SEL_IO_NO_VECTOR_OR_SELECTION_IO_CB to no selection I/O cause */
782
0
        H5CX_get_no_selection_io_cause(&no_selection_io_cause);
783
0
        no_selection_io_cause |= H5D_SEL_IO_NO_VECTOR_OR_SELECTION_IO_CB;
784
0
        H5CX_set_no_selection_io_cause(no_selection_io_cause);
785
786
        /* Set actual selection I/O mode, if this is a raw data operation */
787
0
        if (is_raw) {
788
0
            H5CX_get_actual_selection_io_mode(&actual_selection_io_mode);
789
0
            actual_selection_io_mode |= H5D_SCALAR_IO;
790
0
            H5CX_set_actual_selection_io_mode(actual_selection_io_mode);
791
0
        }
792
0
    }
793
794
0
done:
795
    /* undo the base addr offset to the addrs array if necessary */
796
0
    if (addrs_cooked) {
797
0
        assert(file->base_addr > 0);
798
0
        for (i = 0; i < count; i++)
799
0
            addrs[i] -= file->base_addr;
800
0
    }
801
802
0
    FUNC_LEAVE_NOAPI(ret_value)
803
0
} /* end H5FD_write_vector() */
804
805
/*-------------------------------------------------------------------------
806
 * Function:    H5FD__read_selection_translate
807
 *
808
 * Purpose:     Translates a selection read call to a vector read call if
809
 *              vector reads are supported and !skip_vector_cb,
810
 *              or a series of scalar read calls otherwise.
811
 *
812
 * Return:      Success:    SUCCEED
813
 *                          All reads have completed successfully, and
814
 *                          the results havce been into the supplied
815
 *                          buffers.
816
 *
817
 *              Failure:    FAIL
818
 *                          The contents of supplied buffers are undefined.
819
 *
820
 *-------------------------------------------------------------------------
821
 */
822
static herr_t
823
H5FD__read_selection_translate(uint32_t skip_vector_cb, H5FD_t *file, H5FD_mem_t type, hid_t dxpl_id,
824
                               uint32_t count, H5S_t **mem_spaces, H5S_t **file_spaces, haddr_t offsets[],
825
                               size_t element_sizes[], void *bufs[] /* out */)
826
0
{
827
0
    bool            extend_sizes = false;
828
0
    bool            extend_bufs  = false;
829
0
    uint32_t        i;
830
0
    size_t          element_size = 0;
831
0
    void           *buf          = NULL;
832
0
    bool            use_vector   = false;
833
0
    haddr_t         addrs_local[H5FD_LOCAL_VECTOR_LEN];
834
0
    haddr_t        *addrs = addrs_local;
835
0
    size_t          sizes_local[H5FD_LOCAL_VECTOR_LEN];
836
0
    size_t         *sizes = sizes_local;
837
0
    void           *vec_bufs_local[H5FD_LOCAL_VECTOR_LEN];
838
0
    void          **vec_bufs = vec_bufs_local;
839
0
    hsize_t         file_off[H5FD_SEQ_LIST_LEN];
840
0
    size_t          file_len[H5FD_SEQ_LIST_LEN];
841
0
    hsize_t         mem_off[H5FD_SEQ_LIST_LEN];
842
0
    size_t          mem_len[H5FD_SEQ_LIST_LEN];
843
0
    size_t          file_seq_i;
844
0
    size_t          mem_seq_i;
845
0
    size_t          file_nseq;
846
0
    size_t          mem_nseq;
847
0
    size_t          io_len;
848
0
    size_t          nelmts;
849
0
    hssize_t        hss_nelmts;
850
0
    size_t          seq_nelem;
851
0
    H5S_sel_iter_t *file_iter      = NULL;
852
0
    H5S_sel_iter_t *mem_iter       = NULL;
853
0
    bool            file_iter_init = false;
854
0
    bool            mem_iter_init  = false;
855
0
    H5FD_mem_t      types[2]       = {type, H5FD_MEM_NOLIST};
856
0
    size_t          vec_arr_nalloc = H5FD_LOCAL_VECTOR_LEN;
857
0
    size_t          vec_arr_nused  = 0;
858
0
    herr_t          ret_value      = SUCCEED;
859
860
0
    FUNC_ENTER_PACKAGE
861
862
    /* Sanity checks */
863
0
    assert(file);
864
0
    assert(file->cls);
865
0
    assert((mem_spaces) || (count == 0));
866
0
    assert((file_spaces) || (count == 0));
867
0
    assert((offsets) || (count == 0));
868
0
    assert((element_sizes) || (count == 0));
869
0
    assert((bufs) || (count == 0));
870
871
    /* Check if we're using vector I/O */
872
0
    use_vector = (file->cls->read_vector != NULL) && (!skip_vector_cb);
873
874
0
    if (count > 0) {
875
        /* Verify that the first elements of the element_sizes and bufs arrays are
876
         * valid. */
877
0
        assert(element_sizes[0] != 0);
878
0
        assert(bufs[0] != NULL);
879
880
        /* Allocate sequence lists for memory and file spaces */
881
0
        if (NULL == (file_iter = H5FL_MALLOC(H5S_sel_iter_t)))
882
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTALLOC, FAIL, "couldn't allocate file selection iterator");
883
0
        if (NULL == (mem_iter = H5FL_MALLOC(H5S_sel_iter_t)))
884
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTALLOC, FAIL, "couldn't allocate memory selection iterator");
885
0
    }
886
887
    /* Loop over dataspaces */
888
0
    for (i = 0; i < count; i++) {
889
890
        /* we have already verified that element_sizes[0] != 0 and bufs[0]
891
         * != NULL */
892
893
0
        if (!extend_sizes) {
894
895
0
            if (element_sizes[i] == 0) {
896
897
0
                extend_sizes = true;
898
0
                element_size = element_sizes[i - 1];
899
0
            }
900
0
            else {
901
902
0
                element_size = element_sizes[i];
903
0
            }
904
0
        }
905
906
0
        if (!extend_bufs) {
907
908
0
            if (bufs[i] == NULL) {
909
910
0
                extend_bufs = true;
911
0
                buf         = bufs[i - 1];
912
0
            }
913
0
            else {
914
915
0
                buf = bufs[i];
916
0
            }
917
0
        }
918
919
        /* Initialize sequence lists for memory and file spaces */
920
0
        if (H5S_select_iter_init(file_iter, file_spaces[i], element_size, 0) < 0)
921
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTINIT, FAIL, "can't initialize sequence list for file space");
922
0
        file_iter_init = true;
923
0
        if (H5S_select_iter_init(mem_iter, mem_spaces[i], element_size, 0) < 0)
924
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTINIT, FAIL, "can't initialize sequence list for memory space");
925
0
        mem_iter_init = true;
926
927
        /* Get the number of elements in selection */
928
0
        if ((hss_nelmts = (hssize_t)H5S_GET_SELECT_NPOINTS(file_spaces[i])) < 0)
929
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTCOUNT, FAIL, "can't get number of elements selected");
930
0
        H5_CHECKED_ASSIGN(nelmts, size_t, hss_nelmts, hssize_t);
931
932
#ifndef NDEBUG
933
        /* Verify mem space has the same number of elements */
934
        {
935
            if ((hss_nelmts = (hssize_t)H5S_GET_SELECT_NPOINTS(mem_spaces[i])) < 0)
936
                HGOTO_ERROR(H5E_VFL, H5E_CANTCOUNT, FAIL, "can't get number of elements selected");
937
            assert((hssize_t)nelmts == hss_nelmts);
938
        }
939
#endif /* NDEBUG */
940
941
        /* Initialize values so sequence lists are retrieved on the first
942
         * iteration */
943
0
        file_seq_i = H5FD_SEQ_LIST_LEN;
944
0
        mem_seq_i  = H5FD_SEQ_LIST_LEN;
945
0
        file_nseq  = 0;
946
0
        mem_nseq   = 0;
947
948
        /* Loop until all elements are processed */
949
0
        while (file_seq_i < file_nseq || nelmts > 0) {
950
            /* Fill/refill file sequence list if necessary */
951
0
            if (file_seq_i == H5FD_SEQ_LIST_LEN) {
952
0
                if (H5S_SELECT_ITER_GET_SEQ_LIST(file_iter, H5FD_SEQ_LIST_LEN, SIZE_MAX, &file_nseq,
953
0
                                                 &seq_nelem, file_off, file_len) < 0)
954
0
                    HGOTO_ERROR(H5E_INTERNAL, H5E_UNSUPPORTED, FAIL, "sequence length generation failed");
955
0
                assert(file_nseq > 0);
956
957
0
                nelmts -= seq_nelem;
958
0
                file_seq_i = 0;
959
0
            }
960
0
            assert(file_seq_i < file_nseq);
961
962
            /* Fill/refill memory sequence list if necessary */
963
0
            if (mem_seq_i == H5FD_SEQ_LIST_LEN) {
964
0
                if (H5S_SELECT_ITER_GET_SEQ_LIST(mem_iter, H5FD_SEQ_LIST_LEN, SIZE_MAX, &mem_nseq, &seq_nelem,
965
0
                                                 mem_off, mem_len) < 0)
966
0
                    HGOTO_ERROR(H5E_INTERNAL, H5E_UNSUPPORTED, FAIL, "sequence length generation failed");
967
0
                assert(mem_nseq > 0);
968
969
0
                mem_seq_i = 0;
970
0
            }
971
0
            assert(mem_seq_i < mem_nseq);
972
973
            /* Calculate length of this IO */
974
0
            io_len = MIN(file_len[file_seq_i], mem_len[mem_seq_i]);
975
976
            /* Check if we're using vector I/O */
977
0
            if (use_vector) {
978
                /* Check if we need to extend the arrays */
979
0
                if (vec_arr_nused == vec_arr_nalloc) {
980
                    /* Check if we're using the static arrays */
981
0
                    if (addrs == addrs_local) {
982
0
                        assert(sizes == sizes_local);
983
0
                        assert(vec_bufs == vec_bufs_local);
984
985
                        /* Allocate dynamic arrays */
986
0
                        if (NULL == (addrs = H5MM_malloc(sizeof(addrs_local) * 2)))
987
0
                            HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL,
988
0
                                        "memory allocation failed for address list");
989
0
                        if (NULL == (sizes = H5MM_malloc(sizeof(sizes_local) * 2)))
990
0
                            HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL,
991
0
                                        "memory allocation failed for size list");
992
0
                        if (NULL == (vec_bufs = H5MM_malloc(sizeof(vec_bufs_local) * 2)))
993
0
                            HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL,
994
0
                                        "memory allocation failed for buffer list");
995
996
                        /* Copy the existing data */
997
0
                        (void)H5MM_memcpy(addrs, addrs_local, sizeof(addrs_local));
998
0
                        (void)H5MM_memcpy(sizes, sizes_local, sizeof(sizes_local));
999
0
                        (void)H5MM_memcpy(vec_bufs, vec_bufs_local, sizeof(vec_bufs_local));
1000
0
                    }
1001
0
                    else {
1002
0
                        void *tmp_ptr;
1003
1004
                        /* Reallocate arrays */
1005
0
                        if (NULL == (tmp_ptr = H5MM_realloc(addrs, vec_arr_nalloc * sizeof(*addrs) * 2)))
1006
0
                            HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL,
1007
0
                                        "memory reallocation failed for address list");
1008
0
                        addrs = tmp_ptr;
1009
0
                        if (NULL == (tmp_ptr = H5MM_realloc(sizes, vec_arr_nalloc * sizeof(*sizes) * 2)))
1010
0
                            HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL,
1011
0
                                        "memory reallocation failed for size list");
1012
0
                        sizes = tmp_ptr;
1013
0
                        if (NULL ==
1014
0
                            (tmp_ptr = H5MM_realloc(vec_bufs, vec_arr_nalloc * sizeof(*vec_bufs) * 2)))
1015
0
                            HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL,
1016
0
                                        "memory reallocation failed for buffer list");
1017
0
                        vec_bufs = tmp_ptr;
1018
0
                    }
1019
1020
                    /* Record that we've doubled the array sizes */
1021
0
                    vec_arr_nalloc *= 2;
1022
0
                }
1023
1024
                /* Add this segment to vector read list */
1025
0
                addrs[vec_arr_nused]    = offsets[i] + file_off[file_seq_i];
1026
0
                sizes[vec_arr_nused]    = io_len;
1027
0
                vec_bufs[vec_arr_nused] = (void *)((uint8_t *)buf + mem_off[mem_seq_i]);
1028
0
                vec_arr_nused++;
1029
0
            }
1030
0
            else {
1031
                /* Prepare & restore library for user callback */
1032
0
                H5_BEFORE_USER_CB(FAIL)
1033
0
                    {
1034
                        /* Issue scalar read call */
1035
0
                        ret_value = (file->cls->read)(file, type, dxpl_id, offsets[i] + file_off[file_seq_i],
1036
0
                                                      io_len, (void *)((uint8_t *)buf + mem_off[mem_seq_i]));
1037
0
                    }
1038
0
                H5_AFTER_USER_CB(FAIL)
1039
0
                if (ret_value < 0)
1040
0
                    HGOTO_ERROR(H5E_VFL, H5E_READERROR, FAIL, "driver read request failed");
1041
0
            }
1042
1043
            /* Update file sequence */
1044
0
            if (io_len == file_len[file_seq_i])
1045
0
                file_seq_i++;
1046
0
            else {
1047
0
                file_off[file_seq_i] += io_len;
1048
0
                file_len[file_seq_i] -= io_len;
1049
0
            }
1050
1051
            /* Update memory sequence */
1052
0
            if (io_len == mem_len[mem_seq_i])
1053
0
                mem_seq_i++;
1054
0
            else {
1055
0
                mem_off[mem_seq_i] += io_len;
1056
0
                mem_len[mem_seq_i] -= io_len;
1057
0
            }
1058
0
        }
1059
1060
        /* Make sure both memory and file sequences terminated at the same time */
1061
0
        if (mem_seq_i < mem_nseq)
1062
0
            HGOTO_ERROR(H5E_INTERNAL, H5E_BADVALUE, FAIL,
1063
0
                        "file selection terminated before memory selection");
1064
1065
        /* Terminate iterators */
1066
0
        if (H5S_SELECT_ITER_RELEASE(file_iter) < 0)
1067
0
            HGOTO_ERROR(H5E_INTERNAL, H5E_CANTFREE, FAIL, "can't release file selection iterator");
1068
0
        file_iter_init = false;
1069
0
        if (H5S_SELECT_ITER_RELEASE(mem_iter) < 0)
1070
0
            HGOTO_ERROR(H5E_INTERNAL, H5E_CANTFREE, FAIL, "can't release memory selection iterator");
1071
0
        mem_iter_init = false;
1072
0
    }
1073
1074
    /* Issue vector read call if appropriate */
1075
0
    if (use_vector) {
1076
0
        uint32_t actual_selection_io_mode;
1077
1078
0
        H5_CHECK_OVERFLOW(vec_arr_nused, size_t, uint32_t);
1079
        /* Prepare & restore library for user callback */
1080
0
        H5_BEFORE_USER_CB(FAIL)
1081
0
            {
1082
0
                ret_value = (file->cls->read_vector)(file, dxpl_id, (uint32_t)vec_arr_nused, types, addrs,
1083
0
                                                     sizes, vec_bufs);
1084
0
            }
1085
0
        H5_AFTER_USER_CB(FAIL)
1086
0
        if (ret_value < 0)
1087
0
            HGOTO_ERROR(H5E_VFL, H5E_READERROR, FAIL, "driver read vector request failed");
1088
1089
        /* Set actual selection I/O, if this is a raw data operation */
1090
0
        if (type == H5FD_MEM_DRAW && count > 0) {
1091
0
            H5CX_get_actual_selection_io_mode(&actual_selection_io_mode);
1092
0
            actual_selection_io_mode |= H5D_VECTOR_IO;
1093
0
            H5CX_set_actual_selection_io_mode(actual_selection_io_mode);
1094
0
        }
1095
0
    }
1096
0
    else if (count > 0) {
1097
0
        uint32_t no_selection_io_cause;
1098
0
        uint32_t actual_selection_io_mode;
1099
1100
        /* Add H5D_SEL_IO_NO_VECTOR_OR_SELECTION_IO_CB to no selection I/O cause */
1101
0
        H5CX_get_no_selection_io_cause(&no_selection_io_cause);
1102
0
        no_selection_io_cause |= H5D_SEL_IO_NO_VECTOR_OR_SELECTION_IO_CB;
1103
0
        H5CX_set_no_selection_io_cause(no_selection_io_cause);
1104
1105
        /* Set actual selection I/O, if this is a raw data operation */
1106
0
        if (type == H5FD_MEM_DRAW) {
1107
0
            H5CX_get_actual_selection_io_mode(&actual_selection_io_mode);
1108
0
            actual_selection_io_mode |= H5D_SCALAR_IO;
1109
0
            H5CX_set_actual_selection_io_mode(actual_selection_io_mode);
1110
0
        }
1111
0
    }
1112
1113
0
done:
1114
    /* Terminate and free iterators */
1115
0
    if (file_iter) {
1116
0
        if (file_iter_init && H5S_SELECT_ITER_RELEASE(file_iter) < 0)
1117
0
            HGOTO_ERROR(H5E_INTERNAL, H5E_CANTFREE, FAIL, "can't release file selection iterator");
1118
0
        file_iter = H5FL_FREE(H5S_sel_iter_t, file_iter);
1119
0
    }
1120
0
    if (mem_iter) {
1121
0
        if (mem_iter_init && H5S_SELECT_ITER_RELEASE(mem_iter) < 0)
1122
0
            HGOTO_ERROR(H5E_INTERNAL, H5E_CANTFREE, FAIL, "can't release memory selection iterator");
1123
0
        mem_iter = H5FL_FREE(H5S_sel_iter_t, mem_iter);
1124
0
    }
1125
1126
    /* Cleanup vector arrays */
1127
0
    if (use_vector) {
1128
0
        if (addrs != addrs_local)
1129
0
            addrs = H5MM_xfree(addrs);
1130
0
        if (sizes != sizes_local)
1131
0
            sizes = H5MM_xfree(sizes);
1132
0
        if (vec_bufs != vec_bufs_local)
1133
0
            vec_bufs = H5MM_xfree(vec_bufs);
1134
0
    }
1135
1136
    /* Make sure we cleaned up */
1137
0
    assert(!addrs || addrs == addrs_local);
1138
0
    assert(!sizes || sizes == sizes_local);
1139
0
    assert(!vec_bufs || vec_bufs == vec_bufs_local);
1140
1141
0
    FUNC_LEAVE_NOAPI(ret_value)
1142
0
} /* end H5FD__read_selection_translate() */
1143
1144
/*-------------------------------------------------------------------------
1145
 * Function:    H5FD_read_selection
1146
 *
1147
 * Purpose:     Private version of H5FDread_selection()
1148
 *
1149
 *              Perform count reads from the specified file at the
1150
 *              locations selected in the dataspaces in the file_spaces
1151
 *              array, with each of those dataspaces starting at the file
1152
 *              address specified by the corresponding element of the
1153
 *              offsets array, and with the size of each element in the
1154
 *              dataspace specified by the corresponding element of the
1155
 *              element_sizes array.  The memory type provided by type is
1156
 *              the same for all selections.  Data read is returned in
1157
 *              the locations selected in the dataspaces in the
1158
 *              mem_spaces array, within the buffers provided in the
1159
 *              corresponding elements of the bufs array.
1160
 *
1161
 *              If i > 0 and element_sizes[i] == 0, presume
1162
 *              element_sizes[n] = element_sizes[i-1] for all n >= i and
1163
 *              < count.
1164
 *
1165
 *              If the underlying VFD supports selection reads, pass the
1166
 *              call through directly.
1167
 *
1168
 *              If it doesn't, convert the selection read into a sequence
1169
 *              of vector or scalar reads.
1170
 *
1171
 * Return:      Success:    SUCCEED
1172
 *                          All reads have completed successfully, and
1173
 *                          the results havce been into the supplied
1174
 *                          buffers.
1175
 *
1176
 *              Failure:    FAIL
1177
 *                          The contents of supplied buffers are undefined.
1178
 *
1179
 *-------------------------------------------------------------------------
1180
 */
1181
herr_t
1182
H5FD_read_selection(H5FD_t *file, H5FD_mem_t type, uint32_t count, H5S_t **mem_spaces, H5S_t **file_spaces,
1183
                    haddr_t offsets[], size_t element_sizes[], void *bufs[] /* out */)
1184
0
{
1185
0
    bool     offsets_cooked = false;
1186
0
    hid_t    mem_space_ids_local[H5FD_LOCAL_SEL_ARR_LEN];
1187
0
    hid_t   *mem_space_ids = mem_space_ids_local;
1188
0
    hid_t    file_space_ids_local[H5FD_LOCAL_SEL_ARR_LEN];
1189
0
    hid_t   *file_space_ids = file_space_ids_local;
1190
0
    uint32_t num_spaces     = 0;
1191
0
    hid_t    dxpl_id        = H5I_INVALID_HID; /* DXPL for operation */
1192
0
    uint32_t i;
1193
0
    herr_t   ret_value = SUCCEED; /* Return value */
1194
1195
0
    FUNC_ENTER_NOAPI(FAIL)
1196
1197
    /* Sanity checks */
1198
0
    assert(file);
1199
0
    assert(file->cls);
1200
0
    assert((mem_spaces) || (count == 0));
1201
0
    assert((file_spaces) || (count == 0));
1202
0
    assert((offsets) || (count == 0));
1203
0
    assert((element_sizes) || (count == 0));
1204
0
    assert((bufs) || (count == 0));
1205
1206
    /* Verify that the first elements of the element_sizes and bufs arrays are
1207
     * valid. */
1208
0
    assert((count == 0) || (element_sizes[0] != 0));
1209
0
    assert((count == 0) || (bufs[0] != NULL));
1210
1211
    /* Get proper DXPL for I/O */
1212
0
    dxpl_id = H5CX_get_dxpl();
1213
1214
0
#ifndef H5_HAVE_PARALLEL
1215
    /* The no-op case
1216
     *
1217
     * Do not return early for Parallel mode since the I/O could be a
1218
     * collective transfer.
1219
     */
1220
0
    if (0 == count)
1221
0
        HGOTO_DONE(SUCCEED);
1222
0
#endif /* H5_HAVE_PARALLEL */
1223
1224
0
    if (file->base_addr > 0) {
1225
        /* apply the base_addr offset to the offsets array.  Must undo before
1226
         * we return.
1227
         */
1228
0
        for (i = 0; i < count; i++)
1229
0
            offsets[i] += file->base_addr;
1230
0
        offsets_cooked = true;
1231
0
    }
1232
1233
    /* If the file is open for SWMR read access, allow access to data past
1234
     * the end of the allocated space (the 'eoa').  This is done because the
1235
     * eoa stored in the file's superblock might be out of sync with the
1236
     * objects being written within the file by the application performing
1237
     * SWMR write operations.
1238
     */
1239
    /* For now at least, only check that the offset is not past the eoa, since
1240
     * looking into the highest offset in the selection (different from the
1241
     * bounds) is potentially expensive.
1242
     */
1243
0
    if (!(file->access_flags & H5F_ACC_SWMR_READ)) {
1244
0
        haddr_t eoa;
1245
1246
        /* Prepare & restore library for user callback */
1247
0
        H5_BEFORE_USER_CB(FAIL)
1248
0
            {
1249
0
                eoa = (file->cls->get_eoa)(file, type);
1250
0
            }
1251
0
        H5_AFTER_USER_CB(FAIL)
1252
0
        if (!H5_addr_defined(eoa))
1253
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTINIT, FAIL, "driver get_eoa request failed");
1254
1255
0
        for (i = 0; i < count; i++)
1256
0
            if ((offsets[i]) > eoa)
1257
0
                HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "addr overflow, offsets[%d] = %llu, eoa = %llu",
1258
0
                            (int)i, (unsigned long long)(offsets[i]), (unsigned long long)eoa);
1259
0
    }
1260
1261
    /* if the underlying VFD supports selection read, make the call */
1262
0
    if (file->cls->read_selection) {
1263
0
        uint32_t actual_selection_io_mode;
1264
1265
        /* Allocate array of space IDs if necessary, otherwise use local
1266
         * buffers */
1267
0
        if (count > sizeof(mem_space_ids_local) / sizeof(mem_space_ids_local[0])) {
1268
0
            if (NULL == (mem_space_ids = H5MM_malloc(count * sizeof(hid_t))))
1269
0
                HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL, "memory allocation failed for dataspace list");
1270
0
            if (NULL == (file_space_ids = H5MM_malloc(count * sizeof(hid_t))))
1271
0
                HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL, "memory allocation failed for dataspace list");
1272
0
        }
1273
1274
        /* Create IDs for all dataspaces */
1275
0
        for (; num_spaces < count; num_spaces++) {
1276
0
            if ((mem_space_ids[num_spaces] = H5I_register(H5I_DATASPACE, mem_spaces[num_spaces], true)) < 0)
1277
0
                HGOTO_ERROR(H5E_VFL, H5E_CANTREGISTER, FAIL, "unable to register dataspace ID");
1278
1279
0
            if ((file_space_ids[num_spaces] = H5I_register(H5I_DATASPACE, file_spaces[num_spaces], true)) <
1280
0
                0) {
1281
0
                if (NULL == H5I_remove(mem_space_ids[num_spaces]))
1282
0
                    HDONE_ERROR(H5E_VFL, H5E_CANTREMOVE, FAIL, "problem removing id");
1283
0
                HGOTO_ERROR(H5E_VFL, H5E_CANTREGISTER, FAIL, "unable to register dataspace ID");
1284
0
            }
1285
0
        }
1286
1287
        /* Prepare & restore library for user callback */
1288
0
        H5_BEFORE_USER_CB(FAIL)
1289
0
            {
1290
0
                ret_value = (file->cls->read_selection)(file, type, dxpl_id, count, mem_space_ids,
1291
0
                                                        file_space_ids, offsets, element_sizes, bufs);
1292
0
            }
1293
0
        H5_AFTER_USER_CB(FAIL)
1294
0
        if (ret_value < 0)
1295
0
            HGOTO_ERROR(H5E_VFL, H5E_READERROR, FAIL, "driver read selection request failed");
1296
1297
        /* Set actual selection I/O, if this is a raw data operation */
1298
0
        if (type == H5FD_MEM_DRAW) {
1299
0
            H5CX_get_actual_selection_io_mode(&actual_selection_io_mode);
1300
0
            actual_selection_io_mode |= H5D_SELECTION_IO;
1301
0
            H5CX_set_actual_selection_io_mode(actual_selection_io_mode);
1302
0
        }
1303
0
    }
1304
0
    else
1305
        /* Otherwise, implement the selection read as a sequence of regular
1306
         * or vector read calls.
1307
         */
1308
0
        if (H5FD__read_selection_translate(SKIP_NO_CB, file, type, dxpl_id, count, mem_spaces, file_spaces,
1309
0
                                           offsets, element_sizes, bufs) < 0)
1310
0
            HGOTO_ERROR(H5E_VFL, H5E_READERROR, FAIL, "translation to vector or scalar read failed");
1311
1312
0
done:
1313
    /* undo the base addr offset to the offsets array if necessary */
1314
0
    if (offsets_cooked) {
1315
0
        assert(file->base_addr > 0);
1316
0
        for (i = 0; i < count; i++)
1317
0
            offsets[i] -= file->base_addr;
1318
0
    }
1319
1320
    /* Cleanup dataspace arrays.  Use H5I_remove() so we only close the IDs and
1321
     * not the underlying dataspaces, which were not created by this function.
1322
     */
1323
0
    for (i = 0; i < num_spaces; i++) {
1324
0
        if (NULL == H5I_remove(mem_space_ids[i]))
1325
0
            HDONE_ERROR(H5E_VFL, H5E_CANTREMOVE, FAIL, "problem removing id");
1326
0
        if (NULL == H5I_remove(file_space_ids[i]))
1327
0
            HDONE_ERROR(H5E_VFL, H5E_CANTREMOVE, FAIL, "problem removing id");
1328
0
    }
1329
0
    if (mem_space_ids != mem_space_ids_local)
1330
0
        mem_space_ids = H5MM_xfree(mem_space_ids);
1331
0
    if (file_space_ids != file_space_ids_local)
1332
0
        file_space_ids = H5MM_xfree(file_space_ids);
1333
1334
0
    FUNC_LEAVE_NOAPI(ret_value)
1335
0
} /* end H5FD_read_selection() */
1336
1337
/*-------------------------------------------------------------------------
1338
 * Function:    H5FD_read_selection_id
1339
 *
1340
 * Purpose:     Like H5FD_read_selection(), but takes hid_t arrays instead
1341
 *              of H5S_t * arrays for the dataspaces.
1342
 *
1343
 *              Depending on the parameter skip_cb which is translated into
1344
 *              skip_selection_cb and skip_vector_cb:
1345
 *
1346
 *              --If the underlying VFD supports selection reads and !skip_selection_cb,
1347
 *                pass the call through directly.
1348
 *
1349
 *              --If it doesn't, convert the selection reads into a sequence of vector or
1350
 *                scalar reads depending on skip_vector_cb.
1351
 *
1352
 * Return:      Success:    SUCCEED
1353
 *                          All reads have completed successfully, and
1354
 *                          the results havce been into the supplied
1355
 *                          buffers.
1356
 *
1357
 *              Failure:    FAIL
1358
 *                          The contents of supplied buffers are undefined.
1359
 *
1360
 *-------------------------------------------------------------------------
1361
 */
1362
herr_t
1363
H5FD_read_selection_id(uint32_t skip_cb, H5FD_t *file, H5FD_mem_t type, uint32_t count, hid_t mem_space_ids[],
1364
                       hid_t file_space_ids[], haddr_t offsets[], size_t element_sizes[],
1365
                       void *bufs[] /* out */)
1366
0
{
1367
0
    bool     offsets_cooked = false;
1368
0
    H5S_t   *mem_spaces_local[H5FD_LOCAL_SEL_ARR_LEN];
1369
0
    H5S_t  **mem_spaces = mem_spaces_local;
1370
0
    H5S_t   *file_spaces_local[H5FD_LOCAL_SEL_ARR_LEN];
1371
0
    H5S_t  **file_spaces = file_spaces_local;
1372
0
    hid_t    dxpl_id     = H5I_INVALID_HID; /* DXPL for operation */
1373
0
    uint32_t i;
1374
0
    uint32_t skip_selection_cb;
1375
0
    uint32_t skip_vector_cb;
1376
0
    herr_t   ret_value = SUCCEED; /* Return value */
1377
1378
0
    FUNC_ENTER_NOAPI(FAIL)
1379
1380
    /* Sanity checks */
1381
0
    assert(file);
1382
0
    assert(file->cls);
1383
0
    assert((mem_space_ids) || (count == 0));
1384
0
    assert((file_space_ids) || (count == 0));
1385
0
    assert((offsets) || (count == 0));
1386
0
    assert((element_sizes) || (count == 0));
1387
0
    assert((bufs) || (count == 0));
1388
1389
    /* Verify that the first elements of the element_sizes and bufs arrays are
1390
     * valid. */
1391
0
    assert((count == 0) || (element_sizes[0] != 0));
1392
0
    assert((count == 0) || (bufs[0] != NULL));
1393
1394
    /* Get proper DXPL for I/O */
1395
0
    dxpl_id = H5CX_get_dxpl();
1396
1397
0
#ifndef H5_HAVE_PARALLEL
1398
    /* The no-op case
1399
     *
1400
     * Do not return early for Parallel mode since the I/O could be a
1401
     * collective transfer.
1402
     */
1403
0
    if (0 == count)
1404
0
        HGOTO_DONE(SUCCEED);
1405
0
#endif /* H5_HAVE_PARALLEL */
1406
1407
0
    skip_selection_cb = skip_cb & SKIP_SELECTION_CB;
1408
0
    skip_vector_cb    = skip_cb & SKIP_VECTOR_CB;
1409
1410
0
    if (file->base_addr > 0) {
1411
        /* apply the base_addr offset to the offsets array.  Must undo before
1412
         * we return.
1413
         */
1414
0
        for (i = 0; i < count; i++)
1415
0
            offsets[i] += file->base_addr;
1416
0
        offsets_cooked = true;
1417
0
    }
1418
1419
    /* If the file is open for SWMR read access, allow access to data past
1420
     * the end of the allocated space (the 'eoa').  This is done because the
1421
     * eoa stored in the file's superblock might be out of sync with the
1422
     * objects being written within the file by the application performing
1423
     * SWMR write operations.
1424
     */
1425
    /* For now at least, only check that the offset is not past the eoa, since
1426
     * looking into the highest offset in the selection (different from the
1427
     * bounds) is potentially expensive.
1428
     */
1429
0
    if (!(file->access_flags & H5F_ACC_SWMR_READ)) {
1430
0
        haddr_t eoa;
1431
1432
        /* Prepare & restore library for user callback */
1433
0
        H5_BEFORE_USER_CB(FAIL)
1434
0
            {
1435
0
                eoa = (file->cls->get_eoa)(file, type);
1436
0
            }
1437
0
        H5_AFTER_USER_CB(FAIL)
1438
0
        if (!H5_addr_defined(eoa))
1439
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTINIT, FAIL, "driver get_eoa request failed");
1440
1441
0
        for (i = 0; i < count; i++)
1442
0
            if ((offsets[i]) > eoa)
1443
0
                HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "addr overflow, offsets[%d] = %llu, eoa = %llu",
1444
0
                            (int)i, (unsigned long long)(offsets[i]), (unsigned long long)eoa);
1445
0
    }
1446
1447
    /* if the underlying VFD supports selection read, make the call */
1448
0
    if (!skip_selection_cb && file->cls->read_selection) {
1449
0
        uint32_t actual_selection_io_mode;
1450
1451
        /* Prepare & restore library for user callback */
1452
0
        H5_BEFORE_USER_CB(FAIL)
1453
0
            {
1454
0
                ret_value = (file->cls->read_selection)(file, type, dxpl_id, count, mem_space_ids,
1455
0
                                                        file_space_ids, offsets, element_sizes, bufs);
1456
0
            }
1457
0
        H5_AFTER_USER_CB(FAIL)
1458
0
        if (ret_value < 0)
1459
0
            HGOTO_ERROR(H5E_VFL, H5E_READERROR, FAIL, "driver read selection request failed");
1460
1461
        /* Set actual selection I/O, if this is a raw data operation */
1462
0
        if (type == H5FD_MEM_DRAW) {
1463
0
            H5CX_get_actual_selection_io_mode(&actual_selection_io_mode);
1464
0
            actual_selection_io_mode |= H5D_SELECTION_IO;
1465
0
            H5CX_set_actual_selection_io_mode(actual_selection_io_mode);
1466
0
        }
1467
0
    }
1468
0
    else {
1469
        /* Otherwise, implement the selection read as a sequence of regular
1470
         * or vector read calls.
1471
         */
1472
1473
        /* Allocate arrays of space objects if necessary, otherwise use local
1474
         * buffers */
1475
0
        if (count > sizeof(mem_spaces_local) / sizeof(mem_spaces_local[0])) {
1476
0
            if (NULL == (mem_spaces = H5MM_malloc(count * sizeof(H5S_t *))))
1477
0
                HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL, "memory allocation failed for dataspace list");
1478
0
            if (NULL == (file_spaces = H5MM_malloc(count * sizeof(H5S_t *))))
1479
0
                HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL, "memory allocation failed for dataspace list");
1480
0
        }
1481
1482
        /* Get object pointers for all dataspaces */
1483
0
        for (i = 0; i < count; i++) {
1484
0
            if (NULL == (mem_spaces[i] = (H5S_t *)H5I_object_verify(mem_space_ids[i], H5I_DATASPACE)))
1485
0
                HGOTO_ERROR(H5E_VFL, H5E_BADTYPE, H5I_INVALID_HID, "can't retrieve memory dataspace from ID");
1486
0
            if (NULL == (file_spaces[i] = (H5S_t *)H5I_object_verify(file_space_ids[i], H5I_DATASPACE)))
1487
0
                HGOTO_ERROR(H5E_VFL, H5E_BADTYPE, H5I_INVALID_HID, "can't retrieve file dataspace from ID");
1488
0
        }
1489
1490
        /* Translate to vector or scalar I/O */
1491
1492
0
        if (H5FD__read_selection_translate(skip_vector_cb, file, type, dxpl_id, count, mem_spaces,
1493
0
                                           file_spaces, offsets, element_sizes, bufs) < 0)
1494
0
            HGOTO_ERROR(H5E_VFL, H5E_READERROR, FAIL, "translation to vector or scalar read failed");
1495
0
    }
1496
1497
0
done:
1498
    /* undo the base addr offset to the offsets array if necessary */
1499
0
    if (offsets_cooked) {
1500
0
        assert(file->base_addr > 0);
1501
0
        for (i = 0; i < count; i++)
1502
0
            offsets[i] -= file->base_addr;
1503
0
    }
1504
1505
    /* Cleanup dataspace arrays */
1506
0
    if (mem_spaces != mem_spaces_local)
1507
0
        mem_spaces = H5MM_xfree(mem_spaces);
1508
0
    if (file_spaces != file_spaces_local)
1509
0
        file_spaces = H5MM_xfree(file_spaces);
1510
1511
0
    FUNC_LEAVE_NOAPI(ret_value)
1512
0
} /* end H5FD_read_selection_id() */
1513
1514
/*-------------------------------------------------------------------------
1515
 * Function:    H5FD__write_selection_translate
1516
 *
1517
 * Purpose:     Translates a selection write call to a vector write call
1518
 *              if vector writes are supported and !skip_vector_cb,
1519
 *              or a series of scalar write calls otherwise.
1520
 *
1521
 * Return:      Success:    SUCCEED
1522
 *                          All writes have completed successfully.
1523
 *
1524
 *              Failure:    FAIL
1525
 *                          One or more writes failed.
1526
 *
1527
 *-------------------------------------------------------------------------
1528
 */
1529
static herr_t
1530
H5FD__write_selection_translate(uint32_t skip_vector_cb, H5FD_t *file, H5FD_mem_t type, hid_t dxpl_id,
1531
                                uint32_t count, H5S_t **mem_spaces, H5S_t **file_spaces, haddr_t offsets[],
1532
                                size_t element_sizes[], const void *bufs[])
1533
0
{
1534
0
    bool            extend_sizes = false;
1535
0
    bool            extend_bufs  = false;
1536
0
    uint32_t        i;
1537
0
    size_t          element_size = 0;
1538
0
    const void     *buf          = NULL;
1539
0
    bool            use_vector   = false;
1540
0
    haddr_t         addrs_local[H5FD_LOCAL_VECTOR_LEN];
1541
0
    haddr_t        *addrs = addrs_local;
1542
0
    size_t          sizes_local[H5FD_LOCAL_VECTOR_LEN];
1543
0
    size_t         *sizes = sizes_local;
1544
0
    const void     *vec_bufs_local[H5FD_LOCAL_VECTOR_LEN];
1545
0
    const void    **vec_bufs = vec_bufs_local;
1546
0
    hsize_t         file_off[H5FD_SEQ_LIST_LEN];
1547
0
    size_t          file_len[H5FD_SEQ_LIST_LEN];
1548
0
    hsize_t         mem_off[H5FD_SEQ_LIST_LEN];
1549
0
    size_t          mem_len[H5FD_SEQ_LIST_LEN];
1550
0
    size_t          file_seq_i;
1551
0
    size_t          mem_seq_i;
1552
0
    size_t          file_nseq;
1553
0
    size_t          mem_nseq;
1554
0
    size_t          io_len;
1555
0
    size_t          nelmts;
1556
0
    hssize_t        hss_nelmts;
1557
0
    size_t          seq_nelem;
1558
0
    H5S_sel_iter_t *file_iter      = NULL;
1559
0
    H5S_sel_iter_t *mem_iter       = NULL;
1560
0
    bool            file_iter_init = false;
1561
0
    bool            mem_iter_init  = false;
1562
0
    H5FD_mem_t      types[2]       = {type, H5FD_MEM_NOLIST};
1563
0
    size_t          vec_arr_nalloc = H5FD_LOCAL_VECTOR_LEN;
1564
0
    size_t          vec_arr_nused  = 0;
1565
0
    herr_t          ret_value      = SUCCEED;
1566
1567
0
    FUNC_ENTER_PACKAGE
1568
1569
    /* Sanity checks */
1570
0
    assert(file);
1571
0
    assert(file->cls);
1572
0
    assert((mem_spaces) || (count == 0));
1573
0
    assert((file_spaces) || (count == 0));
1574
0
    assert((offsets) || (count == 0));
1575
0
    assert((element_sizes) || (count == 0));
1576
0
    assert((bufs) || (count == 0));
1577
1578
    /* Check if we're using vector I/O */
1579
0
    use_vector = (file->cls->write_vector != NULL) && (!skip_vector_cb);
1580
1581
0
    if (count > 0) {
1582
        /* Verify that the first elements of the element_sizes and bufs arrays are
1583
         * valid. */
1584
0
        assert(element_sizes[0] != 0);
1585
0
        assert(bufs[0] != NULL);
1586
1587
        /* Allocate sequence lists for memory and file spaces */
1588
0
        if (NULL == (file_iter = H5FL_MALLOC(H5S_sel_iter_t)))
1589
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTALLOC, FAIL, "couldn't allocate file selection iterator");
1590
0
        if (NULL == (mem_iter = H5FL_MALLOC(H5S_sel_iter_t)))
1591
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTALLOC, FAIL, "couldn't allocate memory selection iterator");
1592
0
    }
1593
1594
    /* Loop over dataspaces */
1595
0
    for (i = 0; i < count; i++) {
1596
        /* we have already verified that element_sizes[0] != 0 and bufs[0]
1597
         * != NULL */
1598
0
        if (!extend_sizes) {
1599
0
            if (element_sizes[i] == 0) {
1600
0
                extend_sizes = true;
1601
0
                element_size = element_sizes[i - 1];
1602
0
            }
1603
0
            else
1604
0
                element_size = element_sizes[i];
1605
0
        }
1606
1607
0
        if (!extend_bufs) {
1608
0
            if (bufs[i] == NULL) {
1609
0
                extend_bufs = true;
1610
0
                buf         = bufs[i - 1];
1611
0
            }
1612
0
            else
1613
0
                buf = bufs[i];
1614
0
        }
1615
1616
        /* Initialize sequence lists for memory and file spaces */
1617
0
        if (H5S_select_iter_init(file_iter, file_spaces[i], element_size, 0) < 0)
1618
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTINIT, FAIL, "can't initialize sequence list for file space");
1619
0
        file_iter_init = true;
1620
0
        if (H5S_select_iter_init(mem_iter, mem_spaces[i], element_size, 0) < 0)
1621
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTINIT, FAIL, "can't initialize sequence list for memory space");
1622
0
        mem_iter_init = true;
1623
1624
        /* Get the number of elements in selection */
1625
0
        if ((hss_nelmts = (hssize_t)H5S_GET_SELECT_NPOINTS(file_spaces[i])) < 0)
1626
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTCOUNT, FAIL, "can't get number of elements selected");
1627
0
        H5_CHECKED_ASSIGN(nelmts, size_t, hss_nelmts, hssize_t);
1628
1629
#ifndef NDEBUG
1630
        /* Verify mem space has the same number of elements */
1631
        {
1632
            if ((hss_nelmts = (hssize_t)H5S_GET_SELECT_NPOINTS(mem_spaces[i])) < 0)
1633
                HGOTO_ERROR(H5E_VFL, H5E_CANTCOUNT, FAIL, "can't get number of elements selected");
1634
            assert((hssize_t)nelmts == hss_nelmts);
1635
        }
1636
#endif /* NDEBUG */
1637
1638
        /* Initialize values so sequence lists are retrieved on the first
1639
         * iteration */
1640
0
        file_seq_i = H5FD_SEQ_LIST_LEN;
1641
0
        mem_seq_i  = H5FD_SEQ_LIST_LEN;
1642
0
        file_nseq  = 0;
1643
0
        mem_nseq   = 0;
1644
1645
        /* Loop until all elements are processed */
1646
0
        while (file_seq_i < file_nseq || nelmts > 0) {
1647
            /* Fill/refill file sequence list if necessary */
1648
0
            if (file_seq_i == H5FD_SEQ_LIST_LEN) {
1649
0
                if (H5S_SELECT_ITER_GET_SEQ_LIST(file_iter, H5FD_SEQ_LIST_LEN, SIZE_MAX, &file_nseq,
1650
0
                                                 &seq_nelem, file_off, file_len) < 0)
1651
0
                    HGOTO_ERROR(H5E_INTERNAL, H5E_UNSUPPORTED, FAIL, "sequence length generation failed");
1652
0
                assert(file_nseq > 0);
1653
1654
0
                nelmts -= seq_nelem;
1655
0
                file_seq_i = 0;
1656
0
            }
1657
0
            assert(file_seq_i < file_nseq);
1658
1659
            /* Fill/refill memory sequence list if necessary */
1660
0
            if (mem_seq_i == H5FD_SEQ_LIST_LEN) {
1661
0
                if (H5S_SELECT_ITER_GET_SEQ_LIST(mem_iter, H5FD_SEQ_LIST_LEN, SIZE_MAX, &mem_nseq, &seq_nelem,
1662
0
                                                 mem_off, mem_len) < 0)
1663
0
                    HGOTO_ERROR(H5E_INTERNAL, H5E_UNSUPPORTED, FAIL, "sequence length generation failed");
1664
0
                assert(mem_nseq > 0);
1665
1666
0
                mem_seq_i = 0;
1667
0
            }
1668
0
            assert(mem_seq_i < mem_nseq);
1669
1670
            /* Calculate length of this IO */
1671
0
            io_len = MIN(file_len[file_seq_i], mem_len[mem_seq_i]);
1672
1673
            /* Check if we're using vector I/O */
1674
0
            if (use_vector) {
1675
                /* Check if we need to extend the arrays */
1676
0
                if (vec_arr_nused == vec_arr_nalloc) {
1677
                    /* Check if we're using the static arrays */
1678
0
                    if (addrs == addrs_local) {
1679
0
                        assert(sizes == sizes_local);
1680
0
                        assert(vec_bufs == vec_bufs_local);
1681
1682
                        /* Allocate dynamic arrays */
1683
0
                        if (NULL == (addrs = H5MM_malloc(sizeof(addrs_local) * 2)))
1684
0
                            HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL,
1685
0
                                        "memory allocation failed for address list");
1686
0
                        if (NULL == (sizes = H5MM_malloc(sizeof(sizes_local) * 2)))
1687
0
                            HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL,
1688
0
                                        "memory allocation failed for size list");
1689
0
                        if (NULL == (vec_bufs = H5MM_malloc(sizeof(vec_bufs_local) * 2)))
1690
0
                            HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL,
1691
0
                                        "memory allocation failed for buffer list");
1692
1693
                        /* Copy the existing data */
1694
0
                        (void)H5MM_memcpy(addrs, addrs_local, sizeof(addrs_local));
1695
0
                        (void)H5MM_memcpy(sizes, sizes_local, sizeof(sizes_local));
1696
0
                        (void)H5MM_memcpy(vec_bufs, vec_bufs_local, sizeof(vec_bufs_local));
1697
0
                    }
1698
0
                    else {
1699
0
                        void *tmp_ptr;
1700
1701
                        /* Reallocate arrays */
1702
0
                        if (NULL == (tmp_ptr = H5MM_realloc(addrs, vec_arr_nalloc * sizeof(*addrs) * 2)))
1703
0
                            HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL,
1704
0
                                        "memory reallocation failed for address list");
1705
0
                        addrs = tmp_ptr;
1706
0
                        if (NULL == (tmp_ptr = H5MM_realloc(sizes, vec_arr_nalloc * sizeof(*sizes) * 2)))
1707
0
                            HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL,
1708
0
                                        "memory reallocation failed for size list");
1709
0
                        sizes = tmp_ptr;
1710
0
                        if (NULL ==
1711
0
                            (tmp_ptr = H5MM_realloc(vec_bufs, vec_arr_nalloc * sizeof(*vec_bufs) * 2)))
1712
0
                            HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL,
1713
0
                                        "memory reallocation failed for buffer list");
1714
0
                        vec_bufs = tmp_ptr;
1715
0
                    }
1716
1717
                    /* Record that we've doubled the array sizes */
1718
0
                    vec_arr_nalloc *= 2;
1719
0
                }
1720
1721
                /* Add this segment to vector write list */
1722
0
                addrs[vec_arr_nused]    = offsets[i] + file_off[file_seq_i];
1723
0
                sizes[vec_arr_nused]    = io_len;
1724
0
                vec_bufs[vec_arr_nused] = (const void *)((const uint8_t *)buf + mem_off[mem_seq_i]);
1725
0
                vec_arr_nused++;
1726
0
            }
1727
0
            else {
1728
                /* Prepare & restore library for user callback */
1729
0
                H5_BEFORE_USER_CB(FAIL)
1730
0
                    {
1731
                        /* Issue scalar write call */
1732
0
                        ret_value =
1733
0
                            (file->cls->write)(file, type, dxpl_id, offsets[i] + file_off[file_seq_i], io_len,
1734
0
                                               (const void *)((const uint8_t *)buf + mem_off[mem_seq_i]));
1735
0
                    }
1736
0
                H5_AFTER_USER_CB(FAIL)
1737
0
                if (ret_value < 0)
1738
0
                    HGOTO_ERROR(H5E_VFL, H5E_WRITEERROR, FAIL, "driver write request failed");
1739
0
            }
1740
1741
            /* Update file sequence */
1742
0
            if (io_len == file_len[file_seq_i])
1743
0
                file_seq_i++;
1744
0
            else {
1745
0
                file_off[file_seq_i] += io_len;
1746
0
                file_len[file_seq_i] -= io_len;
1747
0
            }
1748
1749
            /* Update memory sequence */
1750
0
            if (io_len == mem_len[mem_seq_i])
1751
0
                mem_seq_i++;
1752
0
            else {
1753
0
                mem_off[mem_seq_i] += io_len;
1754
0
                mem_len[mem_seq_i] -= io_len;
1755
0
            }
1756
0
        }
1757
1758
        /* Make sure both memory and file sequences terminated at the same time */
1759
0
        if (mem_seq_i < mem_nseq)
1760
0
            HGOTO_ERROR(H5E_INTERNAL, H5E_BADVALUE, FAIL,
1761
0
                        "file selection terminated before memory selection");
1762
1763
        /* Terminate iterators */
1764
0
        if (H5S_SELECT_ITER_RELEASE(file_iter) < 0)
1765
0
            HGOTO_ERROR(H5E_INTERNAL, H5E_CANTFREE, FAIL, "can't release file selection iterator");
1766
0
        file_iter_init = false;
1767
0
        if (H5S_SELECT_ITER_RELEASE(mem_iter) < 0)
1768
0
            HGOTO_ERROR(H5E_INTERNAL, H5E_CANTFREE, FAIL, "can't release memory selection iterator");
1769
0
        mem_iter_init = false;
1770
0
    }
1771
1772
    /* Issue vector write call if appropriate */
1773
0
    if (use_vector) {
1774
0
        uint32_t actual_selection_io_mode;
1775
1776
0
        H5_CHECK_OVERFLOW(vec_arr_nused, size_t, uint32_t);
1777
        /* Prepare & restore library for user callback */
1778
0
        H5_BEFORE_USER_CB(FAIL)
1779
0
            {
1780
0
                ret_value = (file->cls->write_vector)(file, dxpl_id, (uint32_t)vec_arr_nused, types, addrs,
1781
0
                                                      sizes, vec_bufs);
1782
0
            }
1783
0
        H5_AFTER_USER_CB(FAIL)
1784
0
        if (ret_value < 0)
1785
0
            HGOTO_ERROR(H5E_VFL, H5E_WRITEERROR, FAIL, "driver write vector request failed");
1786
1787
        /* Set actual selection I/O, if this is a raw data operation */
1788
0
        if (type == H5FD_MEM_DRAW && count > 0) {
1789
0
            H5CX_get_actual_selection_io_mode(&actual_selection_io_mode);
1790
0
            actual_selection_io_mode |= H5D_VECTOR_IO;
1791
0
            H5CX_set_actual_selection_io_mode(actual_selection_io_mode);
1792
0
        }
1793
0
    }
1794
0
    else if (count > 0) {
1795
0
        uint32_t no_selection_io_cause;
1796
0
        uint32_t actual_selection_io_mode;
1797
1798
        /* Add H5D_SEL_IO_NO_VECTOR_OR_SELECTION_IO_CB to no selection I/O cause */
1799
0
        H5CX_get_no_selection_io_cause(&no_selection_io_cause);
1800
0
        no_selection_io_cause |= H5D_SEL_IO_NO_VECTOR_OR_SELECTION_IO_CB;
1801
0
        H5CX_set_no_selection_io_cause(no_selection_io_cause);
1802
1803
        /* Set actual selection I/O, if this is a raw data operation */
1804
0
        if (type == H5FD_MEM_DRAW) {
1805
0
            H5CX_get_actual_selection_io_mode(&actual_selection_io_mode);
1806
0
            actual_selection_io_mode |= H5D_SCALAR_IO;
1807
0
            H5CX_set_actual_selection_io_mode(actual_selection_io_mode);
1808
0
        }
1809
0
    }
1810
1811
0
done:
1812
    /* Terminate and free iterators */
1813
0
    if (file_iter) {
1814
0
        if (file_iter_init && H5S_SELECT_ITER_RELEASE(file_iter) < 0)
1815
0
            HGOTO_ERROR(H5E_INTERNAL, H5E_CANTFREE, FAIL, "can't release file selection iterator");
1816
0
        file_iter = H5FL_FREE(H5S_sel_iter_t, file_iter);
1817
0
    }
1818
0
    if (mem_iter) {
1819
0
        if (mem_iter_init && H5S_SELECT_ITER_RELEASE(mem_iter) < 0)
1820
0
            HGOTO_ERROR(H5E_INTERNAL, H5E_CANTFREE, FAIL, "can't release memory selection iterator");
1821
0
        mem_iter = H5FL_FREE(H5S_sel_iter_t, mem_iter);
1822
0
    }
1823
1824
    /* Cleanup vector arrays */
1825
0
    if (use_vector) {
1826
0
        if (addrs != addrs_local)
1827
0
            addrs = H5MM_xfree(addrs);
1828
0
        if (sizes != sizes_local)
1829
0
            sizes = H5MM_xfree(sizes);
1830
0
        if (vec_bufs != vec_bufs_local)
1831
0
            vec_bufs = H5MM_xfree(vec_bufs);
1832
0
    }
1833
1834
    /* Make sure we cleaned up */
1835
0
    assert(!addrs || addrs == addrs_local);
1836
0
    assert(!sizes || sizes == sizes_local);
1837
0
    assert(!vec_bufs || vec_bufs == vec_bufs_local);
1838
1839
0
    FUNC_LEAVE_NOAPI(ret_value)
1840
0
} /* end H5FD__write_selection_translate() */
1841
1842
/*-------------------------------------------------------------------------
1843
 * Function:    H5FD_write_selection
1844
 *
1845
 * Purpose:     Private version of H5FDwrite_selection()
1846
 *
1847
 *              Perform count writes to the specified file at the
1848
 *              locations selected in the dataspaces in the file_spaces
1849
 *              array, with each of those dataspaces starting at the file
1850
 *              address specified by the corresponding element of the
1851
 *              offsets array, and with the size of each element in the
1852
 *              dataspace specified by the corresponding element of the
1853
 *              element_sizes array.  The memory type provided by type is
1854
 *              the same for all selections.  Data write is from
1855
 *              the locations selected in the dataspaces in the
1856
 *              mem_spaces array, within the buffers provided in the
1857
 *              corresponding elements of the bufs array.
1858
 *
1859
 *              If i > 0 and element_sizes[i] == 0, presume
1860
 *              element_sizes[n] = element_sizes[i-1] for all n >= i and
1861
 *              < count.
1862
 *
1863
 *              If the underlying VFD supports selection writes, pass the
1864
 *              call through directly.
1865
 *
1866
 *              If it doesn't, convert the selection write into a sequence
1867
 *              of vector or scalar writes.
1868
 *
1869
 * Return:      Success:    SUCCEED
1870
 *                          All writes have completed successfully.
1871
 *
1872
 *              Failure:    FAIL
1873
 *                          One or more writes failed.
1874
 *
1875
 *-------------------------------------------------------------------------
1876
 */
1877
herr_t
1878
H5FD_write_selection(H5FD_t *file, H5FD_mem_t type, uint32_t count, H5S_t **mem_spaces, H5S_t **file_spaces,
1879
                     haddr_t offsets[], size_t element_sizes[], const void *bufs[])
1880
0
{
1881
0
    bool     offsets_cooked = false;
1882
0
    hid_t    mem_space_ids_local[H5FD_LOCAL_SEL_ARR_LEN];
1883
0
    hid_t   *mem_space_ids = mem_space_ids_local;
1884
0
    hid_t    file_space_ids_local[H5FD_LOCAL_SEL_ARR_LEN];
1885
0
    hid_t   *file_space_ids = file_space_ids_local;
1886
0
    haddr_t  eoa;
1887
0
    uint32_t num_spaces = 0;
1888
0
    hid_t    dxpl_id    = H5I_INVALID_HID; /* DXPL for operation */
1889
0
    uint32_t i;
1890
0
    herr_t   ret_value = SUCCEED; /* Return value */
1891
1892
0
    FUNC_ENTER_NOAPI(FAIL)
1893
1894
    /* Sanity checks */
1895
0
    assert(file);
1896
0
    assert(file->cls);
1897
0
    assert((mem_spaces) || (count == 0));
1898
0
    assert((file_spaces) || (count == 0));
1899
0
    assert((offsets) || (count == 0));
1900
0
    assert((element_sizes) || (count == 0));
1901
0
    assert((bufs) || (count == 0));
1902
1903
    /* Verify that the first elements of the element_sizes and bufs arrays are
1904
     * valid. */
1905
0
    assert((count == 0) || (element_sizes[0] != 0));
1906
0
    assert((count == 0) || (bufs[0] != NULL));
1907
1908
    /* Get proper DXPL for I/O */
1909
0
    dxpl_id = H5CX_get_dxpl();
1910
1911
0
#ifndef H5_HAVE_PARALLEL
1912
    /* The no-op case
1913
     *
1914
     * Do not return early for Parallel mode since the I/O could be a
1915
     * collective transfer.
1916
     */
1917
0
    if (0 == count)
1918
0
        HGOTO_DONE(SUCCEED);
1919
0
#endif /* H5_HAVE_PARALLEL */
1920
1921
0
    if (file->base_addr > 0) {
1922
        /* apply the base_addr offset to the offsets array.  Must undo before
1923
         * we return.
1924
         */
1925
0
        for (i = 0; i < count; i++)
1926
0
            offsets[i] += file->base_addr;
1927
0
        offsets_cooked = true;
1928
0
    }
1929
1930
    /* For now at least, only check that the offset is not past the eoa, since
1931
     * looking into the highest offset in the selection (different from the
1932
     * bounds) is potentially expensive.
1933
     */
1934
    /* Prepare & restore library for user callback */
1935
0
    H5_BEFORE_USER_CB(FAIL)
1936
0
        {
1937
0
            eoa = (file->cls->get_eoa)(file, type);
1938
0
        }
1939
0
    H5_AFTER_USER_CB(FAIL)
1940
0
    if (!H5_addr_defined(eoa))
1941
0
        HGOTO_ERROR(H5E_VFL, H5E_CANTINIT, FAIL, "driver get_eoa request failed");
1942
1943
0
    for (i = 0; i < count; i++)
1944
0
        if ((offsets[i]) > eoa)
1945
0
            HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "addr overflow, offsets[%d] = %llu, eoa = %llu", (int)i,
1946
0
                        (unsigned long long)(offsets[i]), (unsigned long long)eoa);
1947
1948
    /* if the underlying VFD supports selection write, make the call */
1949
0
    if (file->cls->write_selection) {
1950
0
        uint32_t actual_selection_io_mode;
1951
1952
        /* Allocate array of space IDs if necessary, otherwise use local
1953
         * buffers */
1954
0
        if (count > sizeof(mem_space_ids_local) / sizeof(mem_space_ids_local[0])) {
1955
0
            if (NULL == (mem_space_ids = H5MM_malloc(count * sizeof(hid_t))))
1956
0
                HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL, "memory allocation failed for dataspace list");
1957
0
            if (NULL == (file_space_ids = H5MM_malloc(count * sizeof(hid_t))))
1958
0
                HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL, "memory allocation failed for dataspace list");
1959
0
        }
1960
1961
        /* Create IDs for all dataspaces */
1962
0
        for (; num_spaces < count; num_spaces++) {
1963
0
            if ((mem_space_ids[num_spaces] = H5I_register(H5I_DATASPACE, mem_spaces[num_spaces], true)) < 0)
1964
0
                HGOTO_ERROR(H5E_VFL, H5E_CANTREGISTER, FAIL, "unable to register dataspace ID");
1965
1966
0
            if ((file_space_ids[num_spaces] = H5I_register(H5I_DATASPACE, file_spaces[num_spaces], true)) <
1967
0
                0) {
1968
0
                if (NULL == H5I_remove(mem_space_ids[num_spaces]))
1969
0
                    HDONE_ERROR(H5E_VFL, H5E_CANTREMOVE, FAIL, "problem removing id");
1970
0
                HGOTO_ERROR(H5E_VFL, H5E_CANTREGISTER, FAIL, "unable to register dataspace ID");
1971
0
            }
1972
0
        }
1973
1974
        /* Prepare & restore library for user callback */
1975
0
        H5_BEFORE_USER_CB(FAIL)
1976
0
            {
1977
0
                ret_value = (file->cls->write_selection)(file, type, dxpl_id, count, mem_space_ids,
1978
0
                                                         file_space_ids, offsets, element_sizes, bufs);
1979
0
            }
1980
0
        H5_AFTER_USER_CB(FAIL)
1981
0
        if (ret_value < 0)
1982
0
            HGOTO_ERROR(H5E_VFL, H5E_WRITEERROR, FAIL, "driver write selection request failed");
1983
1984
        /* Set actual selection I/O, if this is a raw data operation */
1985
0
        if (type == H5FD_MEM_DRAW) {
1986
0
            H5CX_get_actual_selection_io_mode(&actual_selection_io_mode);
1987
0
            actual_selection_io_mode |= H5D_SELECTION_IO;
1988
0
            H5CX_set_actual_selection_io_mode(actual_selection_io_mode);
1989
0
        }
1990
0
    }
1991
0
    else
1992
        /* Otherwise, implement the selection write as a sequence of regular
1993
         * or vector write calls.
1994
         */
1995
1996
0
        if (H5FD__write_selection_translate(SKIP_NO_CB, file, type, dxpl_id, count, mem_spaces, file_spaces,
1997
0
                                            offsets, element_sizes, bufs) < 0)
1998
0
            HGOTO_ERROR(H5E_VFL, H5E_WRITEERROR, FAIL, "translation to vector or scalar write failed");
1999
2000
0
done:
2001
    /* undo the base addr offset to the offsets array if necessary */
2002
0
    if (offsets_cooked) {
2003
0
        assert(file->base_addr > 0);
2004
0
        for (i = 0; i < count; i++)
2005
0
            offsets[i] -= file->base_addr;
2006
0
    }
2007
2008
    /* Cleanup dataspace arrays.  Use H5I_remove() so we only close the IDs and
2009
     * not the underlying dataspaces, which were not created by this function.
2010
     */
2011
0
    for (i = 0; i < num_spaces; i++) {
2012
0
        if (NULL == H5I_remove(mem_space_ids[i]))
2013
0
            HDONE_ERROR(H5E_VFL, H5E_CANTREMOVE, FAIL, "problem removing id");
2014
0
        if (NULL == H5I_remove(file_space_ids[i]))
2015
0
            HDONE_ERROR(H5E_VFL, H5E_CANTREMOVE, FAIL, "problem removing id");
2016
0
    }
2017
0
    if (mem_space_ids != mem_space_ids_local)
2018
0
        mem_space_ids = H5MM_xfree(mem_space_ids);
2019
0
    if (file_space_ids != file_space_ids_local)
2020
0
        file_space_ids = H5MM_xfree(file_space_ids);
2021
2022
0
    FUNC_LEAVE_NOAPI(ret_value)
2023
0
} /* end H5FD_write_selection() */
2024
2025
/*-------------------------------------------------------------------------
2026
 * Function:    H5FD_write_selection_id
2027
 *
2028
 * Purpose:     Like H5FD_write_selection(), but takes hid_t arrays
2029
 *              instead of H5S_t * arrays for the dataspaces.
2030
 *
2031
 *              Depending on the parameter skip_cb which is translated into
2032
 *              skip_selection_cb and skip_vector_cb:
2033
 *
2034
 *              --If the underlying VFD supports selection writes and !skip_selection_cb,
2035
 *                pass the call through directly.
2036
 *
2037
 *              --If it doesn't, convert the selection writes into a sequence of vector or
2038
 *                scalar reads depending on skip_vector_cb.
2039
 *
2040
 * Return:      Success:    SUCCEED
2041
 *                          All writes have completed successfully.
2042
 *
2043
 *              Failure:    FAIL
2044
 *                          One or more writes failed.
2045
 *
2046
 *-------------------------------------------------------------------------
2047
 */
2048
herr_t
2049
H5FD_write_selection_id(uint32_t skip_cb, H5FD_t *file, H5FD_mem_t type, uint32_t count,
2050
                        hid_t mem_space_ids[], hid_t file_space_ids[], haddr_t offsets[],
2051
                        size_t element_sizes[], const void *bufs[])
2052
0
{
2053
0
    bool     offsets_cooked = false;
2054
0
    H5S_t   *mem_spaces_local[H5FD_LOCAL_SEL_ARR_LEN];
2055
0
    H5S_t  **mem_spaces = mem_spaces_local;
2056
0
    H5S_t   *file_spaces_local[H5FD_LOCAL_SEL_ARR_LEN];
2057
0
    H5S_t  **file_spaces = file_spaces_local;
2058
0
    haddr_t  eoa;
2059
0
    hid_t    dxpl_id = H5I_INVALID_HID; /* DXPL for operation */
2060
0
    uint32_t i;
2061
0
    uint32_t skip_selection_cb;
2062
0
    uint32_t skip_vector_cb;
2063
0
    herr_t   ret_value = SUCCEED; /* Return value */
2064
2065
0
    FUNC_ENTER_NOAPI(FAIL)
2066
2067
    /* Sanity checks */
2068
0
    assert(file);
2069
0
    assert(file->cls);
2070
0
    assert((mem_space_ids) || (count == 0));
2071
0
    assert((file_space_ids) || (count == 0));
2072
0
    assert((offsets) || (count == 0));
2073
0
    assert((element_sizes) || (count == 0));
2074
0
    assert((bufs) || (count == 0));
2075
2076
    /* Verify that the first elements of the element_sizes and bufs arrays are
2077
     * valid. */
2078
0
    assert((count == 0) || (element_sizes[0] != 0));
2079
0
    assert((count == 0) || (bufs[0] != NULL));
2080
2081
    /* Get proper DXPL for I/O */
2082
0
    dxpl_id = H5CX_get_dxpl();
2083
2084
0
#ifndef H5_HAVE_PARALLEL
2085
    /* The no-op case
2086
     *
2087
     * Do not return early for Parallel mode since the I/O could be a
2088
     * collective transfer.
2089
     */
2090
0
    if (0 == count)
2091
0
        HGOTO_DONE(SUCCEED);
2092
0
#endif /* H5_HAVE_PARALLEL */
2093
2094
0
    skip_selection_cb = skip_cb & SKIP_SELECTION_CB;
2095
0
    skip_vector_cb    = skip_cb & SKIP_VECTOR_CB;
2096
2097
0
    if (file->base_addr > 0) {
2098
        /* apply the base_addr offset to the offsets array.  Must undo before
2099
         * we return.
2100
         */
2101
0
        for (i = 0; i < count; i++)
2102
0
            offsets[i] += file->base_addr;
2103
0
        offsets_cooked = true;
2104
0
    }
2105
2106
    /* For now at least, only check that the offset is not past the eoa, since
2107
     * looking into the highest offset in the selection (different from the
2108
     * bounds) is potentially expensive.
2109
     */
2110
    /* Prepare & restore library for user callback */
2111
0
    H5_BEFORE_USER_CB(FAIL)
2112
0
        {
2113
0
            eoa = (file->cls->get_eoa)(file, type);
2114
0
        }
2115
0
    H5_AFTER_USER_CB(FAIL)
2116
0
    if (!H5_addr_defined(eoa))
2117
0
        HGOTO_ERROR(H5E_VFL, H5E_CANTINIT, FAIL, "driver get_eoa request failed");
2118
2119
0
    for (i = 0; i < count; i++)
2120
0
        if ((offsets[i]) > eoa)
2121
0
            HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "addr overflow, offsets[%d] = %llu, eoa = %llu", (int)i,
2122
0
                        (unsigned long long)(offsets[i]), (unsigned long long)eoa);
2123
2124
    /* if the underlying VFD supports selection write, make the call */
2125
0
    if (!skip_selection_cb && file->cls->write_selection) {
2126
0
        uint32_t actual_selection_io_mode;
2127
2128
        /* Prepare & restore library for user callback */
2129
0
        H5_BEFORE_USER_CB(FAIL)
2130
0
            {
2131
0
                ret_value = (file->cls->write_selection)(file, type, dxpl_id, count, mem_space_ids,
2132
0
                                                         file_space_ids, offsets, element_sizes, bufs);
2133
0
            }
2134
0
        H5_AFTER_USER_CB(FAIL)
2135
0
        if (ret_value < 0)
2136
0
            HGOTO_ERROR(H5E_VFL, H5E_WRITEERROR, FAIL, "driver write selection request failed");
2137
2138
        /* Set actual selection I/O, if this is a raw data operation */
2139
0
        if (type == H5FD_MEM_DRAW) {
2140
0
            H5CX_get_actual_selection_io_mode(&actual_selection_io_mode);
2141
0
            actual_selection_io_mode |= H5D_SELECTION_IO;
2142
0
            H5CX_set_actual_selection_io_mode(actual_selection_io_mode);
2143
0
        }
2144
0
    }
2145
0
    else {
2146
        /* Otherwise, implement the selection write as a sequence of regular
2147
         * or vector write calls.
2148
         */
2149
2150
        /* Allocate arrays of space objects if necessary, otherwise use local
2151
         * buffers */
2152
0
        if (count > sizeof(mem_spaces_local) / sizeof(mem_spaces_local[0])) {
2153
0
            if (NULL == (mem_spaces = H5MM_malloc(count * sizeof(H5S_t *))))
2154
0
                HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL, "memory allocation failed for dataspace list");
2155
0
            if (NULL == (file_spaces = H5MM_malloc(count * sizeof(H5S_t *))))
2156
0
                HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL, "memory allocation failed for dataspace list");
2157
0
        }
2158
2159
        /* Get object pointers for all dataspaces */
2160
0
        for (i = 0; i < count; i++) {
2161
0
            if (NULL == (mem_spaces[i] = (H5S_t *)H5I_object_verify(mem_space_ids[i], H5I_DATASPACE)))
2162
0
                HGOTO_ERROR(H5E_VFL, H5E_BADTYPE, H5I_INVALID_HID, "can't retrieve memory dataspace from ID");
2163
0
            if (NULL == (file_spaces[i] = (H5S_t *)H5I_object_verify(file_space_ids[i], H5I_DATASPACE)))
2164
0
                HGOTO_ERROR(H5E_VFL, H5E_BADTYPE, H5I_INVALID_HID, "can't retrieve file dataspace from ID");
2165
0
        }
2166
2167
        /* Translate to vector or scalar I/O */
2168
0
        if (H5FD__write_selection_translate(skip_vector_cb, file, type, dxpl_id, count, mem_spaces,
2169
0
                                            file_spaces, offsets, element_sizes, bufs) < 0)
2170
0
            HGOTO_ERROR(H5E_VFL, H5E_WRITEERROR, FAIL, "translation to vector or scalar write failed");
2171
0
    }
2172
2173
0
done:
2174
    /* undo the base addr offset to the offsets array if necessary */
2175
0
    if (offsets_cooked) {
2176
0
        assert(file->base_addr > 0);
2177
0
        for (i = 0; i < count; i++)
2178
0
            offsets[i] -= file->base_addr;
2179
0
    }
2180
2181
    /* Cleanup dataspace arrays */
2182
0
    if (mem_spaces != mem_spaces_local)
2183
0
        mem_spaces = H5MM_xfree(mem_spaces);
2184
0
    if (file_spaces != file_spaces_local)
2185
0
        file_spaces = H5MM_xfree(file_spaces);
2186
2187
0
    FUNC_LEAVE_NOAPI(ret_value)
2188
0
} /* end H5FD_write_selection_id() */
2189
2190
/*-------------------------------------------------------------------------
2191
 * Function:    H5FD_read_vector_from_selection
2192
 *
2193
 * Purpose:     Internal routine for H5FDread_vector_from_selection()
2194
 *
2195
 *              It will translate the selection read to a vector read call
2196
 *              if vector reads are supported, or a series of scalar read
2197
 *              calls otherwise.
2198
 *
2199
 * Return:      Success:    SUCCEED
2200
 *                          All writes have completed successfully.
2201
 *
2202
 *              Failure:    FAIL
2203
 *                          One or more writes failed.
2204
 *
2205
 *-------------------------------------------------------------------------
2206
 */
2207
herr_t
2208
H5FD_read_vector_from_selection(H5FD_t *file, H5FD_mem_t type, uint32_t count, hid_t mem_space_ids[],
2209
                                hid_t file_space_ids[], haddr_t offsets[], size_t element_sizes[],
2210
                                void *bufs[])
2211
0
{
2212
0
    herr_t ret_value = SUCCEED; /* Return value */
2213
2214
0
    FUNC_ENTER_NOAPI(FAIL)
2215
2216
    /* Sanity checks */
2217
0
    assert(file);
2218
0
    assert(file->cls);
2219
0
    assert((mem_space_ids) || (count == 0));
2220
0
    assert((file_space_ids) || (count == 0));
2221
0
    assert((offsets) || (count == 0));
2222
0
    assert((element_sizes) || (count == 0));
2223
0
    assert((bufs) || (count == 0));
2224
2225
    /* Verify that the first elements of the element_sizes and bufs arrays are
2226
     * valid. */
2227
0
    assert((count == 0) || (element_sizes[0] != 0));
2228
0
    assert((count == 0) || (bufs[0] != NULL));
2229
2230
    /* Call private function */
2231
    /* (Note compensating for base address addition in internal routine) */
2232
0
    if (H5FD_read_selection_id(SKIP_SELECTION_CB, file, type, count, mem_space_ids, file_space_ids, offsets,
2233
0
                               element_sizes, bufs) < 0)
2234
0
        HGOTO_ERROR(H5E_VFL, H5E_READERROR, FAIL, "file selection read request failed");
2235
2236
0
done:
2237
0
    FUNC_LEAVE_NOAPI(ret_value)
2238
2239
0
} /* end H5FD_read_vector_from_selection() */
2240
2241
/*-------------------------------------------------------------------------
2242
 * Function:    H5FD_write_vector_from_selection
2243
 *
2244
 * Purpose:     Internal routine for H5FDwrite_vector_from_selection()
2245
 *
2246
 *              It will translate the selection write to a vector write call
2247
 *              if vector writes are supported, or a series of scalar write
2248
 *              calls otherwise.
2249
 *
2250
 * Return:      Success:    SUCCEED
2251
 *                          All writes have completed successfully.
2252
 *
2253
 *              Failure:    FAIL
2254
 *                          One or more writes failed.
2255
 *
2256
 *-------------------------------------------------------------------------
2257
 */
2258
herr_t
2259
H5FD_write_vector_from_selection(H5FD_t *file, H5FD_mem_t type, uint32_t count, hid_t mem_space_ids[],
2260
                                 hid_t file_space_ids[], haddr_t offsets[], size_t element_sizes[],
2261
                                 const void *bufs[])
2262
0
{
2263
0
    herr_t ret_value = SUCCEED; /* Return value */
2264
2265
0
    FUNC_ENTER_NOAPI(FAIL)
2266
2267
    /* Sanity checks */
2268
0
    assert(file);
2269
0
    assert(file->cls);
2270
0
    assert((mem_space_ids) || (count == 0));
2271
0
    assert((file_space_ids) || (count == 0));
2272
0
    assert((offsets) || (count == 0));
2273
0
    assert((element_sizes) || (count == 0));
2274
0
    assert((bufs) || (count == 0));
2275
2276
    /* Verify that the first elements of the element_sizes and bufs arrays are
2277
     * valid. */
2278
0
    assert((count == 0) || (element_sizes[0] != 0));
2279
0
    assert((count == 0) || (bufs[0] != NULL));
2280
2281
    /* Call private function */
2282
    /* (Note compensating for base address addition in internal routine) */
2283
0
    if (H5FD_write_selection_id(SKIP_SELECTION_CB, file, type, count, mem_space_ids, file_space_ids, offsets,
2284
0
                                element_sizes, bufs) < 0)
2285
0
        HGOTO_ERROR(H5E_VFL, H5E_WRITEERROR, FAIL, "file selection write request failed");
2286
2287
0
done:
2288
0
    FUNC_LEAVE_NOAPI(ret_value)
2289
2290
0
} /* end H5FD_write_vector_from_selection() */
2291
2292
/*-------------------------------------------------------------------------
2293
 * Function:    H5FD_read_from_selection
2294
 *
2295
 * Purpose:     Internal routine for H5FDread_from_selection()
2296
 *
2297
 *              It will translate the selection read to a series of
2298
 *              scalar read calls.
2299
 *
2300
 * Return:      Success:    SUCCEED
2301
 *                          All writes have completed successfully.
2302
 *
2303
 *              Failure:    FAIL
2304
 *                          One or more writes failed.
2305
 *
2306
 *-------------------------------------------------------------------------
2307
 */
2308
herr_t
2309
H5FD_read_from_selection(H5FD_t *file, H5FD_mem_t type, uint32_t count, hid_t mem_space_ids[],
2310
                         hid_t file_space_ids[], haddr_t offsets[], size_t element_sizes[], void *bufs[])
2311
0
{
2312
0
    herr_t ret_value = SUCCEED; /* Return value */
2313
2314
0
    FUNC_ENTER_NOAPI(FAIL)
2315
2316
    /* Sanity checks */
2317
0
    assert(file);
2318
0
    assert(file->cls);
2319
0
    assert((mem_space_ids) || (count == 0));
2320
0
    assert((file_space_ids) || (count == 0));
2321
0
    assert((offsets) || (count == 0));
2322
0
    assert((element_sizes) || (count == 0));
2323
0
    assert((bufs) || (count == 0));
2324
2325
    /* Verify that the first elements of the element_sizes and bufs arrays are
2326
     * valid. */
2327
0
    assert((count == 0) || (element_sizes[0] != 0));
2328
0
    assert((count == 0) || (bufs[0] != NULL));
2329
2330
    /* Call private function */
2331
    /* (Note compensating for base address addition in internal routine) */
2332
0
    if (H5FD_read_selection_id(SKIP_SELECTION_CB | SKIP_VECTOR_CB, file, type, count, mem_space_ids,
2333
0
                               file_space_ids, offsets, element_sizes, bufs) < 0)
2334
0
        HGOTO_ERROR(H5E_VFL, H5E_READERROR, FAIL, "file selection read request failed");
2335
2336
0
done:
2337
2338
0
    FUNC_LEAVE_NOAPI(ret_value)
2339
2340
0
} /* end H5FD_read_from_selection() */
2341
2342
/*-------------------------------------------------------------------------
2343
 * Function:    H5FD_write_from_selection
2344
 *
2345
 * Purpose:     Internal routine for H5FDwrite_from_selection()
2346
 *
2347
 *               It will translate the selection write to a series of
2348
 *               scalar write calls.
2349
 *
2350
 * Return:      Success:    SUCCEED
2351
 *                          All writes have completed successfully.
2352
 *
2353
 *              Failure:    FAIL
2354
 *                          One or more writes failed.
2355
 *
2356
 *-------------------------------------------------------------------------
2357
 */
2358
herr_t
2359
H5FD_write_from_selection(H5FD_t *file, H5FD_mem_t type, uint32_t count, hid_t mem_space_ids[],
2360
                          hid_t file_space_ids[], haddr_t offsets[], size_t element_sizes[],
2361
                          const void *bufs[])
2362
0
{
2363
0
    herr_t ret_value = SUCCEED; /* Return value */
2364
2365
0
    FUNC_ENTER_NOAPI(FAIL)
2366
2367
    /* Sanity checks */
2368
0
    assert(file);
2369
0
    assert(file->cls);
2370
0
    assert((mem_space_ids) || (count == 0));
2371
0
    assert((file_space_ids) || (count == 0));
2372
0
    assert((offsets) || (count == 0));
2373
0
    assert((element_sizes) || (count == 0));
2374
0
    assert((bufs) || (count == 0));
2375
2376
    /* Verify that the first elements of the element_sizes and bufs arrays are
2377
     * valid. */
2378
0
    assert((count == 0) || (element_sizes[0] != 0));
2379
0
    assert((count == 0) || (bufs[0] != NULL));
2380
2381
    /* Call private function */
2382
    /* (Note compensating for base address addition in internal routine) */
2383
0
    if (H5FD_write_selection_id(SKIP_SELECTION_CB | SKIP_VECTOR_CB, file, type, count, mem_space_ids,
2384
0
                                file_space_ids, offsets, element_sizes, bufs) < 0)
2385
0
        HGOTO_ERROR(H5E_VFL, H5E_WRITEERROR, FAIL, "file selection write request failed");
2386
2387
0
done:
2388
2389
0
    FUNC_LEAVE_NOAPI(ret_value)
2390
2391
0
} /* end H5FD_write_from_selection() */
2392
2393
/*-------------------------------------------------------------------------
2394
 * Function:    H5FD_set_eoa
2395
 *
2396
 * Purpose:     Private version of H5FDset_eoa()
2397
 *
2398
 *              This function expects the EOA is a RELATIVE address, i.e.
2399
 *              relative to the base address.  This is NOT the same as the
2400
 *              EOA stored in the superblock, which is an absolute
2401
 *              address.  Object addresses are relative.
2402
 *
2403
 * Return:      SUCCEED/FAIL
2404
 *
2405
 *-------------------------------------------------------------------------
2406
 */
2407
herr_t
2408
H5FD_set_eoa(H5FD_t *file, H5FD_mem_t type, haddr_t addr)
2409
1.33k
{
2410
1.33k
    herr_t ret_value = SUCCEED; /* Return value */
2411
2412
1.33k
    FUNC_ENTER_NOAPI(FAIL)
2413
2414
1.33k
    assert(file && file->cls);
2415
1.33k
    assert(H5_addr_defined(addr) && addr <= file->maxaddr);
2416
2417
    /* Prepare & restore library for user callback */
2418
1.33k
    H5_BEFORE_USER_CB(FAIL)
2419
1.33k
        {
2420
            /* Dispatch to driver, convert to absolute address */
2421
1.33k
            ret_value = (file->cls->set_eoa)(file, type, addr + file->base_addr);
2422
1.33k
        }
2423
1.33k
    H5_AFTER_USER_CB(FAIL)
2424
1.33k
    if (ret_value < 0)
2425
0
        HGOTO_ERROR(H5E_VFL, H5E_CANTINIT, FAIL, "driver set_eoa request failed");
2426
2427
1.33k
done:
2428
1.33k
    FUNC_LEAVE_NOAPI(ret_value)
2429
1.33k
} /* end H5FD_set_eoa() */
2430
2431
/*-------------------------------------------------------------------------
2432
 * Function:    H5FD_get_eoa
2433
 *
2434
 * Purpose:     Private version of H5FDget_eoa()
2435
 *
2436
 *              This function returns the EOA as a RELATIVE address, i.e.
2437
 *              relative to the base address.  This is NOT the same as the
2438
 *              EOA stored in the superblock, which is an absolute
2439
 *              address.  Object addresses are relative.
2440
 *
2441
 * Return:      Success:    First byte after allocated memory
2442
 *
2443
 *              Failure:    HADDR_UNDEF
2444
 *
2445
 *-------------------------------------------------------------------------
2446
 */
2447
haddr_t
2448
H5FD_get_eoa(const H5FD_t *file, H5FD_mem_t type)
2449
17.6k
{
2450
17.6k
    haddr_t ret_value = HADDR_UNDEF; /* Return value */
2451
2452
17.6k
    FUNC_ENTER_NOAPI(HADDR_UNDEF)
2453
2454
17.6k
    assert(file && file->cls);
2455
2456
    /* Prepare & restore library for user callback */
2457
17.6k
    H5_BEFORE_USER_CB(HADDR_UNDEF)
2458
17.6k
        {
2459
            /* Dispatch to driver */
2460
17.6k
            ret_value = (file->cls->get_eoa)(file, type);
2461
17.6k
        }
2462
17.6k
    H5_AFTER_USER_CB(HADDR_UNDEF)
2463
17.6k
    if (!H5_addr_defined(ret_value))
2464
0
        HGOTO_ERROR(H5E_VFL, H5E_CANTINIT, HADDR_UNDEF, "driver get_eoa request failed");
2465
2466
    /* Adjust for base address in file (convert to relative address) */
2467
17.6k
    ret_value -= file->base_addr;
2468
2469
17.6k
done:
2470
17.6k
    FUNC_LEAVE_NOAPI(ret_value)
2471
17.6k
} /* end H5FD_get_eoa() */
2472
2473
/*-------------------------------------------------------------------------
2474
 * Function:    H5FD_get_eof
2475
 *
2476
 * Purpose:     Private version of H5FDget_eof()
2477
 *
2478
 *              This function returns the EOF as a RELATIVE address, i.e.
2479
 *              relative to the base address.  This will be different
2480
 *              from  the end of the physical file if there is a user
2481
 *              block.
2482
 *
2483
 * Return:      Success:    The EOF address.
2484
 *
2485
 *              Failure:    HADDR_UNDEF
2486
 *
2487
 *-------------------------------------------------------------------------
2488
 */
2489
haddr_t
2490
H5FD_get_eof(const H5FD_t *file, H5FD_mem_t type)
2491
1.04k
{
2492
1.04k
    haddr_t ret_value = HADDR_UNDEF; /* Return value */
2493
2494
1.04k
    FUNC_ENTER_NOAPI(HADDR_UNDEF)
2495
2496
1.04k
    assert(file && file->cls);
2497
2498
    /* Dispatch to driver */
2499
1.04k
    if (file->cls->get_eof) {
2500
        /* Prepare & restore library for user callback */
2501
1.04k
        H5_BEFORE_USER_CB(HADDR_UNDEF)
2502
1.04k
            {
2503
1.04k
                ret_value = (file->cls->get_eof)(file, type);
2504
1.04k
            }
2505
1.04k
        H5_AFTER_USER_CB(HADDR_UNDEF)
2506
1.04k
        if (!H5_addr_defined(ret_value))
2507
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTGET, HADDR_UNDEF, "driver get_eof request failed");
2508
1.04k
    }
2509
0
    else
2510
0
        ret_value = file->maxaddr;
2511
2512
    /* Adjust for base address in file (convert to relative address)  */
2513
1.04k
    ret_value -= file->base_addr;
2514
2515
1.04k
done:
2516
1.04k
    FUNC_LEAVE_NOAPI(ret_value)
2517
1.04k
} /* end H5FD_get_eof() */
2518
2519
/*-------------------------------------------------------------------------
2520
 * Function:     H5FD_driver_query
2521
 *
2522
 * Purpose:      Similar to H5FD_query(), but intended for cases when we don't
2523
 *               have a file available (e.g. before one is opened). Since we
2524
 *               can't use the file to get the driver, the driver is passed in
2525
 *               as a parameter.
2526
 *
2527
 * Return:       SUCCEED/FAIL
2528
 *
2529
 *-------------------------------------------------------------------------
2530
 */
2531
herr_t
2532
H5FD_driver_query(const H5FD_class_t *driver, unsigned long *flags /*out*/)
2533
264
{
2534
264
    herr_t ret_value = SUCCEED; /* Return value */
2535
2536
264
    FUNC_ENTER_NOAPI_NOINIT_NOERR
2537
2538
264
    assert(driver);
2539
264
    assert(flags);
2540
2541
    /* Check for the driver to query and then query it */
2542
264
    if (driver->query) {
2543
        /* Prepare & restore library for user callback */
2544
264
        H5_BEFORE_USER_CB_NOERR(FAIL)
2545
264
            {
2546
264
                ret_value = (driver->query)(NULL, flags);
2547
264
            }
2548
264
        H5_AFTER_USER_CB_NOERR(FAIL)
2549
264
    }
2550
0
    else
2551
0
        *flags = 0;
2552
2553
264
    FUNC_LEAVE_NOAPI(ret_value)
2554
264
} /* end H5FD_driver_query() */
2555
2556
/*------------------------------------------------------------------------
2557
 * Function: H5FD__vstr_tmp_cmp()
2558
 *
2559
 * Purpose:  This is the comparison callback function used by qsort()
2560
 *           in H5FD__sort_io_req_real( )
2561
 *
2562
 *-------------------------------------------------------------------------
2563
 */
2564
static int
2565
H5FD__srt_tmp_cmp(const void *element_1, const void *element_2)
2566
0
{
2567
0
    haddr_t addr_1    = ((const H5FD_srt_tmp_t *)element_1)->addr;
2568
0
    haddr_t addr_2    = ((const H5FD_srt_tmp_t *)element_2)->addr;
2569
0
    int     ret_value = 0; /* Return value */
2570
2571
0
    FUNC_ENTER_PACKAGE_NOERR
2572
2573
    /* Sanity checks */
2574
0
    assert(H5_addr_defined(addr_1));
2575
0
    assert(H5_addr_defined(addr_2));
2576
2577
    /* Compare the addresses */
2578
0
    if (H5_addr_gt(addr_1, addr_2))
2579
0
        ret_value = 1;
2580
0
    else if (H5_addr_lt(addr_1, addr_2))
2581
0
        ret_value = -1;
2582
2583
0
    FUNC_LEAVE_NOAPI(ret_value)
2584
0
} /* H5FD__srt_tmp_cmp() */
2585
2586
/*-------------------------------------------------------------------------
2587
 *  Function:    H5FD__sort_io_req_real()
2588
 *
2589
 *  Purpose:     Scan the addrs array to see if it is sorted.
2590
 *
2591
 *               If sorted, return true in *was_sorted.
2592
 *
2593
 *               If not sorted, use qsort() to sort the array.
2594
 *               Do this by allocating an array of struct H5FD_srt_tmp_t,
2595
 *               where each instance of H5FD_srt_tmp_t has two fields,
2596
 *               addr and index.  Load the array with the contents of the
2597
 *               addrs array and the index of the associated entry.
2598
 *               Then sort the array using qsort().
2599
 *               Return *false in was_sorted.
2600
 *
2601
 *               This is a common routine used by:
2602
 *               --H5FD_sort_vector_io_req()
2603
 *               --H5FD_sort_selection_io_req()
2604
 *
2605
 *  Return:      SUCCEED/FAIL
2606
 *
2607
 *-------------------------------------------------------------------------
2608
 */
2609
static herr_t
2610
H5FD__sort_io_req_real(size_t count, haddr_t *addrs, bool *was_sorted, struct H5FD_srt_tmp_t **srt_tmp)
2611
0
{
2612
0
    size_t i;
2613
0
    herr_t ret_value = SUCCEED; /* Return value */
2614
2615
0
    FUNC_ENTER_PACKAGE
2616
2617
    /* Sanity checks */
2618
2619
    /* scan the offsets array to see if it is sorted */
2620
0
    for (i = 1; i < count; i++) {
2621
0
        assert(H5_addr_defined(addrs[i - 1]));
2622
2623
0
        if (H5_addr_gt(addrs[i - 1], addrs[i]))
2624
0
            break;
2625
0
        else if (H5_addr_eq(addrs[i - 1], addrs[i]))
2626
0
            HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "duplicate addr in selections");
2627
0
    }
2628
2629
    /* if we traversed the entire array without breaking out, then
2630
     * the array was already sorted */
2631
0
    if (i >= count)
2632
0
        *was_sorted = true;
2633
0
    else
2634
0
        *was_sorted = false;
2635
2636
0
    if (!(*was_sorted)) {
2637
0
        size_t srt_tmp_size;
2638
2639
0
        srt_tmp_size = (count * sizeof(struct H5FD_srt_tmp_t));
2640
2641
0
        if (NULL == (*srt_tmp = (H5FD_srt_tmp_t *)malloc(srt_tmp_size)))
2642
2643
0
            HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL, "can't alloc srt_tmp");
2644
2645
0
        for (i = 0; i < count; i++) {
2646
0
            (*srt_tmp)[i].addr  = addrs[i];
2647
0
            (*srt_tmp)[i].index = i;
2648
0
        }
2649
2650
        /* sort the srt_tmp array */
2651
0
        qsort(*srt_tmp, count, sizeof(struct H5FD_srt_tmp_t), H5FD__srt_tmp_cmp);
2652
2653
        /* verify no duplicate entries */
2654
0
        i = 1;
2655
2656
0
        for (i = 1; i < count; i++) {
2657
0
            assert(H5_addr_lt((*srt_tmp)[i - 1].addr, (*srt_tmp)[i].addr));
2658
2659
0
            if (H5_addr_eq(addrs[i - 1], addrs[i]))
2660
0
                HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "duplicate addrs in array");
2661
0
        }
2662
0
    }
2663
2664
0
done:
2665
0
    FUNC_LEAVE_NOAPI(ret_value)
2666
2667
0
} /* H5FD__sort_io_req_real() */
2668
2669
/*-------------------------------------------------------------------------
2670
 * Function:    H5FD_sort_vector_io_req
2671
 *
2672
 * Purpose:     Determine whether the supplied vector I/O request is
2673
 *              sorted.
2674
 *
2675
 *              if is is, set *vector_was_sorted to true, set:
2676
 *
2677
 *                 *s_types_ptr = types
2678
 *                 *s_addrs_ptr = addrs
2679
 *                 *s_sizes_ptr = sizes
2680
 *                 *s_bufs_ptr = bufs
2681
 *
2682
 *              and return.
2683
 *
2684
 *              If it is not sorted, duplicate the type, addrs, sizes,
2685
 *              and bufs vectors, storing the base addresses of the new
2686
 *              vectors in *s_types_ptr, *s_addrs_ptr, *s_sizes_ptr, and
2687
 *              *s_bufs_ptr respectively.  Determine the sorted order
2688
 *              of the vector I/O request, and load it into the new
2689
 *              vectors in sorted order.
2690
 *
2691
 *              Note that in this case, it is the callers responsibility
2692
 *              to free the sorted vectors.
2693
 *
2694
 *                                            JRM -- 3/15/21
2695
 *
2696
 * Return:      SUCCEED/FAIL
2697
 *
2698
 *-------------------------------------------------------------------------
2699
 */
2700
herr_t
2701
H5FD_sort_vector_io_req(bool *vector_was_sorted, uint32_t _count, H5FD_mem_t types[], haddr_t addrs[],
2702
                        size_t sizes[], H5_flexible_const_ptr_t bufs[], H5FD_mem_t **s_types_ptr,
2703
                        haddr_t **s_addrs_ptr, size_t **s_sizes_ptr, H5_flexible_const_ptr_t **s_bufs_ptr)
2704
0
{
2705
0
    herr_t                 ret_value = SUCCEED; /* Return value */
2706
0
    size_t                 count     = (size_t)_count;
2707
0
    size_t                 i;
2708
0
    struct H5FD_srt_tmp_t *srt_tmp = NULL;
2709
2710
0
    FUNC_ENTER_NOAPI(FAIL)
2711
2712
    /* Sanity checks */
2713
2714
0
    assert(vector_was_sorted);
2715
2716
0
    assert((types) || (count == 0));
2717
0
    assert((addrs) || (count == 0));
2718
0
    assert((sizes) || (count == 0));
2719
0
    assert((bufs) || (count == 0));
2720
2721
    /* verify that the first elements of the sizes and types arrays are
2722
     * valid.
2723
     */
2724
0
    assert((count == 0) || (sizes[0] != 0));
2725
0
    assert((count == 0) || (types[0] != H5FD_MEM_NOLIST));
2726
2727
0
    assert((count == 0) || ((s_types_ptr) && (NULL == *s_types_ptr)));
2728
0
    assert((count == 0) || ((s_addrs_ptr) && (NULL == *s_addrs_ptr)));
2729
0
    assert((count == 0) || ((s_sizes_ptr) && (NULL == *s_sizes_ptr)));
2730
0
    assert((count == 0) || ((s_bufs_ptr) && (NULL == *s_bufs_ptr)));
2731
2732
    /* Sort the addrs array in increasing addr order, while
2733
     * maintaining the association between each addr, and the
2734
     * sizes[], types[], and bufs[] values at the same index.
2735
     */
2736
0
    if (H5FD__sort_io_req_real(count, addrs, vector_was_sorted, &srt_tmp) < 0)
2737
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "sorting error in selection offsets");
2738
2739
0
    if (*vector_was_sorted) {
2740
2741
0
        *s_types_ptr = types;
2742
0
        *s_addrs_ptr = addrs;
2743
0
        *s_sizes_ptr = sizes;
2744
0
        *s_bufs_ptr  = bufs;
2745
0
    }
2746
0
    else {
2747
2748
        /*
2749
         * Allocate the s_types_ptr, s_addrs_ptr, s_sizes_ptr, and s_bufs_ptr
2750
         * arrays and populate them using the mapping provided by
2751
         * the sorted array of H5FD_srt_tmp_t.
2752
         */
2753
0
        size_t j;
2754
0
        size_t fixed_size_index = count;
2755
0
        size_t fixed_type_index = count;
2756
2757
0
        if ((NULL == (*s_types_ptr = (H5FD_mem_t *)malloc(count * sizeof(H5FD_mem_t)))) ||
2758
0
            (NULL == (*s_addrs_ptr = (haddr_t *)malloc(count * sizeof(haddr_t)))) ||
2759
0
            (NULL == (*s_sizes_ptr = (size_t *)malloc(count * sizeof(size_t)))) ||
2760
0
            (NULL ==
2761
0
             (*s_bufs_ptr = (H5_flexible_const_ptr_t *)malloc(count * sizeof(H5_flexible_const_ptr_t))))) {
2762
2763
0
            HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL, "can't alloc sorted vector(s)");
2764
0
        }
2765
2766
0
        assert(sizes[0] != 0);
2767
0
        assert(types[0] != H5FD_MEM_NOLIST);
2768
2769
        /* Scan the sizes and types vectors to determine if the fixed size / type
2770
         * optimization is in use, and if so, to determine the index of the last
2771
         * valid value on each vector.  We have already verified that the first
2772
         * elements of these arrays are valid so we can start at the second
2773
         * element (if it exists).
2774
         */
2775
0
        for (i = 1; i < count && ((fixed_size_index == count) || (fixed_type_index == count)); i++) {
2776
0
            if ((fixed_size_index == count) && (sizes[i] == 0))
2777
0
                fixed_size_index = i - 1;
2778
0
            if ((fixed_type_index == count) && (types[i] == H5FD_MEM_NOLIST))
2779
0
                fixed_type_index = i - 1;
2780
0
        }
2781
2782
0
        assert(fixed_size_index <= count);
2783
0
        assert(fixed_type_index <= count);
2784
2785
        /* Populate the sorted vectors.  Note that the index stored in srt_tmp
2786
         * refers to the index in the unsorted array, while the position of
2787
         * srt_tmp within the sorted array is the index in the sorted arrays */
2788
0
        for (i = 0; i < count; i++) {
2789
2790
0
            j = srt_tmp[i].index;
2791
2792
0
            (*s_types_ptr)[i] = types[MIN(j, fixed_type_index)];
2793
0
            (*s_addrs_ptr)[i] = addrs[j];
2794
0
            (*s_sizes_ptr)[i] = sizes[MIN(j, fixed_size_index)];
2795
0
            (*s_bufs_ptr)[i]  = bufs[j];
2796
0
        }
2797
0
    }
2798
2799
0
done:
2800
0
    if (srt_tmp) {
2801
2802
0
        free(srt_tmp);
2803
0
        srt_tmp = NULL;
2804
0
    }
2805
2806
    /* On failure, free the sorted vectors if they were allocated.
2807
     * Note that we only allocate these vectors if the original array
2808
     * was not sorted -- thus we check both for failure, and for
2809
     * the flag indicating that the original vector was not sorted
2810
     * in increasing address order.
2811
     */
2812
0
    if ((ret_value != SUCCEED) && (!(*vector_was_sorted))) {
2813
2814
        /* free space allocated for sorted vectors */
2815
0
        if (*s_types_ptr) {
2816
2817
0
            free(*s_types_ptr);
2818
0
            *s_types_ptr = NULL;
2819
0
        }
2820
2821
0
        if (*s_addrs_ptr) {
2822
2823
0
            free(*s_addrs_ptr);
2824
0
            *s_addrs_ptr = NULL;
2825
0
        }
2826
2827
0
        if (*s_sizes_ptr) {
2828
2829
0
            free(*s_sizes_ptr);
2830
0
            *s_sizes_ptr = NULL;
2831
0
        }
2832
2833
0
        if (*s_bufs_ptr) {
2834
2835
0
            free(*s_bufs_ptr);
2836
0
            *s_bufs_ptr = NULL;
2837
0
        }
2838
0
    }
2839
2840
0
    FUNC_LEAVE_NOAPI(ret_value)
2841
2842
0
} /* end H5FD_sort_vector_io_req() */
2843
2844
/*-------------------------------------------------------------------------
2845
 * Purpose:     Determine whether the supplied selection I/O request is
2846
 *              sorted.
2847
 *
2848
 *              if is is, set *selection_was_sorted to true, set:
2849
 *
2850
 *                  *s_mem_space_ids_ptr  = mem_space_ids;
2851
 *                  *s_file_space_ids_ptr = file_space_ids;
2852
 *                  *s_offsets_ptr        = offsets;
2853
 *                  *s_element_sizes_ptr  = element_sizes;
2854
 *                  *s_bufs_ptr           = bufs;
2855
 *
2856
 *              and return.
2857
 *
2858
 *              If it is not sorted, duplicate the mem_space_ids, file_space_ids,
2859
 *              offsets, element_sizes and bufs arrays, storing the base
2860
 *              addresses of the new arrays in *s_mem_space_ids_ptr,
2861
 *              s_file_space_ids_ptr, s_offsets_ptr, *s_element_sizes_ptr,
2862
 *              and s_bufs_ptr respectively.  Determine the sorted order
2863
 *              of the selection I/O request, and load it into the new
2864
 *              selections in sorted order.
2865
 *
2866
 *              Note that in this case, it is the caller's responsibility
2867
 *              to free the sorted vectors.
2868
 *
2869
 * Return:      SUCCEED/FAIL
2870
 *
2871
 *-------------------------------------------------------------------------
2872
 */
2873
herr_t
2874
H5FD_sort_selection_io_req(bool *selection_was_sorted, size_t count, hid_t mem_space_ids[],
2875
                           hid_t file_space_ids[], haddr_t offsets[], size_t element_sizes[],
2876
                           H5_flexible_const_ptr_t bufs[], hid_t **s_mem_space_ids_ptr,
2877
                           hid_t **s_file_space_ids_ptr, haddr_t **s_offsets_ptr,
2878
                           size_t **s_element_sizes_ptr, H5_flexible_const_ptr_t **s_bufs_ptr)
2879
0
{
2880
0
    size_t                 i;
2881
0
    struct H5FD_srt_tmp_t *srt_tmp   = NULL;
2882
0
    herr_t                 ret_value = SUCCEED; /* Return value */
2883
2884
0
    FUNC_ENTER_NOAPI(FAIL)
2885
2886
    /* Sanity checks */
2887
2888
0
    assert(selection_was_sorted);
2889
2890
0
    assert((mem_space_ids) || (count == 0));
2891
0
    assert((file_space_ids) || (count == 0));
2892
0
    assert((offsets) || (count == 0));
2893
0
    assert((element_sizes) || (count == 0));
2894
0
    assert((bufs) || (count == 0));
2895
2896
    /* verify that the first elements of the element_sizes and bufs arrays are
2897
     * valid.
2898
     */
2899
0
    assert((count == 0) || (element_sizes[0] != 0));
2900
0
    assert((count == 0) || (bufs[0].cvp != NULL));
2901
2902
0
    assert((count == 0) || ((s_mem_space_ids_ptr) && (NULL == *s_mem_space_ids_ptr)));
2903
0
    assert((count == 0) || ((s_file_space_ids_ptr) && (NULL == *s_file_space_ids_ptr)));
2904
0
    assert((count == 0) || ((s_offsets_ptr) && (NULL == *s_offsets_ptr)));
2905
0
    assert((count == 0) || ((s_element_sizes_ptr) && (NULL == *s_element_sizes_ptr)));
2906
0
    assert((count == 0) || ((s_bufs_ptr) && (NULL == *s_bufs_ptr)));
2907
2908
    /* Sort the offsets array in increasing offset order, while
2909
     * maintaining the association between each offset, and the
2910
     * mem_space_ids[], file_space_ids[], element_sizes and bufs[]
2911
     * values at the same index.
2912
     */
2913
0
    if (H5FD__sort_io_req_real(count, offsets, selection_was_sorted, &srt_tmp) < 0)
2914
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "sorting error in selection offsets");
2915
2916
0
    if (*selection_was_sorted) {
2917
2918
0
        *s_mem_space_ids_ptr  = mem_space_ids;
2919
0
        *s_file_space_ids_ptr = file_space_ids;
2920
0
        *s_offsets_ptr        = offsets;
2921
0
        *s_element_sizes_ptr  = element_sizes;
2922
0
        *s_bufs_ptr           = bufs;
2923
0
    }
2924
0
    else {
2925
2926
        /*
2927
         * Allocate the s_mem_space_ids_ptr, s_file_space_ids_ptr, s_offsets_ptr,
2928
         * s_element_sizes_ptr and s_bufs_ptr arrays and populate them using the
2929
         * mapping provided by the sorted array of H5FD_srt_tmp_t.
2930
         */
2931
0
        size_t j;
2932
0
        size_t fixed_element_sizes_index = count;
2933
0
        size_t fixed_bufs_index          = count;
2934
2935
0
        if ((NULL == (*s_mem_space_ids_ptr = (hid_t *)malloc(count * sizeof(hid_t)))) ||
2936
0
            (NULL == (*s_file_space_ids_ptr = (hid_t *)malloc(count * sizeof(hid_t)))) ||
2937
0
            (NULL == (*s_offsets_ptr = (haddr_t *)malloc(count * sizeof(haddr_t)))) ||
2938
0
            (NULL == (*s_element_sizes_ptr = (size_t *)malloc(count * sizeof(size_t)))) ||
2939
0
            (NULL ==
2940
0
             (*s_bufs_ptr = (H5_flexible_const_ptr_t *)malloc(count * sizeof(H5_flexible_const_ptr_t))))) {
2941
2942
0
            HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL, "can't alloc sorted selection(s)");
2943
0
        }
2944
2945
0
        assert(element_sizes[0] != 0);
2946
0
        assert(bufs[0].cvp != NULL);
2947
2948
        /* Scan the element_sizes and bufs array to determine if the fixed
2949
         * element_sizes / bufs optimization is in use, and if so, to determine
2950
         * the index of the last valid value on each array.
2951
         * We have already verified that the first
2952
         * elements of these arrays are valid so we can start at the second
2953
         * element (if it exists).
2954
         */
2955
0
        for (i = 1; i < count && ((fixed_element_sizes_index == count) || (fixed_bufs_index == count)); i++) {
2956
0
            if ((fixed_element_sizes_index == count) && (element_sizes[i] == 0))
2957
0
                fixed_element_sizes_index = i - 1;
2958
0
            if ((fixed_bufs_index == count) && (bufs[i].cvp == NULL))
2959
0
                fixed_bufs_index = i - 1;
2960
0
        }
2961
2962
0
        assert(fixed_element_sizes_index <= count);
2963
0
        assert(fixed_bufs_index <= count);
2964
2965
        /* Populate the sorted arrays.  Note that the index stored in srt_tmp
2966
         * refers to the index in the unsorted array, while the position of
2967
         * srt_tmp within the sorted array is the index in the sorted arrays */
2968
0
        for (i = 0; i < count; i++) {
2969
2970
0
            j = srt_tmp[i].index;
2971
2972
0
            (*s_mem_space_ids_ptr)[i]  = mem_space_ids[j];
2973
0
            (*s_file_space_ids_ptr)[i] = file_space_ids[j];
2974
0
            (*s_offsets_ptr)[i]        = offsets[j];
2975
0
            (*s_element_sizes_ptr)[i]  = element_sizes[MIN(j, fixed_element_sizes_index)];
2976
0
            (*s_bufs_ptr)[i]           = bufs[MIN(j, fixed_bufs_index)];
2977
0
        }
2978
0
    }
2979
2980
0
done:
2981
0
    if (srt_tmp) {
2982
0
        free(srt_tmp);
2983
0
        srt_tmp = NULL;
2984
0
    }
2985
2986
    /* On failure, free the sorted arrays if they were allocated.
2987
     * Note that we only allocate these arrays if the original array
2988
     * was not sorted -- thus we check both for failure, and for
2989
     * the flag indicating that the original array was not sorted
2990
     * in increasing address order.
2991
     */
2992
0
    if ((ret_value != SUCCEED) && (!(*selection_was_sorted))) {
2993
2994
        /* free space allocated for sorted arrays */
2995
0
        if (*s_mem_space_ids_ptr) {
2996
0
            free(*s_mem_space_ids_ptr);
2997
0
            *s_mem_space_ids_ptr = NULL;
2998
0
        }
2999
3000
0
        if (*s_file_space_ids_ptr) {
3001
0
            free(*s_file_space_ids_ptr);
3002
0
            *s_file_space_ids_ptr = NULL;
3003
0
        }
3004
3005
0
        if (*s_offsets_ptr) {
3006
0
            free(*s_offsets_ptr);
3007
0
            *s_offsets_ptr = NULL;
3008
0
        }
3009
3010
0
        if (*s_element_sizes_ptr) {
3011
0
            free(*s_element_sizes_ptr);
3012
0
            *s_element_sizes_ptr = NULL;
3013
0
        }
3014
3015
0
        if (*s_bufs_ptr) {
3016
0
            free(*s_bufs_ptr);
3017
0
            *s_bufs_ptr = NULL;
3018
0
        }
3019
0
    }
3020
3021
0
    FUNC_LEAVE_NOAPI(ret_value)
3022
3023
0
} /* end H5FD_sort_selection_io_req() */
3024
3025
/*-------------------------------------------------------------------------
3026
 * Function:    H5FD_delete
3027
 *
3028
 * Purpose:     Private version of H5FDdelete()
3029
 *
3030
 * Return:      SUCCEED/FAIL
3031
 *
3032
 *-------------------------------------------------------------------------
3033
 */
3034
herr_t
3035
H5FD_delete(const char *filename, hid_t fapl_id)
3036
0
{
3037
0
    H5FD_class_t      *driver;              /* VFD for file */
3038
0
    H5FD_driver_prop_t driver_prop;         /* Property for driver ID & info */
3039
0
    H5P_genplist_t    *plist;               /* Property list pointer */
3040
0
    herr_t             ret_value = SUCCEED; /* Return value */
3041
3042
0
    FUNC_ENTER_NOAPI(FAIL)
3043
3044
    /* Sanity checks */
3045
3046
0
    assert(filename);
3047
3048
    /* Get file access property list */
3049
0
    if (NULL == (plist = (H5P_genplist_t *)H5I_object(fapl_id)))
3050
0
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a file access property list");
3051
3052
    /* Get the VFD to open the file with */
3053
0
    if (H5P_peek(plist, H5F_ACS_FILE_DRV_NAME, &driver_prop) < 0)
3054
0
        HGOTO_ERROR(H5E_PLIST, H5E_CANTGET, FAIL, "can't get driver ID & info");
3055
3056
    /* Get driver info */
3057
0
    if (NULL == (driver = (H5FD_class_t *)H5I_object(driver_prop.driver_id)))
3058
0
        HGOTO_ERROR(H5E_VFL, H5E_BADVALUE, FAIL, "invalid driver ID in file access property list");
3059
0
    if (NULL == driver->del)
3060
0
        HGOTO_ERROR(H5E_VFL, H5E_UNSUPPORTED, FAIL, "file driver has no 'del' method");
3061
3062
    /* Prepare & restore library for user callback */
3063
0
    H5_BEFORE_USER_CB(FAIL)
3064
0
        {
3065
            /* Dispatch to file driver */
3066
0
            ret_value = (driver->del)(filename, fapl_id);
3067
0
        }
3068
0
    H5_AFTER_USER_CB(FAIL)
3069
0
    if (ret_value < 0)
3070
0
        HGOTO_ERROR(H5E_VFL, H5E_CANTDELETEFILE, FAIL, "delete failed");
3071
3072
0
done:
3073
0
    FUNC_LEAVE_NOAPI(ret_value)
3074
0
} /* end H5FD_delete() */
3075
3076
/*-------------------------------------------------------------------------
3077
 * Function:    H5FD_check_plugin_load
3078
 *
3079
 * Purpose:     Check if a VFD plugin matches the search criteria, and can
3080
 *              be loaded.
3081
 *
3082
 * Note:        Matching the driver's name / value, but the driver having
3083
 *              an incompatible version is not an error, but means that the
3084
 *              driver isn't a "match".  Setting the SUCCEED value to false
3085
 *              and not failing for that case allows the plugin framework
3086
 *              to keep looking for other DLLs that match and have a
3087
 *              compatible version.
3088
 *
3089
 * Return:      SUCCEED / FAIL
3090
 *
3091
 *-------------------------------------------------------------------------
3092
 */
3093
herr_t
3094
H5FD_check_plugin_load(const H5FD_class_t *cls, const H5PL_key_t *key, bool *success)
3095
0
{
3096
0
    herr_t ret_value = SUCCEED; /* Return value */
3097
3098
0
    FUNC_ENTER_NOAPI(FAIL)
3099
3100
    /* Sanity checks */
3101
0
    assert(cls);
3102
0
    assert(key);
3103
0
    assert(success);
3104
3105
    /* Which kind of key are we looking for? */
3106
0
    if (key->vfd.kind == H5FD_GET_DRIVER_BY_NAME) {
3107
        /* Check if plugin name matches VFD class name */
3108
0
        if (cls->name && !strcmp(cls->name, key->vfd.u.name))
3109
0
            *success = true;
3110
0
    }
3111
0
    else {
3112
        /* Sanity check */
3113
0
        assert(key->vfd.kind == H5FD_GET_DRIVER_BY_VALUE);
3114
3115
        /* Check if plugin value matches VFD class value */
3116
0
        if (cls->value == key->vfd.u.value)
3117
0
            *success = true;
3118
0
    }
3119
3120
0
done:
3121
0
    FUNC_LEAVE_NOAPI(ret_value)
3122
0
} /* end H5FD_check_plugin_load() */
3123
3124
/*-------------------------------------------------------------------------
3125
 * Function:    H5FD__get_driver_cb
3126
 *
3127
 * Purpose:     Callback routine to search through registered VFDs
3128
 *
3129
 * Return:      Success:    H5_ITER_STOP if the class and op_data name
3130
 *                          members match. H5_ITER_CONT otherwise.
3131
 *              Failure:    Can't fail
3132
 *
3133
 *-------------------------------------------------------------------------
3134
 */
3135
static int
3136
H5FD__get_driver_cb(void *obj, hid_t id, void *_op_data)
3137
0
{
3138
0
    H5FD_get_driver_ud_t *op_data   = (H5FD_get_driver_ud_t *)_op_data; /* User data for callback */
3139
0
    H5FD_class_t         *cls       = (H5FD_class_t *)obj;
3140
0
    int                   ret_value = H5_ITER_CONT; /* Callback return value */
3141
3142
0
    FUNC_ENTER_PACKAGE_NOERR
3143
3144
0
    if (H5FD_GET_DRIVER_BY_NAME == op_data->key.kind) {
3145
0
        if (0 == strcmp(cls->name, op_data->key.u.name)) {
3146
0
            op_data->found_id = id;
3147
0
            ret_value         = H5_ITER_STOP;
3148
0
        } /* end if */
3149
0
    }     /* end if */
3150
0
    else {
3151
0
        assert(H5FD_GET_DRIVER_BY_VALUE == op_data->key.kind);
3152
0
        if (cls->value == op_data->key.u.value) {
3153
0
            op_data->found_id = id;
3154
0
            ret_value         = H5_ITER_STOP;
3155
0
        } /* end if */
3156
0
    }     /* end else */
3157
3158
0
    FUNC_LEAVE_NOAPI(ret_value)
3159
0
} /* end H5FD__get_driver_cb() */
3160
3161
/*-------------------------------------------------------------------------
3162
 * Function:    H5FD_register_driver_by_name
3163
 *
3164
 * Purpose:     Registers a new VFD as a member of the virtual file driver
3165
 *              class.
3166
 *
3167
 * Return:      Success:    A VFD ID which is good until the library is
3168
 *                          closed.
3169
 *
3170
 *              Failure:    H5I_INVALID_HID
3171
 *
3172
 *-------------------------------------------------------------------------
3173
 */
3174
hid_t
3175
H5FD_register_driver_by_name(const char *name, bool app_ref)
3176
0
{
3177
0
    htri_t driver_is_registered = false;
3178
0
    hid_t  driver_id            = H5I_INVALID_HID;
3179
0
    hid_t  ret_value            = H5I_INVALID_HID; /* Return value */
3180
3181
0
    FUNC_ENTER_NOAPI(H5I_INVALID_HID)
3182
3183
    /* Check if driver is already registered */
3184
0
    if ((driver_is_registered = H5FD_is_driver_registered_by_name(name, &driver_id)) < 0)
3185
0
        HGOTO_ERROR(H5E_VFL, H5E_BADITER, H5I_INVALID_HID, "can't check if driver is already registered");
3186
3187
    /* If driver is already registered, increment ref count on ID and return ID */
3188
0
    if (driver_is_registered) {
3189
0
        assert(driver_id >= 0);
3190
3191
0
        if (H5I_inc_ref(driver_id, app_ref) < 0)
3192
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTINC, H5I_INVALID_HID, "unable to increment ref count on VFD");
3193
0
    } /* end if */
3194
0
    else {
3195
0
        H5PL_key_t          key;
3196
0
        const H5FD_class_t *cls;
3197
3198
        /* Try loading the driver */
3199
0
        key.vfd.kind   = H5FD_GET_DRIVER_BY_NAME;
3200
0
        key.vfd.u.name = name;
3201
0
        if (NULL == (cls = (const H5FD_class_t *)H5PL_load(H5PL_TYPE_VFD, &key)))
3202
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTINIT, H5I_INVALID_HID, "unable to load VFD");
3203
3204
        /* Register the driver we loaded */
3205
0
        if ((driver_id = H5FD_register(cls, sizeof(*cls), app_ref)) < 0)
3206
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTREGISTER, H5I_INVALID_HID, "unable to register VFD ID");
3207
0
    } /* end else */
3208
3209
0
    ret_value = driver_id;
3210
3211
0
done:
3212
0
    FUNC_LEAVE_NOAPI(ret_value)
3213
0
} /* end H5FD_register_driver_by_name() */
3214
3215
/*-------------------------------------------------------------------------
3216
 * Function:    H5FD_register_driver_by_value
3217
 *
3218
 * Purpose:     Registers a new VFD as a member of the virtual file driver
3219
 *              class.
3220
 *
3221
 * Return:      Success:    A VFD ID which is good until the library is
3222
 *                          closed.
3223
 *
3224
 *              Failure:    H5I_INVALID_HID
3225
 *
3226
 *-------------------------------------------------------------------------
3227
 */
3228
hid_t
3229
H5FD_register_driver_by_value(H5FD_class_value_t value, bool app_ref)
3230
0
{
3231
0
    htri_t driver_is_registered = false;
3232
0
    hid_t  driver_id            = H5I_INVALID_HID;
3233
0
    hid_t  ret_value            = H5I_INVALID_HID; /* Return value */
3234
3235
0
    FUNC_ENTER_NOAPI(H5I_INVALID_HID)
3236
3237
    /* Check if driver is already registered */
3238
0
    if ((driver_is_registered = H5FD_is_driver_registered_by_value(value, &driver_id)) < 0)
3239
0
        HGOTO_ERROR(H5E_VFL, H5E_BADITER, H5I_INVALID_HID, "can't check if driver is already registered");
3240
3241
    /* If driver is already registered, increment ref count on ID and return ID */
3242
0
    if (driver_is_registered) {
3243
0
        assert(driver_id >= 0);
3244
3245
0
        if (H5I_inc_ref(driver_id, app_ref) < 0)
3246
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTINC, H5I_INVALID_HID, "unable to increment ref count on VFD");
3247
0
    } /* end if */
3248
0
    else {
3249
0
        H5PL_key_t          key;
3250
0
        const H5FD_class_t *cls;
3251
3252
        /* Try loading the driver */
3253
0
        key.vfd.kind    = H5FD_GET_DRIVER_BY_VALUE;
3254
0
        key.vfd.u.value = value;
3255
0
        if (NULL == (cls = (const H5FD_class_t *)H5PL_load(H5PL_TYPE_VFD, &key)))
3256
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTINIT, H5I_INVALID_HID, "unable to load VFD");
3257
3258
        /* Register the driver we loaded */
3259
0
        if ((driver_id = H5FD_register(cls, sizeof(*cls), app_ref)) < 0)
3260
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTREGISTER, H5I_INVALID_HID, "unable to register VFD ID");
3261
0
    } /* end else */
3262
3263
0
    ret_value = driver_id;
3264
3265
0
done:
3266
0
    FUNC_LEAVE_NOAPI(ret_value)
3267
0
} /* end H5FD_register_driver_by_value() */
3268
3269
/*-------------------------------------------------------------------------
3270
 * Function:    H5FD_is_driver_registered_by_name
3271
 *
3272
 * Purpose:     Checks if a driver with a particular name is registered.
3273
 *              If `registered_id` is non-NULL and a driver with the
3274
 *              specified name has been registered, the driver's ID will be
3275
 *              returned in `registered_id`.
3276
 *
3277
 * Return:      >0 if a VFD with that name has been registered
3278
 *              0 if a VFD with that name has NOT been registered
3279
 *              <0 on errors
3280
 *
3281
 *-------------------------------------------------------------------------
3282
 */
3283
htri_t
3284
H5FD_is_driver_registered_by_name(const char *driver_name, hid_t *registered_id)
3285
0
{
3286
0
    H5FD_get_driver_ud_t op_data;           /* Callback info for driver search */
3287
0
    htri_t               ret_value = false; /* Return value */
3288
3289
0
    FUNC_ENTER_NOAPI(FAIL)
3290
3291
    /* Set up op data for iteration */
3292
0
    op_data.key.kind   = H5FD_GET_DRIVER_BY_NAME;
3293
0
    op_data.key.u.name = driver_name;
3294
0
    op_data.found_id   = H5I_INVALID_HID;
3295
3296
    /* Find driver with name */
3297
0
    if (H5I_iterate(H5I_VFL, H5FD__get_driver_cb, &op_data, false) < 0)
3298
0
        HGOTO_ERROR(H5E_VFL, H5E_BADITER, FAIL, "can't iterate over VFDs");
3299
3300
    /* Found a driver with that name */
3301
0
    if (op_data.found_id != H5I_INVALID_HID) {
3302
0
        if (registered_id)
3303
0
            *registered_id = op_data.found_id;
3304
0
        ret_value = true;
3305
0
    }
3306
3307
0
done:
3308
0
    FUNC_LEAVE_NOAPI(ret_value)
3309
0
} /* end H5FD_is_driver_registered_by_name() */
3310
3311
/*-------------------------------------------------------------------------
3312
 * Function:    H5FD_is_driver_registered_by_value
3313
 *
3314
 * Purpose:     Checks if a driver with a particular value (ID) is
3315
 *              registered. If `registered_id` is non-NULL and a driver
3316
 *              with the specified value has been registered, the driver's
3317
 *              ID will be returned in `registered_id`.
3318
 *
3319
 * Return:      >0 if a VFD with that value has been registered
3320
 *              0 if a VFD with that value has NOT been registered
3321
 *              <0 on errors
3322
 *
3323
 *-------------------------------------------------------------------------
3324
 */
3325
htri_t
3326
H5FD_is_driver_registered_by_value(H5FD_class_value_t driver_value, hid_t *registered_id)
3327
0
{
3328
0
    H5FD_get_driver_ud_t op_data;           /* Callback info for driver search */
3329
0
    htri_t               ret_value = false; /* Return value */
3330
3331
0
    FUNC_ENTER_NOAPI(FAIL)
3332
3333
    /* Set up op data for iteration */
3334
0
    op_data.key.kind    = H5FD_GET_DRIVER_BY_VALUE;
3335
0
    op_data.key.u.value = driver_value;
3336
0
    op_data.found_id    = H5I_INVALID_HID;
3337
3338
    /* Find driver with value */
3339
0
    if (H5I_iterate(H5I_VFL, H5FD__get_driver_cb, &op_data, false) < 0)
3340
0
        HGOTO_ERROR(H5E_VFL, H5E_BADITER, FAIL, "can't iterate over VFDs");
3341
3342
    /* Found a driver with that value */
3343
0
    if (op_data.found_id != H5I_INVALID_HID) {
3344
0
        if (registered_id)
3345
0
            *registered_id = op_data.found_id;
3346
0
        ret_value = true;
3347
0
    }
3348
3349
0
done:
3350
0
    FUNC_LEAVE_NOAPI(ret_value)
3351
0
} /* end H5FD_is_driver_registered_by_value() */
3352
3353
/*-------------------------------------------------------------------------
3354
 * Function:    H5FD_get_driver_id_by_name
3355
 *
3356
 * Purpose:     Retrieves the ID for a registered VFL driver.
3357
 *
3358
 * Return:      Positive if the VFL driver has been registered
3359
 *              Negative on error (if the driver is not a valid driver or
3360
 *                  is not registered)
3361
 *
3362
 *-------------------------------------------------------------------------
3363
 */
3364
hid_t
3365
H5FD_get_driver_id_by_name(const char *name, bool is_api)
3366
0
{
3367
0
    H5FD_get_driver_ud_t op_data;
3368
0
    hid_t                ret_value = H5I_INVALID_HID; /* Return value */
3369
3370
0
    FUNC_ENTER_NOAPI(H5I_INVALID_HID)
3371
3372
    /* Set up op data for iteration */
3373
0
    op_data.key.kind   = H5FD_GET_DRIVER_BY_NAME;
3374
0
    op_data.key.u.name = name;
3375
0
    op_data.found_id   = H5I_INVALID_HID;
3376
3377
    /* Find driver with specified name */
3378
0
    if (H5I_iterate(H5I_VFL, H5FD__get_driver_cb, &op_data, false) < 0)
3379
0
        HGOTO_ERROR(H5E_VFL, H5E_BADITER, H5I_INVALID_HID, "can't iterate over VFL drivers");
3380
3381
    /* Found a driver with that name */
3382
0
    if (op_data.found_id != H5I_INVALID_HID) {
3383
0
        ret_value = op_data.found_id;
3384
0
        if (H5I_inc_ref(ret_value, is_api) < 0)
3385
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTINC, H5I_INVALID_HID, "unable to increment ref count on VFL driver");
3386
0
    }
3387
3388
0
done:
3389
0
    FUNC_LEAVE_NOAPI(ret_value)
3390
0
} /* end H5FD_get_driver_id_by_name() */
3391
3392
/*-------------------------------------------------------------------------
3393
 * Function:    H5FD_get_driver_id_by_value
3394
 *
3395
 * Purpose:     Retrieves the ID for a registered VFL driver.
3396
 *
3397
 * Return:      Positive if the VFL driver has been registered
3398
 *              Negative on error (if the driver is not a valid driver or
3399
 *                  is not registered)
3400
 *
3401
 *-------------------------------------------------------------------------
3402
 */
3403
hid_t
3404
H5FD_get_driver_id_by_value(H5FD_class_value_t value, bool is_api)
3405
0
{
3406
0
    H5FD_get_driver_ud_t op_data;
3407
0
    hid_t                ret_value = H5I_INVALID_HID; /* Return value */
3408
3409
0
    FUNC_ENTER_NOAPI(H5I_INVALID_HID)
3410
3411
    /* Set up op data for iteration */
3412
0
    op_data.key.kind    = H5FD_GET_DRIVER_BY_VALUE;
3413
0
    op_data.key.u.value = value;
3414
0
    op_data.found_id    = H5I_INVALID_HID;
3415
3416
    /* Find driver with specified value */
3417
0
    if (H5I_iterate(H5I_VFL, H5FD__get_driver_cb, &op_data, false) < 0)
3418
0
        HGOTO_ERROR(H5E_VFL, H5E_BADITER, H5I_INVALID_HID, "can't iterate over VFL drivers");
3419
3420
    /* Found a driver with that value */
3421
0
    if (op_data.found_id != H5I_INVALID_HID) {
3422
0
        ret_value = op_data.found_id;
3423
0
        if (H5I_inc_ref(ret_value, is_api) < 0)
3424
0
            HGOTO_ERROR(H5E_VFL, H5E_CANTINC, H5I_INVALID_HID, "unable to increment ref count on VFL driver");
3425
0
    }
3426
3427
0
done:
3428
0
    FUNC_LEAVE_NOAPI(ret_value)
3429
0
} /* end H5FD_get_driver_id_by_value() */