PostgreSQL Source Code  git master
fd.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * fd.h
4  * Virtual file descriptor definitions.
5  *
6  *
7  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/storage/fd.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 
15 /*
16  * calls:
17  *
18  * File {Close, Read, Write, Size, Sync}
19  * {Path Name Open, Allocate, Free} File
20  *
21  * These are NOT JUST RENAMINGS OF THE UNIX ROUTINES.
22  * Use them for all file activity...
23  *
24  * File fd;
25  * fd = PathNameOpenFile("foo", O_RDONLY);
26  *
27  * AllocateFile();
28  * FreeFile();
29  *
30  * Use AllocateFile, not fopen, if you need a stdio file (FILE*); then
31  * use FreeFile, not fclose, to close it. AVOID using stdio for files
32  * that you intend to hold open for any length of time, since there is
33  * no way for them to share kernel file descriptors with other files.
34  *
35  * Likewise, use AllocateDir/FreeDir, not opendir/closedir, to allocate
36  * open directories (DIR*), and OpenTransientFile/CloseTransientFile for an
37  * unbuffered file descriptor.
38  *
39  * If you really can't use any of the above, at least call AcquireExternalFD
40  * or ReserveExternalFD to report any file descriptors that are held for any
41  * length of time. Failure to do so risks unnecessary EMFILE errors.
42  */
43 #ifndef FD_H
44 #define FD_H
45 
46 #include <dirent.h>
47 
49 {
53 
54 typedef int File;
55 
56 
57 /* GUC parameter */
59 extern PGDLLIMPORT bool data_sync_retry;
61 
62 /*
63  * This is private to fd.c, but exported for save/restore_backend_variables()
64  */
65 extern PGDLLIMPORT int max_safe_fds;
66 
67 /*
68  * On Windows, we have to interpret EACCES as possibly meaning the same as
69  * ENOENT, because if a file is unlinked-but-not-yet-gone on that platform,
70  * that's what you get. Ugh. This code is designed so that we don't
71  * actually believe these cases are okay without further evidence (namely,
72  * a pending fsync request getting canceled ... see ProcessSyncRequests).
73  */
74 #ifndef WIN32
75 #define FILE_POSSIBLY_DELETED(err) ((err) == ENOENT)
76 #else
77 #define FILE_POSSIBLY_DELETED(err) ((err) == ENOENT || (err) == EACCES)
78 #endif
79 
80 /*
81  * O_DIRECT is not standard, but almost every Unix has it. We translate it
82  * to the appropriate Windows flag in src/port/open.c. We simulate it with
83  * fcntl(F_NOCACHE) on macOS inside fd.c's open() wrapper. We use the name
84  * PG_O_DIRECT rather than defining O_DIRECT in that case (probably not a good
85  * idea on a Unix).
86  */
87 #if defined(O_DIRECT)
88 #define PG_O_DIRECT O_DIRECT
89 #elif defined(F_NOCACHE)
90 #define PG_O_DIRECT 0x80000000
91 #define PG_O_DIRECT_USE_F_NOCACHE
92 #else
93 #define PG_O_DIRECT 0
94 #endif
95 
96 /*
97  * prototypes for functions in fd.c
98  */
99 
100 /* Operations on virtual Files --- equivalent to Unix kernel file ops */
101 extern File PathNameOpenFile(const char *fileName, int fileFlags);
102 extern File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
103 extern File OpenTemporaryFile(bool interXact);
104 extern void FileClose(File file);
105 extern int FilePrefetch(File file, off_t offset, off_t amount, uint32 wait_event_info);
106 extern int FileRead(File file, void *buffer, size_t amount, off_t offset, uint32 wait_event_info);
107 extern int FileWrite(File file, const void *buffer, size_t amount, off_t offset, uint32 wait_event_info);
108 extern int FileSync(File file, uint32 wait_event_info);
109 extern off_t FileSize(File file);
110 extern int FileTruncate(File file, off_t offset, uint32 wait_event_info);
111 extern void FileWriteback(File file, off_t offset, off_t nbytes, uint32 wait_event_info);
112 extern char *FilePathName(File file);
113 extern int FileGetRawDesc(File file);
114 extern int FileGetRawFlags(File file);
115 extern mode_t FileGetRawMode(File file);
116 
117 /* Operations used for sharing named temporary files */
118 extern File PathNameCreateTemporaryFile(const char *path, bool error_on_failure);
119 extern File PathNameOpenTemporaryFile(const char *path, int mode);
120 extern bool PathNameDeleteTemporaryFile(const char *path, bool error_on_failure);
121 extern void PathNameCreateTemporaryDir(const char *basedir, const char *directory);
122 extern void PathNameDeleteTemporaryDir(const char *dirname);
123 extern void TempTablespacePath(char *path, Oid tablespace);
124 
125 /* Operations that allow use of regular stdio --- USE WITH CAUTION */
126 extern FILE *AllocateFile(const char *name, const char *mode);
127 extern int FreeFile(FILE *file);
128 
129 /* Operations that allow use of pipe streams (popen/pclose) */
130 extern FILE *OpenPipeStream(const char *command, const char *mode);
131 extern int ClosePipeStream(FILE *file);
132 
133 /* Operations to allow use of the <dirent.h> library routines */
134 extern DIR *AllocateDir(const char *dirname);
135 extern struct dirent *ReadDir(DIR *dir, const char *dirname);
136 extern struct dirent *ReadDirExtended(DIR *dir, const char *dirname,
137  int elevel);
138 extern int FreeDir(DIR *dir);
139 
140 /* Operations to allow use of a plain kernel FD, with automatic cleanup */
141 extern int OpenTransientFile(const char *fileName, int fileFlags);
142 extern int OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
143 extern int CloseTransientFile(int fd);
144 
145 /* If you've really really gotta have a plain kernel FD, use this */
146 extern int BasicOpenFile(const char *fileName, int fileFlags);
147 extern int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
148 
149 /* Use these for other cases, and also for long-lived BasicOpenFile FDs */
150 extern bool AcquireExternalFD(void);
151 extern void ReserveExternalFD(void);
152 extern void ReleaseExternalFD(void);
153 
154 /* Make a directory with default permissions */
155 extern int MakePGDirectory(const char *directoryName);
156 
157 /* Miscellaneous support routines */
158 extern void InitFileAccess(void);
159 extern void InitTemporaryFileAccess(void);
160 extern void set_max_safe_fds(void);
161 extern void closeAllVfds(void);
162 extern void SetTempTablespaces(Oid *tableSpaces, int numSpaces);
163 extern bool TempTablespacesAreSet(void);
164 extern int GetTempTablespaces(Oid *tableSpaces, int numSpaces);
165 extern Oid GetNextTempTableSpace(void);
166 extern void AtEOXact_Files(bool isCommit);
167 extern void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid,
168  SubTransactionId parentSubid);
169 extern void RemovePgTempFiles(void);
170 extern void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok,
171  bool unlink_all);
172 extern bool looks_like_temp_rel_name(const char *name);
173 
174 extern int pg_fsync(int fd);
175 extern int pg_fsync_no_writethrough(int fd);
176 extern int pg_fsync_writethrough(int fd);
177 extern int pg_fdatasync(int fd);
178 extern void pg_flush_data(int fd, off_t offset, off_t nbytes);
179 extern int pg_truncate(const char *path, off_t length);
180 extern void fsync_fname(const char *fname, bool isdir);
181 extern int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel);
182 extern int durable_rename(const char *oldfile, const char *newfile, int elevel);
183 extern int durable_unlink(const char *fname, int elevel);
184 extern void SyncDataDirectory(void);
185 extern int data_sync_elevel(int elevel);
186 
187 /* Filename components */
188 #define PG_TEMP_FILES_DIR "pgsql_tmp"
189 #define PG_TEMP_FILE_PREFIX "pgsql_tmp"
190 
191 #endif /* FD_H */
unsigned int uint32
Definition: c.h:490
#define PGDLLIMPORT
Definition: c.h:1303
uint32 SubTransactionId
Definition: c.h:640
const char * name
Definition: encode.c:571
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2709
PGDLLIMPORT int max_safe_fds
Definition: fd.c:157
void pg_flush_data(int fd, off_t offset, off_t nbytes)
Definition: fd.c:458
int FileGetRawDesc(File file)
Definition: fd.c:2277
int MakePGDirectory(const char *directoryName)
Definition: fd.c:3713
int FreeDir(DIR *dir)
Definition: fd.c:2761
void FileWriteback(File file, off_t offset, off_t nbytes, uint32 wait_event_info)
Definition: fd.c:2011
int pg_fsync_no_writethrough(int fd)
Definition: fd.c:411
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2383
int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:1015
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:688
int GetTempTablespaces(Oid *tableSpaces, int numSpaces)
Definition: fd.c:2893
File PathNameOpenTemporaryFile(const char *path, int mode)
Definition: fd.c:1806
int pg_fdatasync(int fd)
Definition: fd.c:444
int FileSync(File file, uint32 wait_event_info)
Definition: fd.c:2188
int CloseTransientFile(int fd)
Definition: fd.c:2609
int BasicOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:993
void closeAllVfds(void)
Definition: fd.c:2820
File PathNameCreateTemporaryFile(const char *path, bool error_on_failure)
Definition: fd.c:1766
void PathNameDeleteTemporaryDir(const char *dirname)
Definition: fd.c:1596
int ClosePipeStream(FILE *file)
Definition: fd.c:2791
PGDLLIMPORT bool data_sync_retry
Definition: fd.c:160
void AtEOXact_Files(bool isCommit)
Definition: fd.c:2965
int FileGetRawFlags(File file)
Definition: fd.c:2287
int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel)
Definition: fd.c:3597
int FreeFile(FILE *file)
Definition: fd.c:2581
PGDLLIMPORT int recovery_init_sync_method
Definition: fd.c:163
mode_t FileGetRawMode(File file)
Definition: fd.c:2297
int pg_fsync_writethrough(int fd)
Definition: fd.c:423
void FileClose(File file)
Definition: fd.c:1883
FILE * OpenPipeStream(const char *command, const char *mode)
Definition: fd.c:2486
void ReleaseExternalFD(void)
Definition: fd.c:1152
void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok, bool unlink_all)
Definition: fd.c:3133
void RemovePgTempFiles(void)
Definition: fd.c:3074
bool TempTablespacesAreSet(void)
Definition: fd.c:2878
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:662
int FilePrefetch(File file, off_t offset, off_t amount, uint32 wait_event_info)
Definition: fd.c:1983
int data_sync_elevel(int elevel)
Definition: fd.c:3736
File PathNameOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:1488
int FileWrite(File file, const void *buffer, size_t amount, off_t offset, uint32 wait_event_info)
Definition: fd.c:2090
Oid GetNextTempTableSpace(void)
Definition: fd.c:2911
File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:1501
bool AcquireExternalFD(void)
Definition: fd.c:1099
int FileRead(File file, void *buffer, size_t amount, off_t offset, uint32 wait_event_info)
Definition: fd.c:2034
struct dirent * ReadDirExtended(DIR *dir, const char *dirname, int elevel)
Definition: fd.c:2724
void InitFileAccess(void)
Definition: fd.c:809
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1629
int durable_unlink(const char *fname, int elevel)
Definition: fd.c:778
PGDLLIMPORT int max_files_per_process
Definition: fd.c:144
void ReserveExternalFD(void)
Definition: fd.c:1134
char * FilePathName(File file)
Definition: fd.c:2261
bool looks_like_temp_rel_name(const char *name)
Definition: fd.c:3249
bool PathNameDeleteTemporaryFile(const char *path, bool error_on_failure)
Definition: fd.c:1837
void set_max_safe_fds(void)
Definition: fd.c:950
int pg_fsync(int fd)
Definition: fd.c:356
int File
Definition: fd.h:54
void PathNameCreateTemporaryDir(const char *basedir, const char *directory)
Definition: fd.c:1565
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2932
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2433
void InitTemporaryFileAccess(void)
Definition: fd.c:839
int OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:2442
RecoveryInitSyncMethod
Definition: fd.h:49
@ RECOVERY_INIT_SYNC_METHOD_SYNCFS
Definition: fd.h:51
@ RECOVERY_INIT_SYNC_METHOD_FSYNC
Definition: fd.h:50
void SyncDataDirectory(void)
Definition: fd.c:3344
off_t FileSize(File file)
Definition: fd.c:2209
int FileTruncate(File file, off_t offset, uint32 wait_event_info)
Definition: fd.c:2226
int pg_truncate(const char *path, off_t length)
Definition: fd.c:631
void SetTempTablespaces(Oid *tableSpaces, int numSpaces)
Definition: fd.c:2849
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2643
void TempTablespacePath(char *path, Oid tablespace)
Definition: fd.c:1684
static char * basedir
static PgChecksumMode mode
Definition: pg_checksums.c:65
char * tablespace
Definition: pgbench.c:226
unsigned int Oid
Definition: postgres_ext.h:31
static int fd(const char *x, int i)
Definition: preproc-init.c:105
Definition: dirent.c:26
Definition: dirent.h:10
static const char * directory
Definition: zic.c:634