Skip to content

Commit 225501c

Browse files
committed
Clean up assorted messiness around AllocateDir() usage.
This patch fixes a couple of low-probability bugs that could lead to reporting an irrelevant errno value (and hence possibly a wrong SQLSTATE) concerning directory-open or file-open failures. It also fixes places where we took shortcuts in reporting such errors, either by using elog instead of ereport or by using ereport but forgetting to specify an errcode. And it eliminates a lot of just plain redundant error-handling code. In service of all this, export fd.c's formerly-static function ReadDirExtended, so that external callers can make use of the coding pattern dir = AllocateDir(path); while ((de = ReadDirExtended(dir, path, LOG)) != NULL) if they'd like to treat directory-open failures as mere LOG conditions rather than errors. Also fix FreeDir to be a no-op if we reach it with dir == NULL, as such a coding pattern would cause. Then, remove code at many call sites that was throwing an error or log message for AllocateDir failure, as ReadDir or ReadDirExtended can handle that job just fine. Aside from being a net code savings, this gets rid of a lot of not-quite-up-to-snuff reports, as mentioned above. (In some places these changes result in replacing a custom error message such as "could not open tablespace directory" with more generic wording "could not open directory", but it was agreed that the custom wording buys little as long as we report the directory name.) In some other call sites where we can't just remove code, change the error reports to be fully project-style-compliant. Also reorder code in restoreTwoPhaseData that was acquiring a lock between AllocateDir and ReadDir; in the unlikely but surely not impossible case that LWLockAcquire changes errno, AllocateDir failures would be misreported. There is no great value in opening the directory before acquiring TwoPhaseStateLock, so just do it in the other order. Also fix CheckXLogRemoved to guarantee that it preserves errno, as quite a number of call sites are implicitly assuming. (Again, it's unlikely but I think not impossible that errno could change during a SpinLockAcquire. If so, this function was broken for its own purposes as well as breaking callers.) And change a few places that were using not-per-project-style messages, such as "could not read directory" when "could not open directory" is more correct. Back-patch the exporting of ReadDirExtended, in case we have occasion to back-patch some fix that makes use of it; it's not needed right now but surely making it global is pretty harmless. Also back-patch the restoreTwoPhaseData and CheckXLogRemoved fixes. The rest of this is essentially cosmetic and need not get back-patched. Michael Paquier, with a bit of additional work by me Discussion: https://postgr.es/m/CAB7nPqRpOCxjiirHmebEFhXVTK7V5Jvw4bz82p7Oimtsm3TyZA@mail.gmail.com
1 parent e73981c commit 225501c

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

src/backend/access/transam/xlog.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3711,10 +3711,16 @@ PreallocXlogFiles(XLogRecPtr endptr)
37113711
* existed while the server has been running, as this function always
37123712
* succeeds if no WAL segments have been removed since startup.
37133713
* 'tli' is only used in the error message.
3714+
*
3715+
* Note: this function guarantees to keep errno unchanged on return.
3716+
* This supports callers that use this to possibly deliver a better
3717+
* error message about a missing file, while still being able to throw
3718+
* a normal file-access error afterwards, if this does return.
37143719
*/
37153720
void
37163721
CheckXLogRemoved(XLogSegNo segno, TimeLineID tli)
37173722
{
3723+
int save_errno = errno;
37183724
/* use volatile pointer to prevent code rearrangement */
37193725
volatile XLogCtlData *xlogctl = XLogCtl;
37203726
XLogSegNo lastRemovedSegNo;
@@ -3728,11 +3734,13 @@ CheckXLogRemoved(XLogSegNo segno, TimeLineID tli)
37283734
char filename[MAXFNAMELEN];
37293735

37303736
XLogFileName(filename, tli, segno);
3737+
errno = save_errno;
37313738
ereport(ERROR,
37323739
(errcode_for_file_access(),
37333740
errmsg("requested WAL segment %s has already been removed",
37343741
filename)));
37353742
}
3743+
errno = save_errno;
37363744
}
37373745

37383746
/*

src/backend/storage/file/fd.c

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ static int FileAccess(File file);
296296
static File OpenTemporaryFileInTablespace(Oid tblspcOid, bool rejectError);
297297
static bool reserveAllocatedDesc(void);
298298
static int FreeDesc(AllocateDesc *desc);
299-
static struct dirent *ReadDirExtended(DIR *dir, const char *dirname, int elevel);
300299

301300
static void AtProcExit_Files(int code, Datum arg);
302301
static void CleanupTempFiles(bool isProcExit);
@@ -2083,6 +2082,10 @@ CloseTransientFile(int fd)
20832082
* necessary to open the directory, and with closing it after an elog.
20842083
* When done, call FreeDir rather than closedir.
20852084
*
2085+
* Returns NULL, with errno set, on failure. Note that failure detection
2086+
* is commonly left to the following call of ReadDir or ReadDirExtended;
2087+
* see the comments for ReadDir.
2088+
*
20862089
* Ideally this should be the *only* direct call of opendir() in the backend.
20872090
*/
20882091
DIR *
@@ -2145,8 +2148,8 @@ AllocateDir(const char *dirname)
21452148
* FreeDir(dir);
21462149
*
21472150
* since a NULL dir parameter is taken as indicating AllocateDir failed.
2148-
* (Make sure errno hasn't been changed since AllocateDir if you use this
2149-
* shortcut.)
2151+
* (Make sure errno isn't changed between AllocateDir and ReadDir if you
2152+
* use this shortcut.)
21502153
*
21512154
* The pathname passed to AllocateDir must be passed to this routine too,
21522155
* but it is only used for error reporting.
@@ -2158,10 +2161,15 @@ ReadDir(DIR *dir, const char *dirname)
21582161
}
21592162

21602163
/*
2161-
* Alternate version that allows caller to specify the elevel for any
2162-
* error report. If elevel < ERROR, returns NULL on any error.
2164+
* Alternate version of ReadDir that allows caller to specify the elevel
2165+
* for any error report (whether it's reporting an initial failure of
2166+
* AllocateDir or a subsequent directory read failure).
2167+
*
2168+
* If elevel < ERROR, returns NULL after any error. With the normal coding
2169+
* pattern, this will result in falling out of the loop immediately as
2170+
* though the directory contained no (more) entries.
21632171
*/
2164-
static struct dirent *
2172+
struct dirent *
21652173
ReadDirExtended(DIR *dir, const char *dirname, int elevel)
21662174
{
21672175
struct dirent *dent;
@@ -2191,14 +2199,22 @@ ReadDirExtended(DIR *dir, const char *dirname, int elevel)
21912199
/*
21922200
* Close a directory opened with AllocateDir.
21932201
*
2194-
* Note we do not check closedir's return value --- it is up to the caller
2195-
* to handle close errors.
2202+
* Returns closedir's return value (with errno set if it's not 0).
2203+
* Note we do not check the return value --- it is up to the caller
2204+
* to handle close errors if wanted.
2205+
*
2206+
* Does nothing if dir == NULL; we assume that directory open failure was
2207+
* already reported if desired.
21962208
*/
21972209
int
21982210
FreeDir(DIR *dir)
21992211
{
22002212
int i;
22012213

2214+
/* Nothing to do if AllocateDir failed */
2215+
if (dir == NULL)
2216+
return 0;
2217+
22022218
DO_DB(elog(LOG, "FreeDir: Allocated %d", numAllocatedDescs));
22032219

22042220
/* Remove dir from list of allocated dirs, if it's present */

src/include/storage/fd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ extern int ClosePipeStream(FILE *file);
8787
/* Operations to allow use of the <dirent.h> library routines */
8888
extern DIR *AllocateDir(const char *dirname);
8989
extern struct dirent *ReadDir(DIR *dir, const char *dirname);
90+
extern struct dirent *ReadDirExtended(DIR *dir, const char *dirname,
91+
int elevel);
9092
extern int FreeDir(DIR *dir);
9193

9294
/* Operations to allow use of a plain kernel FD, with automatic cleanup */

0 commit comments

Comments
 (0)