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 files COPYING and Copyright.html.  COPYING can be found at the root   *
9  * of the source code distribution tree; Copyright.html can be found at the  *
10  * root level of an installed copy of the electronic HDF5 document set and   *
11  * is linked from the top-level documents page.  It can also be found at     *
12  * http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have          *
13  * access to either file, you may request a copy from help@hdfgroup.org.     *
14  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /****************/
17 /* Module Setup */
18 /****************/
19 
20 #define H5F_PACKAGE		/*suppress error about including H5Fpkg	  */
21 #define H5G_PACKAGE		/*suppress error about including H5Gpkg	  */
22 
23 
24 /***********/
25 /* Headers */
26 /***********/
27 #include "H5private.h"		/* Generic Functions			*/
28 #include "H5Eprivate.h"		/* Error handling		  	*/
29 #include "H5Fpkg.h"             /* File access				*/
30 #include "H5FDprivate.h"	/* File drivers				*/
31 #include "H5FLprivate.h"        /* Free Lists                           */
32 #include "H5Gpkg.h"		/* Groups		  		*/
33 #include "H5Iprivate.h"		/* IDs			  		*/
34 #include "H5MMprivate.h"        /* Memory management                    */
35 #include "H5Pprivate.h"		/* Property lists			*/
36 #include "H5SMprivate.h"        /* Shared Object Header Messages        */
37 
38 
39 /****************/
40 /* Local Macros */
41 /****************/
42 
43 /* Maximum size of super-block buffers */
44 #define H5F_MAX_SUPERBLOCK_SIZE  134
45 
46 
47 /******************/
48 /* Local Typedefs */
49 /******************/
50 
51 
52 /********************/
53 /* Package Typedefs */
54 /********************/
55 
56 
57 /********************/
58 /* Local Prototypes */
59 /********************/
60 
61 /* Metadata cache (H5AC) callbacks */
62 static H5F_super_t *H5F_sblock_load(H5F_t *f, hid_t dxpl_id, haddr_t addr, void *udata);
63 static herr_t H5F_sblock_flush(H5F_t *f, hid_t dxpl_id, hbool_t destroy, haddr_t addr, H5F_super_t *sblock);
64 static herr_t H5F_sblock_dest(H5F_t *f, H5F_super_t * sblock);
65 static herr_t H5F_sblock_clear(H5F_t *f, H5F_super_t *sblock, hbool_t destroy);
66 static herr_t H5F_sblock_size(const H5F_t *f, const H5F_super_t *sblock, size_t *size_ptr);
67 
68 
69 /*********************/
70 /* Package Variables */
71 /*********************/
72 
73 /* H5F inherits cache-like properties from H5AC */
74 const H5AC_class_t H5AC_SUPERBLOCK[1] = {{
75     H5AC_SUPERBLOCK_ID,
76     (H5AC_load_func_t)H5F_sblock_load,
77     (H5AC_flush_func_t)H5F_sblock_flush,
78     (H5AC_dest_func_t)H5F_sblock_dest,
79     (H5AC_clear_func_t)H5F_sblock_clear,
80     (H5AC_size_func_t)H5F_sblock_size,
81 }};
82 
83 /*****************************/
84 /* Library Private Variables */
85 /*****************************/
86 
87 /* Declare extern the free list to manage the H5F_super_t struct */
88 H5FL_EXTERN(H5F_super_t);
89 
90 
91 /*******************/
92 /* Local Variables */
93 /*******************/
94 
95 
96 
97 /*-------------------------------------------------------------------------
98  * Function:    H5F_sblock_load
99  *
100  * Purpose:     Loads the superblock from the file, and deserializes
101  *              its information into the H5F_super_t structure.
102  *
103  * Return:      Success:        SUCCEED
104  *              Failure:        NULL
105  *
106  * Programmer:  Mike McGreevy
107  *              mamcgree@hdfgroup.org
108  *              April 8, 2009
109  *
110  *-------------------------------------------------------------------------
111  */
112 static H5F_super_t *
H5F_sblock_load(H5F_t * f,hid_t dxpl_id,haddr_t UNUSED addr,void * _udata)113 H5F_sblock_load(H5F_t *f, hid_t dxpl_id, haddr_t UNUSED addr, void *_udata)
114 {
115     H5F_super_t        *sblock = NULL;      /* File's superblock */
116     haddr_t             base_addr = HADDR_UNDEF;        /* Base address of file */
117     uint8_t             sbuf[H5F_MAX_SUPERBLOCK_SIZE];     /* Buffer for superblock */
118     H5P_genplist_t     *c_plist;            /* File creation property list  */
119     H5F_file_t         *shared;             /* shared part of `file'        */
120     H5FD_t             *lf;                 /* file driver part of `shared' */
121     haddr_t             stored_eoa;         /*relative end-of-addr in file  */
122     haddr_t             eof;                /*end of file address           */
123     uint8_t             sizeof_addr;        /* Size of offsets in the file (in bytes) */
124     uint8_t             sizeof_size;        /* Size of lengths in the file (in bytes) */
125     const size_t        fixed_size = H5F_SUPERBLOCK_FIXED_SIZE; /*fixed sizeof superblock   */
126     size_t              variable_size;      /*variable sizeof superblock    */
127     uint8_t            *p;                  /* Temporary pointer into encoding buffer */
128     unsigned            super_vers;         /* Superblock version          */
129     hbool_t            *dirtied = (hbool_t *)_udata;  /* Set up dirtied out value */
130     H5F_super_t        *ret_value;          /* Return value */
131 
132     FUNC_ENTER_NOAPI_NOINIT
133 
134     /* check arguments */
135     HDassert(f);
136     HDassert(H5F_addr_eq(addr, 0));
137     HDassert(dirtied);
138 
139     /* Short cuts */
140     shared = f->shared;
141     lf = shared->lf;
142 
143     /* Get the shared file creation property list */
144     if(NULL == (c_plist = (H5P_genplist_t *)H5I_object(shared->fcpl_id)))
145         HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "can't get property list")
146 
147     /* Get the base address for the file in the VFD */
148     if(HADDR_UNDEF == (base_addr = H5FD_get_base_addr(lf)))
149         HGOTO_ERROR(H5E_FILE, H5E_CANTGET, NULL, "failed to get base address for file driver")
150 
151     /* Allocate space for the superblock */
152     if(NULL == (sblock = H5FL_CALLOC(H5F_super_t)))
153         HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")
154 
155     /* Read fixed-size portion of the superblock */
156     p = sbuf;
157     H5_CHECK_OVERFLOW(fixed_size, size_t, haddr_t);
158     if(H5FD_set_eoa(lf, H5FD_MEM_SUPER, (haddr_t)fixed_size) < 0)
159         HGOTO_ERROR(H5E_FILE, H5E_CANTINIT, NULL, "set end of space allocation request failed")
160     if(H5FD_read(lf, dxpl_id, H5FD_MEM_SUPER, (haddr_t)0, fixed_size, p) < 0)
161         HGOTO_ERROR(H5E_FILE, H5E_READERROR, NULL, "unable to read superblock")
162 
163     /* Skip over signature (already checked when locating the superblock) */
164     p += H5F_SIGNATURE_LEN;
165 
166     /* Superblock version */
167     super_vers = *p++;
168     if(super_vers > HDF5_SUPERBLOCK_VERSION_LATEST)
169         HGOTO_ERROR(H5E_FILE, H5E_BADVALUE, NULL, "bad superblock version number")
170     if(H5P_set(c_plist, H5F_CRT_SUPER_VERS_NAME, &super_vers) < 0)
171         HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, NULL, "unable to set superblock version")
172 
173     /* Record the superblock version */
174     sblock->super_vers = super_vers;
175 
176     /* Sanity check */
177     HDassert(((size_t)(p - sbuf)) == fixed_size);
178 
179     /* Determine the size of the variable-length part of the superblock */
180     variable_size = (size_t)H5F_SUPERBLOCK_VARLEN_SIZE(super_vers, f);
181     HDassert(variable_size > 0);
182     HDassert(fixed_size + variable_size <= sizeof(sbuf));
183 
184     /* Read in variable-sized portion of superblock */
185     if(H5FD_set_eoa(lf, H5FD_MEM_SUPER, (haddr_t)(fixed_size + variable_size)) < 0)
186         HGOTO_ERROR(H5E_FILE, H5E_CANTINIT, NULL, "set end of space allocation request failed")
187     if(H5FD_read(lf, dxpl_id, H5FD_MEM_SUPER, (haddr_t)fixed_size, variable_size, p) < 0)
188         HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "unable to read superblock")
189 
190     /* Check for older version of superblock format */
191     if(super_vers < HDF5_SUPERBLOCK_VERSION_2) {
192         uint32_t	status_flags;	    /* File status flags	   */
193         unsigned        btree_k[H5B_NUM_BTREE_ID];  /* B-tree internal node 'K' values */
194         unsigned        sym_leaf_k;         /* Symbol table leaf node's 'K' value */
195 
196         /* Freespace version (hard-wired) */
197         if(HDF5_FREESPACE_VERSION != *p++)
198             HGOTO_ERROR(H5E_FILE, H5E_BADVALUE, NULL, "bad free space version number")
199 
200         /* Root group version number (hard-wired) */
201         if(HDF5_OBJECTDIR_VERSION != *p++)
202             HGOTO_ERROR(H5E_FILE, H5E_BADVALUE, NULL, "bad object directory version number")
203 
204         /* Skip over reserved byte */
205         p++;
206 
207         /* Shared header version number (hard-wired) */
208         if(HDF5_SHAREDHEADER_VERSION != *p++)
209             HGOTO_ERROR(H5E_FILE, H5E_BADVALUE, NULL, "bad shared-header format version number")
210 
211         /* Size of file addresses */
212         sizeof_addr = *p++;
213         if(sizeof_addr != 2 && sizeof_addr != 4 &&
214                 sizeof_addr != 8 && sizeof_addr != 16 && sizeof_addr != 32)
215             HGOTO_ERROR(H5E_FILE, H5E_BADVALUE, NULL, "bad byte number in an address")
216         if(H5P_set(c_plist, H5F_CRT_ADDR_BYTE_NUM_NAME, &sizeof_addr) < 0)
217             HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, NULL, "unable to set byte number in an address")
218         shared->sizeof_addr = sizeof_addr;  /* Keep a local copy also */
219 
220         /* Size of file sizes */
221         sizeof_size = *p++;
222         if(sizeof_size != 2 && sizeof_size != 4 &&
223                 sizeof_size != 8 && sizeof_size != 16 && sizeof_size != 32)
224             HGOTO_ERROR(H5E_FILE, H5E_BADVALUE, NULL, "bad byte number for object size")
225         if(H5P_set(c_plist, H5F_CRT_OBJ_BYTE_NUM_NAME, &sizeof_size) < 0)
226             HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, NULL, "unable to set byte number for object size")
227         shared->sizeof_size = sizeof_size;  /* Keep a local copy also */
228 
229         /* Skip over reserved byte */
230         p++;
231 
232         /* Various B-tree sizes */
233         UINT16DECODE(p, sym_leaf_k);
234         if(sym_leaf_k == 0)
235             HGOTO_ERROR(H5E_FILE, H5E_BADRANGE, NULL, "bad symbol table leaf node 1/2 rank")
236         if(H5P_set(c_plist, H5F_CRT_SYM_LEAF_NAME, &sym_leaf_k) < 0)
237             HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, NULL, "unable to set rank for symbol table leaf nodes")
238         sblock->sym_leaf_k = sym_leaf_k;    /* Keep a local copy also */
239 
240         /* Need 'get' call to set other array values */
241         if(H5P_get(c_plist, H5F_CRT_BTREE_RANK_NAME, btree_k) < 0)
242             HGOTO_ERROR(H5E_PLIST, H5E_CANTGET, NULL, "unable to get rank for btree internal nodes")
243         UINT16DECODE(p, btree_k[H5B_SNODE_ID]);
244         if(btree_k[H5B_SNODE_ID] == 0)
245             HGOTO_ERROR(H5E_FILE, H5E_BADRANGE, NULL, "bad 1/2 rank for btree internal nodes")
246         /*
247          * Delay setting the value in the property list until we've checked
248          * for the indexed storage B-tree internal 'K' value later.
249          */
250 
251         /* File status flags (not really used yet) */
252         UINT32DECODE(p, status_flags);
253         HDassert(status_flags <= 255);
254         sblock->status_flags = (uint8_t)status_flags;
255         if(sblock->status_flags & ~H5F_SUPER_ALL_FLAGS)
256             HGOTO_ERROR(H5E_FILE, H5E_BADVALUE, NULL, "bad flag value for superblock")
257 
258         /*
259          * If the superblock version # is greater than 0, read in the indexed
260          * storage B-tree internal 'K' value
261          */
262         if(super_vers > HDF5_SUPERBLOCK_VERSION_DEF) {
263             UINT16DECODE(p, btree_k[H5B_CHUNK_ID]);
264             /* Reserved bytes are present only in version 1 */
265             if(super_vers == HDF5_SUPERBLOCK_VERSION_1)
266                 p += 2;   /* reserved */
267         } /* end if */
268         else
269             btree_k[H5B_CHUNK_ID] = HDF5_BTREE_CHUNK_IK_DEF;
270 
271         /* Set the B-tree internal node values, etc */
272         if(H5P_set(c_plist, H5F_CRT_BTREE_RANK_NAME, btree_k) < 0)
273             HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, NULL, "unable to set rank for btree internal nodes")
274         HDmemcpy(sblock->btree_k, btree_k, sizeof(unsigned) * (size_t)H5B_NUM_BTREE_ID);    /* Keep a local copy also */
275 
276         /* Remainder of "variable-sized" portion of superblock */
277         H5F_addr_decode(f, (const uint8_t **)&p, &sblock->base_addr/*out*/);
278         H5F_addr_decode(f, (const uint8_t **)&p, &sblock->ext_addr/*out*/);
279         H5F_addr_decode(f, (const uint8_t **)&p, &stored_eoa/*out*/);
280         H5F_addr_decode(f, (const uint8_t **)&p, &sblock->driver_addr/*out*/);
281 
282         /* Allocate space for the root group symbol table entry */
283         HDassert(!sblock->root_ent);
284         if(NULL == (sblock->root_ent = (H5G_entry_t *)H5MM_calloc(sizeof(H5G_entry_t))))
285             HGOTO_ERROR(H5E_FILE, H5E_CANTALLOC, NULL, "can't allocate space for root group symbol table entry")
286 
287         /* decode the root group symbol table entry */
288         if(H5G_ent_decode(f, (const uint8_t **)&p, sblock->root_ent) < 0)
289             HGOTO_ERROR(H5E_FILE, H5E_CANTDECODE, NULL, "can't decode root group symbol table entry")
290 
291         /* Set the root group address to the correct value */
292         sblock->root_addr = sblock->root_ent->header;
293 
294         /*
295          * Check if superblock address is different from base address and
296          * adjust base address and "end of address" address if so.
297          */
298         if(!H5F_addr_eq(base_addr, sblock->base_addr)) {
299             /* Check if the superblock moved earlier in the file */
300             if(H5F_addr_lt(base_addr, sblock->base_addr))
301                 stored_eoa -= (sblock->base_addr - base_addr);
302             else
303                 /* The superblock moved later in the file */
304                 stored_eoa += (base_addr - sblock->base_addr);
305 
306             /* Adjust base address for offsets of the HDF5 data in the file */
307             sblock->base_addr = base_addr;
308 
309             /* Set the base address for the file in the VFD now */
310             if(H5FD_set_base_addr(lf, sblock->base_addr) < 0)
311                 HGOTO_ERROR(H5E_FILE, H5E_CANTSET, NULL, "failed to set base address for file driver")
312 
313             /* Indicate that the superblock should be marked dirty */
314             *dirtied = TRUE;
315         } /* end if */
316 
317         /* This step is for h5repart tool only. If user wants to change file driver
318          *  from family to sec2 while using h5repart, set the driver address to
319          *  undefined to let the library ignore the family driver information saved
320          *  in the superblock.
321          */
322         if(H5F_HAS_FEATURE(f, H5FD_FEAT_IGNORE_DRVRINFO)) {
323             /* Eliminate the driver info */
324             sblock->driver_addr = HADDR_UNDEF;
325 
326             /* Indicate that the superblock should be marked dirty */
327             *dirtied = TRUE;
328         } /* end if */
329 
330         /* Decode the optional driver information block */
331         if(H5F_addr_defined(sblock->driver_addr)) {
332             uint8_t dbuf[H5F_MAX_DRVINFOBLOCK_SIZE];     /* Buffer for driver info block */
333             char drv_name[9];       /* Name of driver */
334             unsigned drv_vers;      /* Version of driver info block */
335             size_t drv_variable_size; /* Size of variable-length portion of driver info block, in bytes */
336 
337             /* Read in fixed-sized portion of driver info block */
338             p = dbuf;
339             if(H5FD_set_eoa(lf, H5FD_MEM_SUPER, sblock->driver_addr + H5F_DRVINFOBLOCK_HDR_SIZE) < 0)
340                 HGOTO_ERROR(H5E_FILE, H5E_CANTINIT, NULL, "set end of space allocation request failed")
341             if(H5FD_read(lf, dxpl_id, H5FD_MEM_SUPER, sblock->driver_addr, (size_t)H5F_DRVINFOBLOCK_HDR_SIZE, p) < 0)
342                 HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "unable to read driver information block")
343 
344             /* Version number */
345             drv_vers = *p++;
346             if(drv_vers != HDF5_DRIVERINFO_VERSION_0)
347                 HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "bad driver information block version number")
348 
349             p += 3; /* reserved bytes */
350 
351             /* Driver info size */
352             UINT32DECODE(p, drv_variable_size);
353 
354             /* Sanity check */
355             HDassert(H5F_DRVINFOBLOCK_HDR_SIZE + drv_variable_size <= sizeof(dbuf));
356 
357             /* Driver name and/or version */
358             HDstrncpy(drv_name, (const char *)p, (size_t)8);
359             drv_name[8] = '\0';
360             p += 8; /* advance past name/version */
361 
362             /* Check if driver matches driver information saved. Unfortunately, we can't push this
363              * function to each specific driver because we're checking if the driver is correct.
364              */
365             if(!HDstrncmp(drv_name, "NCSAfami", (size_t)8) && HDstrcmp(lf->cls->name, "family"))
366                 HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "family driver should be used")
367             if(!HDstrncmp(drv_name, "NCSAmult", (size_t)8) && HDstrcmp(lf->cls->name, "multi"))
368                 HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "multi driver should be used")
369 
370             /* Read in variable-sized portion of driver info block */
371             if(H5FD_set_eoa(lf, H5FD_MEM_SUPER, sblock->driver_addr + H5F_DRVINFOBLOCK_HDR_SIZE + drv_variable_size) < 0)
372                 HGOTO_ERROR(H5E_FILE, H5E_CANTINIT, NULL, "set end of space allocation request failed")
373             if(H5FD_read(lf, dxpl_id, H5FD_MEM_SUPER, sblock->driver_addr + H5F_DRVINFOBLOCK_HDR_SIZE, drv_variable_size, p) < 0)
374                 HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "unable to read file driver information")
375 
376             /* Decode driver information */
377             if(H5FD_sb_decode(lf, drv_name, p) < 0)
378                 HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "unable to decode driver information")
379         } /* end if */
380     } /* end if */
381     else {
382         uint32_t computed_chksum;       /* Computed checksum  */
383         uint32_t read_chksum;           /* Checksum read from file  */
384 
385         /* Size of file addresses */
386         sizeof_addr = *p++;
387         if(sizeof_addr != 2 && sizeof_addr != 4 &&
388                 sizeof_addr != 8 && sizeof_addr != 16 && sizeof_addr != 32)
389             HGOTO_ERROR(H5E_FILE, H5E_BADVALUE, NULL, "bad byte number in an address")
390         if(H5P_set(c_plist, H5F_CRT_ADDR_BYTE_NUM_NAME, &sizeof_addr) < 0)
391             HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, NULL, "unable to set byte number in an address")
392         shared->sizeof_addr = sizeof_addr;  /* Keep a local copy also */
393 
394         /* Size of file sizes */
395         sizeof_size = *p++;
396         if(sizeof_size != 2 && sizeof_size != 4 &&
397                 sizeof_size != 8 && sizeof_size != 16 && sizeof_size != 32)
398             HGOTO_ERROR(H5E_FILE, H5E_BADVALUE, NULL, "bad byte number for object size")
399         if(H5P_set(c_plist, H5F_CRT_OBJ_BYTE_NUM_NAME, &sizeof_size) < 0)
400             HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, NULL, "unable to set byte number for object size")
401         shared->sizeof_size = sizeof_size;  /* Keep a local copy also */
402 
403         /* File status flags (not really used yet) */
404         sblock->status_flags = *p++;
405         if(sblock->status_flags & ~H5F_SUPER_ALL_FLAGS)
406             HGOTO_ERROR(H5E_FILE, H5E_BADVALUE, NULL, "bad flag value for superblock")
407 
408         /* Base, superblock extension, end of file & root group object header addresses */
409         H5F_addr_decode(f, (const uint8_t **)&p, &sblock->base_addr/*out*/);
410         H5F_addr_decode(f, (const uint8_t **)&p, &sblock->ext_addr/*out*/);
411         H5F_addr_decode(f, (const uint8_t **)&p, &stored_eoa/*out*/);
412         H5F_addr_decode(f, (const uint8_t **)&p, &sblock->root_addr/*out*/);
413 
414         /* Compute checksum for superblock */
415         computed_chksum = H5_checksum_metadata(sbuf, (size_t)(p - sbuf), 0);
416 
417         /* Decode checksum */
418         UINT32DECODE(p, read_chksum);
419 
420         /* Verify correct checksum */
421         if(read_chksum != computed_chksum)
422             HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "bad checksum on driver information block")
423 
424         /*
425          * Check if superblock address is different from base address and
426          * adjust base address and "end of address" address if so.
427          */
428         if(!H5F_addr_eq(base_addr, sblock->base_addr)) {
429             /* Check if the superblock moved earlier in the file */
430             if(H5F_addr_lt(base_addr, sblock->base_addr))
431                 stored_eoa -= (sblock->base_addr - base_addr);
432             else
433                 /* The superblock moved later in the file */
434                 stored_eoa += (base_addr - sblock->base_addr);
435 
436             /* Adjust base address for offsets of the HDF5 data in the file */
437             sblock->base_addr = base_addr;
438 
439             /* Set the base address for the file in the VFD now */
440             if(H5FD_set_base_addr(lf, sblock->base_addr) < 0)
441                 HGOTO_ERROR(H5E_FILE, H5E_CANTSET, NULL, "failed to set base address for file driver")
442 
443             /* Indicate that the superblock should be marked dirty */
444             *dirtied = TRUE;
445         } /* end if */
446 
447         /* Get the B-tree internal node values, etc */
448         if(H5P_get(c_plist, H5F_CRT_BTREE_RANK_NAME, sblock->btree_k) < 0)
449             HGOTO_ERROR(H5E_PLIST, H5E_CANTGET, NULL, "unable to get rank for btree internal nodes")
450         if(H5P_get(c_plist, H5F_CRT_SYM_LEAF_NAME, &sblock->sym_leaf_k) < 0)
451             HGOTO_ERROR(H5E_PLIST, H5E_CANTGET, NULL, "unable to get rank for btree internal nodes")
452     } /* end else */
453 
454     /*
455      * The user-defined data is the area of the file before the base
456      * address.
457      */
458     if(H5P_set(c_plist, H5F_CRT_USER_BLOCK_NAME, &sblock->base_addr) < 0)
459         HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, NULL, "unable to set userblock size")
460 
461     /*
462      * Make sure that the data is not truncated. One case where this is
463      * possible is if the first file of a family of files was opened
464      * individually.
465      */
466     if(HADDR_UNDEF == (eof = H5FD_get_eof(lf)))
467         HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "unable to determine file size")
468 
469     /* (Account for the stored EOA being absolute offset -QAK) */
470     if((eof + sblock->base_addr) < stored_eoa)
471         HGOTO_ERROR(H5E_FILE, H5E_TRUNCATED, NULL, "truncated file: eof = %llu, sblock->base_addr = %llu, stored_eoa = %llu", (unsigned long long)eof, (unsigned long long)sblock->base_addr, (unsigned long long)stored_eoa)
472 
473     /*
474      * Tell the file driver how much address space has already been
475      * allocated so that it knows how to allocate additional memory.
476      */
477     /* (Account for the stored EOA being absolute offset -NAF) */
478     if(H5FD_set_eoa(lf, H5FD_MEM_SUPER, stored_eoa - sblock->base_addr) < 0)
479         HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "unable to set end-of-address marker for file")
480 
481     /* Read the file's superblock extension, if there is one. */
482     if(H5F_addr_defined(sblock->ext_addr)) {
483         H5O_loc_t ext_loc;      /* "Object location" for superblock extension */
484         H5O_btreek_t btreek;    /* v1 B-tree 'K' value message from superblock extension */
485         H5O_drvinfo_t drvinfo;  /* Driver info message from superblock extension */
486         htri_t status;          /* Status for message existing */
487 
488         /* Sanity check - superblock extension should only be defined for
489          *      superblock version >= 2.
490          */
491         HDassert(super_vers >= HDF5_SUPERBLOCK_VERSION_2);
492 
493         /* Check for superblock extension being located "outside" the stored
494          *      'eoa' value, which can occur with the split/multi VFD.
495          */
496         if(H5F_addr_gt(sblock->ext_addr, stored_eoa)) {
497             /* Set the 'eoa' for the object header memory type large enough
498              *  to give some room for a reasonably sized superblock extension.
499              *  (This is _rather_ a kludge -QAK)
500              */
501             if(H5FD_set_eoa(lf, H5FD_MEM_OHDR, (haddr_t)(sblock->ext_addr + 1024)) < 0)
502                 HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "unable to set end-of-address marker for file")
503         } /* end if */
504 
505         /* Open the superblock extension */
506 	if(H5F_super_ext_open(f, sblock->ext_addr, &ext_loc) < 0)
507             HGOTO_ERROR(H5E_FILE, H5E_CANTOPENOBJ, NULL, "unable to open file's superblock extension")
508 
509         /* Check for the extension having a 'driver info' message */
510         if((status = H5O_msg_exists(&ext_loc, H5O_DRVINFO_ID, dxpl_id)) < 0)
511             HGOTO_ERROR(H5E_FILE, H5E_CANTGET, NULL, "unable to read object header")
512         if(status) {
513             /* Check for ignoring the driver info for this file */
514             if(H5F_HAS_FEATURE(f, H5FD_FEAT_IGNORE_DRVRINFO)) {
515                 /* Indicate that the superblock should be marked dirty */
516                 *dirtied = TRUE;
517             } /* end if */
518             else {
519                 /* Retrieve the 'driver info' structure */
520                 if(NULL == H5O_msg_read(&ext_loc, H5O_DRVINFO_ID, &drvinfo, dxpl_id))
521                     HGOTO_ERROR(H5E_FILE, H5E_CANTGET, NULL, "driver info message not present")
522 
523                 /* Check if driver matches driver information saved. Unfortunately, we can't push this
524                  * function to each specific driver because we're checking if the driver is correct.
525                  */
526                 if(!HDstrncmp(drvinfo.name, "NCSAfami", (size_t)8) && HDstrcmp(lf->cls->name, "family"))
527                     HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "family driver should be used")
528                 if(!HDstrncmp(drvinfo.name, "NCSAmult", (size_t)8) && HDstrcmp(lf->cls->name, "multi"))
529                     HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "multi driver should be used")
530 
531                 /* Decode driver information */
532                 if(H5FD_sb_decode(lf, drvinfo.name, drvinfo.buf) < 0)
533                     HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "unable to decode driver information")
534 
535                 /* Reset driver info message */
536                 H5O_msg_reset(H5O_DRVINFO_ID, &drvinfo);
537             } /* end else */
538         } /* end if */
539 
540         /* Read in the shared OH message information if there is any */
541         if(H5SM_get_info(&ext_loc, c_plist, dxpl_id) < 0)
542             HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "unable to read SOHM table information")
543 
544         /* Check for the extension having a 'v1 B-tree "K"' message */
545         if((status = H5O_msg_exists(&ext_loc, H5O_BTREEK_ID, dxpl_id)) < 0)
546             HGOTO_ERROR(H5E_FILE, H5E_CANTGET, NULL, "unable to read object header")
547         if(status) {
548             /* Retrieve the 'v1 B-tree "K"' structure */
549             if(NULL == H5O_msg_read(&ext_loc, H5O_BTREEK_ID, &btreek, dxpl_id))
550                 HGOTO_ERROR(H5E_FILE, H5E_CANTGET, NULL, "v1 B-tree 'K' info message not present")
551 
552             /* Set non-default v1 B-tree 'K' value info from file */
553             sblock->btree_k[H5B_CHUNK_ID] = btreek.btree_k[H5B_CHUNK_ID];
554             sblock->btree_k[H5B_SNODE_ID] = btreek.btree_k[H5B_SNODE_ID];
555             sblock->sym_leaf_k = btreek.sym_leaf_k;
556 
557             /* Set non-default v1 B-tree 'K' values in the property list */
558             if(H5P_set(c_plist, H5F_CRT_BTREE_RANK_NAME, btreek.btree_k) < 0)
559                 HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, NULL, "unable to set rank for btree internal nodes")
560             if(H5P_set(c_plist, H5F_CRT_SYM_LEAF_NAME, &btreek.sym_leaf_k) < 0)
561                 HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, NULL, "unable to set rank for symbol table leaf nodes")
562         } /* end if */
563 
564         /* Close superblock extension */
565         if(H5F_super_ext_close(f, &ext_loc, dxpl_id, FALSE) < 0)
566 	    HGOTO_ERROR(H5E_FILE, H5E_CANTRELEASE, NULL, "unable to close file's superblock extension")
567     } /* end if */
568 
569     /* Set return value */
570     ret_value = sblock;
571 
572 done:
573     /* Release the [possibly partially initialized] superblock on errors */
574     if(!ret_value && sblock)
575         if(H5F_super_free(sblock) < 0)
576             HDONE_ERROR(H5E_FILE, H5E_CANTFREE, NULL, "unable to destroy superblock data")
577 
578     FUNC_LEAVE_NOAPI(ret_value)
579 } /* end H5F_sblock_load() */
580 
581 
582 /*-------------------------------------------------------------------------
583  * Function:    H5F_sblock_flush
584  *
585  * Purpose:     Flushes the superblock.
586  *
587  * Return:      Success:        SUCCEED
588  *              Failure:        NULL
589  *
590  * Programmer:  Mike McGreevy
591  *              mamcgree@hdfgroup.org
592  *              April 8, 2009
593  *
594  *-------------------------------------------------------------------------
595  */
596 static herr_t
H5F_sblock_flush(H5F_t * f,hid_t dxpl_id,hbool_t destroy,haddr_t UNUSED addr,H5F_super_t * sblock)597 H5F_sblock_flush(H5F_t *f, hid_t dxpl_id, hbool_t destroy, haddr_t UNUSED addr,
598     H5F_super_t *sblock)
599 {
600     herr_t          ret_value = SUCCEED;
601 
602     FUNC_ENTER_NOAPI_NOINIT
603 
604     /* check arguments */
605     HDassert(f);
606     HDassert(H5F_addr_eq(addr, 0));
607     HDassert(sblock);
608 
609     if(sblock->cache_info.is_dirty) {
610         uint8_t         buf[H5F_MAX_SUPERBLOCK_SIZE + H5F_MAX_DRVINFOBLOCK_SIZE];  /* Superblock & driver info blockencoding buffer */
611         uint8_t        *p;                          /* Ptr into encoding buffer */
612         haddr_t         rel_eoa;                    /* Relative EOA for file */
613         size_t          superblock_size;            /* Size of superblock, in bytes */
614         size_t          driver_size;                /* Size of driver info block (bytes)*/
615 
616         /* Encode the common portion of the file superblock for all versions */
617         p = buf;
618         HDmemcpy(p, H5F_SIGNATURE, (size_t)H5F_SIGNATURE_LEN);
619         p += H5F_SIGNATURE_LEN;
620         *p++ = (uint8_t)sblock->super_vers;
621 
622         /* Check for older version of superblock format */
623         if(sblock->super_vers < HDF5_SUPERBLOCK_VERSION_2) {
624             *p++ = (uint8_t)HDF5_FREESPACE_VERSION;     /* (hard-wired) */
625             *p++ = (uint8_t)HDF5_OBJECTDIR_VERSION;     /* (hard-wired) */
626             *p++ = 0;   /* reserved*/
627 
628             *p++ = (uint8_t)HDF5_SHAREDHEADER_VERSION;  /* (hard-wired) */
629             *p++ = (uint8_t)H5F_SIZEOF_ADDR(f);
630             *p++ = (uint8_t)H5F_SIZEOF_SIZE(f);
631             *p++ = 0;   /* reserved */
632 
633             UINT16ENCODE(p, sblock->sym_leaf_k);
634             UINT16ENCODE(p, sblock->btree_k[H5B_SNODE_ID]);
635             UINT32ENCODE(p, (uint32_t)sblock->status_flags);
636 
637             /*
638              * Versions of the superblock >0 have the indexed storage B-tree
639              * internal 'K' value stored
640              */
641             if(sblock->super_vers > HDF5_SUPERBLOCK_VERSION_DEF) {
642                 UINT16ENCODE(p, sblock->btree_k[H5B_CHUNK_ID]);
643                 *p++ = 0;   /*reserved */
644                 *p++ = 0;   /*reserved */
645             } /* end if */
646 
647             H5F_addr_encode(f, &p, sblock->base_addr);
648             H5F_addr_encode(f, &p, sblock->ext_addr);
649             rel_eoa = H5FD_get_eoa(f->shared->lf, H5FD_MEM_SUPER);
650             H5F_addr_encode(f, &p, (rel_eoa + sblock->base_addr));
651             H5F_addr_encode(f, &p, sblock->driver_addr);
652 
653             /* Encode the root group object entry, including the cached stab info */
654             if(H5G_ent_encode(f, &p, sblock->root_ent) < 0)
655                 HGOTO_ERROR(H5E_FILE, H5E_CANTENCODE, FAIL, "can't encode root group symbol table entry")
656 
657             /* Encode the driver information block. */
658             H5_ASSIGN_OVERFLOW(driver_size, H5FD_sb_size(f->shared->lf), hsize_t, size_t);
659 
660             /* Checking whether driver block address is defined here is to handle backward
661              * compatibility.  If the file was created with v1.6 library or earlier and no
662              * driver info block was written in the superblock, we don't write it either even
663              * though there's some driver info.  Otherwise, the driver block extended will
664              * overwrite the (meta)data right after the superblock. This situation happens to
665              * the family driver particularly.  SLU - 2009/3/24
666              */
667             if(driver_size > 0 && H5F_addr_defined(sblock->driver_addr)) {
668                 char driver_name[9];    /* Name of driver, for driver info block */
669                 uint8_t *dbuf = p;      /* Pointer to beginning of driver info */
670 
671                 /* Encode the driver information block */
672                 *p++ = HDF5_DRIVERINFO_VERSION_0; /* Version */
673                 *p++ = 0; /* reserved */
674                 *p++ = 0; /* reserved */
675                 *p++ = 0; /* reserved */
676 
677                 /* Driver info size, excluding header */
678                 UINT32ENCODE(p, driver_size);
679 
680                 /* Encode driver-specific data */
681                 if(H5FD_sb_encode(f->shared->lf, driver_name, dbuf + H5F_DRVINFOBLOCK_HDR_SIZE) < 0)
682                     HGOTO_ERROR(H5E_FILE, H5E_CANTINIT, FAIL, "unable to encode driver information")
683 
684                 /* Store driver name (set in 'H5FD_sb_encode' call above) */
685                 HDmemcpy(p, driver_name, (size_t)8);
686 
687                 /* Advance buffer pointer past name & variable-sized portion of driver info */
688                 /* (for later use in computing the superblock size) */
689                 p += 8 + driver_size;
690             } /* end if */
691         } /* end if */
692         else {
693             uint32_t        chksum;                 /* Checksum temporary variable      */
694             H5O_loc_t       *root_oloc;             /* Pointer to root group's object location */
695 
696             /* Size of file addresses & offsets, and status flags */
697             *p++ = (uint8_t)H5F_SIZEOF_ADDR(f);
698             *p++ = (uint8_t)H5F_SIZEOF_SIZE(f);
699             *p++ = sblock->status_flags;
700 
701             /* Base, superblock extension & end of file addresses */
702             H5F_addr_encode(f, &p, sblock->base_addr);
703             H5F_addr_encode(f, &p, sblock->ext_addr);
704             rel_eoa = H5FD_get_eoa(f->shared->lf, H5FD_MEM_SUPER);
705             H5F_addr_encode(f, &p, (rel_eoa + sblock->base_addr));
706 
707             /* Retrieve information for root group */
708             if(NULL == (root_oloc = H5G_oloc(f->shared->root_grp)))
709                 HGOTO_ERROR(H5E_FILE, H5E_CANTINIT, FAIL, "unable to retrieve root group information")
710 
711             /* Encode address of root group's object header */
712             H5F_addr_encode(f, &p, root_oloc->addr);
713 
714             /* Compute superblock checksum */
715             chksum = H5_checksum_metadata(buf, ((size_t)H5F_SUPERBLOCK_SIZE(sblock->super_vers, f) - H5F_SIZEOF_CHKSUM), 0);
716 
717             /* Superblock checksum */
718             UINT32ENCODE(p, chksum);
719 
720             /* Sanity check */
721             HDassert((size_t)(p - buf) == (size_t)H5F_SUPERBLOCK_SIZE(sblock->super_vers, f));
722         } /* end else */
723 
724         /* Retrieve the total size of the superblock info */
725         H5_ASSIGN_OVERFLOW(superblock_size, (p - buf), ptrdiff_t, size_t);
726 
727         /* Double check we didn't overrun the block (unlikely) */
728         HDassert(superblock_size <= sizeof(buf));
729 
730         /* Write superblock */
731         /* (always at relative address 0) */
732         if(H5FD_write(f->shared->lf, dxpl_id, H5FD_MEM_SUPER, (haddr_t)0, superblock_size, buf) < 0)
733             HGOTO_ERROR(H5E_IO, H5E_WRITEERROR, FAIL, "unable to write superblock")
734 
735         /* Check for newer version of superblock format & superblock extension */
736         if(sblock->super_vers >= HDF5_SUPERBLOCK_VERSION_2 && H5F_addr_defined(sblock->ext_addr)) {
737             /* Check for ignoring the driver info for this file */
738             if(!H5F_HAS_FEATURE(f, H5FD_FEAT_IGNORE_DRVRINFO)) {
739                 /* Check for driver info message */
740                 H5_ASSIGN_OVERFLOW(driver_size, H5FD_sb_size(f->shared->lf), hsize_t, size_t);
741                 if(driver_size > 0) {
742                     H5O_drvinfo_t drvinfo;      /* Driver info */
743                     H5O_loc_t 	ext_loc; 	/* "Object location" for superblock extension */
744                     uint8_t dbuf[H5F_MAX_DRVINFOBLOCK_SIZE];  /* Driver info block encoding buffer */
745 
746                     /* Sanity check */
747                     HDassert(driver_size <= H5F_MAX_DRVINFOBLOCK_SIZE);
748 
749                     /* Encode driver-specific data */
750                     if(H5FD_sb_encode(f->shared->lf, drvinfo.name, dbuf) < 0)
751                         HGOTO_ERROR(H5E_FILE, H5E_CANTINIT, FAIL, "unable to encode driver information")
752 
753                     /* Open the superblock extension's object header */
754                     if(H5F_super_ext_open(f, sblock->ext_addr, &ext_loc) < 0)
755                         HGOTO_ERROR(H5E_FILE, H5E_CANTOPENOBJ, FAIL, "unable to open file's superblock extension")
756 
757                     /* Write driver info information to the superblock extension */
758                     drvinfo.len = driver_size;
759                     drvinfo.buf = dbuf;
760                     if(H5O_msg_write(&ext_loc, H5O_DRVINFO_ID, H5O_MSG_FLAG_DONTSHARE, H5O_UPDATE_TIME, &drvinfo, dxpl_id) < 0)
761                         HGOTO_ERROR(H5E_FILE, H5E_WRITEERROR, FAIL, "unable to update driver info header message")
762 
763                     /* Close the superblock extension object header */
764                     if(H5F_super_ext_close(f, &ext_loc, dxpl_id, FALSE) < 0)
765                         HGOTO_ERROR(H5E_FILE, H5E_CANTCLOSEOBJ, FAIL, "unable to close file's superblock extension")
766                 } /* end if */
767             } /* end if */
768         } /* end if */
769 
770         /* Reset the dirty flag.  */
771         sblock->cache_info.is_dirty = FALSE;
772     } /* end if */
773 
774     if(destroy)
775         if(H5F_sblock_dest(f, sblock) < 0)
776             HGOTO_ERROR(H5E_FSPACE, H5E_CLOSEERROR, FAIL, "can't close superblock")
777 
778 done:
779     FUNC_LEAVE_NOAPI(ret_value)
780 } /* end H5F_sblock_flush() */
781 
782 
783 /*-------------------------------------------------------------------------
784  * Function:    H5F_sblock_dest
785  *
786  * Purpose:     Frees memory used by the superblock.
787  *
788  * Return:      Non-negative on success/Negative on failure
789  *
790  * Programmer:  Mike McGreevy
791  *              April 8, 2009
792  *
793  *-------------------------------------------------------------------------
794  */
795 static herr_t
H5F_sblock_dest(H5F_t UNUSED * f,H5F_super_t * sblock)796 H5F_sblock_dest(H5F_t UNUSED *f, H5F_super_t* sblock)
797 {
798     herr_t ret_value = SUCCEED;         /* Return value */
799 
800     FUNC_ENTER_NOAPI_NOINIT
801 
802     /* Sanity check */
803     HDassert(sblock);
804 
805     /* Free superblock */
806     if(H5F_super_free(sblock) < 0)
807         HGOTO_ERROR(H5E_FILE, H5E_CANTFREE, FAIL, "unable to destroy superblock")
808 
809 done:
810     FUNC_LEAVE_NOAPI(ret_value)
811 } /* end H5F_sblock_dest() */
812 
813 
814 /*-------------------------------------------------------------------------
815  * Function:    H5F_sblock_clear
816  *
817  * Purpose:     Mark the superblock as no longer being dirty.
818  *
819  * Return:      Non-negative on success/Negative on failure
820  *
821  * Programmer:  Mike McGreevy
822  *              April 8, 2009
823  *
824  *-------------------------------------------------------------------------
825  */
826 static herr_t
H5F_sblock_clear(H5F_t * f,H5F_super_t * sblock,hbool_t destroy)827 H5F_sblock_clear(H5F_t *f, H5F_super_t *sblock, hbool_t destroy)
828 {
829     herr_t ret_value = SUCCEED;         /* Return value */
830 
831     FUNC_ENTER_NOAPI_NOINIT
832 
833     /*
834      * Check arguments.
835      */
836     HDassert(sblock);
837 
838     /* Reset the dirty flag.  */
839     sblock->cache_info.is_dirty = FALSE;
840 
841     if(destroy)
842         if(H5F_sblock_dest(f, sblock) < 0)
843             HGOTO_ERROR(H5E_FILE, H5E_CANTFREE, FAIL, "unable to delete superblock")
844 
845 done:
846     FUNC_LEAVE_NOAPI(ret_value)
847 } /* end H5F_sblock_clear() */
848 
849 
850 /*-------------------------------------------------------------------------
851  * Function:    H5F_sblock_size
852  *
853  * Purpose:     Returns the size of the superblock encoded on disk.
854  *
855  * Return:      Non-negative on success/Negative on failure
856  *
857  * Programmer:  Mike McGreevy
858  *              April 8, 2009
859  *
860  *-------------------------------------------------------------------------
861  */
862 static herr_t
H5F_sblock_size(const H5F_t * f,const H5F_super_t * sblock,size_t * size_ptr)863 H5F_sblock_size(const H5F_t *f, const H5F_super_t *sblock, size_t *size_ptr)
864 {
865     FUNC_ENTER_NOAPI_NOINIT_NOERR
866 
867     /* check arguments */
868     HDassert(f);
869     HDassert(sblock);
870     HDassert(size_ptr);
871 
872     /* Set size value */
873     *size_ptr = (size_t)H5F_SUPERBLOCK_SIZE(sblock->super_vers, f);
874 
875     FUNC_LEAVE_NOAPI(SUCCEED)
876 } /* end H5F_sblock_size() */
877 
878