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 
48 struct iovec; /* avoid including port/pg_iovec.h here */
49 
50 typedef int File;
51 
52 
53 /* GUC parameter */
55 extern PGDLLIMPORT bool data_sync_retry;
56 
57 /*
58  * This is private to fd.c, but exported for save/restore_backend_variables()
59  */
60 extern int max_safe_fds;
61 
62 /*
63  * On Windows, we have to interpret EACCES as possibly meaning the same as
64  * ENOENT, because if a file is unlinked-but-not-yet-gone on that platform,
65  * that's what you get. Ugh. This code is designed so that we don't
66  * actually believe these cases are okay without further evidence (namely,
67  * a pending fsync request getting canceled ... see ProcessSyncRequests).
68  */
69 #ifndef WIN32
70 #define FILE_POSSIBLY_DELETED(err) ((err) == ENOENT)
71 #else
72 #define FILE_POSSIBLY_DELETED(err) ((err) == ENOENT || (err) == EACCES)
73 #endif
74 
75 /*
76  * prototypes for functions in fd.c
77  */
78 
79 /* Operations on virtual Files --- equivalent to Unix kernel file ops */
80 extern File PathNameOpenFile(const char *fileName, int fileFlags);
81 extern File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
82 extern File OpenTemporaryFile(bool interXact);
83 extern void FileClose(File file);
84 extern int FilePrefetch(File file, off_t offset, int amount, uint32 wait_event_info);
85 extern int FileRead(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info);
86 extern int FileWrite(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info);
87 extern int FileSync(File file, uint32 wait_event_info);
88 extern off_t FileSize(File file);
89 extern int FileTruncate(File file, off_t offset, uint32 wait_event_info);
90 extern void FileWriteback(File file, off_t offset, off_t nbytes, uint32 wait_event_info);
91 extern char *FilePathName(File file);
92 extern int FileGetRawDesc(File file);
93 extern int FileGetRawFlags(File file);
94 extern mode_t FileGetRawMode(File file);
95 
96 /* Operations used for sharing named temporary files */
97 extern File PathNameCreateTemporaryFile(const char *name, bool error_on_failure);
98 extern File PathNameOpenTemporaryFile(const char *path, int mode);
99 extern bool PathNameDeleteTemporaryFile(const char *name, bool error_on_failure);
100 extern void PathNameCreateTemporaryDir(const char *base, const char *name);
101 extern void PathNameDeleteTemporaryDir(const char *name);
102 extern void TempTablespacePath(char *path, Oid tablespace);
103 
104 /* Operations that allow use of regular stdio --- USE WITH CAUTION */
105 extern FILE *AllocateFile(const char *name, const char *mode);
106 extern int FreeFile(FILE *file);
107 
108 /* Operations that allow use of pipe streams (popen/pclose) */
109 extern FILE *OpenPipeStream(const char *command, const char *mode);
110 extern int ClosePipeStream(FILE *file);
111 
112 /* Operations to allow use of the <dirent.h> library routines */
113 extern DIR *AllocateDir(const char *dirname);
114 extern struct dirent *ReadDir(DIR *dir, const char *dirname);
115 extern struct dirent *ReadDirExtended(DIR *dir, const char *dirname,
116  int elevel);
117 extern int FreeDir(DIR *dir);
118 
119 /* Operations to allow use of a plain kernel FD, with automatic cleanup */
120 extern int OpenTransientFile(const char *fileName, int fileFlags);
121 extern int OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
122 extern int CloseTransientFile(int fd);
123 
124 /* If you've really really gotta have a plain kernel FD, use this */
125 extern int BasicOpenFile(const char *fileName, int fileFlags);
126 extern int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
127 
128 /* Use these for other cases, and also for long-lived BasicOpenFile FDs */
129 extern bool AcquireExternalFD(void);
130 extern void ReserveExternalFD(void);
131 extern void ReleaseExternalFD(void);
132 
133 /* Make a directory with default permissions */
134 extern int MakePGDirectory(const char *directoryName);
135 
136 /* Miscellaneous support routines */
137 extern void InitFileAccess(void);
138 extern void set_max_safe_fds(void);
139 extern void closeAllVfds(void);
140 extern void SetTempTablespaces(Oid *tableSpaces, int numSpaces);
141 extern bool TempTablespacesAreSet(void);
142 extern int GetTempTablespaces(Oid *tableSpaces, int numSpaces);
143 extern Oid GetNextTempTableSpace(void);
144 extern void AtEOXact_Files(bool isCommit);
145 extern void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid,
146  SubTransactionId parentSubid);
147 extern void RemovePgTempFiles(void);
148 extern void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok,
149  bool unlink_all);
150 extern bool looks_like_temp_rel_name(const char *name);
151 
152 extern int pg_fsync(int fd);
153 extern int pg_fsync_no_writethrough(int fd);
154 extern int pg_fsync_writethrough(int fd);
155 extern int pg_fdatasync(int fd);
156 extern void pg_flush_data(int fd, off_t offset, off_t amount);
157 extern ssize_t pg_pwritev_with_retry(int fd,
158  const struct iovec *iov,
159  int iovcnt,
160  off_t offset);
161 extern int pg_truncate(const char *path, off_t length);
162 extern void fsync_fname(const char *fname, bool isdir);
163 extern int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel);
164 extern int durable_rename(const char *oldfile, const char *newfile, int loglevel);
165 extern int durable_unlink(const char *fname, int loglevel);
166 extern int durable_rename_excl(const char *oldfile, const char *newfile, int loglevel);
167 extern void SyncDataDirectory(void);
168 extern int data_sync_elevel(int elevel);
169 
170 /* Filename components */
171 #define PG_TEMP_FILES_DIR "pgsql_tmp"
172 #define PG_TEMP_FILE_PREFIX "pgsql_tmp"
173 
174 #endif /* FD_H */
void PathNameCreateTemporaryDir(const char *base, const char *name)
Definition: fd.c:1542
int FreeDir(DIR *dir)
Definition: fd.c:2733
int durable_unlink(const char *fname, int loglevel)
Definition: fd.c:777
int GetTempTablespaces(Oid *tableSpaces, int numSpaces)
Definition: fd.c:2864
static PgChecksumMode mode
Definition: pg_checksums.c:61
File PathNameOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:1465
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2903
int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel)
Definition: fd.c:3496
void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok, bool unlink_all)
Definition: fd.c:3097
int MakePGDirectory(const char *directoryName)
Definition: fd.c:3612
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2615
void FileClose(File file)
Definition: fd.c:1854
int max_safe_fds
Definition: fd.c:156
void FileWriteback(File file, off_t offset, off_t nbytes, uint32 wait_event_info)
Definition: fd.c:1982
File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:1478
int CloseTransientFile(int fd)
Definition: fd.c:2581
void AtEOXact_Files(bool isCommit)
Definition: fd.c:2936
bool PathNameDeleteTemporaryFile(const char *name, bool error_on_failure)
Definition: fd.c:1808
int pg_fsync_writethrough(int fd)
Definition: fd.c:414
int FreeFile(FILE *file)
Definition: fd.c:2553
uint32 SubTransactionId
Definition: c.h:591
void PathNameDeleteTemporaryDir(const char *name)
Definition: fd.c:1573
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:2696
#define PGDLLIMPORT
Definition: c.h:1317
File OpenTemporaryFile(bool interXact)
Definition: fd.c:1606
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2404
Definition: dirent.c:25
FILE * OpenPipeStream(const char *command, const char *mode)
Definition: fd.c:2457
bool AcquireExternalFD(void)
Definition: fd.c:1076
int durable_rename(const char *oldfile, const char *newfile, int loglevel)
Definition: fd.c:687
int ClosePipeStream(FILE *file)
Definition: fd.c:2763
void ReserveExternalFD(void)
Definition: fd.c:1111
char * tablespace
Definition: pgbench.c:190
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2681
bool TempTablespacesAreSet(void)
Definition: fd.c:2849
char * FilePathName(File file)
Definition: fd.c:2232
unsigned int uint32
Definition: c.h:441
Oid GetNextTempTableSpace(void)
Definition: fd.c:2882
void pg_flush_data(int fd, off_t offset, off_t amount)
Definition: fd.c:457
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2354
void SyncDataDirectory(void)
Definition: fd.c:3289
int FileSync(File file, uint32 wait_event_info)
Definition: fd.c:2159
off_t FileSize(File file)
Definition: fd.c:2180
static int elevel
Definition: vacuumlazy.c:333
void ReleaseExternalFD(void)
Definition: fd.c:1129
void closeAllVfds(void)
Definition: fd.c:2792
int FileGetRawFlags(File file)
Definition: fd.c:2258
File PathNameOpenTemporaryFile(const char *path, int mode)
Definition: fd.c:1779
File PathNameCreateTemporaryFile(const char *name, bool error_on_failure)
Definition: fd.c:1741
Definition: pg_iovec.h:24
int pg_fsync_no_writethrough(int fd)
Definition: fd.c:402
int FileGetRawDesc(File file)
Definition: fd.c:2248
bool looks_like_temp_rel_name(const char *name)
Definition: fd.c:3219
int durable_rename_excl(const char *oldfile, const char *newfile, int loglevel)
Definition: fd.c:814
int FileWrite(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info)
Definition: fd.c:2061
void set_max_safe_fds(void)
Definition: fd.c:971
int pg_fdatasync(int fd)
Definition: fd.c:437
int FileTruncate(File file, off_t offset, uint32 wait_event_info)
Definition: fd.c:2197
void InitFileAccess(void)
Definition: fd.c:854
int FilePrefetch(File file, off_t offset, int amount, uint32 wait_event_info)
Definition: fd.c:1954
int pg_fsync(int fd)
Definition: fd.c:347
int data_sync_elevel(int elevel)
Definition: fd.c:3635
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:661
void RemovePgTempFiles(void)
Definition: fd.c:3038
void SetTempTablespaces(Oid *tableSpaces, int numSpaces)
Definition: fd.c:2821
const char * name
Definition: encode.c:515
mode_t FileGetRawMode(File file)
Definition: fd.c:2268
int FileRead(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info)
Definition: fd.c:2005
int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:1036
void TempTablespacePath(char *path, Oid tablespace)
Definition: fd.c:1659
int BasicOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:1014
ssize_t pg_pwritev_with_retry(int fd, const struct iovec *iov, int iovcnt, off_t offset)
Definition: fd.c:3645
int pg_truncate(const char *path, off_t length)
Definition: fd.c:630
PGDLLIMPORT bool data_sync_retry
Definition: fd.c:159
PGDLLIMPORT int max_files_per_process
Definition: fd.c:143
int File
Definition: fd.h:48
int OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:2413