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-2021, 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 struct iovec; /* avoid including port/pg_iovec.h here */
55 
56 typedef int File;
57 
58 
59 /* GUC parameter */
61 extern PGDLLIMPORT bool data_sync_retry;
62 extern int recovery_init_sync_method;
63 
64 /*
65  * This is private to fd.c, but exported for save/restore_backend_variables()
66  */
67 extern int max_safe_fds;
68 
69 /*
70  * On Windows, we have to interpret EACCES as possibly meaning the same as
71  * ENOENT, because if a file is unlinked-but-not-yet-gone on that platform,
72  * that's what you get. Ugh. This code is designed so that we don't
73  * actually believe these cases are okay without further evidence (namely,
74  * a pending fsync request getting canceled ... see ProcessSyncRequests).
75  */
76 #ifndef WIN32
77 #define FILE_POSSIBLY_DELETED(err) ((err) == ENOENT)
78 #else
79 #define FILE_POSSIBLY_DELETED(err) ((err) == ENOENT || (err) == EACCES)
80 #endif
81 
82 /*
83  * O_DIRECT is not standard, but almost every Unix has it. We translate it
84  * to the appropriate Windows flag in src/port/open.c. We simulate it with
85  * fcntl(F_NOCACHE) on macOS inside fd.c's open() wrapper. We use the name
86  * PG_O_DIRECT rather than defining O_DIRECT in that case (probably not a good
87  * idea on a Unix).
88  */
89 #if defined(O_DIRECT)
90 #define PG_O_DIRECT O_DIRECT
91 #elif defined(F_NOCACHE)
92 #define PG_O_DIRECT 0x80000000
93 #define PG_O_DIRECT_USE_F_NOCACHE
94 #else
95 #define PG_O_DIRECT 0
96 #endif
97 
98 /*
99  * prototypes for functions in fd.c
100  */
101 
102 /* Operations on virtual Files --- equivalent to Unix kernel file ops */
103 extern File PathNameOpenFile(const char *fileName, int fileFlags);
104 extern File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
105 extern File OpenTemporaryFile(bool interXact);
106 extern void FileClose(File file);
107 extern int FilePrefetch(File file, off_t offset, int amount, uint32 wait_event_info);
108 extern int FileRead(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info);
109 extern int FileWrite(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info);
110 extern int FileSync(File file, uint32 wait_event_info);
111 extern off_t FileSize(File file);
112 extern int FileTruncate(File file, off_t offset, uint32 wait_event_info);
113 extern void FileWriteback(File file, off_t offset, off_t nbytes, uint32 wait_event_info);
114 extern char *FilePathName(File file);
115 extern int FileGetRawDesc(File file);
116 extern int FileGetRawFlags(File file);
117 extern mode_t FileGetRawMode(File file);
118 
119 /* Operations used for sharing named temporary files */
120 extern File PathNameCreateTemporaryFile(const char *name, bool error_on_failure);
121 extern File PathNameOpenTemporaryFile(const char *path, int mode);
122 extern bool PathNameDeleteTemporaryFile(const char *name, bool error_on_failure);
123 extern void PathNameCreateTemporaryDir(const char *base, const char *name);
124 extern void PathNameDeleteTemporaryDir(const char *name);
125 extern void TempTablespacePath(char *path, Oid tablespace);
126 
127 /* Operations that allow use of regular stdio --- USE WITH CAUTION */
128 extern FILE *AllocateFile(const char *name, const char *mode);
129 extern int FreeFile(FILE *file);
130 
131 /* Operations that allow use of pipe streams (popen/pclose) */
132 extern FILE *OpenPipeStream(const char *command, const char *mode);
133 extern int ClosePipeStream(FILE *file);
134 
135 /* Operations to allow use of the <dirent.h> library routines */
136 extern DIR *AllocateDir(const char *dirname);
137 extern struct dirent *ReadDir(DIR *dir, const char *dirname);
138 extern struct dirent *ReadDirExtended(DIR *dir, const char *dirname,
139  int elevel);
140 extern int FreeDir(DIR *dir);
141 
142 /* Operations to allow use of a plain kernel FD, with automatic cleanup */
143 extern int OpenTransientFile(const char *fileName, int fileFlags);
144 extern int OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
145 extern int CloseTransientFile(int fd);
146 
147 /* If you've really really gotta have a plain kernel FD, use this */
148 extern int BasicOpenFile(const char *fileName, int fileFlags);
149 extern int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
150 
151 /* Use these for other cases, and also for long-lived BasicOpenFile FDs */
152 extern bool AcquireExternalFD(void);
153 extern void ReserveExternalFD(void);
154 extern void ReleaseExternalFD(void);
155 
156 /* Make a directory with default permissions */
157 extern int MakePGDirectory(const char *directoryName);
158 
159 /* Miscellaneous support routines */
160 extern void InitFileAccess(void);
161 extern void InitTemporaryFileAccess(void);
162 extern void set_max_safe_fds(void);
163 extern void closeAllVfds(void);
164 extern void SetTempTablespaces(Oid *tableSpaces, int numSpaces);
165 extern bool TempTablespacesAreSet(void);
166 extern int GetTempTablespaces(Oid *tableSpaces, int numSpaces);
167 extern Oid GetNextTempTableSpace(void);
168 extern void AtEOXact_Files(bool isCommit);
169 extern void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid,
170  SubTransactionId parentSubid);
171 extern void RemovePgTempFiles(void);
172 extern void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok,
173  bool unlink_all);
174 extern bool looks_like_temp_rel_name(const char *name);
175 
176 extern int pg_fsync(int fd);
177 extern int pg_fsync_no_writethrough(int fd);
178 extern int pg_fsync_writethrough(int fd);
179 extern int pg_fdatasync(int fd);
180 extern void pg_flush_data(int fd, off_t offset, off_t amount);
181 extern ssize_t pg_pwritev_with_retry(int fd,
182  const struct iovec *iov,
183  int iovcnt,
184  off_t offset);
185 extern int pg_truncate(const char *path, off_t length);
186 extern void fsync_fname(const char *fname, bool isdir);
187 extern int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel);
188 extern int durable_rename(const char *oldfile, const char *newfile, int loglevel);
189 extern int durable_unlink(const char *fname, int loglevel);
190 extern int durable_rename_excl(const char *oldfile, const char *newfile, int loglevel);
191 extern void SyncDataDirectory(void);
192 extern int data_sync_elevel(int elevel);
193 
194 /* Filename components */
195 #define PG_TEMP_FILES_DIR "pgsql_tmp"
196 #define PG_TEMP_FILE_PREFIX "pgsql_tmp"
197 
198 #endif /* FD_H */
void PathNameCreateTemporaryDir(const char *base, const char *name)
Definition: fd.c:1641
int FreeDir(DIR *dir)
Definition: fd.c:2838
int durable_unlink(const char *fname, int loglevel)
Definition: fd.c:787
int GetTempTablespaces(Oid *tableSpaces, int numSpaces)
Definition: fd.c:2969
static PgChecksumMode mode
Definition: pg_checksums.c:65
File PathNameOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:1564
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:3008
int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel)
Definition: fd.c:3666
void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok, bool unlink_all)
Definition: fd.c:3209
int MakePGDirectory(const char *directoryName)
Definition: fd.c:3782
void InitTemporaryFileAccess(void)
Definition: fd.c:911
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2720
void FileClose(File file)
Definition: fd.c:1959
int max_safe_fds
Definition: fd.c:158
void FileWriteback(File file, off_t offset, off_t nbytes, uint32 wait_event_info)
Definition: fd.c:2087
File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:1577
int CloseTransientFile(int fd)
Definition: fd.c:2686
void AtEOXact_Files(bool isCommit)
Definition: fd.c:3041
bool PathNameDeleteTemporaryFile(const char *name, bool error_on_failure)
Definition: fd.c:1913
int pg_fsync_writethrough(int fd)
Definition: fd.c:424
int FreeFile(FILE *file)
Definition: fd.c:2658
uint32 SubTransactionId
Definition: c.h:591
void PathNameDeleteTemporaryDir(const char *name)
Definition: fd.c:1672
unsigned int Oid
Definition: postgres_ext.h:31
Definition: dirent.h:9
static int fd(const char *x, int i)
Definition: preproc-init.c:105
struct dirent * ReadDirExtended(DIR *dir, const char *dirname, int elevel)
Definition: fd.c:2801
#define PGDLLIMPORT
Definition: c.h:1317
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1705
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2509
int recovery_init_sync_method
Definition: fd.c:164
Definition: dirent.c:25
FILE * OpenPipeStream(const char *command, const char *mode)
Definition: fd.c:2562
bool AcquireExternalFD(void)
Definition: fd.c:1175
int durable_rename(const char *oldfile, const char *newfile, int loglevel)
Definition: fd.c:697
int ClosePipeStream(FILE *file)
Definition: fd.c:2868
void ReserveExternalFD(void)
Definition: fd.c:1210
char * tablespace
Definition: pgbench.c:226
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2786
bool TempTablespacesAreSet(void)
Definition: fd.c:2954
char * FilePathName(File file)
Definition: fd.c:2337
unsigned int uint32
Definition: c.h:441
Oid GetNextTempTableSpace(void)
Definition: fd.c:2987
void pg_flush_data(int fd, off_t offset, off_t amount)
Definition: fd.c:467
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2459
void SyncDataDirectory(void)
Definition: fd.c:3423
int FileSync(File file, uint32 wait_event_info)
Definition: fd.c:2264
off_t FileSize(File file)
Definition: fd.c:2285
static int elevel
Definition: vacuumlazy.c:401
void ReleaseExternalFD(void)
Definition: fd.c:1228
void closeAllVfds(void)
Definition: fd.c:2897
int FileGetRawFlags(File file)
Definition: fd.c:2363
File PathNameOpenTemporaryFile(const char *path, int mode)
Definition: fd.c:1882
File PathNameCreateTemporaryFile(const char *name, bool error_on_failure)
Definition: fd.c:1842
Definition: pg_iovec.h:24
int pg_fsync_no_writethrough(int fd)
Definition: fd.c:412
int FileGetRawDesc(File file)
Definition: fd.c:2353
bool looks_like_temp_rel_name(const char *name)
Definition: fd.c:3331
int durable_rename_excl(const char *oldfile, const char *newfile, int loglevel)
Definition: fd.c:827
int FileWrite(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info)
Definition: fd.c:2166
void set_max_safe_fds(void)
Definition: fd.c:1026
int pg_fdatasync(int fd)
Definition: fd.c:447
int FileTruncate(File file, off_t offset, uint32 wait_event_info)
Definition: fd.c:2302
void InitFileAccess(void)
Definition: fd.c:881
int FilePrefetch(File file, off_t offset, int amount, uint32 wait_event_info)
Definition: fd.c:2059
int pg_fsync(int fd)
Definition: fd.c:357
int data_sync_elevel(int elevel)
Definition: fd.c:3805
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:671
void RemovePgTempFiles(void)
Definition: fd.c:3150
void SetTempTablespaces(Oid *tableSpaces, int numSpaces)
Definition: fd.c:2926
const char * name
Definition: encode.c:561
mode_t FileGetRawMode(File file)
Definition: fd.c:2373
int FileRead(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info)
Definition: fd.c:2110
int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:1091
void TempTablespacePath(char *path, Oid tablespace)
Definition: fd.c:1760
int BasicOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:1069
ssize_t pg_pwritev_with_retry(int fd, const struct iovec *iov, int iovcnt, off_t offset)
Definition: fd.c:3815
RecoveryInitSyncMethod
Definition: fd.h:48
int pg_truncate(const char *path, off_t length)
Definition: fd.c:640
PGDLLIMPORT bool data_sync_retry
Definition: fd.c:161
PGDLLIMPORT int max_files_per_process
Definition: fd.c:145
int File
Definition: fd.h:54
int OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:2518