Coverage Report

Created: 2025-11-11 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hdf5/src/H5SM.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
/* Module Setup */
15
/****************/
16
17
#define H5O_FRIEND      /*suppress error about including H5Opkg   */
18
#include "H5SMmodule.h" /* This source code file is part of the H5SM module */
19
20
/***********/
21
/* Headers */
22
/***********/
23
#include "H5private.h"   /* Generic Functions     */
24
#include "H5Eprivate.h"  /* Error handling        */
25
#include "H5Fprivate.h"  /* File access                          */
26
#include "H5FLprivate.h" /* Free Lists                           */
27
#include "H5MFprivate.h" /* File memory management    */
28
#include "H5MMprivate.h" /* Memory management     */
29
#include "H5Opkg.h"      /* Object Headers                       */
30
#include "H5SMpkg.h"     /* Shared object header messages        */
31
32
/****************/
33
/* Local Macros */
34
/****************/
35
36
/******************/
37
/* Local Typedefs */
38
/******************/
39
40
/* Udata struct for calls to H5SM__read_iter_op */
41
typedef struct H5SM_read_udata_t {
42
    H5F_t            *file;         /* File in which sharing is happening (in) */
43
    H5O_msg_crt_idx_t idx;          /* Creation index of this message (in) */
44
    size_t            buf_size;     /* Size of the encoded message (out) */
45
    void             *encoding_buf; /* The encoded message (out) */
46
} H5SM_read_udata_t;
47
48
/* Typedef to increment a reference count in the B-tree */
49
typedef struct {
50
    H5SM_mesg_key_t *key;      /* IN: key for message being incremented */
51
    bool             found;    /* OUT: if message was found */
52
    H5O_fheap_id_t   fheap_id; /* OUT: fheap ID of record */
53
} H5SM_incr_ref_opdata_t;
54
55
/********************/
56
/* Local Prototypes */
57
/********************/
58
static herr_t  H5SM__create_index(H5F_t *f, H5SM_index_header_t *header);
59
static herr_t  H5SM__delete_index(H5F_t *f, H5SM_index_header_t *header, bool delete_heap);
60
static haddr_t H5SM__create_list(H5F_t *f, H5SM_index_header_t *header);
61
static herr_t  H5SM__find_in_list(const H5SM_list_t *list, const H5SM_mesg_key_t *key, size_t *empty_pos,
62
                                  size_t *list_pos);
63
static herr_t  H5SM__convert_list_to_btree(H5F_t *f, H5SM_index_header_t *header, H5SM_list_t **_list,
64
                                           H5HF_t *fheap, H5O_t *open_oh);
65
static herr_t  H5SM__bt2_convert_to_list_op(const void *record, void *op_data);
66
static herr_t  H5SM__convert_btree_to_list(H5F_t *f, H5SM_index_header_t *header);
67
static herr_t  H5SM__incr_ref(void *record, void *_op_data, bool *changed);
68
static herr_t  H5SM__write_mesg(H5F_t *f, H5O_t *open_oh, H5SM_index_header_t *header, bool defer,
69
                                unsigned type_id, void *mesg, unsigned *cache_flags_ptr);
70
static herr_t  H5SM__decr_ref(void *record, void *op_data, bool *changed);
71
static herr_t  H5SM__delete_from_index(H5F_t *f, H5O_t *open_oh, H5SM_index_header_t *header,
72
                                       const H5O_shared_t *mesg, unsigned *cache_flags,
73
                                       size_t  */*out*/ mesg_size, void  **/*out*/ encoded_mesg);
74
static herr_t  H5SM__type_to_flag(unsigned type_id, unsigned *type_flag);
75
static herr_t  H5SM__read_iter_op(H5O_t *oh, H5O_mesg_t *mesg, unsigned sequence, void *_udata);
76
static herr_t  H5SM__read_mesg_fh_cb(const void *obj, size_t obj_len, void *_udata);
77
static herr_t  H5SM__read_mesg(H5F_t *f, const H5SM_sohm_t *mesg, H5HF_t *fheap, H5O_t *open_oh,
78
                               size_t *encoding_size /*out*/, void **encoded_mesg /*out*/);
79
80
/*********************/
81
/* Package Variables */
82
/*********************/
83
84
/* Package initialization variable */
85
bool H5_PKG_INIT_VAR = false;
86
87
H5FL_DEFINE(H5SM_master_table_t);
88
H5FL_ARR_DEFINE(H5SM_index_header_t, H5O_SHMESG_MAX_NINDEXES);
89
H5FL_DEFINE(H5SM_list_t);
90
H5FL_ARR_DEFINE(H5SM_sohm_t, H5O_SHMESG_MAX_LIST_SIZE);
91
92
/*****************************/
93
/* Library Private Variables */
94
/*****************************/
95
96
/*******************/
97
/* Local Variables */
98
/*******************/
99
100
/*-------------------------------------------------------------------------
101
 * Function:    H5SM_init
102
 *
103
 * Purpose:     Initializes the Shared Message interface.
104
 *
105
 *              Creates a master SOHM table in the file and in the cache.
106
 *              This function should not be called for files that have
107
 *              SOHMs disabled in the FCPL.
108
 *
109
 * Return:      Non-negative on success/Negative on failure
110
 *
111
 *-------------------------------------------------------------------------
112
 */
113
herr_t
114
H5SM_init(H5F_t *f, H5P_genplist_t *fc_plist, const H5O_loc_t *ext_loc)
115
0
{
116
0
    H5O_shmesg_table_t   sohm_table;                 /* SOHM message for superblock extension */
117
0
    H5SM_master_table_t *table      = NULL;          /* SOHM master table for file */
118
0
    H5AC_ring_t          orig_ring  = H5AC_RING_INV; /* Original ring value */
119
0
    haddr_t              table_addr = HADDR_UNDEF;   /* Address of SOHM master table in file */
120
0
    unsigned             list_max, btree_min;        /* Phase change limits for SOHM indices */
121
0
    unsigned             index_type_flags[H5O_SHMESG_MAX_NINDEXES]; /* Messages types stored in each index */
122
0
    unsigned minsizes[H5O_SHMESG_MAX_NINDEXES]; /* Message size sharing threshold for each index */
123
0
    unsigned type_flags_used;                   /* Message type flags used, for sanity checking */
124
0
    unsigned x;                                 /* Local index variable */
125
0
    herr_t   ret_value = SUCCEED;               /* Return value */
126
127
0
    FUNC_ENTER_NOAPI_TAG(H5AC__SOHM_TAG, FAIL)
128
129
0
    assert(f);
130
    /* File should not already have a SOHM table */
131
0
    assert(!H5_addr_defined(H5F_SOHM_ADDR(f)));
132
133
    /* Set the ring type in the DXPL */
134
0
    H5AC_set_ring(H5AC_RING_USER, &orig_ring);
135
136
    /* Initialize master table */
137
0
    if (NULL == (table = H5FL_CALLOC(H5SM_master_table_t)))
138
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTALLOC, FAIL, "memory allocation failed for SOHM table");
139
0
    table->num_indexes = H5F_SOHM_NINDEXES(f);
140
0
    table->table_size  = H5SM_TABLE_SIZE(f);
141
142
    /* Get information from fcpl */
143
0
    if (H5P_get(fc_plist, H5F_CRT_SHMSG_INDEX_TYPES_NAME, &index_type_flags) < 0)
144
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTGET, FAIL, "can't get SOHM type flags");
145
0
    if (H5P_get(fc_plist, H5F_CRT_SHMSG_LIST_MAX_NAME, &list_max) < 0)
146
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTGET, FAIL, "can't get SOHM list maximum");
147
0
    if (H5P_get(fc_plist, H5F_CRT_SHMSG_BTREE_MIN_NAME, &btree_min) < 0)
148
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTGET, FAIL, "can't get SOHM btree minimum");
149
0
    if (H5P_get(fc_plist, H5F_CRT_SHMSG_INDEX_MINSIZE_NAME, &minsizes) < 0)
150
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTGET, FAIL, "can't get SOHM message min sizes");
151
152
    /* Verify that values are valid */
153
0
    if (table->num_indexes > H5O_SHMESG_MAX_NINDEXES)
154
0
        HGOTO_ERROR(H5E_SOHM, H5E_BADRANGE, FAIL, "number of indexes in property list is too large");
155
156
    /* Check that type flags weren't duplicated anywhere */
157
0
    type_flags_used = 0;
158
0
    for (x = 0; x < table->num_indexes; ++x) {
159
0
        if (index_type_flags[x] & type_flags_used)
160
0
            HGOTO_ERROR(H5E_SOHM, H5E_BADVALUE, FAIL,
161
0
                        "the same shared message type flag is assigned to more than one index");
162
0
        type_flags_used |= index_type_flags[x];
163
0
    } /* end for */
164
165
    /* Check that number of indexes in table and in superblock make sense.
166
     * Right now we just use one byte to hold the number of indexes.
167
     */
168
0
    assert(table->num_indexes < 256);
169
170
    /* Check that list and btree cutoffs make sense.  There can't be any
171
     * values greater than the list max but less than the btree min; the
172
     * list max has to be greater than or equal to one less than the btree
173
     * min.
174
     */
175
0
    assert(list_max + 1 >= btree_min);
176
0
    assert(table->num_indexes > 0 && table->num_indexes <= H5O_SHMESG_MAX_NINDEXES);
177
178
    /* Allocate the SOHM indexes as an array. */
179
0
    if (NULL == (table->indexes =
180
0
                     (H5SM_index_header_t *)H5FL_ARR_MALLOC(H5SM_index_header_t, (size_t)table->num_indexes)))
181
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTALLOC, FAIL, "memory allocation failed for SOHM indexes");
182
183
    /* Initialize all of the indexes, but don't allocate space for them to
184
     * hold messages until we actually need to write to them.
185
     */
186
0
    for (x = 0; x < table->num_indexes; x++) {
187
0
        table->indexes[x].btree_min     = btree_min;
188
0
        table->indexes[x].list_max      = list_max;
189
0
        table->indexes[x].mesg_types    = index_type_flags[x];
190
0
        table->indexes[x].min_mesg_size = minsizes[x];
191
0
        table->indexes[x].index_addr    = HADDR_UNDEF;
192
0
        table->indexes[x].heap_addr     = HADDR_UNDEF;
193
0
        table->indexes[x].num_messages  = 0;
194
195
        /* Indexes start as lists unless the list-to-btree threshold is zero */
196
0
        if (table->indexes[x].list_max > 0)
197
0
            table->indexes[x].index_type = H5SM_LIST;
198
0
        else
199
0
            table->indexes[x].index_type = H5SM_BTREE;
200
201
        /* Compute the size of a list index for this SOHM index */
202
0
        table->indexes[x].list_size = H5SM_LIST_SIZE(f, list_max);
203
0
    } /* end for */
204
205
    /* Allocate space for the table on disk */
206
0
    if (HADDR_UNDEF == (table_addr = H5MF_alloc(f, H5FD_MEM_SOHM_TABLE, (hsize_t)table->table_size)))
207
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTALLOC, FAIL, "file allocation failed for SOHM table");
208
209
    /* Cache the new table */
210
0
    if (H5AC_insert_entry(f, H5AC_SOHM_TABLE, table_addr, table, H5AC__NO_FLAGS_SET) < 0)
211
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTINS, FAIL, "can't add SOHM table to cache");
212
213
    /* Record the address of the master table in the file */
214
0
    H5F_SET_SOHM_ADDR(f, table_addr);
215
216
    /* Check for sharing attributes in this file, which means that creation
217
     *  indices must be tracked on object header message in the file.
218
     */
219
0
    if (type_flags_used & H5O_SHMESG_ATTR_FLAG)
220
0
        H5F_SET_STORE_MSG_CRT_IDX(f, true);
221
222
    /* Set the ring type to superblock extension */
223
0
    H5AC_set_ring(H5AC_RING_SBE, NULL);
224
225
    /* Write shared message information to the superblock extension */
226
0
    sohm_table.addr     = H5F_SOHM_ADDR(f);
227
0
    sohm_table.version  = H5F_SOHM_VERS(f);
228
0
    sohm_table.nindexes = H5F_SOHM_NINDEXES(f);
229
0
    if (H5O_msg_create(ext_loc, H5O_SHMESG_ID, H5O_MSG_FLAG_CONSTANT | H5O_MSG_FLAG_DONTSHARE,
230
0
                       H5O_UPDATE_TIME, &sohm_table) < 0)
231
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTINIT, FAIL, "unable to update SOHM header message");
232
233
0
done:
234
    /* Reset the ring in the API context */
235
0
    if (orig_ring != H5AC_RING_INV)
236
0
        H5AC_set_ring(orig_ring, NULL);
237
238
0
    if (ret_value < 0) {
239
0
        if (table_addr != HADDR_UNDEF)
240
0
            H5MF_xfree(f, H5FD_MEM_SOHM_TABLE, table_addr, (hsize_t)table->table_size);
241
0
        if (table != NULL)
242
0
            table = H5FL_FREE(H5SM_master_table_t, table);
243
0
    } /* end if */
244
245
0
    FUNC_LEAVE_NOAPI_TAG(ret_value)
246
0
} /* end H5SM_init() */
247
248
/*-------------------------------------------------------------------------
249
 * Function:    H5SM__type_to_flag
250
 *
251
 * Purpose:     Get the shared message flag for a given message type.
252
 *
253
 * Return:      Non-negative on success/Negative on failure
254
 *
255
 *-------------------------------------------------------------------------
256
 */
257
static herr_t
258
H5SM__type_to_flag(unsigned type_id, unsigned *type_flag)
259
0
{
260
0
    herr_t ret_value = SUCCEED; /* Return value */
261
262
0
    FUNC_ENTER_PACKAGE
263
264
    /* Translate the H5O type_id into an H5SM type flag */
265
0
    switch (type_id) {
266
0
        case H5O_FILL_ID:
267
0
            type_id = H5O_FILL_NEW_ID;
268
            /* FALLTHROUGH */
269
0
            H5_ATTR_FALLTHROUGH
270
271
0
        case H5O_SDSPACE_ID:
272
0
        case H5O_DTYPE_ID:
273
0
        case H5O_FILL_NEW_ID:
274
0
        case H5O_PLINE_ID:
275
0
        case H5O_ATTR_ID:
276
0
            *type_flag = (unsigned)1 << type_id;
277
0
            break;
278
279
0
        default:
280
0
            HGOTO_ERROR(H5E_SOHM, H5E_BADTYPE, FAIL, "unknown message type ID");
281
0
    } /* end switch */
282
283
0
done:
284
0
    FUNC_LEAVE_NOAPI(ret_value)
285
0
} /* end H5SM__type_to_flag() */
286
287
/*-------------------------------------------------------------------------
288
 * Function:    H5SM__get_index
289
 *
290
 * Purpose:     Get the index number for a given message type.
291
 *
292
 *              Returns the number of the index in the supplied table
293
 *              that holds messages of type type_id, or negative if
294
 *              there is no index for this message type.
295
 *
296
 * Return:      Non-negative on success/Negative on failure
297
 *
298
 *-------------------------------------------------------------------------
299
 */
300
herr_t
301
H5SM__get_index(const H5SM_master_table_t *table, unsigned type_id, ssize_t *idx)
302
0
{
303
0
    unsigned type_flag;
304
0
    ssize_t  found_index = -1;
305
0
    herr_t   ret_value   = SUCCEED; /* Return value */
306
307
0
    FUNC_ENTER_PACKAGE
308
309
    /* Translate the H5O type_id into an H5SM type flag */
310
0
    if (H5SM__type_to_flag(type_id, &type_flag) < 0)
311
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTGET, FAIL, "can't map message type to flag");
312
313
    /* Search the indexes until we find one that matches this flag or we've
314
     * searched them all.
315
     */
316
0
    for (size_t x = 0; x < table->num_indexes; ++x)
317
0
        if (table->indexes[x].mesg_types & type_flag) {
318
0
            found_index = (ssize_t)x;
319
0
            break;
320
0
        }
321
322
    /* Set output parameter */
323
0
    *idx = found_index;
324
325
0
done:
326
0
    FUNC_LEAVE_NOAPI(ret_value)
327
0
} /* end H5SM__get_index() */
328
329
/*-------------------------------------------------------------------------
330
 * Function:    H5SM_type_shared
331
 *
332
 * Purpose:     Check if a given message type is shared in a file.
333
 *
334
 * Return:      Non-negative on success/Negative on failure
335
 *
336
 *-------------------------------------------------------------------------
337
 */
338
htri_t
339
H5SM_type_shared(H5F_t *f, unsigned type_id)
340
0
{
341
0
    H5SM_master_table_t *table = NULL;      /* Shared object master table */
342
0
    unsigned             type_flag;         /* Flag corresponding to message type */
343
0
    size_t               u;                 /* Local index variable */
344
0
    htri_t               ret_value = false; /* Return value */
345
346
0
    FUNC_ENTER_NOAPI_NOINIT_TAG(H5AC__SOHM_TAG)
347
348
    /* Translate the H5O type_id into an H5SM type flag */
349
0
    if (H5SM__type_to_flag(type_id, &type_flag) < 0)
350
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTGET, FAIL, "can't map message type to flag");
351
352
    /* Look up the master SOHM table */
353
0
    if (H5_addr_defined(H5F_SOHM_ADDR(f))) {
354
0
        H5SM_table_cache_ud_t cache_udata; /* User-data for callback */
355
356
        /* Set up user data for callback */
357
0
        cache_udata.f = f;
358
359
0
        if (NULL == (table = (H5SM_master_table_t *)H5AC_protect(f, H5AC_SOHM_TABLE, H5F_SOHM_ADDR(f),
360
0
                                                                 &cache_udata, H5AC__READ_ONLY_FLAG)))
361
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTPROTECT, FAIL, "unable to load SOHM master table");
362
0
    } /* end if */
363
0
    else
364
        /* No shared messages of any type */
365
0
        HGOTO_DONE(false);
366
367
    /* Search the indexes until we find one that matches this flag or we've
368
     * searched them all.
369
     */
370
0
    for (u = 0; u < table->num_indexes; u++)
371
0
        if (table->indexes[u].mesg_types & type_flag)
372
0
            HGOTO_DONE(true);
373
374
0
done:
375
    /* Release the master SOHM table */
376
0
    if (table && H5AC_unprotect(f, H5AC_SOHM_TABLE, H5F_SOHM_ADDR(f), table, H5AC__NO_FLAGS_SET) < 0)
377
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTUNPROTECT, FAIL, "unable to close SOHM master table");
378
379
0
    FUNC_LEAVE_NOAPI_TAG(ret_value)
380
0
} /* end H5SM_type_shared() */
381
382
/*-------------------------------------------------------------------------
383
 * Function:    H5SM_get_fheap_addr
384
 *
385
 * Purpose:     Gets the address of the fractal heap used to store
386
 *              messages of type type_id.
387
 *
388
 * Return:      Non-negative on success/negative on failure
389
 *
390
 *-------------------------------------------------------------------------
391
 */
392
herr_t
393
H5SM_get_fheap_addr(H5F_t *f, unsigned type_id, haddr_t *fheap_addr)
394
1
{
395
1
    H5SM_master_table_t  *table = NULL;        /* Shared object master table */
396
1
    H5SM_table_cache_ud_t cache_udata;         /* User-data for callback */
397
1
    ssize_t               index_num;           /* Which index */
398
1
    herr_t                ret_value = SUCCEED; /* Return value */
399
400
1
    FUNC_ENTER_NOAPI_TAG(H5AC__SOHM_TAG, FAIL)
401
402
    /* Sanity checks */
403
1
    assert(f);
404
1
    assert(fheap_addr);
405
406
    /* Set up user data for callback */
407
1
    cache_udata.f = f;
408
409
    /* Look up the master SOHM table */
410
1
    if (NULL == (table = (H5SM_master_table_t *)H5AC_protect(f, H5AC_SOHM_TABLE, H5F_SOHM_ADDR(f),
411
1
                                                             &cache_udata, H5AC__READ_ONLY_FLAG)))
412
1
        HGOTO_ERROR(H5E_SOHM, H5E_CANTPROTECT, FAIL, "unable to load SOHM master table");
413
414
    /* Look up index for message type */
415
0
    if (H5SM__get_index(table, type_id, &index_num) < 0)
416
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTGET, FAIL, "unable to check for SOHM index");
417
0
    if (index_num < 0)
418
0
        HGOTO_ERROR(H5E_SOHM, H5E_NOTFOUND, FAIL, "unable to find correct SOHM index");
419
420
    /* Retrieve heap address for index */
421
0
    *fheap_addr = table->indexes[index_num].heap_addr;
422
423
1
done:
424
    /* Release the master SOHM table */
425
1
    if (table && H5AC_unprotect(f, H5AC_SOHM_TABLE, H5F_SOHM_ADDR(f), table, H5AC__NO_FLAGS_SET) < 0)
426
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTUNPROTECT, FAIL, "unable to close SOHM master table");
427
428
1
    FUNC_LEAVE_NOAPI_TAG(ret_value)
429
1
} /* end H5SM_get_fheap_addr() */
430
431
/*-------------------------------------------------------------------------
432
 * Function:    H5SM__create_index
433
 *
434
 * Purpose:     Allocates storage for an index, populating the HEADER struct.
435
 *
436
 * Return:      Non-negative on success/negative on failure
437
 *
438
 *-------------------------------------------------------------------------
439
 */
440
static herr_t
441
H5SM__create_index(H5F_t *f, H5SM_index_header_t *header)
442
0
{
443
0
    H5HF_create_t fheap_cparam;     /* Fractal heap creation parameters */
444
0
    H5HF_t       *fheap     = NULL; /* Fractal heap handle */
445
0
    H5B2_t       *bt2       = NULL; /* v2 B-tree handle for index */
446
0
    herr_t        ret_value = SUCCEED;
447
448
0
    FUNC_ENTER_PACKAGE
449
450
    /* Sanity check */
451
0
    assert(header);
452
0
    assert(header->index_addr == HADDR_UNDEF);
453
0
    assert(header->btree_min <= header->list_max + 1);
454
455
    /* In most cases, the index starts as a list */
456
0
    if (header->list_max > 0) {
457
0
        haddr_t list_addr = HADDR_UNDEF; /* Address of SOHM list */
458
459
        /* Create the list index */
460
0
        if (HADDR_UNDEF == (list_addr = H5SM__create_list(f, header)))
461
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTCREATE, FAIL, "list creation failed for SOHM index");
462
463
        /* Set the index type & address */
464
0
        header->index_type = H5SM_LIST;
465
0
        header->index_addr = list_addr;
466
0
    } /* end if */
467
    /* index is a B-tree */
468
0
    else {
469
0
        H5B2_create_t bt2_cparam;              /* v2 B-tree creation parameters */
470
0
        haddr_t       tree_addr = HADDR_UNDEF; /* Address of SOHM B-tree */
471
472
        /* Create the v2 B-tree index */
473
0
        bt2_cparam.cls           = H5SM_INDEX;
474
0
        bt2_cparam.node_size     = (uint32_t)H5SM_B2_NODE_SIZE;
475
0
        bt2_cparam.rrec_size     = (uint32_t)H5SM_SOHM_ENTRY_SIZE(f);
476
0
        bt2_cparam.split_percent = H5SM_B2_SPLIT_PERCENT;
477
0
        bt2_cparam.merge_percent = H5SM_B2_MERGE_PERCENT;
478
0
        if (NULL == (bt2 = H5B2_create(f, &bt2_cparam, f)))
479
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTCREATE, FAIL, "B-tree creation failed for SOHM index");
480
481
        /* Retrieve the v2 B-tree's address in the file */
482
0
        if (H5B2_get_addr(bt2, &tree_addr) < 0)
483
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTGET, FAIL, "can't get v2 B-tree address for SOHM index");
484
485
        /* Set the index type & address */
486
0
        header->index_type = H5SM_BTREE;
487
0
        header->index_addr = tree_addr;
488
0
    } /* end else */
489
490
    /* Create a heap to hold the shared messages that the list or B-tree will index */
491
0
    memset(&fheap_cparam, 0, sizeof(fheap_cparam));
492
0
    fheap_cparam.managed.width            = H5O_FHEAP_MAN_WIDTH;
493
0
    fheap_cparam.managed.start_block_size = H5O_FHEAP_MAN_START_BLOCK_SIZE;
494
0
    fheap_cparam.managed.max_direct_size  = H5O_FHEAP_MAN_MAX_DIRECT_SIZE;
495
0
    fheap_cparam.managed.max_index        = H5O_FHEAP_MAN_MAX_INDEX;
496
0
    fheap_cparam.managed.start_root_rows  = H5O_FHEAP_MAN_START_ROOT_ROWS;
497
0
    fheap_cparam.checksum_dblocks         = H5O_FHEAP_CHECKSUM_DBLOCKS;
498
0
    fheap_cparam.id_len                   = 0;
499
0
    fheap_cparam.max_man_size             = H5O_FHEAP_MAX_MAN_SIZE;
500
0
    if (NULL == (fheap = H5HF_create(f, &fheap_cparam)))
501
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTINIT, FAIL, "unable to create fractal heap");
502
503
0
    if (H5HF_get_heap_addr(fheap, &(header->heap_addr)) < 0)
504
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTGETSIZE, FAIL, "can't get fractal heap address");
505
506
#ifndef NDEBUG
507
    {
508
        size_t fheap_id_len; /* Size of a fractal heap ID */
509
510
        /* Sanity check ID length */
511
        if (H5HF_get_id_len(fheap, &fheap_id_len) < 0)
512
            HGOTO_ERROR(H5E_SOHM, H5E_CANTGETSIZE, FAIL, "can't get fractal heap ID length");
513
        assert(fheap_id_len == H5O_FHEAP_ID_LEN);
514
    }
515
#endif /* NDEBUG */
516
517
0
done:
518
    /* Release resources */
519
0
    if (fheap && H5HF_close(fheap) < 0)
520
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTCLOSEOBJ, FAIL, "can't close fractal heap");
521
0
    if (bt2 && H5B2_close(bt2) < 0)
522
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTCLOSEOBJ, FAIL, "can't close v2 B-tree for SOHM index");
523
524
0
    FUNC_LEAVE_NOAPI(ret_value)
525
0
} /* end H5SM__create_index */
526
527
/*-------------------------------------------------------------------------
528
 * Function:    H5SM__delete_index
529
 *
530
 * Purpose:     De-allocates storage for an index whose header is HEADER.
531
 *
532
 *              If DELETE_HEAP is true, deletes the index's heap, eliminating
533
 *              it completely.
534
 *
535
 *              If DELETE_HEAP is false, the heap is not deleted.  This is
536
 *              useful when deleting only the index header as the index is
537
 *              converted from a list to a B-tree and back again.
538
 *
539
 * Return:      Non-negative on success/negative on failure
540
 *
541
 *-------------------------------------------------------------------------
542
 */
543
static herr_t
544
H5SM__delete_index(H5F_t *f, H5SM_index_header_t *header, bool delete_heap)
545
0
{
546
0
    herr_t ret_value = SUCCEED; /* Return value */
547
548
0
    FUNC_ENTER_PACKAGE
549
550
    /* Determine whether index is a list or a B-tree. */
551
0
    if (header->index_type == H5SM_LIST) {
552
0
        unsigned index_status = 0; /* Index list's status in the metadata cache */
553
554
        /* Check the index list's status in the metadata cache */
555
0
        if (H5AC_get_entry_status(f, header->index_addr, &index_status) < 0)
556
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTGET, FAIL,
557
0
                        "unable to check metadata cache status for direct block");
558
559
        /* If the index list is in the cache, expunge it now */
560
0
        if (index_status & H5AC_ES__IN_CACHE) {
561
            /* Sanity checks on index list */
562
0
            assert(!(index_status & H5AC_ES__IS_PINNED));
563
0
            assert(!(index_status & H5AC_ES__IS_PROTECTED));
564
565
            /* Evict the index list from the metadata cache */
566
0
            if (H5AC_expunge_entry(f, H5AC_SOHM_LIST, header->index_addr, H5AC__FREE_FILE_SPACE_FLAG) < 0)
567
0
                HGOTO_ERROR(H5E_SOHM, H5E_CANTREMOVE, FAIL, "unable to remove list index from cache");
568
0
        } /* end if */
569
0
    }     /* end if */
570
0
    else {
571
0
        assert(header->index_type == H5SM_BTREE);
572
573
        /* Delete the B-tree. */
574
0
        if (H5B2_delete(f, header->index_addr, f, NULL, NULL) < 0)
575
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTDELETE, FAIL, "unable to delete B-tree");
576
577
        /* Revert to list unless B-trees can have zero records */
578
0
        if (header->btree_min > 0)
579
0
            header->index_type = H5SM_LIST;
580
0
    } /* end else */
581
582
    /* Free the index's heap if requested. */
583
0
    if (delete_heap == true) {
584
0
        if (H5HF_delete(f, header->heap_addr) < 0)
585
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTDELETE, FAIL, "unable to delete fractal heap");
586
0
        header->heap_addr = HADDR_UNDEF;
587
0
    } /* end if */
588
589
    /* Reset index info */
590
0
    header->index_addr   = HADDR_UNDEF;
591
0
    header->num_messages = 0;
592
593
0
done:
594
0
    FUNC_LEAVE_NOAPI(ret_value)
595
0
} /* end H5SM__delete_index */
596
597
/*-------------------------------------------------------------------------
598
 * Function:    H5SM__create_list
599
 *
600
 * Purpose:     Creates a list of SOHM messages.
601
 *
602
 *              Called when a new index is created from scratch or when a
603
 *              B-tree needs to be converted back into a list.
604
 *
605
 * Return:      Non-negative on success/Negative on failure
606
 *
607
 *-------------------------------------------------------------------------
608
 */
609
static haddr_t
610
H5SM__create_list(H5F_t *f, H5SM_index_header_t *header)
611
0
{
612
0
    H5SM_list_t *list = NULL;             /* List of messages */
613
0
    hsize_t      x;                       /* Counter variable */
614
0
    size_t       num_entries;             /* Number of messages to create in list */
615
0
    haddr_t      addr      = HADDR_UNDEF; /* Address of the list on disk */
616
0
    haddr_t      ret_value = HADDR_UNDEF; /* Return value */
617
618
0
    FUNC_ENTER_PACKAGE
619
620
0
    assert(f);
621
0
    assert(header);
622
623
0
    num_entries = header->list_max;
624
625
    /* Allocate list in memory */
626
0
    if (NULL == (list = H5FL_CALLOC(H5SM_list_t)))
627
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTALLOC, HADDR_UNDEF, "file allocation failed for SOHM list");
628
0
    if (NULL == (list->messages = (H5SM_sohm_t *)H5FL_ARR_CALLOC(H5SM_sohm_t, num_entries)))
629
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTALLOC, HADDR_UNDEF, "file allocation failed for SOHM list");
630
631
    /* Initialize messages in list */
632
0
    for (x = 0; x < num_entries; x++)
633
0
        list->messages[x].location = H5SM_NO_LOC;
634
635
    /* Point list at header passed in */
636
0
    list->header = header;
637
638
    /* Allocate space for the list on disk */
639
0
    if (HADDR_UNDEF == (addr = H5MF_alloc(f, H5FD_MEM_SOHM_INDEX, (hsize_t)header->list_size)))
640
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTALLOC, HADDR_UNDEF, "file allocation failed for SOHM list");
641
642
    /* Put the list into the cache */
643
0
    if (H5AC_insert_entry(f, H5AC_SOHM_LIST, addr, list, H5AC__NO_FLAGS_SET) < 0)
644
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTINS, HADDR_UNDEF, "can't add SOHM list to cache");
645
646
    /* Set return value */
647
0
    ret_value = addr;
648
649
0
done:
650
0
    if (ret_value == HADDR_UNDEF) {
651
0
        if (list != NULL) {
652
0
            if (list->messages != NULL)
653
0
                list->messages = H5FL_ARR_FREE(H5SM_sohm_t, list->messages);
654
0
            list = H5FL_FREE(H5SM_list_t, list);
655
0
        } /* end if */
656
0
        if (addr != HADDR_UNDEF)
657
0
            H5MF_xfree(f, H5FD_MEM_SOHM_INDEX, addr, (hsize_t)header->list_size);
658
0
    } /* end if */
659
660
0
    FUNC_LEAVE_NOAPI(ret_value)
661
0
} /* end H5SM__create_list */
662
663
/*-------------------------------------------------------------------------
664
 * Function:    H5SM__convert_list_to_btree
665
 *
666
 * Purpose:     Given a list index, turns it into a B-tree index.  This is
667
 *              done when too many messages are added to the list.
668
 *
669
 *              Requires that *_LIST be a valid list and currently protected
670
 *              in the cache.  Unprotects (and expunges) *_LIST from the cache.
671
 *
672
 *              _LIST needs to be a double pointer so that the calling function
673
 *              knows if it is released from the cache if this function exits
674
 *              in error.  Trying to free it again will trigger an assert.
675
 *
676
 * Return:      Non-negative on success
677
 *              Negative on failure
678
 *
679
 *-------------------------------------------------------------------------
680
 */
681
static herr_t
682
H5SM__convert_list_to_btree(H5F_t *f, H5SM_index_header_t *header, H5SM_list_t **_list, H5HF_t *fheap,
683
                            H5O_t *open_oh)
684
0
{
685
0
    H5SM_list_t    *list;         /* Pointer to the existing message list */
686
0
    H5SM_mesg_key_t key;          /* Key for inserting records in v2 B-tree */
687
0
    H5B2_create_t   bt2_cparam;   /* v2 B-tree creation parameters */
688
0
    H5B2_t         *bt2 = NULL;   /* v2 B-tree handle for index */
689
0
    haddr_t         tree_addr;    /* New v2 B-tree's address */
690
0
    size_t          num_messages; /* Number of messages being tracked */
691
0
    size_t          x;
692
0
    void           *encoding_buf = NULL;
693
0
    herr_t          ret_value    = SUCCEED; /* Return value */
694
695
0
    FUNC_ENTER_PACKAGE
696
697
0
    assert(_list && *_list);
698
0
    assert(header);
699
700
    /* Get pointer to list of messages to convert */
701
0
    list = *_list;
702
703
    /* Create the new v2 B-tree for tracking the messages */
704
0
    bt2_cparam.cls           = H5SM_INDEX;
705
0
    bt2_cparam.node_size     = (uint32_t)H5SM_B2_NODE_SIZE;
706
0
    bt2_cparam.rrec_size     = (uint32_t)H5SM_SOHM_ENTRY_SIZE(f);
707
0
    bt2_cparam.split_percent = H5SM_B2_SPLIT_PERCENT;
708
0
    bt2_cparam.merge_percent = H5SM_B2_MERGE_PERCENT;
709
0
    if (NULL == (bt2 = H5B2_create(f, &bt2_cparam, f)))
710
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTCREATE, FAIL, "B-tree creation failed for SOHM index");
711
712
    /* Retrieve the v2 B-tree's address in the file */
713
0
    if (H5B2_get_addr(bt2, &tree_addr) < 0)
714
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTGET, FAIL, "can't get v2 B-tree address for SOHM index");
715
716
    /* Set up key values that all messages will use.  Since these messages
717
     * are in the heap, they have a heap ID and no encoding or type_id.
718
     */
719
0
    key.file          = f;
720
0
    key.fheap         = fheap;
721
0
    key.encoding_size = 0;
722
0
    key.encoding      = NULL;
723
724
    /* Insert each record into the new B-tree */
725
0
    for (x = 0; x < header->list_max; x++) {
726
0
        if (list->messages[x].location != H5SM_NO_LOC) {
727
            /* Copy message into key */
728
0
            key.message = list->messages[x];
729
730
            /* Get the encoded message */
731
0
            if (H5SM__read_mesg(f, &(key.message), fheap, open_oh, &key.encoding_size, &encoding_buf) < 0)
732
0
                HGOTO_ERROR(H5E_SOHM, H5E_CANTLOAD, FAIL, "Couldn't read SOHM message in list");
733
734
0
            key.encoding = encoding_buf;
735
736
            /* Insert the message into the B-tree */
737
0
            if (H5B2_insert(bt2, &key) < 0)
738
0
                HGOTO_ERROR(H5E_SOHM, H5E_CANTINSERT, FAIL, "couldn't add SOHM to B-tree");
739
740
            /* Free buffer from H5SM__read_mesg */
741
0
            if (encoding_buf)
742
0
                encoding_buf = H5MM_xfree(encoding_buf);
743
0
        } /* end if */
744
0
    }     /* end for */
745
746
    /* Unprotect list in cache and release heap */
747
0
    if (H5AC_unprotect(f, H5AC_SOHM_LIST, header->index_addr, list,
748
0
                       H5AC__DELETED_FLAG | H5AC__FREE_FILE_SPACE_FLAG) < 0)
749
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTUNPROTECT, FAIL, "unable to release SOHM list");
750
0
    *_list = list = NULL;
751
752
    /* Delete the old list index (but not its heap, which the new index is
753
     * still using!)
754
     */
755
0
    num_messages = header->num_messages; /* preserve this across the index deletion */
756
0
    if (H5SM__delete_index(f, header, false) < 0)
757
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTDELETE, FAIL, "can't free list index");
758
759
    /* Set/restore header info */
760
0
    header->index_addr   = tree_addr;
761
0
    header->index_type   = H5SM_BTREE;
762
0
    header->num_messages = num_messages;
763
764
0
done:
765
    /* Release resources */
766
0
    if (bt2 && H5B2_close(bt2) < 0)
767
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTCLOSEOBJ, FAIL, "can't close v2 B-tree for SOHM index");
768
0
    if (encoding_buf)
769
0
        encoding_buf = H5MM_xfree(encoding_buf);
770
771
0
    FUNC_LEAVE_NOAPI(ret_value)
772
0
} /* H5SM__convert_list_to_btree() */
773
774
/*-------------------------------------------------------------------------
775
 * Function:  H5SM__bt2_convert_to_list_op
776
 *
777
 * Purpose: An H5B2_remove_t callback function to convert a SOHM
778
 *              B-tree index to a list.
779
 *
780
 *              Inserts this record into the list passed through op_data.
781
 *
782
 * Return:  Non-negative on success
783
 *              Negative on failure
784
 *
785
 *-------------------------------------------------------------------------
786
 */
787
static herr_t
788
H5SM__bt2_convert_to_list_op(const void *record, void *op_data)
789
0
{
790
0
    const H5SM_sohm_t *message = (const H5SM_sohm_t *)record;
791
0
    const H5SM_list_t *list    = (const H5SM_list_t *)op_data;
792
0
    size_t             mesg_idx; /* Index of message to modify */
793
794
0
    FUNC_ENTER_PACKAGE_NOERR
795
796
    /* Sanity checks */
797
0
    assert(record);
798
0
    assert(op_data);
799
800
    /* Get the message index, and increment the # of messages in list */
801
0
    mesg_idx = list->header->num_messages++;
802
0
    assert(list->header->num_messages <= list->header->list_max);
803
804
    /* Insert this message at the end of the list */
805
0
    assert(list->messages[mesg_idx].location == H5SM_NO_LOC);
806
0
    assert(message->location != H5SM_NO_LOC);
807
0
    H5MM_memcpy(&(list->messages[mesg_idx]), message, sizeof(H5SM_sohm_t));
808
809
0
    FUNC_LEAVE_NOAPI(SUCCEED)
810
0
} /* end H5SM__bt2_convert_to_list_op() */
811
812
/*-------------------------------------------------------------------------
813
 * Function:    H5SM__convert_btree_to_list
814
 *
815
 * Purpose:     Given a B-tree index, turns it into a list index.  This is
816
 *              done when too many messages are deleted from the B-tree.
817
 *
818
 * Return:      Non-negative on success
819
 *              Negative on failure
820
 *
821
 *-------------------------------------------------------------------------
822
 */
823
static herr_t
824
H5SM__convert_btree_to_list(H5F_t *f, H5SM_index_header_t *header)
825
0
{
826
0
    H5SM_list_t         *list = NULL;
827
0
    H5SM_list_cache_ud_t cache_udata; /* User-data for metadata cache callback */
828
0
    haddr_t              btree_addr;
829
0
    herr_t               ret_value = SUCCEED;
830
831
0
    FUNC_ENTER_PACKAGE
832
833
    /* Remember the address of the old B-tree, but change the header over to be
834
     * a list..
835
     */
836
0
    btree_addr = header->index_addr;
837
838
0
    header->num_messages = 0;
839
0
    header->index_type   = H5SM_LIST;
840
841
    /* Create a new list index */
842
0
    if (HADDR_UNDEF == (header->index_addr = H5SM__create_list(f, header)))
843
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTINIT, FAIL, "unable to create shared message list");
844
845
    /* Set up user data for metadata cache callback */
846
0
    cache_udata.f      = f;
847
0
    cache_udata.header = header;
848
849
    /* Protect the SOHM list */
850
0
    if (NULL == (list = (H5SM_list_t *)H5AC_protect(f, H5AC_SOHM_LIST, header->index_addr, &cache_udata,
851
0
                                                    H5AC__NO_FLAGS_SET)))
852
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTPROTECT, FAIL, "unable to load SOHM list index");
853
854
    /* Delete the B-tree and have messages copy themselves to the
855
     * list as they're deleted
856
     */
857
0
    if (H5B2_delete(f, btree_addr, f, H5SM__bt2_convert_to_list_op, list) < 0)
858
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTDELETE, FAIL, "unable to delete B-tree");
859
860
0
done:
861
    /* Release the SOHM list from the cache */
862
0
    if (list && H5AC_unprotect(f, H5AC_SOHM_LIST, header->index_addr, list, H5AC__DIRTIED_FLAG) < 0)
863
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTUNPROTECT, FAIL, "unable to unprotect SOHM index");
864
865
0
    FUNC_LEAVE_NOAPI(ret_value)
866
0
} /* end H5SM__convert_btree_to_list() */
867
868
/*-------------------------------------------------------------------------
869
 * Function:    H5SM__can_share_common
870
 *
871
 * Purpose:     "trivial" checks for determining if a message can be shared.
872
 *
873
 * Note:  These checks are common to the "can share" and "try share"
874
 *    routines and are the "fast" checks before we need to protect
875
 *    the SOHM master table.
876
 *
877
 * Return:      true if message could be a SOHM
878
 *              false if this message couldn't be a SOHM
879
 *              Negative on failure
880
 *
881
 *-------------------------------------------------------------------------
882
 */
883
static htri_t
884
H5SM__can_share_common(const H5F_t *f, unsigned type_id, const void *mesg)
885
0
{
886
0
    htri_t ret_value = FAIL; /* Return value */
887
888
0
    FUNC_ENTER_PACKAGE
889
890
    /* Check whether this message ought to be shared or not */
891
    /* If sharing is disabled in this file, don't share the message */
892
0
    if (!H5_addr_defined(H5F_SOHM_ADDR(f)))
893
0
        HGOTO_DONE(false);
894
895
    /* Type-specific check */
896
0
    if ((ret_value = H5O_msg_can_share(type_id, mesg)) < 0)
897
0
        HGOTO_ERROR(H5E_SOHM, H5E_BADTYPE, FAIL, "can_share callback returned error");
898
0
    if (ret_value == false)
899
0
        HGOTO_DONE(false);
900
901
    /* At this point, the message passes the "trivial" checks and is worth
902
     *  further checks.
903
     */
904
905
0
done:
906
0
    FUNC_LEAVE_NOAPI(ret_value)
907
0
} /* end H5SM__can_share_common() */
908
909
/*-------------------------------------------------------------------------
910
 * Function:    H5SM_can_share
911
 *
912
 * Purpose:     Checks if an object header message would be shared or is
913
 *    already shared.
914
 *
915
 *              If not, returns false and does nothing.
916
 *
917
 * Return:      true if message will be a SOHM
918
 *              false if this message won't be a SOHM
919
 *              Negative on failure
920
 *
921
 *-------------------------------------------------------------------------
922
 */
923
htri_t
924
H5SM_can_share(H5F_t *f, H5SM_master_table_t *table, ssize_t *sohm_index_num, unsigned type_id,
925
               const void *mesg)
926
0
{
927
0
    size_t               mesg_size;
928
0
    H5SM_master_table_t *my_table = NULL;
929
0
    ssize_t              index_num;
930
0
    htri_t               tri_ret;
931
0
    htri_t               ret_value = true;
932
933
0
    FUNC_ENTER_NOAPI_TAG(H5AC__SOHM_TAG, FAIL)
934
935
    /* "trivial" sharing checks */
936
0
    if ((tri_ret = H5SM__can_share_common(f, type_id, mesg)) < 0)
937
0
        HGOTO_ERROR(H5E_SOHM, H5E_BADTYPE, FAIL, "'trivial' sharing checks returned error");
938
0
    if (tri_ret == false)
939
0
        HGOTO_DONE(false);
940
941
    /* Look up the master SOHM table */
942
    /* (use incoming master SOHM table if possible) */
943
0
    if (table)
944
0
        my_table = table;
945
0
    else {
946
0
        H5SM_table_cache_ud_t cache_udata; /* User-data for callback */
947
948
        /* Set up user data for callback */
949
0
        cache_udata.f = f;
950
951
0
        if (NULL == (my_table = (H5SM_master_table_t *)H5AC_protect(f, H5AC_SOHM_TABLE, H5F_SOHM_ADDR(f),
952
0
                                                                    &cache_udata, H5AC__READ_ONLY_FLAG)))
953
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTPROTECT, FAIL, "unable to load SOHM master table");
954
0
    } /* end if */
955
956
    /* Find the right index for this message type.  If there is no such index
957
     * then this type of message isn't shareable
958
     */
959
0
    if (H5SM__get_index(my_table, type_id, &index_num) < 0)
960
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTGET, FAIL, "unable to check for SOHM index");
961
0
    if (index_num < 0)
962
0
        HGOTO_DONE(false);
963
964
    /* If the message isn't big enough, don't bother sharing it */
965
0
    if (0 == (mesg_size = H5O_msg_raw_size(f, type_id, true, mesg)))
966
0
        HGOTO_ERROR(H5E_SOHM, H5E_BADMESG, FAIL, "unable to get OH message size");
967
0
    if (mesg_size < my_table->indexes[index_num].min_mesg_size)
968
0
        HGOTO_DONE(false);
969
970
    /* At this point, the message will be shared, set the index number if requested. */
971
0
    if (sohm_index_num)
972
0
        *sohm_index_num = index_num;
973
974
0
done:
975
    /* Release the master SOHM table, if we protected it */
976
0
    if (my_table && my_table != table &&
977
0
        H5AC_unprotect(f, H5AC_SOHM_TABLE, H5F_SOHM_ADDR(f), my_table, H5AC__NO_FLAGS_SET) < 0)
978
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTUNPROTECT, FAIL, "unable to close SOHM master table");
979
980
0
    FUNC_LEAVE_NOAPI_TAG(ret_value)
981
0
} /* end H5SM_can_share() */
982
983
/*-------------------------------------------------------------------------
984
 * Function:    H5SM_try_share
985
 *
986
 * Purpose:     Attempts to share an object header message.
987
 *
988
 *              MESG_LOC is an H5O_mesg_loc_t struct that gives the message's
989
 *              location in an object header (address and index).  This
990
 *              function sets the type_id in MESG_LOC.
991
 *              If MESG_LOC is not NULL, this message will be "unique but
992
 *              shareable" and will be entered in the index but not actually
993
 *              shared.  If it is NULL, this message will be fully shared if
994
 *              it is shareable at all.
995
 *
996
 *              OPEN_OH is the object header that is currently open and
997
 *              protected.  If NULL, the SM module will protect any object
998
 *              header it needs (which can cause an error if that OH is
999
 *              already protected!).
1000
 *
1001
 *              DEFER_FLAGS indicates whether the sharing operation should
1002
 *              actually occur, or whether this is just a set up call for a
1003
 *              future sharing operation.  In the latter case this argument
1004
 *              should be H5SM_DEFER.  If the message was previously deferred
1005
 *              this argument should be H5SM_WAS_DEFERRED.
1006
 *
1007
 *              MESG_FLAGS will have the H5O_MSG_FLAG_SHAREABLE or
1008
 *              H5O_MSG_FLAG_SHARED flag set if one is appropriate.  This is
1009
 *              the only way to tell the difference between a message that
1010
 *              has just been fully shared and a message that is only
1011
 *              "shareable" and is still in the object header, so it cannot
1012
 *              be NULL if MESG_LOC is not NULL.  If MESG_LOC is NULL, then
1013
 *              the message won't be "unique but shareable" and MESG_FLAGS
1014
 *              can be NULL as well.
1015
 *
1016
 *              If the message should be shared (if sharing has been
1017
 *              enabled and this message qualifies), this function turns the
1018
 *              message into a shared message, sets the H5O_MSG_FLAG_SHARED
1019
 *              flag in mesg_flags, and returns true.
1020
 *
1021
 *              If the message isn't shared, returns false.  If the message
1022
 *              isn't shared but was entered in the shared message index,
1023
 *              the H5O_MSG_FLAG_SHAREABLE flag will be set in mesg_flags
1024
 *              and returns true.
1025
 *
1026
 *              If this message was already shared, increments its reference
1027
 *              count, and leaves it otherwise unchanged, returning true and
1028
 *              setting the H5O_MSG_FLAG_SHARED flag in mesg_flags.
1029
 *
1030
 *              If mesg_flags is NULL, the calling function should just look
1031
 *              at the return value to determine if the message was shared
1032
 *              or not.  Such messages should never be "unique but shareable."
1033
 *
1034
 * Return:      true if message is now a SOHM
1035
 *              false if this message is not a SOHM
1036
 *              Negative on failure
1037
 *
1038
 *-------------------------------------------------------------------------
1039
 */
1040
htri_t
1041
H5SM_try_share(H5F_t *f, H5O_t *open_oh, unsigned defer_flags, unsigned type_id, void *mesg,
1042
               unsigned *mesg_flags)
1043
18
{
1044
18
    H5SM_master_table_t  *table = NULL;
1045
18
    H5SM_table_cache_ud_t cache_udata; /* User-data for callback */
1046
18
    unsigned              cache_flags = H5AC__NO_FLAGS_SET;
1047
18
    ssize_t               index_num;
1048
18
    htri_t                tri_ret;
1049
#ifndef NDEBUG
1050
    unsigned deferred_type = UINT_MAX;
1051
#endif
1052
18
    htri_t ret_value = true;
1053
1054
18
    FUNC_ENTER_NOAPI_TAG(H5AC__SOHM_TAG, FAIL)
1055
1056
    /* If we previously deferred this operation, the saved message type should
1057
     * be the same as the one we get here.  In debug mode, we make sure this
1058
     * holds true; otherwise we can leave now if it wasn't shared in the DEFER
1059
     * pass. */
1060
18
    if (defer_flags & H5SM_WAS_DEFERRED)
1061
#ifndef NDEBUG
1062
        deferred_type = ((H5O_shared_t *)mesg)->type;
1063
#else  /* NDEBUG */
1064
0
        if ((((H5O_shared_t *)mesg)->type != H5O_SHARE_TYPE_HERE) &&
1065
0
            (((H5O_shared_t *)mesg)->type != H5O_SHARE_TYPE_SOHM))
1066
0
            HGOTO_DONE(false);
1067
18
#endif /* NDEBUG */
1068
1069
    /* "trivial" sharing checks */
1070
18
    if (mesg_flags && (*mesg_flags & H5O_MSG_FLAG_DONTSHARE))
1071
18
        HGOTO_DONE(false);
1072
0
    if ((tri_ret = H5SM__can_share_common(f, type_id, mesg)) < 0)
1073
0
        HGOTO_ERROR(H5E_SOHM, H5E_BADTYPE, FAIL, "'trivial' sharing checks returned error");
1074
0
    if (tri_ret == false)
1075
0
        HGOTO_DONE(false);
1076
1077
    /* Set up user data for callback */
1078
0
    cache_udata.f = f;
1079
1080
    /* Look up the master SOHM table */
1081
0
    if (NULL == (table = (H5SM_master_table_t *)H5AC_protect(f, H5AC_SOHM_TABLE, H5F_SOHM_ADDR(f),
1082
0
                                                             &cache_udata, H5AC__NO_FLAGS_SET)))
1083
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTPROTECT, FAIL, "unable to load SOHM master table");
1084
1085
    /* "complex" sharing checks */
1086
0
    if ((tri_ret = H5SM_can_share(f, table, &index_num, type_id, mesg)) < 0)
1087
0
        HGOTO_ERROR(H5E_SOHM, H5E_BADTYPE, FAIL, "'complex' sharing checks returned error");
1088
0
    if (tri_ret == false)
1089
0
        HGOTO_DONE(false);
1090
1091
    /* At this point, the message will be shared. */
1092
1093
    /* If the index hasn't been allocated yet, create it */
1094
0
    if (table->indexes[index_num].index_addr == HADDR_UNDEF) {
1095
0
        if (H5SM__create_index(f, &(table->indexes[index_num])) < 0)
1096
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTINIT, FAIL, "unable to create SOHM index");
1097
0
        cache_flags |= H5AC__DIRTIED_FLAG;
1098
0
    } /* end if */
1099
1100
    /* Write the message as a shared message.  This may or may not cause the
1101
     * message to become shared (if it is unique, it will not be shared).
1102
     */
1103
0
    if (H5SM__write_mesg(f, open_oh, &(table->indexes[index_num]), (defer_flags & H5SM_DEFER) != 0, type_id,
1104
0
                         mesg, &cache_flags) < 0)
1105
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTINSERT, FAIL, "can't write shared message");
1106
1107
    /* Set flags if this message was "written" without error and wasn't a
1108
     * 'defer' attempt; it is now either fully shared or "shareable".
1109
     */
1110
0
    if (mesg_flags) {
1111
0
        if (((H5O_shared_t *)mesg)->type == H5O_SHARE_TYPE_HERE)
1112
0
            *mesg_flags |= H5O_MSG_FLAG_SHAREABLE;
1113
0
        else {
1114
0
            assert(((H5O_shared_t *)mesg)->type == H5O_SHARE_TYPE_SOHM);
1115
0
            *mesg_flags |= H5O_MSG_FLAG_SHARED;
1116
0
        } /* end else */
1117
0
    }     /* end if */
1118
1119
18
done:
1120
18
    assert((ret_value != true) || ((H5O_shared_t *)mesg)->type == H5O_SHARE_TYPE_HERE ||
1121
18
           ((H5O_shared_t *)mesg)->type == H5O_SHARE_TYPE_SOHM);
1122
#ifndef NDEBUG
1123
    /* If we previously deferred this operation, make sure the saved message
1124
     * type is the same as the one we get here. */
1125
    if (defer_flags & H5SM_WAS_DEFERRED)
1126
        assert(deferred_type == ((H5O_shared_t *)mesg)->type);
1127
#endif /* NDEBUG */
1128
1129
    /* Release the master SOHM table */
1130
18
    if (table && H5AC_unprotect(f, H5AC_SOHM_TABLE, H5F_SOHM_ADDR(f), table, cache_flags) < 0)
1131
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTUNPROTECT, FAIL, "unable to close SOHM master table");
1132
1133
18
    FUNC_LEAVE_NOAPI_TAG(ret_value)
1134
18
} /* end H5SM_try_share() */
1135
1136
/*-------------------------------------------------------------------------
1137
 * Function:  H5SM__incr_ref
1138
 *
1139
 * Purpose: Increment the reference count for a SOHM message and return
1140
 *              the message's heap ID.
1141
 *
1142
 *              The message pointer is actually returned via op_data, which
1143
 *              should be a pointer to a H5SM_fheap_id_t.
1144
 *
1145
 * Return:  Non-negative on success
1146
 *              Negative on failure
1147
 *
1148
 *-------------------------------------------------------------------------
1149
 */
1150
static herr_t
1151
H5SM__incr_ref(void *record, void *_op_data, bool *changed)
1152
0
{
1153
0
    H5SM_sohm_t            *message   = (H5SM_sohm_t *)record;
1154
0
    H5SM_incr_ref_opdata_t *op_data   = (H5SM_incr_ref_opdata_t *)_op_data;
1155
0
    herr_t                  ret_value = SUCCEED;
1156
1157
0
    FUNC_ENTER_PACKAGE
1158
1159
0
    assert(record);
1160
0
    assert(op_data);
1161
0
    assert(changed);
1162
1163
    /* If the message was previously shared in an object header, share
1164
     * it in the heap now.
1165
     */
1166
0
    if (message->location == H5SM_IN_OH) {
1167
0
        assert(op_data->key && op_data->key->fheap);
1168
1169
        /* Put the message in the heap and record its new heap ID */
1170
0
        if (H5HF_insert(op_data->key->fheap, op_data->key->encoding_size, op_data->key->encoding,
1171
0
                        &message->u.heap_loc.fheap_id) < 0)
1172
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTINSERT, FAIL, "unable to insert message into fractal heap");
1173
1174
0
        message->location             = H5SM_IN_HEAP;
1175
0
        message->u.heap_loc.ref_count = 2;
1176
0
    } /* end if */
1177
0
    else {
1178
0
        assert(message->location == H5SM_IN_HEAP);
1179
        /* If it's already in the heap, just increment the ref count */
1180
0
        ++message->u.heap_loc.ref_count;
1181
0
    } /* end else */
1182
1183
    /* If we got here, the message has changed */
1184
0
    *changed = true;
1185
1186
    /* Set the heap ID and indicate it was found */
1187
0
    op_data->fheap_id = message->u.heap_loc.fheap_id;
1188
0
    op_data->found    = true;
1189
1190
0
done:
1191
0
    FUNC_LEAVE_NOAPI(ret_value)
1192
0
} /* end H5SM__incr_ref() */
1193
1194
/*-------------------------------------------------------------------------
1195
 * Function:    H5SM__write_mesg
1196
 *
1197
 * Purpose:     This routine adds a shareable message to an index.
1198
 *              The behavior is controlled by the DEFER parameter:
1199
 *
1200
 *              If DEFER is true, this routine Simulates adding a shareable
1201
 *              message to an index.  It determines what the outcome would
1202
 *              be with DEFER set the false and updates the shared message
1203
 *              info, but does not actually add the message to a heap, list,
1204
 *              or b-tree.  Assumes that an open object header will be
1205
 *              available when H5SM__write_mesg is called with DEFER set to
1206
 *              false.
1207
 *
1208
 *              If DEFER is false, this routine adds a shareable message to
1209
 *              an index.  If this is the first such message and we have an
1210
 *              object header location for this message, we record it in the
1211
 *              index but don't modify the message passed in.  If the message
1212
 *              is already in the index or we don't have an object header
1213
 *              location for it, it is shared in the heap and this function
1214
 *              sets its sharing struct to reflect this.
1215
 *
1216
 *              The index could be a list or a B-tree.
1217
 *
1218
 * Return:      Non-negative on success
1219
 *              Negative on failure
1220
 *
1221
 *-------------------------------------------------------------------------
1222
 */
1223
static herr_t
1224
H5SM__write_mesg(H5F_t *f, H5O_t *open_oh, H5SM_index_header_t *header, bool defer, unsigned type_id,
1225
                 void *mesg, unsigned *cache_flags_ptr)
1226
0
{
1227
0
    H5SM_list_t         *list = NULL;             /* List index */
1228
0
    H5SM_mesg_key_t      key;                     /* Key used to search the index */
1229
0
    H5SM_list_cache_ud_t cache_udata;             /* User-data for metadata cache callback */
1230
0
    H5O_shared_t         shared;                  /* Shared H5O message */
1231
0
    bool                 found = false;           /* Was the message in the index? */
1232
0
    H5HF_t              *fheap = NULL;            /* Fractal heap handle */
1233
0
    H5B2_t              *bt2   = NULL;            /* v2 B-tree handle for index */
1234
0
    size_t               buf_size;                /* Size of the encoded message */
1235
0
    void                *encoding_buf = NULL;     /* Buffer for encoded message */
1236
0
    size_t               empty_pos    = SIZE_MAX; /* Empty entry in list */
1237
0
    herr_t               ret_value    = SUCCEED;
1238
1239
0
    FUNC_ENTER_PACKAGE
1240
1241
    /* Sanity check */
1242
0
    assert(header);
1243
0
    assert(header->index_type != H5SM_BADTYPE);
1244
0
    assert(cache_flags_ptr);
1245
1246
    /* Encode the message to be written */
1247
0
    if ((buf_size = H5O_msg_raw_size(f, type_id, true, mesg)) == 0)
1248
0
        HGOTO_ERROR(H5E_SOHM, H5E_BADSIZE, FAIL, "can't find message size");
1249
0
    if (NULL == (encoding_buf = H5MM_malloc(buf_size)))
1250
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTALLOC, FAIL, "can't allocate buffer for encoding");
1251
0
    if (H5O_msg_encode(f, type_id, true, (unsigned char *)encoding_buf, mesg) < 0)
1252
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTENCODE, FAIL, "can't encode message to be shared");
1253
1254
    /* Open the fractal heap for this index */
1255
0
    if (NULL == (fheap = H5HF_open(f, header->heap_addr)))
1256
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTOPENOBJ, FAIL, "unable to open fractal heap");
1257
1258
    /* Set up a key for the message to be written */
1259
0
    key.file             = f;
1260
0
    key.fheap            = fheap;
1261
0
    key.encoding         = encoding_buf;
1262
0
    key.encoding_size    = buf_size;
1263
0
    key.message.hash     = H5_checksum_lookup3(encoding_buf, buf_size, type_id);
1264
0
    key.message.location = H5SM_NO_LOC;
1265
1266
    /* Assume the message is already in the index and try to increment its
1267
     * reference count.  If this fails, the message isn't in the index after
1268
     * all and we'll need to add it.
1269
     */
1270
0
    if (header->index_type == H5SM_LIST) {
1271
0
        size_t list_pos; /* Position in a list index */
1272
1273
        /* Set up user data for metadata cache callback */
1274
0
        cache_udata.f      = f;
1275
0
        cache_udata.header = header;
1276
1277
        /* The index is a list; get it from the cache */
1278
0
        if (NULL == (list = (H5SM_list_t *)H5AC_protect(f, H5AC_SOHM_LIST, header->index_addr, &cache_udata,
1279
0
                                                        defer ? H5AC__READ_ONLY_FLAG : H5AC__NO_FLAGS_SET)))
1280
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTPROTECT, FAIL, "unable to load SOHM index");
1281
1282
        /* See if the message is already in the index and get its location.
1283
         * Also record the first empty list position we find in case we need it
1284
         * later.
1285
         */
1286
0
        if (H5SM__find_in_list(list, &key, &empty_pos, &list_pos) < 0)
1287
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTINSERT, FAIL, "unable to search for message in list");
1288
1289
0
        if (defer) {
1290
0
            if (list_pos != SIZE_MAX)
1291
0
                found = true;
1292
0
        } /* end if */
1293
0
        else {
1294
0
            if (list_pos != SIZE_MAX) {
1295
                /* If the message was previously shared in an object header, share
1296
                 * it in the heap now.
1297
                 */
1298
0
                if (list->messages[list_pos].location == H5SM_IN_OH) {
1299
                    /* Put the message in the heap and record its new heap ID */
1300
0
                    if (H5HF_insert(fheap, key.encoding_size, key.encoding, &shared.u.heap_id) < 0)
1301
0
                        HGOTO_ERROR(H5E_SOHM, H5E_CANTINSERT, FAIL,
1302
0
                                    "unable to insert message into fractal heap");
1303
1304
0
                    list->messages[list_pos].location             = H5SM_IN_HEAP;
1305
0
                    list->messages[list_pos].u.heap_loc.fheap_id  = shared.u.heap_id;
1306
0
                    list->messages[list_pos].u.heap_loc.ref_count = 2;
1307
0
                } /* end if */
1308
0
                else {
1309
                    /* If the message was already in the heap, increase its ref count */
1310
0
                    assert(list->messages[list_pos].location == H5SM_IN_HEAP);
1311
0
                    ++(list->messages[list_pos].u.heap_loc.ref_count);
1312
0
                } /* end else */
1313
1314
                /* Set up the shared location to point to the shared location */
1315
0
                shared.u.heap_id = list->messages[list_pos].u.heap_loc.fheap_id;
1316
0
                found            = true;
1317
0
            } /* end if */
1318
0
        }     /* end else */
1319
0
    }         /* end if */
1320
    /* Index is a B-tree */
1321
0
    else {
1322
0
        assert(header->index_type == H5SM_BTREE);
1323
1324
        /* Open the index v2 B-tree */
1325
0
        if (NULL == (bt2 = H5B2_open(f, header->index_addr, f)))
1326
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTOPENOBJ, FAIL, "unable to open v2 B-tree for SOHM index");
1327
1328
0
        if (defer) {
1329
            /* If this returns 0, it means that the message wasn't found. */
1330
            /* If it return 1, set the heap_id in the shared struct.  It will
1331
             * return a heap ID, since a message with a reference count greater
1332
             * than 1 is always shared in the heap.
1333
             */
1334
0
            if (H5B2_find(bt2, &key, &found, NULL, NULL) < 0)
1335
0
                HGOTO_ERROR(H5E_SOHM, H5E_NOTFOUND, FAIL, "can't search for message in index");
1336
0
        } /* end if */
1337
0
        else {
1338
0
            H5SM_incr_ref_opdata_t op_data;
1339
1340
            /* Set up callback info */
1341
0
            op_data.key   = &key;
1342
0
            op_data.found = false;
1343
1344
            /* Set the heap_id in the shared struct, if the message was found.
1345
             * It will return a heap ID, since a message with a reference count
1346
             * greater than 1 is always shared in the heap.
1347
             */
1348
0
            if (H5B2_modify(bt2, &key, true, H5SM__incr_ref, &op_data) < 0)
1349
0
                HGOTO_ERROR(H5E_SOHM, H5E_CANTMODIFY, FAIL, "B-tree modification failed");
1350
0
            if (op_data.found) {
1351
0
                shared.u.heap_id = op_data.fheap_id;
1352
0
                found            = true;
1353
0
            } /* end if */
1354
0
        }     /* end else */
1355
0
    }         /* end else */
1356
1357
0
    if (found) {
1358
        /* If the message was found, it's shared in the heap (now).  Set up a
1359
         * shared message so we can mark it as shared.
1360
         */
1361
0
        shared.type = H5O_SHARE_TYPE_SOHM;
1362
1363
#ifdef H5_USING_MEMCHECKER
1364
        /* Reset the shared message payload if deferring.  This doesn't matter
1365
         *      in the long run since the payload will get overwritten when the
1366
         *      non-deferred call to this routine occurs, but it stops memory
1367
         *      checkers like valgrind from whining when the partially initialized
1368
         *      shared message is serialized. -QAK
1369
         */
1370
        if (defer)
1371
            memset(&shared.u, 0, sizeof(shared.u));
1372
#endif /* H5_USING_MEMCHECKER */
1373
0
    }  /* end if */
1374
0
    else {
1375
0
        htri_t share_in_ohdr; /* Whether the new message can be shared in another object's header */
1376
1377
        /* Add the message to the index */
1378
1379
        /* Check if the message can be shared in another object's header */
1380
0
        if ((share_in_ohdr = H5O_msg_can_share_in_ohdr(type_id)) < 0)
1381
0
            HGOTO_ERROR(H5E_SOHM, H5E_BADTYPE, FAIL, "'share in ohdr' check returned error");
1382
1383
        /* If this message can be shared in an object header location, it is
1384
         *      "shareable" but not shared in the heap.
1385
         *
1386
         * If 'defer' flag is set:
1387
         *      We will insert it in the index but not modify the original
1388
         *              message.
1389
         *      If it can't be shared in an object header location, we will
1390
         *              insert it in the heap.  Note that we will only share
1391
         *              the message in the object header if there is an
1392
         *              "open_oh" available.
1393
         *
1394
         * If 'defer' flag is not set:
1395
         *      Insert it in the index but don't modify the original message.
1396
         *      If it can't be shared in an object header location or there's
1397
         *              no object header location available, insert it in the
1398
         *              heap.
1399
         */
1400
0
        if (share_in_ohdr && open_oh) {
1401
            /* Set up shared component info */
1402
0
            shared.type = H5O_SHARE_TYPE_HERE;
1403
1404
            /* Retrieve any creation index from the native message */
1405
0
            if (H5O_msg_get_crt_index(type_id, mesg, &shared.u.loc.index) < 0)
1406
0
                HGOTO_ERROR(H5E_SOHM, H5E_CANTGET, FAIL, "unable to retrieve creation index");
1407
1408
0
            if (defer)
1409
0
                shared.u.loc.oh_addr = HADDR_UNDEF;
1410
0
            else {
1411
0
                shared.u.loc.oh_addr = H5O_OH_GET_ADDR(open_oh);
1412
1413
                /* Copy shared component info into key for inserting into index */
1414
0
                key.message.location   = H5SM_IN_OH;
1415
0
                key.message.u.mesg_loc = shared.u.loc;
1416
0
            } /* end else */
1417
0
        }     /* end if */
1418
0
        else {
1419
            /* Set up shared component info */
1420
            /* (heap ID set below, if not deferred) */
1421
0
            shared.type = H5O_SHARE_TYPE_SOHM;
1422
1423
0
            if (!defer) {
1424
                /* Put the message in the heap and record its new heap ID */
1425
0
                if (H5HF_insert(fheap, key.encoding_size, key.encoding, &shared.u.heap_id) < 0)
1426
0
                    HGOTO_ERROR(H5E_SOHM, H5E_CANTINSERT, FAIL, "unable to insert message into fractal heap");
1427
1428
0
                key.message.location             = H5SM_IN_HEAP;
1429
0
                key.message.u.heap_loc.fheap_id  = shared.u.heap_id;
1430
0
                key.message.u.heap_loc.ref_count = 1;
1431
0
            } /* end if */
1432
0
        }     /* end else */
1433
1434
0
        if (!defer) {
1435
            /* Set common information */
1436
0
            key.message.msg_type_id = type_id;
1437
1438
            /* Check whether the list has grown enough that it needs to become a B-tree */
1439
0
            if (header->index_type == H5SM_LIST && header->num_messages >= header->list_max)
1440
0
                if (H5SM__convert_list_to_btree(f, header, &list, fheap, open_oh) < 0)
1441
0
                    HGOTO_ERROR(H5E_SOHM, H5E_CANTDELETE, FAIL, "unable to convert list to B-tree");
1442
1443
            /* Insert the new message into the SOHM index */
1444
0
            if (header->index_type == H5SM_LIST) {
1445
                /* Index is a list.  Find an empty spot if we haven't already */
1446
0
                if (empty_pos == SIZE_MAX) {
1447
0
                    size_t pos;
1448
1449
0
                    if (H5SM__find_in_list(list, NULL, &empty_pos, &pos) < 0)
1450
0
                        HGOTO_ERROR(H5E_SOHM, H5E_CANTINSERT, FAIL, "unable to search for message in list");
1451
1452
0
                    if (pos == SIZE_MAX || empty_pos == SIZE_MAX)
1453
0
                        HGOTO_ERROR(H5E_SOHM, H5E_CANTINSERT, FAIL, "unable to find empty entry in list");
1454
0
                }
1455
                /* Insert message into list */
1456
0
                assert(list->messages[empty_pos].location == H5SM_NO_LOC);
1457
0
                assert(key.message.location != H5SM_NO_LOC);
1458
0
                list->messages[empty_pos] = key.message;
1459
0
            } /* end if */
1460
            /* Index is a B-tree */
1461
0
            else {
1462
0
                assert(header->index_type == H5SM_BTREE);
1463
1464
                /* Open the index v2 B-tree, if it isn't already */
1465
0
                if (NULL == bt2) {
1466
0
                    if (NULL == (bt2 = H5B2_open(f, header->index_addr, f)))
1467
0
                        HGOTO_ERROR(H5E_SOHM, H5E_CANTOPENOBJ, FAIL,
1468
0
                                    "unable to open v2 B-tree for SOHM index");
1469
0
                } /* end if */
1470
1471
0
                if (H5B2_insert(bt2, &key) < 0)
1472
0
                    HGOTO_ERROR(H5E_SOHM, H5E_CANTINSERT, FAIL, "couldn't add SOHM to B-tree");
1473
0
            } /* end else */
1474
1475
0
            ++(header->num_messages);
1476
0
            (*cache_flags_ptr) |= H5AC__DIRTIED_FLAG;
1477
0
        } /* end if */
1478
0
    }     /* end else */
1479
1480
    /* Set the file pointer & message type for the shared component */
1481
0
    shared.file        = f;
1482
0
    shared.msg_type_id = type_id;
1483
1484
    /* Update the original message's shared component */
1485
0
    if (H5O_msg_set_share(type_id, &shared, mesg) < 0)
1486
0
        HGOTO_ERROR(H5E_SOHM, H5E_BADMESG, FAIL, "unable to set sharing information");
1487
1488
0
done:
1489
    /* Release the fractal heap & v2 B-tree if we opened them */
1490
0
    if (fheap && H5HF_close(fheap) < 0)
1491
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTCLOSEOBJ, FAIL, "can't close fractal heap");
1492
0
    if (bt2 && H5B2_close(bt2) < 0)
1493
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTCLOSEOBJ, FAIL, "can't close v2 B-tree for SOHM index");
1494
1495
    /* If we got a list out of the cache, release it (it is always dirty after writing a message) */
1496
0
    if (list && H5AC_unprotect(f, H5AC_SOHM_LIST, header->index_addr, list,
1497
0
                               defer ? H5AC__NO_FLAGS_SET : H5AC__DIRTIED_FLAG) < 0)
1498
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTUNPROTECT, FAIL, "unable to close SOHM index");
1499
1500
0
    if (encoding_buf)
1501
0
        encoding_buf = H5MM_xfree(encoding_buf);
1502
1503
0
    FUNC_LEAVE_NOAPI(ret_value)
1504
0
} /* end H5SM__write_mesg() */
1505
1506
/*-------------------------------------------------------------------------
1507
 * Function:    H5SM_delete
1508
 *
1509
 * Purpose:     Given an object header message that is being deleted,
1510
 *              checks if it is a SOHM.  If so, decrements its reference
1511
 *              count.
1512
 *
1513
 *              If an object header is currently protected, it needs to
1514
 *              be passed in as open_oh so the SM code doesn't try to
1515
 *              re-protect it.
1516
 *
1517
 * Return:      Non-negative on success/Negative on failure
1518
 *
1519
 *-------------------------------------------------------------------------
1520
 */
1521
herr_t
1522
H5SM_delete(H5F_t *f, H5O_t *open_oh, H5O_shared_t *sh_mesg)
1523
0
{
1524
0
    H5SM_master_table_t  *table       = NULL;
1525
0
    unsigned              cache_flags = H5AC__NO_FLAGS_SET;
1526
0
    H5SM_table_cache_ud_t cache_udata; /* User-data for callback */
1527
0
    ssize_t               index_num;
1528
0
    size_t                mesg_size   = 0;
1529
0
    void                 *mesg_buf    = NULL;
1530
0
    void                 *native_mesg = NULL;
1531
0
    unsigned              type_id; /* Message type ID to operate on */
1532
0
    herr_t                ret_value = SUCCEED;
1533
1534
0
    FUNC_ENTER_NOAPI_TAG(H5AC__SOHM_TAG, FAIL)
1535
1536
0
    assert(f);
1537
0
    assert(H5_addr_defined(H5F_SOHM_ADDR(f)));
1538
0
    assert(sh_mesg);
1539
1540
    /* Get message type */
1541
0
    type_id = sh_mesg->msg_type_id;
1542
1543
    /* Set up user data for callback */
1544
0
    cache_udata.f = f;
1545
1546
    /* Look up the master SOHM table */
1547
0
    if (NULL == (table = (H5SM_master_table_t *)H5AC_protect(f, H5AC_SOHM_TABLE, H5F_SOHM_ADDR(f),
1548
0
                                                             &cache_udata, H5AC__NO_FLAGS_SET)))
1549
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTPROTECT, FAIL, "unable to load SOHM master table");
1550
1551
    /* Find the correct index and try to delete from it */
1552
0
    if (H5SM__get_index(table, type_id, &index_num) < 0)
1553
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTGET, FAIL, "unable to check for SOHM index");
1554
0
    if (index_num < 0)
1555
0
        HGOTO_ERROR(H5E_SOHM, H5E_NOTFOUND, FAIL, "unable to find correct SOHM index");
1556
1557
    /* If mesg_buf is not NULL, the message's reference count has reached
1558
     * zero and any file space it uses needs to be freed.  mesg_buf holds the
1559
     * serialized form of the message.
1560
     */
1561
0
    if (H5SM__delete_from_index(f, open_oh, &(table->indexes[index_num]), sh_mesg, &cache_flags, &mesg_size,
1562
0
                                &mesg_buf) < 0)
1563
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTDELETE, FAIL, "unable to delete message from SOHM index");
1564
1565
    /* Release the master SOHM table */
1566
0
    if (H5AC_unprotect(f, H5AC_SOHM_TABLE, H5F_SOHM_ADDR(f), table, cache_flags) < 0)
1567
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTUNPROTECT, FAIL, "unable to close SOHM master table");
1568
0
    table = NULL;
1569
1570
    /* If buf was allocated, delete the message it holds.  This message may
1571
     * reference other shared messages that also need to be deleted, so the
1572
     * master table needs to be unprotected when we do this.
1573
     */
1574
0
    if (mesg_buf) {
1575
0
        if (NULL ==
1576
0
            (native_mesg = H5O_msg_decode(f, open_oh, type_id, mesg_size, (const unsigned char *)mesg_buf)))
1577
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTDECODE, FAIL, "can't decode shared message.");
1578
1579
0
        if (H5O_msg_delete(f, open_oh, type_id, native_mesg) < 0)
1580
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTFREE, FAIL, "can't delete shared message.");
1581
0
    } /* end if */
1582
1583
0
done:
1584
    /* Release the master SOHM table (should only happen on error) */
1585
0
    if (table && H5AC_unprotect(f, H5AC_SOHM_TABLE, H5F_SOHM_ADDR(f), table, cache_flags) < 0)
1586
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTUNPROTECT, FAIL, "unable to close SOHM master table");
1587
1588
    /* Release any native message we decoded */
1589
0
    if (native_mesg)
1590
0
        H5O_msg_free(type_id, native_mesg);
1591
1592
    /* Free encoding buf */
1593
0
    if (mesg_buf)
1594
0
        mesg_buf = H5MM_xfree(mesg_buf);
1595
1596
0
    FUNC_LEAVE_NOAPI_TAG(ret_value)
1597
0
} /* end H5SM_delete() */
1598
1599
/*-------------------------------------------------------------------------
1600
 * Function:    H5SM__find_in_list
1601
 *
1602
 * Purpose:     Find a message's location in a list.  Also find the first
1603
 *              empty location in the list (since if we don't find the
1604
 *              message, we may want to insert it into an open spot).
1605
 *
1606
 *              If KEY is NULL, simply find the first empty location in the
1607
 *              list.
1608
 *
1609
 *              If EMPTY_POS is NULL, don't store anything in it.
1610
 *
1611
 * Return:      Success:    SUCCEED
1612
 *                          pos = position (if found)
1613
 *                          pos = SIZE_MAX (if not found)
1614
 *                          empty_pos = indeterminate (if found)
1615
 *                          empty_pos = 1st empty position (if not found)
1616
 *
1617
 *              Failure:    FAIL
1618
 *                          pos & empty_pos indeterminate
1619
 *
1620
 *-------------------------------------------------------------------------
1621
 */
1622
static herr_t
1623
H5SM__find_in_list(const H5SM_list_t *list, const H5SM_mesg_key_t *key, size_t *empty_pos, size_t *pos)
1624
0
{
1625
0
    size_t x;
1626
0
    herr_t ret_value = SUCCEED; /* Return value */
1627
1628
0
    FUNC_ENTER_PACKAGE
1629
1630
0
    assert(list);
1631
    /* Both key and empty_pos can be NULL, but not both! */
1632
0
    assert(key || empty_pos);
1633
1634
    /* Initialize empty_pos to an invalid value */
1635
0
    if (empty_pos)
1636
0
        *empty_pos = SIZE_MAX;
1637
1638
    /* Find the first (only) message equal to the key passed in.
1639
     * Also record the first empty position we find.
1640
     */
1641
0
    for (x = 0; x < list->header->list_max; x++) {
1642
0
        if (list->messages[x].location != H5SM_NO_LOC) {
1643
0
            int cmp;
1644
1645
0
            if (H5SM__message_compare(key, &(list->messages[x]), &cmp) < 0)
1646
0
                HGOTO_ERROR(H5E_SOHM, H5E_CANTCOMPARE, FAIL, "can't compare message records");
1647
1648
0
            if (0 == cmp) {
1649
0
                *pos = x;
1650
0
                HGOTO_DONE(SUCCEED);
1651
0
            }
1652
0
        }
1653
0
        else if (empty_pos && list->messages[x].location == H5SM_NO_LOC) {
1654
            /* Note position */
1655
0
            *empty_pos = x;
1656
1657
            /* Found earlier position possible, don't check any more */
1658
0
            empty_pos = NULL;
1659
0
        }
1660
0
    }
1661
1662
    /* If we reached this point, we didn't find the message */
1663
0
    *pos = SIZE_MAX;
1664
1665
0
done:
1666
0
    FUNC_LEAVE_NOAPI(ret_value)
1667
0
} /* end H5SM__find_in_list */
1668
1669
/*-------------------------------------------------------------------------
1670
 * Function:  H5SM__decr_ref
1671
 *
1672
 * Purpose: Decrement the reference count for a SOHM message.  Doesn't
1673
 *              remove the record from the B-tree even if the refcount
1674
 *              reaches zero.
1675
 *
1676
 *              The new message is returned through op_data.  If its
1677
 *              reference count is zero, the calling function should
1678
 *              remove this record from the B-tree.
1679
 *
1680
 * Return:  Non-negative on success
1681
 *              Negative on failure
1682
 *
1683
 *-------------------------------------------------------------------------
1684
 */
1685
static herr_t
1686
H5SM__decr_ref(void *record, void *op_data, bool *changed)
1687
0
{
1688
0
    H5SM_sohm_t *message = (H5SM_sohm_t *)record;
1689
1690
0
    FUNC_ENTER_PACKAGE_NOERR
1691
1692
0
    assert(record);
1693
0
    assert(op_data);
1694
0
    assert(changed);
1695
1696
    /* Adjust the message's reference count if it's stored in the heap.
1697
     * Messages stored in object headers always have refcounts of 1,
1698
     * so the calling function should know to just delete such a message
1699
     */
1700
0
    if (message->location == H5SM_IN_HEAP) {
1701
0
        --message->u.heap_loc.ref_count;
1702
0
        *changed = true;
1703
0
    } /* end if */
1704
1705
0
    if (op_data)
1706
0
        *(H5SM_sohm_t *)op_data = *message;
1707
1708
0
    FUNC_LEAVE_NOAPI(SUCCEED)
1709
0
} /* end H5SM__decr_ref() */
1710
1711
/*-------------------------------------------------------------------------
1712
 * Function:    H5SM__delete_from_index
1713
 *
1714
 * Purpose:     Decrement the reference count for a particular message in this
1715
 *              index.  If the reference count reaches zero, allocate a buffer
1716
 *              to hold the serialized form of this message so that any
1717
 *              resources it uses can be freed, and return this buffer in
1718
 *              ENCODED_MESG.
1719
 *
1720
 * Return:      Non-negative on success
1721
 *              Negative on failure
1722
 *
1723
 *-------------------------------------------------------------------------
1724
 */
1725
static herr_t
1726
H5SM__delete_from_index(H5F_t *f, H5O_t *open_oh, H5SM_index_header_t *header, const H5O_shared_t *mesg,
1727
                        unsigned *cache_flags, size_t *mesg_size /*out*/, void **encoded_mesg /*out*/)
1728
0
{
1729
0
    H5SM_list_t    *list = NULL;
1730
0
    H5SM_mesg_key_t key;
1731
0
    H5SM_sohm_t     message;             /* Deleted message returned from index */
1732
0
    H5SM_sohm_t    *message_ptr;         /* Pointer to deleted message returned from index */
1733
0
    H5HF_t         *fheap = NULL;        /* Fractal heap that contains the message */
1734
0
    H5B2_t         *bt2   = NULL;        /* v2 B-tree handle for index */
1735
0
    size_t          buf_size;            /* Size of the encoded message (out) */
1736
0
    void           *encoding_buf = NULL; /* The encoded message (out) */
1737
0
    unsigned        type_id;             /* Message type to operate on */
1738
0
    herr_t          ret_value = SUCCEED;
1739
1740
0
    FUNC_ENTER_PACKAGE
1741
1742
    /* Sanity check */
1743
0
    assert(f);
1744
0
    assert(header);
1745
0
    assert(mesg);
1746
0
    assert(cache_flags);
1747
0
    assert(*encoded_mesg == NULL);
1748
1749
    /* Get the message type for later */
1750
0
    type_id = mesg->msg_type_id;
1751
1752
    /* Open the heap for this type of message. */
1753
0
    if (NULL == (fheap = H5HF_open(f, header->heap_addr)))
1754
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTOPENOBJ, FAIL, "unable to open fractal heap");
1755
1756
    /* Get the message size and encoded message for the message to be deleted,
1757
     * either from its OH or from the heap.
1758
     */
1759
0
    if (mesg->type == H5O_SHARE_TYPE_HERE) {
1760
0
        key.message.location    = H5SM_IN_OH;
1761
0
        key.message.msg_type_id = type_id;
1762
0
        key.message.u.mesg_loc  = mesg->u.loc;
1763
0
    } /* end if */
1764
0
    else {
1765
0
        key.message.location             = H5SM_IN_HEAP;
1766
0
        key.message.msg_type_id          = type_id;
1767
0
        key.message.u.heap_loc.ref_count = 0; /* Refcount isn't relevant here */
1768
0
        key.message.u.heap_loc.fheap_id  = mesg->u.heap_id;
1769
0
    } /* end else */
1770
1771
    /* Get the encoded message */
1772
0
    if (H5SM__read_mesg(f, &key.message, fheap, open_oh, &buf_size, &encoding_buf) < 0)
1773
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTOPENOBJ, FAIL, "unable to open fractal heap");
1774
1775
    /* Set up key for message to be deleted. */
1776
0
    key.file          = f;
1777
0
    key.fheap         = fheap;
1778
0
    key.encoding      = encoding_buf;
1779
0
    key.encoding_size = buf_size;
1780
0
    key.message.hash  = H5_checksum_lookup3(encoding_buf, buf_size, type_id);
1781
1782
    /* Try to find the message in the index */
1783
0
    if (header->index_type == H5SM_LIST) {
1784
0
        H5SM_list_cache_ud_t cache_udata; /* User-data for metadata cache callback */
1785
0
        size_t               list_pos;    /* Position of the message in the list */
1786
1787
        /* Set up user data for metadata cache callback */
1788
0
        cache_udata.f      = f;
1789
0
        cache_udata.header = header;
1790
1791
        /* If the index is stored as a list, get it from the cache */
1792
0
        if (NULL == (list = (H5SM_list_t *)H5AC_protect(f, H5AC_SOHM_LIST, header->index_addr, &cache_udata,
1793
0
                                                        H5AC__NO_FLAGS_SET)))
1794
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTPROTECT, FAIL, "unable to load SOHM index");
1795
1796
        /* Find the message in the list */
1797
0
        if (H5SM__find_in_list(list, &key, NULL, &list_pos) < 0)
1798
0
            HGOTO_ERROR(H5E_SOHM, H5E_NOTFOUND, FAIL, "unable to search for message in list");
1799
0
        if (list_pos == SIZE_MAX)
1800
0
            HGOTO_ERROR(H5E_SOHM, H5E_NOTFOUND, FAIL, "message not in index");
1801
1802
0
        if (list->messages[list_pos].location == H5SM_IN_HEAP)
1803
0
            --(list->messages[list_pos].u.heap_loc.ref_count);
1804
1805
        /* Point to the message */
1806
0
        message_ptr = &list->messages[list_pos];
1807
0
    } /* end if */
1808
0
    else {
1809
        /* Index is a B-tree */
1810
0
        assert(header->index_type == H5SM_BTREE);
1811
1812
        /* Open the index v2 B-tree */
1813
0
        if (NULL == (bt2 = H5B2_open(f, header->index_addr, f)))
1814
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTOPENOBJ, FAIL, "unable to open v2 B-tree for SOHM index");
1815
1816
        /* If this returns failure, it means that the message wasn't found.
1817
         * If it succeeds, a copy of the modified message will be returned.
1818
         */
1819
0
        if (H5B2_modify(bt2, &key, false, H5SM__decr_ref, &message) < 0)
1820
0
            HGOTO_ERROR(H5E_SOHM, H5E_NOTFOUND, FAIL, "message not in index");
1821
1822
        /* Point to the message */
1823
0
        message_ptr = &message;
1824
0
    } /* end else */
1825
1826
    /* If the ref count is zero or this message was in an OH (which always
1827
     * has a ref count of 1) delete the message from the index
1828
     */
1829
0
    if (message_ptr->location == H5SM_IN_OH || message_ptr->u.heap_loc.ref_count == 0) {
1830
        /* Save the location */
1831
0
        H5SM_storage_loc_t old_loc = message_ptr->location;
1832
1833
        /* Updated the index header, so set its dirty flag */
1834
0
        --header->num_messages;
1835
0
        *cache_flags |= H5AC__DIRTIED_FLAG;
1836
1837
        /* Remove the message from the index */
1838
0
        if (header->index_type == H5SM_LIST)
1839
0
            message_ptr->location = H5SM_NO_LOC;
1840
0
        else {
1841
            /* Open the index v2 B-tree, if it isn't already */
1842
0
            if (NULL == bt2) {
1843
0
                if (NULL == (bt2 = H5B2_open(f, header->index_addr, f)))
1844
0
                    HGOTO_ERROR(H5E_SOHM, H5E_CANTOPENOBJ, FAIL, "unable to open v2 B-tree for SOHM index");
1845
0
            } /* end if */
1846
1847
0
            if (H5B2_remove(bt2, &key, NULL, NULL) < 0)
1848
0
                HGOTO_ERROR(H5E_SOHM, H5E_CANTREMOVE, FAIL, "unable to delete message from index");
1849
0
        } /* end else */
1850
1851
        /* Remove the message from the heap if it was stored in the heap*/
1852
0
        if (old_loc == H5SM_IN_HEAP)
1853
0
            if (H5HF_remove(fheap, &(message_ptr->u.heap_loc.fheap_id)) < 0)
1854
0
                HGOTO_ERROR(H5E_SOHM, H5E_CANTREMOVE, FAIL, "unable to remove message from heap");
1855
1856
        /* Return the message's encoding so anything it references can be freed */
1857
0
        *encoded_mesg = encoding_buf;
1858
0
        *mesg_size    = buf_size;
1859
1860
        /* If there are no messages left in the index, delete it */
1861
0
        if (header->num_messages == 0) {
1862
1863
            /* Unprotect cache and release heap */
1864
0
            if (list && H5AC_unprotect(f, H5AC_SOHM_LIST, header->index_addr, list,
1865
0
                                       H5AC__DELETED_FLAG | H5AC__FREE_FILE_SPACE_FLAG) < 0)
1866
0
                HGOTO_ERROR(H5E_SOHM, H5E_CANTUNPROTECT, FAIL, "unable to release SOHM list");
1867
0
            list = NULL;
1868
1869
0
            assert(fheap);
1870
0
            if (H5HF_close(fheap) < 0)
1871
0
                HGOTO_ERROR(H5E_SOHM, H5E_CANTCLOSEOBJ, FAIL, "can't close fractal heap");
1872
0
            fheap = NULL;
1873
1874
            /* Delete the index and its heap */
1875
0
            if (H5SM__delete_index(f, header, true) < 0)
1876
0
                HGOTO_ERROR(H5E_SOHM, H5E_CANTDELETE, FAIL, "can't delete empty index");
1877
0
        } /* end if */
1878
0
        else if (header->index_type == H5SM_BTREE && header->num_messages < header->btree_min) {
1879
            /* Otherwise, if we've just passed the btree-to-list cutoff, convert
1880
             * this B-tree into a list
1881
             */
1882
0
            if (H5SM__convert_btree_to_list(f, header) < 0)
1883
0
                HGOTO_ERROR(H5E_SOHM, H5E_CANTINIT, FAIL, "unable to convert btree to list");
1884
0
        } /* end if */
1885
0
    }     /* end if */
1886
1887
0
done:
1888
    /* Release the SOHM list */
1889
0
    if (list && H5AC_unprotect(f, H5AC_SOHM_LIST, header->index_addr, list, H5AC__DIRTIED_FLAG) < 0)
1890
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTUNPROTECT, FAIL, "unable to close SOHM index");
1891
1892
    /* Release the fractal heap & v2 B-tree if we opened them */
1893
0
    if (fheap && H5HF_close(fheap) < 0)
1894
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTCLOSEOBJ, FAIL, "can't close fractal heap");
1895
0
    if (bt2 && H5B2_close(bt2) < 0)
1896
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTCLOSEOBJ, FAIL, "can't close v2 B-tree for SOHM index");
1897
1898
    /* Free the message encoding, if we're not returning it in encoded_mesg
1899
     * or if there's been an error.
1900
     */
1901
0
    if (encoding_buf && (NULL == *encoded_mesg || ret_value < 0)) {
1902
0
        encoding_buf = H5MM_xfree(encoding_buf);
1903
0
        *mesg_size   = 0;
1904
0
    }
1905
1906
0
    FUNC_LEAVE_NOAPI(ret_value)
1907
0
} /* end H5SM__delete_from_index() */
1908
1909
/*-------------------------------------------------------------------------
1910
 * Function:    H5SM_get_info
1911
 *
1912
 * Purpose:     Get the shared message info for a file, if there is any.
1913
 *
1914
 * Return:      Non-negative on success/Negative on failure
1915
 *
1916
 *-------------------------------------------------------------------------
1917
 */
1918
herr_t
1919
H5SM_get_info(const H5O_loc_t *ext_loc, H5P_genplist_t *fc_plist)
1920
22
{
1921
22
    H5F_t               *f = ext_loc->file;         /* File pointer (convenience variable) */
1922
22
    H5O_shmesg_table_t   sohm_table;                /* SOHM message from superblock extension */
1923
22
    H5SM_master_table_t *table     = NULL;          /* SOHM master table */
1924
22
    H5AC_ring_t          orig_ring = H5AC_RING_INV; /* Original ring value */
1925
22
    unsigned             tmp_sohm_nindexes;         /* Number of shared messages indexes in the table */
1926
22
    htri_t               status;                    /* Status for message existing */
1927
22
    herr_t               ret_value = SUCCEED;       /* Return value */
1928
1929
22
    FUNC_ENTER_NOAPI_TAG(H5AC__SOHM_TAG, FAIL)
1930
1931
    /* Sanity check */
1932
22
    assert(ext_loc);
1933
22
    assert(f);
1934
22
    assert(fc_plist);
1935
1936
    /* Check for the extension having a 'shared message info' message */
1937
22
    if ((status = H5O_msg_exists(ext_loc, H5O_SHMESG_ID)) < 0)
1938
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTGET, FAIL, "unable to read object header");
1939
22
    if (status) {
1940
2
        H5SM_table_cache_ud_t cache_udata;                          /* User-data for callback */
1941
2
        unsigned              index_flags[H5O_SHMESG_MAX_NINDEXES]; /* Message flags for each index */
1942
2
        unsigned              minsizes[H5O_SHMESG_MAX_NINDEXES];    /* Minimum message size for each index */
1943
2
        unsigned              sohm_l2b;                             /* SOHM list-to-btree cutoff */
1944
2
        unsigned              sohm_b2l;                             /* SOHM btree-to-list cutoff */
1945
2
        unsigned              u;                                    /* Local index variable */
1946
1947
        /* Retrieve the 'shared message info' structure */
1948
2
        if (NULL == H5O_msg_read(ext_loc, H5O_SHMESG_ID, &sohm_table))
1949
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTGET, FAIL, "shared message info message not present");
1950
1951
        /* Portably initialize the arrays */
1952
2
        memset(index_flags, 0, sizeof(index_flags));
1953
2
        memset(minsizes, 0, sizeof(minsizes));
1954
1955
        /* Set SOHM info from file */
1956
2
        H5F_SET_SOHM_ADDR(f, sohm_table.addr);
1957
2
        H5F_SET_SOHM_VERS(f, sohm_table.version);
1958
2
        H5F_SET_SOHM_NINDEXES(f, sohm_table.nindexes);
1959
2
        assert(H5_addr_defined(H5F_SOHM_ADDR(f)));
1960
2
        assert(H5F_SOHM_NINDEXES(f) > 0 && H5F_SOHM_NINDEXES(f) <= H5O_SHMESG_MAX_NINDEXES);
1961
1962
        /* Set up user data for callback */
1963
2
        cache_udata.f = f;
1964
1965
        /* Set the ring type in the DXPL */
1966
2
        H5AC_set_ring(H5AC_RING_USER, &orig_ring);
1967
1968
        /* Read the rest of the SOHM table information from the cache */
1969
2
        if (NULL == (table = (H5SM_master_table_t *)H5AC_protect(f, H5AC_SOHM_TABLE, H5F_SOHM_ADDR(f),
1970
2
                                                                 &cache_udata, H5AC__READ_ONLY_FLAG)))
1971
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTPROTECT, FAIL, "unable to load SOHM master table");
1972
1973
        /* Get index conversion limits */
1974
2
        sohm_l2b = (unsigned)table->indexes[0].list_max;
1975
2
        sohm_b2l = (unsigned)table->indexes[0].btree_min;
1976
1977
        /* Iterate through all indices */
1978
10
        for (u = 0; u < table->num_indexes; ++u) {
1979
            /* Pack information about the individual SOHM index */
1980
8
            index_flags[u] = table->indexes[u].mesg_types;
1981
8
            minsizes[u]    = (unsigned)table->indexes[u].min_mesg_size;
1982
1983
            /* Sanity check */
1984
8
            assert(sohm_l2b == table->indexes[u].list_max);
1985
8
            assert(sohm_b2l == table->indexes[u].btree_min);
1986
1987
            /* Check for sharing attributes in this file, which means that creation
1988
             *  indices must be tracked on object header message in the file.
1989
             */
1990
8
            if (index_flags[u] & H5O_SHMESG_ATTR_FLAG)
1991
0
                H5F_SET_STORE_MSG_CRT_IDX(f, true);
1992
8
        } /* end for */
1993
1994
        /* Set values in the property list */
1995
2
        tmp_sohm_nindexes = H5F_SOHM_NINDEXES(f);
1996
2
        if (H5P_set(fc_plist, H5F_CRT_SHMSG_NINDEXES_NAME, &tmp_sohm_nindexes) < 0)
1997
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTSET, FAIL, "can't set number of SOHM indexes");
1998
2
        if (H5P_set(fc_plist, H5F_CRT_SHMSG_INDEX_TYPES_NAME, index_flags) < 0)
1999
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTSET, FAIL, "can't set type flags for indexes");
2000
2
        if (H5P_set(fc_plist, H5F_CRT_SHMSG_INDEX_MINSIZE_NAME, minsizes) < 0)
2001
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTSET, FAIL, "can't set type flags for indexes");
2002
2
        if (H5P_set(fc_plist, H5F_CRT_SHMSG_LIST_MAX_NAME, &sohm_l2b) < 0)
2003
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTGET, FAIL, "can't set SOHM cutoff in property list");
2004
2
        if (H5P_set(fc_plist, H5F_CRT_SHMSG_BTREE_MIN_NAME, &sohm_b2l) < 0)
2005
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTGET, FAIL, "can't set SOHM cutoff in property list");
2006
2
    } /* end if */
2007
20
    else {
2008
        /* No SOHM info in file */
2009
20
        H5F_SET_SOHM_ADDR(f, HADDR_UNDEF);
2010
20
        H5F_SET_SOHM_VERS(f, 0);
2011
20
        H5F_SET_SOHM_NINDEXES(f, 0);
2012
2013
        /* Shared object header messages are disabled */
2014
20
        tmp_sohm_nindexes = H5F_SOHM_NINDEXES(f);
2015
20
        if (H5P_set(fc_plist, H5F_CRT_SHMSG_NINDEXES_NAME, &tmp_sohm_nindexes) < 0)
2016
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTSET, FAIL, "can't set number of SOHM indexes");
2017
20
    } /* end else */
2018
2019
22
done:
2020
    /* Reset the ring in the API context */
2021
22
    if (orig_ring != H5AC_RING_INV)
2022
2
        H5AC_set_ring(orig_ring, NULL);
2023
2024
    /* Release the master SOHM table if we took it out of the cache */
2025
22
    if (table && H5AC_unprotect(f, H5AC_SOHM_TABLE, H5F_SOHM_ADDR(f), table, H5AC__NO_FLAGS_SET) < 0)
2026
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTUNPROTECT, FAIL, "unable to close SOHM master table");
2027
2028
22
    FUNC_LEAVE_NOAPI_TAG(ret_value)
2029
22
} /* end H5SM_get_info() */
2030
2031
/*-------------------------------------------------------------------------
2032
 * Function:    H5SM_reconstitute
2033
 *
2034
 * Purpose:     Reconstitute a shared object header message structure from
2035
 *              a plain heap ID.
2036
 *
2037
 * Return:      Non-negative on success/Negative on failure
2038
 *
2039
 *-------------------------------------------------------------------------
2040
 */
2041
herr_t
2042
H5SM_reconstitute(H5O_shared_t *sh_mesg, H5F_t *f, unsigned msg_type_id, H5O_fheap_id_t heap_id)
2043
0
{
2044
0
    FUNC_ENTER_NOAPI_NOINIT_NOERR
2045
2046
    /* Sanity check args */
2047
0
    assert(sh_mesg);
2048
2049
    /* Set flag for shared message */
2050
0
    sh_mesg->type        = H5O_SHARE_TYPE_SOHM;
2051
0
    sh_mesg->file        = f;
2052
0
    sh_mesg->msg_type_id = msg_type_id;
2053
0
    sh_mesg->u.heap_id   = heap_id;
2054
2055
0
    FUNC_LEAVE_NOAPI(SUCCEED)
2056
0
} /* end H5SM_reconstitute() */
2057
2058
/*-------------------------------------------------------------------------
2059
 * Function:  H5SM__get_refcount_bt2_cb
2060
 *
2061
 * Purpose: v2 B-tree 'find' callback to retrieve the record for a message
2062
 *
2063
 * Return:  SUCCEED/FAIL
2064
 *
2065
 *-------------------------------------------------------------------------
2066
 */
2067
static herr_t
2068
H5SM__get_refcount_bt2_cb(const void *_record, void *_op_data)
2069
0
{
2070
0
    const H5SM_sohm_t *record  = (const H5SM_sohm_t *)_record; /* v2 B-tree record for message */
2071
0
    H5SM_sohm_t       *op_data = (H5SM_sohm_t *)_op_data;      /* "op data" from v2 B-tree find */
2072
2073
0
    FUNC_ENTER_PACKAGE_NOERR
2074
2075
    /*
2076
     * Check arguments.
2077
     */
2078
0
    assert(record);
2079
0
    assert(op_data);
2080
2081
    /* Make a copy of the record */
2082
0
    *op_data = *record;
2083
2084
0
    FUNC_LEAVE_NOAPI(SUCCEED)
2085
0
} /* end H5SM__get_refcount_bt2_cb() */
2086
2087
/*-------------------------------------------------------------------------
2088
 * Function:    H5SM_get_refcount
2089
 *
2090
 * Purpose:     Retrieve the reference count for a message shared in the heap
2091
 *
2092
 * Return:      Non-negative on success/Negative on failure
2093
 *
2094
 *-------------------------------------------------------------------------
2095
 */
2096
herr_t
2097
H5SM_get_refcount(H5F_t *f, unsigned type_id, const H5O_shared_t *sh_mesg, hsize_t *ref_count)
2098
0
{
2099
0
    H5HF_t               *fheap = NULL;           /* Fractal heap that contains shared messages */
2100
0
    H5B2_t               *bt2   = NULL;           /* v2 B-tree handle for index */
2101
0
    H5SM_master_table_t  *table = NULL;           /* SOHM master table */
2102
0
    H5SM_table_cache_ud_t tbl_cache_udata;        /* User-data for callback */
2103
0
    H5SM_list_t          *list   = NULL;          /* SOHM index list for message type (if in list form) */
2104
0
    H5SM_index_header_t  *header = NULL;          /* Index header for message type */
2105
0
    H5SM_mesg_key_t       key;                    /* Key for looking up message */
2106
0
    H5SM_sohm_t           message;                /* Shared message returned from callback */
2107
0
    ssize_t               index_num;              /* Table index for message type */
2108
0
    size_t                buf_size;               /* Size of the encoded message */
2109
0
    void                 *encoding_buf = NULL;    /* Buffer for encoded message */
2110
0
    herr_t                ret_value    = SUCCEED; /* Return value */
2111
2112
0
    FUNC_ENTER_NOAPI_NOINIT_TAG(H5AC__SOHM_TAG)
2113
2114
    /* Sanity check */
2115
0
    assert(f);
2116
0
    assert(sh_mesg);
2117
0
    assert(ref_count);
2118
2119
    /* Set up user data for callback */
2120
0
    tbl_cache_udata.f = f;
2121
2122
    /* Look up the master SOHM table */
2123
0
    if (NULL == (table = (H5SM_master_table_t *)H5AC_protect(f, H5AC_SOHM_TABLE, H5F_SOHM_ADDR(f),
2124
0
                                                             &tbl_cache_udata, H5AC__READ_ONLY_FLAG)))
2125
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTPROTECT, FAIL, "unable to load SOHM master table");
2126
2127
    /* Find the correct index and find the message in it */
2128
0
    if (H5SM__get_index(table, type_id, &index_num) < 0)
2129
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTGET, FAIL, "unable to check for SOHM index");
2130
0
    if (index_num < 0)
2131
0
        HGOTO_ERROR(H5E_SOHM, H5E_NOTFOUND, FAIL, "unable to find correct SOHM index");
2132
0
    header = &(table->indexes[index_num]);
2133
2134
    /* Open the heap for this message type */
2135
0
    if (NULL == (fheap = H5HF_open(f, header->heap_addr)))
2136
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTOPENOBJ, FAIL, "unable to open fractal heap");
2137
2138
    /* Set up a SOHM message to correspond to the shared message passed in */
2139
0
    key.message.location             = H5SM_IN_HEAP;
2140
0
    key.message.u.heap_loc.fheap_id  = sh_mesg->u.heap_id;
2141
0
    key.message.u.heap_loc.ref_count = 0; /* Ref count isn't needed to find message */
2142
2143
    /* Get the encoded message */
2144
0
    if (H5SM__read_mesg(f, &key.message, fheap, NULL, &buf_size, &encoding_buf) < 0)
2145
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTOPENOBJ, FAIL, "unable to open fractal heap");
2146
2147
    /* Set up key for message to locate */
2148
0
    key.file          = f;
2149
0
    key.fheap         = fheap;
2150
0
    key.encoding      = encoding_buf;
2151
0
    key.encoding_size = buf_size;
2152
0
    key.message.hash  = H5_checksum_lookup3(encoding_buf, buf_size, type_id);
2153
2154
    /* Try to find the message in the index */
2155
0
    if (header->index_type == H5SM_LIST) {
2156
0
        H5SM_list_cache_ud_t lst_cache_udata; /* User-data for metadata cache callback */
2157
0
        size_t               list_pos;        /* Position of the message in the list */
2158
2159
        /* Set up user data for metadata cache callback */
2160
0
        lst_cache_udata.f      = f;
2161
0
        lst_cache_udata.header = header;
2162
2163
        /* If the index is stored as a list, get it from the cache */
2164
0
        if (NULL == (list = (H5SM_list_t *)H5AC_protect(f, H5AC_SOHM_LIST, header->index_addr,
2165
0
                                                        &lst_cache_udata, H5AC__READ_ONLY_FLAG)))
2166
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTPROTECT, FAIL, "unable to load SOHM index");
2167
2168
        /* Find the message in the list */
2169
0
        if (H5SM__find_in_list(list, &key, NULL, &list_pos) < 0)
2170
0
            HGOTO_ERROR(H5E_SOHM, H5E_NOTFOUND, FAIL, "unable to search for message in list");
2171
0
        if (list_pos == SIZE_MAX)
2172
0
            HGOTO_ERROR(H5E_SOHM, H5E_NOTFOUND, FAIL, "message not in index");
2173
2174
        /* Copy the message */
2175
0
        message = list->messages[list_pos];
2176
0
    } /* end if */
2177
0
    else {
2178
0
        bool msg_exists; /* Whether the message exists in the v2 B-tree */
2179
2180
        /* Index is a B-tree */
2181
0
        assert(header->index_type == H5SM_BTREE);
2182
2183
        /* Open the index v2 B-tree */
2184
0
        if (NULL == (bt2 = H5B2_open(f, header->index_addr, f)))
2185
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTOPENOBJ, FAIL, "unable to open v2 B-tree for SOHM index");
2186
2187
        /* Look up the message in the v2 B-tree */
2188
0
        msg_exists = false;
2189
0
        if (H5B2_find(bt2, &key, &msg_exists, H5SM__get_refcount_bt2_cb, &message) < 0)
2190
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTGET, FAIL, "error finding message in index");
2191
0
        if (!msg_exists)
2192
0
            HGOTO_ERROR(H5E_SOHM, H5E_NOTFOUND, FAIL, "message not in index");
2193
0
    } /* end else */
2194
2195
    /* Set the refcount for the message */
2196
0
    assert(message.location == H5SM_IN_HEAP);
2197
0
    *ref_count = message.u.heap_loc.ref_count;
2198
2199
0
done:
2200
    /* Release resources */
2201
0
    if (list && H5AC_unprotect(f, H5AC_SOHM_LIST, header->index_addr, list, H5AC__NO_FLAGS_SET) < 0)
2202
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTUNPROTECT, FAIL, "unable to close SOHM index");
2203
0
    if (table && H5AC_unprotect(f, H5AC_SOHM_TABLE, H5F_SOHM_ADDR(f), table, H5AC__NO_FLAGS_SET) < 0)
2204
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTUNPROTECT, FAIL, "unable to close SOHM master table");
2205
0
    if (fheap && H5HF_close(fheap) < 0)
2206
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTCLOSEOBJ, FAIL, "can't close fractal heap");
2207
0
    if (bt2 && H5B2_close(bt2) < 0)
2208
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTCLOSEOBJ, FAIL, "can't close v2 B-tree for SOHM index");
2209
0
    if (encoding_buf)
2210
0
        encoding_buf = H5MM_xfree(encoding_buf);
2211
2212
0
    FUNC_LEAVE_NOAPI_TAG(ret_value)
2213
0
} /* end H5SM_get_refcount() */
2214
2215
/*-------------------------------------------------------------------------
2216
 * Function:  H5SM__read_iter_op
2217
 *
2218
 * Purpose: OH iteration callback to get the encoded version of a message
2219
 *              by index.
2220
 *
2221
 *              The buffer needs to be freed.
2222
 *
2223
 * Return:  0 if this is not the message we're searching for
2224
 *              1 if this is the message we're searching for (with encoded
2225
 *                      value returned in udata)
2226
 *              negative on error
2227
 *
2228
 *-------------------------------------------------------------------------
2229
 */
2230
static herr_t
2231
H5SM__read_iter_op(H5O_t *oh, H5O_mesg_t *mesg /*in,out*/, unsigned sequence, void *_udata /*in,out*/)
2232
0
{
2233
0
    H5SM_read_udata_t *udata     = (H5SM_read_udata_t *)_udata;
2234
0
    herr_t             ret_value = H5_ITER_CONT;
2235
2236
0
    FUNC_ENTER_PACKAGE
2237
2238
    /*
2239
     * Check arguments.
2240
     */
2241
0
    assert(oh);
2242
0
    assert(mesg);
2243
0
    assert(udata);
2244
0
    assert(NULL == udata->encoding_buf);
2245
2246
    /* Check the creation index for this message */
2247
0
    if (sequence == udata->idx) {
2248
        /* Check if the message is dirty & flush it to the object header if so */
2249
0
        if (mesg->dirty)
2250
0
            if (H5O_msg_flush(udata->file, oh, mesg) < 0)
2251
0
                HGOTO_ERROR(H5E_SOHM, H5E_CANTENCODE, H5_ITER_ERROR,
2252
0
                            "unable to encode object header message");
2253
2254
        /* Get the message's encoded size */
2255
0
        udata->buf_size = mesg->raw_size;
2256
0
        assert(udata->buf_size);
2257
2258
        /* Allocate buffer to return the message in */
2259
0
        if (NULL == (udata->encoding_buf = H5MM_malloc(udata->buf_size)))
2260
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTALLOC, H5_ITER_ERROR, "memory allocation failed");
2261
2262
        /* Copy the encoded message into the buffer to return */
2263
0
        H5MM_memcpy(udata->encoding_buf, mesg->raw, udata->buf_size);
2264
2265
        /* Found the message we were looking for */
2266
0
        ret_value = H5_ITER_STOP;
2267
0
    } /* end if */
2268
2269
0
done:
2270
0
    FUNC_LEAVE_NOAPI(ret_value)
2271
0
} /* end H5SM__read_iter_op() */
2272
2273
/*-------------------------------------------------------------------------
2274
 * Function:  H5SM__read_mesg_fh_cb
2275
 *
2276
 * Purpose: Callback for H5HF_op, used in H5SM__read_mesg below.
2277
 *              Makes a copy of the message in the heap data, returned in the
2278
 *              UDATA struct.
2279
 *
2280
 * Return:  Negative on error, non-negative on success
2281
 *
2282
 *-------------------------------------------------------------------------
2283
 */
2284
static herr_t
2285
H5SM__read_mesg_fh_cb(const void *obj, size_t obj_len, void *_udata)
2286
0
{
2287
0
    H5SM_read_udata_t *udata     = (H5SM_read_udata_t *)_udata;
2288
0
    herr_t             ret_value = SUCCEED;
2289
2290
0
    FUNC_ENTER_PACKAGE
2291
2292
    /* Allocate a buffer to hold the message */
2293
0
    if (NULL == (udata->encoding_buf = H5MM_malloc(obj_len)))
2294
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTALLOC, FAIL, "memory allocation failed");
2295
2296
    /* Copy the message from the heap */
2297
0
    H5MM_memcpy(udata->encoding_buf, obj, obj_len);
2298
0
    udata->buf_size = obj_len;
2299
2300
0
done:
2301
0
    FUNC_LEAVE_NOAPI(ret_value)
2302
0
} /* end H5SM__read_mesg_fh_cb() */
2303
2304
/*-------------------------------------------------------------------------
2305
 * Function:  H5SM__read_mesg
2306
 *
2307
 * Purpose: Given an H5SM_sohm_t sohm, encodes the message into a buffer.
2308
 *              This buffer should then be freed.
2309
 *
2310
 * Return:  Non-negative on success/negative on error
2311
 *
2312
 *-------------------------------------------------------------------------
2313
 */
2314
static herr_t
2315
H5SM__read_mesg(H5F_t *f, const H5SM_sohm_t *mesg, H5HF_t *fheap, H5O_t *open_oh,
2316
                size_t *encoding_size /*out*/, void **encoded_mesg /*out*/)
2317
0
{
2318
0
    H5SM_read_udata_t udata;            /* User data for callbacks */
2319
0
    H5O_loc_t         oloc;             /* Object location for message in object header */
2320
0
    H5O_t            *oh        = NULL; /* Object header for message in object header */
2321
0
    herr_t            ret_value = SUCCEED;
2322
2323
0
    FUNC_ENTER_PACKAGE
2324
2325
0
    assert(f);
2326
0
    assert(mesg);
2327
0
    assert(fheap);
2328
2329
    /* Set up user data for message iteration */
2330
0
    udata.file         = f;
2331
0
    udata.idx          = mesg->u.mesg_loc.index;
2332
0
    udata.encoding_buf = NULL;
2333
0
    udata.idx          = 0;
2334
2335
    /* Get the message size and encoded message for the message to be deleted,
2336
     * either from its OH or from the heap.
2337
     */
2338
0
    if (mesg->location == H5SM_IN_OH) {
2339
        /* Read message from object header */
2340
0
        const H5O_msg_class_t *type = NULL; /* Actual H5O class type for the ID */
2341
0
        H5O_mesg_operator_t    op;          /* Wrapper for operator */
2342
2343
0
        type = H5O_msg_class_g[mesg->msg_type_id]; /* map the type ID to the actual type object */
2344
0
        assert(type);
2345
2346
        /* Reset object location for operation */
2347
0
        if (H5O_loc_reset(&oloc) < 0)
2348
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTRESET, FAIL, "unable to initialize location");
2349
2350
0
        if (NULL == open_oh || mesg->u.mesg_loc.oh_addr != H5O_OH_GET_ADDR(open_oh)) {
2351
            /* Open the object in the file */
2352
0
            oloc.file = f;
2353
0
            oloc.addr = mesg->u.mesg_loc.oh_addr;
2354
0
            if (H5O_open(&oloc) < 0)
2355
0
                HGOTO_ERROR(H5E_SOHM, H5E_CANTLOAD, FAIL, "unable to open object header");
2356
2357
            /* Load the object header from the cache */
2358
0
            if (NULL == (oh = H5O_protect(&oloc, H5AC__READ_ONLY_FLAG, false)))
2359
0
                HGOTO_ERROR(H5E_SOHM, H5E_CANTPROTECT, FAIL, "unable to load object header");
2360
0
        } /* end if */
2361
0
        else
2362
0
            oh = open_oh;
2363
2364
        /* Use the "real" iterate routine so it doesn't try to protect the OH */
2365
0
        op.op_type  = H5O_MESG_OP_LIB;
2366
0
        op.u.lib_op = H5SM__read_iter_op;
2367
0
        if ((ret_value = H5O__msg_iterate_real(f, oh, type, &op, &udata)) < 0)
2368
0
            HGOTO_ERROR(H5E_SOHM, H5E_BADITER, FAIL, "unable to iterate over object header messages");
2369
0
    } /* end if */
2370
0
    else {
2371
0
        assert(mesg->location == H5SM_IN_HEAP);
2372
2373
        /* Copy the message from the heap */
2374
0
        if (H5HF_op(fheap, &(mesg->u.heap_loc.fheap_id), H5SM__read_mesg_fh_cb, &udata) < 0)
2375
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTLOAD, FAIL, "can't read message from fractal heap.");
2376
0
    } /* end else */
2377
0
    assert(udata.encoding_buf);
2378
0
    assert(udata.buf_size);
2379
2380
    /* Record the returned values */
2381
0
    *encoded_mesg  = udata.encoding_buf;
2382
0
    *encoding_size = udata.buf_size;
2383
2384
0
done:
2385
    /* Close the object header if we opened one and had an error */
2386
0
    if (oh && oh != open_oh) {
2387
0
        if (oh && H5O_unprotect(&oloc, oh, H5AC__NO_FLAGS_SET) < 0)
2388
0
            HDONE_ERROR(H5E_SOHM, H5E_CANTUNPROTECT, FAIL, "unable to release object header");
2389
0
        if (H5O_close(&oloc, NULL) < 0)
2390
0
            HDONE_ERROR(H5E_SOHM, H5E_CANTCLOSEOBJ, FAIL, "unable to close object header");
2391
0
    } /* end if */
2392
2393
    /* Release the encoding buffer on error */
2394
0
    if (ret_value < 0 && udata.encoding_buf)
2395
0
        udata.encoding_buf = H5MM_xfree(udata.encoding_buf);
2396
2397
0
    FUNC_LEAVE_NOAPI(ret_value)
2398
0
} /* end H5SM__read_mesg */
2399
2400
/*-------------------------------------------------------------------------
2401
 * Function:  H5SM__table_free
2402
 *
2403
 * Purpose: Frees memory used by the SOHM table.
2404
 *
2405
 * Return:  Non-negative on success/Negative on failure
2406
 *
2407
 *-------------------------------------------------------------------------
2408
 */
2409
herr_t
2410
H5SM__table_free(H5SM_master_table_t *table)
2411
2
{
2412
2
    FUNC_ENTER_PACKAGE_NOERR
2413
2414
    /* Sanity check */
2415
2
    assert(table);
2416
2
    assert(table->indexes);
2417
2418
2
    table->indexes = H5FL_ARR_FREE(H5SM_index_header_t, table->indexes);
2419
2420
2
    table = H5FL_FREE(H5SM_master_table_t, table);
2421
2422
2
    FUNC_LEAVE_NOAPI(SUCCEED)
2423
2
} /* end H5SM__table_free() */
2424
2425
/*-------------------------------------------------------------------------
2426
 * Function:  H5SM__list_free
2427
 *
2428
 * Purpose: Frees all memory used by the list.
2429
 *
2430
 * Return:  Non-negative on success/Negative on failure
2431
 *
2432
 *-------------------------------------------------------------------------
2433
 */
2434
herr_t
2435
H5SM__list_free(H5SM_list_t *list)
2436
0
{
2437
0
    FUNC_ENTER_PACKAGE_NOERR
2438
2439
0
    assert(list);
2440
0
    assert(list->messages);
2441
2442
0
    list->messages = H5FL_ARR_FREE(H5SM_sohm_t, list->messages);
2443
2444
0
    list = H5FL_FREE(H5SM_list_t, list);
2445
2446
0
    FUNC_LEAVE_NOAPI(SUCCEED)
2447
0
} /* end H5SM__list_free() */
2448
2449
/*-------------------------------------------------------------------------
2450
 * Function:    H5SM_table_debug
2451
 *
2452
 * Purpose:     Print debugging information for the master table.
2453
 *
2454
 *              If table_vers and num_indexes are not UINT_MAX, they are used
2455
 *              instead of the values in the superblock.
2456
 *
2457
 * Return:      Non-negative on success/Negative on failure
2458
 *
2459
 *-------------------------------------------------------------------------
2460
 */
2461
herr_t
2462
H5SM_table_debug(H5F_t *f, haddr_t table_addr, FILE *stream, int indent, int fwidth, unsigned table_vers,
2463
                 unsigned num_indexes)
2464
0
{
2465
0
    H5SM_master_table_t  *table = NULL;        /* SOHM master table */
2466
0
    H5SM_table_cache_ud_t cache_udata;         /* User-data for callback */
2467
0
    unsigned              x;                   /* Counter variable */
2468
0
    herr_t                ret_value = SUCCEED; /* Return value */
2469
2470
0
    FUNC_ENTER_NOAPI_TAG(H5AC__SOHM_TAG, FAIL)
2471
2472
0
    assert(f);
2473
0
    assert(table_addr != HADDR_UNDEF);
2474
0
    assert(stream);
2475
0
    assert(indent >= 0);
2476
0
    assert(fwidth >= 0);
2477
2478
    /* If table_vers and num_indexes are UINT_MAX, replace them with values from
2479
     * userblock
2480
     */
2481
0
    if (table_vers == UINT_MAX)
2482
0
        table_vers = H5F_SOHM_VERS(f);
2483
0
    else if (table_vers != H5F_SOHM_VERS(f))
2484
0
        fprintf(stream, "*** SOHM TABLE VERSION DOESN'T MATCH VERSION IN SUPERBLOCK!\n");
2485
0
    if (num_indexes == UINT_MAX)
2486
0
        num_indexes = H5F_SOHM_NINDEXES(f);
2487
0
    else if (num_indexes != H5F_SOHM_NINDEXES(f))
2488
0
        fprintf(stream, "*** NUMBER OF SOHM INDEXES DOESN'T MATCH VALUE IN SUPERBLOCK!\n");
2489
2490
    /* Check arguments.  Version must be 0, the only version implemented so far */
2491
0
    if (table_vers > HDF5_SHAREDHEADER_VERSION)
2492
0
        HGOTO_ERROR(H5E_SOHM, H5E_BADVALUE, FAIL, "unknown shared message table version");
2493
0
    if (num_indexes == 0 || num_indexes > H5O_SHMESG_MAX_NINDEXES)
2494
0
        HGOTO_ERROR(H5E_SOHM, H5E_BADVALUE, FAIL,
2495
0
                    "number of indexes must be between 1 and H5O_SHMESG_MAX_NINDEXES");
2496
2497
    /* Set up user data for callback */
2498
0
    cache_udata.f = f;
2499
2500
    /* Look up the master SOHM table */
2501
0
    if (NULL == (table = (H5SM_master_table_t *)H5AC_protect(f, H5AC_SOHM_TABLE, table_addr, &cache_udata,
2502
0
                                                             H5AC__READ_ONLY_FLAG)))
2503
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTPROTECT, FAIL, "unable to load SOHM master table");
2504
2505
0
    fprintf(stream, "%*sShared Message Master Table...\n", indent, "");
2506
0
    for (x = 0; x < num_indexes; ++x) {
2507
0
        fprintf(stream, "%*sIndex %d...\n", indent, "", x);
2508
0
        fprintf(stream, "%*s%-*s %s\n", indent + 3, "", fwidth, "SOHM Index Type:",
2509
0
                (table->indexes[x].index_type == H5SM_LIST
2510
0
                     ? "List"
2511
0
                     : (table->indexes[x].index_type == H5SM_BTREE ? "B-Tree" : "Unknown")));
2512
2513
0
        fprintf(stream, "%*s%-*s %" PRIuHADDR "\n", indent + 3, "", fwidth,
2514
0
                "Address of index:", table->indexes[x].index_addr);
2515
0
        fprintf(stream, "%*s%-*s %" PRIuHADDR "\n", indent + 3, "", fwidth,
2516
0
                "Address of index's heap:", table->indexes[x].heap_addr);
2517
0
        fprintf(stream, "%*s%-*s 0x%08x\n", indent + 3, "", fwidth,
2518
0
                "Message type flags:", table->indexes[x].mesg_types);
2519
0
        fprintf(stream, "%*s%-*s %zu\n", indent + 3, "", fwidth,
2520
0
                "Minimum size of messages:", table->indexes[x].min_mesg_size);
2521
0
        fprintf(stream, "%*s%-*s %zu\n", indent + 3, "", fwidth,
2522
0
                "Number of messages:", table->indexes[x].num_messages);
2523
0
        fprintf(stream, "%*s%-*s %zu\n", indent + 3, "", fwidth,
2524
0
                "Maximum list size:", table->indexes[x].list_max);
2525
0
        fprintf(stream, "%*s%-*s %zu\n", indent + 3, "", fwidth,
2526
0
                "Minimum B-tree size:", table->indexes[x].btree_min);
2527
0
    } /* end for */
2528
2529
0
done:
2530
0
    if (table && H5AC_unprotect(f, H5AC_SOHM_TABLE, table_addr, table, H5AC__NO_FLAGS_SET) < 0)
2531
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTUNPROTECT, FAIL, "unable to close SOHM master table");
2532
2533
0
    FUNC_LEAVE_NOAPI_TAG(ret_value)
2534
0
} /* end H5SM_table_debug() */
2535
2536
/*-------------------------------------------------------------------------
2537
 * Function:    H5SM_list_debug
2538
 *
2539
 * Purpose:     Print debugging information for a SOHM list.
2540
 *
2541
 *              Relies on the list version and number of messages passed in.
2542
 *
2543
 * Return:      Non-negative on success/Negative on failure
2544
 *
2545
 *-------------------------------------------------------------------------
2546
 */
2547
herr_t
2548
H5SM_list_debug(H5F_t *f, haddr_t list_addr, FILE *stream, int indent, int fwidth, haddr_t table_addr)
2549
0
{
2550
0
    H5SM_master_table_t  *table = NULL;        /* SOHM master table */
2551
0
    H5SM_list_t          *list  = NULL;        /* SOHM index list for message type (if in list form) */
2552
0
    H5SM_list_cache_ud_t  lst_cache_udata;     /* List user-data for metadata cache callback */
2553
0
    H5SM_table_cache_ud_t tbl_cache_udata;     /* Table user-data for metadata cache callback */
2554
0
    H5HF_t               *fh = NULL;           /* Fractal heap for SOHM messages */
2555
0
    unsigned              index_num;           /* Index of list, within master table */
2556
0
    unsigned              x;                   /* Counter variable */
2557
0
    herr_t                ret_value = SUCCEED; /* Return value */
2558
2559
0
    FUNC_ENTER_NOAPI_TAG(H5AC__SOHM_TAG, FAIL)
2560
2561
0
    assert(f);
2562
0
    assert(list_addr != HADDR_UNDEF);
2563
0
    assert(stream);
2564
0
    assert(indent >= 0);
2565
0
    assert(fwidth >= 0);
2566
2567
    /* Set up user data for callback */
2568
0
    tbl_cache_udata.f = f;
2569
2570
    /* Look up the master SOHM table */
2571
0
    if (NULL == (table = (H5SM_master_table_t *)H5AC_protect(f, H5AC_SOHM_TABLE, table_addr, &tbl_cache_udata,
2572
0
                                                             H5AC__READ_ONLY_FLAG)))
2573
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTPROTECT, FAIL, "unable to load SOHM master table");
2574
2575
    /* Determine which index the list is part of */
2576
0
    index_num = table->num_indexes;
2577
0
    for (x = 0; x < table->num_indexes; x++) {
2578
0
        if (H5_addr_eq(table->indexes[x].index_addr, list_addr)) {
2579
0
            index_num = x;
2580
0
            break;
2581
0
        } /* end if */
2582
0
    }     /* end for */
2583
0
    if (x == table->num_indexes)
2584
0
        HGOTO_ERROR(H5E_SOHM, H5E_BADVALUE, FAIL,
2585
0
                    "list address doesn't match address for any indices in table");
2586
2587
    /* Set up user data for metadata cache callback */
2588
0
    lst_cache_udata.f      = f;
2589
0
    lst_cache_udata.header = &(table->indexes[index_num]);
2590
2591
    /* Get the list from the cache */
2592
0
    if (NULL == (list = (H5SM_list_t *)H5AC_protect(f, H5AC_SOHM_LIST, list_addr, &lst_cache_udata,
2593
0
                                                    H5AC__READ_ONLY_FLAG)))
2594
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTPROTECT, FAIL, "unable to load SOHM index");
2595
2596
    /* Open the heap, if one exists */
2597
0
    if (H5_addr_defined(table->indexes[index_num].heap_addr))
2598
0
        if (NULL == (fh = H5HF_open(f, table->indexes[index_num].heap_addr)))
2599
0
            HGOTO_ERROR(H5E_SOHM, H5E_CANTOPENOBJ, FAIL, "unable to open SOHM heap");
2600
2601
0
    fprintf(stream, "%*sShared Message List Index...\n", indent, "");
2602
0
    for (x = 0; x < table->indexes[index_num].num_messages; ++x) {
2603
0
        fprintf(stream, "%*sShared Object Header Message %d...\n", indent, "", x);
2604
0
        fprintf(stream, "%*s%-*s %08lu\n", indent + 3, "", fwidth,
2605
0
                "Hash value:", (unsigned long)list->messages[x].hash);
2606
0
        if (list->messages[x].location == H5SM_IN_HEAP) {
2607
0
            assert(fh);
2608
2609
0
            fprintf(stream, "%*s%-*s %s\n", indent + 3, "", fwidth, "Location:", "in heap");
2610
0
            fprintf(stream, "%*s%-*s 0x%" PRIx64 "\n", indent + 3, "", fwidth,
2611
0
                    "Heap ID:", list->messages[x].u.heap_loc.fheap_id.val);
2612
0
            fprintf(stream, "%*s%-*s %" PRIuHSIZE "\n", indent + 3, "", fwidth,
2613
0
                    "Reference count:", list->messages[x].u.heap_loc.ref_count);
2614
0
        } /* end if */
2615
0
        else if (list->messages[x].location == H5SM_IN_OH) {
2616
0
            fprintf(stream, "%*s%-*s %s\n", indent + 3, "", fwidth, "Location:", "in object header");
2617
0
            fprintf(stream, "%*s%-*s %" PRIuHADDR "\n", indent + 3, "", fwidth,
2618
0
                    "Object header address:", list->messages[x].u.mesg_loc.oh_addr);
2619
0
            fprintf(stream, "%*s%-*s %" PRIuHADDR "\n", indent + 3, "", fwidth,
2620
0
                    "Message creation index:", list->messages[x].u.mesg_loc.oh_addr);
2621
0
            fprintf(stream, "%*s%-*s %u\n", indent + 3, "", fwidth,
2622
0
                    "Message type ID:", list->messages[x].msg_type_id);
2623
0
        } /* end if */
2624
0
        else
2625
0
            fprintf(stream, "%*s%-*s %s\n", indent + 3, "", fwidth, "Location:", "invalid");
2626
0
    } /* end for */
2627
2628
0
done:
2629
0
    if (fh && H5HF_close(fh) < 0)
2630
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTCLOSEOBJ, FAIL, "unable to close SOHM heap");
2631
0
    if (list && H5AC_unprotect(f, H5AC_SOHM_LIST, list_addr, list, H5AC__NO_FLAGS_SET) < 0)
2632
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTUNPROTECT, FAIL, "unable to close SOHM index");
2633
0
    if (table && H5AC_unprotect(f, H5AC_SOHM_TABLE, table_addr, table, H5AC__NO_FLAGS_SET) < 0)
2634
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTUNPROTECT, FAIL, "unable to close SOHM master table");
2635
2636
0
    FUNC_LEAVE_NOAPI_TAG(ret_value)
2637
0
} /* end H5SM_list_debug() */
2638
2639
/*-------------------------------------------------------------------------
2640
 * Function:    H5SM_ih_size
2641
 *
2642
 * Purpose:     Loop through the master SOHM table (if there is one) to:
2643
 *      1. collect storage used for header
2644
 *                      1. collect storage used for B-tree and List
2645
 *         (include btree storage used by huge objects in fractal heap)
2646
 *                      2. collect fractal heap storage
2647
 *
2648
 * Return:      Non-negative on success/Negative on failure
2649
 *
2650
 *-------------------------------------------------------------------------
2651
 */
2652
herr_t
2653
H5SM_ih_size(H5F_t *f, hsize_t *hdr_size, H5_ih_info_t *ih_info)
2654
0
{
2655
0
    H5SM_master_table_t  *table = NULL;        /* SOHM master table */
2656
0
    H5SM_table_cache_ud_t cache_udata;         /* User-data for callback */
2657
0
    H5HF_t               *fheap = NULL;        /* Fractal heap handle */
2658
0
    H5B2_t               *bt2   = NULL;        /* v2 B-tree handle for index */
2659
0
    unsigned              u;                   /* Local index variable */
2660
0
    herr_t                ret_value = SUCCEED; /* Return value */
2661
2662
0
    FUNC_ENTER_NOAPI_TAG(H5AC__SOHM_TAG, FAIL)
2663
2664
    /* Sanity check */
2665
0
    assert(f);
2666
0
    assert(H5_addr_defined(H5F_SOHM_ADDR(f)));
2667
0
    assert(hdr_size);
2668
0
    assert(ih_info);
2669
2670
    /* Set up user data for callback */
2671
0
    cache_udata.f = f;
2672
2673
    /* Look up the master SOHM table */
2674
0
    if (NULL == (table = (H5SM_master_table_t *)H5AC_protect(f, H5AC_SOHM_TABLE, H5F_SOHM_ADDR(f),
2675
0
                                                             &cache_udata, H5AC__READ_ONLY_FLAG)))
2676
0
        HGOTO_ERROR(H5E_SOHM, H5E_CANTPROTECT, FAIL, "unable to load SOHM master table");
2677
2678
    /* Get SOHM header size */
2679
0
    *hdr_size = table->table_size;
2680
2681
    /* Loop over all the indices for shared messages */
2682
0
    for (u = 0; u < table->num_indexes; u++) {
2683
        /* Get index storage size (for either B-tree or list) */
2684
0
        if (table->indexes[u].index_type == H5SM_BTREE) {
2685
0
            if (H5_addr_defined(table->indexes[u].index_addr)) {
2686
                /* Open the index v2 B-tree */
2687
0
                if (NULL == (bt2 = H5B2_open(f, table->indexes[u].index_addr, f)))
2688
0
                    HGOTO_ERROR(H5E_SOHM, H5E_CANTOPENOBJ, FAIL, "unable to open v2 B-tree for SOHM index");
2689
2690
0
                if (H5B2_size(bt2, &(ih_info->index_size)) < 0)
2691
0
                    HGOTO_ERROR(H5E_SOHM, H5E_CANTGET, FAIL, "can't retrieve B-tree storage info");
2692
2693
                /* Close the v2 B-tree */
2694
0
                if (H5B2_close(bt2) < 0)
2695
0
                    HGOTO_ERROR(H5E_SOHM, H5E_CANTCLOSEOBJ, FAIL, "can't close v2 B-tree for SOHM index");
2696
0
                bt2 = NULL;
2697
0
            } /* end if */
2698
0
        }     /* end if */
2699
0
        else {
2700
0
            assert(table->indexes[u].index_type == H5SM_LIST);
2701
0
            ih_info->index_size += table->indexes[u].list_size;
2702
0
        } /* end else */
2703
2704
        /* Check for heap for this index */
2705
0
        if (H5_addr_defined(table->indexes[u].heap_addr)) {
2706
            /* Open the fractal heap for this index */
2707
0
            if (NULL == (fheap = H5HF_open(f, table->indexes[u].heap_addr)))
2708
0
                HGOTO_ERROR(H5E_SOHM, H5E_CANTOPENOBJ, FAIL, "unable to open fractal heap");
2709
2710
            /* Get heap storage size */
2711
0
            if (H5HF_size(fheap, &(ih_info->heap_size)) < 0)
2712
0
                HGOTO_ERROR(H5E_SOHM, H5E_CANTGET, FAIL, "can't retrieve fractal heap storage info");
2713
2714
            /* Close the fractal heap */
2715
0
            if (H5HF_close(fheap) < 0)
2716
0
                HGOTO_ERROR(H5E_SOHM, H5E_CANTCLOSEOBJ, FAIL, "can't close fractal heap");
2717
0
            fheap = NULL;
2718
0
        } /* end if */
2719
0
    }     /* end for */
2720
2721
0
done:
2722
    /* Release resources */
2723
0
    if (fheap && H5HF_close(fheap) < 0)
2724
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTCLOSEOBJ, FAIL, "can't close fractal heap");
2725
0
    if (bt2 && H5B2_close(bt2) < 0)
2726
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTCLOSEOBJ, FAIL, "can't close v2 B-tree for SOHM index");
2727
0
    if (table && H5AC_unprotect(f, H5AC_SOHM_TABLE, H5F_SOHM_ADDR(f), table, H5AC__NO_FLAGS_SET) < 0)
2728
0
        HDONE_ERROR(H5E_SOHM, H5E_CANTUNPROTECT, FAIL, "unable to close SOHM master table");
2729
2730
0
    FUNC_LEAVE_NOAPI_TAG(ret_value)
2731
0
} /* end H5SM_ih_size() */