@@ -470,17 +470,17 @@ int cfs_munmap(FileMap* map)
470
470
*/
471
471
uint32 cfs_alloc_page (FileMap * map , uint32 oldSize , uint32 newSize )
472
472
{
473
- pg_atomic_fetch_add_u32 (& map -> usedSize , newSize - oldSize );
474
- return pg_atomic_fetch_add_u32 (& map -> physSize , newSize );
473
+ pg_atomic_fetch_add_u32 (& map -> hdr . usedSize , newSize - oldSize );
474
+ return pg_atomic_fetch_add_u32 (& map -> hdr . physSize , newSize );
475
475
}
476
476
477
477
/*
478
478
* Update logical file size
479
479
*/
480
480
void cfs_extend (FileMap * map , uint32 newSize )
481
481
{
482
- uint32 oldSize = pg_atomic_read_u32 (& map -> virtSize );
483
- while (newSize > oldSize && !pg_atomic_compare_exchange_u32 (& map -> virtSize , & oldSize , newSize ));
482
+ uint32 oldSize = pg_atomic_read_u32 (& map -> hdr . virtSize );
483
+ while (newSize > oldSize && !pg_atomic_compare_exchange_u32 (& map -> hdr . virtSize , & oldSize , newSize ));
484
484
}
485
485
486
486
/*
@@ -584,8 +584,10 @@ void cfs_lock_file(FileMap* map, char const* file_path)
584
584
if (md2 >= 0 )
585
585
{
586
586
/* Recover map. */
587
- if (!cfs_read_file (md2 , map , sizeof (FileMap )))
588
- elog (WARNING , "CFS failed to read file %s: %m" , map_bck_path );
587
+ if (!cfs_read_file (md2 , & map -> hdr , sizeof (map -> hdr )))
588
+ elog (WARNING , "CFS failed to read file header %s: %m" , map_bck_path );
589
+ else if (!cfs_read_file (md2 , map -> inodes , sizeof (map -> inodes )))
590
+ elog (WARNING , "CFS failed to read file inodes %s: %m" , map_bck_path );
589
591
590
592
close (md2 );
591
593
}
@@ -699,9 +701,9 @@ static bool cfs_gc_file(char* map_path, bool background)
699
701
}
700
702
701
703
succeed = true;
702
- usedSize = pg_atomic_read_u32 (& map -> usedSize );
703
- physSize = pg_atomic_read_u32 (& map -> physSize );
704
- virtSize = pg_atomic_read_u32 (& map -> virtSize );
704
+ usedSize = pg_atomic_read_u32 (& map -> hdr . usedSize );
705
+ physSize = pg_atomic_read_u32 (& map -> hdr . physSize );
706
+ virtSize = pg_atomic_read_u32 (& map -> hdr . virtSize );
705
707
706
708
cfs_state -> gc_stat .scannedFiles += 1 ;
707
709
@@ -754,15 +756,20 @@ static bool cfs_gc_file(char* map_path, bool background)
754
756
if (md2 >= 0 )
755
757
{
756
758
/* Recover map */
757
- if (!cfs_read_file (md2 , newMap , sizeof (FileMap )))
759
+ if (!cfs_read_file (md2 , & newMap -> hdr , sizeof (newMap -> hdr )))
758
760
{
759
- elog (WARNING , "CFS failed to read file %s: %m" , map_bck_path );
761
+ elog (WARNING , "CFS failed to read file header %s: %m" , map_bck_path );
762
+ goto Cleanup ;
763
+ }
764
+ if (!cfs_read_file (md2 , newMap -> inodes , sizeof (newMap -> inodes )))
765
+ {
766
+ elog (WARNING , "CFS failed to read file inodes %s: %m" , map_bck_path );
760
767
goto Cleanup ;
761
768
}
762
769
close (md2 );
763
770
md2 = -1 ;
764
- newSize = pg_atomic_read_u32 (& newMap -> usedSize );
765
- virtSize = pg_atomic_read_u32 (& newMap -> virtSize );
771
+ newSize = pg_atomic_read_u32 (& newMap -> hdr . usedSize );
772
+ virtSize = pg_atomic_read_u32 (& newMap -> hdr . virtSize );
766
773
n_pages = virtSize / BLCKSZ ;
767
774
remove_backups = false;
768
775
goto ReplaceMap ;
@@ -791,9 +798,9 @@ static bool cfs_gc_file(char* map_path, bool background)
791
798
}
792
799
793
800
/* Reread variables after locking file */
794
- usedSize = pg_atomic_read_u32 (& map -> usedSize );
795
- physSize = pg_atomic_read_u32 (& map -> physSize );
796
- virtSize = pg_atomic_read_u32 (& map -> virtSize );
801
+ usedSize = pg_atomic_read_u32 (& map -> hdr . usedSize );
802
+ physSize = pg_atomic_read_u32 (& map -> hdr . physSize );
803
+ virtSize = pg_atomic_read_u32 (& map -> hdr . virtSize );
797
804
n_pages = virtSize / BLCKSZ ;
798
805
799
806
md2 = open (map_bck_path , O_CREAT |O_RDWR |PG_BINARY |O_TRUNC , 0600 );
@@ -872,8 +879,17 @@ static bool cfs_gc_file(char* map_path, bool background)
872
879
}
873
880
fd2 = -1 ;
874
881
882
+ pg_atomic_write_u32 (& newMap -> hdr .usedSize , newSize );
883
+ pg_atomic_write_u32 (& newMap -> hdr .physSize , newSize );
884
+ pg_atomic_write_u32 (& newMap -> hdr .virtSize , virtSize );
885
+
875
886
/* Persist copy of map file */
876
- if (!cfs_write_file (md2 , newMap , sizeof (FileMap )))
887
+ if (!cfs_write_file (md2 , & newMap -> hdr , sizeof (newMap -> hdr )))
888
+ {
889
+ elog (WARNING , "CFS failed to write file %s: %m" , map_bck_path );
890
+ goto Cleanup ;
891
+ }
892
+ if (!cfs_write_file (md2 , newMap -> inodes , sizeof (newMap -> inodes )))
877
893
{
878
894
elog (WARNING , "CFS failed to write file %s: %m" , map_bck_path );
879
895
goto Cleanup ;
@@ -963,8 +979,8 @@ static bool cfs_gc_file(char* map_path, bool background)
963
979
* If crash happens at this point, map can be recovered from backup file
964
980
*/
965
981
memcpy (map -> inodes , newMap -> inodes , n_pages * sizeof (inode_t ));
966
- pg_atomic_write_u32 (& map -> usedSize , newSize );
967
- pg_atomic_write_u32 (& map -> physSize , newSize );
982
+ pg_atomic_write_u32 (& map -> hdr . usedSize , newSize );
983
+ pg_atomic_write_u32 (& map -> hdr . physSize , newSize );
968
984
map -> generation += 1 ; /* force all backends to reopen the file */
969
985
970
986
/* Before removing backup files and releasing locks
@@ -1370,8 +1386,8 @@ Datum cfs_compression_ratio(PG_FUNCTION_ARGS)
1370
1386
break ;
1371
1387
}
1372
1388
1373
- virtSize += pg_atomic_read_u32 (& map -> virtSize );
1374
- physSize += pg_atomic_read_u32 (& map -> physSize );
1389
+ virtSize += pg_atomic_read_u32 (& map -> hdr . virtSize );
1390
+ physSize += pg_atomic_read_u32 (& map -> hdr . physSize );
1375
1391
1376
1392
if (cfs_munmap (map ) < 0 )
1377
1393
elog (WARNING , "CFS failed to unmap file %s: %m" , map_path );
@@ -1421,8 +1437,8 @@ Datum cfs_fragmentation(PG_FUNCTION_ARGS)
1421
1437
close (md );
1422
1438
break ;
1423
1439
}
1424
- usedSize += pg_atomic_read_u32 (& map -> usedSize );
1425
- physSize += pg_atomic_read_u32 (& map -> physSize );
1440
+ usedSize += pg_atomic_read_u32 (& map -> hdr . usedSize );
1441
+ physSize += pg_atomic_read_u32 (& map -> hdr . physSize );
1426
1442
1427
1443
if (cfs_munmap (map ) < 0 )
1428
1444
elog (WARNING , "CFS failed to unmap file %s: %m" , map_path );
0 commit comments