Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 06f7235

Browse files
committed
Recover CFS map file
1 parent 3ede63c commit 06f7235

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

src/backend/storage/file/cfs.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,49 @@ void cfs_gc_segment(char const* fileName)
14991499
}
15001500

15011501

1502+
void cfs_recover_map(FileMap* map)
1503+
{
1504+
int i;
1505+
uint32 physSize;
1506+
uint32 virtSize;
1507+
uint32 usedSize = 0;
1508+
1509+
physSize = pg_atomic_read_u32(&map->hdr.physSize);
1510+
virtSize = pg_atomic_read_u32(&map->hdr.virtSize);
1511+
1512+
for (i = 0; i < RELSEG_SIZE; i++)
1513+
{
1514+
inode_t inode = map->inodes[i];
1515+
int size = CFS_INODE_SIZE(inode);
1516+
if (size != 0)
1517+
{
1518+
uint32 offs = CFS_INODE_OFFS(inode);
1519+
if (offs + size > physSize)
1520+
{
1521+
physSize = offs + size;
1522+
}
1523+
if ((i+1)*BLCKSZ > virtSize)
1524+
{
1525+
virtSize = (i+1)*BLCKSZ;
1526+
}
1527+
usedSize += size;
1528+
}
1529+
if (usedSize != pg_atomic_read_u32(&map->hdr.usedSize))
1530+
{
1531+
pg_atomic_write_u32(&map->hdr.usedSize, usedSize);
1532+
}
1533+
if (physSize != pg_atomic_read_u32(&map->hdr.physSize))
1534+
{
1535+
pg_atomic_write_u32(&map->hdr.physSize, physSize);
1536+
}
1537+
if (virtSize != pg_atomic_read_u32(&map->hdr.virtSize))
1538+
{
1539+
pg_atomic_write_u32(&map->hdr.virtSize, virtSize);
1540+
}
1541+
}
1542+
}
1543+
1544+
15021545
Datum cfs_gc_activity_processed_bytes(PG_FUNCTION_ARGS)
15031546
{
15041547
PG_RETURN_INT64(cfs_state->gc_stat.processedBytes);

src/backend/storage/file/fd.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -984,10 +984,10 @@ LruDelete(File file)
984984
if (vfdP->fileFlags & PG_COMPRESSION)
985985
{
986986
if (cfs_munmap(vfdP->map))
987-
elog(ERROR, "could not unmap file \"%s.cfm\": %m", vfdP->fileName);
987+
elog(ERROR, "CFS: could not unmap file \"%s.cfm\": %m", vfdP->fileName);
988988

989989
if (close(vfdP->md))
990-
elog(ERROR, "could not close map file \"%s.cfm\": %m", vfdP->fileName);
990+
elog(ERROR, "CFS: could not close map file \"%s.cfm\": %m", vfdP->fileName);
991991

992992
vfdP->md = VFD_CLOSED;
993993
nfile -= 2;
@@ -1075,13 +1075,13 @@ LruInsert(File file)
10751075
pfree(mapFileName);
10761076
if (vfdP->md < 0)
10771077
{
1078-
elog(LOG, "RE_OPEN MAP FAILED: %d", errno);
1078+
elog(LOG, "CFS: reopen map failed: %d", errno);
10791079
return -1;
10801080
}
10811081
vfdP->map = cfs_mmap(vfdP->md);
10821082
if (vfdP->map == MAP_FAILED)
10831083
{
1084-
elog(LOG, "RE_MAP FAILED: %d", errno);
1084+
elog(LOG, "CFS: remap failed: %d", errno);
10851085
close(vfdP->md);
10861086
return -1;
10871087
}
@@ -1093,7 +1093,7 @@ LruInsert(File file)
10931093
vfdP->fileMode);
10941094
if (vfdP->fd < 0)
10951095
{
1096-
DO_DB(elog(LOG, "re-open failed: %m"));
1096+
DO_DB(elog(LOG, "CFS: reopen failed: %m"));
10971097
cfs_munmap(vfdP->map);
10981098
close(vfdP->md);
10991099
vfdP->md = VFD_CLOSED;
@@ -1347,26 +1347,31 @@ PathNameOpenFile(FileName fileName, int fileFlags, int fileMode)
13471347
if (vfdP->md < 0)
13481348
{
13491349
save_errno = errno;
1350-
elog(LOG, "RE_OPEN MAP FAILED: %d", errno);
1350+
elog(LOG, "CFS: open map failed: %d", errno);
13511351
goto io_error;
13521352
}
13531353
vfdP->map = cfs_mmap(vfdP->md);
13541354
if (vfdP->map == MAP_FAILED)
13551355
{
13561356
save_errno = errno;
1357-
elog(LOG, "RE_MAP FAILED: %d", errno);
1357+
elog(LOG, "CFS: map failed: %d", errno);
13581358
close(vfdP->md);
13591359
goto io_error;
13601360
}
13611361
/* We need to copy generation before openning data file */
13621362
vfdP->generation = vfdP->map->generation;
13631363
pg_read_barrier();
13641364

1365+
if (InRecovery)
1366+
{
1367+
cfs_recover_map(vfdP->map);
1368+
}
1369+
13651370
vfdP->fd = BasicOpenFile(fileName, fileFlags, fileMode);
13661371
if (vfdP->fd < 0)
13671372
{
13681373
save_errno = errno;
1369-
DO_DB(elog(LOG, "re-open failed: %m"));
1374+
DO_DB(elog(LOG, "CFS: open failed: %m"));
13701375
cfs_munmap(vfdP->map);
13711376
close(vfdP->md);
13721377
vfdP->md = VFD_CLOSED;
@@ -1562,10 +1567,10 @@ FileClose(File file)
15621567
if (vfdP->fileFlags & PG_COMPRESSION)
15631568
{
15641569
if (cfs_munmap(vfdP->map))
1565-
elog(ERROR, "could not unmap file \"%s.cfm\": %m", vfdP->fileName);
1570+
elog(ERROR, "CFS: could not unmap file \"%s.cfm\": %m", vfdP->fileName);
15661571

15671572
if (close(vfdP->md))
1568-
elog(ERROR, "could not close map file \"%s.cfm\": %m", vfdP->fileName);
1573+
elog(ERROR, "CFS: could not close map file \"%s.cfm\": %m", vfdP->fileName);
15691574
vfdP->md = VFD_CLOSED;
15701575
--nfile;
15711576
}
@@ -1610,7 +1615,7 @@ FileClose(File file)
16101615
if (vfdP->fileFlags & PG_COMPRESSION) {
16111616
char* mapFileName = psprintf("%s.cfm", vfdP->fileName);
16121617
if (unlink(mapFileName))
1613-
elog(LOG, "could not unlink file \"%s\": %m", mapFileName);
1618+
elog(LOG, "CFS: could not unlink file \"%s\": %m", mapFileName);
16141619
pfree(mapFileName);
16151620
}
16161621

@@ -1976,7 +1981,7 @@ FileWrite(File file, char *buffer, int amount)
19761981
*/
19771982
pos = cfs_alloc_page(map, CFS_INODE_SIZE(inode), compressedSize);
19781983
if (pos > pos + compressedSize) {
1979-
elog(ERROR, "CFS segment file exceeed 4Gb limit");
1984+
elog(ERROR, "CFS: segment file exceeed 4Gb limit");
19801985
}
19811986

19821987
inode = CFS_INODE(compressedSize, pos);
@@ -2037,7 +2042,7 @@ FileWrite(File file, char *buffer, int amount)
20372042
}
20382043
else
20392044
{
2040-
elog(LOG, "Write to file %s block %u position %u size %u failed with code %d: %m",
2045+
elog(LOG, "CFS: write to file %s block %u position %u size %u failed with code %d: %m",
20412046
VfdCache[file].fileName, (uint32)(VfdCache[file].seekPos / BLCKSZ),
20422047
(uint32)seekPos, amount, returnCode);
20432048
returnCode = 0;

src/include/storage/cfs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "port/atomics.h"
77
#include "storage/rijndael.h"
88

9-
#define CFS_VERSION "0.42"
9+
#define CFS_VERSION "0.43"
1010

1111
#define CFS_GC_LOCK 0x10000000
1212

@@ -133,6 +133,7 @@ void cfs_encrypt(const char* fname, void* block, uint32 offs, uint32 size);
133133
void cfs_decrypt(const char* fname, void* block, uint32 offs, uint32 size);
134134

135135
void cfs_gc_segment(char const* name);
136+
void cfs_recover_map(FileMap* map);
136137

137138
extern CfsState* cfs_state;
138139

0 commit comments

Comments
 (0)