summaryrefslogtreecommitdiffstats
path: root/src/H5FScache.c
blob: d5f2817f77326f34bf72801f99ec134394446fbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*-------------------------------------------------------------------------
 *
 * Created:     H5FScache.c
 *              May  2 2006
 *              Quincey Koziol <koziol@hdfgroup.org>
 *
 * Purpose:     Implement file free space metadata cache methods.
 *
 *-------------------------------------------------------------------------
 */

/****************/
/* Module Setup */
/****************/

#include "H5FSmodule.h"         /* This source code file is part of the H5FS module */


/***********/
/* Headers */
/***********/
#include "H5private.h"		/* Generic Functions			*/
#include "H5ACprivate.h"        /* Metadata cache                       */
#include "H5Eprivate.h"		/* Error handling		  	*/
#include "H5Fprivate.h"		/* File          			*/
#include "H5FSpkg.h"		/* File free space			*/
#include "H5MFprivate.h"	/* File memory management		*/
#include "H5MMprivate.h"	/* Memory management			*/
#include "H5VMprivate.h"	/* Vectors and arrays 			*/
#include "H5WBprivate.h"        /* Wrapped Buffers                      */


/****************/
/* Local Macros */
/****************/

/* File free space format version #'s */
#define H5FS_HDR_VERSION        0               /* Header */
#define H5FS_SINFO_VERSION      0               /* Serialized sections */


/******************/
/* Local Typedefs */
/******************/

/* User data for skip list iterator callback for iterating over section size nodes when syncing */
typedef struct {
    H5FS_sinfo_t *sinfo;        /* Free space section info */
    uint8_t **image;            /* Pointer to address of buffer pointer to serialize with */
    unsigned sect_cnt_size;     /* # of bytes to encode section size counts in */
} H5FS_iter_ud_t;


/********************/
/* Package Typedefs */
/********************/


/********************/
/* Local Prototypes */
/********************/

/* Section info routines */
static herr_t H5FS__sinfo_serialize_sect_cb(void *_item, void H5_ATTR_UNUSED *key, void *_udata);
static herr_t H5FS__sinfo_serialize_node_cb(void *_item, void H5_ATTR_UNUSED *key, void *_udata);

/* Metadata cache callbacks */
static herr_t H5FS__cache_hdr_get_initial_load_size(void *udata, size_t *image_len);
static htri_t H5FS__cache_hdr_verify_chksum(const void *image_ptr, size_t len, void *udata_ptr);
static void *H5FS__cache_hdr_deserialize(const void *image, size_t len,
    void *udata, hbool_t *dirty);
static herr_t H5FS__cache_hdr_image_len(const void *thing, size_t *image_len);
static herr_t H5FS__cache_hdr_pre_serialize(H5F_t *f, void *thing, haddr_t addr,
    size_t len, haddr_t *new_addr, size_t *new_len, unsigned *flags);
static herr_t H5FS__cache_hdr_serialize(const H5F_t *f, void *image, 
    size_t len, void *thing);
static herr_t H5FS__cache_hdr_notify(H5AC_notify_action_t action, void *thing);
static herr_t H5FS__cache_hdr_free_icr(void *thing);

static herr_t H5FS__cache_sinfo_get_initial_load_size(void *udata, size_t *image_len);
static htri_t H5FS__cache_sinfo_verify_chksum(const void *image_ptr, size_t len, void *udata_ptr);
static void *H5FS__cache_sinfo_deserialize(const void *image, size_t len,
    void *udata, hbool_t *dirty);
static herr_t H5FS__cache_sinfo_image_len(const void *thing, size_t *image_len);
static herr_t H5FS__cache_sinfo_pre_serialize(H5F_t *f, void *thing,
    haddr_t addr, size_t len, haddr_t *new_addr, size_t *new_len, unsigned *flags);
static herr_t H5FS__cache_sinfo_serialize(const H5F_t *f, void *image,
    size_t len, void *thing);
static herr_t H5FS__cache_sinfo_notify(H5AC_notify_action_t action, void *thing);
static herr_t H5FS__cache_sinfo_free_icr(void *thing);


/*********************/
/* Package Variables */
/*********************/

/* H5FS header inherits cache-like properties from H5AC */
const H5AC_class_t H5AC_FSPACE_HDR[1] = {{
    H5AC_FSPACE_HDR_ID,                 /* Metadata client ID */
    "Free Space Header",                /* Metadata client name (for debugging) */
    H5FD_MEM_FSPACE_HDR,                /* File space memory type for client */
    H5AC__CLASS_NO_FLAGS_SET,           /* Client class behavior flags */
    H5FS__cache_hdr_get_initial_load_size,      /* 'get_initial_load_size' callback */
    NULL,				/* 'get_final_load_size' callback */
    H5FS__cache_hdr_verify_chksum,	/* 'verify_chksum' callback */
    H5FS__cache_hdr_deserialize,        /* 'deserialize' callback */
    H5FS__cache_hdr_image_len,          /* 'image_len' callback */
    H5FS__cache_hdr_pre_serialize,      /* 'pre_serialize' callback */
    H5FS__cache_hdr_serialize,          /* 'serialize' callback */
    H5FS__cache_hdr_notify, 		/* 'notify' callback */
    H5FS__cache_hdr_free_icr,           /* 'free_icr' callback */
    NULL,                               /* 'fsf_size' callback */
}};

/* H5FS section info inherits cache-like properties from H5AC */
const H5AC_class_t H5AC_FSPACE_SINFO[1] = {{
    H5AC_FSPACE_SINFO_ID,               /* Metadata client ID */
    "Free Space Section Info",          /* Metadata client name (for debugging) */
    H5FD_MEM_FSPACE_SINFO,              /* File space memory type for client */
    H5AC__CLASS_NO_FLAGS_SET,           /* Client class behavior flags */
    H5FS__cache_sinfo_get_initial_load_size,    /* 'get_initial_load_size' callback */
    NULL,				/* 'get_final_load_size' callback */
    H5FS__cache_sinfo_verify_chksum,	/* 'verify_chksum' callback */
    H5FS__cache_sinfo_deserialize,      /* 'deserialize' callback */
    H5FS__cache_sinfo_image_len,        /* 'image_len' callback */
    H5FS__cache_sinfo_pre_serialize,    /* 'pre_serialize' callback */
    H5FS__cache_sinfo_serialize,        /* 'serialize' callback */
    H5FS__cache_sinfo_notify, 		/* 'notify' callback */
    H5FS__cache_sinfo_free_icr,         /* 'free_icr' callback */
    NULL,                               /* 'fsf_size' callback */
}};


/*****************************/
/* Library Private Variables */
/*****************************/


/*******************/
/* Local Variables */
/*******************/



/*-------------------------------------------------------------------------
 * Function:    H5FS__cache_hdr_get_initial_load_size
 *
 * Purpose:     Compute the size of the data structure on disk.
 *
 * Return:      Non-negative on success/Negative on failure
 *
 * Programmer:  Quincey Koziol
 *              koziol@hdfgroup.org
 *              August 14, 2013
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5FS__cache_hdr_get_initial_load_size(void *_udata, size_t *image_len)
{
    H5FS_hdr_cache_ud_t *udata = (H5FS_hdr_cache_ud_t *)_udata; /* User-data for metadata cache callback */

    FUNC_ENTER_STATIC_NOERR

    /* Check arguments */
    HDassert(udata);
    HDassert(udata->f);
    HDassert(image_len);

    /* Set the image length size */
    *image_len = (size_t)H5FS_HEADER_SIZE(udata->f);

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5FS__cache_hdr_get_initial_load_size() */


/*-------------------------------------------------------------------------
 * Function:    H5FS__cache_hdr_verify_chksum
 *
 * Purpose:     Verify the computed checksum of the data structure is the
 *              same as the stored chksum.
 *
 * Return:      Success:        TRUE/FALSE
 *              Failure:        Negative
 *
 * Programmer:	Vailin Choi; Aug 2015
 *
 *-------------------------------------------------------------------------
 */
htri_t
H5FS__cache_hdr_verify_chksum(const void *_image, size_t len, void H5_ATTR_UNUSED *_udata)
{
    const uint8_t *image = (const uint8_t *)_image;       /* Pointer into raw data buffer */
    uint32_t stored_chksum;     /* Stored metadata checksum value */
    uint32_t computed_chksum;   /* Computed metadata checksum value */
    htri_t ret_value = TRUE;	/* Return value */

    FUNC_ENTER_STATIC_NOERR

    /* Check arguments */
    HDassert(image);

    /* Get stored and computed checksums */
    H5F_get_checksums(image, len, &stored_chksum, &computed_chksum);

    if(stored_chksum != computed_chksum)
        ret_value = FALSE;

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5FS__cache_hdr_verify_chksum() */


/*-------------------------------------------------------------------------
 * Function:	H5FS__cache_hdr_deserialize
 *
 * Purpose:	Given a buffer containing the on disk image of the free space
 *      	manager section info, allocate an instance of H5FS_t, load
 *      	it with the data contained in the image, and return a pointer
 *              to the new instance.
 *
 * Return:	Success:	Pointer to new object
 *		Failure:	NULL
 *
 * Programmer:	Quincey Koziol
 *		koziol@hdfgroup.org
 *		August 18 2013
 *
 *-------------------------------------------------------------------------
 */
static void *
H5FS__cache_hdr_deserialize(const void *_image, size_t H5_ATTR_NDEBUG_UNUSED len, void *_udata, 
    hbool_t H5_ATTR_UNUSED *dirty)
{
    H5FS_t		*fspace = NULL; /* Free space header info */
    H5FS_hdr_cache_ud_t *udata = (H5FS_hdr_cache_ud_t *)_udata;  /* User data for callback */
    const uint8_t	*image = (const uint8_t *)_image;       /* Pointer into raw data buffer */
    uint32_t            stored_chksum;  /* Stored metadata checksum value */
    unsigned            nclasses;       /* Number of section classes */
    H5FS_t		*ret_value = NULL;      /* Return value */

    FUNC_ENTER_STATIC

    /* Check arguments */
    HDassert(image);
    HDassert(udata);
    HDassert(udata->f);

    /* Allocate a new free space manager */
    if(NULL == (fspace = H5FS__new(udata->f, udata->nclasses, udata->classes, udata->cls_init_udata)))
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")

    /* Set free space manager's internal information */
    fspace->addr = udata->addr;

    /* Magic number */
    if(HDmemcmp(image, H5FS_HDR_MAGIC, (size_t)H5_SIZEOF_MAGIC))
        HGOTO_ERROR(H5E_FSPACE, H5E_CANTLOAD, NULL, "wrong free space header signature")
    image += H5_SIZEOF_MAGIC;

    /* Version */
    if(*image++ != H5FS_HDR_VERSION)
        HGOTO_ERROR(H5E_FSPACE, H5E_CANTLOAD, NULL, "wrong free space header version")

    /* Client ID */
    fspace->client = (H5FS_client_t)*image++;
    if(fspace->client >= H5FS_NUM_CLIENT_ID)
        HGOTO_ERROR(H5E_FSPACE, H5E_CANTLOAD, NULL, "unknown client ID in free space header")

    /* Total space tracked */
    H5F_DECODE_LENGTH(udata->f, image, fspace->tot_space);

    /* Total # of free space sections tracked */
    H5F_DECODE_LENGTH(udata->f, image, fspace->tot_sect_count);

    /* # of serializable free space sections tracked */
    H5F_DECODE_LENGTH(udata->f, image, fspace->serial_sect_count);

    /* # of ghost free space sections tracked */
    H5F_DECODE_LENGTH(udata->f, image, fspace->ghost_sect_count);

    /* # of section classes */
    /* (only check if we actually have some classes) */
    UINT16DECODE(image, nclasses);
    if(fspace->nclasses > 0 && nclasses > fspace->nclasses)
        HGOTO_ERROR(H5E_FSPACE, H5E_CANTLOAD, NULL, "section class count mismatch")

    /* Shrink percent */
    UINT16DECODE(image, fspace->shrink_percent);

    /* Expand percent */
    UINT16DECODE(image, fspace->expand_percent);

    /* Size of address space free space sections are within 
     * (log2 of actual value) 
     */
    UINT16DECODE(image, fspace->max_sect_addr);

    /* Max. size of section to track */
    H5F_DECODE_LENGTH(udata->f, image, fspace->max_sect_size);

    /* Address of serialized free space sections */
    H5F_addr_decode(udata->f, &image, &fspace->sect_addr);

    /* Size of serialized free space sections */
    H5F_DECODE_LENGTH(udata->f, image, fspace->sect_size);

    /* Allocated size of serialized free space sections */
    H5F_DECODE_LENGTH(udata->f, image, fspace->alloc_sect_size);

    /* checksum verification already done in verify_chksum cb */

    /* Metadata checksum */
    UINT32DECODE(image, stored_chksum);

    /* Sanity check */
    HDassert((size_t)(image - (const uint8_t *)_image) <= len);

    /* Set return value */
    ret_value = fspace;

done:
    /* Release resources */
    if(!ret_value && fspace)
        if(H5FS__hdr_dest(fspace) < 0)
            HDONE_ERROR(H5E_FSPACE, H5E_CANTFREE, NULL, "unable to destroy free space header")

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5FS__cache_hdr_deserialize() */


/*-------------------------------------------------------------------------
 * Function:    H5FS__cache_hdr_image_len
 *
 * Purpose:     Compute the size of the data structure on disk and return
 *              it in *image_len.
 *
 * Return:      Non-negative on success/Negative on failure
 *
 * Programmer:  Quincey Koziol
 *              koziol@hdfgroup.org
 *              August 14, 2013
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5FS__cache_hdr_image_len(const void *_thing, size_t *image_len)
{
    const H5FS_t *fspace = (const H5FS_t *)_thing;       /* Pointer to the object */

    FUNC_ENTER_STATIC_NOERR

    /* Check arguments */
    HDassert(fspace);
    HDassert(fspace->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC);
    HDassert(fspace->cache_info.type == H5AC_FSPACE_HDR);
    HDassert(image_len);

    /* Set the image length size */
    *image_len = fspace->hdr_size;

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5FS__cache_hdr_image_len() */


/*-------------------------------------------------------------------------
 * Function:	H5FS__cache_hdr_pre_serialize
 *
 * Purpose:	The free space manager header contains the address, size, and 
 *		allocation size of the free space manager section info.  However,
 *		since it is possible for the section info to either not be allocated
 *		at all, or be allocated in temporary (AKA imaginary) files space,
 *		it is possible for the above mentioned fields to contain giberish
 *		when the free space manager header is serialized.
 *
 *		This function exists to prevent this problem.  It does so by 
 *		forcing allocation of real file space for the section information.
 *
 *		Note that in the Version 2 cache, this problem was dealt with by
 *		simply flushing the section info before flushing the header.  This
 *		was possible, since the clients handled file I/O directly.  As 
 *		this responsibility has moved to the cache in Version 3, this 
 *		solution is no longer directly applicable.
 *
 * Return:	Success:	SUCCEED
 *		Failure:	FAIL
 *
 * Programmer:	John Mainzer
 *		6/21/14
 *
 *-------------------------------------------------------------------------
 */
static herr_t 
H5FS__cache_hdr_pre_serialize(H5F_t *f, void *_thing,
    haddr_t addr, size_t H5_ATTR_UNUSED len, haddr_t H5_ATTR_NDEBUG_UNUSED *new_addr, 
    size_t H5_ATTR_NDEBUG_UNUSED *new_len, unsigned *flags)
{
    H5FS_t 	*fspace = (H5FS_t *)_thing;     /* Pointer to the object */
    H5AC_ring_t orig_ring = H5AC_RING_INV;      /* Original ring value */
    herr_t     	 ret_value = SUCCEED;           /* Return value */

    FUNC_ENTER_STATIC

    /* Sanity check */
    HDassert(f);
    HDassert(fspace);
    HDassert(fspace->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC);
    HDassert(fspace->cache_info.type == H5AC_FSPACE_HDR);
    HDassert(H5F_addr_defined(addr));
    HDassert(new_addr);
    HDassert(new_len);
    HDassert(flags);

    if(fspace->sinfo) {
        H5AC_ring_t ring;

        /* Retrieve the ring type for the header */
        if(H5AC_get_entry_ring(f, addr, &ring) < 0)
            HGOTO_ERROR(H5E_FSPACE, H5E_CANTGET, FAIL, "unable to get property value");

        /* Set the ring type for the section info in the API context */
        H5AC_set_ring(ring, &orig_ring);

        /* This implies that the header "owns" the section info.  
         *
         * Unfortunately, the comments in the code are not clear as to 
         * what this means, but from reviewing the code (most particularly
         * H5FS_close(), H5FS_sinfo_lock, and H5FS_sinfo_unlock()), I 
         * gather that it means that the header is maintaining a pointer to 
         * an instance of H5FS_sinfo_t in which free space data is 
         * maintained, and either:
         *
         * 1) The instance of H5FS_sinfo_t is not in the metadata cache.
         *
         *    This will be TRUE iff H5F_addr_defined(fspace->sect_addr) 
         *    is FALSE, and fspace->sinfo is not NULL.  This is sometimes
         *    referred to as "floating" section info in the comments.
         *
         *    If the section info structure contains free space data 
         *    that must be placed on disk eventually, then 
         *
         *        fspace->serial_sect_count > 0
         *
         *    and
         *
         *        H5F_addr_defined(fspace->addr)
         *
         *    will both be TRUE.  If this contition does not hold, then
         *    either the free space info is not persistent 
         *    (!H5F_addr_defined(fspace->addr)???) or the section info 
         *    contains no free space data that must be written to file 
         *    ( fspace->serial_sect_count == 0 ).
         *
         * 2) The instance of H5FS_sinfo_t is in the metadata cache with
         *    address in temporary file space (AKA imaginary file space).
         *    The entry may or may not be protected, and if protected, it 
         *    may be protected either RW or RO (as indicated by 
         *    fspace->sinfo_protected and  fspace->sinfo_accmod).
         *
         * 3) The instance of H5FS_sinfo_t is in the metadata cache with
         *    address in real file space.  As in case 2) above, the entry
         *    may or may not be protected, and if protected, it 
         *    may be protected either RW or RO (as indicated by 
         *    fspace->sinfo_protected and  fspace->sinfo_accmod).
         *
         * Observe that fspace->serial_sect_count > 0 must be TRUE in 
         * cases 2) and 3), as the section info should not be stored on 
         * disk if it doesn't exist.  Similarly, since the section info
         * will not be stored to disk unless the header is, 
         * H5F_addr_defined(fspace->addr) must hold as well.
         *
         * As the objective is to touch up the free space manager header
         * so that it contains sensical data on the size and location of 
         * the section information, we have to handle each of the above
         * cases differently.
         *
         * Case 1) If either fspace->serial_sect_count == 0 or 
         *         ! H5F_addr_defined(fspace->addr) do nothing as either 
         *         the free space manager data is not persistent, or the 
         *         section info is empty.
         *
         *         Otherwise, allocate space for the section info in real
         *         file space, insert the section info at this location, and 
         *         set fspace->sect_addr, fspace->sect_size, and 
         *         fspace->alloc_sect_size to reflect the new location
         *         of the section info.  Note that it is not necessary to
         *         force a write of the section info.
         *
         * Case 2) Allocate space for the section info in real file space,
         *         and tell the metadata cache to relocate the entry.  
         *         Update fspace->sect_addr, fspace->sect_size, and 
         *         fspace->alloc_sect_size to reflect the new location.
         *
         * Case 3) Nothing to be done in this case, although it is useful
         *         to perform sanity checks.
         *
         * Note that while we may alter the contents of the free space 
         * header in cases 1) and 2), there is no need to mark the header 
         * as dirty, as the metadata cache would not be attempting to 
         * serialize the header if it thought it was clean.
         */
        if(fspace->serial_sect_count > 0 && H5F_addr_defined(fspace->addr)) {
            /* Sanity check */
            HDassert(fspace->sect_size > 0);

            if(!H5F_addr_defined(fspace->sect_addr)) { /* case 1 */
                haddr_t tag = HADDR_UNDEF;
                haddr_t sect_addr;
                hsize_t saved_sect_size, new_sect_size;

                /* allocate file space for the section info, and insert it
                 * into the metadata cache.
                 */
                saved_sect_size = fspace->sect_size;
                if(HADDR_UNDEF == (sect_addr = H5MF_alloc((H5F_t *)f, H5FD_MEM_FSPACE_SINFO, fspace->sect_size)))
                    HGOTO_ERROR(H5E_FSPACE, H5E_NOSPACE, FAIL, "file allocation failed for free space sections")

                /* fspace->sect_size may change in size after H5MF_alloc().
                 * If increased in size, free the previous allocation and
                 * allocate again with the bigger fspace->sect_size.
                 */
                if(fspace->sect_size > saved_sect_size) {

                    new_sect_size = fspace->sect_size;

                    if(H5MF_xfree(f, H5FD_MEM_FSPACE_SINFO, sect_addr, saved_sect_size) < 0)
                        HGOTO_ERROR(H5E_FSPACE, H5E_CANTFREE, FAIL, "unable to free free space sections")

                    if(HADDR_UNDEF == (sect_addr = H5MF_alloc((H5F_t *)f, H5FD_MEM_FSPACE_SINFO, new_sect_size)))
                        HGOTO_ERROR(H5E_FSPACE, H5E_NOSPACE, FAIL, "file allocation failed for free space sections")
                    fspace->sect_size = new_sect_size;
                    fspace->alloc_sect_size = new_sect_size;
                } else {
                    fspace->alloc_sect_size = saved_sect_size;
                    fspace->sect_size = saved_sect_size;
                }
                fspace->sect_addr = sect_addr;

                /* Get the tag for this free space manager and use it to insert the entry */
                if(H5AC_get_tag((const void *)fspace, &tag) < 0)
                    HGOTO_ERROR(H5E_FSPACE, H5E_CANTTAG, FAIL, "can't get tag for metadata cache object")
                H5_BEGIN_TAG(tag)
                if(H5AC_insert_entry((H5F_t *)f, H5AC_FSPACE_SINFO, fspace->sect_addr, fspace->sinfo, H5AC__NO_FLAGS_SET) < 0)
                    HGOTO_ERROR_TAG(H5E_FSPACE, H5E_CANTINIT, FAIL, "can't add free space sections to cache")
                H5_END_TAG

                HDassert(fspace->sinfo->cache_info.size == fspace->alloc_sect_size);

                /* the metadata cache is now managing the section info, 
                 * so set fspace->sinfo to NULL.
                 */
                fspace->sinfo = NULL;
            } /* end if */
            else if(H5F_IS_TMP_ADDR(f, fspace->sect_addr)) { /* case 2 */
                haddr_t new_sect_addr;

                /* move the section info from temporary (AKA imaginary) file
                 * space to real file space.
                 */

                /* if my reading of the code is correct, this should always
                 * be the case.  If not, we will have to add code to resize
                 * file space allocation for section info as well as moving it.
                 */
                HDassert(fspace->sect_size > 0);
                HDassert(fspace->alloc_sect_size == (size_t)fspace->sect_size);

                /* Allocate space for the section info in file */
                if(HADDR_UNDEF == (new_sect_addr = H5MF_alloc((H5F_t *)f, H5FD_MEM_FSPACE_SINFO, fspace->sect_size)))
                    HGOTO_ERROR(H5E_FSPACE, H5E_NOSPACE, FAIL, "file allocation failed for free space sections")

                fspace->alloc_sect_size = (size_t)fspace->sect_size;
                HDassert(fspace->sinfo->cache_info.size == fspace->alloc_sect_size);

                /* Let the metadata cache know the section info moved */
                if(H5AC_move_entry((H5F_t *)f, H5AC_FSPACE_SINFO, fspace->sect_addr, new_sect_addr) < 0)
                    HGOTO_ERROR(H5E_HEAP, H5E_CANTMOVE, FAIL, "unable to move section info")

                fspace->sect_addr = new_sect_addr;
            } /* end else-if */
            else { /* case 3 -- nothing to do but sanity checking */
                /* if my reading of the code is correct, this should always
                 * be the case.  If not, we will have to add code to resize
                 * file space allocation for section info.
                 */
                HDassert(fspace->sect_size > 0);
                HDassert(fspace->alloc_sect_size == (size_t)fspace->sect_size);
            } /* end else */
        } /* end else */
        else {
            /* for one reason or another (see comment above) there should
             * not be any file space allocated for the section info.
             */
	    HDassert(!H5F_addr_defined(fspace->sect_addr));
        } /* end else */
    } /* end if */
    else if(H5F_addr_defined(fspace->sect_addr)) {
        /* Here the metadata cache is managing the section info.  
         *
         * Do some sanity checks, and then test to see if the section 
         * info is in real file space.  If it isn't relocate it into 
         * real file space lest the header be written to file with 
         * a nonsense section info address.
         */
        if(!H5F_POINT_OF_NO_RETURN(f)) {
            HDassert(fspace->sect_size > 0);
            HDassert(fspace->alloc_sect_size == (size_t)fspace->sect_size);
        } /* end if */

        if(H5F_IS_TMP_ADDR(f, fspace->sect_addr)) {
            unsigned sect_status = 0;
            haddr_t new_sect_addr;

            /* we have work to do -- must relocate section info into 
             * real file space.
             *
             * Since the section info address is in temporary space (AKA
             * imaginary space), it follows that the entry must be in 
             * cache.  Further, since fspace->sinfo is NULL, it must be 
             * unprotected and un-pinned.  Start by verifying this.
             */
            if(H5AC_get_entry_status(f, fspace->sect_addr, &sect_status) < 0)
                HGOTO_ERROR(H5E_FSPACE, H5E_CANTGET, FAIL, "can't get section info status")

            HDassert(sect_status & H5AC_ES__IN_CACHE);
            HDassert((sect_status & H5AC_ES__IS_PROTECTED) == 0);
            HDassert((sect_status & H5AC_ES__IS_PINNED) == 0);

            /* Allocate space for the section info in file */
            if(HADDR_UNDEF == (new_sect_addr = H5MF_alloc((H5F_t *)f, H5FD_MEM_FSPACE_SINFO, fspace->sect_size)))
                HGOTO_ERROR(H5E_FSPACE, H5E_NOSPACE, FAIL, "file allocation failed for free space sections")

            fspace->alloc_sect_size = (size_t)fspace->sect_size;

            /* Sanity check */
            HDassert(!H5F_addr_eq(fspace->sect_addr, new_sect_addr));

            /* Let the metadata cache know the section info moved */
            if(H5AC_move_entry((H5F_t *)f, H5AC_FSPACE_SINFO, fspace->sect_addr, new_sect_addr) < 0)
                HGOTO_ERROR(H5E_FSPACE, H5E_CANTMOVE, FAIL, "unable to move section info")

            /* Update the internal address for the section info */
            fspace->sect_addr = new_sect_addr;

            /* No need to mark the header dirty, as we are about to 
             * serialize it.
             */
        } /* end if */
    } /* end else-if */
    else {      /* there is no section info at present */
        /* do some sanity checks */
        HDassert(fspace->serial_sect_count == 0);
        HDassert(fspace->tot_sect_count == fspace->ghost_sect_count);
    } /* end else */

    /* what ever happened above, set *flags to 0 */
    *flags = 0;

done:
    /* Reset the ring in the API context */
    if(orig_ring != H5AC_RING_INV)
        H5AC_set_ring(orig_ring, NULL);

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5FS__cache_hdr_pre_serialize() */


/*-------------------------------------------------------------------------
 * Function:    H5FS__cache_hdr_serialize
 *
 * Purpose: 	Given an instance of H5FS_t and a suitably sized buffer,
 *      	serialize the contents of the instance of H5FS_t and write
 *      	its contents to the buffer.  This buffer will be used to 
 *      	write the image of the instance to file.
 *
 * Return:      Success:        SUCCEED
 *              Failure:        FAIL
 *
 * Programmer:  John Mainzer
 *              6/21/14
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5FS__cache_hdr_serialize(const H5F_t *f, void *_image, size_t H5_ATTR_NDEBUG_UNUSED len,
    void *_thing)
{
    H5FS_t     *fspace = (H5FS_t *)_thing;      /* Pointer to the object */
    uint8_t    *image = (uint8_t *)_image;      /* Pointer into raw data buffer */
    uint32_t    metadata_chksum;       /* Computed metadata checksum value */
    herr_t      ret_value = SUCCEED;   /* Return value */

    FUNC_ENTER_STATIC_NOERR

    /* Check arguments */
    HDassert(f);
    HDassert(image);
    HDassert(fspace);
    HDassert(fspace->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC);
    HDassert(fspace->cache_info.type == H5AC_FSPACE_HDR);
    HDassert(fspace->hdr_size == len);

    /* The section information does not always exits, and if it does, 
     * it is not always in the cache.  To make matters more interesting, 
     * even if it is in the cache, it may not be in real file space.
     *
     * The pre-serialize function should have moved the section info 
     * into real file space if necessary before this function was called.
     * The following asserts are a cursory check on this.
     */
    HDassert((! H5F_addr_defined(fspace->sect_addr)) || (! H5F_IS_TMP_ADDR(f, fspace->sect_addr)));

    if(!H5F_POINT_OF_NO_RETURN(f))
        HDassert((! H5F_addr_defined(fspace->sect_addr)) || 
                 ((fspace->sect_size > 0) && 
                 (fspace->alloc_sect_size == (size_t)fspace->sect_size)));

    /* Magic number */
    H5MM_memcpy(image, H5FS_HDR_MAGIC, (size_t)H5_SIZEOF_MAGIC);
    image += H5_SIZEOF_MAGIC;

    /* Version # */
    *image++ = H5FS_HDR_VERSION;

    /* Client ID */
    H5_CHECKED_ASSIGN(*image++, uint8_t, fspace->client, int);

    /* Total space tracked */
    H5F_ENCODE_LENGTH(f, image, fspace->tot_space);

    /* Total # of free space sections tracked */
    H5F_ENCODE_LENGTH(f, image, fspace->tot_sect_count);

    /* # of serializable free space sections tracked */
    H5F_ENCODE_LENGTH(f, image, fspace->serial_sect_count);

    /* # of ghost free space sections tracked */
    H5F_ENCODE_LENGTH(f, image, fspace->ghost_sect_count);

    /* # of section classes */
    UINT16ENCODE(image, fspace->nclasses);

    /* Shrink percent */
    UINT16ENCODE(image, fspace->shrink_percent);

    /* Expand percent */
    UINT16ENCODE(image, fspace->expand_percent);

    /* Size of address space free space sections are within (log2 of 
     * actual value) 
     */
    UINT16ENCODE(image, fspace->max_sect_addr);

    /* Max. size of section to track */
    H5F_ENCODE_LENGTH(f, image, fspace->max_sect_size);

    /* Address of serialized free space sections */
    H5F_addr_encode(f, &image, fspace->sect_addr);

    /* Size of serialized free space sections */
    H5F_ENCODE_LENGTH(f, image, fspace->sect_size);

    /* Allocated size of serialized free space sections */
    H5F_ENCODE_LENGTH(f, image, fspace->alloc_sect_size);

    /* Compute checksum */
    metadata_chksum = H5_checksum_metadata((uint8_t *)_image, (size_t)(image - (uint8_t *)_image), 0);

    /* Metadata checksum */
    UINT32ENCODE(image, metadata_chksum);

    /* sanity checks */
    HDassert((size_t)(image - (uint8_t *)_image) == fspace->hdr_size);

    FUNC_LEAVE_NOAPI(ret_value)
} /* H5FS__cache_hdr_serialize() */


/*-------------------------------------------------------------------------
 * Function:    H5FS__cache_hdr_notify
 *
 * Purpose:     Handle cache action notifications
 *
 * Return:      SUCCEED/FAIL
 *
 * Programmer:  Quincey Koziol
 *              koziol@lbl.gov
 *              January 3, 2017
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5FS__cache_hdr_notify(H5AC_notify_action_t action, void *_thing)
{
    H5FS_t *fspace = (H5FS_t *)_thing;  /* Pointer to the object */
    herr_t ret_value = SUCCEED;         /* Return value */

    FUNC_ENTER_NOAPI_NOINIT
    
    /* Sanity check */
    HDassert(fspace);

    /* Determine which action to take */
    switch(action) {
        case H5AC_NOTIFY_ACTION_AFTER_INSERT:
        case H5AC_NOTIFY_ACTION_AFTER_LOAD:
        case H5AC_NOTIFY_ACTION_AFTER_FLUSH:
            /* do nothing */
            break;

        case H5AC_NOTIFY_ACTION_ENTRY_DIRTIED:
            if(H5AC_unsettle_entry_ring(fspace) < 0)
                HGOTO_ERROR(H5E_FSPACE, H5E_CANTFLUSH, FAIL, "unable to mark FSM ring as unsettled")
            break;

        case H5AC_NOTIFY_ACTION_ENTRY_CLEANED:
        case H5AC_NOTIFY_ACTION_CHILD_DIRTIED:
        case H5AC_NOTIFY_ACTION_CHILD_CLEANED:
        case H5AC_NOTIFY_ACTION_CHILD_UNSERIALIZED:
        case H5AC_NOTIFY_ACTION_CHILD_SERIALIZED:
        case H5AC_NOTIFY_ACTION_BEFORE_EVICT:
            /* do nothing */
            break;

        default:
#ifdef NDEBUG
            HGOTO_ERROR(H5E_FSPACE, H5E_BADVALUE, FAIL, "unknown action from metadata cache")
#else /* NDEBUG */
            HDassert(0 && "Unknown action?!?");
#endif /* NDEBUG */
    } /* end switch */

done:
    FUNC_LEAVE_NOAPI(ret_value)
}   /* end H5FS__cache_hdr_notify() */


/*-------------------------------------------------------------------------
 * Function:	H5FS__cache_hdr_free_icr
 *
 * Purpose:	Destroys a free space header in memory.
 *
 * Note:	The metadata cache sets the object's cache_info.magic to
 *		H5C__H5C_CACHE_ENTRY_T_BAD_MAGIC before calling a free_icr
 *		callback (checked in assert).
 *
 * Return:	Success:        SUCCEED
 *              Failure:        FAIL
 *
 * Programmer:	Quincey Koziol
 *		koziol@ncsa.uiuc.edu
 *		May  2 2006
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5FS__cache_hdr_free_icr(void *_thing)
{
    H5FS_t *fspace = (H5FS_t *)_thing;  /* Pointer to the object */
    herr_t ret_value = SUCCEED;         /* Return value */

    FUNC_ENTER_STATIC

    /* Sanity checks */
    HDassert(fspace);
    HDassert(fspace->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_BAD_MAGIC);
    HDassert(fspace->cache_info.type == H5AC_FSPACE_HDR);

    /* We should not still be holding on to the free space section info */
    HDassert(!fspace->sinfo);

    /* Destroy free space header */
    if(H5FS__hdr_dest(fspace) < 0)
        HGOTO_ERROR(H5E_FSPACE, H5E_CANTFREE, FAIL, "unable to destroy free space header")

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5FS__cache_hdr_free_icr() */


/*-------------------------------------------------------------------------
 * Function:	H5FS__cache_sinfo_get_initial_load_size()
 *
 * Purpose:	Compute the size of the on disk image of the free space 
 *		manager section info, and place this value in *image_len.
 *
 * Return:	Success:	SUCCEED
 *		Failure:	FAIL
 *
 * Programmer:	John Mainzer
 *		7/7/14
 *
 *-------------------------------------------------------------------------
 */
static herr_t 
H5FS__cache_sinfo_get_initial_load_size(void  *_udata, size_t *image_len)
{
    const H5FS_t *fspace;        			/* free space manager */
    H5FS_sinfo_cache_ud_t *udata = (H5FS_sinfo_cache_ud_t *)_udata; /* User data for callback */

    FUNC_ENTER_STATIC_NOERR

    /* Sanity checks */
    HDassert(udata);
    fspace = udata->fspace;
    HDassert(fspace);
    HDassert(fspace->sect_size > 0);
    HDassert(image_len);

    /* Set the image length size */
    *image_len = (size_t)(fspace->sect_size);

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5FS__cache_sinfo_get_initial_load_size() */


/*-------------------------------------------------------------------------
 * Function:    H5FS__cache_sinfo_verify_chksum
 *
 * Purpose:     Verify the computed checksum of the data structure is the
 *              same as the stored chksum.
 *
 * Return:      Success:        TRUE/FALSE
 *              Failure:        Negative
 *
 * Programmer:	Vailin Choi; Aug 2015
 *
 *-------------------------------------------------------------------------
 */
htri_t
H5FS__cache_sinfo_verify_chksum(const void *_image, size_t len, void H5_ATTR_UNUSED *_udata)
{
    const uint8_t *image = (const uint8_t *)_image;       /* Pointer into raw data buffer */
    uint32_t stored_chksum;     /* Stored metadata checksum value */
    uint32_t computed_chksum;   /* Computed metadata checksum value */
    htri_t ret_value = TRUE;	/* Return value */

    FUNC_ENTER_PACKAGE_NOERR

    /* Check arguments */
    HDassert(image);

    /* Get stored and computed checksums */
    H5F_get_checksums(image, len, &stored_chksum, &computed_chksum);

    if(stored_chksum != computed_chksum)
        ret_value = FALSE;

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5FS__cache_sinfo_verify_chksum() */


/*-------------------------------------------------------------------------
 * Function:	H5FS__cache_sinfo_deserialize
 *
 * Purpose:	Given a buffer containing the on disk image of the free space
 *		manager section info, allocate an instance of H5FS_sinfo_t, load
 *		it with the data contained in the image, and return a pointer to
 *		the new instance.
 *
 * Return:	Success:	Pointer to in core representation
 *		Failure:	NULL
 *
 * Programmer:	John Mainzer
 *		7/7/14
 *
 *-------------------------------------------------------------------------
 */
static void *
H5FS__cache_sinfo_deserialize(const void *_image, size_t H5_ATTR_NDEBUG_UNUSED len, void *_udata,
    hbool_t H5_ATTR_NDEBUG_UNUSED *dirty)
{
    H5FS_sinfo_cache_ud_t  *udata = (H5FS_sinfo_cache_ud_t *)_udata;    /* User data for callback */
    H5FS_t                 *fspace;         /* free space manager */
    H5FS_sinfo_t           *sinfo = NULL;   /* Free space section info */
    haddr_t                 fs_addr;        /* Free space header address */
    size_t                  old_sect_size;  /* Old section size */
    const uint8_t          *image = (const uint8_t *)_image;    /* Pointer into raw data buffer */
    const uint8_t          *chksum_image;   /* Points to chksum location */
    uint32_t                stored_chksum;  /* Stored metadata checksum  */
    void *                  ret_value = NULL;   /* Return value */

    FUNC_ENTER_STATIC

    /* Sanity checks */
    HDassert(image);
    HDassert(udata);
    fspace = udata->fspace;
    HDassert(fspace);
    HDassert(fspace->sect_size == len);
    HDassert(dirty);

    /* Allocate a new free space section info */
    if(NULL == (sinfo = H5FS__sinfo_new(udata->f, fspace)))
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")

    /* initialize old_sect_size */
    H5_CHECKED_ASSIGN(old_sect_size, size_t, fspace->sect_size, hsize_t);

    /* Magic number */
    if(HDmemcmp(image, H5FS_SINFO_MAGIC, (size_t)H5_SIZEOF_MAGIC))
        HGOTO_ERROR(H5E_FSPACE, H5E_CANTLOAD, NULL, "wrong free space sections signature")
    image += H5_SIZEOF_MAGIC;

    /* Version */
    if(*image++ != H5FS_SINFO_VERSION)
        HGOTO_ERROR(H5E_FSPACE, H5E_CANTLOAD, NULL, "wrong free space sections version")

    /* Address of free space header for these sections */
    H5F_addr_decode(udata->f, &image, &fs_addr);
    if(H5F_addr_ne(fs_addr, fspace->addr))
        HGOTO_ERROR(H5E_FSPACE, H5E_CANTLOAD, NULL, "incorrect header address for free space sections")

    /* Check for any serialized sections */
    if(fspace->serial_sect_count > 0) {
        hsize_t old_tot_sect_count;                         /* Total section count from header */
        hsize_t H5_ATTR_NDEBUG_UNUSED old_serial_sect_count; /* Total serializable section count from header */
        hsize_t H5_ATTR_NDEBUG_UNUSED old_ghost_sect_count;  /* Total ghost section count from header */
        hsize_t H5_ATTR_NDEBUG_UNUSED old_tot_space;         /* Total space managed from header */
        unsigned sect_cnt_size;                             /* The size of the section size counts */

        /* Compute the size of the section counts */
        sect_cnt_size = H5VM_limit_enc_size((uint64_t)fspace->serial_sect_count);

        /* Reset the section count, the "add" routine will update it */
        old_tot_sect_count = fspace->tot_sect_count;
        old_serial_sect_count = fspace->serial_sect_count;
        old_ghost_sect_count = fspace->ghost_sect_count;
        old_tot_space = fspace->tot_space;
        fspace->tot_sect_count = 0;
        fspace->serial_sect_count = 0;
        fspace->ghost_sect_count = 0;
        fspace->tot_space = 0;

        /* Walk through the image, deserializing sections */
        do {
            hsize_t sect_size = 0;      /* Current section size */
            size_t node_count = 0;      /* # of sections of this size */
            size_t u;               /* Local index variable */

            /* The number of sections of this node's size */
            UINT64DECODE_VAR(image, node_count, sect_cnt_size);
            HDassert(node_count);

            /* The size of the sections for this node */
            UINT64DECODE_VAR(image, sect_size, sinfo->sect_len_size);
            HDassert(sect_size);

            /* Loop over nodes of this size */
            for(u = 0; u < node_count; u++) {
                H5FS_section_info_t *new_sect;  /* Section that was deserialized */
                haddr_t sect_addr = 0;  /* Address of free space section in the address space */
                unsigned sect_type;     /* Type of free space section */
                unsigned des_flags;     /* Flags from deserialize callback */

                /* The address of the section */
                UINT64DECODE_VAR(image, sect_addr, sinfo->sect_off_size);

                /* The type of this section */
                sect_type = *image++;

                /* Call 'deserialize' callback for this section */
                des_flags = 0;
                HDassert(fspace->sect_cls[sect_type].deserialize);
                if(NULL == (new_sect = (*fspace->sect_cls[sect_type].deserialize) (&fspace->sect_cls[sect_type], image, sect_addr, sect_size, &des_flags)))
                    HGOTO_ERROR(H5E_FSPACE, H5E_CANTDECODE, NULL, "can't deserialize section")

                /* Update offset in serialization image */
                image += fspace->sect_cls[sect_type].serial_size;

                /* Insert section in free space manager, unless requested not to */
                if(!(des_flags & H5FS_DESERIALIZE_NO_ADD))
                    if(H5FS_sect_add(udata->f, fspace, new_sect, H5FS_ADD_DESERIALIZING, udata) < 0)
                        HGOTO_ERROR(H5E_FSPACE, H5E_CANTINSERT, NULL, "can't add section to free space manager")
            } /* end for */

            if(fspace->tot_sect_count == old_tot_sect_count)
                break;

        } while(image < (((const uint8_t *)_image + old_sect_size) - H5FS_SIZEOF_CHKSUM));

        /* Sanity check */
        HDassert((size_t)(image - (const uint8_t *)_image) <= (old_sect_size - H5FS_SIZEOF_CHKSUM));
        HDassert(old_sect_size == fspace->sect_size);
        HDassert(old_tot_sect_count == fspace->tot_sect_count);
        HDassert(old_serial_sect_count == fspace->serial_sect_count);
        HDassert(old_ghost_sect_count == fspace->ghost_sect_count);
        HDassert(old_tot_space == fspace->tot_space);
    } /* end if */

    /* checksum verification already done in verify_chksum cb */

    /* There may be empty space between entries and chksum */
    chksum_image = (const uint8_t *)(_image) + old_sect_size - H5FS_SIZEOF_CHKSUM;
    /* Metadata checksum */
    UINT32DECODE(chksum_image, stored_chksum);

    /* Sanity check */
    HDassert((image == chksum_image) || 
             ((size_t)((image - (const uint8_t *)_image) + (chksum_image - image))  == old_sect_size));

    /* Set return value */
    ret_value = sinfo;

done:
    if(!ret_value && sinfo)
        if(H5FS__sinfo_dest(sinfo) < 0)
            HDONE_ERROR(H5E_FSPACE, H5E_CANTFREE, NULL, "unable to destroy free space info")

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5FS__cache_sinfo_deserialize() */


/*-------------------------------------------------------------------------
 * Function:    H5FS__cache_sinfo_image_len
 *
 * Purpose:     Compute the size of the data structure on disk and return
 *              it in *image_len.
 *
 * Return:      Non-negative on success/Negative on failure
 *
 * Programmer:  Quincey Koziol
 *              koziol@hdfgroup.org
 *              August 14, 2013
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5FS__cache_sinfo_image_len(const void *_thing, size_t *image_len)
{
    const H5FS_sinfo_t *sinfo = (const H5FS_sinfo_t *)_thing;   /* Pointer to the object */

    FUNC_ENTER_STATIC_NOERR

    /* Sanity checks */
    HDassert(sinfo);
    HDassert(sinfo->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC);
    HDassert(sinfo->cache_info.type == H5AC_FSPACE_SINFO);
    HDassert(sinfo->fspace);
    HDassert(sinfo->fspace->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC);
    HDassert(sinfo->fspace->cache_info.type == H5AC_FSPACE_HDR);
    HDassert(image_len);

    /* Set the image length size */
    H5_CHECKED_ASSIGN(*image_len, size_t, sinfo->fspace->alloc_sect_size, hsize_t);

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5FS__cache_sinfo_image_len() */


/*-------------------------------------------------------------------------
 * Function:	H5FS__cache_sinfo_pre_serialize
 *
 * Purpose:	The objective of this function is to test to see if file space 
 *		for the section info is located in temporary (AKA imaginary) file 
 *		space.  If it is, relocate file space for the section info to 
 *		regular file space.
 *
 * Return:	Success:	SUCCEED
 *		Failure:	FAIL
 *
 * Programmer:	John Mainzer
 *		7/7/14
 *
 *-------------------------------------------------------------------------
 */
static herr_t 
H5FS__cache_sinfo_pre_serialize(H5F_t *f, void *_thing, haddr_t addr,
    size_t H5_ATTR_NDEBUG_UNUSED len, haddr_t *new_addr, size_t H5_ATTR_NDEBUG_UNUSED *new_len, 
    unsigned *flags)
{
    H5FS_sinfo_t 	*sinfo = (H5FS_sinfo_t *)_thing;        /* Pointer to the object */
    H5FS_t 		*fspace;                /* Free space header */
    haddr_t             sinfo_addr;             /* Address for section info */
    herr_t              ret_value = SUCCEED;    /* Return value */

    FUNC_ENTER_STATIC

    /* Sanity checks */
    HDassert(f);
    HDassert(sinfo);
    HDassert(sinfo->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC);
    HDassert(sinfo->cache_info.type == H5AC_FSPACE_SINFO);
    fspace = sinfo->fspace;
    HDassert(fspace->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC);
    HDassert(fspace->cache_info.type == H5AC_FSPACE_HDR);
    HDassert(fspace->cache_info.is_pinned);
    HDassert(H5F_addr_defined(addr));
    HDassert(H5F_addr_eq(fspace->sect_addr, addr));
    HDassert(fspace->sect_size == len);
    HDassert(new_addr);
    HDassert(new_len);
    HDassert(flags);

    sinfo_addr = addr; /* this will change if we relocate the section data */

    /* Check for section info at temporary address */
    if(H5F_IS_TMP_ADDR(f, fspace->sect_addr)) {
        /* Sanity check */
        HDassert(fspace->sect_size > 0);
        HDassert(H5F_addr_eq(fspace->sect_addr, addr));

        /* Allocate space for the section info in file */
        if(HADDR_UNDEF == (sinfo_addr = H5MF_alloc((H5F_t *)f, H5FD_MEM_FSPACE_SINFO, fspace->sect_size)))
            HGOTO_ERROR(H5E_FSPACE, H5E_NOSPACE, FAIL, "file allocation failed for free space sections")

        fspace->alloc_sect_size = (size_t)fspace->sect_size;

        /* Sanity check */
        HDassert(!H5F_addr_eq(sinfo->fspace->sect_addr, sinfo_addr));

        /* Let the metadata cache know the section info moved */
        if(H5AC_move_entry((H5F_t *)f, H5AC_FSPACE_SINFO, sinfo->fspace->sect_addr, sinfo_addr) < 0)
            HGOTO_ERROR(H5E_FSPACE, H5E_CANTMOVE, FAIL, "unable to move section info")

        /* Update the internal address for the section info */
        sinfo->fspace->sect_addr = sinfo_addr;

        /* Mark free space header as dirty */
        if(H5AC_mark_entry_dirty(fspace) < 0)
            HGOTO_ERROR(H5E_FSPACE, H5E_CANTMARKDIRTY, FAIL, "unable to mark free space header as dirty")
    } /* end if */

    if(!H5F_addr_eq(addr, sinfo_addr)) {
        *new_addr = sinfo_addr;
        *flags = H5C__SERIALIZE_MOVED_FLAG;
    } /* end if */
    else
        *flags = 0;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5FS__cache_sinfo_pre_serialize() */


/*-------------------------------------------------------------------------
 * Function:	H5FS__cache_sinfo_serialize
 *
 * Purpose:	Given an instance of H5FS_sinfo_t and a suitably sized buffer,
 *		serialize the contents of the instance of H5FS_sinfo_t and write 
 *		its contents to the buffer.  This buffer will be used to write 
 *		the image of the instance to file.
 *
 * Return:	Success:	SUCCEED
 *		Failure:	FAIL
 *
 * Programmer:	John Mainzer
 *		6/21/14
 *
 *-------------------------------------------------------------------------
 */
static herr_t 
H5FS__cache_sinfo_serialize(const H5F_t *f, void *_image, size_t len,
    void *_thing)
{
    H5FS_sinfo_t        *sinfo = (H5FS_sinfo_t *)_thing;        /* Pointer to the object */
    H5FS_iter_ud_t      udata;              /* User data for callbacks */
    uint8_t             *image = (uint8_t *)_image;     /* Pointer into raw data buffer */
    uint8_t             *chksum_image = NULL;           /* Points to chksum location */
    uint32_t            metadata_chksum;    /* Computed metadata checksum value */
    unsigned            bin;                /* Current bin we are on */
    herr_t              ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_STATIC

    /* Sanity checks */
    HDassert(f);
    HDassert(image);
    HDassert(sinfo);
    HDassert(sinfo->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC);
    HDassert(sinfo->cache_info.type == H5AC_FSPACE_SINFO);
    HDassert(sinfo->fspace->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC);
    HDassert(sinfo->fspace->cache_info.type == H5AC_FSPACE_HDR);
    HDassert(sinfo->fspace->cache_info.is_pinned);
    HDassert(sinfo->fspace->sect_size == len);
    HDassert(sinfo->fspace->sect_cls);

    /* Magic number */
    H5MM_memcpy(image, H5FS_SINFO_MAGIC, (size_t)H5_SIZEOF_MAGIC);
    image += H5_SIZEOF_MAGIC;

    /* Version # */
    *image++ = H5FS_SINFO_VERSION;

    /* Address of free space header for these sections */
    H5F_addr_encode(f, &image, sinfo->fspace->addr);

    /* Set up user data for iterator */
    udata.sinfo = sinfo;
    udata.image = &image;
    udata.sect_cnt_size = H5VM_limit_enc_size((uint64_t)sinfo->fspace->serial_sect_count);

    /* Iterate over all the bins */
    for(bin = 0; bin < sinfo->nbins; bin++)
        /* Check if there are any sections in this bin */
        if(sinfo->bins[bin].bin_list)
            /* Iterate over list of section size nodes for bin */
            if(H5SL_iterate(sinfo->bins[bin].bin_list, H5FS__sinfo_serialize_node_cb, &udata) < 0)
                HGOTO_ERROR(H5E_FSPACE, H5E_BADITER, FAIL, "can't iterate over section size nodes")


    /* Compute checksum */

    /* There may be empty space between entries and chksum */
    chksum_image = (uint8_t *)(_image) + len - H5FS_SIZEOF_CHKSUM;
    metadata_chksum = H5_checksum_metadata(_image, (size_t)(chksum_image - (uint8_t *)_image), 0);
    /* Metadata checksum */
    UINT32ENCODE(chksum_image, metadata_chksum);

    /* Sanity check */
    HDassert((chksum_image == image) ||
             ((size_t)((image - (uint8_t *)_image) + (chksum_image - image)) == sinfo->fspace->sect_size));
    HDassert(sinfo->fspace->sect_size <= sinfo->fspace->alloc_sect_size);

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5FS__cache_sinfo_serialize() */


/*-------------------------------------------------------------------------
 * Function:    H5FS__cache_sinfo_notify
 *
 * Purpose:     Handle cache action notifications
 *
 * Return:      SUCCEED/FAIL
 *
 * Programmer:  Dana Robinson
 *              Fall 2012
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5FS__cache_sinfo_notify(H5AC_notify_action_t action, void *_thing)
{
    H5FS_sinfo_t *sinfo = (H5FS_sinfo_t *)_thing;
    herr_t ret_value = SUCCEED;         /* Return value */

    FUNC_ENTER_PACKAGE
    
    /* Sanity check */
    HDassert(sinfo);

    /* Check if the file was opened with SWMR-write access */
    if(sinfo->fspace->swmr_write) {
        /* Determine which action to take */
        switch(action) {
            case H5AC_NOTIFY_ACTION_AFTER_INSERT:
	    case H5AC_NOTIFY_ACTION_AFTER_LOAD:
                /* Create flush dependency on parent */
                if(H5FS__create_flush_depend((H5AC_info_t *)sinfo->fspace, (H5AC_info_t *)sinfo) < 0)
                    HGOTO_ERROR(H5E_FSPACE, H5E_CANTDEPEND, FAIL, "unable to create flush dependency between data block and header, address = %llu", (unsigned long long)sinfo->fspace->sect_addr)
                break;

	    case H5AC_NOTIFY_ACTION_AFTER_FLUSH:
            case H5AC_NOTIFY_ACTION_ENTRY_DIRTIED:
            case H5AC_NOTIFY_ACTION_ENTRY_CLEANED:
            case H5AC_NOTIFY_ACTION_CHILD_DIRTIED:
            case H5AC_NOTIFY_ACTION_CHILD_CLEANED:
            case H5AC_NOTIFY_ACTION_CHILD_UNSERIALIZED:
            case H5AC_NOTIFY_ACTION_CHILD_SERIALIZED:
                /* do nothing */
                break;

            case H5AC_NOTIFY_ACTION_BEFORE_EVICT:
		/* Destroy flush dependency on parent */
                if(H5FS__destroy_flush_depend((H5AC_info_t *)sinfo->fspace, (H5AC_info_t *)sinfo) < 0)
                    HGOTO_ERROR(H5E_FSPACE, H5E_CANTUNDEPEND, FAIL, "unable to destroy flush dependency")
                break;

            default:
#ifdef NDEBUG
                HGOTO_ERROR(H5E_FSPACE, H5E_BADVALUE, FAIL, "unknown action from metadata cache")
#else /* NDEBUG */
                HDassert(0 && "Unknown action?!?");
#endif /* NDEBUG */
        } /* end switch */
    } /* end if */

done:
    FUNC_LEAVE_NOAPI(ret_value)
}   /* end H5FS__cache_sinfo_notify() */


/*-------------------------------------------------------------------------
 * Function:	H5FS__cache_sinfo_free_icr
 *
 * Purpose:	Free the memory used for the in core representation of the 
 *		free space manager section info.
 *
 * Note:	The metadata cache sets the object's cache_info.magic to
 *		H5C__H5C_CACHE_ENTRY_T_BAD_MAGIC before calling a free_icr
 *		callback (checked in assert).
 *
 * Return:	Success:	SUCCEED
 *		Failure:	FAIL
 *
 * Programmer:	John Mainzer
 *		6/21/14
 *
 *-------------------------------------------------------------------------
 */
static herr_t 
H5FS__cache_sinfo_free_icr(void *_thing)
{
    H5FS_sinfo_t        *sinfo = (H5FS_sinfo_t *)_thing;    /* Pointer to the object */
    herr_t              ret_value = SUCCEED;    /* Return value */

    FUNC_ENTER_STATIC

    /* Sanity checks */
    HDassert(sinfo);
    HDassert(sinfo->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_BAD_MAGIC);
    HDassert(sinfo->cache_info.type == H5AC_FSPACE_SINFO);
    HDassert(sinfo->fspace->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC);
    HDassert(sinfo->fspace->cache_info.type == H5AC_FSPACE_HDR);
    HDassert(sinfo->fspace->cache_info.is_pinned);

    /* Destroy free space info */
    if(H5FS__sinfo_dest(sinfo) < 0)
        HGOTO_ERROR(H5E_FSPACE, H5E_CANTFREE, FAIL, "unable to destroy free space info")

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5FS__cache_sinfo_free_icr() */


/*-------------------------------------------------------------------------
 * Function:	H5FS__sinfo_serialize_sect_cb
 *
 * Purpose:	Skip list iterator callback to serialize free space sections
 *              of a particular size
 *
 * Return:      SUCCEED/FAIL
 *
 * Programmer:	Quincey Koziol
 *              Monday, May  8, 2006
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5FS__sinfo_serialize_sect_cb(void *_item, void H5_ATTR_UNUSED *key, void *_udata)
{
    H5FS_section_class_t *sect_cls;     /* Class of section */
    H5FS_section_info_t *sect= (H5FS_section_info_t *)_item;   /* Free space section to work on */
    H5FS_iter_ud_t *udata = (H5FS_iter_ud_t *)_udata; /* Callback info */
    herr_t ret_value = SUCCEED;         /* Return value */

    FUNC_ENTER_STATIC

    /* Check arguments. */
    HDassert(sect);
    HDassert(udata->sinfo);
    HDassert(udata->image);

    /* Get section's class */
    sect_cls = &udata->sinfo->fspace->sect_cls[sect->type];

    /* Check if this section should be serialized (i.e. is not a ghost section) */
    if(!(sect_cls->flags & H5FS_CLS_GHOST_OBJ)) {
        /* The address of the section */
        UINT64ENCODE_VAR(*udata->image, sect->addr, udata->sinfo->sect_off_size);

        /* The type of this section */
        *(*udata->image)++ = (uint8_t)sect->type;

        /* Call 'serialize' callback for this section */
        if(sect_cls->serialize) {
            if((*sect_cls->serialize)(sect_cls, sect, *udata->image) < 0)
                HGOTO_ERROR(H5E_FSPACE, H5E_CANTSERIALIZE, FAIL, "can't synchronize section")

            /* Update offset in serialization buffer */
            (*udata->image) += sect_cls->serial_size;
        } /* end if */
        else
            HDassert(sect_cls->serial_size == 0);
    } /* end if */

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* H5FS__sinfo_serialize_sect_cb() */


/*-------------------------------------------------------------------------
 * Function:	H5FS__sinfo_serialize_node_cb
 *
 * Purpose:	Skip list iterator callback to serialize free space sections
 *              in a bin
 *
 * Return:      SUCCEED/FAIL
 *
 * Programmer:	Quincey Koziol
 *              Monday, May  8, 2006
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5FS__sinfo_serialize_node_cb(void *_item, void H5_ATTR_UNUSED *key, void *_udata)
{
    H5FS_node_t *fspace_node = (H5FS_node_t *)_item;   /* Free space size node to work on */
    H5FS_iter_ud_t *udata = (H5FS_iter_ud_t *)_udata; /* Callback info */
    herr_t ret_value = SUCCEED;         /* Return value */

    FUNC_ENTER_STATIC

    /* Check arguments. */
    HDassert(fspace_node);
    HDassert(udata->sinfo);
    HDassert(udata->image);

    /* Check if this node has any serializable sections */
    if(fspace_node->serial_count > 0) {
        /* The number of serializable sections of this node's size */
        UINT64ENCODE_VAR(*udata->image, fspace_node->serial_count, udata->sect_cnt_size);

        /* The size of the sections for this node */
        UINT64ENCODE_VAR(*udata->image, fspace_node->sect_size, udata->sinfo->sect_len_size);

        /* Iterate through all the sections of this size */
        HDassert(fspace_node->sect_list);
        if(H5SL_iterate(fspace_node->sect_list, H5FS__sinfo_serialize_sect_cb, udata) < 0)
            HGOTO_ERROR(H5E_FSPACE, H5E_BADITER, FAIL, "can't iterate over section nodes")
    } /* end if */

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* H5FS__sinfo_serialize_node_cb() */

anity check */ HDassert((size_t)(image - (uint8_t *)_image) == len); done: FUNC_LEAVE_NOAPI(ret_value) } /* end H5HF__cache_hdr_serialize() */ /*------------------------------------------------------------------------- * Function: H5HF__cache_hdr_free_icr * * Purpose: Free the in core representation of the fractal heap header. * * This routine frees just the header itself, not the * associated version 2 B-Tree, the associated Free Space Manager, * nor the indirect/direct block tree that is rooted in the header. * * This routine also does not free the file space that may * be allocated to the header. * * Note: The metadata cache sets the object's cache_info.magic to * H5C__H5C_CACHE_ENTRY_T_BAD_MAGIC before calling a free_icr * callback (checked in assert). * * Return: Success: SUCCEED * Failure: FAIL * * Programmer: John Mainzer * 6/21/14 * *------------------------------------------------------------------------- */ static herr_t H5HF__cache_hdr_free_icr(void *_thing) { H5HF_hdr_t *hdr = (H5HF_hdr_t *)_thing; /* Fractal heap info */ herr_t ret_value = SUCCEED; /* Return value */ FUNC_ENTER_STATIC /* Sanity checks */ HDassert(hdr); HDassert(hdr->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_BAD_MAGIC); HDassert(hdr->cache_info.type == H5AC_FHEAP_HDR); HDassert(hdr->rc == 0); if(H5HF_hdr_free(hdr) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTRELEASE, FAIL, "unable to release fractal heap header") done: FUNC_LEAVE_NOAPI(ret_value) } /* end H5HF__cache_hdr_free_icr() */ /*------------------------------------------------------------------------- * Function: H5HF__cache_iblock_get_initial_load_size() * * Purpose: Compute the size of the on disk image of the indirect * block, and place this value in *image_len. * * Return: Success: SUCCEED * Failure: FAIL * * Programmer: John Mainzer * 6/21/14 * *------------------------------------------------------------------------- */ static herr_t H5HF__cache_iblock_get_initial_load_size(void *_udata, size_t *image_len) { H5HF_iblock_cache_ud_t *udata = (H5HF_iblock_cache_ud_t *)_udata; /* User data for callback */ FUNC_ENTER_STATIC_NOERR /* Sanity checks */ HDassert(udata); HDassert(udata->par_info); HDassert(udata->par_info->hdr); HDassert(image_len); /* Set the image length size */ *image_len = (size_t)H5HF_MAN_INDIRECT_SIZE(udata->par_info->hdr, *udata->nrows); FUNC_LEAVE_NOAPI(SUCCEED) } /* end H5HF__cache_iblock_get_initial_load_size() */ /*------------------------------------------------------------------------- * Function: H5HF__cache_iblock_verify_chksum * * Purpose: Verify the computed checksum of the data structure is the * same as the stored chksum. * * Return: Success: TRUE/FALSE * Failure: Negative * * Programmer: Vailin Choi; Aug 2015 * *------------------------------------------------------------------------- */ static htri_t H5HF__cache_iblock_verify_chksum(const void *_image, size_t len, void H5_ATTR_UNUSED *_udata) { const uint8_t *image = (const uint8_t *)_image; /* Pointer into raw data buffer */ uint32_t stored_chksum; /* Stored metadata checksum value */ uint32_t computed_chksum; /* Computed metadata checksum value */ htri_t ret_value = TRUE; /* Return value */ FUNC_ENTER_STATIC_NOERR /* Check arguments */ HDassert(image); /* Get stored and computed checksums */ H5F_get_checksums(image, len, &stored_chksum, &computed_chksum); if(stored_chksum != computed_chksum) ret_value = FALSE; FUNC_LEAVE_NOAPI(ret_value) } /* end H5HF__cache_iblock_verify_chksum() */ /*------------------------------------------------------------------------- * Function: H5HF__cache_iblock_deserialize * * Purpose: Given a buffer containing the on disk image of the indirect * block, allocate an instance of H5HF_indirect_t, load the data * in the buffer into this new instance, and return a pointer to * it. * * As best I can tell, the size of the indirect block image is fully * know before the image is loaded, so this function should succeed * unless the image is corrupt or memory allocation fails. * * Return: Success: Pointer to in core representation * Failure: NULL * * Programmer: John Mainzer * 6/21/14 * *------------------------------------------------------------------------- */ static void * H5HF__cache_iblock_deserialize(const void *_image, size_t len, void *_udata, hbool_t H5_ATTR_UNUSED *dirty) { H5HF_hdr_t *hdr; /* Shared fractal heap information */ H5HF_iblock_cache_ud_t *udata = (H5HF_iblock_cache_ud_t *)_udata; /* User data for callback */ H5HF_indirect_t *iblock = NULL; /* Indirect block info */ const uint8_t *image = (const uint8_t *)_image; /* Pointer into raw data buffer */ haddr_t heap_addr; /* Address of heap header in the file */ uint32_t stored_chksum; /* Stored metadata checksum value */ unsigned u; /* Local index variable */ void * ret_value = NULL; /* Return value */ FUNC_ENTER_STATIC /* Sanity checks */ HDassert(image); HDassert(udata); HDassert(dirty); hdr = udata->par_info->hdr; HDassert(hdr->f); /* Set the shared heap header's file context for this operation */ hdr->f = udata->f; /* Allocate space for the fractal heap indirect block */ if(NULL == (iblock = H5FL_CALLOC(H5HF_indirect_t))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed") /* Share common heap information */ iblock->hdr = hdr; if(H5HF_hdr_incr(hdr) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTINC, NULL, "can't increment reference count on shared heap header") /* Set block's internal information */ iblock->rc = 0; iblock->nrows = *udata->nrows; iblock->nchildren = 0; /* Compute size of indirect block */ iblock->size = H5HF_MAN_INDIRECT_SIZE(hdr, iblock->nrows); /* sanity check */ HDassert(iblock->size == len); /* Magic number */ if(HDmemcmp(image, H5HF_IBLOCK_MAGIC, (size_t)H5_SIZEOF_MAGIC)) HGOTO_ERROR(H5E_HEAP, H5E_BADVALUE, NULL, "wrong fractal heap indirect block signature") image += H5_SIZEOF_MAGIC; /* Version */ if(*image++ != H5HF_IBLOCK_VERSION) HGOTO_ERROR(H5E_HEAP, H5E_VERSION, NULL, "wrong fractal heap direct block version") /* Address of heap that owns this block */ H5F_addr_decode(udata->f, &image, &heap_addr); if(H5F_addr_ne(heap_addr, hdr->heap_addr)) HGOTO_ERROR(H5E_HEAP, H5E_CANTLOAD, NULL, "incorrect heap header address for direct block") /* Address of parent block */ iblock->parent = udata->par_info->iblock; /* this copy of the parent pointer is needed by the notify callback so */ /* that it can take down flush dependencies on eviction even if */ /* the parent pointer has been nulled out. JRM -- 5/18/14 */ iblock->fd_parent = udata->par_info->iblock; iblock->par_entry = udata->par_info->entry; if(iblock->parent) { /* Share parent block */ if(H5HF_iblock_incr(iblock->parent) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTINC, NULL, "can't increment reference count on shared indirect block") /* Set max. # of rows in this block */ iblock->max_rows = iblock->nrows; } /* end if */ else { /* Set max. # of rows in this block */ iblock->max_rows = hdr->man_dtable.max_root_rows; } /* end else */ /* Offset of heap within the heap's address space */ UINT64DECODE_VAR(image, iblock->block_off, hdr->heap_off_size); /* Allocate & decode child block entry tables */ HDassert(iblock->nrows > 0); if(NULL == (iblock->ents = H5FL_SEQ_MALLOC(H5HF_indirect_ent_t, (size_t)(iblock->nrows * hdr->man_dtable.cparam.width)))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed for direct entries") if(hdr->filter_len > 0) { unsigned dir_rows; /* Number of direct rows in this indirect block */ /* Compute the number of direct rows for this indirect block */ dir_rows = MIN(iblock->nrows, hdr->man_dtable.max_direct_rows); /* Allocate indirect block filtered entry array */ if(NULL == (iblock->filt_ents = H5FL_SEQ_MALLOC(H5HF_indirect_filt_ent_t, (size_t)(dir_rows * hdr->man_dtable.cparam.width)))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed for block entries") } /* end if */ else iblock->filt_ents = NULL; for(u = 0; u < (iblock->nrows * hdr->man_dtable.cparam.width); u++) { /* Decode child block address */ H5F_addr_decode(udata->f, &image, &(iblock->ents[u].addr)); /* Check for heap with I/O filters */ if(hdr->filter_len > 0) { /* Sanity check */ HDassert(iblock->filt_ents); /* Decode extra information for direct blocks */ if(u < (hdr->man_dtable.max_direct_rows * hdr->man_dtable.cparam.width)) { /* Size of filtered direct block */ H5F_DECODE_LENGTH(udata->f, image, iblock->filt_ents[u].size); /* Sanity check */ /* (either both the address & size are defined or both are * not defined) */ HDassert((H5F_addr_defined(iblock->ents[u].addr) && iblock->filt_ents[u].size) || (!H5F_addr_defined(iblock->ents[u].addr) && iblock->filt_ents[u].size == 0)); /* I/O filter mask for filtered direct block */ UINT32DECODE(image, iblock->filt_ents[u].filter_mask); } /* end if */ } /* end if */ /* Count child blocks */ if(H5F_addr_defined(iblock->ents[u].addr)) { iblock->nchildren++; iblock->max_child = u; } /* end if */ } /* end for */ /* Sanity check */ HDassert(iblock->nchildren); /* indirect blocks w/no children should have been deleted */ /* checksum verification already done by verify_chksum cb */ /* Metadata checksum */ UINT32DECODE(image, stored_chksum); /* Sanity check */ HDassert((size_t)(image - (const uint8_t *)_image) == iblock->size); /* Check if we have any indirect block children */ if(iblock->nrows > hdr->man_dtable.max_direct_rows) { unsigned indir_rows;/* Number of indirect rows in this indirect block */ /* Compute the number of indirect rows for this indirect block */ indir_rows = iblock->nrows - hdr->man_dtable.max_direct_rows; /* Allocate & initialize child indirect block pointer array */ if(NULL == (iblock->child_iblocks = H5FL_SEQ_CALLOC(H5HF_indirect_ptr_t, (size_t)(indir_rows * hdr->man_dtable.cparam.width)))) HGOTO_ERROR(H5E_HEAP, H5E_NOSPACE, NULL, "memory allocation failed for block entries") } /* end if */ else iblock->child_iblocks = NULL; /* Set return value */ ret_value = (void *)iblock; done: if(!ret_value && iblock) if(H5HF_man_iblock_dest(iblock) < 0) HDONE_ERROR(H5E_HEAP, H5E_CANTFREE, NULL, "unable to destroy fractal heap indirect block") FUNC_LEAVE_NOAPI(ret_value) } /* end H5HF__cache_iblock_deserialize() */ /*------------------------------------------------------------------------- * Function: H5HF__cache_iblock_image_len * * Purpose: Return the size of the on disk image of the iblock. * * Return: Success: SUCCEED * Failure: FAIL * * Programmer: John Mainzer * 6/21/14 * *------------------------------------------------------------------------- */ static herr_t H5HF__cache_iblock_image_len(const void *_thing, size_t *image_len) { const H5HF_indirect_t *iblock = (const H5HF_indirect_t *)_thing; /* Indirect block info */ FUNC_ENTER_STATIC_NOERR /* Sanity checks */ HDassert(iblock); HDassert(iblock->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC); HDassert(iblock->cache_info.type == H5AC_FHEAP_IBLOCK); HDassert(image_len); *image_len = iblock->size; FUNC_LEAVE_NOAPI(SUCCEED) } /* end H5HF__cache_iblock_image_len() */ /*------------------------------------------------------------------------- * Function: H5HF__cache_iblock_pre_serialize * * Purpose: The primary objective of this function is to determine if the * indirect block is currently allocated in temporary file space, * and if so, to move it to real file space before the entry is * serialized. * * In debug compiles, this function also verifies that all * immediate flush dependency children of this indirect block * are either clean or are not in cache. * * Return: Success: SUCCEED * Failure: FAIL * * Programmer: John Mainzer * 6/21/14 * *------------------------------------------------------------------------- */ static herr_t H5HF__cache_iblock_pre_serialize(H5F_t *f, hid_t dxpl_id, void *_thing, haddr_t addr, size_t H5_ATTR_UNUSED len, haddr_t *new_addr, size_t H5_ATTR_UNUSED *new_len, unsigned *flags) { H5HF_hdr_t *hdr; /* Shared fractal heap information */ H5HF_indirect_t *iblock = (H5HF_indirect_t *)_thing; /* Indirect block info */ herr_t ret_value = SUCCEED; /* Return value */ FUNC_ENTER_STATIC /* Sanity checks */ HDassert(f); HDassert(iblock); HDassert(iblock->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC); HDassert(iblock->cache_info.type == H5AC_FHEAP_IBLOCK); HDassert(iblock->cache_info.size == iblock->size); HDassert(H5F_addr_defined(addr)); HDassert(H5F_addr_eq(iblock->addr, addr)); HDassert(new_addr); HDassert(new_len); HDassert(flags); hdr = iblock->hdr; HDassert(hdr); HDassert(hdr->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC); HDassert(hdr->cache_info.type == H5AC_FHEAP_HDR); #ifndef NDEBUG { hbool_t descendants_clean = TRUE; hbool_t fd_children_clean = TRUE; unsigned iblock_status = 0; /* verify that flush dependencies are working correctly. Do this * by verifying that all immediate flush dependency children of this * iblock are clean. */ if(H5AC_get_entry_status(f, iblock->addr, &iblock_status) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTGET, FAIL, "can't get iblock status") /* since the current iblock is the guest of honor in a flush, we know * that it is locked into the cache for the duration of the call. Hence * there is no need to check to see if it is pinned or protected, or to * protect it if it is not. */ if(H5HF__cache_verify_iblock_descendants_clean((H5F_t *)f, dxpl_id, iblock->addr, iblock, &iblock_status, &fd_children_clean, &descendants_clean) < 0) HGOTO_ERROR(H5E_HEAP, H5E_SYSTEM, FAIL, "can't verify descendants clean.") HDassert(fd_children_clean); } #endif /* NDEBUG */ /* Check to see if we must re-allocate the iblock from temporary to * normal (AKA real) file space. */ if(H5F_IS_TMP_ADDR(f, addr)) { haddr_t iblock_addr; /* Allocate 'normal' space for the new indirect block on disk */ if(HADDR_UNDEF == (iblock_addr = H5MF_alloc((H5F_t *)f, H5FD_MEM_FHEAP_IBLOCK, dxpl_id, (hsize_t)iblock->size))) HGOTO_ERROR(H5E_HEAP, H5E_NOSPACE, FAIL, "file allocation failed for fractal heap indirect block") /* Sanity check */ HDassert(!H5F_addr_eq(iblock->addr, iblock_addr)); /* Let the metadata cache know the block moved */ if(H5AC_move_entry((H5F_t *)f, H5AC_FHEAP_IBLOCK, iblock->addr, iblock_addr, dxpl_id) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTMOVE, FAIL, "unable to move indirect block") /* Update the internal address for the block */ iblock->addr = iblock_addr; /* Check for root indirect block */ if(NULL == iblock->parent) { /* Update information about indirect block's location */ hdr->man_dtable.table_addr = iblock_addr; /* Mark that heap header was modified */ if(H5HF_hdr_dirty(hdr) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTDIRTY, FAIL, "can't mark heap header as dirty") } /* end if */ else { H5HF_indirect_t *par_iblock; /* Parent indirect block */ unsigned par_entry; /* Entry in parent indirect block */ /* Get parent information */ par_iblock = iblock->parent; par_entry = iblock->par_entry; /* Update information about indirect block's location */ par_iblock->ents[par_entry].addr = iblock_addr; /* Mark that parent was modified */ if(H5HF_iblock_dirty(par_iblock) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTDIRTY, FAIL, "can't mark heap header as dirty") } /* end if */ *new_addr = iblock_addr; *flags = H5AC__SERIALIZE_MOVED_FLAG; } /* end if */ else *flags = 0; done: FUNC_LEAVE_NOAPI(ret_value) } /* end H5HF__cache_iblock_pre_serialize() */ /*------------------------------------------------------------------------- * Function: H5HF__cache_iblock_serialize * * Purpose: Given a pointer to an iblock, and a pointer to a buffer of * the appropriate size, write the contents of the iblock to the * buffer in format appropriate for writing to disk. * * Return: Success: SUCCEED * Failure: FAIL * * Programmer: John Mainzer * 6/21/14 * *------------------------------------------------------------------------- */ static herr_t H5HF__cache_iblock_serialize(const H5F_t *f, void *_image, size_t len, void *_thing) { H5HF_hdr_t *hdr; /* Shared fractal heap information */ H5HF_indirect_t *iblock = (H5HF_indirect_t *)_thing; /* Indirect block info */ uint8_t *image = (uint8_t *)_image; /* Pointer into raw data buffer */ #ifndef NDEBUG unsigned nchildren = 0; /* Track # of children */ size_t max_child = 0; /* Track max. child entry used */ #endif /* NDEBUG */ uint32_t metadata_chksum; /* Computed metadata checksum value */ size_t u; /* Local index variable */ herr_t ret_value = SUCCEED; /* Return value */ FUNC_ENTER_STATIC_NOERR /* Sanity checks */ HDassert(f); HDassert(image); HDassert(iblock); HDassert(iblock->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC); HDassert(iblock->cache_info.type == H5AC_FHEAP_IBLOCK); HDassert(iblock->cache_info.size == iblock->size); HDassert(len == iblock->size); /* Indirect block must be in 'normal' file space */ HDassert(!H5F_IS_TMP_ADDR(f, iblock->addr)); HDassert(H5F_addr_eq(iblock->addr, iblock->cache_info.addr)); /* Get the pointer to the shared heap header */ hdr = iblock->hdr; /* Set the shared heap header's file context for this operation */ hdr->f = f; /* Magic number */ HDmemcpy(image, H5HF_IBLOCK_MAGIC, (size_t)H5_SIZEOF_MAGIC); image += H5_SIZEOF_MAGIC; /* Version # */ *image++ = H5HF_IBLOCK_VERSION; /* Address of heap header for heap which owns this block */ H5F_addr_encode(f, &image, hdr->heap_addr); /* Offset of block in heap */ UINT64ENCODE_VAR(image, iblock->block_off, hdr->heap_off_size); /* Encode indirect block-specific fields */ for(u = 0; u < (iblock->nrows * hdr->man_dtable.cparam.width); u++) { /* Encode child block address */ H5F_addr_encode(f, &image, iblock->ents[u].addr); /* Check for heap with I/O filters */ if(hdr->filter_len > 0) { /* Sanity check */ HDassert(iblock->filt_ents); /* Encode extra information for direct blocks */ if(u < (hdr->man_dtable.max_direct_rows * hdr->man_dtable.cparam.width)) { /* Sanity check */ /* (either both the address & size are defined or both are * not defined) */ HDassert((H5F_addr_defined(iblock->ents[u].addr) && iblock->filt_ents[u].size) || (!H5F_addr_defined(iblock->ents[u].addr) && iblock->filt_ents[u].size == 0)); /* Size of filtered direct block */ H5F_ENCODE_LENGTH(f, image, iblock->filt_ents[u].size); /* I/O filter mask for filtered direct block */ UINT32ENCODE(image, iblock->filt_ents[u].filter_mask); } /* end if */ } /* end if */ #ifndef NDEBUG /* Count child blocks */ if(H5F_addr_defined(iblock->ents[u].addr)) { nchildren++; if(u > max_child) max_child = u; } /* end if */ #endif /* NDEBUG */ } /* end for */ /* Compute checksum */ metadata_chksum = H5_checksum_metadata((uint8_t *)_image, (size_t)(image - (uint8_t *)_image), 0); /* Metadata checksum */ UINT32ENCODE(image, metadata_chksum); /* Sanity checks */ HDassert((size_t)(image - (uint8_t *)_image) == iblock->size); #ifndef NDEBUG HDassert(nchildren == iblock->nchildren); HDassert(max_child == iblock->max_child); #endif /* NDEBUG */ FUNC_LEAVE_NOAPI(ret_value) } /* end H5HF__cache_iblock_serialize() */ /*------------------------------------------------------------------------- * Function: H5HF__cache_iblock_notify * * Purpose: This function is used to create and destroy flush dependency * relationships between iblocks and their parents as indirect blocks * are loaded / inserted and evicted from the metadata cache. * * In general, the parent will be another iblock, but it may be the * header if the iblock in question is the root iblock. * * Return: Success: SUCCEED * Failure: FAIL * * Programmer: John Mainzer * 6/21/14 * *------------------------------------------------------------------------- */ static herr_t H5HF__cache_iblock_notify(H5AC_notify_action_t action, void *_thing) { H5HF_indirect_t *iblock = (H5HF_indirect_t *)_thing; /* Indirect block info */ herr_t ret_value = SUCCEED; /* Return value */ FUNC_ENTER_STATIC /* Sanity checks */ HDassert(iblock); HDassert(iblock->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC); HDassert(iblock->cache_info.type == H5AC_FHEAP_IBLOCK); HDassert(iblock->hdr); if(action == H5AC_NOTIFY_ACTION_BEFORE_EVICT) HDassert((iblock->parent == iblock->fd_parent) || ((NULL == iblock->parent) && (iblock->fd_parent))); /* further sanity checks */ if(iblock->parent == NULL) { /* pointer from hdr to root iblock will not be set up unless */ /* the fractal heap has already pinned the hdr. Do what */ /* sanity checking we can. */ if((iblock->block_off == 0) && (iblock->hdr->root_iblock_flags & H5HF_ROOT_IBLOCK_PINNED)) HDassert(iblock->hdr->root_iblock == iblock); } /* end if */ else { /* if this is a child iblock, verify that the pointers are */ /* either uninitialized or set up correctly. */ H5HF_indirect_t *par_iblock = iblock->parent; unsigned indir_idx; /* Index in parent's child iblock pointer array */ /* Sanity check */ HDassert(par_iblock->child_iblocks); HDassert(iblock->par_entry >= (iblock->hdr->man_dtable.max_direct_rows * iblock->hdr->man_dtable.cparam.width)); /* Compute index in parent's child iblock pointer array */ indir_idx = iblock->par_entry - (iblock->hdr->man_dtable.max_direct_rows * iblock->hdr->man_dtable.cparam.width); /* The pointer to iblock in the parent may not be set yet -- */ /* verify that it is either NULL, or that it has been set to */ /* iblock. */ HDassert((NULL == par_iblock->child_iblocks[indir_idx]) || (par_iblock->child_iblocks[indir_idx] == iblock)); } /* end else */ switch(action) { case H5AC_NOTIFY_ACTION_AFTER_INSERT: case H5AC_NOTIFY_ACTION_AFTER_LOAD: if(iblock->parent) { /* this is a child iblock */ /* create flush dependency with parent iblock */ if(H5AC_create_flush_dependency(iblock->parent, iblock) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTDEPEND, FAIL, "unable to create flush dependency") } /* end if */ else { /* this is the root iblock */ /* create flush dependency with header */ if(H5AC_create_flush_dependency(iblock->hdr, iblock) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTDEPEND, FAIL, "unable to create flush dependency") } /* end else */ break; case H5AC_NOTIFY_ACTION_AFTER_FLUSH: case H5AC_NOTIFY_ACTION_ENTRY_DIRTIED: case H5AC_NOTIFY_ACTION_ENTRY_CLEANED: case H5AC_NOTIFY_ACTION_CHILD_DIRTIED: case H5AC_NOTIFY_ACTION_CHILD_CLEANED: case H5AC_NOTIFY_ACTION_CHILD_UNSERIALIZED: case H5AC_NOTIFY_ACTION_CHILD_SERIALIZED: /* do nothing */ break; case H5AC_NOTIFY_ACTION_BEFORE_EVICT: if(iblock->fd_parent) { /* this is a child iblock */ /* destroy flush dependency with parent iblock */ if(H5AC_destroy_flush_dependency(iblock->fd_parent, iblock) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTUNDEPEND, FAIL, "unable to destroy flush dependency") } /* end if */ else { /* this is the root iblock */ /* destroy flush dependency with header */ if(H5AC_destroy_flush_dependency(iblock->hdr, iblock) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTUNDEPEND, FAIL, "unable to destroy flush dependency") } /* end else */ break; default: HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "unknown action from metadata cache") break; } /* end switch */ done: FUNC_LEAVE_NOAPI(ret_value) } /* end H5HF__cache_iblock_notify() */ /*------------------------------------------------------------------------- * Function: H5HF__cache_iblock_free_icr * * Purpose: Unlink the supplied instance of H5HF_indirect_t from the * fractal heap and free its memory. * * Note: The metadata cache sets the object's cache_info.magic to * H5C__H5C_CACHE_ENTRY_T_BAD_MAGIC before calling a free_icr * callback (checked in assert). * * Return: Success: SUCCEED * Failure: FAIL * * Programmer: John Mainzer * 6/21/14 * *------------------------------------------------------------------------- */ static herr_t H5HF__cache_iblock_free_icr(void *thing) { H5HF_indirect_t *iblock = (H5HF_indirect_t *)thing; /* Fractal heap indirect block to free */ herr_t ret_value = SUCCEED; /* Return value */ FUNC_ENTER_STATIC /* Sanity checks */ HDassert(iblock); HDassert(iblock->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_BAD_MAGIC); HDassert(iblock->cache_info.type == H5AC_FHEAP_IBLOCK); HDassert(iblock->rc == 0); HDassert(iblock->hdr); /* Destroy fractal heap indirect block */ if(H5HF_man_iblock_dest(iblock) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTFREE, FAIL, "unable to destroy fractal heap indirect block") done: FUNC_LEAVE_NOAPI(ret_value) } /* end H5HF__cache_iblock_free_icr() */ /*------------------------------------------------------------------------- * Function: H5HF__cache_dblock_get_initial_load_size() * * Purpose: Determine the size of the direct block on disk image, and * return it in *image_len. * * Return: Success: SUCCEED * Failure: FAIL * * Programmer: John Mainzer * 6/21/14 * *------------------------------------------------------------------------- */ static herr_t H5HF__cache_dblock_get_initial_load_size(void *_udata, size_t *image_len) { const H5HF_dblock_cache_ud_t *udata = (const H5HF_dblock_cache_ud_t *)_udata; /* User data for callback */ const H5HF_parent_t *par_info; /* Pointer to parent information */ const H5HF_hdr_t *hdr; /* Shared fractal heap information */ FUNC_ENTER_STATIC_NOERR /* Sanity checks */ HDassert(udata); HDassert(image_len); /* Convenience variables */ par_info = (const H5HF_parent_t *)(&(udata->par_info)); HDassert(par_info); hdr = par_info->hdr; HDassert(hdr); /* Check for I/O filters on this heap */ if(hdr->filter_len > 0) { /* Check for root direct block */ if(par_info->iblock == NULL) /* filtered root direct block */ *image_len = hdr->pline_root_direct_size; else /* filtered direct block */ *image_len = par_info->iblock->filt_ents[par_info->entry].size; } /* end if */ else *image_len = udata->dblock_size; FUNC_LEAVE_NOAPI(SUCCEED) } /* end H5HF__cache_dblock_get_initial_load_size() */ /*------------------------------------------------------------------------- * Function: H5HF__cache_dblock_verify_chksum * * Purpose: Verify the computed checksum of the data structure is the * same as the stored chksum. * * Return: Success: TRUE/FALSE * Failure: Negative * * Programmer: Vailin Choi; Aug 2015 * *------------------------------------------------------------------------- */ static htri_t H5HF__cache_dblock_verify_chksum(const void *_image, size_t len, void *_udata) { const uint8_t *image = (const uint8_t *)_image; /* Pointer into raw data buffer */ H5HF_dblock_cache_ud_t *udata = (H5HF_dblock_cache_ud_t *)_udata; /* User data for callback */ void *read_buf = NULL; /* Pointer to buffer to read in */ H5HF_hdr_t *hdr; /* Shared fractal heap information */ H5HF_parent_t *par_info; /* Pointer to parent information */ uint32_t stored_chksum; /* Stored metadata checksum value */ uint32_t computed_chksum; /* Computed metadata checksum value */ size_t chk_size; /* The size for validating checksum */ uint8_t *chk_p; /* Pointer to the area for validating checksum */ htri_t ret_value = TRUE; /* Return value */ FUNC_ENTER_STATIC /* Sanity checks */ HDassert(image); HDassert(udata); par_info = (H5HF_parent_t *)(&(udata->par_info)); HDassert(par_info); hdr = par_info->hdr; HDassert(hdr); /* Get out if data block is not checksummed */ if(!(hdr->checksum_dblocks)) HGOTO_DONE(TRUE); if(hdr->filter_len > 0) { size_t nbytes; /* Number of bytes used in buffer, after applying reverse filters */ unsigned filter_mask; /* Excluded filters for direct block */ H5Z_cb_t filter_cb = {NULL, NULL}; /* Filter callback structure */ /* Allocate buffer to perform I/O filtering on and copy image into * it. Must do this as H5Z_pipeline() may re-size the buffer * provided to it. */ if(NULL == (read_buf = H5MM_malloc(len))) HGOTO_ERROR(H5E_HEAP, H5E_NOSPACE, FAIL, "memory allocation failed for pipeline buffer") /* Set up parameters for filter pipeline */ nbytes = len; filter_mask = udata->filter_mask; HDmemcpy(read_buf, image, len); /* Push direct block data through I/O filter pipeline */ if(H5Z_pipeline(&(hdr->pline), H5Z_FLAG_REVERSE, &filter_mask, H5Z_ENABLE_EDC, filter_cb, &nbytes, &len, &read_buf) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTFILTER, FAIL, "output pipeline failed") /* Update info about direct block */ udata->decompressed = TRUE; len = nbytes; } /* end if */ else read_buf = (void *)image; /* Casting away const OK - QAK */ /* Decode checksum */ chk_size = (size_t)(H5HF_MAN_ABS_DIRECT_OVERHEAD(hdr) - H5HF_SIZEOF_CHKSUM); chk_p = (uint8_t *)read_buf + chk_size; /* Metadata checksum */ UINT32DECODE(chk_p, stored_chksum); chk_p -= H5HF_SIZEOF_CHKSUM; /* Reset checksum field, for computing the checksum */ /* (Casting away const OK - QAK) */ HDmemset(chk_p, 0, (size_t)H5HF_SIZEOF_CHKSUM); /* Compute checksum on entire direct block */ computed_chksum = H5_checksum_metadata(read_buf, len, 0); /* Restore the checksum */ UINT32ENCODE(chk_p, stored_chksum) /* Verify checksum */ if(stored_chksum != computed_chksum) HGOTO_DONE(FALSE); /* Save the decompressed data to be used later in deserialize callback */ if(hdr->filter_len > 0) { /* Sanity check */ HDassert(udata->decompressed); HDassert(len == udata->dblock_size); /* Allocate block buffer */ if(NULL == (udata->dblk = H5FL_BLK_MALLOC(direct_block, (size_t)len))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed") /* Copy un-filtered data into block's buffer */ HDmemcpy(udata->dblk, read_buf, len); } /* end if */ done: /* Release the read buffer */ if(read_buf && read_buf != image) H5MM_xfree(read_buf); FUNC_LEAVE_NOAPI(ret_value) } /* end H5HF__cache_dblock_verify_chksum() */ /*------------------------------------------------------------------------- * Function: H5HF__cache_dblock_deserialize * * Purpose: Given a buffer containing the on disk image of a direct * block, allocate an instance of H5HF_direct_t, load the data * in the buffer into this new instance, and return a pointer to * it. * * As best I can tell, the size of the direct block image is fully * know before the image is loaded, so this function should succeed * unless the image is corrupt or memory allocation fails. * * Return: Success: Pointer to in core representation * Failure: NULL * * Programmer: John Mainzer * 6/21/14 * *------------------------------------------------------------------------- */ static void * H5HF__cache_dblock_deserialize(const void *_image, size_t len, void *_udata, hbool_t H5_ATTR_UNUSED *dirty) { H5HF_hdr_t *hdr; /* Shared fractal heap information */ H5HF_dblock_cache_ud_t *udata = (H5HF_dblock_cache_ud_t *)_udata; /* User data for callback */ H5HF_parent_t *par_info; /* Pointer to parent information */ H5HF_direct_t *dblock = NULL; /* Direct block info */ const uint8_t *image = (const uint8_t *)_image;/* Pointer into raw data buffer */ void *read_buf = NULL; /* Pointer to buffer to decompress */ haddr_t heap_addr; /* Address of heap header in the file */ void * ret_value = NULL; /* Return value */ FUNC_ENTER_STATIC /* Sanity checks */ HDassert(image); HDassert(udata); par_info = (H5HF_parent_t *)(&(udata->par_info)); HDassert(par_info); hdr = par_info->hdr; HDassert(hdr); HDassert(hdr->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC); HDassert(hdr->cache_info.type == H5AC_FHEAP_HDR); HDassert(dirty); /* Allocate space for the fractal heap direct block */ if(NULL == (dblock = H5FL_CALLOC(H5HF_direct_t))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed") HDmemset(&dblock->cache_info, 0, sizeof(H5AC_info_t)); /* Set the shared heap header's file context for this operation */ hdr->f = udata->f; /* Share common heap information */ dblock->hdr = hdr; if(H5HF_hdr_incr(hdr) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTINC, NULL, "can't increment reference count on shared heap header") /* Set block's internal information */ dblock->size = udata->dblock_size; /* Check for I/O filters on this heap */ if(hdr->filter_len > 0) { /* Direct block is already decompressed in verify_chksum callback */ if(udata->decompressed) { /* Sanity check */ HDassert(udata->dblk); /* Take ownership of the decompressed direct block */ dblock->blk = udata->dblk; udata->dblk = NULL; } /* end if */ else { H5Z_cb_t filter_cb = {NULL, NULL}; /* Filter callback structure */ size_t nbytes; /* Number of bytes used in buffer, after applying reverse filters */ unsigned filter_mask; /* Excluded filters for direct block */ /* Sanity check */ HDassert(udata->dblk == NULL); /* Allocate buffer to perform I/O filtering on and copy image into * it. Must do this as H5Z_pipeline() may resize the buffer * provided to it. */ if(NULL == (read_buf = H5MM_malloc(len))) HGOTO_ERROR(H5E_HEAP, H5E_NOSPACE, NULL, "memory allocation failed for pipeline buffer") /* Copy compressed image into buffer */ HDmemcpy(read_buf, image, len); /* Push direct block data through I/O filter pipeline */ nbytes = len; filter_mask = udata->filter_mask; if(H5Z_pipeline(&(hdr->pline), H5Z_FLAG_REVERSE, &filter_mask, H5Z_ENABLE_EDC, filter_cb, &nbytes, &len, &read_buf) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTFILTER, NULL, "output pipeline failed") /* Sanity check */ HDassert(nbytes == dblock->size); /* Copy un-filtered data into block's buffer */ HDmemcpy(dblock->blk, read_buf, dblock->size); } /* end if */ } /* end if */ else { /* Sanity checks */ HDassert(udata->dblk == NULL); HDassert(!udata->decompressed); /* Allocate block buffer */ /* XXX: Change to using free-list factories */ if(NULL == (dblock->blk = H5FL_BLK_MALLOC(direct_block, (size_t)dblock->size))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed") /* Copy image to dblock->blk */ HDassert(dblock->size == len); HDmemcpy(dblock->blk, image, dblock->size); } /* end else */ /* Start decoding direct block */ image = dblock->blk; /* Magic number */ if(HDmemcmp(image, H5HF_DBLOCK_MAGIC, (size_t)H5_SIZEOF_MAGIC)) HGOTO_ERROR(H5E_HEAP, H5E_BADVALUE, NULL, "wrong fractal heap direct block signature") image += H5_SIZEOF_MAGIC; /* Version */ if(*image++ != H5HF_DBLOCK_VERSION) HGOTO_ERROR(H5E_HEAP, H5E_VERSION, NULL, "wrong fractal heap direct block version") /* Address of heap that owns this block (just for file integrity checks) */ H5F_addr_decode(udata->f, &image, &heap_addr); if(H5F_addr_ne(heap_addr, hdr->heap_addr)) HGOTO_ERROR(H5E_HEAP, H5E_CANTLOAD, NULL, "incorrect heap header address for direct block") /* Address of parent block */ dblock->parent = par_info->iblock; dblock->fd_parent = par_info->iblock; dblock->par_entry = par_info->entry; if(dblock->parent) { /* Share parent block */ if(H5HF_iblock_incr(dblock->parent) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTINC, NULL, "can't increment reference count on shared indirect block") } /* end if */ /* Offset of heap within the heap's address space */ UINT64DECODE_VAR(image, dblock->block_off, hdr->heap_off_size); /* Decode checksum on direct block, if requested */ if(hdr->checksum_dblocks) { uint32_t stored_chksum; /* Metadata checksum value */ /* checksum verification already done in verify_chksum cb */ /* Metadata checksum */ UINT32DECODE(image, stored_chksum); } /* end if */ /* Sanity check */ HDassert((size_t)(image - dblock->blk) == (size_t)H5HF_MAN_ABS_DIRECT_OVERHEAD(hdr)); /* Set return value */ ret_value = (void *)dblock; done: /* Release the read buffer */ if(read_buf) H5MM_xfree(read_buf); /* Cleanup on error */ if(!ret_value && dblock) if(H5HF_man_dblock_dest(dblock) < 0) HDONE_ERROR(H5E_HEAP, H5E_CANTFREE, NULL, "unable to destroy fractal heap direct block") FUNC_LEAVE_NOAPI(ret_value) } /* end H5HF__cache_dblock_deserialize() */ /*------------------------------------------------------------------------- * Function: H5HF__cache_dblock_image_len * * Purpose: Report the actual size of the direct block image on disk. * Note that this value will probably be incorrect if compression * is enabled and the entry is dirty. * * Return: Success: SUCCEED * Failure: FAIL * * Programmer: John Mainzer * 6/21/14 * *------------------------------------------------------------------------- */ static herr_t H5HF__cache_dblock_image_len(const void *_thing, size_t *image_len) { const H5HF_direct_t *dblock = (const H5HF_direct_t *)_thing; /* Direct block info */ const H5HF_hdr_t *hdr; /* Shared fractal heap information */ size_t size; FUNC_ENTER_STATIC_NOERR /* Sanity checks */ HDassert(dblock); HDassert(dblock->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC); HDassert(dblock->cache_info.type == H5AC_FHEAP_DBLOCK); HDassert(image_len); /* Set up convenience variables */ hdr = dblock->hdr; HDassert(hdr); /* Check for I/O filters on this heap */ if(hdr->filter_len > 0) { /* * If the data is available, set to the compressed * size of the direct block -- otherwise set it equal to the * uncompressed size. * * We have three possible scenarios here. * * First, the block may never have been flushed. In this * case, both dblock->file_size and the size stored in the * parent (either the header or the parent iblock) will all * be zero. In this case, return the uncompressed size * stored in dblock->size as the size. * * Second, the block may have just been serialized, in which * case, dblock->file_size should be zero, and the correct * on disk size should be stored in the parent (again, either * the header or the parent iblock as case may be). * * Third, we may be in the process of discarding this * dblock without writing it. In this case, dblock->file_size * should be non-zero and have the correct size. Note that * in this case, the direct block will have been detached, * and thus looking up the parent will likely return incorrect * data. */ if(dblock->file_size != 0) size = dblock->file_size; else { const H5HF_indirect_t *par_iblock = dblock->parent; /* Parent iblock */ if(par_iblock) size = par_iblock->filt_ents[dblock->par_entry].size; else size = hdr->pline_root_direct_size; if(size == 0) size = dblock->size; } /* end else */ } /* end if */ else size = dblock->size; /* Set the image size */ HDassert(size > 0); *image_len = size; FUNC_LEAVE_NOAPI(SUCCEED) } /* end H5HF__cache_dblock_image_len() */ /*------------------------------------------------------------------------- * Function: H5HF__cache_dblock_pre_serialize * * Purpose: In principle, the purpose of this function is to determine * the size and location of the disk image of the target direct * block. In this case, the uncompressed size of the block is * fixed, but since the direct block could be compressed, * we may need to compute and report the compressed size. * * This is a bit sticky in the case of a direct block when I/O * filters are enabled, as the size of the compressed version * of the on disk image is not known until the direct block has * been run through the filters. Further, the location of the * on disk image may change if the compressed size of the image * changes as well. * * To complicate matters further, the direct block may have been * initially allocated in temporary (AKA imaginary) file space. * In this case, we must relocate the direct block's on-disk * image to "real" file space regardless of whether it has changed * size. * * One simplifying factor is the direct block's "blk" field, * which contains a pointer to a buffer which (with the exception * of a small header) contains the on disk image in uncompressed * form. * * To square this particular circle, this function does * everything the serialize function usually does, with the * exception of copying the image into the image buffer provided * to the serialize function by the metadata cache. The data to * copy is provided to the serialize function in a buffer pointed * to by the write_buf field. * * If I/O filters are enabled, on exit, * H5HF__cache_dblock_pre_serialize() sets the write_buf field to * point to a buffer containing the filtered image of the direct * block. The serialize function should free this block, and set * the write_buf field to NULL after copying it into the image * buffer provided by the metadata cache. * * If I/O filters are not enabled, this function prepares * the buffer pointed to by the blk field for copying to the * image buffer provided by the metadata cache, and sets the * write_buf field equal to the blk field. In this case, the * serialize function should simply set the write_buf field to * NULL after copying the direct block image into the image * buffer. * * In both of the above cases, the length of the buffer pointed * to by write_buf is provided in the write_len field. This * field must contain 0 on entry to this function, and should * be set back to 0 at the end of the serialize function. * * Return: Success: SUCCEED * Failure: FAIL * * Programmer: John Mainzer * 6/21/14 * *------------------------------------------------------------------------- */ static herr_t H5HF__cache_dblock_pre_serialize(H5F_t *f, hid_t dxpl_id, void *_thing, haddr_t addr, size_t len, haddr_t *new_addr, size_t *new_len, unsigned *flags) { hbool_t at_tmp_addr; /* Flag to indicate direct block is */ /* at temporary address */ haddr_t dblock_addr; H5HF_hdr_t *hdr; /* Shared fractal heap information */ H5HF_direct_t *dblock = (H5HF_direct_t *)_thing; /* Direct block info */ H5HF_indirect_t *par_iblock; /* Parent indirect block */ unsigned par_entry = 0; /* Entry in parent indirect block */ void *write_buf; /* Pointer to buffer to write out */ size_t write_size; /* Size of buffer to write out */ uint8_t *image; /* Pointer into raw data buffer */ unsigned dblock_flags = 0; herr_t ret_value = SUCCEED; /* Return value */ FUNC_ENTER_STATIC /* Sanity checks */ HDassert(f); HDassert(dblock); HDassert(dblock->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC); HDassert(dblock->cache_info.type == H5AC_FHEAP_DBLOCK); HDassert(dblock->write_buf == NULL); HDassert(dblock->write_size == 0); HDassert(dblock->cache_info.size == len); HDassert(H5F_addr_defined(addr)); HDassert(new_addr); HDassert(new_len); HDassert(flags); /* Set up local variables */ hdr = dblock->hdr; dblock_addr = addr; /* will update dblock_addr if we move the block */ /* Set the shared heap header's file context for this operation */ hdr->f = (H5F_t *)f; HDassert(hdr); HDassert(hdr->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC); HDassert(hdr->cache_info.type == H5AC_FHEAP_HDR); if(dblock->parent) { /* this is the common case, in which the direct block is the child * of an indirect block. Set up the convenience variables we will * need if the address and/or compressed size of the on disk image * of the direct block changes, and do some sanity checking in * passing. */ par_iblock = dblock->parent; par_entry = dblock->par_entry; HDassert(par_iblock->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC); HDassert(par_iblock->cache_info.type == H5AC_FHEAP_IBLOCK); HDassert(H5F_addr_eq(par_iblock->ents[par_entry].addr, addr)); } /* end if */ else { /* the direct block is a root direct block -- just set par_iblock * to NULL, as the field will not be used. */ par_iblock = NULL; } /* end else */ at_tmp_addr = H5F_IS_TMP_ADDR(f, addr); /* Begin by preping the direct block to be written to disk. Do * this by writing the correct magic number, the dblock version, * the address of the header, the offset of the block in the heap, * and the checksum at the beginning of the block. */ HDassert(dblock->blk); image = dblock->blk; /* Magic number */ HDmemcpy(image, H5HF_DBLOCK_MAGIC, (size_t)H5_SIZEOF_MAGIC); image += H5_SIZEOF_MAGIC; /* Version # */ *image++ = H5HF_DBLOCK_VERSION; /* Address of heap header for heap which owns this block */ H5F_addr_encode(f, &image, hdr->heap_addr); /* Offset of block in heap */ UINT64ENCODE_VAR(image, dblock->block_off, hdr->heap_off_size); /* Metadata checksum */ if(hdr->checksum_dblocks) { uint32_t metadata_chksum; /* Computed metadata checksum value */ /* Clear the checksum field, to compute the checksum */ HDmemset(image, 0, (size_t)H5HF_SIZEOF_CHKSUM); /* Compute checksum on entire direct block */ metadata_chksum = H5_checksum_metadata(dblock->blk, dblock->size, 0); /* Metadata checksum */ UINT32ENCODE(image, metadata_chksum); } /* end if */ /* at this point, dblock->blk should point to an uncompressed image of * the direct block. If I/O filters are not enabled, this image should * be ready to hand off to the metadata cache. */ /* Sanity check */ HDassert((size_t)(image - dblock->blk) == (size_t)H5HF_MAN_ABS_DIRECT_OVERHEAD(hdr)); /* If I/O filters are enabled on this heap, we must run the direct block * image through the filters to obtain the image that we will hand off * to the metadata cache. */ /* Check for I/O filters on this heap */ if(hdr->filter_len > 0) { H5Z_cb_t filter_cb = {NULL, NULL}; /* Filter callback structure */ size_t nbytes; /* Number of bytes used */ unsigned filter_mask = 0; /* Filter mask for block */ /* Allocate buffer to perform I/O filtering on */ write_size = dblock->size; if(NULL == (write_buf = H5MM_malloc(write_size))) HGOTO_ERROR(H5E_HEAP, H5E_NOSPACE, FAIL, "memory allocation failed for pipeline buffer") /* Copy the direct block's image into the buffer to compress */ HDmemcpy(write_buf, dblock->blk, write_size); /* Push direct block data through I/O filter pipeline */ nbytes = write_size; if(H5Z_pipeline(&(hdr->pline), 0, &filter_mask, H5Z_ENABLE_EDC, filter_cb, &nbytes, &write_size, &write_buf) < 0) HGOTO_ERROR(H5E_HEAP, H5E_WRITEERROR, FAIL, "output pipeline failed") /* Use the compressed number of bytes as the size to write */ write_size = nbytes; /* If the size and/or location of the on disk image of the * direct block changes, we must touch up its parent to reflect * these changes. Do this differently depending on whether the * direct block's parent is an indirect block or (rarely) the * fractal heap header. In this case, the direct block is known * as a root direct block. */ /* Check for root direct block */ if(dblock->parent == NULL) { hbool_t hdr_changed = FALSE; /* Whether the header info changed */ /* Sanity check */ HDassert(H5F_addr_eq(hdr->man_dtable.table_addr, addr)); HDassert(hdr->pline_root_direct_size > 0); /* Check if the filter mask changed */ if(hdr->pline_root_direct_filter_mask != filter_mask) { hdr->pline_root_direct_filter_mask = filter_mask; hdr_changed = TRUE; } /* end if */ /* verify that the cache's last record of the compressed * size matches the heap's last record. This value will * likely change shortly. */ HDassert(len == hdr->pline_root_direct_size); /* Check if we need to re-size the block on disk */ if(hdr->pline_root_direct_size != write_size || at_tmp_addr) { /* Check if the direct block is NOT currently allocated * in temp. file space * * (temp. file space does not need to be freed) */ if(!at_tmp_addr) /* Release direct block's current disk space */ if(H5MF_xfree(f, H5FD_MEM_FHEAP_DBLOCK, dxpl_id, addr, (hsize_t)hdr->pline_root_direct_size) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTFREE, FAIL, "unable to free fractal heap direct block") /* Allocate space for the compressed direct block */ if(HADDR_UNDEF == (dblock_addr = H5MF_alloc((H5F_t *)f, H5FD_MEM_FHEAP_DBLOCK, dxpl_id, (hsize_t)write_size))) HGOTO_ERROR(H5E_HEAP, H5E_NOSPACE, FAIL, "file allocation failed for fractal heap direct block") /* Update information about compressed direct block's * location & size */ HDassert(hdr->man_dtable.table_addr == addr); HDassert(hdr->pline_root_direct_size == len); hdr->man_dtable.table_addr = dblock_addr; hdr->pline_root_direct_size = write_size; /* Note that heap header was modified */ hdr_changed = TRUE; } /* end if */ /* Check if heap header was modified */ if(hdr_changed) if(H5HF_hdr_dirty(hdr) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTDIRTY, FAIL, "can't mark heap header as dirty") } /* end if */ else { /* the direct block's parent is an indirect block */ hbool_t par_changed = FALSE; /* Whether the parent's infochanged */ /* Sanity check */ HDassert(par_iblock); HDassert(par_iblock->filt_ents[par_entry].size > 0); /* Check if the filter mask changed */ if(par_iblock->filt_ents[par_entry].filter_mask != filter_mask) { par_iblock->filt_ents[par_entry].filter_mask = filter_mask; par_changed = TRUE; } /* end if */ /* verify that the cache's last record of the compressed * size matches the heap's last record. This value will * likely change shortly. */ HDassert(len == par_iblock->filt_ents[par_entry].size); /* Check if we need to re-size the block on disk */ if(par_iblock->filt_ents[par_entry].size != write_size || at_tmp_addr) { /* Check if the direct block is NOT currently allocated * in temp. file space * * (temp. file space does not need to be freed) */ if(!at_tmp_addr) /* Release direct block's current disk space */ if(H5MF_xfree(f, H5FD_MEM_FHEAP_DBLOCK, dxpl_id, addr, (hsize_t)par_iblock->filt_ents[par_entry].size) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTFREE, FAIL, "unable to free fractal heap direct block") /* Allocate space for the compressed direct block */ if(HADDR_UNDEF == (dblock_addr = H5MF_alloc((H5F_t *)f, H5FD_MEM_FHEAP_DBLOCK, dxpl_id, (hsize_t)write_size))) HGOTO_ERROR(H5E_HEAP, H5E_NOSPACE, FAIL, "file allocation failed for fractal heap direct block") /* Update information about compressed direct block's * location & size */ HDassert(par_iblock->ents[par_entry].addr == addr); HDassert(par_iblock->filt_ents[par_entry].size == len); par_iblock->ents[par_entry].addr = dblock_addr; par_iblock->filt_ents[par_entry].size = write_size; /* Note that parent was modified */ par_changed = TRUE; } /* end if */ /* Check if parent was modified */ if(par_changed) if(H5HF_iblock_dirty(par_iblock) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTDIRTY, FAIL, "can't mark heap header as dirty") } /* end else */ } /* end if */ else { /* I/O filters are not enabled -- thus all we need to do is check to * see if the direct block is in temporary (AKA imaginary) file * space, and move it to real file space if it is. * * As in the I/O filters case above, we will have to touch up the * direct blocks parent if the direct block is relocated. * * Recall that temporary file space need not be freed, which * simplifies matters slightly. */ write_buf = dblock->blk; write_size = dblock->size; /* Check to see if we must re-allocate direct block from 'temp.' * to 'normal' file space */ if(at_tmp_addr) { /* Allocate 'normal' space for the direct block */ if(HADDR_UNDEF == (dblock_addr = H5MF_alloc((H5F_t *)f, H5FD_MEM_FHEAP_DBLOCK, dxpl_id, (hsize_t)write_size))) HGOTO_ERROR(H5E_HEAP, H5E_NOSPACE, FAIL, "file allocation failed for fractal heap direct block") /* Check for root direct block */ if(NULL == dblock->parent) { /* Sanity checks */ HDassert(H5F_addr_eq(hdr->man_dtable.table_addr, addr)); HDassert(!H5F_addr_eq(hdr->man_dtable.table_addr, dblock_addr)); /* Update information about direct block's location */ hdr->man_dtable.table_addr = dblock_addr; /* Mark that heap header was modified */ if(H5HF_hdr_dirty(hdr) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTDIRTY, FAIL, "can't mark heap header as dirty") } /* end if */ else { /* the direct block's parent is an indirect block */ /* Sanity checks */ HDassert(par_iblock); HDassert(par_iblock->ents); HDassert(H5F_addr_eq(par_iblock->ents[par_entry].addr, addr)); HDassert(!H5F_addr_eq(par_iblock->ents[par_entry].addr, dblock_addr)); /* Update information about direct block's location */ par_iblock->ents[par_entry].addr = dblock_addr; /* Mark that parent was modified */ if(H5HF_iblock_dirty(par_iblock) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTDIRTY, FAIL, "can't mark heap header as dirty") } /* end else */ } /* end if */ } /* end else */ /* At this point, write_buf points to a buffer containing the image * of the direct block that is ready to copy into the image buffer, * and write_size contains the length of this buffer. * * Also, if image size or address has changed, the direct block's * parent has been modified to reflect the change. * * Now, make note of the pointer and length of the above buffer for * use by the serialize function. */ dblock->write_buf = (uint8_t *)write_buf; dblock->write_size = write_size; /* finally, pass data back to the metadata cache as appropriate */ if(!H5F_addr_eq(addr, dblock_addr)) { dblock_flags |= H5AC__SERIALIZE_MOVED_FLAG; *new_addr = dblock_addr; } /* end if */ if((hdr->filter_len > 0) && (len != write_size)) { dblock_flags |= H5AC__SERIALIZE_RESIZED_FLAG; *new_len = write_size; } /* end if */ *flags = dblock_flags; /* final sanity check */ HDassert(dblock->write_buf); HDassert(dblock->write_size > 0); done: /* discard the write buf if we have an error */ if(write_buf && (write_buf != dblock->blk) && (dblock->write_buf == NULL)) H5MM_xfree(write_buf); FUNC_LEAVE_NOAPI(ret_value) } /* end H5HF__cache_dblock_pre_serialize() */ /*------------------------------------------------------------------------- * Function: H5HF__cache_dblock_serialize * * Purpose: In principle, this function is supposed to construct the on * disk image of the direct block, and place that image in the * image buffer provided by the metadata cache. * * However, since there are cases in which the pre_serialize * function has to construct the on disk image to determine its size * and address, this function simply copies the image prepared by * the pre-serialize function into the supplied image buffer, and * discards a buffer if necessary. * * Return: Success: SUCCEED * Failure: FAIL * * Programmer: John Mainzer * 6/21/14 * *------------------------------------------------------------------------- */ static herr_t H5HF__cache_dblock_serialize(const H5F_t *f, void *image, size_t len, void *_thing) { H5HF_direct_t *dblock = (H5HF_direct_t *)_thing; /* Direct block info */ herr_t ret_value = SUCCEED; /* Return value */ FUNC_ENTER_STATIC_NOERR /* Sanity checks */ HDassert(f); HDassert(image); HDassert(len > 0); HDassert(dblock); HDassert(dblock->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC); HDassert(dblock->cache_info.type == H5AC_FHEAP_DBLOCK); HDassert((dblock->blk != dblock->write_buf) || (dblock->cache_info.size == dblock->size)); HDassert(dblock->write_buf); HDassert(dblock->write_size > 0); HDassert((dblock->blk != dblock->write_buf) || (dblock->write_size == dblock->size)); HDassert(dblock->write_size == len); /* Copy the image from *(dblock->write_buf) to *image */ HDmemcpy(image, dblock->write_buf, dblock->write_size); /* Free *(dblock->write_buf) if it was allocated by the * pre-serialize function */ if(dblock->write_buf != dblock->blk) H5MM_xfree(dblock->write_buf); /* Reset the write_buf and write_size fields */ dblock->write_buf = NULL; dblock->write_size = 0; FUNC_LEAVE_NOAPI(ret_value) } /* end H5HF__cache_dblock_serialize() */ /*------------------------------------------------------------------------- * Function: H5HF__cache_dblock_notify * * Purpose: Setup / takedown flush dependencies as direct blocks * are loaded / inserted and evicted from the metadata cache. * * Return: Success: SUCCEED * Failure: FAIL * * Programmer: John Mainzer * 6/21/14 * *------------------------------------------------------------------------- */ static herr_t H5HF__cache_dblock_notify(H5AC_notify_action_t action, void *_thing) { H5HF_direct_t *dblock = (H5HF_direct_t *)_thing; /* Fractal heap direct block */ herr_t ret_value = SUCCEED; /* Return value */ FUNC_ENTER_STATIC /* Sanity checks */ HDassert(dblock); HDassert(dblock->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC); HDassert(dblock->cache_info.type == H5AC_FHEAP_DBLOCK); HDassert(dblock->hdr); HDassert((dblock->fd_parent) || ((dblock->hdr->man_dtable.curr_root_rows == 0) && (dblock->block_off == (hsize_t)0))); switch(action) { case H5AC_NOTIFY_ACTION_AFTER_INSERT: case H5AC_NOTIFY_ACTION_AFTER_LOAD: HDassert(dblock->parent == dblock->fd_parent); if(dblock->parent) { /* this is a leaf dblock */ /* create flush dependency with parent iblock */ if(H5AC_create_flush_dependency(dblock->parent, dblock) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTDEPEND, FAIL, "unable to create flush dependency") } /* end if */ else { /* this is a root dblock */ /* create flush dependency with header */ if(H5AC_create_flush_dependency(dblock->hdr, dblock) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTDEPEND, FAIL, "unable to create flush dependency") } /* end else */ break; case H5AC_NOTIFY_ACTION_AFTER_FLUSH: case H5AC_NOTIFY_ACTION_ENTRY_DIRTIED: case H5AC_NOTIFY_ACTION_ENTRY_CLEANED: case H5AC_NOTIFY_ACTION_CHILD_DIRTIED: case H5AC_NOTIFY_ACTION_CHILD_CLEANED: case H5AC_NOTIFY_ACTION_CHILD_UNSERIALIZED: case H5AC_NOTIFY_ACTION_CHILD_SERIALIZED: /* do nothing */ break; case H5AC_NOTIFY_ACTION_BEFORE_EVICT: HDassert((dblock->parent == dblock->fd_parent) || ((NULL == dblock->parent) && (dblock->fd_parent))); if(dblock->fd_parent) { /* this is a leaf dblock */ /* destroy flush dependency with parent iblock */ if(H5AC_destroy_flush_dependency(dblock->fd_parent, dblock) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTUNDEPEND, FAIL, "unable to destroy flush dependency") } /* end if */ else { /* this is a root dblock */ /* destroy flush dependency with header */ if(H5AC_destroy_flush_dependency(dblock->hdr, dblock) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTUNDEPEND, FAIL, "unable to destroy flush dependency") } /* end else */ break; default: HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "unknown action from metadata cache") break; } /* end switch */ done: FUNC_LEAVE_NOAPI(ret_value) } /* end H5HF__cache_dblock_notify() */ /*------------------------------------------------------------------------- * Function: H5HF__cache_dblock_free_icr * * Purpose: Free the in core memory allocated to the supplied direct * block. * * Note: The metadata cache sets the object's cache_info.magic to * H5C__H5C_CACHE_ENTRY_T_BAD_MAGIC before calling a free_icr * callback (checked in assert). * * Return: Success: SUCCEED * Failure: FAIL * * Programmer: John Mainzer * 6/21/14 * *------------------------------------------------------------------------- */ static herr_t H5HF__cache_dblock_free_icr(void *_thing) { H5HF_direct_t *dblock = (H5HF_direct_t *)_thing; /* Fractal heap direct block */ herr_t ret_value = SUCCEED; /* Return value */ FUNC_ENTER_STATIC /* Sanity checks */ HDassert(dblock); HDassert(dblock->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_BAD_MAGIC); HDassert(dblock->cache_info.type == H5AC_FHEAP_DBLOCK); /* Destroy fractal heap direct block */ if(H5HF_man_dblock_dest(dblock) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTFREE, FAIL, "unable to destroy fractal heap direct block") done: FUNC_LEAVE_NOAPI(ret_value) } /* end H5HF__cache_dblock_free_icr() */ /*------------------------------------------------------------------------ * Function: H5HF__cache_verify_hdr_descendants_clean * * Purpose: Sanity checking routine that verifies that all indirect * and direct blocks that are descendants of the supplied * instance of H5HF_hdr_t are clean. Set *clean to * TRUE if this is the case, and to FALSE otherwise. * * Update -- 8/24/15 * * With the advent of the metadata cache image feature, it is * possible for the pre-serialize and serialize calls to be * invoked outside of a flush. While this serialization * observes flush dependencies for the order of serialization, * the entries are not written to disk, and hence dirty entries * remain dirty. * * To address this, updated the sanity checks in this function * to treat entries whose images are up to date as clean if * a cache serialization is in progress. * * Update -- 9/29/16 * * The implementation of flush dependencies has been changed. * Prior to this change, a flush dependency parent could be * flushed if and only if all its flush dependency decendants * were clean. In the new definition, a flush dependency * parent can be flushed if all its immediate flush dependency * children are clean, regardless of any other dirty * decendants. * * Further, metadata cache entries are now allowed to have * multiple flush dependency parents. * * This means that the fractal heap is no longer ncessarily * flushed from the bottom up. * * For example, it is now possible for a dirty fractal heap * header to be flushed before a dirty dblock, as long as the * there in an interviening iblock, and the header has no * dirty immediate flush dependency children. * * Also, I gather that under some circumstances, a dblock * will be direct a flush dependency child both of the iblock * that points to it, and of the fractal heap header. * * As a result of these changes, the functionality of these * sanity checking routines has been modified significantly. * Instead of scanning the fractal heap from a starting point * down, and verifying that there were no dirty entries, the * functions now scan downward from the starting point and * verify that there are no dirty flush dependency children * of the specified flush dependency parent. In passing, * they also walk the data structure, and verify it. * * * Return: Non-negative on success/Negative on failure * * Programmer: John Mainzer * 5/25/14 * *------------------------------------------------------------------------- */ #ifndef NDEBUG static herr_t H5HF__cache_verify_hdr_descendants_clean(H5F_t *f, hid_t dxpl_id, H5HF_hdr_t *hdr, hbool_t *fd_clean, hbool_t *clean) { hbool_t fd_exists = FALSE; /* whether flush dependency exists. */ haddr_t hdr_addr; /* Address of header */ unsigned hdr_status = 0; /* Header cache entry status */ herr_t ret_value = SUCCEED; /* Return value */ FUNC_ENTER_STATIC /* Sanity checks */ HDassert(f); HDassert(hdr); HDassert(hdr->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC); HDassert(hdr->cache_info.type == H5AC_FHEAP_HDR); HDassert(fd_clean); HDassert(clean); hdr_addr = hdr->cache_info.addr; HDassert(hdr_addr == hdr->heap_addr); if(H5AC_get_entry_status(f, hdr_addr, &hdr_status) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTGET, FAIL, "can't get hdr status") HDassert(hdr_status & H5AC_ES__IN_CACHE); /* We have three basic scenarios we have to deal with: * * The first, and most common case, is that there is a root iblock. * In this case we need to verify that the root iblock and all its * children are clean. * * The second, and much less common case, is that in which the * the fractal heap contains only one direct block, which is * pointed to by hdr->man_dtable.table_addr. In this case, all we * need to do is verify that the root direct block is clean. * * Finally, it is possible that the fractal heap is empty, and * has neither a root indirect block nor a root direct block. * In this case, we have nothing to do. */ /* There are two ways in which we can arrive at the first scenario. * * By far the most common is when hdr->root_iblock contains a pointer * to the root iblock -- in this case the root iblock is almost certainly * pinned, although we can't count on that. * * However, it is also possible that there is a root iblock that * is no longer pointed to by the header. In this case, the on * disk address of the iblock will be in hdr->man_dtable.table_addr * and hdr->man_dtable.curr_root_rows will contain a positive value. * * Since the former case is far and away the most common, we don't * worry too much about efficiency in the second case. */ if(hdr->root_iblock || ((hdr->man_dtable.curr_root_rows > 0) && (HADDR_UNDEF != hdr->man_dtable.table_addr))) { H5HF_indirect_t *root_iblock = hdr->root_iblock; haddr_t root_iblock_addr; unsigned root_iblock_status = 0; hbool_t root_iblock_in_cache; /* make note of the on disk address of the root iblock */ if(root_iblock == NULL) /* hdr->man_dtable.table_addr must contain address of root * iblock. Check to see if it is in cache. If it is, * protect it and put its address in root_iblock. */ root_iblock_addr = hdr->man_dtable.table_addr; else root_iblock_addr = root_iblock->addr; /* get the status of the root iblock */ HDassert(root_iblock_addr != HADDR_UNDEF); if(H5AC_get_entry_status(f, root_iblock_addr, &root_iblock_status) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTGET, FAIL, "can't get root iblock status") root_iblock_in_cache = ( (root_iblock_status & H5AC_ES__IN_CACHE) != 0); HDassert(root_iblock_in_cache || (root_iblock == NULL)); if(!root_iblock_in_cache) { /* we are done */ *clean = TRUE; *fd_clean = TRUE; } /* end if */ else if((root_iblock_status & H5AC_ES__IS_DIRTY) && (((root_iblock_status & H5AC_ES__IMAGE_IS_UP_TO_DATE) == 0) || (!H5AC_get_serialization_in_progress(f)))) { *clean = FALSE; /* verify that a flush dependency exists between the header and * the root inode. */ if(H5AC_flush_dependency_exists(f, hdr->heap_addr, root_iblock_addr, &fd_exists) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTGET, FAIL, "can't check flush dependency") HDassert(fd_exists); *fd_clean = FALSE; } /* end else-if */ else { /* must examine children */ hbool_t unprotect_root_iblock = FALSE; /* At this point, the root iblock may be pinned, protected, * both, or neither, and we may or may not have a pointer * to root iblock in memory. * * Before we call H5HF__cache_verify_iblock_descendants_clean(), * we must ensure that the root iblock is either pinned or * protected or both, and that we have a pointer to it. * Do this as follows: */ if(root_iblock == NULL) { /* we don't have ptr to root iblock */ if(0 == (root_iblock_status & H5AC_ES__IS_PROTECTED)) { /* just protect the root iblock -- this will give us * the pointer we need to proceed, and ensure that * it is locked into the metadata cache for the * duration. * * Note that the udata is only used in the load callback. * While the fractal heap makes heavy use of the udata * in this case, since we know that the entry is in cache, * we can pass NULL udata. * * The tag specified in the dxpl we received * as a parameter (via dxpl_id) may not be correct. * Grab the (hopefully) correct tag from the header, * and load it into the dxpl via the H5_BEGIN_TAG and * H5_END_TAG macros. Note that any error bracked by * these macros must be reported with HGOTO_ERROR_TAG. */ H5_BEGIN_TAG(dxpl_id, hdr->heap_addr, FAIL) if(NULL == (root_iblock = (H5HF_indirect_t *)H5AC_protect(f, dxpl_id, H5AC_FHEAP_IBLOCK, root_iblock_addr, NULL, H5AC__READ_ONLY_FLAG))) HGOTO_ERROR_TAG(H5E_HEAP, H5E_CANTPROTECT, FAIL, "H5AC_protect() faild.") H5_END_TAG(FAIL) unprotect_root_iblock = TRUE; } /* end if */ else { /* the root iblock is protected, and we have no * legitimate way of getting a pointer to it. * * We square this circle by using the * H5AC_get_entry_ptr_from_addr() to get the needed * pointer. * * WARNING: This call should be used only in debugging * routines, and it should be avoided there when * possible. * * Further, if we ever multi-thread the cache, * this routine will have to be either discarded * or heavily re-worked. * * Finally, keep in mind that the entry whose * pointer is obtained in this fashion may not * be in a stable state. * * Assuming that the flush dependency code is working * as it should, the only reason for the root iblock to * be unpinned is if none of its children are in cache. * This unfortunately means that if it is protected and * not pinned, the fractal heap is in the process of loading * or inserting one of its children. The obvious * implication is that there is a significant chance that * the root iblock is in an unstable state. * * All this suggests that using * H5AC_get_entry_ptr_from_addr() to obtain the pointer * to the protected root iblock is questionable here. * However, since this is test/debugging code, I expect * that we will use this approach until it causes problems, * or we think of a better way. */ if(H5AC_get_entry_ptr_from_addr(f, root_iblock_addr, (void **)(&root_iblock)) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTGET, FAIL, "H5AC_get_entry_ptr_from_addr() failed.") HDassert(root_iblock); } /* end else */ } /* end if */ else { /* root_iblock != NULL */ /* we have the pointer to the root iblock. Protect it * if it is neither pinned nor protected -- otherwise we * are ready to go. */ H5HF_indirect_t * iblock = NULL; if(((root_iblock_status & H5AC_ES__IS_PINNED) == 0) && ((root_iblock_status & H5AC_ES__IS_PROTECTED) == 0)) { /* the root iblock is neither pinned nor protected -- hence * we must protect it before we proceed * * Note that the udata is only used in the load callback. * While the fractal heap makes heavy use of the udata * in this case, since we know that the entry is in cache, * we can pass NULL udata. * * The tag associated specified in the dxpl we received * as a parameter (via dxpl_id) may not be correct. * Grab the (hopefully) correct tag from the header, * and load it into the dxpl via the H5_BEGIN_TAG and * H5_END_TAG macros. Note that any error bracked by * these macros must be reported with HGOTO_ERROR_TAG. */ H5_BEGIN_TAG(dxpl_id, hdr->heap_addr, FAIL) if(NULL == (iblock = (H5HF_indirect_t *)H5AC_protect(f, dxpl_id, H5AC_FHEAP_IBLOCK, root_iblock_addr, NULL, H5AC__READ_ONLY_FLAG))) HGOTO_ERROR_TAG(H5E_HEAP, H5E_CANTPROTECT, FAIL, "H5AC_protect() faild.") H5_END_TAG(FAIL) unprotect_root_iblock = TRUE; HDassert(iblock == root_iblock); } /* end if */ } /* end else */ /* at this point, one way or another, the root iblock is locked * in memory for the duration of the call. Do some sanity checks, * and then call H5HF__cache_verify_iblock_descendants_clean(). */ HDassert(root_iblock->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC); HDassert(root_iblock->cache_info.type == H5AC_FHEAP_IBLOCK); if(H5HF__cache_verify_iblock_descendants_clean(f, dxpl_id, hdr->heap_addr, root_iblock, &root_iblock_status, fd_clean, clean) < 0) HGOTO_ERROR(H5E_HEAP, H5E_SYSTEM, FAIL, "can't verify root iblock & descendants clean.") /* Unprotect the root indirect block if required */ if(unprotect_root_iblock) { HDassert(root_iblock); if(H5AC_unprotect(f, dxpl_id, H5AC_FHEAP_IBLOCK, root_iblock_addr, root_iblock, H5AC__NO_FLAGS_SET) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTUNPROTECT, FAIL, "H5AC_unprotect() faild.") } /* end if */ } /* end else */ } /* end if */ else if((hdr->man_dtable.curr_root_rows == 0) && (HADDR_UNDEF != hdr->man_dtable.table_addr)) { haddr_t root_dblock_addr; unsigned root_dblock_status = 0; hbool_t in_cache; hbool_t type_ok; /* this is scenario 2 -- we have a root dblock */ root_dblock_addr = hdr->man_dtable.table_addr; if(H5AC_get_entry_status(f, root_dblock_addr, &root_dblock_status) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTGET, FAIL, "can't get root dblock status") if(root_dblock_status & H5AC_ES__IN_CACHE) { if(H5AC_verify_entry_type(f, root_dblock_addr, &H5AC_FHEAP_DBLOCK[0], &in_cache, &type_ok) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTGET, FAIL, "can't check dblock type") HDassert(in_cache); if(!type_ok) HGOTO_ERROR(H5E_HEAP, H5E_SYSTEM, FAIL, "root dblock addr doesn't refer to a dblock?!?") /* If a root dblock is in cache, it must have a flush * dependency relationship with the header, and it * may not be the parent in any flush dependency * relationship. * * We don't test this fully, but we will verify that * the root iblock is a child in a flush dependency * relationship with the header. */ if(H5AC_flush_dependency_exists(f, hdr->heap_addr, root_dblock_addr, &fd_exists) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTGET, FAIL, "can't check flush dependency") if(!fd_exists) HGOTO_ERROR(H5E_HEAP, H5E_SYSTEM, FAIL, "root dblock is not a flush dep parent of header.") if(0 != (root_dblock_status & H5AC_ES__IS_FLUSH_DEP_PARENT)) HGOTO_ERROR(H5E_HEAP, H5E_SYSTEM, FAIL, "root dblock in cache and is a flush dep parent.") *clean = !((root_dblock_status & H5AC_ES__IS_DIRTY) && (((root_dblock_status & H5AC_ES__IMAGE_IS_UP_TO_DATE) == 0) || (!H5AC_get_serialization_in_progress(f)))); *fd_clean = *clean; } /* end if */ else { /* root dblock not in cache */ *fd_clean = TRUE; *clean = TRUE; } /* end else */ } /* end else-if */ else { /* this is scenario 3 -- the fractal heap is empty, and we * have nothing to do. */ *fd_clean = TRUE; *clean = TRUE; } /* end else */ done: FUNC_LEAVE_NOAPI(ret_value) } /* H5HF__cache_verify_hdr_descendants_clean() */ #endif /* NDEBUG */ /*------------------------------------------------------------------------ * Function: H5HF__cache_verify_iblock_descendants_clean * * Purpose: Sanity checking routine that verifies that all indirect * and direct blocks that are decendents of the supplied * instance of H5HF_indirect_t are clean. Set *clean * to TRUE if this is the case, and to FALSE otherwise. * * In passing, the function also does a cursory check to * spot any obvious errors in the flush dependency setup. * If any problems are found, the function returns failure. * Note that these checks are not exhaustive, thus passing * them does not mean that the flush dependencies are * correct -- only that there is nothing obviously wrong * with them. * * WARNING: At its top level call, this function is * intended to be called from H5HF_cache_iblock_flush(), * and thus presumes that the supplied indirect block * is in cache. Any other use of this function and * its descendants must insure that this assumption is * met. * * Note that this function and * H5HF__cache_verify_descendant_iblocks_clean() are * recursive co-routines. * * Update -- 9/29/16 * * The implementation of flush dependencies has been changed. * Prior to this change, a flush dependency parent could be * flushed if and only if all its flush dependency decendants * were clean. In the new definition, a flush dependency * parent can be flushed if all its immediate flush dependency * children are clean, regardless of any other dirty * decendants. * * Further, metadata cache entries are now allowed to have * multiple flush dependency parents. * * This means that the fractal heap is no longer ncessarily * flushed from the bottom up. * * For example, it is now possible for a dirty fractal heap * header to be flushed before a dirty dblock, as long as the * there in an interviening iblock, and the header has no * dirty immediate flush dependency children. * * Also, I gather that under some circumstances, a dblock * will be direct a flush dependency child both of the iblock * that points to it, and of the fractal heap header. * * As a result of these changes, the functionality of these * sanity checking routines has been modified significantly. * Instead of scanning the fractal heap from a starting point * down, and verifying that there were no dirty entries, the * functions now scan downward from the starting point and * verify that there are no dirty flush dependency children * of the specified flush dependency parent. In passing, * they also walk the data structure, and verify it. * * Return: Non-negative on success/Negative on failure * * Programmer: John Mainzer * 5/25/14 * *------------------------------------------------------------------------- */ #ifndef NDEBUG static herr_t H5HF__cache_verify_iblock_descendants_clean(H5F_t *f, hid_t dxpl_id, haddr_t fd_parent_addr, H5HF_indirect_t *iblock, unsigned *iblock_status, hbool_t * fd_clean, hbool_t *clean) { hbool_t has_dblocks = FALSE; hbool_t has_iblocks = FALSE; herr_t ret_value = SUCCEED; /* Return value */ FUNC_ENTER_STATIC /* Sanity checks */ HDassert(f); HDassert(H5F_addr_defined(fd_parent_addr)); HDassert(iblock); HDassert(iblock->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC); HDassert(iblock->cache_info.type == H5AC_FHEAP_IBLOCK); HDassert(iblock_status); HDassert(fd_clean); HDassert(*fd_clean); HDassert(clean); /* note that *clean need not be TRUE */ if((*fd_clean) && H5HF__cache_verify_iblocks_dblocks_clean(f, fd_parent_addr, iblock, fd_clean, clean, &has_dblocks) < 0) HGOTO_ERROR(H5E_HEAP, H5E_SYSTEM, FAIL, "can't verify dblocks clean.") if((*fd_clean) && H5HF__cache_verify_descendant_iblocks_clean(f, dxpl_id, fd_parent_addr, iblock, fd_clean, clean, &has_iblocks) < 0) HGOTO_ERROR(H5E_HEAP, H5E_SYSTEM, FAIL, "can't verify iblocks clean.") /* verify that flush dependency setup is plausible */ if(0 == (*iblock_status & H5AC_ES__IS_FLUSH_DEP_CHILD)) HGOTO_ERROR(H5E_HEAP, H5E_SYSTEM, FAIL, "iblock is not a flush dep child.") if(((has_dblocks || has_iblocks)) && (0 == (*iblock_status & H5AC_ES__IS_FLUSH_DEP_PARENT))) HGOTO_ERROR(H5E_HEAP, H5E_SYSTEM, FAIL, "iblock has children and is not a flush dep parent.") if(((has_dblocks || has_iblocks)) && (0 == (*iblock_status & H5AC_ES__IS_PINNED))) HGOTO_ERROR(H5E_HEAP, H5E_SYSTEM, FAIL, "iblock has children and is not pinned.") done: FUNC_LEAVE_NOAPI(ret_value) } /* H5HF__cache_verify_iblock_descendants_clean() */ #endif /* NDEBUG */ /*------------------------------------------------------------------------ * Function: H5HF__cache_verify_iblocks_dblocks_clean * * Purpose: Sanity checking routine that attempts to verify that all * direct blocks pointed to by the supplied indirect block * are either clean, or not in the cache. * * In passing, the function also does a cursory check to * spot any obvious errors in the flush dependency setup. * If any problems are found, the function returns failure. * Note that these checks are not exhaustive, thus passing * them does not mean that the flush dependencies are * correct -- only that there is nothing obviously wrong * with them. * * WARNING: This function presumes that the supplied * iblock is in the cache, and will not be removed * during the call. Caller must ensure that this is * the case before the call. * * Update -- 8/24/15 * * With the advent of the metadata cache image feature, it is * possible for the pre-serialize and serialize calls to be * invoked outside of a flush. While this serialization * observes flush dependencies for the order of serialization, * the entries are not written to disk, and hence dirty entries * remain dirty. * * To address this, updated the sanity checks in this function * to treat entries whose images are up to date as clean if * a cache serialization is in progress. * * Update -- 9/29/16 * * The implementation of flush dependencies has been changed. * Prior to this change, a flush dependency parent could be * flushed if and only if all its flush dependency decendants * were clean. In the new definition, a flush dependency * parent can be flushed if all its immediate flush dependency * children are clean, regardless of any other dirty * decendants. * * Further, metadata cache entries are now allowed to have * multiple flush dependency parents. * * This means that the fractal heap is no longer ncessarily * flushed from the bottom up. * * For example, it is now possible for a dirty fractal heap * header to be flushed before a dirty dblock, as long as the * there in an interviening iblock, and the header has no * dirty immediate flush dependency children. * * Also, I gather that under some circumstances, a dblock * will be direct a flush dependency child both of the iblock * that points to it, and of the fractal heap header. * * As a result of these changes, the functionality of these * sanity checking routines has been modified significantly. * Instead of scanning the fractal heap from a starting point * down, and verifying that there were no dirty entries, the * functions now scan downward from the starting point and * verify that there are no dirty flush dependency children * of the specified flush dependency parent. In passing, * they also walk the data structure, and verify it. * * Return: Non-negative on success/Negative on failure * * Programmer: John Mainzer * 5/25/14 * *------------------------------------------------------------------------- */ #ifndef NDEBUG static herr_t H5HF__cache_verify_iblocks_dblocks_clean(H5F_t *f, haddr_t fd_parent_addr, H5HF_indirect_t *iblock, hbool_t *fd_clean, hbool_t *clean, hbool_t *has_dblocks) { unsigned num_direct_rows; unsigned max_dblock_index; unsigned i; haddr_t iblock_addr; herr_t ret_value = SUCCEED; /* Return value */ FUNC_ENTER_STATIC /* Sanity checks */ HDassert(f); HDassert(H5F_addr_defined(fd_parent_addr)); HDassert(iblock); HDassert(iblock->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC); HDassert(iblock->cache_info.type == H5AC_FHEAP_IBLOCK); HDassert(fd_clean); HDassert(*fd_clean); HDassert(clean); /* note that *clean need not be true */ HDassert(has_dblocks); i = 0; num_direct_rows = MIN(iblock->nrows, iblock->hdr->man_dtable.max_direct_rows); HDassert(num_direct_rows <= iblock->nrows); max_dblock_index = (num_direct_rows * iblock->hdr->man_dtable.cparam.width) - 1; iblock_addr = iblock->addr; HDassert(H5F_addr_defined(iblock_addr)); while((*fd_clean) && (i <= max_dblock_index)) { haddr_t dblock_addr; dblock_addr = iblock->ents[i].addr; if(H5F_addr_defined(dblock_addr)) { hbool_t in_cache; hbool_t type_ok; if(H5AC_verify_entry_type(f, dblock_addr, &H5AC_FHEAP_DBLOCK[0], &in_cache, &type_ok) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTGET, FAIL, "can't check dblock type") if(in_cache) { /* dblock is in cache */ hbool_t fd_exists; unsigned dblock_status = 0; if(!type_ok) HGOTO_ERROR(H5E_HEAP, H5E_SYSTEM, FAIL, "dblock addr doesn't refer to a dblock?!?") if(H5AC_get_entry_status(f, dblock_addr, &dblock_status) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTGET, FAIL, "can't get dblock status") HDassert(dblock_status & H5AC_ES__IN_CACHE); *has_dblocks = TRUE; if((dblock_status & H5AC_ES__IS_DIRTY) && (((dblock_status & H5AC_ES__IMAGE_IS_UP_TO_DATE) == 0) || (!H5AC_get_serialization_in_progress(f)))) { *clean = FALSE; if(H5AC_flush_dependency_exists(f, fd_parent_addr, dblock_addr, &fd_exists) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTGET, FAIL, "can't check flush dependency") if(fd_exists) *fd_clean = FALSE; } /* end if */ /* If a child dblock is in cache, it must have a flush * dependency relationship with this iblock. Test this * here. */ if(H5AC_flush_dependency_exists(f, iblock_addr, dblock_addr, &fd_exists) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTGET, FAIL, "can't check flush dependency") if(!fd_exists) HGOTO_ERROR(H5E_HEAP, H5E_SYSTEM, FAIL, "dblock in cache and not a flush dep child of iblock.") } /* end if */ } /* end if */ i++; } /* end while */ done: FUNC_LEAVE_NOAPI(ret_value) } /* H5HF__cache_verify_iblocks_dblocks_clean() */ #endif /* NDEBUG */ /*------------------------------------------------------------------------ * Function: H5HF__cache_verify_descendant_iblocks_clean * * Purpose: Sanity checking routine that attempts to verify that all * direct blocks pointed to by the supplied indirect block * are either clean, or not in the cache. * * In passing, the function also does a cursory check to * spot any obvious errors in the flush dependency setup. * If any problems are found, the function returns failure. * Note that these checks are not exhaustive, thus passing * them does not mean that the flush dependencies are * correct -- only that there is nothing obviously wrong * with them. * * WARNING: This function presumes that the supplied * iblock is in the cache, and will not be removed * during the call. Caller must ensure that this is * the case before the call. * * Update -- 8/24/15 * * With the advent of the metadata cache image feature, it is * possible for the pre-serialize and serialize calls to be * invoked outside of a flush. While this serialization * observes flush dependencies for the order of serialization, * the entries are not written to disk, and hence dirty entries * remain dirty. * * To address this, updated the sanity checks in this function * to treat entries whose images are up to date as clean if * a cache serialization is in progress. * * Update -- 9/29/16 * * The implementation of flush dependencies has been changed. * Prior to this change, a flush dependency parent could be * flushed if and only if all its flush dependency decendants * were clean. In the new definition, a flush dependency * parent can be flushed if all its immediate flush dependency * children are clean, regardless of any other dirty * decendants. * * Further, metadata cache entries are now allowed to have * multiple flush dependency parents. * * This means that the fractal heap is no longer ncessarily * flushed from the bottom up. * * For example, it is now possible for a dirty fractal heap * header to be flushed before a dirty dblock, as long as the * there in an interviening iblock, and the header has no * dirty immediate flush dependency children. * * Also, I gather that under some circumstances, a dblock * will be direct a flush dependency child both of the iblock * that points to it, and of the fractal heap header. * * As a result of these changes, the functionality of these * sanity checking routines has been modified significantly. * Instead of scanning the fractal heap from a starting point * down, and verifying that there were no dirty entries, the * functions now scan downward from the starting point and * verify that there are no dirty flush dependency children * of the specified flush dependency parent. In passing, * they also walk the data structure, and verify it. * * * Return: Non-negative on success/Negative on failure * * Programmer: John Mainzer * 5/25/14 * *------------------------------------------------------------------------- */ #ifndef NDEBUG static herr_t H5HF__cache_verify_descendant_iblocks_clean(H5F_t *f, hid_t dxpl_id, haddr_t fd_parent_addr, H5HF_indirect_t *iblock, hbool_t *fd_clean, hbool_t *clean, hbool_t *has_iblocks) { unsigned first_iblock_index; unsigned last_iblock_index; unsigned num_direct_rows; unsigned i; haddr_t iblock_addr; herr_t ret_value = SUCCEED; /* Return value */ FUNC_ENTER_STATIC /* Sanity checks */ HDassert(f); HDassert(H5F_addr_defined(fd_parent_addr)); HDassert(iblock); HDassert(iblock->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC); HDassert(iblock->cache_info.type == H5AC_FHEAP_IBLOCK); HDassert(fd_clean); HDassert(*fd_clean); HDassert(clean); /* note that *clean need not be true */ HDassert(has_iblocks); num_direct_rows = MIN(iblock->nrows, iblock->hdr->man_dtable.max_direct_rows); HDassert(num_direct_rows <= iblock->nrows); iblock_addr = iblock->addr; first_iblock_index = num_direct_rows * iblock->hdr->man_dtable.cparam.width; last_iblock_index = (iblock->nrows * iblock->hdr->man_dtable.cparam.width) - 1; i = first_iblock_index; while((*fd_clean) && (i <= last_iblock_index)) { haddr_t child_iblock_addr = iblock->ents[i].addr; if(H5F_addr_defined(child_iblock_addr)) { unsigned child_iblock_status = 0; if(H5AC_get_entry_status(f, child_iblock_addr, &child_iblock_status) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTGET, FAIL, "can't get iblock status") if(child_iblock_status & H5AC_ES__IN_CACHE) { hbool_t fd_exists; *has_iblocks = TRUE; if((child_iblock_status & H5AC_ES__IS_DIRTY) && (((child_iblock_status & H5AC_ES__IMAGE_IS_UP_TO_DATE) == 0) || (!H5AC_get_serialization_in_progress(f)))) { *clean = FALSE; if(H5AC_flush_dependency_exists(f, fd_parent_addr, child_iblock_addr, &fd_exists) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTGET, FAIL, "can't check flush dependency") if(fd_exists) *fd_clean = FALSE; } /* end if */ /* if the child iblock is in cache and *fd_clean is TRUE, * we must continue to explore down the fractal heap tree * structure to verify that all descendant blocks that are * flush dependency children of the entry at parent_addr are * either clean, or not in the metadata cache. We do this * with a recursive call to * H5HF__cache_verify_iblock_descendants_clean(). * However, we can't make this call unless the child iblock * is somehow locked into the cache -- typically via either * pinning or protecting. * * If the child iblock is pinned, we can look up its pointer * on the current iblock's pinned child iblock list, and * and use that pointer in the recursive call. * * If the entry is unprotected and unpinned, we simply * protect it. * * If, however, the the child iblock is already protected, * but not pinned, we have a bit of a problem, as we have * no legitimate way of looking up its pointer in memory. * * To solve this problem, I have added a new metadata cache * call to obtain the pointer. * * WARNING: This call should be used only in debugging * routines, and it should be avoided there when * possible. * * Further, if we ever multi-thread the cache, * this routine will have to be either discarded * or heavily re-worked. * * Finally, keep in mind that the entry whose * pointer is obtained in this fashion may not * be in a stable state. * * Assuming that the flush dependency code is working * as it should, the only reason for the child entry to * be unpinned is if none of its children are in cache. * This unfortunately means that if it is protected and * not pinned, the fractal heap is in the process of loading * or inserting one of its children. The obvious implication * is that there is a significant chance that the child * iblock is in an unstable state. * * All this suggests that using the new call to obtain the * pointer to the protected child iblock is questionable * here. However, since this is test/debugging code, I * expect that we will use this approach until it causes * problems, or we think of a better way. */ if(*fd_clean) { H5HF_indirect_t *child_iblock = NULL; hbool_t unprotect_child_iblock = FALSE; if(0 == (child_iblock_status & H5AC_ES__IS_PINNED)) { /* child iblock is not pinned */ if(0 == (child_iblock_status & H5AC_ES__IS_PROTECTED)) { /* child iblock is unprotected, and unpinned */ /* protect it. Note that the udata is only */ /* used in the load callback. While the */ /* fractal heap makes heavy use of the udata */ /* in this case, since we know that the */ /* entry is in cache, we can pass NULL udata */ /* */ /* The tag associated specified in the dxpl */ /* we received as a parameter (via dxpl_id) */ /* may not be correct. */ /* */ /* Grab the (hopefully) correct tag from the */ /* parent iblock, and load it into the dxpl */ /* via the H5_BEGIN_TAG and H5_END_TAG */ /* macros. Note that any error bracked by */ /* these macros must be reported with */ /* HGOTO_ERROR_TAG. */ H5_BEGIN_TAG(dxpl_id, iblock->hdr->heap_addr, FAIL) if(NULL == (child_iblock = (H5HF_indirect_t *) H5AC_protect(f, dxpl_id, H5AC_FHEAP_IBLOCK, child_iblock_addr, NULL, H5AC__READ_ONLY_FLAG))) HGOTO_ERROR_TAG(H5E_HEAP, H5E_CANTPROTECT, FAIL, "H5AC_protect() faild.") H5_END_TAG(FAIL) unprotect_child_iblock = TRUE; } /* end if */ else { /* child iblock is protected -- use */ /* H5AC_get_entry_ptr_from_addr() to get a */ /* pointer to the entry. This is very slimy -- */ /* come up with a better solution. */ if(H5AC_get_entry_ptr_from_addr(f, child_iblock_addr, (void **)(&child_iblock)) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTGET, FAIL, "H5AC_get_entry_ptr_from_addr() faild.") HDassert(child_iblock); } /* end else */ } /* end if */ else { /* child iblock is pinned -- look it up in the */ /* parent iblocks child_iblocks array. */ HDassert(iblock->child_iblocks); child_iblock = iblock->child_iblocks[i - first_iblock_index]; } /* end else */ /* At this point, one way or another we should have * a pointer to the child iblock. Verify that we * that we have the correct one. */ HDassert(child_iblock); HDassert(child_iblock->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC); HDassert(child_iblock->cache_info.type == H5AC_FHEAP_IBLOCK); HDassert(child_iblock->addr == child_iblock_addr); /* now make the recursive call */ if(H5HF__cache_verify_iblock_descendants_clean(f, dxpl_id, fd_parent_addr, child_iblock, &child_iblock_status, fd_clean, clean) < 0) HGOTO_ERROR(H5E_HEAP, H5E_SYSTEM, FAIL, "can't verify child iblock clean.") /* if iblock_addr != fd_parent_addr, verify that a flush * dependency relationship exists between iblock and * the child iblock. */ if(fd_parent_addr != iblock_addr) { if(H5AC_flush_dependency_exists(f, iblock_addr, child_iblock_addr, &fd_exists) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTGET, FAIL, "can't check flush dependency") if(!fd_exists) HGOTO_ERROR(H5E_HEAP, H5E_SYSTEM, FAIL, "iblock is not a flush dep parent of child_iblock.") } /* end if */ /* if we protected the child iblock, unprotect it now */ if(unprotect_child_iblock) { if(H5AC_unprotect(f, dxpl_id, H5AC_FHEAP_IBLOCK, child_iblock_addr, child_iblock, H5AC__NO_FLAGS_SET) < 0) HGOTO_ERROR(H5E_HEAP, H5E_CANTUNPROTECT, FAIL, "H5AC_unprotect() faild.") } /* end if */ } /* end if */ } /* end if */ } /* end if */ i++; } /* end while */ done: FUNC_LEAVE_NOAPI(ret_value) } /* H5HF__cache_verify_descendant_iblocks_clean() */ #endif /* NDEBUG */