Skip to content

Commit 4a8146b

Browse files
committed
Fix race condition in CFS
1 parent 6f6c1a7 commit 4a8146b

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

src/backend/storage/file/cfs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ static bool cfs_gc_file(char* map_path, bool background)
871871
fd2 = -1;
872872

873873
/* Persist copy of map file */
874-
if (!cfs_write_file(md2, &newMap, sizeof(newMap)))
874+
if (!cfs_write_file(md2, &newMap, sizeof(FileMap)))
875875
{
876876
elog(WARNING, "CFS failed to write file %s: %m", map_bck_path);
877877
goto Cleanup;

src/backend/storage/file/fd.c

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,44 +1068,47 @@ LruInsert(File file)
10681068
* overall system file table being full. So, be prepared to release
10691069
* another FD if necessary...
10701070
*/
1071-
vfdP->fd = BasicOpenFile(vfdP->fileName, vfdP->fileFlags,
1072-
vfdP->fileMode);
1073-
if (vfdP->fd < 0)
1074-
{
1075-
DO_DB(elog(LOG, "RE-OPEN FAILED: %m"));
1076-
return -1;
1077-
}
1078-
else
1079-
DO_DB(elog(LOG, "RE_OPEN SUCCESS"));
1080-
1081-
if (vfdP->fileFlags & PG_COMPRESSION)
1071+
if (vfdP->fileFlags & PG_COMPRESSION)
10821072
{
10831073
char* mapFileName = psprintf("%s.cfm", vfdP->fileName);
10841074
vfdP->md = open(mapFileName, vfdP->fileFlags & ~PG_COMPRESSION, vfdP->fileMode);
10851075
pfree(mapFileName);
1086-
10871076
if (vfdP->md < 0)
10881077
{
10891078
elog(LOG, "RE_OPEN MAP FAILED: %d", errno);
1090-
close(vfdP->fd);
1091-
vfdP->fd = VFD_CLOSED;
10921079
return -1;
10931080
}
1094-
10951081
vfdP->map = cfs_mmap(vfdP->md);
10961082
if (vfdP->map == MAP_FAILED)
10971083
{
10981084
elog(LOG, "RE_MAP FAILED: %d", errno);
1099-
close(vfdP->fd);
11001085
close(vfdP->md);
1101-
vfdP->fd = VFD_CLOSED;
1086+
return -1;
1087+
}
1088+
/* We need to copy generation before openning data file */
1089+
vfdP->generation = vfdP->map->generation;
1090+
1091+
vfdP->fd = BasicOpenFile(vfdP->fileName, vfdP->fileFlags,
1092+
vfdP->fileMode);
1093+
if (vfdP->fd < 0)
1094+
{
1095+
DO_DB(elog(LOG, "re-open failed: %m"));
1096+
cfs_munmap(vfdP->map);
1097+
close(vfdP->md);
11021098
vfdP->md = VFD_CLOSED;
11031099
return -1;
11041100
}
11051101
nfile += 2;
11061102
}
11071103
else
11081104
{
1105+
vfdP->fd = BasicOpenFile(vfdP->fileName, vfdP->fileFlags,
1106+
vfdP->fileMode);
1107+
if (vfdP->fd < 0)
1108+
{
1109+
DO_DB(elog(LOG, "RE-OPEN FAILED: %m"));
1110+
return -1;
1111+
}
11091112

11101113
/*
11111114
* Seek to the right position. We need no special case for seekPos
@@ -1553,6 +1556,7 @@ FileClose(File file)
15531556

15541557
if (close(vfdP->md))
15551558
elog(ERROR, "could not close map file \"%s.cfm\": %m", vfdP->fileName);
1559+
vfdP->md = VFD_CLOSED;
15561560
--nfile;
15571561
}
15581562
--nfile;

src/include/storage/cfs.h

Lines changed: 1 addition & 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.29"
9+
#define CFS_VERSION "0.32"
1010

1111
#define CFS_GC_LOCK 0x10000000
1212

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy