Debian Codegraph @

storage/innobase/include/os0file.h

raw
1 /***********************************************************************
2
3 Copyright (c) 1995, 2026, Oracle and/or its affiliates.
4 Copyright (c) 2009, Percona Inc.
5
6 Portions of this file contain modifications contributed and copyrighted
7 by Percona Inc.. Those modifications are
8 gratefully acknowledged and are described briefly in the InnoDB
9 documentation. The contributions by Percona Inc. are incorporated with
10 their permission, and subject to the conditions contained in the file
11 COPYING.Percona.
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License, version 2.0,
15 as published by the Free Software Foundation.
16
17 This program is designed to work with certain software (including
18 but not limited to OpenSSL) that is licensed under separate terms,
19 as designated in a particular file or component or in included license
20 documentation. The authors of MySQL hereby grant you an additional
21 permission to link the program and your derivative works with the
22 separately licensed software that they have either included with
23 the program or referenced in the documentation.
24
25 This program is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 GNU General Public License, version 2.0, for more details.
29
30 You should have received a copy of the GNU General Public License
31 along with this program; if not, write to the Free Software
32 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33
34 ***********************************************************************/
35
36 /** @file include/os0file.h
37 The interface to the operating system file io
38
39 Created 10/21/1995 Heikki Tuuri
40 *******************************************************/
41
42 #ifndef os0file_h
43 #define os0file_h
44
45 #include "my_dbug.h"
46 #include "my_io.h"
47
48 #include "os/file.h"
49 #include "os0atomic.h"
50 #include "os0enc.h"
51
52 #ifndef _WIN32
53 #include <dirent.h>
54 #include <sys/stat.h>
55 #include <sys/statvfs.h>
56 #include <time.h>
57 #else
58 #include <Strsafe.h>
59 #include <locale>
60 #include <string>
61 #endif /* !_WIN32 */
62
63 #include <functional>
64 #include <stack>
65
66 /** Prefix all files and directory created under data directory with special
67 string so that it never conflicts with MySQL schema directory. */
68 #define OS_FILE_PREFIX "#"
69
70 /** File node of a tablespace or the log data space */
71 struct fil_node_t;
72
73 extern bool os_has_said_disk_full;
74
75 /** Number of pending read operations */
76 extern std::atomic<ulint> os_n_pending_reads;
77 /** Number of pending write operations */
78 extern std::atomic<ulint> os_n_pending_writes;
79
80 /* Flush after each os_fsync_threshold bytes */
81 extern unsigned long long os_fsync_threshold;
82
83 /** File offset in bytes */
84 typedef uint64_t os_offset_t;
85
86 namespace file {
87 /** Blocks for doing IO, used in the transparent compression
88 and encryption code. */
89 struct Block {
90 /** Default constructor */
91 Block() noexcept : m_ptr(nullptr), m_in_use() {}
92
93 /** Free the given memory block.
94 @param[in] obj the memory block to be freed. */
95 static void free(file::Block *obj) noexcept;
96
97 /** Pointer to the memory block. */
98 byte *m_ptr;
99 /** Size of the data in memory block. This may be not UNIV_PAGE_SIZE if the
100 data was compressed before encryption. */
101 size_t m_size;
102 /** This padding is needed to avoid false sharing. TBD: of what exactly? We
103 can't use alignas because std::vector<Block> uses std::allocator which in
104 C++14 doesn't have to handle overaligned types. (see § 20.7.9.1.5 of N4140
105 draft) */
106 byte pad[ut::INNODB_CACHE_LINE_SIZE];
107 std::atomic<bool> m_in_use;
108 };
109 } // namespace file
110
111 /** Raw file handle. */
112 using os_fd_t = int;
113
114 static constexpr os_fd_t OS_FD_CLOSED = -1;
115
116 #ifdef _WIN32
117
118 typedef HANDLE os_file_dir_t; /*!< directory stream */
119
120 /** Use unbuffered I/O */
121 #define UNIV_NON_BUFFERED_IO
122
123 /** Windows file handle */
124 using os_file_t = HANDLE;
125
126 static const os_file_t OS_FILE_CLOSED = INVALID_HANDLE_VALUE;
127
128 /** Convert a C file descriptor to a native file handle
129 @param fd file descriptor
130 @return native file handle */
131 #define OS_FILE_FROM_FD(fd) (HANDLE) _get_osfhandle(fd)
132
133 /** Associates a C file descriptor with an existing native file handle
134 @param[in] file native file handle
135 @return C file descriptor */
136 #define OS_FD_FROM_FILE(file) _open_osfhandle((intptr_t)file, _O_RDONLY)
137
138 /** Closes the file associated with C file descriptor fd
139 @param[in] fd C file descriptor
140 @return 0 if success */
141 #define OS_FILE_CLOSE_FD(fd) _close(fd)
142
143 #else /* _WIN32 */
144
145 /** File handle */
146 using os_file_t = os_fd_t;
147
148 static constexpr os_fd_t OS_FILE_CLOSED = OS_FD_CLOSED;
149
150 /** Convert a C file descriptor to a native file handle
151 @param fd file descriptor
152 @return native file handle */
153 #define OS_FILE_FROM_FD(fd) fd
154
155 /** C file descriptor from an existing native file handle
156 @param[in] file native file handle
157 @return C file descriptor */
158 #define OS_FD_FROM_FILE(file) file
159
160 /** Closes the file associated with C file descriptor fd
161 @param[in] fd C file descriptor
162 @return 0 if success */
163 #define OS_FILE_CLOSE_FD(fd) (os_file_close(fd) ? 0 : OS_FD_CLOSED)
164
165 #endif /* _WIN32 */
166
167 /** Common file descriptor for file IO instrumentation with PFS
168 on windows and other platforms */
169 struct pfs_os_file_t {
170 #ifdef UNIV_PFS_IO
171 struct PSI_file *m_psi;
172 #else /* UNIV_PFS_IO */
173 pfs_os_file_t &operator=(os_file_t file) {
174 m_file = file;
175 return (*this);
176 }
177 #endif /* UNIV_PFS_IO */
178
179 os_file_t m_file;
180 };
181
182 /** The next value should be smaller or equal to the smallest sector size used
183 on any disk. A log block is required to be a portion of disk which is written
184 so that if the start and the end of a block get written to disk, then the
185 whole block gets written. This should be true even in most cases of a crash:
186 if this fails for a log block, then it is equivalent to a media failure in the
187 log. */
188
189 constexpr uint32_t OS_FILE_LOG_BLOCK_SIZE = 512;
190
191 /** Options for os_file_create_func @{ */
192 enum os_file_create_t {
193 OS_FILE_OPEN = 51, /*!< to open an existing file (if
194 doesn't exist, error) */
195 OS_FILE_CREATE, /*!< to create new file (if
196 exists, error) */
197 OS_FILE_OPEN_RAW, /*!< to open a raw device or disk
198 partition */
199 OS_FILE_CREATE_PATH, /*!< to create the directories */
200 OS_FILE_OPEN_RETRY, /*!< open with retry */
201
202 /** Flags that can be combined with the above values. Please ensure
203 that the above values stay below 128. */
204
205 OS_FILE_ON_ERROR_NO_EXIT = 128, /*!< do not exit on unknown errors */
206 OS_FILE_ON_ERROR_SILENT = 256 /*!< don't print diagnostic messages to
207 the log unless it is a fatal error,
208 this flag is only used if
209 ON_ERROR_NO_EXIT is set */
210 };
211
212 static const ulint OS_FILE_READ_ONLY = 333;
213 static const ulint OS_FILE_READ_WRITE = 444;
214
215 /** Used by MySQLBackup */
216 static const ulint OS_FILE_READ_ALLOW_DELETE = 555;
217 /** @} */
218
219 /** Types for file create @{ */
220 static const ulint OS_DATA_FILE = 100;
221 static const ulint OS_LOG_FILE = 101;
222 static const ulint OS_LOG_FILE_RESIZING = 102;
223 /* Don't use this for Data files, Log files. Use it for smaller files
224 or if number of bytes to write are not multiple of sector size.
225 With this flag, writes to file will be always buffered and ignores the value
226 of innodb_flush_method. */
227 static const ulint OS_BUFFERED_FILE = 103;
228 static const ulint OS_CLONE_DATA_FILE = 104;
229 static const ulint OS_CLONE_LOG_FILE = 105;
230 static const ulint OS_DBLWR_FILE = 106;
231 static const ulint OS_DATA_FILE_FOR_SPACE_ID_READ = 107;
232 /** @} */
233
234 /** Error codes from os_file_get_last_error @{ */
235 static const ulint OS_FILE_NOT_FOUND = 71;
236 static const ulint OS_FILE_DISK_FULL = 72;
237 static const ulint OS_FILE_ALREADY_EXISTS = 73;
238 static const ulint OS_FILE_PATH_ERROR = 74;
239
240 /** wait for OS aio resources to become available again */
241 static const ulint OS_FILE_AIO_RESOURCES_RESERVED = 75;
242
243 static const ulint OS_FILE_SHARING_VIOLATION = 76;
244 static const ulint OS_FILE_ERROR_NOT_SPECIFIED = 77;
245 static const ulint OS_FILE_INSUFFICIENT_RESOURCE = 78;
246 static const ulint OS_FILE_AIO_INTERRUPTED = 79;
247 static const ulint OS_FILE_OPERATION_ABORTED = 80;
248 static const ulint OS_FILE_ACCESS_VIOLATION = 81;
249 static const ulint OS_FILE_NAME_TOO_LONG = 82;
250 static const ulint OS_FILE_TOO_MANY_OPENED = 83;
251
252 static const ulint OS_FILE_ERROR_MAX = 100;
253 /** @} */
254
255 /** Types for AIO operations @{ */
256
257 /** No transformations during read/write, write as is. */
258 #define IORequestRead IORequest(IORequest::READ)
259 #define IORequestWrite IORequest(IORequest::WRITE)
260
261 /**
262 The IO Context that is passed down to the low level IO code */
263 class IORequest {
264 public:
265 /** Flags passed in the request, they can be ORred together. */
266 enum {
267 UNSET = 0,
268 READ = 1,
269 WRITE = 2,
270
271 /** Request for a doublewrite page IO */
272 DBLWR = 4,
273
274 /** Enumerations below can be ORed to READ/WRITE above*/
275
276 /** Data file */
277 DATA_FILE = 8,
278
279 /** Log file request*/
280 LOG = 16,
281
282 /** Disable partial read warnings */
283 DISABLE_PARTIAL_IO_WARNINGS = 32,
284
285 /** Do not to wake i/o-handler threads, but the caller will do
286 the waking explicitly later, in this way the caller can post
287 several requests in a batch; NOTE that the batch must not be
288 so big that it exhausts the slots in AIO arrays! NOTE that
289 a simulated batch may introduce hidden chances of deadlocks,
290 because I/Os are not actually handled until all
291 have been posted: use with great caution! */
292 DO_NOT_WAKE = 64,
293
294 /** Ignore failed reads of non-existent pages */
295 IGNORE_MISSING = 128,
296
297 /** Use punch hole if available, only makes sense if
298 compression algorithm != NONE. Ignored if not set */
299 PUNCH_HOLE = 256,
300
301 /** Force raw read, do not try to compress/decompress.
302 This can be used to force a read and write without any
303 compression e.g., for redo log, merge sort temporary files
304 and the truncate redo log. */
305 NO_COMPRESSION = 512,
306
307 /** Row log used in online DDL */
308 ROW_LOG = 1024,
309
310 /** We optimise cases where punch hole is not done if the compressed length
311 of the page is the same as the original size of the page. Ignore such
312 optimisations if this flag is set. */
313 DISABLE_PUNCH_HOLE_OPTIMISATION = 2048
314 };
315
316 /** Default constructor */
317 IORequest()
318 : m_block_size(UNIV_SECTOR_SIZE),
319 m_type(READ),
320 m_compression(),
321 m_encryption(),
322 m_eblock(nullptr),
323 m_elen(0) {
324 /* No op */
325 }
326
327 /**
328 @param[in] type Request type, can be a value that is
329 ORed from the above enum */
330 explicit IORequest(int type)
331 : m_block_size(UNIV_SECTOR_SIZE),
332 m_type(type),
333 m_compression(),
334 m_encryption(),
335 m_eblock(nullptr),
336 m_elen(0) {
337 if (is_log() || is_row_log()) {
338 disable_compression();
339 }
340
341 if (!is_punch_hole_supported()) {
342 clear_punch_hole();
343 }
344 }
345
346 /** @return true if ignore missing flag is set */
347 [[nodiscard]] static bool ignore_missing(int type) {
348 return ((type & IGNORE_MISSING) == IGNORE_MISSING);
349 }
350
351 /** @return true if it is a read request */
352 [[nodiscard]] bool is_read() const { return ((m_type & READ) == READ); }
353
354 /** @return true if it is a write request */
355 [[nodiscard]] bool is_write() const { return ((m_type & WRITE) == WRITE); }
356
357 /** @return true if it is a redo log write */
358 [[nodiscard]] bool is_log() const { return ((m_type & LOG) == LOG); }
359
360 /** @return true if it is a row log entry used in online DDL */
361 [[nodiscard]] bool is_row_log() const {
362 return ((m_type & ROW_LOG) == ROW_LOG);
363 }
364
365 /** @return true if the simulated AIO thread should be woken up */
366 [[nodiscard]] bool is_wake() const { return ((m_type & DO_NOT_WAKE) == 0); }
367
368 /** @return true if partial read warning disabled */
369 [[nodiscard]] bool is_partial_io_warning_disabled() const {
370 return ((m_type & DISABLE_PARTIAL_IO_WARNINGS) ==
371 DISABLE_PARTIAL_IO_WARNINGS);
372 }
373
374 /** Disable partial read warnings */
375 void disable_partial_io_warnings() { m_type |= DISABLE_PARTIAL_IO_WARNINGS; }
376
377 /** @return true if missing files should be ignored */
378 [[nodiscard]] bool ignore_missing() const { return (ignore_missing(m_type)); }
379
380 /** @return true if punch hole should be used */
381 [[nodiscard]] bool punch_hole() const {
382 return ((m_type & PUNCH_HOLE) == PUNCH_HOLE);
383 }
384
385 /** @return true if punch hole needs to be done always if it's supported and
386 if the page is to be compressed. */
387 [[nodiscard]] bool is_punch_hole_optimisation_disabled() const {
388 ut_ad(is_compressed() && punch_hole());
389
390 return (m_type & DISABLE_PUNCH_HOLE_OPTIMISATION) ==
391 DISABLE_PUNCH_HOLE_OPTIMISATION;
392 }
393
394 /** @return true if the read should be validated */
395 [[nodiscard]] bool validate() const {
396 ut_ad(is_read() ^ is_write());
397
398 return (!is_read() || !punch_hole());
399 }
400
401 /** Set the punch hole flag */
402 void set_punch_hole() {
403 if (is_punch_hole_supported()) {
404 m_type |= PUNCH_HOLE;
405 }
406 }
407
408 /** Set the force punch hole flag */
409 void disable_punch_hole_optimisation() {
410 if (is_punch_hole_supported()) {
411 m_type |= DISABLE_PUNCH_HOLE_OPTIMISATION;
412 }
413 }
414
415 /** Clear the do not wake flag */
416 void clear_do_not_wake() { m_type &= ~DO_NOT_WAKE; }
417
418 /** Clear the punch hole flag */
419 void clear_punch_hole() { m_type &= ~PUNCH_HOLE; }
420
421 /** @return the block size to use for IO */
422 [[nodiscard]] ulint block_size() const { return (m_block_size); }
423
424 /** Set the block size for IO
425 @param[in] block_size Block size to set */
426 void block_size(ulint block_size) {
427 m_block_size = static_cast<uint32_t>(block_size);
428 }
429
430 /** Returns original size of the IO to make. If one was not specified, then 0
431 is returned. */
432 uint32_t get_original_size() const { return m_original_size; }
433
434 void set_original_size(uint32_t original_size) {
435 m_original_size = original_size;
436 }
437
438 /** Clear all compression related flags */
439 void clear_compressed() {
440 clear_punch_hole();
441
442 m_compression.m_type = Compression::NONE;
443 }
444
445 /** Compare two requests
446 @return true if the are equal */
447 bool operator==(const IORequest &rhs) const { return (m_type == rhs.m_type); }
448
449 /** Set compression algorithm
450 @param[in] type The compression algorithm to use */
451 void compression_algorithm(Compression::Type type) {
452 if (type == Compression::NONE) {
453 return;
454 }
455
456 set_punch_hole();
457
458 m_compression.m_type = type;
459 }
460
461 /** Get the compression algorithm.
462 @return the compression algorithm */
463 [[nodiscard]] Compression compression_algorithm() const {
464 return (m_compression);
465 }
466
467 /** @return true if the page should be compressed */
468 [[nodiscard]] bool is_compressed() const {
469 return (compression_algorithm().m_type != Compression::NONE);
470 }
471
472 /** @return true if the page read should not be transformed. */
473 [[nodiscard]] bool is_compression_enabled() const {
474 return ((m_type & NO_COMPRESSION) == 0);
475 }
476
477 /** Disable transformations. */
478 void disable_compression() { m_type |= NO_COMPRESSION; }
479
480 /** Get the encryption algorithm.
481 @return the encryption algorithm */
482 [[nodiscard]] Encryption encryption_algorithm() const {
483 return (m_encryption);
484 }
485
486 /** @return true if the page should be encrypted. */
487 [[nodiscard]] bool is_encrypted() const {
488 return (m_encryption.get_type() != Encryption::NONE);
489 }
490
491 /** Clear all encryption related flags */
492 void clear_encrypted() {
493 m_encryption.set_key(nullptr);
494 m_encryption.set_key_length(0);
495 m_encryption.set_initial_vector(nullptr);
496 m_encryption.set_type(Encryption::NONE);
497 }
498
499 /** Note that the IO is for double write buffer page write. */
500 void dblwr() { m_type |= DBLWR; }
501
502 /** @return true if the request is for a dblwr page. */
503 [[nodiscard]] bool is_dblwr() const { return ((m_type & DBLWR) == DBLWR); }
504
505 /** @return true if punch hole is supported */
506 static bool is_punch_hole_supported() {
507 /* In this debugging mode, we act as if punch hole is supported,
508 and then skip any calls to actually punch a hole here.
509 In this way, Transparent Page Compression is still being tested. */
510 DBUG_EXECUTE_IF("ignore_punch_hole", return (true););
511
512 #if defined(HAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE) || defined(_WIN32)
513 return (true);
514 #else
515 return (false);
516 #endif /* HAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE || _WIN32 */
517 }
518
519 /** @return string representation. */
520 std::string to_string() const {
521 std::ostringstream os;
522
523 os << "bs: " << m_block_size << " flags:";
524
525 if (m_type & READ) {
526 os << " READ";
527 } else if (m_type & WRITE) {
528 os << " WRITE";
529 } else if (m_type & DBLWR) {
530 os << " DBLWR";
531 }
532
533 /** Enumerations below can be ORed to READ/WRITE above*/
534
535 /** Data file */
536 if (m_type & DATA_FILE) {
537 os << " | DATA_FILE";
538 }
539
540 if (m_type & LOG) {
541 os << " | LOG";
542 }
543
544 if (m_type & DISABLE_PARTIAL_IO_WARNINGS) {
545 os << " | DISABLE_PARTIAL_IO_WARNINGS";
546 }
547
548 if (m_type & DO_NOT_WAKE) {
549 os << " | IGNORE_MISSING";
550 }
551
552 if (m_type & PUNCH_HOLE) {
553 os << " | PUNCH_HOLE";
554 }
555
556 if (m_type & NO_COMPRESSION) {
557 os << " | NO_COMPRESSION";
558 }
559
560 os << ", comp: " << m_compression.to_string();
561 os << ", enc: " << m_encryption.to_string(m_encryption.get_type());
562
563 return (os.str());
564 }
565
566 /** Get a reference to the underlying encryption information.
567 @return reference to the encryption information. */
568 [[nodiscard]] Encryption &get_encryption_info() noexcept {
569 return m_encryption;
570 }
571
572 /** Set the encrypted block to the given value.
573 @param[in] eblock the encrypted block. */
574 void set_encrypted_block(const file::Block *eblock) noexcept {
575 m_eblock = eblock;
576 }
577
578 /** Get the encrypted block.
579 @return the encrypted block. */
580 [[nodiscard]] const file::Block *get_encrypted_block() const noexcept {
581 return m_eblock;
582 }
583
584 private:
585 /* File system best block size */
586 uint32_t m_block_size{};
587
588 /** Request type bit flags */
589 int m_type{};
590
591 /** Compression algorithm */
592 Compression m_compression{};
593
594 /** Encryption algorithm */
595 Encryption m_encryption{};
596
597 /** The encrypted block. */
598 const file::Block *m_eblock{};
599
600 /** The length of data in encrypted block. */
601 uint32_t m_elen{};
602
603 /** Length of the original IO size.
604 For reads it is an expected uncompressed length.
605 For writes it is a length up to which the write is to be extended with a punch
606 hole, if supported. */
607 uint32_t m_original_size{};
608 };
609
610 /** @} */
611
612 /** Sparse file size information. */
613 struct os_file_size_t {
614 /** Total size of file in bytes */
615 os_offset_t m_total_size;
616
617 /** If it is a sparse file then this is the number of bytes
618 actually allocated for the file. */
619 os_offset_t m_alloc_size;
620 };
621
622 /** Win NT does not allow more than 64 */
623 static const ulint OS_AIO_N_PENDING_IOS_PER_THREAD = 32;
624
625 /** Modes for aio operations @{ */
626 enum class AIO_mode : size_t {
627 /** Normal asynchronous i/o not for ibuf pages or ibuf bitmap pages */
628 NORMAL = 21,
629
630 /** Asynchronous i/o for ibuf pages or ibuf bitmap pages */
631 IBUF = 22,
632
633 /** Asynchronous i/o where the calling thread will itself wait for
634 the i/o to complete, doing also the job of the i/o-handler thread;
635 can be used for any pages, ibuf or non-ibuf. This is used to save
636 CPU time, as we can do with fewer thread switches. Plain synchronous
637 I/O is not as good, because it must serialize the file seek and read
638 or write, causing a bottleneck for parallelism. */
639 SYNC = 24
640 };
641 /** @} */
642
643 extern ulint os_n_file_reads;
644 extern ulint os_n_file_writes;
645 extern ulint os_n_fsyncs;
646
647 /* File types for directory entry data type */
648
649 enum os_file_type_t {
650 /** Get status failed. */
651 OS_FILE_TYPE_FAILED,
652
653 /** stat() failed, with ENAMETOOLONG */
654 OS_FILE_TYPE_NAME_TOO_LONG,
655
656 /** stat() failed with EACCESS */
657 OS_FILE_PERMISSION_ERROR,
658
659 /** File doesn't exist. */
660 OS_FILE_TYPE_MISSING,
661
662 /** File exists but type is unknown. */
663 OS_FILE_TYPE_UNKNOWN,
664
665 /** Ordinary file. */
666 OS_FILE_TYPE_FILE,
667
668 /** Directory. */
669 OS_FILE_TYPE_DIR,
670
671 /** Symbolic link. */
672 OS_FILE_TYPE_LINK,
673
674 /** Block device. */
675 OS_FILE_TYPE_BLOCK
676 };
677
678 /* Maximum path string length in bytes when referring to tables with in the
679 './databasename/tablename.ibd' path format; we can allocate at least 2 buffers
680 of this size from the thread stack; that is why this should not be made much
681 bigger than 4000 bytes. The maximum path length used by any storage engine
682 in the server must be at least this big. */
683 constexpr uint32_t OS_FILE_MAX_PATH = 4000;
684 static_assert(FN_REFLEN_SE >= OS_FILE_MAX_PATH,
685 "(FN_REFLEN_SE < OS_FILE_MAX_PATH)");
686
687 /** Struct used in fetching information of a file in a directory */
688 struct os_file_stat_t {
689 char name[OS_FILE_MAX_PATH]; /*!< path to a file */
690 os_file_type_t type; /*!< file type */
691 os_offset_t size; /*!< file size in bytes */
692 os_offset_t alloc_size; /*!< Allocated size for
693 sparse files in bytes */
694 uint32_t block_size; /*!< Block size to use for IO
695 in bytes*/
696 time_t ctime; /*!< creation time */
697 time_t mtime; /*!< modification time */
698 time_t atime; /*!< access time */
699 bool rw_perm; /*!< true if can be opened
700 in read-write mode. Only valid
701 if type == OS_FILE_TYPE_FILE */
702 };
703
704 #ifndef UNIV_HOTBACKUP
705 /** Create a temporary file. This function is like tmpfile(3). It will create
706 the file in the MySQL server configuration parameter (--tmpdir).
707 @return temporary file handle, or NULL on error */
708 FILE *os_file_create_tmpfile();
709 #endif /* !UNIV_HOTBACKUP */
710
711 /** This function attempts to create a directory named pathname. The new
712 directory gets default permissions. On Unix the permissions are
713 (0770 & ~umask). If the directory exists already, nothing is done and
714 the call succeeds, unless the fail_if_exists arguments is true.
715 If another error occurs, such as a permission error, this does not crash,
716 but reports the error and returns false.
717 @param[in] pathname directory name as null-terminated string
718 @param[in] fail_if_exists if true, pre-existing directory is treated as
719 an error.
720 @return true if call succeeds, false on error */
721 bool os_file_create_directory(const char *pathname, bool fail_if_exists);
722
723 /** Callback function type to be implemented by caller. It is called for each
724 entry in directory.
725 @param[in] path path to the file
726 @param[in] name name of the file */
727 typedef std::function<void(const char *path, const char *name)> os_dir_cbk_t;
728
729 /** This function scans the contents of a directory and invokes the callback
730 for each entry.
731 @param[in] path directory name as null-terminated string
732 @param[in] scan_cbk use callback to be called for each entry
733 @param[in] is_drop attempt to drop the directory after scan
734 @return true if call succeeds, false on error */
735 bool os_file_scan_directory(const char *path, os_dir_cbk_t scan_cbk,
736 bool is_drop);
737
738 /** NOTE! Use the corresponding macro
739 os_file_create_simple_no_error_handling(), not directly this function!
740 A simple function to open or create a file.
741 @param[in] name name of the file or path as a
742 null-terminated string
743 @param[in] create_mode create mode
744 @param[in] access_type OS_FILE_READ_ONLY, OS_FILE_READ_WRITE, or
745 OS_FILE_READ_ALLOW_DELETE; the last option
746 is used by a backup program reading the file
747 @param[in] read_only if true read only mode checks are enforced
748 @param[in] umask UNIX access permission to be set when creating a
749 file. Use os_umask_default to use global default
750 umask.
751 @param[out] success true if succeeded
752 @return own: handle to the file, not defined if error, error number
753 can be retrieved with os_file_get_last_error */
754 [[nodiscard]] pfs_os_file_t os_file_create_simple_no_error_handling_func(
755 const char *name, ulint create_mode, ulint access_type, bool read_only,
756 #ifndef _WIN32
757 mode_t umask,
758 #endif
759 bool *success);
760
761 /** Tries to disable OS caching on an opened file descriptor.
762 @param[in] fd file descriptor to alter
763 @param[in] file_name file name, used in the diagnostic message
764 @param[in] operation_name "open" or "create"; used in the diagnostic
765 message
766 @param[in] on_error_silent if true then don't print any message to the log
767 */
768 void os_file_set_nocache(int fd, const char *file_name,
769 const char *operation_name,
770 bool on_error_silent = false);
771 /** NOTE! Use the corresponding macro os_file_create(), not directly
772 this function!
773 Opens an existing file or creates a new.
774 @param[in] name name of the file or path as a null-terminated
775 string
776 @param[in] create_mode create mode
777 @param[in] purpose OS_DATA_FILE, OS_LOG_FILE etc.
778 @param[in] read_only if true read only mode checks are enforced
779 @param[out] success true if succeeded
780 @return own: handle to the file, not defined if error, error number
781 can be retrieved with os_file_get_last_error */
782 [[nodiscard]] pfs_os_file_t os_file_create_func(const char *name,
783 ulint create_mode,
784 ulint purpose, bool read_only,
785 bool *success);
786
787 /** Deletes a file. The file has to be closed before calling this.
788 @param[in] name file path as a null-terminated string
789 @return true if success */
790 bool os_file_delete_func(const char *name);
791
792 /** Deletes a file if it exists. The file has to be closed before calling this.
793 @param[in] name file path as a null-terminated string
794 @param[out] exist indicate if file pre-exist
795 @return true if success */
796 bool os_file_delete_if_exists_func(const char *name, bool *exist);
797
798 /** NOTE! Use the corresponding macro os_file_rename(), not directly
799 this function!
800 Renames a file (can also move it to another directory). It is safest that the
801 file is closed before calling this function.
802 @param[in] oldpath old file path as a null-terminated string
803 @param[in] newpath new file path
804 @return true if success */
805 bool os_file_rename_func(const char *oldpath, const char *newpath);
806
807 /** NOTE! Use the corresponding macro os_file_close(), not directly
808 this function!
809 Closes a file handle. In case of error, error number can be retrieved with
810 os_file_get_last_error.
811 @param[in] file Handle to a file
812 @return true if success */
813 bool os_file_close_func(os_file_t file);
814
815 #ifdef UNIV_PFS_IO
816
817 /* Keys to register InnoDB I/O with performance schema */
818 extern mysql_pfs_key_t innodb_log_file_key;
819 extern mysql_pfs_key_t innodb_temp_file_key;
820 extern mysql_pfs_key_t innodb_dblwr_file_key;
821 extern mysql_pfs_key_t innodb_arch_file_key;
822 extern mysql_pfs_key_t innodb_clone_file_key;
823 extern mysql_pfs_key_t innodb_data_file_key;
824 extern mysql_pfs_key_t innodb_tablespace_open_file_key;
825
826 /* Following four macros are instumentations to register
827 various file I/O operations with performance schema.
828 1) register_pfs_file_open_begin() and register_pfs_file_open_end() are
829 used to register file creation, opening and closing.
830 2) register_pfs_file_rename_begin() and register_pfs_file_rename_end()
831 are used to register file renaming.
832 3) register_pfs_file_io_begin() and register_pfs_file_io_end() are
833 used to register actual file read, write and flush
834 4) register_pfs_file_close_begin() and register_pfs_file_close_end()
835 are used to register file deletion operations*/
836 #define register_pfs_file_open_begin(state, locker, key, op, name, \
837 src_location) \
838 do { \
839 locker = PSI_FILE_CALL(get_thread_file_name_locker)(state, key.m_value, \
840 op, name, &locker); \
841 if (locker != nullptr) { \
842 PSI_FILE_CALL(start_file_open_wait) \
843 (locker, src_location.filename, static_cast<uint>(src_location.line)); \
844 } \
845 } while (0)
846
847 #define register_pfs_file_open_end(locker, file, result) \
848 do { \
849 if (locker != nullptr) { \
850 file.m_psi = PSI_FILE_CALL(end_file_open_wait)(locker, result); \
851 } \
852 } while (0)
853
854 #define register_pfs_file_rename_begin(state, locker, key, op, from, to, \
855 src_location) \
856 do { \
857 locker = PSI_FILE_CALL(get_thread_file_name_locker)(state, key.m_value, \
858 op, from, &locker); \
859 if (locker != nullptr) { \
860 PSI_FILE_CALL(start_file_rename_wait) \
861 (locker, (size_t)0, from, to, src_location.filename, \
862 static_cast<uint>(src_location.line)); \
863 } \
864 } while (0)
865
866 #define register_pfs_file_rename_end(locker, from, to, result) \
867 do { \
868 if (locker != nullptr) { \
869 PSI_FILE_CALL(end_file_rename_wait)(locker, from, to, result); \
870 } \
871 } while (0)
872
873 #define register_pfs_file_close_begin(state, locker, key, op, name, \
874 src_location) \
875 do { \
876 locker = PSI_FILE_CALL(get_thread_file_name_locker)(state, key.m_value, \
877 op, name, &locker); \
878 if (locker != nullptr) { \
879 PSI_FILE_CALL(start_file_close_wait) \
880 (locker, src_location.filename, static_cast<uint>(src_location.line)); \
881 } \
882 } while (0)
883
884 #define register_pfs_file_close_end(locker, result) \
885 do { \
886 if (locker != nullptr) { \
887 PSI_FILE_CALL(end_file_close_wait)(locker, result); \
888 } \
889 } while (0)
890
891 #define register_pfs_file_io_begin(state, locker, file, count, op, \
892 src_location) \
893 do { \
894 locker = \
895 PSI_FILE_CALL(get_thread_file_stream_locker)(state, file.m_psi, op); \
896 if (locker != nullptr) { \
897 PSI_FILE_CALL(start_file_wait) \
898 (locker, count, src_location.filename, \
899 static_cast<uint>(src_location.line)); \
900 } \
901 } while (0)
902
903 #define register_pfs_file_io_end(locker, count) \
904 do { \
905 if (locker != nullptr) { \
906 PSI_FILE_CALL(end_file_wait)(locker, count); \
907 } \
908 } while (0)
909
910 /* Following macros/functions are file I/O APIs that would be performance
911 schema instrumented if "UNIV_PFS_IO" is defined. They would point to
912 wrapper functions with performance schema instrumentation in such case.
913
914 os_file_create
915 os_file_create_simple_no_error_handling
916 os_file_close
917 os_file_rename
918 os_aio
919 os_file_read
920 os_file_read_no_error_handling
921 os_file_read_no_error_handling_int_fd
922 os_file_write
923
924 The wrapper functions have the prefix of "innodb_". */
925
926 #define os_file_create(key, name, create, purpose, read_only, success) \
927 pfs_os_file_create_func(key, name, create, purpose, read_only, success, \
928 UT_LOCATION_HERE)
929
930 #ifndef _WIN32
931 #define os_file_create_simple_no_error_handling(key, name, create_mode, \
932 access, read_only, success) \
933 pfs_os_file_create_simple_no_error_handling_func( \
934 key, name, create_mode, access, read_only, os_innodb_umask_default, \
935 success, UT_LOCATION_HERE)
936
937 #define os_file_create_simple_no_error_handling_with_umask( \
938 key, name, create_mode, access, read_only, umask, success) \
939 pfs_os_file_create_simple_no_error_handling_func(key, name, create_mode, \
940 access, read_only, umask, \
941 success, UT_LOCATION_HERE)
942 #else
943 #define os_file_create_simple_no_error_handling(key, name, create_mode, \
944 access, read_only, success) \
945 pfs_os_file_create_simple_no_error_handling_func( \
946 key, name, create_mode, access, read_only, success, UT_LOCATION_HERE)
947 #endif
948
949 #define os_file_close_pfs(file) pfs_os_file_close_func(file, UT_LOCATION_HERE)
950
951 #define os_aio(type, mode, name, file, buf, offset, n, read_only, message1, \
952 message2) \
953 pfs_os_aio_func(type, mode, name, file, buf, offset, n, read_only, message1, \
954 message2, UT_LOCATION_HERE)
955
956 #define os_file_read_pfs(type, file_name, file, buf, offset, n) \
957 pfs_os_file_read_func(type, file_name, file, buf, offset, n, UT_LOCATION_HERE)
958
959 #define os_file_read_first_page_pfs(type, file_name, file, buf, n) \
960 pfs_os_file_read_first_page_func(type, file_name, file, buf, n, \
961 UT_LOCATION_HERE)
962
963 #define os_file_copy_pfs(src, src_offset, dest, dest_offset, size) \
964 pfs_os_file_copy_func(src, src_offset, dest, dest_offset, size, \
965 UT_LOCATION_HERE)
966
967 #define os_file_read_no_error_handling_pfs(type, file_name, file, buf, offset, \
968 n, o) \
969 pfs_os_file_read_no_error_handling_func(type, file_name, file, buf, offset, \
970 n, o, UT_LOCATION_HERE)
971
972 #define os_file_read_no_error_handling_int_fd(type, file_name, file, buf, \
973 offset, n, o) \
974 pfs_os_file_read_no_error_handling_int_fd_func( \
975 type, file_name, file, buf, offset, n, o, UT_LOCATION_HERE)
976
977 #define os_file_write_pfs(type, name, file, buf, offset, n) \
978 pfs_os_file_write_func(type, name, file, buf, offset, n, UT_LOCATION_HERE)
979
980 #define os_file_write_int_fd(type, name, file, buf, offset, n) \
981 pfs_os_file_write_int_fd_func(type, name, file, buf, offset, n, \
982 UT_LOCATION_HERE)
983
984 #define os_file_flush_pfs(file) pfs_os_file_flush_func(file, UT_LOCATION_HERE)
985
986 #define os_file_rename(key, oldpath, newpath) \
987 pfs_os_file_rename_func(key, oldpath, newpath, UT_LOCATION_HERE)
988
989 #define os_file_delete(key, name) \
990 pfs_os_file_delete_func(key, name, UT_LOCATION_HERE)
991
992 #define os_file_delete_if_exists(key, name, exist) \
993 pfs_os_file_delete_if_exists_func(key, name, exist, UT_LOCATION_HERE)
994
995 /** NOTE! Please use the corresponding macro
996 os_file_create_simple_no_error_handling(), not directly this function!
997 A performance schema instrumented wrapper function for
998 os_file_create_simple_no_error_handling(). Add instrumentation to
999 monitor file creation/open.
1000 @param[in] key Performance Schema Key
1001 @param[in] name name of the file or path as a null-terminated
1002 string
1003 @param[in] create_mode create mode
1004 @param[in] access_type OS_FILE_READ_ONLY, OS_FILE_READ_WRITE, or
1005 OS_FILE_READ_ALLOW_DELETE; the last option is
1006 used by a backup program reading the file
1007 @param[in] read_only if true read only mode checks are enforced
1008 @param[in] umask UNIX access permission to be set when creating a
1009 file. Use os_umask_default to use global default
1010 umask.
1011 @param[out] success true if succeeded
1012 @param[in] src_location location where func invoked
1013 @return own: handle to the file, not defined if error, error number
1014 can be retrieved with os_file_get_last_error */
1015 [[nodiscard]] static inline pfs_os_file_t
1016 pfs_os_file_create_simple_no_error_handling_func(
1017 mysql_pfs_key_t key, const char *name, ulint create_mode, ulint access_type,
1018 bool read_only,
1019 #ifndef _WIN32
1020 mode_t umask,
1021 #endif
1022 bool *success, ut::Location src_location);
1023
1024 /** NOTE! Please use the corresponding macro os_file_create(), not directly
1025 this function!
1026 A performance schema wrapper function for os_file_create().
1027 Add instrumentation to monitor file creation/open.
1028 @param[in] key Performance Schema Key
1029 @param[in] name name of the file or path as a null-terminated
1030 string
1031 @param[in] create_mode create mode
1032 @param[in] purpose OS_DATA_FILE, OS_LOG_FILE etc.
1033 @param[in] read_only if true read only mode checks are enforced
1034 @param[out] success true if succeeded
1035 @param[in] src_location location where func invoked
1036 @return own: handle to the file, not defined if error, error number
1037 can be retrieved with os_file_get_last_error */
1038 [[nodiscard]] static inline pfs_os_file_t pfs_os_file_create_func(
1039 mysql_pfs_key_t key, const char *name, ulint create_mode, ulint purpose,
1040 bool read_only, bool *success, ut::Location src_location);
1041
1042 /** NOTE! Please use the corresponding macro os_file_close(), not directly
1043 this function!
1044 A performance schema instrumented wrapper function for os_file_close().
1045 @param[in] file handle to a file
1046 @param[in] src_location location where func invoked
1047 @return true if success */
1048 static inline bool pfs_os_file_close_func(pfs_os_file_t file,
1049 ut::Location src_location);
1050
1051 /** NOTE! Please use the corresponding macro os_file_read(), not directly
1052 this function!
1053 This is the performance schema instrumented wrapper function for
1054 os_file_read() which requests a synchronous read operation.
1055 @param[in, out] type IO request context
1056 @param[in] file_name file name
1057 @param[in] file Open file handle
1058 @param[out] buf buffer where to read
1059 @param[in] offset file offset where to read
1060 @param[in] n number of bytes to read
1061 @param[in] src_location location where func invoked
1062 @return DB_SUCCESS if request was successful */
1063 static inline dberr_t pfs_os_file_read_func(IORequest &type,
1064 const char *file_name,
1065 pfs_os_file_t file, void *buf,
1066 os_offset_t offset, ulint n,
1067 ut::Location src_location);
1068
1069 /** NOTE! Please use the corresponding macro os_file_read_first_page(),
1070 not directly this function!
1071 This is the performance schema instrumented wrapper function for
1072 os_file_read_first_page() which requests a synchronous read operation
1073 of page 0 of IBD file
1074 @param[in, out] type IO request context
1075 @param[in] file_name file name
1076 @param[in] file Open file handle
1077 @param[out] buf buffer where to read
1078 @param[in] n number of bytes to read
1079 @param[in] src_location location where func invoked
1080 @return DB_SUCCESS if request was successful */
1081 static inline dberr_t pfs_os_file_read_first_page_func(
1082 IORequest &type, const char *file_name, pfs_os_file_t file, void *buf,
1083 ulint n, ut::Location src_location);
1084
1085 /** copy data from one file to another file. Data is read/written
1086 at current file offset.
1087 @param[in] src file handle to copy from
1088 @param[in] src_offset offset to copy from
1089 @param[in] dest file handle to copy to
1090 @param[in] dest_offset offset to copy to
1091 @param[in] size number of bytes to copy
1092 @param[in] src_location location where func invoked
1093 @return DB_SUCCESS if successful */
1094 static inline dberr_t pfs_os_file_copy_func(pfs_os_file_t src,
1095 os_offset_t src_offset,
1096 pfs_os_file_t dest,
1097 os_offset_t dest_offset, uint size,
1098 ut::Location src_location);
1099
1100 /** NOTE! Please use the corresponding macro os_file_read_no_error_handling(),
1101 not directly this function!
1102 This is the performance schema instrumented wrapper function for
1103 os_file_read_no_error_handling_func() which requests a synchronous
1104 read operation.
1105 @param[in, out] type IO request context
1106 @param[in] file_name file name
1107 @param[in] file Open file handle
1108 @param[out] buf buffer where to read
1109 @param[in] offset file offset where to read
1110 @param[in] n number of bytes to read
1111 @param[out] o number of bytes actually read
1112 @param[in] src_location location where func invoked
1113 @return DB_SUCCESS if request was successful */
1114 static inline dberr_t pfs_os_file_read_no_error_handling_func(
1115 IORequest &type, const char *file_name, pfs_os_file_t file, void *buf,
1116 os_offset_t offset, ulint n, ulint *o, ut::Location src_location);
1117
1118 /** NOTE! Please use the corresponding macro
1119 os_file_read_no_error_handling_int_fd(), not directly this function!
1120 This is the performance schema instrumented wrapper function for
1121 os_file_read_no_error_handling_int_fd_func() which requests a
1122 synchronous read operation on files with int type descriptors.
1123 @param[in, out] type IO request context
1124 @param[in] file_name file name
1125 @param[in] file Open file handle
1126 @param[out] buf buffer where to read
1127 @param[in] offset file offset where to read
1128 @param[in] n number of bytes to read
1129 @param[out] o number of bytes actually read
1130 @param[in] src_location location where func invoked
1131 @return DB_SUCCESS if request was successful */
1132
1133 static inline dberr_t pfs_os_file_read_no_error_handling_int_fd_func(
1134 IORequest &type, const char *file_name, int file, void *buf,
1135 os_offset_t offset, ulint n, ulint *o, ut::Location src_location);
1136
1137 /** NOTE! Please use the corresponding macro os_aio(), not directly this
1138 function!
1139 Performance schema wrapper function of os_aio() which requests
1140 an asynchronous I/O operation.
1141 @param[in] type IO request context
1142 @param[in] mode IO mode
1143 @param[in] name Name of the file or path as NUL terminated
1144 string
1145 @param[in] file Open file handle
1146 @param[out] buf buffer where to read
1147 @param[in] offset file offset where to read
1148 @param[in] n how many bytes to read or write; this
1149 must not cross a file boundary; in AIO this must be a block size multiple
1150 @param[in] read_only if true read only mode checks are enforced
1151 @param[in,out] m1 Message for the AIO handler, (can be used to
1152 identify a completed AIO operation); ignored
1153 if mode is OS_AIO_SYNC
1154 @param[in,out] m2 message for the AIO handler (can be used to
1155 identify a completed AIO operation); ignored
1156 if mode is OS_AIO_SYNC
1157 @param[in] location location where func invoked
1158 @return DB_SUCCESS if request was queued successfully, false if fail */
1159 static inline dberr_t pfs_os_aio_func(IORequest &type, AIO_mode mode,
1160 const char *name, pfs_os_file_t file,
1161 void *buf, os_offset_t offset, ulint n,
1162 bool read_only, fil_node_t *m1, void *m2,
1163 ut::Location location);
1164
1165 /** NOTE! Please use the corresponding macro os_file_write(), not directly
1166 this function!
1167 This is the performance schema instrumented wrapper function for
1168 os_file_write() which requests a synchronous write operation.
1169 @param[in, out] type IO request context
1170 @param[in] name Name of the file or path as NUL terminated
1171 string
1172 @param[in] file Open file handle
1173 @param[out] buf buffer where to read
1174 @param[in] offset file offset where to read
1175 @param[in] n number of bytes to read
1176 @param[in] src_location location where func invoked
1177 @return DB_SUCCESS if request was successful */
1178 static inline dberr_t pfs_os_file_write_func(IORequest &type, const char *name,
1179 pfs_os_file_t file,
1180 const void *buf,
1181 os_offset_t offset, ulint n,
1182 ut::Location src_location);
1183
1184 /** NOTE! Please use the corresponding macro os_file_write(), not
1185 directly this function!
1186 This is the performance schema instrumented wrapper function for
1187 os_file_write() which requests a synchronous write operation
1188 on files with int type descriptors.
1189 @param[in, out] type IO request context
1190 @param[in] name Name of the file or path as NUL terminated
1191 string
1192 @param[in] file Open file handle
1193 @param[out] buf buffer where to read
1194 @param[in] offset file offset where to read
1195 @param[in] n number of bytes to read
1196 @param[in] src_location location where func invoked
1197 @return DB_SUCCESS if request was successful */
1198 static inline dberr_t pfs_os_file_write_int_fd_func(IORequest &type,
1199 const char *name, int file,
1200 const void *buf,
1201 os_offset_t offset, ulint n,
1202 ut::Location src_location);
1203
1204 /** NOTE! Please use the corresponding macro os_file_flush(), not directly
1205 this function!
1206 This is the performance schema instrumented wrapper function for
1207 os_file_flush() which flushes the write buffers of a given file to the disk.
1208 Flushes the write buffers of a given file to the disk.
1209 @param[in] file Open file handle
1210 @param[in] src_location location where func invoked
1211 @return true if success */
1212 static inline bool pfs_os_file_flush_func(pfs_os_file_t file,
1213 ut::Location src_location);
1214
1215 /** NOTE! Please use the corresponding macro os_file_rename(), not directly
1216 this function!
1217 This is the performance schema instrumented wrapper function for
1218 os_file_rename()
1219 @param[in] key Performance Schema Key
1220 @param[in] oldpath old file path as a null-terminated string
1221 @param[in] newpath new file path
1222 @param[in] src_location location where func invoked
1223 @return true if success */
1224 static inline bool pfs_os_file_rename_func(mysql_pfs_key_t key,
1225 const char *oldpath,
1226 const char *newpath,
1227 ut::Location src_location);
1228
1229 /**
1230 NOTE! Please use the corresponding macro os_file_delete(), not directly
1231 this function!
1232 This is the performance schema instrumented wrapper function for
1233 os_file_delete()
1234 @param[in] key Performance Schema Key
1235 @param[in] name old file path as a null-terminated string
1236 @param[in] src_location location where func invoked
1237 @return true if success */
1238 static inline bool pfs_os_file_delete_func(mysql_pfs_key_t key,
1239 const char *name,
1240 ut::Location src_location);
1241
1242 /**
1243 NOTE! Please use the corresponding macro os_file_delete_if_exists(), not
1244 directly this function!
1245 This is the performance schema instrumented wrapper function for
1246 os_file_delete_if_exists()
1247 @param[in] key Performance Schema Key
1248 @param[in] name old file path as a null-terminated string
1249 @param[in] exist indicate if file pre-exist
1250 @param[in] src_location location where func invoked
1251 @return true if success */
1252 static inline bool pfs_os_file_delete_if_exists_func(mysql_pfs_key_t key,
1253 const char *name,
1254 bool *exist,
1255 ut::Location src_location);
1256
1257 #else /* UNIV_PFS_IO */
1258
1259 /* If UNIV_PFS_IO is not defined, these I/O APIs point
1260 to original un-instrumented file I/O APIs */
1261
1262 #define os_file_create(key, name, create, purpose, read_only, success) \
1263 os_file_create_func(name, create, purpose, read_only, success)
1264
1265 #ifndef _WIN32
1266
1267 #define os_file_create_simple_no_error_handling(key, name, create_mode, \
1268 access, read_only, success) \
1269 os_file_create_simple_no_error_handling_func( \
1270 name, create_mode, access, read_only, os_innodb_umask_default, success)
1271
1272 #define os_file_create_simple_no_error_handling_with_umask( \
1273 key, name, create_mode, access, read_only, umask, success) \
1274 os_file_create_simple_no_error_handling_func(name, create_mode, access, \
1275 read_only, umask, success)
1276
1277 #else
1278
1279 #define os_file_create_simple_no_error_handling(key, name, create_mode, \
1280 access, read_only, success) \
1281 os_file_create_simple_no_error_handling_func(name, create_mode, access, \
1282 read_only, success)
1283
1284 #endif
1285
1286 #define os_file_close_pfs(file) os_file_close_func(file)
1287
1288 #define os_aio(type, mode, name, file, buf, offset, n, read_only, message1, \
1289 message2) \
1290 os_aio_func(type, mode, name, file, buf, offset, n, read_only, message1, \
1291 message2)
1292
1293 #define os_file_read_pfs(type, file_name, file, buf, offset, n) \
1294 os_file_read_func(type, file_name, file, buf, offset, n)
1295
1296 #define os_file_read_first_page_pfs(type, file_name, file, buf, n) \
1297 os_file_read_first_page_func(type, file_name, file, buf, n)
1298
1299 #define os_file_copy_pfs(src, src_offset, dest, dest_offset, size) \
1300 os_file_copy_func(src, src_offset, dest, dest_offset, size)
1301
1302 #define os_file_read_no_error_handling_pfs(type, file_name, file, buf, offset, \
1303 n, o) \
1304 os_file_read_no_error_handling_func(type, file_name, file, buf, offset, n, o)
1305
1306 #define os_file_read_no_error_handling_int_fd(type, file_name, file, buf, \
1307 offset, n, o) \
1308 os_file_read_no_error_handling_func(type, file_name, OS_FILE_FROM_FD(file), \
1309 buf, offset, n, o)
1310
1311 #define os_file_write_pfs(type, name, file, buf, offset, n) \
1312 os_file_write_func(type, name, file, buf, offset, n)
1313
1314 #define os_file_write_int_fd(type, name, file, buf, offset, n) \
1315 os_file_write_func(type, name, OS_FILE_FROM_FD(file), buf, offset, n)
1316
1317 #define os_file_flush_pfs(file) os_file_flush_func(file)
1318
1319 #define os_file_rename(key, oldpath, newpath) \
1320 os_file_rename_func(oldpath, newpath)
1321
1322 #define os_file_delete(key, name) os_file_delete_func(name)
1323
1324 #define os_file_delete_if_exists(key, name, exist) \
1325 os_file_delete_if_exists_func(name, exist)
1326
1327 #endif /* UNIV_PFS_IO */
1328
1329 #ifdef UNIV_PFS_IO
1330 #define os_file_close(file) os_file_close_pfs(file)
1331 #else
1332 #define os_file_close(file) os_file_close_pfs((file).m_file)
1333 #endif
1334
1335 #ifdef UNIV_PFS_IO
1336 #define os_file_read(type, file_name, file, buf, offset, n) \
1337 os_file_read_pfs(type, file_name, file, buf, offset, n)
1338 #else
1339 #define os_file_read(type, file_name, file, buf, offset, n) \
1340 os_file_read_pfs(type, file_name, file.m_file, buf, offset, n)
1341 #endif
1342
1343 #ifdef UNIV_PFS_IO
1344 #define os_file_read_first_page(type, file_name, file, buf, n) \
1345 os_file_read_first_page_pfs(type, file_name, file, buf, n)
1346 #else
1347 #define os_file_read_first_page(type, file_name, file, buf, n) \
1348 os_file_read_first_page_pfs(type, file_name, file.m_file, buf, n)
1349 #endif
1350
1351 #ifdef UNIV_PFS_IO
1352 #define os_file_flush(file) os_file_flush_pfs(file)
1353 #else
1354 #define os_file_flush(file) os_file_flush_pfs(file.m_file)
1355 #endif
1356
1357 #ifdef UNIV_PFS_IO
1358 #define os_file_write(type, name, file, buf, offset, n) \
1359 os_file_write_pfs(type, name, file, buf, offset, n)
1360 #else
1361 #define os_file_write(type, name, file, buf, offset, n) \
1362 os_file_write_pfs(type, name, file.m_file, buf, offset, n)
1363 #endif
1364
1365 #ifdef UNIV_PFS_IO
1366 #define os_file_copy(src, src_offset, dest, dest_offset, size) \
1367 os_file_copy_pfs(src, src_offset, dest, dest_offset, size)
1368 #else
1369 #define os_file_copy(src, src_offset, dest, dest_offset, size) \
1370 os_file_copy_pfs(src.m_file, src_offset, dest.m_file, dest_offset, size)
1371 #endif
1372
1373 #ifdef UNIV_PFS_IO
1374 #define os_file_read_no_error_handling(type, file_name, file, buf, offset, n, \
1375 o) \
1376 os_file_read_no_error_handling_pfs(type, file_name, file, buf, offset, n, o)
1377 #else
1378 #define os_file_read_no_error_handling(type, file_name, file, buf, offset, n, \
1379 o) \
1380 os_file_read_no_error_handling_pfs(type, file_name, file.m_file, buf, \
1381 offset, n, o)
1382 #endif
1383
1384 #ifdef UNIV_HOTBACKUP
1385 /** Closes a file handle.
1386 @param[in] file handle to a file
1387 @return true if success */
1388 bool os_file_close_no_error_handling(os_file_t file);
1389 #endif /* UNIV_HOTBACKUP */
1390
1391 /** Gets a file size.
1392 @param[in] filename Full path to the filename to check
1393 @return file size if OK, else set m_total_size to ~0 and m_alloc_size to
1394 errno. */
1395 [[nodiscard]] os_file_size_t os_file_get_size(const char *filename);
1396
1397 /** Gets a file size.
1398 @param[in] file Handle to a file
1399 @return file size, or (os_offset_t) -1 on failure */
1400 [[nodiscard]] os_offset_t os_file_get_size(pfs_os_file_t file);
1401
1402 /** Allocate a block to file using fallocate from the given offset if
1403 fallocate is supported. Falls back to the old slower method of writing
1404 zeros otherwise.
1405 @param[in] name name of the file
1406 @param[in] file handle to the file
1407 @param[in] offset file offset
1408 @param[in] size file size
1409 @param[in] flush flush file content to disk
1410 @return true if success */
1411 [[nodiscard]] bool os_file_set_size_fast(const char *name, pfs_os_file_t file,
1412 os_offset_t offset, os_offset_t size,
1413 bool flush);
1414
1415 /** Write the specified number of zeros to a file from specific offset.
1416 @param[in] name name of the file or path as a null-terminated
1417 string
1418 @param[in] file handle to a file
1419 @param[in] offset file offset
1420 @param[in] size file size
1421 @param[in] flush flush file content to disk
1422 @return true if success */
1423 [[nodiscard]] bool os_file_set_size(const char *name, pfs_os_file_t file,
1424 os_offset_t offset, os_offset_t size,
1425 bool flush);
1426
1427 /** Truncates a file at its current position.
1428 @param[in,out] file file to be truncated
1429 @return true if success */
1430 bool os_file_set_eof(FILE *file); /*!< in: file to be truncated */
1431
1432 /** Truncates a file to a specified size in bytes.
1433 Do nothing if the size to preserve is greater or equal to the current
1434 size of the file.
1435 @param[in] pathname file path
1436 @param[in] file file to be truncated
1437 @param[in] size size to preserve in bytes
1438 @return true if success */
1439 bool os_file_truncate(const char *pathname, pfs_os_file_t file,
1440 os_offset_t size);
1441
1442 /** Set read/write position of a file handle to specific offset.
1443 @param[in] pathname file path
1444 @param[in] file file handle
1445 @param[in] offset read/write offset
1446 @return true if success */
1447 bool os_file_seek(const char *pathname, os_file_t file, os_offset_t offset);
1448
1449 /** NOTE! Use the corresponding macro os_file_flush(), not directly this
1450 function!
1451 Flushes the write buffers of a given file to the disk.
1452 @param[in] file handle to a file
1453 @return true if success */
1454 bool os_file_flush_func(os_file_t file);
1455
1456 /** Retrieves the last error number if an error occurs in a file io function.
1457 The number should be retrieved before any other OS calls (because they may
1458 overwrite the error number). If the number is not known to this program,
1459 the OS error number + 100 is returned.
1460 @param[in] report_all_errors true if we want an error message printed
1461 for all errors
1462 @return error number, or OS error number + 100 */
1463 ulint os_file_get_last_error(bool report_all_errors);
1464
1465 /** NOTE! Use the corresponding macro os_file_read_first_page(), not directly
1466 this function!
1467 Requests a synchronous read operation of page 0 of IBD file.
1468 @param[in] type IO request context
1469 @param[in] file_name file name
1470 @param[in] file Open file handle
1471 @param[out] buf buffer where to read
1472 @param[in] offset file offset where to read
1473 @param[in] n number of bytes to read
1474 @return DB_SUCCESS if request was successful, DB_IO_ERROR on failure */
1475 [[nodiscard]] dberr_t os_file_read_func(IORequest &type, const char *file_name,
1476 os_file_t file, void *buf,
1477 os_offset_t offset, ulint n);
1478
1479 /** NOTE! Use the corresponding macro os_file_read_first_page(),
1480 not directly this function!
1481 Requests a synchronous read operation of page 0 of IBD file
1482 @param[in] type IO request context
1483 @param[in] file_name file name
1484 @param[in] file Open file handle
1485 @param[out] buf buffer where to read
1486 @param[in] n number of bytes to read
1487 @return DB_SUCCESS if request was successful, DB_IO_ERROR on failure */
1488 [[nodiscard]] dberr_t os_file_read_first_page_func(IORequest &type,
1489 const char *file_name,
1490 os_file_t file, void *buf,
1491 ulint n);
1492
1493 /** Copy data from one file to another file. Data is read/written
1494 at current file offset.
1495 @param[in] src_file file handle to copy from
1496 @param[in] src_offset offset to copy from
1497 @param[in] dest_file file handle to copy to
1498 @param[in] dest_offset offset to copy to
1499 @param[in] size number of bytes to copy
1500 @return DB_SUCCESS if successful */
1501 [[nodiscard]] dberr_t os_file_copy_func(os_file_t src_file,
1502 os_offset_t src_offset,
1503 os_file_t dest_file,
1504 os_offset_t dest_offset, uint size);
1505
1506 /** Rewind file to its start, read at most size - 1 bytes from it to str, and
1507 NUL-terminate str. All errors are silently ignored. This function is
1508 mostly meant to be used with temporary files.
1509 @param[in,out] file File to read from
1510 @param[in,out] str Buffer where to read
1511 @param[in] size Size of buffer */
1512 void os_file_read_string(FILE *file, char *str, ulint size);
1513
1514 /** NOTE! Use the corresponding macro os_file_read_no_error_handling(),
1515 not directly this function!
1516 Requests a synchronous positioned read operation. This function does not do
1517 any error handling. In case of error it returns false.
1518 @param[in] type IO request context
1519 @param[in] file_name file name
1520 @param[in] file Open file handle
1521 @param[out] buf buffer where to read
1522 @param[in] offset file offset where to read
1523 @param[in] n number of bytes to read
1524 @param[out] o number of bytes actually read
1525 @return DB_SUCCESS or error code */
1526 [[nodiscard]] dberr_t os_file_read_no_error_handling_func(
1527 IORequest &type, const char *file_name, os_file_t file, void *buf,
1528 os_offset_t offset, ulint n, ulint *o);
1529
1530 /** NOTE! Use the corresponding macro os_file_write(), not directly this
1531 function!
1532 Requests a synchronous write operation.
1533 @param[in,out] type IO request context
1534 @param[in] name name of the file or path as a null-terminated
1535 string
1536 @param[in] file Open file handle
1537 @param[out] buf buffer where to read
1538 @param[in] offset file offset where to read
1539 @param[in] n number of bytes to read
1540 @return DB_SUCCESS if request was successful */
1541 [[nodiscard]] dberr_t os_file_write_func(IORequest &type, const char *name,
1542 os_file_t file, const void *buf,
1543 os_offset_t offset, ulint n);
1544
1545 /** Check the existence and type of a given path.
1546 @param[in] path pathname of the file
1547 @param[out] exists true if file exists
1548 @param[out] type type of the file (if it exists)
1549 @return true if call succeeded */
1550 bool os_file_status(const char *path, bool *exists, os_file_type_t *type);
1551
1552 /** Check the existence and usefulness of a given path.
1553 @param[in] path path name
1554 @retval true if the path exists and can be used
1555 @retval false if the path does not exist or if the path is
1556 unusable to get to a possibly existing file or directory. */
1557 bool os_file_exists(const char *path);
1558
1559 /** Create all missing subdirectories along the given path.
1560 @return DB_SUCCESS if OK, otherwise error code. */
1561 dberr_t os_file_create_subdirs_if_needed(const char *path);
1562
1563 #ifdef UNIV_ENABLE_UNIT_TEST_GET_PARENT_DIR
1564 /* Test the function os_file_get_parent_dir. */
1565 void unit_test_os_file_get_parent_dir();
1566 #endif /* UNIV_ENABLE_UNIT_TEST_GET_PARENT_DIR */
1567
1568 #ifdef UNIV_HOTBACKUP
1569 /** Deallocates the "Blocks" in block_cache */
1570 void meb_free_block_cache();
1571 #endif /* UNIV_HOTBACKUP */
1572
1573 /** Creates and initializes block_cache. Creates array of MAX_BLOCKS
1574 and allocates the memory in each block to hold BUFFER_BLOCK_SIZE
1575 of data.
1576
1577 This function is called by InnoDB during srv_start().
1578 It is also called by MEB while applying the redo logs on TDE tablespaces,
1579 the "Blocks" allocated in this block_cache are used to hold the decrypted
1580 page data. */
1581 void os_create_block_cache();
1582
1583 /** Initializes the asynchronous io system.
1584 Creates an array for ibuf i/o (if not in read-only mode).
1585 Also creates one array each for read and write where each
1586 array is divided logically into n_readers and n_writers
1587 respectively. The caller must create an i/o handler thread for each
1588 segment in these arrays by calling os_aio_start_threads().
1589
1590 @param[in] n_readers number of reader threads
1591 @param[in] n_writers number of writer threads */
1592 bool os_aio_init(ulint n_readers, ulint n_writers);
1593
1594 /** Starts one thread for each segment created in os_aio_init */
1595 void os_aio_start_threads();
1596
1597 /**
1598 Frees the asynchronous io system. */
1599 void os_aio_free();
1600
1601 /**
1602 NOTE! Use the corresponding macro os_aio(), not directly this function!
1603 Requests an asynchronous i/o operation.
1604 @param[in] type IO request context
1605 @param[in] aio_mode IO mode
1606 @param[in] name Name of the file or path as NUL terminated
1607 string
1608 @param[in] file Open file handle
1609 @param[out] buf buffer where to read
1610 @param[in] offset file offset where to read
1611 @param[in] n how many bytes to read or write; this
1612 must not cross a file boundary; in AIO this must be a block size multiple
1613 @param[in] read_only if true read only mode checks are enforced
1614 @param[in,out] m1 Message for the AIO handler, (can be used to
1615 identify a completed AIO operation); ignored if mode is OS_AIO_SYNC
1616 @param[in,out] m2 message for the AIO handler (can be used to
1617 identify a completed AIO operation); ignored if mode is OS_AIO_SYNC
1618 @return DB_SUCCESS or error code */
1619 dberr_t os_aio_func(IORequest &type, AIO_mode aio_mode, const char *name,
1620 pfs_os_file_t file, void *buf, os_offset_t offset, ulint n,
1621 bool read_only, fil_node_t *m1, void *m2);
1622
1623 /** Wakes up all async i/o threads so that they know to exit themselves in
1624 shutdown. */
1625 void os_aio_wake_all_threads_at_shutdown();
1626
1627 /** Waits until there are no pending writes in os_aio_write_array. There can
1628 be other, synchronous, pending writes. */
1629 void os_aio_wait_until_no_pending_writes();
1630
1631 /** Wakes up simulated aio i/o-handler threads if they have something to do. */
1632 void os_aio_simulated_wake_handler_threads();
1633
1634 /** This function can be called if one wants to post a batch of reads and
1635 prefers an i/o-handler thread to handle them all at once later. You must
1636 call os_aio_simulated_wake_handler_threads later to ensure the threads
1637 are not left sleeping! */
1638 void os_aio_simulated_put_read_threads_to_sleep();
1639
1640 /** Waits for an AIO operation to complete. This function is used to wait the
1641 for completed requests. The AIO array of pending requests is divided
1642 into segments. The thread specifies which segment or slot it wants to wait
1643 for. NOTE: this function will also take care of freeing the AIO slot,
1644 therefore no other thread is allowed to do the freeing!
1645 @param[in] segment The number of the segment in the AIO arrays to
1646 wait for; segment 0 is the ibuf I/O thread,
1647 then follow the non-ibuf read threads,
1648 and as the last are the non-ibuf write threads
1649 @param[out] m1 the messages passed with the AIO request; note
1650 that also in the case where the AIO operation
1651 failed, these output parameters are valid and
1652 can be used to restart the operation,
1653 for example
1654 @param[out] m2 callback message
1655 @param[out] request OS_FILE_WRITE or ..._READ
1656 @return DB_SUCCESS or error code */
1657 dberr_t os_aio_handler(ulint segment, fil_node_t **m1, void **m2,
1658 IORequest *request);
1659
1660 /** Prints info of the aio arrays.
1661 @param[in,out] file file where to print */
1662 void os_aio_print(FILE *file);
1663
1664 /** Refreshes the statistics used to print per-second averages. */
1665 void os_aio_refresh_stats();
1666
1667 /** Checks that all slots in the system have been freed, that is, there are
1668 no pending io operations. */
1669 bool os_aio_all_slots_free();
1670
1671 #ifdef UNIV_DEBUG
1672
1673 /** Prints all pending IO
1674 @param[in] file File where to print */
1675 void os_aio_print_pending_io(FILE *file);
1676
1677 #endif /* UNIV_DEBUG */
1678
1679 /** Get available free space on disk
1680 @param[in] path pathname of a directory or file in disk
1681 @param[out] free_space free space available in bytes
1682 @return DB_SUCCESS if all OK */
1683 dberr_t os_get_free_space(const char *path, uint64_t &free_space);
1684
1685 /** This function returns information about the specified file
1686 @param[in] path pathname of the file
1687 @param[out] stat_info information of a file in a directory
1688 @param[in] check_rw_perm for testing whether the file can be opened
1689 in RW mode
1690 @param[in] read_only true if file is opened in read-only mode
1691 @return DB_SUCCESS if all OK */
1692 dberr_t os_file_get_status(const char *path, os_file_stat_t *stat_info,
1693 bool check_rw_perm, bool read_only);
1694
1695 /** Check if a file can be opened in read-write mode.
1696 @param[in] name filename to check
1697 @param[in] read_only true if check for read-only mode only
1698 @retval true if file can be opened in the specified mode (rw or ro);
1699 or file does not exist
1700 @retval false if file exists and can't be opened in the specified mode */
1701 bool os_file_check_mode(const char *name, bool read_only);
1702
1703 #ifndef UNIV_HOTBACKUP
1704
1705 /** return any of the tmpdir path */
1706 char *innobase_mysql_tmpdir();
1707
1708 /** Creates a temporary file in the location specified by the parameter
1709 path. If the path is NULL, then it will be created in --tmpdir.
1710 @param[in] path location for creating temporary file
1711 @return temporary file descriptor, or OS_FD_CLOSED on error */
1712 os_fd_t innobase_mysql_tmpfile(const char *path);
1713
1714 #endif /* !UNIV_HOTBACKUP */
1715
1716 /** If it is a compressed page return the compressed page data + footer size
1717 @param[in] buf Buffer to check, must include header + 10 bytes
1718 @return ULINT_UNDEFINED if the page is not a compressed page or length
1719 of the compressed data (including footer) if it is a compressed page */
1720 ulint os_file_compressed_page_size(const byte *buf);
1721
1722 /** If it is a compressed page return the original page data + footer size
1723 @param[in] buf Buffer to check, must include header + 10 bytes
1724 @return ULINT_UNDEFINED if the page is not a compressed page or length
1725 of the original data + footer if it is a compressed page */
1726 ulint os_file_original_page_size(const byte *buf);
1727
1728 #ifndef _WIN32
1729 /** Set the global file create umask. This value is to be set once, at startup
1730 and never modified.
1731 @param[in] umask The umask to use for all InnoDB file creation.
1732 */
1733 void os_file_set_umask(mode_t umask);
1734
1735 /** A magic constant for the umask parameter that indicates caller wants the
1736 `os_innodb_umask` value to be used. The `os_innodb_umask` is a static value,
1737 private to this module, and to the file creation methods, so it should not be
1738 used directly. */
1739 constexpr mode_t os_innodb_umask_default = std::numeric_limits<mode_t>::max();
1740
1741 #endif
1742
1743 /** Free storage space associated with a section of the file.
1744 @param[in] fh Open file handle
1745 @param[in] off Starting offset (SEEK_SET)
1746 @param[in] len Size of the hole
1747 @return DB_SUCCESS or error code */
1748 [[nodiscard]] dberr_t os_file_punch_hole(os_file_t fh, os_offset_t off,
1749 os_offset_t len);
1750
1751 /** Check if the file system supports sparse files.
1752
1753 Warning: On POSIX systems we try and punch a hole from offset 0 to
1754 the system configured page size. This should only be called on an empty
1755 file.
1756
1757 Note: On Windows we use the name and on Unices we use the file handle.
1758
1759 @param[in] fh File handle for the file - if opened
1760 @return true if the file system supports sparse files */
1761 [[nodiscard]] bool os_is_sparse_file_supported(pfs_os_file_t fh);
1762
1763 /** Decompress the page data contents. Page type must be FIL_PAGE_COMPRESSED, if
1764 not then the source contents are left unchanged and DB_SUCCESS is returned.
1765 @param[in] dblwr_read true of double write recovery in progress
1766 @param[in,out] src Data read from disk, decompressed data will be
1767 copied to this page
1768 @param[in,out] dst Scratch area to use for decompression or
1769 nullptr.
1770 @param[in] dst_len If dst is valid, then size of the scratch area
1771 in bytes
1772 @return DB_SUCCESS or error code */
1773 [[nodiscard]] dberr_t os_file_decompress_page(bool dblwr_read, byte *src,
1774 byte *dst, ulint dst_len);
1775
1776 /** Compress a data page
1777 @param[in] compression Compression algorithm
1778 @param[in] block_size File system block size
1779 @param[in] src Source contents to compress
1780 @param[in] src_len Length in bytes of the source
1781 @param[out] dst Compressed page contents
1782 @param[out] dst_len Length in bytes of dst contents
1783 @return buffer data, dst_len will have the length of the data */
1784 byte *os_file_compress_page(Compression compression, ulint block_size,
1785 byte *src, ulint src_len, byte *dst,
1786 ulint *dst_len);
1787
1788 /** Determine if O_DIRECT is supported.
1789 @retval true if O_DIRECT is supported.
1790 @retval false if O_DIRECT is not supported. */
1791 [[nodiscard]] bool os_is_o_direct_supported();
1792
1793 /** Fill the pages with NULs
1794 @param[in] file File handle
1795 @param[in] name File name
1796 @param[in] page_size physical page size
1797 @param[in] start Offset from the start of the file in bytes
1798 @param[in] len Length in bytes
1799 @return DB_SUCCESS or error code */
1800 [[nodiscard]] dberr_t os_file_write_zeros(pfs_os_file_t file, const char *name,
1801 ulint page_size, os_offset_t start,
1802 ulint len);
1803
1804 #ifndef UNIV_NONINL
1805 /** Class to scan the directory hierarchy using a depth first scan. */
1806 class Dir_Walker {
1807 public:
1808 using Path = std::string;
1809
1810 /** Check if the path is a directory. The file/directory must exist.
1811 @param[in] path The path to check
1812 @return true if it is a directory */
1813 static bool is_directory(const Path &path);
1814
1815 /** Depth first traversal of the directory starting from basedir
1816 @param[in] basedir Start scanning from this directory
1817 @param[in] recursive `true` if scan should be recursive
1818 @param[in] f Function to call for each entry */
1819 template <typename F>
1820 static void walk(const Path &basedir, bool recursive, F &&f) {
1821 #ifdef _WIN32
1822 walk_win32(basedir, recursive, [&](const Path &path, size_t) { f(path); });
1823 #else
1824 walk_posix(basedir, recursive, [&](const Path &path, size_t) { f(path); });
1825 #endif /* _WIN32 */
1826 }
1827
1828 private:
1829 /** Directory names for the depth first directory scan. */
1830 struct Entry {
1831 /** Constructor
1832 @param[in] path Directory to traverse
1833 @param[in] depth Relative depth to the base
1834 directory in walk() */
1835 Entry(const Path &path, size_t depth) : m_path(path), m_depth(depth) {}
1836
1837 /** Path to the directory */
1838 Path m_path;
1839
1840 /** Relative depth of m_path */
1841 size_t m_depth;
1842 };
1843
1844 using Function = std::function<void(const Path &, size_t)>;
1845
1846 /** Depth first traversal of the directory starting from basedir
1847 @param[in] basedir Start scanning from this directory
1848 @param[in] recursive `true` if scan should be recursive
1849 @param[in] f Function to call for each entry */
1850 #ifdef _WIN32
1851 static void walk_win32(const Path &basedir, bool recursive, Function &&f);
1852 #else
1853 static void walk_posix(const Path &basedir, bool recursive, Function &&f);
1854 #endif /* _WIN32 */
1855 };
1856
1857 /** Allocate a page for sync IO
1858 @return pointer to page */
1859 [[nodiscard]] file::Block *os_alloc_block() noexcept;
1860
1861 /** Get the sector aligned frame pointer.
1862 @param[in] block the memory block containing the page frame.
1863 @return the sector aligned frame pointer. */
1864 [[nodiscard]] byte *os_block_get_frame(const file::Block *block) noexcept;
1865
1866 /** Free a page after sync IO
1867 @param[in,out] block The block to free/release */
1868 void os_free_block(file::Block *block) noexcept;
1869
1870 inline void file::Block::free(file::Block *obj) noexcept { os_free_block(obj); }
1871
1872 /** Encrypt a page content when write it to disk.
1873 @param[in] type IO flags
1874 @param[out] buf buffer to read or write
1875 @param[in] n number of bytes to read/write, starting from
1876 offset
1877 @return pointer to the encrypted page */
1878 file::Block *os_file_encrypt_page(const IORequest &type, void *&buf, ulint n);
1879
1880 /** Allocate the buffer for IO on a transparently compressed table.
1881 @param[in] type IO flags
1882 @param[out] buf buffer to read or write
1883 @param[in,out] n number of bytes to read/write, starting from
1884 offset
1885 @return pointer to allocated page, compressed data is written to the offset
1886 that is aligned on the disk sector size */
1887 file::Block *os_file_compress_page(IORequest &type, void *&buf, ulint *n);
1888
1889 /** This is a wrapper function for the os_file_write() function call. The
1890 purpose of this wrapper function is to retry on i/o error. On I/O error
1891 (perhaps because of disk full situation) keep retrying the write operation
1892 till it succeeds.
1893 @param[in] type IO flags
1894 @param[in] name name of the file or path as a null-terminated string
1895 @param[in] file handle to an open file
1896 @param[out] buf buffer from which to write
1897 @param[in] offset file offset from the start where to read
1898 @param[in] n number of bytes to read, starting from offset
1899 @return DB_SUCCESS if request was successful, false if fail */
1900 dberr_t os_file_write_retry(IORequest &type, const char *name,
1901 pfs_os_file_t file, const void *buf,
1902 os_offset_t offset, ulint n);
1903
1904 #include "os0file.ic"
1905 #endif /* UNIV_NONINL */
1906
1907 #endif /* os0file_h */