1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright by The HDF Group.                                               *
3  * Copyright by the Board of Trustees of the University of Illinois.         *
4  * All rights reserved.                                                      *
5  *                                                                           *
6  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
7  * terms governing use, modification, and redistribution, is contained in    *
8  * the COPYING file, which can be found at the root of the source code       *
9  * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
10  * If you do not have access to either file, you may request a copy from     *
11  * help@hdfgroup.org.                                                        *
12  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13 
14 /****************/
15 /* Module Setup */
16 /****************/
17 
18 #include "H5Fmodule.h"          /* This source code file is part of the H5F module */
19 
20 
21 /***********/
22 /* Headers */
23 /***********/
24 #include "H5private.h"		/* Generic Functions                    */
25 #include "H5ACprivate.h"        /* Metadata cache                       */
26 #include "H5Eprivate.h"		/* Error handling                       */
27 #include "H5Fpkg.h"             /* File access                          */
28 #include "H5FDprivate.h"	/* File drivers                         */
29 #include "H5Iprivate.h"		/* IDs                                  */
30 #include "H5MFprivate.h"        /* File memory management               */
31 #include "H5MMprivate.h"	/* Memory management			*/
32 #include "H5Pprivate.h"		/* Property lists                       */
33 #include "H5SMprivate.h"        /* Shared Object Header Messages        */
34 
35 
36 /****************/
37 /* Local Macros */
38 /****************/
39 
40 
41 /******************/
42 /* Local Typedefs */
43 /******************/
44 
45 
46 /********************/
47 /* Package Typedefs */
48 /********************/
49 
50 
51 /********************/
52 /* Local Prototypes */
53 /********************/
54 static herr_t H5F_super_ext_create(H5F_t *f, hid_t dxpl_id, H5O_loc_t *ext_ptr);
55 static herr_t H5F__update_super_ext_driver_msg(H5F_t *f, hid_t dxpl_id);
56 
57 
58 /*********************/
59 /* Package Variables */
60 /*********************/
61 
62 
63 /*****************************/
64 /* Library Private Variables */
65 /*****************************/
66 
67 /* Declare a free list to manage the H5F_super_t struct */
68 H5FL_DEFINE(H5F_super_t);
69 
70 
71 /*******************/
72 /* Local Variables */
73 /*******************/
74 
75 
76 
77 /*-------------------------------------------------------------------------
78  * Function:    H5F_super_ext_create
79  *
80  * Purpose:     Create the superblock extension
81  *
82  * Return:      Success:        non-negative on success
83  *              Failure:        Negative
84  *
85  * Programmer:  Vailin Choi; Feb 2009
86  *
87  *-------------------------------------------------------------------------
88  */
89 static herr_t
H5F_super_ext_create(H5F_t * f,hid_t dxpl_id,H5O_loc_t * ext_ptr)90 H5F_super_ext_create(H5F_t *f, hid_t dxpl_id, H5O_loc_t *ext_ptr)
91 {
92     herr_t ret_value = SUCCEED;         /* Return value */
93 
94     FUNC_ENTER_NOAPI_NOINIT
95 
96     /* Sanity check */
97     HDassert(f);
98     HDassert(f->shared);
99     HDassert(f->shared->sblock);
100     HDassert(!H5F_addr_defined(f->shared->sblock->ext_addr));
101     HDassert(ext_ptr);
102 
103     /* Check for older version of superblock format that can't support superblock extensions */
104     if(f->shared->sblock->super_vers < HDF5_SUPERBLOCK_VERSION_2)
105         HGOTO_ERROR(H5E_FILE, H5E_CANTCREATE, FAIL, "superblock extension not permitted with version %u of superblock", f->shared->sblock->super_vers)
106     else if(H5F_addr_defined(f->shared->sblock->ext_addr))
107         HGOTO_ERROR(H5E_FILE, H5E_CANTCREATE, FAIL, "superblock extension already exists?!?!")
108     else {
109         /* The superblock extension isn't actually a group, but the
110          * default group creation list should work fine.
111          * If we don't supply a size for the object header, HDF5 will
112          * allocate H5O_MIN_SIZE by default.  This is currently
113          * big enough to hold the biggest possible extension, but should
114          * be tuned if more information is added to the superblock
115          * extension.
116          */
117         H5O_loc_reset(ext_ptr);
118         if(H5O_create(f, dxpl_id, (size_t)0, (size_t)1, H5P_GROUP_CREATE_DEFAULT, ext_ptr) < 0)
119             HGOTO_ERROR(H5E_OHDR, H5E_CANTCREATE, FAIL, "unable to create superblock extension")
120 
121         /* Record the address of the superblock extension */
122         f->shared->sblock->ext_addr = ext_ptr->addr;
123     } /* end else */
124 
125 done:
126     FUNC_LEAVE_NOAPI(ret_value)
127 } /* H5F_super_ext_create() */
128 
129 
130 /*-------------------------------------------------------------------------
131  * Function:    H5F_super_ext_open
132  *
133  * Purpose:     Open an existing superblock extension
134  *
135  * Return:      Success:        non-negative on success
136  *              Failure:        Negative
137  *
138  * Programmer:  Vailin Choi; Feb 2009
139  *
140  *-------------------------------------------------------------------------
141  */
142 herr_t
H5F_super_ext_open(H5F_t * f,haddr_t ext_addr,H5O_loc_t * ext_ptr)143 H5F_super_ext_open(H5F_t *f, haddr_t ext_addr, H5O_loc_t *ext_ptr)
144 {
145     herr_t ret_value = SUCCEED;         /* Return value */
146 
147     FUNC_ENTER_NOAPI_NOINIT
148 
149     /* Sanity check */
150     HDassert(f);
151     HDassert(H5F_addr_defined(ext_addr));
152     HDassert(ext_ptr);
153 
154     /* Set up "fake" object location for superblock extension */
155     H5O_loc_reset(ext_ptr);
156     ext_ptr->file = f;
157     ext_ptr->addr = ext_addr;
158 
159     /* Open the superblock extension object header */
160     if(H5O_open(ext_ptr) < 0)
161 	HGOTO_ERROR(H5E_OHDR, H5E_CANTOPENOBJ, FAIL, "unable to open superblock extension")
162 
163 done:
164     FUNC_LEAVE_NOAPI(ret_value)
165 } /* H5F_super_ext_open() */
166 
167 
168 /*-------------------------------------------------------------------------
169  * Function:   H5F_super_ext_close
170  *
171  * Purpose:    Close superblock extension
172  *
173  * Return:     Success:        non-negative on success
174  *             Failure:        Negative
175  *
176  * Programmer:  Vailin Choi; Feb 2009
177  *
178  *-------------------------------------------------------------------------
179  */
180 herr_t
H5F_super_ext_close(H5F_t * f,H5O_loc_t * ext_ptr,hid_t dxpl_id,hbool_t was_created)181 H5F_super_ext_close(H5F_t *f, H5O_loc_t *ext_ptr, hid_t dxpl_id,
182     hbool_t was_created)
183 {
184     H5P_genplist_t *dxpl = NULL;        /* DXPL for setting ring */
185     H5AC_ring_t orig_ring = H5AC_RING_INV;      /* Original ring value */
186     herr_t ret_value = SUCCEED;         /* Return value */
187 
188     FUNC_ENTER_NOAPI_NOINIT
189 
190     /* Sanity check */
191     HDassert(f);
192     HDassert(ext_ptr);
193 
194     /* Check if extension was created */
195     if(was_created) {
196         /* Set the ring type in the DXPL */
197         if(H5AC_set_ring(dxpl_id, H5AC_RING_SBE, &dxpl, &orig_ring) < 0)
198             HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set ring value")
199 
200         /* Increment link count on superblock extension's object header */
201         if(H5O_link(ext_ptr, 1, dxpl_id) < 0)
202             HGOTO_ERROR(H5E_FILE, H5E_LINKCOUNT, FAIL, "unable to increment hard link count")
203 
204         /* Decrement refcount on superblock extension's object header in memory */
205         if(H5O_dec_rc_by_loc(ext_ptr, dxpl_id) < 0)
206             HGOTO_ERROR(H5E_FILE, H5E_CANTDEC, FAIL, "unable to decrement refcount on superblock extension");
207     } /* end if */
208 
209     /* Twiddle the number of open objects to avoid closing the file. */
210     f->nopen_objs++;
211     if(H5O_close(ext_ptr, NULL) < 0)
212         HGOTO_ERROR(H5E_FILE, H5E_CANTCLOSEOBJ, FAIL, "unable to close superblock extension")
213     f->nopen_objs--;
214 
215 done:
216     /* Reset the ring in the DXPL */
217     if(H5AC_reset_ring(dxpl, orig_ring) < 0)
218         HDONE_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set property value")
219 
220     FUNC_LEAVE_NOAPI(ret_value)
221 } /* H5F_super_ext_close() */
222 
223 
224 /*-------------------------------------------------------------------------
225  * Function:    H5F__update_super_ext_driver_msg
226  *
227  * Purpose:	Update the superblock extension file driver info message if
228  *		we are using a V 2 superblock.  Observe that the function
229  *		is a NO-OP if the file driver info message does not exist.
230  *              This is necessary, as the function is called whenever the
231  *		EOA is updated, and were it to create the file driver info
232  *		message, it would find itself in an infinite recursion.
233  *
234  * Return:      Success:        SUCCEED
235  *              Failure:        FAIL
236  *
237  * Programmer:  John Mainzer
238  *              11/10/15
239  *
240  *-------------------------------------------------------------------------
241  */
242 static herr_t
H5F__update_super_ext_driver_msg(H5F_t * f,hid_t dxpl_id)243 H5F__update_super_ext_driver_msg(H5F_t *f, hid_t dxpl_id)
244 {
245     H5F_super_t *sblock;        /* Pointer to the super block */
246     herr_t ret_value = SUCCEED; /* Return value */
247 
248     FUNC_ENTER_STATIC
249 
250     /* Sanity check */
251     HDassert(f);
252     HDassert(f->shared);
253     sblock = f->shared->sblock;
254     HDassert(sblock);
255     HDassert(sblock->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC);
256     HDassert(sblock->cache_info.type == H5AC_SUPERBLOCK);
257 
258     /* Update the driver information message in the superblock extension
259      * if appropriate.
260      */
261     if(sblock->super_vers >= HDF5_SUPERBLOCK_VERSION_2) {
262         if(H5F_addr_defined(sblock->ext_addr)) {
263             /* Check for ignoring the driver info for this file */
264             if(!H5F_HAS_FEATURE(f, H5FD_FEAT_IGNORE_DRVRINFO)) {
265                 size_t     driver_size;    /* Size of driver info block (bytes)*/
266 
267                 /* Check for driver info */
268                 H5_CHECKED_ASSIGN(driver_size, size_t, H5FD_sb_size(f->shared->lf), hsize_t);
269 
270 		/* Nothing to do unless there is both driver info and
271                  * the driver info superblock extension message has
272                  * already been created.
273                  */
274                 if(driver_size > 0) {
275                     H5O_drvinfo_t drvinfo;      /* Driver info */
276                     uint8_t dbuf[H5F_MAX_DRVINFOBLOCK_SIZE];  /* Driver info block encoding buffer */
277 
278                     /* Sanity check */
279                     HDassert(driver_size <= H5F_MAX_DRVINFOBLOCK_SIZE);
280 
281                     /* Encode driver-specific data */
282                     if(H5FD_sb_encode(f->shared->lf, drvinfo.name, dbuf) < 0)
283                         HGOTO_ERROR(H5E_FILE, H5E_CANTINIT, FAIL, "unable to encode driver information")
284 
285                     /* Write the message to the superblock extension.
286                      *
287                      * Note that the superblock extension and the
288                      * file driver info message must already exist.
289                      */
290                     drvinfo.len = driver_size;
291                     drvinfo.buf = dbuf;
292                     if(H5F_super_ext_write_msg(f, dxpl_id, H5O_DRVINFO_ID, &drvinfo, FALSE, H5O_MSG_NO_FLAGS_SET) < 0)
293                         HGOTO_ERROR(H5E_FILE, H5E_WRITEERROR, FAIL, "unable to update driver info header message")
294                 } /* end if driver_size > 0 */
295             } /* end if !H5F_HAS_FEATURE(f, H5FD_FEAT_IGNORE_DRVRINFO) */
296         } /* end if superblock extension exists */
297     } /* end if sblock->super_vers >= HDF5_SUPERBLOCK_VERSION_2 */
298 
299 done:
300     FUNC_LEAVE_NOAPI(ret_value)
301 } /* end H5F__update_super_ext_driver_msg() */
302 
303 
304 /*-------------------------------------------------------------------------
305  * Function:    H5F__super_read
306  *
307  * Purpose:     Reads the superblock from the file or from the BUF. If
308  *              ADDR is a valid address, then it reads it from the file.
309  *              If not, then BUF must be non-NULL for it to read from the
310  *              BUF.
311  *
312  * Return:      Success:        SUCCEED
313  *              Failure:        FAIL
314  *
315  * Programmer:  Bill Wendling
316  *              wendling@ncsa.uiuc.edu
317  *              Sept 12, 2003
318  *
319  *-------------------------------------------------------------------------
320  */
321 herr_t
H5F__super_read(H5F_t * f,hid_t meta_dxpl_id,hid_t raw_dxpl_id,hbool_t initial_read)322 H5F__super_read(H5F_t *f, hid_t meta_dxpl_id, hid_t raw_dxpl_id, hbool_t initial_read)
323 {
324     H5P_genplist_t     *dxpl = NULL;        /* DXPL object */
325     H5AC_ring_t         ring, orig_ring = H5AC_RING_INV;
326     H5F_super_t *       sblock = NULL;      /* Superblock structure */
327     H5F_superblock_cache_ud_t udata;        /* User data for cache callbacks */
328     H5P_genplist_t     *c_plist;            /* File creation property list  */
329     H5FD_io_info_t      fdio_info;          /* File driver I/O info */
330     unsigned            sblock_flags = H5AC__NO_FLAGS_SET;       /* flags used in superblock unprotect call      */
331     haddr_t             super_addr;         /* Absolute address of superblock */
332     haddr_t             eof;                /* End of file address */
333     unsigned      	rw_flags;           /* Read/write permissions for file */
334     hbool_t 		skip_eof_check = FALSE; /* Whether to skip checking the EOF value */
335     herr_t              ret_value = SUCCEED; /* Return value */
336 
337     FUNC_ENTER_PACKAGE_TAG(meta_dxpl_id, H5AC__SUPERBLOCK_TAG, FAIL)
338 
339     /* initialize the drvinfo to NULL -- we will overwrite this if there
340      * is a driver information block
341      */
342     f->shared->drvinfo = NULL;
343 
344     /* Get the DXPL plist object for DXPL ID */
345     if(NULL == (dxpl = (H5P_genplist_t *)H5I_object(meta_dxpl_id)))
346         HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "can't get property list")
347     if((H5P_get(dxpl, H5AC_RING_NAME, &orig_ring)) < 0)
348         HGOTO_ERROR(H5E_PLIST, H5E_CANTGET, FAIL, "unable to get property value");
349 
350     /* Set up file driver I/O info */
351     fdio_info.file = f->shared->lf;
352     fdio_info.meta_dxpl = dxpl;
353     if(NULL == (fdio_info.raw_dxpl = (H5P_genplist_t *)H5I_object(raw_dxpl_id)))
354         HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "can't get property list")
355 
356     /* Find the superblock */
357     if(H5FD_locate_signature(&fdio_info, &super_addr) < 0)
358         HGOTO_ERROR(H5E_FILE, H5E_NOTHDF5, FAIL, "unable to locate file signature")
359     if(HADDR_UNDEF == super_addr)
360         HGOTO_ERROR(H5E_FILE, H5E_NOTHDF5, FAIL, "file signature not found")
361 
362     /* Check for userblock present */
363     if(H5F_addr_gt(super_addr, 0)) {
364         /* Set the base address for the file in the VFD now */
365         if(H5F__set_base_addr(f, super_addr) < 0)
366             HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "failed to set base address for file driver")
367     } /* end if */
368 
369     /* Determine file intent for superblock protect */
370 
371     /* Must tell cache at protect time that the super block is to be
372      * flushed last (and collectively in the parallel case).
373      */
374     rw_flags = H5AC__FLUSH_LAST_FLAG;
375 #ifdef H5_HAVE_PARALLEL
376     rw_flags |= H5C__FLUSH_COLLECTIVELY_FLAG;
377 #endif /* H5_HAVE_PARALLEL */
378     if(!(H5F_INTENT(f) & H5F_ACC_RDWR))
379         rw_flags |= H5AC__READ_ONLY_FLAG;
380 
381     /* Get the shared file creation property list */
382     if(NULL == (c_plist = (H5P_genplist_t *)H5I_object(f->shared->fcpl_id)))
383         HGOTO_ERROR(H5E_FILE, H5E_BADTYPE, FAIL, "can't get property list")
384 
385     /* Make certain we can read the fixed-size portion of the superblock */
386     if(H5F__set_eoa(f, H5FD_MEM_SUPER,
387               (haddr_t)(H5F_SUPERBLOCK_FIXED_SIZE + H5F_SUPERBLOCK_MINIMAL_VARLEN_SIZE)) < 0)
388         HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "set end of space allocation request failed")
389 
390     /* Set up the user data for cache callbacks */
391     udata.f = f;
392     udata.ignore_drvrinfo = H5F_HAS_FEATURE(f, H5FD_FEAT_IGNORE_DRVRINFO);
393     udata.sym_leaf_k = 0;
394     if(H5P_get(c_plist, H5F_CRT_BTREE_RANK_NAME, udata.btree_k) < 0)
395         HGOTO_ERROR(H5E_FILE, H5E_CANTGET, FAIL, "unable to get rank for btree internal nodes")
396     udata.stored_eof = HADDR_UNDEF;
397     udata.drvrinfo_removed = FALSE;
398 
399     /* Set the ring type in the DXPL */
400     ring = H5AC_RING_SB;
401     if((H5P_set(dxpl, H5AC_RING_NAME, &ring)) < 0)
402         HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, FAIL, "unable to set property value");
403 
404     /* Look up the superblock */
405     if(NULL == (sblock = (H5F_super_t *)H5AC_protect(f, meta_dxpl_id, H5AC_SUPERBLOCK, (haddr_t)0, &udata, rw_flags)))
406         HGOTO_ERROR(H5E_FILE, H5E_CANTPROTECT, FAIL, "unable to load superblock")
407 
408     if(H5F_INTENT(f) & H5F_ACC_SWMR_WRITE)
409        	if(sblock->super_vers < HDF5_SUPERBLOCK_VERSION_3)
410 	    HGOTO_ERROR(H5E_FILE, H5E_CANTPROTECT, FAIL, "invalid superblock version for SWMR_WRITE")
411 
412     /* Enable all latest version support when file has v3 superblock */
413     if(sblock->super_vers >= HDF5_SUPERBLOCK_VERSION_3)
414 	f->shared->latest_flags |= H5F_LATEST_ALL_FLAGS;
415 
416     /* Pin the superblock in the cache */
417     if(H5AC_pin_protected_entry(sblock) < 0)
418         HGOTO_ERROR(H5E_FILE, H5E_CANTPIN, FAIL, "unable to pin superblock")
419 
420     /* Mark the superblock dirty if it was modified during loading */
421     if(((rw_flags & H5AC__READ_ONLY_FLAG) == 0) && udata.ignore_drvrinfo && udata.drvrinfo_removed) {
422         HDassert(sblock->super_vers < HDF5_SUPERBLOCK_VERSION_2);
423         sblock_flags |= H5AC__DIRTIED_FLAG;
424     } /* end if */
425 
426     /* The superblock must be flushed last (and collectively in parallel) */
427     sblock_flags |= H5AC__FLUSH_LAST_FLAG;
428 #ifdef H5_HAVE_PARALLEL
429     sblock_flags |= H5AC__FLUSH_COLLECTIVELY_FLAG;
430 #endif /* H5_HAVE_PARALLEL */
431 
432     /* Check if superblock address is different from base address and adjust
433      * base address and "end of address" address if so.
434      */
435     if(!H5F_addr_eq(super_addr, sblock->base_addr)) {
436         /* Check if the superblock moved earlier in the file */
437         if(H5F_addr_lt(super_addr, sblock->base_addr))
438             udata.stored_eof -= (sblock->base_addr - super_addr);
439         else
440             /* The superblock moved later in the file */
441             udata.stored_eof += (super_addr - sblock->base_addr);
442 
443         /* Adjust base address for offsets of the HDF5 data in the file */
444         sblock->base_addr = super_addr;
445 
446         /* Set the base address for the file in the VFD now */
447         if(H5F__set_base_addr(f, sblock->base_addr) < 0)
448             HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "failed to set base address for file driver")
449 
450         /* Indicate that the superblock should be marked dirty */
451         if((rw_flags & H5AC__READ_ONLY_FLAG) == 0)
452             sblock_flags |= H5AC__DIRTIED_FLAG;
453     } /* end if */
454 
455     /* Set information in the file's creation property list */
456     if(H5P_set(c_plist, H5F_CRT_SUPER_VERS_NAME, &sblock->super_vers) < 0)
457         HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set superblock version")
458     if(H5P_set(c_plist, H5F_CRT_ADDR_BYTE_NUM_NAME, &sblock->sizeof_addr) < 0)
459         HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set byte number in an address")
460     if(H5P_set(c_plist, H5F_CRT_OBJ_BYTE_NUM_NAME, &sblock->sizeof_size) < 0)
461         HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set byte number for object size")
462 
463     /* Handle the B-tree 'K' values */
464     if(sblock->super_vers < HDF5_SUPERBLOCK_VERSION_2) {
465         /* Sanity check */
466         HDassert(udata.sym_leaf_k != 0);
467 
468         /* Set the symbol table internal node 'K' value */
469         if(H5P_set(c_plist, H5F_CRT_SYM_LEAF_NAME, &udata.sym_leaf_k) < 0)
470             HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set rank for symbol table leaf nodes")
471         sblock->sym_leaf_k = udata.sym_leaf_k;
472 
473         /* Set the B-tree internal node values, etc */
474         if(H5P_set(c_plist, H5F_CRT_BTREE_RANK_NAME, udata.btree_k) < 0)
475             HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set rank for btree internal nodes")
476         HDmemcpy(sblock->btree_k, udata.btree_k, sizeof(unsigned) * (size_t)H5B_NUM_BTREE_ID);
477     } /* end if */
478     else {
479         /* Get the (default) B-tree internal node values, etc */
480         /* (Note: these may be reset in a superblock extension) */
481         if(H5P_get(c_plist, H5F_CRT_BTREE_RANK_NAME, sblock->btree_k) < 0)
482             HGOTO_ERROR(H5E_FILE, H5E_CANTGET, FAIL, "unable to get rank for btree internal nodes")
483         if(H5P_get(c_plist, H5F_CRT_SYM_LEAF_NAME, &sblock->sym_leaf_k) < 0)
484             HGOTO_ERROR(H5E_FILE, H5E_CANTGET, FAIL, "unable to get rank for btree internal nodes")
485     } /* end else */
486 
487     /*
488      * The user-defined data is the area of the file before the base
489      * address.
490      */
491     if(H5P_set(c_plist, H5F_CRT_USER_BLOCK_NAME, &sblock->base_addr) < 0)
492         HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set userblock size")
493 
494     /*
495      * Make sure that the data is not truncated. One case where this is
496      * possible is if the first file of a family of files was opened
497      * individually.
498      */
499     /* Can skip this test when it is not the initial file open--
500      * H5F__super_read() call from H5F_evict_tagged_metadata() for
501      * refreshing object.
502      * When flushing file buffers and fractal heap is involved,
503      * the library will allocate actual space for tmp addresses
504      * via the file layer.  The aggregator allocates a block,
505      * thus the eoa might be greater than eof.
506      * Note: the aggregator is changed again after being reset
507      * earlier before H5AC_flush due to allocation of tmp addresses.
508      */
509     /* The EOF check must be skipped when the file is opened for SWMR read,
510      * as the file can appear truncated if only part of it has been
511      * been flushed to disk by the SWMR writer process.
512      */
513     if(H5F_INTENT(f) & H5F_ACC_SWMR_READ) {
514 	/*
515 	 * When the file is opened for SWMR read access, skip the check if:
516 	 * --the file is already marked for SWMR writing and
517 	 * --the file has version 3 superblock for SWMR support
518 	 */
519 	if((sblock->status_flags & H5F_SUPER_SWMR_WRITE_ACCESS) &&
520                (sblock->status_flags & H5F_SUPER_WRITE_ACCESS) &&
521                 sblock->super_vers >= HDF5_SUPERBLOCK_VERSION_3)
522 	    skip_eof_check = TRUE;
523     } /* end if */
524     if(!skip_eof_check && initial_read) {
525         if(HADDR_UNDEF == (eof = H5FD_get_eof(f->shared->lf, H5FD_MEM_DEFAULT)))
526             HGOTO_ERROR(H5E_FILE, H5E_CANTGET, FAIL, "unable to determine file size")
527 
528         /* (Account for the stored EOA being absolute offset -QAK) */
529         if((eof + sblock->base_addr) < udata.stored_eof)
530             HGOTO_ERROR(H5E_FILE, H5E_TRUNCATED, FAIL, "truncated file: eof = %llu, sblock->base_addr = %llu, stored_eof = %llu", (unsigned long long)eof, (unsigned long long)sblock->base_addr, (unsigned long long)udata.stored_eof)
531     } /* end if */
532 
533     /*
534      * Tell the file driver how much address space has already been
535      * allocated so that it knows how to allocate additional memory.
536      */
537 
538     /* Set the ring type in the DXPL */
539     ring = H5AC_RING_SBE;
540     if((H5P_set(dxpl, H5AC_RING_NAME, &ring)) < 0)
541         HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, FAIL, "unable to set property value");
542 
543     /* Decode the optional driver information block */
544     if(H5F_addr_defined(sblock->driver_addr)) {
545         H5O_drvinfo_t *drvinfo;         /* Driver info */
546         H5F_drvrinfo_cache_ud_t drvrinfo_udata;  /* User data for metadata callbacks */
547         unsigned drvinfo_flags = H5AC__NO_FLAGS_SET;    /* Flags used in driver info block unprotect call */
548 
549         /* Sanity check - driver info block should only be defined for
550          *      superblock version < 2.
551          */
552         HDassert(sblock->super_vers < HDF5_SUPERBLOCK_VERSION_2);
553 
554         /* Set up user data */
555         drvrinfo_udata.f           = f;
556         drvrinfo_udata.driver_addr = sblock->driver_addr;
557 
558         /* extend EOA so we can read at least the fixed sized
559          * portion of the driver info block
560          */
561         if(H5FD_set_eoa(f->shared->lf, H5FD_MEM_SUPER, sblock->driver_addr + H5F_DRVINFOBLOCK_HDR_SIZE) < 0) /* will extend eoa later if required */
562             HGOTO_ERROR(H5E_FILE, H5E_CANTINIT, FAIL, "set end of space allocation request failed")
563 
564         /* Look up the driver info block */
565         if(NULL == (drvinfo = (H5O_drvinfo_t *)H5AC_protect(f, meta_dxpl_id, H5AC_DRVRINFO, sblock->driver_addr, &drvrinfo_udata, rw_flags)))
566             HGOTO_ERROR(H5E_FILE, H5E_CANTPROTECT, FAIL, "unable to load driver info block")
567 
568         /* Loading the driver info block is enough to set up the right info */
569 
570         /* Check if we need to rewrite the driver info block info */
571         if(((rw_flags & H5AC__READ_ONLY_FLAG) == 0) && H5F_HAS_FEATURE(f, H5FD_FEAT_DIRTY_DRVRINFO_LOAD))
572             drvinfo_flags |= H5AC__DIRTIED_FLAG;
573 
574         /* set the pin entry flag so that the driver information block
575          * cache entry will be pinned in the cache.
576          */
577         drvinfo_flags |= H5AC__PIN_ENTRY_FLAG;
578 
579         /* Release the driver info block */
580         if(H5AC_unprotect(f, meta_dxpl_id, H5AC_DRVRINFO, sblock->driver_addr, drvinfo, drvinfo_flags) < 0)
581             HGOTO_ERROR(H5E_FILE, H5E_CANTUNPROTECT, FAIL, "unable to release driver info block")
582 
583         /* save a pointer to the driver information cache entry */
584         f->shared->drvinfo = drvinfo;
585     } /* end if */
586 
587     /* (Account for the stored EOA being absolute offset -NAF) */
588     if(H5F__set_eoa(f, H5FD_MEM_DEFAULT, udata.stored_eof - sblock->base_addr) < 0)
589         HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set end-of-address marker for file")
590 
591     /* Decode the optional superblock extension info */
592     if(H5F_addr_defined(sblock->ext_addr)) {
593         H5O_loc_t ext_loc;      /* "Object location" for superblock extension */
594         H5O_btreek_t btreek;    /* v1 B-tree 'K' value message from superblock extension */
595         H5O_drvinfo_t drvinfo;  /* Driver info message from superblock extension */
596 	size_t u; 		/* Local index variable */
597         htri_t status;          /* Status for message existing */
598 
599         /* Sanity check - superblock extension should only be defined for
600          *      superblock version >= 2.
601          */
602         HDassert(sblock->super_vers >= HDF5_SUPERBLOCK_VERSION_2);
603 
604         /* Check for superblock extension being located "outside" the stored
605          *      'eoa' value, which can occur with the split/multi VFD.
606          */
607         if(H5F_addr_gt(sblock->ext_addr, udata.stored_eof)) {
608             /* Set the 'eoa' for the object header memory type large enough
609              *  to give some room for a reasonably sized superblock extension.
610              *  (This is _rather_ a kludge -QAK)
611              */
612             if(H5F__set_eoa(f, H5FD_MEM_OHDR, (haddr_t)(sblock->ext_addr + 1024)) < 0)
613                 HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set end-of-address marker for file")
614         } /* end if */
615 
616         /* Open the superblock extension */
617 	if(H5F_super_ext_open(f, sblock->ext_addr, &ext_loc) < 0)
618             HGOTO_ERROR(H5E_FILE, H5E_CANTOPENOBJ, FAIL, "unable to open file's superblock extension")
619 
620         /* Check for the extension having a 'driver info' message */
621         if((status = H5O_msg_exists(&ext_loc, H5O_DRVINFO_ID, meta_dxpl_id)) < 0)
622             HGOTO_ERROR(H5E_FILE, H5E_EXISTS, FAIL, "unable to read object header")
623         if(status) {
624             /* Check for ignoring the driver info for this file */
625             if(!udata.ignore_drvrinfo) {
626 
627                 /* Retrieve the 'driver info' structure */
628                 if(NULL == H5O_msg_read(&ext_loc, H5O_DRVINFO_ID, &drvinfo, meta_dxpl_id))
629                     HGOTO_ERROR(H5E_FILE, H5E_CANTGET, FAIL, "driver info message not present")
630 
631                 /* Validate and decode driver information */
632                 if(H5FD_sb_load(f->shared->lf, drvinfo.name, drvinfo.buf) < 0) {
633                     H5O_msg_reset(H5O_DRVINFO_ID, &drvinfo);
634                     HGOTO_ERROR(H5E_FILE, H5E_CANTDECODE, FAIL, "unable to decode driver information")
635                 } /* end if */
636 
637                 /* Reset driver info message */
638                 H5O_msg_reset(H5O_DRVINFO_ID, &drvinfo);
639 
640 		HDassert(FALSE == f->shared->drvinfo_sb_msg_exists);
641 		f->shared->drvinfo_sb_msg_exists = TRUE;
642             } /* end else */
643         } /* end if */
644 
645         /* Read in the shared OH message information if there is any */
646         if(H5SM_get_info(&ext_loc, c_plist, meta_dxpl_id) < 0)
647             HGOTO_ERROR(H5E_FILE, H5E_CANTGET, FAIL, "unable to read SOHM table information")
648 
649         /* Check for the extension having a 'v1 B-tree "K"' message */
650         if((status = H5O_msg_exists(&ext_loc, H5O_BTREEK_ID, meta_dxpl_id)) < 0)
651             HGOTO_ERROR(H5E_FILE, H5E_EXISTS, FAIL, "unable to read object header")
652         if(status) {
653             /* Retrieve the 'v1 B-tree "K"' structure */
654             if(NULL == H5O_msg_read(&ext_loc, H5O_BTREEK_ID, &btreek, meta_dxpl_id))
655                 HGOTO_ERROR(H5E_FILE, H5E_CANTGET, FAIL, "v1 B-tree 'K' info message not present")
656 
657             /* Set non-default v1 B-tree 'K' value info from file */
658             sblock->btree_k[H5B_CHUNK_ID] = btreek.btree_k[H5B_CHUNK_ID];
659             sblock->btree_k[H5B_SNODE_ID] = btreek.btree_k[H5B_SNODE_ID];
660             sblock->sym_leaf_k = btreek.sym_leaf_k;
661 
662             /* Set non-default v1 B-tree 'K' values in the property list */
663             if(H5P_set(c_plist, H5F_CRT_BTREE_RANK_NAME, btreek.btree_k) < 0)
664                 HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set rank for btree internal nodes")
665             if(H5P_set(c_plist, H5F_CRT_SYM_LEAF_NAME, &btreek.sym_leaf_k) < 0)
666                 HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set rank for symbol table leaf nodes")
667         } /* end if */
668 
669         /* Check for the extension having a 'free-space manager info' message */
670         if((status = H5O_msg_exists(&ext_loc, H5O_FSINFO_ID, meta_dxpl_id)) < 0)
671             HGOTO_ERROR(H5E_FILE, H5E_EXISTS, FAIL, "unable to read object header")
672         if(status) {
673             H5O_fsinfo_t fsinfo;   /* File space info message from superblock extension */
674             uint8_t flags;          /* Message flags */
675 
676             /* Get message flags */
677             if(H5O_msg_get_flags(&ext_loc, H5O_FSINFO_ID, meta_dxpl_id, &flags) < 0)
678                 HGOTO_ERROR(H5E_FILE, H5E_CANTGET, FAIL, "unable to message flags for free-space manager info message")
679 
680             /* If message is NOT marked "unknown"--set up file space info  */
681             if(!(flags & H5O_MSG_FLAG_WAS_UNKNOWN)) {
682 
683                 /* Retrieve the 'file space info' structure */
684                 if(NULL == H5O_msg_read(&ext_loc, H5O_FSINFO_ID, &fsinfo, meta_dxpl_id))
685                     HGOTO_ERROR(H5E_FILE, H5E_CANTGET, FAIL, "unable to get free-space manager info message")
686 
687                 /* Update changed values */
688                 if(f->shared->fs_strategy != fsinfo.strategy) {
689                     f->shared->fs_strategy = fsinfo.strategy;
690 
691                     /* Set non-default strategy in the property list */
692                     if(H5P_set(c_plist, H5F_CRT_FILE_SPACE_STRATEGY_NAME, &fsinfo.strategy) < 0)
693                         HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set file space strategy")
694                 } /* end if */
695                 if(f->shared->fs_persist != fsinfo.persist) {
696                     f->shared->fs_persist = fsinfo.persist;
697 
698                     /* Set non-default strategy in the property list */
699                     if(H5P_set(c_plist, H5F_CRT_FREE_SPACE_PERSIST_NAME, &fsinfo.persist) < 0)
700                         HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set file space strategy")
701                 } /* end if */
702                 if(f->shared->fs_threshold != fsinfo.threshold) {
703                     f->shared->fs_threshold = fsinfo.threshold;
704 
705                     /* Set non-default threshold in the property list */
706                     if(H5P_set(c_plist, H5F_CRT_FREE_SPACE_THRESHOLD_NAME, &fsinfo.threshold) < 0)
707                         HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set file space strategy")
708                 } /* end if */
709 
710                 HDassert(f->shared->fs_page_size >= H5F_FILE_SPACE_PAGE_SIZE_MIN);
711                 HDassert(fsinfo.page_size >= H5F_FILE_SPACE_PAGE_SIZE_MIN);
712                 if(f->shared->fs_page_size != fsinfo.page_size) {
713                     f->shared->fs_page_size = fsinfo.page_size;
714 
715                     /* Set file space page size in the property list */
716                     if(H5P_set(c_plist, H5F_CRT_FILE_SPACE_PAGE_SIZE_NAME, &fsinfo.page_size) < 0)
717                         HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set file space page size")
718                 } /* end if */
719                 if(f->shared->pgend_meta_thres != fsinfo.pgend_meta_thres)
720                     /* Initialize page end meta threshold */
721                     f->shared->pgend_meta_thres = fsinfo.pgend_meta_thres;
722 
723                 if(f->shared->eoa_pre_fsm_fsalloc != fsinfo.eoa_pre_fsm_fsalloc)
724                     f->shared->eoa_pre_fsm_fsalloc = fsinfo.eoa_pre_fsm_fsalloc;
725 
726                 /* f->shared->eoa_pre_fsm_fsalloc must always be HADDR_UNDEF
727                  * in the absence of persistant free space managers.
728                  */
729                 HDassert((!f->shared->fs_persist) || (f->shared->eoa_pre_fsm_fsalloc != HADDR_UNDEF));
730                 HDassert(!f->shared->first_alloc_dealloc);
731 
732                 if((f->shared->eoa_pre_fsm_fsalloc != HADDR_UNDEF) &&
733                    (H5F_INTENT(f) & H5F_ACC_RDWR))
734                     f->shared->first_alloc_dealloc = TRUE;
735 
736                 f->shared->fs_addr[0] = HADDR_UNDEF;
737                 for(u = 1; u < NELMTS(f->shared->fs_addr); u++)
738                     f->shared->fs_addr[u] = fsinfo.fs_addr[u - 1];
739 
740                 if(fsinfo.mapped && (rw_flags & H5AC__READ_ONLY_FLAG) == 0) {
741 
742                     /* Do the same kluge until we know for sure.  VC */
743 #if 1 /* bug fix test code -- tidy this up if all goes well */ /* JRM */
744                     /* KLUGE ALERT!!
745                      *
746                      * H5F_super_ext_write_msg() expects f->shared->sblock to
747                      * be set -- verify that it is NULL, and then set it.
748                      * Set it back to NULL when we are done.
749                      */
750                      HDassert(f->shared->sblock == NULL);
751                      f->shared->sblock = sblock;
752 #endif /* JRM */
753 
754                     if(H5F_super_ext_remove_msg(f, meta_dxpl_id, H5O_FSINFO_ID) < 0)
755                         HGOTO_ERROR(H5E_FILE, H5E_CANTDELETE, FAIL,  "error in removing message from superblock extension")
756 
757                     if(H5F_super_ext_write_msg(f, meta_dxpl_id, H5O_FSINFO_ID, &fsinfo, TRUE, H5O_MSG_FLAG_MARK_IF_UNKNOWN) < 0)
758                         HGOTO_ERROR(H5E_FILE, H5E_WRITEERROR, FAIL, "error in writing fsinfo message to superblock extension")
759 #if 1 /* bug fix test code -- tidy this up if all goes well */ /* JRM */
760                     f->shared->sblock = NULL;
761 #endif /* JRM */
762 
763                 }
764             } /* end if not marked "unknown" */
765         } /* end if */
766 
767 	/* Check for the extension having a 'metadata cache image' message */
768         if((status = H5O_msg_exists(&ext_loc, H5O_MDCI_MSG_ID, meta_dxpl_id)) < 0)
769             HGOTO_ERROR(H5E_FILE, H5E_EXISTS, FAIL, "unable to read object header")
770         if(status) {
771             hbool_t 	rw = ((rw_flags & H5AC__READ_ONLY_FLAG) == 0);
772 	    H5O_mdci_t  mdci_msg;
773 
774 	    /* if the metadata cache image superblock extension message exists,
775              * read its contents and pass the data on to the metadata cache.
776              * Given this data, the cache will load and decode the metadata
777  	     * cache image block, decoded it and load its contents into the
778 	     * the cache on the test protect call.
779              *
780              * Further, if the file is opened R/W, the metadata cache will
781 	     * delete the metadata cache image superblock extension and free
782 	     * the cache image block.  Don't do this now as f->shared
783 	     * is not fully setup, which complicates matters.
784              */
785 
786             /* Retrieve the 'metadata cache image message' structure */
787 	    if(NULL == H5O_msg_read(&ext_loc, H5O_MDCI_MSG_ID, &mdci_msg, meta_dxpl_id))
788                 HGOTO_ERROR(H5E_FILE, H5E_CANTGET, FAIL, "unable to get metadata cache image message")
789 
790             /* Indicate to the cache that there's an image to load on first protect call */
791             if(H5AC_load_cache_image_on_next_protect(f, mdci_msg.addr, mdci_msg.size, rw) < 0)
792 		HGOTO_ERROR(H5E_FILE, H5E_CANTLOAD, FAIL, "call to H5AC_load_cache_image_on_next_protect failed");
793         } /* end if */
794 
795         /* Close superblock extension */
796         if(H5F_super_ext_close(f, &ext_loc, meta_dxpl_id, FALSE) < 0)
797 	    HGOTO_ERROR(H5E_FILE, H5E_CANTCLOSEOBJ, FAIL, "unable to close file's superblock extension")
798     } /* end if */
799 
800     /* Update the driver info if VFD indicated to do so */
801     /* (NOTE: only for later versions of superblock, earlier versions are handled
802      *          earlier in this routine.
803      */
804     if(((rw_flags & H5AC__READ_ONLY_FLAG) == 0) &&
805             sblock->super_vers >= HDF5_SUPERBLOCK_VERSION_2 &&
806             H5F_addr_defined(sblock->ext_addr)) {
807         /* Check for modifying the driver info when opening the file */
808         if(H5F_HAS_FEATURE(f, H5FD_FEAT_DIRTY_DRVRINFO_LOAD)) {
809             size_t          driver_size;    /* Size of driver info block (bytes) */
810 
811             /* Check for driver info message */
812             H5_CHECKED_ASSIGN(driver_size, size_t, H5FD_sb_size(f->shared->lf), hsize_t);
813             if(driver_size > 0) {
814                 H5O_drvinfo_t drvinfo;      /* Driver info */
815                 uint8_t dbuf[H5F_MAX_DRVINFOBLOCK_SIZE];  /* Driver info block encoding buffer */
816 
817                 /* Sanity check */
818                 HDassert(driver_size <= H5F_MAX_DRVINFOBLOCK_SIZE);
819 
820                 /* Encode driver-specific data */
821                 if(H5FD_sb_encode(f->shared->lf, drvinfo.name, dbuf) < 0)
822                     HGOTO_ERROR(H5E_FILE, H5E_CANTINIT, FAIL, "unable to encode driver information")
823 
824                 /* Set the driver info information for the superblock extension */
825                 drvinfo.len = driver_size;
826                 drvinfo.buf = dbuf;
827 
828                 /* Write driver info information to the superblock extension */
829 
830 #if 1 /* bug fix test code -- tidy this up if all goes well */ /* JRM */
831 		/* KLUGE ALERT!!
832 		 *
833 		 * H5F_super_ext_write_msg() expects f->shared->sblock to
834 		 * be set -- verify that it is NULL, and then set it.
835 		 * Set it back to NULL when we are done.
836 		 */
837 		HDassert(f->shared->sblock == NULL);
838 		f->shared->sblock = sblock;
839 #endif /* JRM */
840 		if(H5F_super_ext_write_msg(f, meta_dxpl_id, H5O_DRVINFO_ID, &drvinfo, FALSE, H5O_MSG_NO_FLAGS_SET) < 0)
841                     HGOTO_ERROR(H5E_FILE, H5E_WRITEERROR, FAIL, "error in writing message to superblock extension")
842 
843 #if 1 /* bug fix test code -- tidy this up if all goes well */ /* JRM */
844 		f->shared->sblock = NULL;
845 #endif /* JRM */
846 
847             } /* end if */
848         } /* end if */
849         /* Check for eliminating the driver info block */
850         else if(H5F_HAS_FEATURE(f, H5FD_FEAT_IGNORE_DRVRINFO)) {
851             /* Remove the driver info message from the superblock extension */
852             if(H5F_super_ext_remove_msg(f, meta_dxpl_id, H5O_DRVINFO_ID) < 0)
853                 HGOTO_ERROR(H5E_FILE, H5E_CANTRELEASE, FAIL, "error in removing message from superblock extension")
854 
855             /* Check if the superblock extension was removed */
856             if(!H5F_addr_defined(sblock->ext_addr))
857                 sblock_flags |= H5AC__DIRTIED_FLAG;
858         } /* end if */
859     } /* end if */
860 
861     /* Set the pointer to the pinned superblock */
862     f->shared->sblock = sblock;
863 
864     /* Set the page aggregation mode */
865     if(H5F__set_paged_aggr(f, (hbool_t)H5F_PAGED_AGGR(f)) < 0)
866         HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "failed to set paged_aggr status for file driver")
867 
868 done:
869     /* Reset the ring in the DXPL */
870     if(H5AC_reset_ring(dxpl, orig_ring) < 0)
871         HDONE_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set property value")
872 
873     /* Release the superblock */
874     if(sblock && H5AC_unprotect(f, meta_dxpl_id, H5AC_SUPERBLOCK, (haddr_t)0, sblock, sblock_flags) < 0)
875         HDONE_ERROR(H5E_FILE, H5E_CANTUNPROTECT, FAIL, "unable to close superblock")
876 
877     /* If we have failed, make sure no entries are left in the
878      * metadata cache, so that it can be shut down and discarded.
879      */
880     if(ret_value < 0) {
881         /* Unpin and discard drvinfo cache entry */
882         if(f->shared->drvinfo) {
883             if(H5AC_unpin_entry(f->shared->drvinfo) < 0)
884                 HDONE_ERROR(H5E_FILE, H5E_CANTUNPIN, FAIL, "unable to unpin driver info")
885 
886             /* Evict the driver info block from the cache */
887             if(H5AC_expunge_entry(f, meta_dxpl_id, H5AC_DRVRINFO, sblock->driver_addr, H5AC__NO_FLAGS_SET) < 0)
888                 HDONE_ERROR(H5E_FILE, H5E_CANTEXPUNGE, FAIL, "unable to expunge driver info block")
889         } /* end if */
890 
891         /* Unpin & discard superblock */
892         if(sblock) {
893             /* Unpin superblock in cache */
894             if(H5AC_unpin_entry(sblock) < 0)
895                 HDONE_ERROR(H5E_FILE, H5E_CANTUNPIN, FAIL, "unable to unpin superblock")
896 
897             /* Evict the superblock from the cache */
898             if(H5AC_expunge_entry(f, meta_dxpl_id, H5AC_SUPERBLOCK, (haddr_t)0, H5AC__NO_FLAGS_SET) < 0)
899                 HDONE_ERROR(H5E_FILE, H5E_CANTEXPUNGE, FAIL, "unable to expunge superblock")
900         } /* end if */
901     } /* end if */
902 
903     FUNC_LEAVE_NOAPI_TAG(ret_value, FAIL)
904 } /* end H5F__super_read() */
905 
906 
907 /*-------------------------------------------------------------------------
908  * Function:    H5F__super_init
909  *
910  * Purpose:     Allocates the superblock for the file and initializes
911  *              information about the superblock in memory.  Writes extension
912  *              messages if any are needed.
913  *
914  * Return:      Success:        SUCCEED
915  *              Failure:        FAIL
916  *
917  * Programmer:  Quincey Koziol
918  *              koziol@ncsa.uiuc.edu
919  *              Sept 15, 2003
920  *
921  *-------------------------------------------------------------------------
922  */
923 herr_t
H5F__super_init(H5F_t * f,hid_t dxpl_id)924 H5F__super_init(H5F_t *f, hid_t dxpl_id)
925 {
926     H5F_super_t    *sblock = NULL;      /* Superblock cache structure                 */
927     hbool_t         sblock_in_cache = FALSE;    /* Whether the superblock has been inserted into the metadata cache */
928     H5O_drvinfo_t  *drvinfo = NULL;     /* Driver info */
929     hbool_t         drvinfo_in_cache = FALSE;   /* Whether the driver info block has been inserted into the metadata cache */
930     H5P_genplist_t *plist;              /* File creation property list                */
931     H5P_genplist_t *dxpl = NULL;
932     H5AC_ring_t ring, orig_ring = H5AC_RING_INV;
933     hsize_t         userblock_size;     /* Size of userblock, in bytes                */
934     hsize_t         superblock_size;    /* Size of superblock, in bytes               */
935     size_t          driver_size;        /* Size of driver info block (bytes)          */
936     unsigned super_vers = HDF5_SUPERBLOCK_VERSION_DEF; /* Superblock version for file */
937     H5O_loc_t       ext_loc;            /* Superblock extension object location */
938     hbool_t         need_ext;           /* Whether the superblock extension is needed */
939     hbool_t         ext_created = FALSE; /* Whether the extension has been created */
940     hbool_t         non_default_fs_settings = FALSE;    /* Whether the file has non-default free-space settings */
941     herr_t          ret_value = SUCCEED; /* Return Value                              */
942 
943     FUNC_ENTER_PACKAGE_TAG(dxpl_id, H5AC__SUPERBLOCK_TAG, FAIL)
944 
945     /* Allocate space for the superblock */
946     if(NULL == (sblock = H5FL_CALLOC(H5F_super_t)))
947         HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed")
948 
949     /* Initialize various address information */
950     sblock->base_addr = HADDR_UNDEF;
951     sblock->ext_addr = HADDR_UNDEF;
952     sblock->driver_addr = HADDR_UNDEF;
953     sblock->root_addr = HADDR_UNDEF;
954 
955     /* Get the shared file creation property list */
956     if(NULL == (plist = (H5P_genplist_t *)H5I_object(f->shared->fcpl_id)))
957         HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a property list")
958 
959     /* Initialize sym_leaf_k */
960     if(H5P_get(plist, H5F_CRT_SYM_LEAF_NAME, &sblock->sym_leaf_k) < 0)
961         HGOTO_ERROR(H5E_PLIST, H5E_CANTGET, FAIL, "can't get byte number for object size")
962 
963     /* Initialize btree_k */
964     if(H5P_get(plist, H5F_CRT_BTREE_RANK_NAME, &sblock->btree_k[0]) < 0)
965         HGOTO_ERROR(H5E_PLIST, H5E_CANTGET, FAIL, "unable to get rank for btree internal nodes")
966 
967     /* Check for non-default free-space settings */
968     if(!(f->shared->fs_strategy == H5F_FILE_SPACE_STRATEGY_DEF &&
969             f->shared->fs_persist == H5F_FREE_SPACE_PERSIST_DEF &&
970             f->shared->fs_threshold == H5F_FREE_SPACE_THRESHOLD_DEF &&
971 	    f->shared->fs_page_size == H5F_FILE_SPACE_PAGE_SIZE_DEF))
972         non_default_fs_settings = TRUE;
973 
974     /* Bump superblock version if latest superblock version support is enabled */
975     if(H5F_USE_LATEST_FLAGS(f, H5F_LATEST_SUPERBLOCK))
976         super_vers = HDF5_SUPERBLOCK_VERSION_LATEST;
977     /* Bump superblock version to create superblock extension for SOHM info */
978     else if(f->shared->sohm_nindexes > 0)
979         super_vers = HDF5_SUPERBLOCK_VERSION_2;
980     /*
981      *	Bump superblock version to create superblock extension for:
982      * 	-- non-default file space strategy or
983      * 	-- non-default persisting free-space or
984      *  -- non-default free-space threshold or
985      *  -- non-default page_size
986      */
987     else if(non_default_fs_settings)
988         super_vers = HDF5_SUPERBLOCK_VERSION_2;
989     /* Check for non-default indexed storage B-tree internal 'K' value
990      * and set the version # of the superblock to 1 if it is a non-default
991      * value.
992      */
993     else if(sblock->btree_k[H5B_CHUNK_ID] != HDF5_BTREE_CHUNK_IK_DEF)
994         super_vers = HDF5_SUPERBLOCK_VERSION_1;
995 
996     /* If a newer superblock version is required, set it here */
997     if(super_vers != HDF5_SUPERBLOCK_VERSION_DEF) {
998         H5P_genplist_t *c_plist;              /* Property list */
999 
1000         if(NULL == (c_plist = (H5P_genplist_t *)H5I_object(f->shared->fcpl_id)))
1001             HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not property list")
1002         if(H5P_set(c_plist, H5F_CRT_SUPER_VERS_NAME, &super_vers) < 0)
1003             HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, FAIL, "unable to set superblock version")
1004     } /* end if */
1005 
1006     if(H5FD_set_paged_aggr(f->shared->lf, (hbool_t)H5F_PAGED_AGGR(f)) < 0)
1007         HGOTO_ERROR(H5E_FILE, H5E_CANTINIT, FAIL, "failed to set paged_aggr status for file driver")
1008 
1009     /*
1010      * The superblock starts immediately after the user-defined
1011      * header, which we have already insured is a proper size. The
1012      * base address is set to the same thing as the superblock for
1013      * now.
1014      */
1015     if(H5P_get(plist, H5F_CRT_USER_BLOCK_NAME, &userblock_size) < 0)
1016         HGOTO_ERROR(H5E_FILE, H5E_CANTGET, FAIL, "unable to get userblock size")
1017 
1018     /* Sanity check the userblock size vs. the file's allocation alignment */
1019     if(userblock_size > 0) {
1020 	/* Set up the alignment to use for page or aggr fs */
1021 	hsize_t alignment = H5F_PAGED_AGGR(f) ? f->shared->fs_page_size : f->shared->alignment;
1022 
1023         if(userblock_size < alignment)
1024             HGOTO_ERROR(H5E_FILE, H5E_BADVALUE, FAIL, "userblock size must be > file object alignment")
1025         if(0 != (userblock_size % alignment))
1026             HGOTO_ERROR(H5E_FILE, H5E_BADVALUE, FAIL, "userblock size must be an integral multiple of file object alignment")
1027     } /* end if */
1028 
1029     sblock->base_addr = userblock_size;
1030     sblock->status_flags = 0;
1031 
1032     /* Reserve space for the userblock */
1033     if(H5F__set_eoa(f, H5FD_MEM_SUPER, userblock_size) < 0)
1034         HGOTO_ERROR(H5E_FILE, H5E_CANTINIT, FAIL, "unable to set EOA value for userblock")
1035 
1036     /* Set the base address for the file in the VFD now, after allocating
1037      *  space for userblock.
1038      */
1039     if(H5F__set_base_addr(f, sblock->base_addr) < 0)
1040         HGOTO_ERROR(H5E_FILE, H5E_CANTINIT, FAIL, "failed to set base address for file driver")
1041 
1042     /* Save a local copy of the superblock version number, size of addresses & offsets */
1043     sblock->super_vers = super_vers;
1044     sblock->sizeof_addr = f->shared->sizeof_addr;
1045     sblock->sizeof_size = f->shared->sizeof_size;
1046 
1047     /* Compute the size of the superblock */
1048     superblock_size = (hsize_t)H5F_SUPERBLOCK_SIZE(sblock);
1049 
1050     /* Compute the size of the driver information block */
1051     H5_CHECKED_ASSIGN(driver_size, size_t, H5FD_sb_size(f->shared->lf), hsize_t);
1052 
1053     /* The following code sets driver_size to the valued needed
1054      * for the driver info block, and sets the driver info block
1055      * address regardless of the version of the superblock.
1056      */
1057     if(driver_size > 0) {
1058         /* Add in the driver info header, for older superblocks */
1059         /* Superblock versions >= 2 will put the driver info in a message
1060          *      and don't need the header -QAK, 1/4/2017
1061          */
1062         if(super_vers < HDF5_SUPERBLOCK_VERSION_2)
1063             driver_size += H5F_DRVINFOBLOCK_HDR_SIZE;
1064 
1065         /*
1066          * The file driver information block begins immediately after the
1067          * superblock. (relative to base address in file)
1068          */
1069         sblock->driver_addr = superblock_size;
1070     } /* end if */
1071 
1072     /*
1073      * Allocate space for the superblock & driver info block.
1074      * We do it with one allocation request because the superblock needs to be
1075      * at the beginning of the file and only the first allocation request is
1076      * required to return memory at format address zero.
1077      */
1078     if(super_vers < HDF5_SUPERBLOCK_VERSION_2)
1079         superblock_size += driver_size;
1080 
1081     /* Set the ring type in the DXPL */
1082     if(H5AC_set_ring(dxpl_id, H5AC_RING_SB, &dxpl, &orig_ring) < 0)
1083         HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set ring value")
1084 
1085     /* Insert superblock into cache, pinned */
1086     if(H5AC_insert_entry(f, dxpl_id, H5AC_SUPERBLOCK, (haddr_t)0, sblock, H5AC__PIN_ENTRY_FLAG | H5AC__FLUSH_LAST_FLAG | H5AC__FLUSH_COLLECTIVELY_FLAG) < 0)
1087         HGOTO_ERROR(H5E_CACHE, H5E_CANTINS, FAIL, "can't add superblock to cache")
1088     sblock_in_cache = TRUE;
1089 
1090     /* Keep a copy of the superblock info */
1091     f->shared->sblock = sblock;
1092 
1093     /* Allocate space for the superblock */
1094     if(HADDR_UNDEF == H5MF_alloc(f, H5FD_MEM_SUPER, dxpl_id, superblock_size))
1095 	HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "file allocation failed for superblock")
1096 
1097     /* set the drvinfo filed to NULL -- will overwrite this later if needed */
1098     f->shared->drvinfo = NULL;
1099 
1100     /*
1101      * Determine if we will need a superblock extension
1102      */
1103 
1104     /* Files with SOHM indices always need the superblock extension */
1105     if(f->shared->sohm_nindexes > 0) {
1106         HDassert(super_vers >= HDF5_SUPERBLOCK_VERSION_2);
1107         need_ext = TRUE;
1108     } /* end if */
1109     /* Files with non-default free space settings always need the superblock extension */
1110     else if(non_default_fs_settings) {
1111         HDassert(super_vers >= HDF5_SUPERBLOCK_VERSION_2);
1112         need_ext = TRUE;
1113     } /* end if */
1114     /* If we're going to use a version of the superblock format which allows
1115      *  for the superblock extension, check for non-default values to store
1116      *  in it.
1117      */
1118     else if(super_vers >= HDF5_SUPERBLOCK_VERSION_2) {
1119         /* Check for non-default v1 B-tree 'K' values to store */
1120         if(sblock->btree_k[H5B_SNODE_ID] != HDF5_BTREE_SNODE_IK_DEF ||
1121                 sblock->btree_k[H5B_CHUNK_ID] != HDF5_BTREE_CHUNK_IK_DEF ||
1122                 sblock->sym_leaf_k != H5F_CRT_SYM_LEAF_DEF)
1123             need_ext = TRUE;
1124         /* Check for driver info to store */
1125         else if(driver_size > 0)
1126             need_ext = TRUE;
1127         else
1128             need_ext = FALSE;
1129     } /* end if */
1130     else
1131         need_ext = FALSE;
1132 
1133     /* Set the ring type in the DXPL */
1134     ring = H5AC_RING_SBE;
1135     if((H5P_set(dxpl, H5AC_RING_NAME, &ring)) < 0)
1136         HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, FAIL, "unable to set property value")
1137 
1138     /* Create the superblock extension for "extra" superblock data, if necessary. */
1139     if(need_ext) {
1140         /* The superblock extension isn't actually a group, but the
1141          * default group creation list should work fine.
1142          * If we don't supply a size for the object header, HDF5 will
1143          * allocate H5O_MIN_SIZE by default.  This is currently
1144          * big enough to hold the biggest possible extension, but should
1145          * be tuned if more information is added to the superblock
1146          * extension.
1147          */
1148 	if(H5F_super_ext_create(f, dxpl_id, &ext_loc) < 0)
1149             HGOTO_ERROR(H5E_FILE, H5E_CANTCREATE, FAIL, "unable to create superblock extension")
1150         ext_created = TRUE;
1151 
1152         /* Create the Shared Object Header Message table and register it with
1153          *      the metadata cache, if this file supports shared messages.
1154          */
1155         if(f->shared->sohm_nindexes > 0) {
1156             /* Initialize the shared message code & write the SOHM message to the extension */
1157             if(H5SM_init(f, plist, &ext_loc, dxpl_id) < 0)
1158                 HGOTO_ERROR(H5E_FILE, H5E_CANTINIT, FAIL, "unable to create SOHM table")
1159         } /* end if */
1160 
1161         /* Check for non-default v1 B-tree 'K' values to store */
1162         if(sblock->btree_k[H5B_SNODE_ID] != HDF5_BTREE_SNODE_IK_DEF ||
1163                 sblock->btree_k[H5B_CHUNK_ID] != HDF5_BTREE_CHUNK_IK_DEF ||
1164                 sblock->sym_leaf_k != H5F_CRT_SYM_LEAF_DEF) {
1165             H5O_btreek_t btreek;        /* v1 B-tree 'K' value message for superblock extension */
1166 
1167             /* Write v1 B-tree 'K' value information to the superblock extension */
1168             btreek.btree_k[H5B_CHUNK_ID] = sblock->btree_k[H5B_CHUNK_ID];
1169             btreek.btree_k[H5B_SNODE_ID] = sblock->btree_k[H5B_SNODE_ID];
1170             btreek.sym_leaf_k = sblock->sym_leaf_k;
1171             if(H5O_msg_create(&ext_loc, H5O_BTREEK_ID, H5O_MSG_FLAG_CONSTANT | H5O_MSG_FLAG_DONTSHARE, H5O_UPDATE_TIME, &btreek, dxpl_id) < 0)
1172                 HGOTO_ERROR(H5E_FILE, H5E_CANTINIT, FAIL, "unable to update v1 B-tree 'K' value header message")
1173         } /* end if */
1174 
1175         /* Check for driver info to store */
1176         if(driver_size > 0) {
1177             H5O_drvinfo_t info;      /* Driver info */
1178             uint8_t dbuf[H5F_MAX_DRVINFOBLOCK_SIZE];  /* Driver info block encoding buffer */
1179 
1180             /* Sanity check */
1181             HDassert(driver_size <= H5F_MAX_DRVINFOBLOCK_SIZE);
1182 
1183             /* Encode driver-specific data */
1184             HDmemset(dbuf, 0, sizeof(dbuf));
1185             if(H5FD_sb_encode(f->shared->lf, info.name, dbuf) < 0)
1186                 HGOTO_ERROR(H5E_FILE, H5E_CANTINIT, FAIL, "unable to encode driver information")
1187 
1188             /* Write driver info information to the superblock extension */
1189             info.len = driver_size;
1190             info.buf = dbuf;
1191             if(H5O_msg_create(&ext_loc, H5O_DRVINFO_ID, H5O_MSG_FLAG_DONTSHARE, H5O_UPDATE_TIME, &info, dxpl_id) < 0)
1192                 HGOTO_ERROR(H5E_FILE, H5E_CANTINIT, FAIL, "unable to update driver info header message")
1193 
1194             HDassert(FALSE == f->shared->drvinfo_sb_msg_exists);
1195             f->shared->drvinfo_sb_msg_exists = TRUE;
1196         } /* end if */
1197 
1198         /* Check for non-default free-space info settings */
1199         if(non_default_fs_settings) {
1200             H5F_mem_page_t ptype;
1201             H5O_fsinfo_t fsinfo;    /* Free space manager info message */
1202 
1203             /* Write free-space manager info message to superblock extension object header if needed */
1204             fsinfo.strategy = f->shared->fs_strategy;
1205             fsinfo.persist = f->shared->fs_persist;
1206             fsinfo.threshold = f->shared->fs_threshold;
1207             fsinfo.page_size = f->shared->fs_page_size;
1208             fsinfo.pgend_meta_thres = f->shared->pgend_meta_thres;
1209             fsinfo.eoa_pre_fsm_fsalloc = HADDR_UNDEF;
1210             fsinfo.mapped = FALSE;
1211 
1212             for(ptype = H5F_MEM_PAGE_SUPER; ptype < H5F_MEM_PAGE_NTYPES; H5_INC_ENUM(H5F_mem_page_t, ptype))
1213                 fsinfo.fs_addr[ptype - 1] = HADDR_UNDEF;
1214 
1215             if(H5O_msg_create(&ext_loc, H5O_FSINFO_ID, H5O_MSG_FLAG_DONTSHARE | H5O_MSG_FLAG_MARK_IF_UNKNOWN, H5O_UPDATE_TIME, &fsinfo, dxpl_id) < 0)
1216                 HGOTO_ERROR(H5E_FILE, H5E_CANTINIT, FAIL, "unable to update free-space info header message")
1217 	} /* end if */
1218     } /* end if */
1219     else {
1220         /* Check for creating an "old-style" driver info block */
1221         if(driver_size > 0) {
1222             /* Sanity check */
1223             HDassert(H5F_addr_defined(sblock->driver_addr));
1224 
1225             /* Allocate space for the driver info */
1226             if(NULL == (drvinfo = (H5O_drvinfo_t *)H5MM_calloc(sizeof(H5O_drvinfo_t))))
1227                 HGOTO_ERROR(H5E_FILE, H5E_CANTALLOC, FAIL, "memory allocation failed for driver info message")
1228 
1229             /* Set up driver info message */
1230             /* (NOTE: All the actual information (name & driver information) is
1231              *          actually based on the VFD info in the file handle and
1232              *          will be encoded by the VFD's 'encode' callback, so it
1233              *          doesn't need to be set here. -QAK, 7/20/2013
1234              */
1235             H5_CHECKED_ASSIGN(drvinfo->len, size_t, H5FD_sb_size(f->shared->lf), hsize_t);
1236 
1237             /* Insert driver info block into cache */
1238             if(H5AC_insert_entry(f, dxpl_id, H5AC_DRVRINFO, sblock->driver_addr, drvinfo, H5AC__PIN_ENTRY_FLAG | H5AC__FLUSH_LAST_FLAG | H5AC__FLUSH_COLLECTIVELY_FLAG) < 0)
1239                 HGOTO_ERROR(H5E_FILE, H5E_CANTINS, FAIL, "can't add driver info block to cache")
1240             drvinfo_in_cache = TRUE;
1241             f->shared->drvinfo = drvinfo;
1242         } /* end if */
1243         else
1244             HDassert(!H5F_addr_defined(sblock->driver_addr));
1245     } /* end if */
1246 
1247 done:
1248     /* Reset the ring in the DXPL */
1249     if(H5AC_reset_ring(dxpl, orig_ring) < 0)
1250         HDONE_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set property value")
1251 
1252     /* Close superblock extension, if it was created */
1253     if(ext_created && H5F_super_ext_close(f, &ext_loc, dxpl_id, ext_created) < 0)
1254         HDONE_ERROR(H5E_FILE, H5E_CANTRELEASE, FAIL, "unable to close file's superblock extension")
1255 
1256     /* Cleanup on failure */
1257     if(ret_value < 0) {
1258         /* Check if the driver info block has been allocated yet */
1259         if(drvinfo) {
1260             /* Check if we've cached it already */
1261             if(drvinfo_in_cache) {
1262                 /* Unpin drvinfo in cache */
1263                 if(H5AC_unpin_entry(drvinfo) < 0)
1264                     HDONE_ERROR(H5E_FILE, H5E_CANTUNPIN, FAIL, "unable to unpin driver info")
1265 
1266                 /* Evict the driver info block from the cache */
1267                 if(H5AC_expunge_entry(f, dxpl_id, H5AC_DRVRINFO, sblock->driver_addr, H5AC__NO_FLAGS_SET) < 0)
1268                     HDONE_ERROR(H5E_FILE, H5E_CANTEXPUNGE, FAIL, "unable to expunge driver info block")
1269             } /* end if */
1270             else
1271                 /* Free driver info block */
1272                 H5MM_xfree(drvinfo);
1273         } /* end if */
1274 
1275         /* Check if the superblock has been allocated yet */
1276         if(sblock) {
1277             /* Check if we've cached it already */
1278             if(sblock_in_cache) {
1279                 /* Unpin superblock in cache */
1280                 if(H5AC_unpin_entry(sblock) < 0)
1281                     HDONE_ERROR(H5E_FILE, H5E_CANTUNPIN, FAIL, "unable to unpin superblock")
1282 
1283                 /* Evict the superblock from the cache */
1284                 if(H5AC_expunge_entry(f, dxpl_id, H5AC_SUPERBLOCK, (haddr_t)0, H5AC__NO_FLAGS_SET) < 0)
1285                     HDONE_ERROR(H5E_FILE, H5E_CANTEXPUNGE, FAIL, "unable to expunge superblock")
1286             } /* end if */
1287             else
1288                 /* Free superblock */
1289                 if(H5F__super_free(sblock) < 0)
1290                     HDONE_ERROR(H5E_FILE, H5E_CANTFREE, FAIL, "unable to destroy superblock")
1291 
1292             /* Reset variables in file structure */
1293             f->shared->sblock = NULL;
1294         } /* end if */
1295     } /* end if */
1296 
1297     FUNC_LEAVE_NOAPI_TAG(ret_value, FAIL)
1298 } /* end H5F__super_init() */
1299 
1300 
1301 /*-------------------------------------------------------------------------
1302  * Function:    H5F_eoa_dirty
1303  *
1304  * Purpose:     Mark the file's EOA info dirty
1305  *
1306  * Return:      Success:        non-negative on success
1307  *              Failure:        Negative
1308  *
1309  * Programmer:  Quincey Koziol
1310  *              January 4, 2017
1311  *
1312  *-------------------------------------------------------------------------
1313  */
1314 herr_t
H5F_eoa_dirty(H5F_t * f,hid_t dxpl_id)1315 H5F_eoa_dirty(H5F_t *f, hid_t dxpl_id)
1316 {
1317     herr_t ret_value = SUCCEED;         /* Return value */
1318 
1319     FUNC_ENTER_NOAPI(FAIL)
1320 
1321     /* Sanity check */
1322     HDassert(f);
1323     HDassert(f->shared);
1324     HDassert(f->shared->sblock);
1325 
1326     /* Mark superblock dirty in cache, so change to EOA will get encoded */
1327     if(H5F_super_dirty(f) < 0)
1328         HGOTO_ERROR(H5E_FILE, H5E_CANTMARKDIRTY, FAIL, "unable to mark superblock as dirty")
1329 
1330     /* If the driver information block exists, mark it dirty as well
1331      * so that the change in eoa will be reflected there as well if
1332      * appropriate.
1333      */
1334     if(f->shared->drvinfo) {
1335         if(H5AC_mark_entry_dirty(f->shared->drvinfo) < 0)
1336             HGOTO_ERROR(H5E_FILE, H5E_CANTMARKDIRTY, FAIL, "unable to mark drvinfo as dirty")
1337     } /* end if */
1338     /* If the driver info is stored as a message, update that instead */
1339     else if(f->shared->drvinfo_sb_msg_exists) {
1340         if(H5F__update_super_ext_driver_msg(f, dxpl_id) < 0)
1341             HGOTO_ERROR(H5E_FILE, H5E_CANTMARKDIRTY, FAIL, "unable to mark drvinfo message as dirty")
1342     } /* end if */
1343 
1344 done:
1345     FUNC_LEAVE_NOAPI(ret_value)
1346 } /* H5F_eoa_dirty() */
1347 
1348 
1349 /*-------------------------------------------------------------------------
1350  * Function:    H5F_super_dirty
1351  *
1352  * Purpose:     Mark the file's superblock dirty
1353  *
1354  * Return:      Success:        non-negative on success
1355  *              Failure:        Negative
1356  *
1357  * Programmer:  Quincey Koziol
1358  *              August 14, 2009
1359  *
1360  *-------------------------------------------------------------------------
1361  */
1362 herr_t
H5F_super_dirty(H5F_t * f)1363 H5F_super_dirty(H5F_t *f)
1364 {
1365     herr_t ret_value = SUCCEED;         /* Return value */
1366 
1367     FUNC_ENTER_NOAPI(FAIL)
1368 
1369     /* Sanity check */
1370     HDassert(f);
1371     HDassert(f->shared);
1372     HDassert(f->shared->sblock);
1373 
1374     /* Mark superblock dirty in cache, so change to EOA will get encoded */
1375     if(H5AC_mark_entry_dirty(f->shared->sblock) < 0)
1376         HGOTO_ERROR(H5E_FILE, H5E_CANTMARKDIRTY, FAIL, "unable to mark superblock as dirty")
1377 
1378 done:
1379     FUNC_LEAVE_NOAPI(ret_value)
1380 } /* H5F_super_dirty() */
1381 
1382 
1383 /*-------------------------------------------------------------------------
1384  * Function:    H5F__super_free
1385  *
1386  * Purpose:     Destroyer the file's superblock
1387  *
1388  * Return:      Success:        non-negative on success
1389  *              Failure:        Negative
1390  *
1391  * Programmer:  Quincey Koziol
1392  *              April 1, 2010
1393  *
1394  *-------------------------------------------------------------------------
1395  */
1396 herr_t
H5F__super_free(H5F_super_t * sblock)1397 H5F__super_free(H5F_super_t *sblock)
1398 {
1399     FUNC_ENTER_PACKAGE_NOERR
1400 
1401     /* Sanity check */
1402     HDassert(sblock);
1403 
1404     /* Free root group symbol table entry, if any */
1405     sblock->root_ent = (H5G_entry_t *)H5MM_xfree(sblock->root_ent);
1406 
1407     /* Free superblock */
1408     sblock = (H5F_super_t *)H5FL_FREE(H5F_super_t, sblock);
1409 
1410     FUNC_LEAVE_NOAPI(SUCCEED)
1411 } /* H5F__super_free() */
1412 
1413 
1414 /*-------------------------------------------------------------------------
1415  * Function:    H5F__super_size
1416  *
1417  * Purpose:     Get storage size of the superblock and superblock extension
1418  *
1419  * Return:      Success:        non-negative on success
1420  *              Failure:        Negative
1421  *
1422  * Programmer:  Vailin Choi
1423  *              July 11, 2007
1424  *
1425  *-------------------------------------------------------------------------
1426  */
1427 herr_t
H5F__super_size(H5F_t * f,hid_t dxpl_id,hsize_t * super_size,hsize_t * super_ext_size)1428 H5F__super_size(H5F_t *f, hid_t dxpl_id, hsize_t *super_size, hsize_t *super_ext_size)
1429 {
1430     H5P_genplist_t *dxpl = NULL;        /* DXPL for setting ring */
1431     H5AC_ring_t orig_ring = H5AC_RING_INV;      /* Original ring value */
1432     herr_t ret_value = SUCCEED;         /* Return value */
1433 
1434     FUNC_ENTER_PACKAGE
1435 
1436     /* Sanity check */
1437     HDassert(f);
1438     HDassert(f->shared);
1439     HDassert(f->shared->sblock);
1440 
1441     /* Set the superblock size */
1442     if(super_size)
1443 	*super_size = (hsize_t)H5F_SUPERBLOCK_SIZE(f->shared->sblock);
1444 
1445     /* Set the superblock extension size */
1446     if(super_ext_size) {
1447         if(H5F_addr_defined(f->shared->sblock->ext_addr)) {
1448             H5O_loc_t ext_loc;          /* "Object location" for superblock extension */
1449             H5O_hdr_info_t hdr_info;    /* Object info for superblock extension */
1450 
1451             /* Set up "fake" object location for superblock extension */
1452             H5O_loc_reset(&ext_loc);
1453             ext_loc.file = f;
1454             ext_loc.addr = f->shared->sblock->ext_addr;
1455 
1456             /* Set the ring type in the DXPL */
1457             if(H5AC_set_ring(dxpl_id, H5AC_RING_SBE, &dxpl, &orig_ring) < 0)
1458                 HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set ring value")
1459 
1460             /* Get object header info for superblock extension */
1461             if(H5O_get_hdr_info(&ext_loc, dxpl_id, &hdr_info) < 0)
1462                 HGOTO_ERROR(H5E_FILE, H5E_CANTGET, FAIL, "unable to retrieve superblock extension info")
1463 
1464             /* Set the superblock extension size */
1465             *super_ext_size = hdr_info.space.total;
1466         } /* end if */
1467         else
1468             /* Set the superblock extension size to zero */
1469             *super_ext_size = (hsize_t)0;
1470     } /* end if */
1471 
1472 done:
1473     /* Reset the ring in the DXPL */
1474     if(H5AC_reset_ring(dxpl, orig_ring) < 0)
1475         HDONE_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set property value")
1476 
1477     FUNC_LEAVE_NOAPI(ret_value)
1478 } /* H5F__super_size() */
1479 
1480 
1481 /*-------------------------------------------------------------------------
1482  * Function:    H5F_super_ext_write_msg()
1483  *
1484  * Purpose:     Write the message with ID to the superblock extension
1485  *
1486  * Return:      Non-negative on success/Negative on failure
1487  *
1488  * Programmer:  Vailin Choi; Feb 2009
1489  *
1490  *-------------------------------------------------------------------------
1491  */
1492 herr_t
H5F_super_ext_write_msg(H5F_t * f,hid_t dxpl_id,unsigned id,void * mesg,hbool_t may_create,unsigned mesg_flags)1493 H5F_super_ext_write_msg(H5F_t *f, hid_t dxpl_id, unsigned id, void *mesg,
1494     hbool_t may_create, unsigned mesg_flags)
1495 {
1496     H5P_genplist_t *dxpl = NULL;        /* DXPL for setting ring */
1497     H5AC_ring_t orig_ring = H5AC_RING_INV;      /* Original ring value */
1498     hbool_t     ext_created = FALSE;   /* Whether superblock extension was created */
1499     hbool_t     ext_opened = FALSE;    /* Whether superblock extension was opened */
1500     H5O_loc_t 	ext_loc; 	/* "Object location" for superblock extension */
1501     htri_t 	status;       	/* Indicate whether the message exists or not */
1502     herr_t 	ret_value = SUCCEED;         /* Return value */
1503 
1504     FUNC_ENTER_NOAPI(FAIL)
1505 
1506     /* Sanity checks */
1507     HDassert(f);
1508     HDassert(f->shared);
1509     HDassert(f->shared->sblock);
1510 
1511     /* Set the ring type in the DXPL */
1512     if(H5AC_set_ring(dxpl_id, H5AC_RING_SBE, &dxpl, &orig_ring) < 0)
1513         HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set ring value")
1514 
1515     /* Open/create the superblock extension object header */
1516     if(H5F_addr_defined(f->shared->sblock->ext_addr)) {
1517 	if(H5F_super_ext_open(f, f->shared->sblock->ext_addr, &ext_loc) < 0)
1518 	    HGOTO_ERROR(H5E_FILE, H5E_CANTOPENOBJ, FAIL, "unable to open file's superblock extension")
1519     } /* end if */
1520     else {
1521         HDassert(may_create);
1522 	if(H5F_super_ext_create(f, dxpl_id, &ext_loc) < 0)
1523 	    HGOTO_ERROR(H5E_FILE, H5E_CANTCREATE, FAIL, "unable to create file's superblock extension")
1524         ext_created = TRUE;
1525     } /* end else */
1526     HDassert(H5F_addr_defined(ext_loc.addr));
1527     ext_opened = TRUE;
1528 
1529     /* Check if message with ID does not exist in the object header */
1530     if((status = H5O_msg_exists(&ext_loc, id, dxpl_id)) < 0)
1531 	HGOTO_ERROR(H5E_OHDR, H5E_CANTGET, FAIL, "unable to check object header for message or message exists")
1532 
1533     /* Check for creating vs. writing */
1534     if(may_create) {
1535 	if(status)
1536 	    HGOTO_ERROR(H5E_OHDR, H5E_CANTGET, FAIL, "Message should not exist")
1537 
1538 	/* Create the message with ID in the superblock extension */
1539 	if(H5O_msg_create(&ext_loc, id, (mesg_flags | H5O_MSG_FLAG_DONTSHARE), H5O_UPDATE_TIME, mesg, dxpl_id) < 0)
1540 	    HGOTO_ERROR(H5E_OHDR, H5E_CANTGET, FAIL, "unable to create the message in object header")
1541     } /* end if */
1542     else {
1543 	if(!status)
1544 	    HGOTO_ERROR(H5E_OHDR, H5E_CANTGET, FAIL, "Message should exist")
1545 
1546 	/* Update the message with ID in the superblock extension */
1547 	if(H5O_msg_write(&ext_loc, id, (mesg_flags | H5O_MSG_FLAG_DONTSHARE), H5O_UPDATE_TIME, mesg, dxpl_id) < 0)
1548 	    HGOTO_ERROR(H5E_OHDR, H5E_CANTGET, FAIL, "unable to write the message in object header")
1549     } /* end else */
1550 
1551 done:
1552     /* Reset the ring in the DXPL */
1553     if(H5AC_reset_ring(dxpl, orig_ring) < 0)
1554         HDONE_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set property value")
1555 
1556     /* Close the superblock extension, if it was opened */
1557     if(ext_opened && H5F_super_ext_close(f, &ext_loc, dxpl_id, ext_created) < 0)
1558         HDONE_ERROR(H5E_FILE, H5E_CANTRELEASE, FAIL, "unable to close file's superblock extension")
1559 
1560     /* Mark superblock dirty in cache, if superblock extension was created */
1561     if(ext_created && H5AC_mark_entry_dirty(f->shared->sblock) < 0)
1562         HDONE_ERROR(H5E_FILE, H5E_CANTMARKDIRTY, FAIL, "unable to mark superblock as dirty")
1563 
1564     FUNC_LEAVE_NOAPI(ret_value)
1565 } /* H5F_super_ext_write_msg() */
1566 
1567 
1568 /*-------------------------------------------------------------------------
1569  * Function:    H5F_super_ext_remove_msg
1570  *
1571  * Purpose:     Remove the message with ID from the superblock extension
1572  *
1573  * Return:      Non-negative on success/Negative on failure
1574  *
1575  * Programmer:  Vailin Choi; Feb 2009
1576  *
1577  *-------------------------------------------------------------------------
1578  */
1579 herr_t
H5F_super_ext_remove_msg(H5F_t * f,hid_t dxpl_id,unsigned id)1580 H5F_super_ext_remove_msg(H5F_t *f, hid_t dxpl_id, unsigned id)
1581 {
1582     H5P_genplist_t *dxpl = NULL;        /* DXPL for setting ring */
1583     H5AC_ring_t orig_ring = H5AC_RING_INV;      /* Original ring value */
1584     H5O_loc_t 	ext_loc; 	/* "Object location" for superblock extension */
1585     hbool_t     ext_opened = FALSE;     /* Whether the superblock extension was opened */
1586     int		null_count = 0;	/* # of null messages */
1587     htri_t      status;         /* Indicate whether the message exists or not */
1588     herr_t 	ret_value = SUCCEED;         /* Return value */
1589 
1590     FUNC_ENTER_NOAPI(FAIL)
1591 
1592     /* Make sure that the superblock extension object header exists */
1593     HDassert(H5F_addr_defined(f->shared->sblock->ext_addr));
1594 
1595     /* Set the ring type in the DXPL */
1596     if(H5AC_set_ring(dxpl_id, H5AC_RING_SBE, &dxpl, &orig_ring) < 0)
1597         HGOTO_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set ring value")
1598 
1599     /* Open superblock extension object header */
1600     if(H5F_super_ext_open(f, f->shared->sblock->ext_addr, &ext_loc) < 0)
1601 	HGOTO_ERROR(H5E_FILE, H5E_CANTRELEASE, FAIL, "error in starting file's superblock extension")
1602     ext_opened = TRUE;
1603 
1604     /* Check if message with ID exists in the object header */
1605     if((status = H5O_msg_exists(&ext_loc, id, dxpl_id)) < 0)
1606 	HGOTO_ERROR(H5E_OHDR, H5E_CANTGET, FAIL, "unable to check object header for message")
1607     else if(status) { /* message exists */
1608 	H5O_hdr_info_t 	hdr_info;       /* Object header info for superblock extension */
1609 
1610 	/* Remove the message */
1611 	if(H5O_msg_remove(&ext_loc, id, H5O_ALL, TRUE, dxpl_id) < 0)
1612 	    HGOTO_ERROR(H5E_OHDR, H5E_CANTDELETE, FAIL, "unable to delete free-space manager info message")
1613 
1614 	/* Get info for the superblock extension's object header */
1615 	if(H5O_get_hdr_info(&ext_loc, dxpl_id, &hdr_info) < 0)
1616 	    HGOTO_ERROR(H5E_OHDR, H5E_CANTGET, FAIL, "unable to retrieve superblock extension info")
1617 
1618 	/* If the object header is an empty base chunk, remove superblock extension */
1619 	if(hdr_info.nchunks == 1) {
1620 	    if((null_count = H5O_msg_count(&ext_loc, H5O_NULL_ID, dxpl_id)) < 0)
1621 		HGOTO_ERROR(H5E_SYM, H5E_CANTCOUNT, FAIL, "unable to count messages")
1622 	    else if((unsigned)null_count == hdr_info.nmesgs) {
1623 		HDassert(H5F_addr_defined(ext_loc.addr));
1624 		if(H5O_delete(f, dxpl_id, ext_loc.addr) < 0)
1625 		    HGOTO_ERROR(H5E_SYM, H5E_CANTCOUNT, FAIL, "unable to count messages")
1626 		f->shared->sblock->ext_addr = HADDR_UNDEF;
1627 	    } /* end if */
1628 	} /* end if */
1629     } /* end if */
1630 
1631 done:
1632     /* Reset the ring in the DXPL */
1633     if(H5AC_reset_ring(dxpl, orig_ring) < 0)
1634         HDONE_ERROR(H5E_FILE, H5E_CANTSET, FAIL, "unable to set property value")
1635 
1636     /* Close superblock extension object header, if opened */
1637     if(ext_opened && H5F_super_ext_close(f, &ext_loc, dxpl_id, FALSE) < 0)
1638        HDONE_ERROR(H5E_FILE, H5E_CANTRELEASE, FAIL, "unable to close file's superblock extension")
1639 
1640     FUNC_LEAVE_NOAPI(ret_value)
1641 } /* H5F_super_ext_remove_msg() */
1642 
1643