Age Owner TLA Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * fd.h
4 : * Virtual file descriptor definitions.
5 : *
6 : *
7 : * Portions Copyright (c) 1996-2025, 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, ReadV, Write, WriteV, 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 "port/pg_iovec.h"
47 :
48 : #include <dirent.h>
49 : #include <fcntl.h>
50 :
51 : typedef int File;
52 :
53 :
54 : #define IO_DIRECT_DATA 0x01
55 : #define IO_DIRECT_WAL 0x02
56 : #define IO_DIRECT_WAL_INIT 0x04
57 :
58 :
59 : /* GUC parameter */
60 : extern PGDLLIMPORT int max_files_per_process;
61 : extern PGDLLIMPORT bool data_sync_retry;
62 : extern PGDLLIMPORT int recovery_init_sync_method;
63 : extern PGDLLIMPORT int io_direct_flags;
64 :
65 : /*
66 : * This is private to fd.c, but exported for save/restore_backend_variables()
67 : */
68 : extern PGDLLIMPORT int max_safe_fds;
69 :
70 : /*
71 : * On Windows, we have to interpret EACCES as possibly meaning the same as
72 : * ENOENT, because if a file is unlinked-but-not-yet-gone on that platform,
73 : * that's what you get. Ugh. This code is designed so that we don't
74 : * actually believe these cases are okay without further evidence (namely,
75 : * a pending fsync request getting canceled ... see ProcessSyncRequests).
76 : */
77 : #ifndef WIN32
78 : #define FILE_POSSIBLY_DELETED(err) ((err) == ENOENT)
79 : #else
80 : #define FILE_POSSIBLY_DELETED(err) ((err) == ENOENT || (err) == EACCES)
81 : #endif
82 :
83 : /*
84 : * O_DIRECT is not standard, but almost every Unix has it. We translate it
85 : * to the appropriate Windows flag in src/port/open.c. We simulate it with
86 : * fcntl(F_NOCACHE) on macOS inside fd.c's open() wrapper. We use the name
87 : * PG_O_DIRECT rather than defining O_DIRECT in that case (probably not a good
88 : * idea on a Unix).
89 : */
90 : #if defined(O_DIRECT)
91 : #define PG_O_DIRECT O_DIRECT
92 : #elif defined(F_NOCACHE)
93 : #define PG_O_DIRECT 0x80000000
94 : #define PG_O_DIRECT_USE_F_NOCACHE
95 : /*
96 : * The value we defined to stand in for O_DIRECT when simulating it with
97 : * F_NOCACHE had better not collide with any of the standard flags.
98 : */
99 : StaticAssertDecl((PG_O_DIRECT &
100 : (O_APPEND |
101 : O_CLOEXEC |
102 : O_CREAT |
103 : O_DSYNC |
104 : O_EXCL |
105 : O_RDWR |
106 : O_RDONLY |
107 : O_SYNC |
108 : O_TRUNC |
109 : O_WRONLY)) == 0,
110 : "PG_O_DIRECT value collides with standard flag");
111 : #else
112 : #define PG_O_DIRECT 0
113 : #endif
114 :
115 : /*
116 : * prototypes for functions in fd.c
117 : */
118 :
119 : struct PgAioHandle;
120 :
121 : /* Operations on virtual Files --- equivalent to Unix kernel file ops */
122 : extern File PathNameOpenFile(const char *fileName, int fileFlags);
123 : extern File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
124 : extern File OpenTemporaryFile(bool interXact);
125 : extern void FileClose(File file);
126 : extern int FilePrefetch(File file, pgoff_t offset, pgoff_t amount, uint32 wait_event_info);
127 : extern ssize_t FileReadV(File file, const struct iovec *iov, int iovcnt, pgoff_t offset, uint32 wait_event_info);
128 : extern ssize_t FileWriteV(File file, const struct iovec *iov, int iovcnt, pgoff_t offset, uint32 wait_event_info);
129 : extern int FileStartReadV(struct PgAioHandle *ioh, File file, int iovcnt, pgoff_t offset, uint32 wait_event_info);
130 : extern int FileSync(File file, uint32 wait_event_info);
131 : extern int FileZero(File file, pgoff_t offset, pgoff_t amount, uint32 wait_event_info);
132 : extern int FileFallocate(File file, pgoff_t offset, pgoff_t amount, uint32 wait_event_info);
133 :
134 : extern pgoff_t FileSize(File file);
135 : extern int FileTruncate(File file, pgoff_t offset, uint32 wait_event_info);
136 : extern void FileWriteback(File file, pgoff_t offset, pgoff_t nbytes, uint32 wait_event_info);
137 : extern char *FilePathName(File file);
138 : extern int FileGetRawDesc(File file);
139 : extern int FileGetRawFlags(File file);
140 : extern mode_t FileGetRawMode(File file);
141 :
142 : /* Operations used for sharing named temporary files */
143 : extern File PathNameCreateTemporaryFile(const char *path, bool error_on_failure);
144 : extern File PathNameOpenTemporaryFile(const char *path, int mode);
145 : extern bool PathNameDeleteTemporaryFile(const char *path, bool error_on_failure);
146 : extern void PathNameCreateTemporaryDir(const char *basedir, const char *directory);
147 : extern void PathNameDeleteTemporaryDir(const char *dirname);
148 : extern void TempTablespacePath(char *path, Oid tablespace);
149 :
150 : /* Operations that allow use of regular stdio --- USE WITH CAUTION */
151 : extern FILE *AllocateFile(const char *name, const char *mode);
152 : extern int FreeFile(FILE *file);
153 :
154 : /* Operations that allow use of pipe streams (popen/pclose) */
155 : extern FILE *OpenPipeStream(const char *command, const char *mode);
156 : extern int ClosePipeStream(FILE *file);
157 :
158 : /* Operations to allow use of the <dirent.h> library routines */
159 : extern DIR *AllocateDir(const char *dirname);
160 : extern struct dirent *ReadDir(DIR *dir, const char *dirname);
161 : extern struct dirent *ReadDirExtended(DIR *dir, const char *dirname,
162 : int elevel);
163 : extern int FreeDir(DIR *dir);
164 :
165 : /* Operations to allow use of a plain kernel FD, with automatic cleanup */
166 : extern int OpenTransientFile(const char *fileName, int fileFlags);
167 : extern int OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
168 : extern int CloseTransientFile(int fd);
169 :
170 : /* If you've really really gotta have a plain kernel FD, use this */
171 : extern int BasicOpenFile(const char *fileName, int fileFlags);
172 : extern int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
173 :
174 : /* Use these for other cases, and also for long-lived BasicOpenFile FDs */
175 : extern bool AcquireExternalFD(void);
176 : extern void ReserveExternalFD(void);
177 : extern void ReleaseExternalFD(void);
178 :
179 : /* Make a directory with default permissions */
180 : extern int MakePGDirectory(const char *directoryName);
181 :
182 : /* Miscellaneous support routines */
183 : extern void InitFileAccess(void);
184 : extern void InitTemporaryFileAccess(void);
185 : extern void set_max_safe_fds(void);
186 : extern void closeAllVfds(void);
187 : extern void SetTempTablespaces(Oid *tableSpaces, int numSpaces);
188 : extern bool TempTablespacesAreSet(void);
189 : extern int GetTempTablespaces(Oid *tableSpaces, int numSpaces);
190 : extern Oid GetNextTempTableSpace(void);
191 : extern void AtEOXact_Files(bool isCommit);
192 : extern void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid,
193 : SubTransactionId parentSubid);
194 : extern void RemovePgTempFiles(void);
195 : extern void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok,
196 : bool unlink_all);
197 : extern bool looks_like_temp_rel_name(const char *name);
198 :
199 : extern int pg_fsync(int fd);
200 : extern int pg_fsync_no_writethrough(int fd);
201 : extern int pg_fsync_writethrough(int fd);
202 : extern int pg_fdatasync(int fd);
203 : extern bool pg_file_exists(const char *name);
204 : extern void pg_flush_data(int fd, pgoff_t offset, pgoff_t nbytes);
205 : extern int pg_truncate(const char *path, pgoff_t length);
206 : extern void fsync_fname(const char *fname, bool isdir);
207 : extern int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel);
208 : extern int durable_rename(const char *oldfile, const char *newfile, int elevel);
209 : extern int durable_unlink(const char *fname, int elevel);
210 : extern void SyncDataDirectory(void);
211 : extern int data_sync_elevel(int elevel);
212 :
213 : static inline ssize_t
34 michael@paquier.xyz 214 GNC 408877 : FileRead(File file, void *buffer, size_t amount, pgoff_t offset,
215 : uint32 wait_event_info)
216 : {
736 tmunro@postgresql.or 217 CBC 408877 : struct iovec iov = {
218 : .iov_base = buffer,
219 : .iov_len = amount
220 : };
221 :
222 408877 : return FileReadV(file, &iov, 1, offset, wait_event_info);
223 : }
224 :
225 : static inline ssize_t
34 michael@paquier.xyz 226 GNC 184932 : FileWrite(File file, const void *buffer, size_t amount, pgoff_t offset,
227 : uint32 wait_event_info)
228 : {
736 tmunro@postgresql.or 229 CBC 184932 : struct iovec iov = {
736 tmunro@postgresql.or 230 ECB (183322) : .iov_base = unconstify(void *, buffer),
231 : .iov_len = amount
232 : };
233 :
736 tmunro@postgresql.or 234 CBC 184932 : return FileWriteV(file, &iov, 1, offset, wait_event_info);
235 : }
236 :
237 : #endif /* FD_H */
|