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

Commit 55eac74

Browse files
committed
Rewrite check for fiel sizse overflow
1 parent e18cea1 commit 55eac74

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

src/backend/storage/file/cfs.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -470,14 +470,16 @@ int cfs_munmap(FileMap* map)
470470
*/
471471
uint32 cfs_alloc_page(FileMap* map, uint32 oldSize, uint32 newSize)
472472
{
473-
pg_atomic_fetch_add_u32(&map->hdr.usedSize, newSize - oldSize);
474-
return pg_atomic_fetch_add_u32(&map->hdr.physSize, newSize);
475-
}
473+
uint32 oldPhysSize = pg_atomic_read_u32(&map->hdr.physSize);
474+
uint32 newPhysSize;
476475

477-
void cfs_undo_alloc_page(FileMap* map, uint32 oldSize, uint32 newSize)
478-
{
479-
pg_atomic_fetch_sub_u32(&map->hdr.usedSize, newSize - oldSize);
480-
pg_atomic_fetch_sub_u32(&map->hdr.physSize, newSize);
476+
do {
477+
newPhysSize = oldPhysSize + newSize;
478+
if (oldPhysSize > newPhysSize)
479+
elog(ERROR, "CFS: segment file exceed 4Gb limit");
480+
} while (!pg_atomic_compare_exchange_u32(&map->hdr.physSize, &oldPhysSize, newPhysSize));
481+
482+
return oldPhysSize;
481483
}
482484

483485
/*

src/backend/storage/file/fd.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,10 +1980,6 @@ FileWrite(File file, char *buffer, int amount)
19801980
* because we want to write all updated pages sequentially
19811981
*/
19821982
pos = cfs_alloc_page(map, CFS_INODE_SIZE(inode), compressedSize);
1983-
if (pos > CFS_RED_LINE) {
1984-
cfs_undo_alloc_page(map, CFS_INODE_SIZE(inode), compressedSize);
1985-
elog(ERROR, "CFS: segment file exceed 4Gb limit");
1986-
}
19871983

19881984
inode = CFS_INODE(compressedSize, pos);
19891985
buffer = compressedBuffer;
@@ -2110,7 +2106,7 @@ FileWrite(File file, char *buffer, int amount)
21102106
{
21112107
elog(LOG, "CFS: backend %d forced to perform GC on file %s block %u because it's size exceed %u bytes",
21122108
MyProcPid, VfdCache[file].fileName, (uint32)(VfdCache[file].seekPos / BLCKSZ), pos);
2113-
cfs_gc_segment(VfdCache[file].fileName, pos + amount < CFS_YELLOW_LINE);
2109+
cfs_gc_segment(VfdCache[file].fileName, pos + amount < CFS_RED_LINE);
21142110
}
21152111
}
21162112
return returnCode;

src/include/storage/cfs.h

Lines changed: 2 additions & 4 deletions
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.43"
9+
#define CFS_VERSION "0.45"
1010

1111
#define CFS_GC_LOCK 0x10000000
1212

@@ -52,8 +52,7 @@ typedef uint64 inode_t;
5252
#define CFS_INODE(size,offs) (((inode_t)(size) << 32) | (offs))
5353

5454
#define CFS_IMPLICIT_GC_THRESHOLD 0x80000000U /* 2Gb */
55-
#define CFS_YELLOW_LINE 0xC0000000U /* 3Gb */
56-
#define CFS_RED_LINE 0xFFF00000U /* 4Gb - page_size*100 */
55+
#define CFS_RED_LINE 0xC0000000U /* 3Gb */
5756

5857
size_t cfs_compress(void* dst, size_t dst_size, void const* src, size_t src_size);
5958
size_t cfs_decompress(void* dst, size_t dst_size, void const* src, size_t src_size);
@@ -125,7 +124,6 @@ typedef struct
125124
void cfs_lock_file(FileMap* map, char const* path);
126125
void cfs_unlock_file(FileMap* map);
127126
uint32 cfs_alloc_page(FileMap* map, uint32 oldSize, uint32 newSize);
128-
void cfs_undo_alloc_page(FileMap* map, uint32 oldSize, uint32 newSize);
129127
void cfs_extend(FileMap* map, uint32 pos);
130128
bool cfs_control_gc(bool enabled);
131129
int cfs_msync(FileMap* map);

0 commit comments

Comments
 (0)