2
0

fd.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*-------------------------------------------------------------------------
  2. *
  3. * fd.h
  4. * Virtual file descriptor definitions.
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2022, 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. * calls:
  16. *
  17. * File {Close, Read, Write, Size, Sync}
  18. * {Path Name Open, Allocate, Free} File
  19. *
  20. * These are NOT JUST RENAMINGS OF THE UNIX ROUTINES.
  21. * Use them for all file activity...
  22. *
  23. * File fd;
  24. * fd = PathNameOpenFile("foo", O_RDONLY);
  25. *
  26. * AllocateFile();
  27. * FreeFile();
  28. *
  29. * Use AllocateFile, not fopen, if you need a stdio file (FILE*); then
  30. * use FreeFile, not fclose, to close it. AVOID using stdio for files
  31. * that you intend to hold open for any length of time, since there is
  32. * no way for them to share kernel file descriptors with other files.
  33. *
  34. * Likewise, use AllocateDir/FreeDir, not opendir/closedir, to allocate
  35. * open directories (DIR*), and OpenTransientFile/CloseTransientFile for an
  36. * unbuffered file descriptor.
  37. *
  38. * If you really can't use any of the above, at least call AcquireExternalFD
  39. * or ReserveExternalFD to report any file descriptors that are held for any
  40. * length of time. Failure to do so risks unnecessary EMFILE errors.
  41. */
  42. #ifndef FD_H
  43. #define FD_H
  44. #include <dirent.h>
  45. typedef enum RecoveryInitSyncMethod
  46. {
  47. RECOVERY_INIT_SYNC_METHOD_FSYNC,
  48. RECOVERY_INIT_SYNC_METHOD_SYNCFS
  49. } RecoveryInitSyncMethod;
  50. struct iovec; /* avoid including port/pg_iovec.h here */
  51. typedef int File;
  52. /* GUC parameter */
  53. extern PGDLLIMPORT int max_files_per_process;
  54. extern PGDLLIMPORT bool data_sync_retry;
  55. extern PGDLLIMPORT int recovery_init_sync_method;
  56. /*
  57. * This is private to fd.c, but exported for save/restore_backend_variables()
  58. */
  59. extern PGDLLIMPORT int max_safe_fds;
  60. /*
  61. * On Windows, we have to interpret EACCES as possibly meaning the same as
  62. * ENOENT, because if a file is unlinked-but-not-yet-gone on that platform,
  63. * that's what you get. Ugh. This code is designed so that we don't
  64. * actually believe these cases are okay without further evidence (namely,
  65. * a pending fsync request getting canceled ... see ProcessSyncRequests).
  66. */
  67. #ifndef WIN32
  68. #define FILE_POSSIBLY_DELETED(err) ((err) == ENOENT)
  69. #else
  70. #define FILE_POSSIBLY_DELETED(err) ((err) == ENOENT || (err) == EACCES)
  71. #endif
  72. /*
  73. * O_DIRECT is not standard, but almost every Unix has it. We translate it
  74. * to the appropriate Windows flag in src/port/open.c. We simulate it with
  75. * fcntl(F_NOCACHE) on macOS inside fd.c's open() wrapper. We use the name
  76. * PG_O_DIRECT rather than defining O_DIRECT in that case (probably not a good
  77. * idea on a Unix).
  78. */
  79. #if defined(O_DIRECT)
  80. #define PG_O_DIRECT O_DIRECT
  81. #elif defined(F_NOCACHE)
  82. #define PG_O_DIRECT 0x80000000
  83. #define PG_O_DIRECT_USE_F_NOCACHE
  84. #else
  85. #define PG_O_DIRECT 0
  86. #endif
  87. /*
  88. * prototypes for functions in fd.c
  89. */
  90. /* Operations on virtual Files --- equivalent to Unix kernel file ops */
  91. extern File PathNameOpenFile(const char *fileName, int fileFlags);
  92. extern File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
  93. extern File OpenTemporaryFile(bool interXact);
  94. extern void FileClose(File file);
  95. extern int FilePrefetch(File file, off_t offset, int amount, uint32 wait_event_info);
  96. extern int FileRead(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info);
  97. extern int FileWrite(File file, char *buffer, int amount, off_t offset, uint32 wait_event_info);
  98. extern int FileSync(File file, uint32 wait_event_info);
  99. extern off_t FileSize(File file);
  100. extern int FileTruncate(File file, off_t offset, uint32 wait_event_info);
  101. extern void FileWriteback(File file, off_t offset, off_t nbytes, uint32 wait_event_info);
  102. extern char *FilePathName(File file);
  103. extern int FileGetRawDesc(File file);
  104. extern int FileGetRawFlags(File file);
  105. extern mode_t FileGetRawMode(File file);
  106. /* Operations used for sharing named temporary files */
  107. extern File PathNameCreateTemporaryFile(const char *name, bool error_on_failure);
  108. extern File PathNameOpenTemporaryFile(const char *path, int mode);
  109. extern bool PathNameDeleteTemporaryFile(const char *name, bool error_on_failure);
  110. extern void PathNameCreateTemporaryDir(const char *base, const char *name);
  111. extern void PathNameDeleteTemporaryDir(const char *name);
  112. extern void TempTablespacePath(char *path, Oid tablespace);
  113. /* Operations that allow use of regular stdio --- USE WITH CAUTION */
  114. extern FILE *AllocateFile(const char *name, const char *mode);
  115. extern int FreeFile(FILE *file);
  116. /* Operations that allow use of pipe streams (popen/pclose) */
  117. extern FILE *OpenPipeStream(const char *command, const char *mode);
  118. extern int ClosePipeStream(FILE *file);
  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. /* Operations to allow use of a plain kernel FD, with automatic cleanup */
  126. extern int OpenTransientFile(const char *fileName, int fileFlags);
  127. extern int OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
  128. extern int CloseTransientFile(int fd);
  129. /* If you've really really gotta have a plain kernel FD, use this */
  130. extern int BasicOpenFile(const char *fileName, int fileFlags);
  131. extern int BasicOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode);
  132. /* Use these for other cases, and also for long-lived BasicOpenFile FDs */
  133. extern bool AcquireExternalFD(void);
  134. extern void ReserveExternalFD(void);
  135. extern void ReleaseExternalFD(void);
  136. /* Make a directory with default permissions */
  137. extern int MakePGDirectory(const char *directoryName);
  138. /* Miscellaneous support routines */
  139. extern void InitFileAccess(void);
  140. extern void InitTemporaryFileAccess(void);
  141. extern void set_max_safe_fds(void);
  142. extern void closeAllVfds(void);
  143. extern void SetTempTablespaces(Oid *tableSpaces, int numSpaces);
  144. extern bool TempTablespacesAreSet(void);
  145. extern int GetTempTablespaces(Oid *tableSpaces, int numSpaces);
  146. extern Oid GetNextTempTableSpace(void);
  147. extern void AtEOXact_Files(bool isCommit);
  148. extern void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid,
  149. SubTransactionId parentSubid);
  150. extern void RemovePgTempFiles(void);
  151. extern void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok,
  152. bool unlink_all);
  153. extern bool looks_like_temp_rel_name(const char *name);
  154. extern int pg_fsync(int fd);
  155. extern int pg_fsync_no_writethrough(int fd);
  156. extern int pg_fsync_writethrough(int fd);
  157. extern int pg_fdatasync(int fd);
  158. extern void pg_flush_data(int fd, off_t offset, off_t amount);
  159. extern ssize_t pg_pwritev_with_retry(int fd,
  160. const struct iovec *iov,
  161. int iovcnt,
  162. off_t offset);
  163. extern int pg_truncate(const char *path, off_t length);
  164. extern void fsync_fname(const char *fname, bool isdir);
  165. extern int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel);
  166. extern int durable_rename(const char *oldfile, const char *newfile, int loglevel);
  167. extern int durable_unlink(const char *fname, int loglevel);
  168. extern int durable_rename_excl(const char *oldfile, const char *newfile, int loglevel);
  169. extern void SyncDataDirectory(void);
  170. extern int data_sync_elevel(int elevel);
  171. /* Filename components */
  172. #define PG_TEMP_FILES_DIR "pgsql_tmp"
  173. #define PG_TEMP_FILE_PREFIX "pgsql_tmp"
  174. #endif /* FD_H */