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  * prototypes for functions in fd.c
84  */
85 
86 /* Operations on virtual Files --- equivalent to Unix kernel file ops */
87 extern File PathNameOpenFile(const char *fileName, int fileFlags);
88 extern File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
89 extern File OpenTemporaryFile(bool interXact);
90 extern void FileClose(File file);
91 extern int FilePrefetch(File file, off_t offset, int amount, uint32 wait_event_info);
92 extern int FileRead(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info);
93 extern int FileWrite(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info);
94 extern int FileSync(File file, uint32 wait_event_info);
95 extern off_t FileSize(File file);
96 extern int FileTruncate(File file, off_t offset, uint32 wait_event_info);
97 extern void FileWriteback(File file, off_t offset, off_t nbytes, uint32 wait_event_info);
98 extern char *FilePathName(File file);
99 extern int FileGetRawDesc(File file);
100 extern int FileGetRawFlags(File file);
101 extern mode_t FileGetRawMode(File file);
102 
103 /* Operations used for sharing named temporary files */
104 extern File PathNameCreateTemporaryFile(const char *name, bool error_on_failure);
105 extern File PathNameOpenTemporaryFile(const char *path, int mode);
106 extern bool PathNameDeleteTemporaryFile(const char *name, bool error_on_failure);
107 extern void PathNameCreateTemporaryDir(const char *base, const char *name);
108 extern void PathNameDeleteTemporaryDir(const char *name);
109 extern void TempTablespacePath(char *path, Oid tablespace);
110 
111 /* Operations that allow use of regular stdio --- USE WITH CAUTION */
112 extern FILE *AllocateFile(const char *name, const char *mode);
113 extern int FreeFile(FILE *file);
114 
115 /* Operations that allow use of pipe streams (popen/pclose) */
116 extern FILE *OpenPipeStream(const char *command, const char *mode);
117 extern int ClosePipeStream(FILE *file);
118 
119 /* Operations to allow use of the <dirent.h> library routines */
120 extern DIR *AllocateDir(const char *dirname);
121 extern struct dirent *ReadDir(DIR *dir, const char *dirname);
122 extern struct dirent *ReadDirExtended(DIR *dir, const char *dirname,
123  int elevel);
124 extern int FreeDir(DIR *dir);
125 
126 /* Operations to allow use of a plain kernel FD, with automatic cleanup */
127 extern int OpenTransientFile(const char *fileName, int fileFlags);
128 extern int OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
129 extern int CloseTransientFile(int fd);
130 
131 /* If you've really really gotta have a plain kernel FD, use this */
132 extern int BasicOpenFile(const char *fileName, int fileFlags);
133 extern int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
134 
135 /* Use these for other cases, and also for long-lived BasicOpenFile FDs */
136 extern bool AcquireExternalFD(void);
137 extern void ReserveExternalFD(void);
138 extern void ReleaseExternalFD(void);
139 
140 /* Make a directory with default permissions */
141 extern int MakePGDirectory(const char *directoryName);
142 
143 /* Miscellaneous support routines */
144 extern void InitFileAccess(void);
145 extern void set_max_safe_fds(void);
146 extern void closeAllVfds(void);
147 extern void SetTempTablespaces(Oid *tableSpaces, int numSpaces);
148 extern bool TempTablespacesAreSet(void);
149 extern int GetTempTablespaces(Oid *tableSpaces, int numSpaces);
150 extern Oid GetNextTempTableSpace(void);
151 extern void AtEOXact_Files(bool isCommit);
152 extern void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid,
153  SubTransactionId parentSubid);
154 extern void RemovePgTempFiles(void);
155 extern void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok,
156  bool unlink_all);
157 extern bool looks_like_temp_rel_name(const char *name);
158 
159 extern int pg_fsync(int fd);
160 extern int pg_fsync_no_writethrough(int fd);
161 extern int pg_fsync_writethrough(int fd);
162 extern int pg_fdatasync(int fd);
163 extern void pg_flush_data(int fd, off_t offset, off_t amount);
164 extern ssize_t pg_pwritev_with_retry(int fd,
165  const struct iovec *iov,
166  int iovcnt,
167  off_t offset);
168 extern int pg_truncate(const char *path, off_t length);
169 extern void fsync_fname(const char *fname, bool isdir);
170 extern int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel);
171 extern int durable_rename(const char *oldfile, const char *newfile, int loglevel);
172 extern int durable_unlink(const char *fname, int loglevel);
173 extern int durable_rename_excl(const char *oldfile, const char *newfile, int loglevel);
174 extern void SyncDataDirectory(void);
175 extern int data_sync_elevel(int elevel);
176 
177 /* Filename components */
178 #define PG_TEMP_FILES_DIR "pgsql_tmp"
179 #define PG_TEMP_FILE_PREFIX "pgsql_tmp"
180 
181 #endif /* FD_H */
void PathNameCreateTemporaryDir(const char *base, const char *name)
Definition: fd.c:1561
int FreeDir(DIR *dir)
Definition: fd.c:2752
int durable_unlink(const char *fname, int loglevel)
Definition: fd.c:782
int GetTempTablespaces(Oid *tableSpaces, int numSpaces)
Definition: fd.c:2883
static PgChecksumMode mode
Definition: pg_checksums.c:61
File PathNameOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:1484
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2922
int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel)
Definition: fd.c:3575
void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok, bool unlink_all)
Definition: fd.c:3118
int MakePGDirectory(const char *directoryName)
Definition: fd.c:3691
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2634
void FileClose(File file)
Definition: fd.c:1873
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:2001
File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:1497
int CloseTransientFile(int fd)
Definition: fd.c:2600
void AtEOXact_Files(bool isCommit)
Definition: fd.c:2955
bool PathNameDeleteTemporaryFile(const char *name, bool error_on_failure)
Definition: fd.c:1827
int pg_fsync_writethrough(int fd)
Definition: fd.c:419
int FreeFile(FILE *file)
Definition: fd.c:2572
uint32 SubTransactionId
Definition: c.h:591
void PathNameDeleteTemporaryDir(const char *name)
Definition: fd.c:1592
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:2715
#define PGDLLIMPORT
Definition: c.h:1317
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1625
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2423
int recovery_init_sync_method
Definition: fd.c:164
Definition: dirent.c:25
FILE * OpenPipeStream(const char *command, const char *mode)
Definition: fd.c:2476
bool AcquireExternalFD(void)
Definition: fd.c:1095
int durable_rename(const char *oldfile, const char *newfile, int loglevel)
Definition: fd.c:692
int ClosePipeStream(FILE *file)
Definition: fd.c:2782
void ReserveExternalFD(void)
Definition: fd.c:1130
char * tablespace
Definition: pgbench.c:224
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2700
bool TempTablespacesAreSet(void)
Definition: fd.c:2868
char * FilePathName(File file)
Definition: fd.c:2251
unsigned int uint32
Definition: c.h:441
Oid GetNextTempTableSpace(void)
Definition: fd.c:2901
void pg_flush_data(int fd, off_t offset, off_t amount)
Definition: fd.c:462
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2373
void SyncDataDirectory(void)
Definition: fd.c:3332
int FileSync(File file, uint32 wait_event_info)
Definition: fd.c:2178
off_t FileSize(File file)
Definition: fd.c:2199
static int elevel
Definition: vacuumlazy.c:400
void ReleaseExternalFD(void)
Definition: fd.c:1148
void closeAllVfds(void)
Definition: fd.c:2811
int FileGetRawFlags(File file)
Definition: fd.c:2277
File PathNameOpenTemporaryFile(const char *path, int mode)
Definition: fd.c:1798
File PathNameCreateTemporaryFile(const char *name, bool error_on_failure)
Definition: fd.c:1760
Definition: pg_iovec.h:24
int pg_fsync_no_writethrough(int fd)
Definition: fd.c:407
int FileGetRawDesc(File file)
Definition: fd.c:2267
bool looks_like_temp_rel_name(const char *name)
Definition: fd.c:3240
int durable_rename_excl(const char *oldfile, const char *newfile, int loglevel)
Definition: fd.c:822
int FileWrite(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info)
Definition: fd.c:2080
void set_max_safe_fds(void)
Definition: fd.c:990
int pg_fdatasync(int fd)
Definition: fd.c:442
int FileTruncate(File file, off_t offset, uint32 wait_event_info)
Definition: fd.c:2216
void InitFileAccess(void)
Definition: fd.c:873
int FilePrefetch(File file, off_t offset, int amount, uint32 wait_event_info)
Definition: fd.c:1973
int pg_fsync(int fd)
Definition: fd.c:352
int data_sync_elevel(int elevel)
Definition: fd.c:3714
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:666
void RemovePgTempFiles(void)
Definition: fd.c:3059
void SetTempTablespaces(Oid *tableSpaces, int numSpaces)
Definition: fd.c:2840
const char * name
Definition: encode.c:515
mode_t FileGetRawMode(File file)
Definition: fd.c:2287
int FileRead(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info)
Definition: fd.c:2024
int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:1055
void TempTablespacePath(char *path, Oid tablespace)
Definition: fd.c:1678
int BasicOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:1033
ssize_t pg_pwritev_with_retry(int fd, const struct iovec *iov, int iovcnt, off_t offset)
Definition: fd.c:3724
RecoveryInitSyncMethod
Definition: fd.h:48
int pg_truncate(const char *path, off_t length)
Definition: fd.c:635
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:2432