1 /*-------------------------------------------------------------------------
4 * Virtual file descriptor definitions.
7 * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/include/storage/fd.h
12 *-------------------------------------------------------------------------
18 * File {Close, Read, Write, Size, Sync}
19 * {Path Name Open, Allocate, Free} File
21 * These are NOT JUST RENAMINGS OF THE UNIX ROUTINES.
22 * Use them for all file activity...
25 * fd = PathNameOpenFile("foo", O_RDONLY);
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.
35 * Likewise, use AllocateDir/FreeDir, not opendir/closedir, to allocate
36 * open directories (DIR*), and OpenTransientFile/CloseTransientFile for an
37 * unbuffered file descriptor.
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.
48 typedef enum RecoveryInitSyncMethod
50 RECOVERY_INIT_SYNC_METHOD_FSYNC
,
51 RECOVERY_INIT_SYNC_METHOD_SYNCFS
52 } RecoveryInitSyncMethod
;
54 struct iovec
; /* avoid including port/pg_iovec.h here */
60 extern PGDLLIMPORT
int max_files_per_process
;
61 extern PGDLLIMPORT
bool data_sync_retry
;
62 extern int recovery_init_sync_method
;
65 * This is private to fd.c, but exported for save/restore_backend_variables()
67 extern int max_safe_fds
;
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).
77 #define FILE_POSSIBLY_DELETED(err) ((err) == ENOENT)
79 #define FILE_POSSIBLY_DELETED(err) ((err) == ENOENT || (err) == EACCES)
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
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
99 * prototypes for functions in fd.c
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
);
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
);
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
);
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
);
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
,
140 extern int FreeDir(DIR *dir
);
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
);
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
);
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);
156 /* Make a directory with default permissions */
157 extern int MakePGDirectory(const char *directoryName
);
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
,
174 extern bool looks_like_temp_rel_name(const char *name
);
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
,
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
);
194 /* Filename components */
195 #define PG_TEMP_FILES_DIR "pgsql_tmp"
196 #define PG_TEMP_FILE_PREFIX "pgsql_tmp"