Hi hackers, there are a very long discuss about TDE, and we agreed on that if the temporary file I/O can be aligned to some fixed size, it will be easier to use some kind of encryption algorithm.
discuss: https://www.postgresql.org/message-id/20211025155814.GD20998%40tamriel.snowman.net This patch adjust file->curOffset and file->pos before the real IO to ensure the start offset is aligned.
From e04c2595711998d2c3eb4546e98a38d340200949 Mon Sep 17 00:00:00 2001 From: Sasasu <i...@sasa.su> Date: Fri, 26 Nov 2021 00:12:14 +0800 Subject: [PATCH] buffile: ensure start offset is aligned with BLCKSZ. If we can ensure that all IO is aligned with BLCKSZ, some other work (TDE recently and maybe AIO in future) will like it. this commit ensure all IO on buffile is aligned with BLCKSZ by careful adjusting file->curOffset and file->pos. Signed-off-by: Sasasu <i...@sasa.su> --- src/backend/storage/file/buffile.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/backend/storage/file/buffile.c b/src/backend/storage/file/buffile.c index b673150dbe..3be08eb723 100644 --- a/src/backend/storage/file/buffile.c +++ b/src/backend/storage/file/buffile.c @@ -430,6 +430,9 @@ BufFileLoadBuffer(BufFile *file) { File thisfile; + /* the offset of the buffile is aligned with BLCKSZ */ + Assert(file->curOffset % BLCKSZ == 0); + /* * Advance to next component file if necessary and possible. */ @@ -437,7 +440,7 @@ BufFileLoadBuffer(BufFile *file) file->curFile + 1 < file->numFiles) { file->curFile++; - file->curOffset = 0L; + file->curOffset -= MAX_PHYSICAL_FILESIZE; } /* @@ -506,6 +509,9 @@ BufFileDumpBuffer(BufFile *file) if ((off_t) bytestowrite > availbytes) bytestowrite = (int) availbytes; + /* and the buffer is aligned with BLCKSZ */ + Assert(file->curOffset % BLCKSZ == 0); + thisfile = file->files[file->curFile]; bytestowrite = FileWrite(thisfile, file->buffer.data + wpos, @@ -564,11 +570,17 @@ BufFileRead(BufFile *file, void *ptr, size_t size) if (file->pos >= file->nbytes) { /* Try to load more data into buffer. */ - file->curOffset += file->pos; - file->pos = 0; + int offsetInBlock = file->curOffset % BLCKSZ; file->nbytes = 0; + file->pos += offsetInBlock; + file->curOffset -= offsetInBlock; + + /* pos out of current block, move to next block */ + if (file->pos >= BLCKSZ) + file->pos -= BLCKSZ, file->curOffset += BLCKSZ; + BufFileLoadBuffer(file); - if (file->nbytes <= 0) + if (file->nbytes <= 0 || (file->nbytes == file->pos && file->nbytes != BLCKSZ)) break; /* no more data available */ } -- 2.34.1
OpenPGP_signature
Description: OpenPGP digital signature