1 /***********************************************************************
2 
3 Copyright (c) 1995, 2017, Oracle and/or its affiliates. All Rights Reserved.
4 Copyright (c) 2009, Percona Inc.
5 Copyright (c) 2013, 2021, MariaDB Corporation.
6 
7 Portions of this file contain modifications contributed and copyrighted
8 by Percona Inc.. Those modifications are
9 gratefully acknowledged and are described briefly in the InnoDB
10 documentation. The contributions by Percona Inc. are incorporated with
11 their permission, and subject to the conditions contained in the file
12 COPYING.Percona.
13 
14 This program is free software; you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by the
16 Free Software Foundation; version 2 of the License.
17 
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
21 Public License for more details.
22 
23 You should have received a copy of the GNU General Public License along with
24 this program; if not, write to the Free Software Foundation, Inc.,
25 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
26 
27 ***********************************************************************/
28 
29 /**************************************************//**
30 @file include/os0file.h
31 The interface to the operating system file io
32 
33 Created 10/21/1995 Heikki Tuuri
34 *******************************************************/
35 
36 #ifndef os0file_h
37 #define os0file_h
38 
39 #include "page0size.h"
40 #include "os0api.h"
41 
42 #ifndef _WIN32
43 #include <dirent.h>
44 #include <sys/stat.h>
45 #include <time.h>
46 #endif /* !_WIN32 */
47 
48 /** File node of a tablespace or the log data space */
49 struct fil_node_t;
50 struct fil_space_t;
51 
52 extern bool	os_has_said_disk_full;
53 
54 /** File offset in bytes */
55 typedef ib_uint64_t os_offset_t;
56 
57 #ifdef _WIN32
58 
59 typedef HANDLE	os_file_dir_t;	/*!< directory stream */
60 
61 /** We define always WIN_ASYNC_IO, and check at run-time whether
62 the OS actually supports it: Win 95 does not, NT does. */
63 # define WIN_ASYNC_IO
64 
65 /** Use unbuffered I/O */
66 # define UNIV_NON_BUFFERED_IO
67 
68 /** File handle */
69 typedef HANDLE os_file_t;
70 
71 
72 #else /* _WIN32 */
73 
74 typedef DIR*	os_file_dir_t;	/*!< directory stream */
75 
76 /** File handle */
77 typedef int	os_file_t;
78 
79 #endif /* _WIN32 */
80 
81 static const os_file_t OS_FILE_CLOSED = IF_WIN(os_file_t(INVALID_HANDLE_VALUE),-1);
82 
83 /** File descriptor with optional PERFORMANCE_SCHEMA instrumentation */
84 struct pfs_os_file_t
85 {
86 	/** Default constructor */
m_filepfs_os_file_t87 	pfs_os_file_t(os_file_t file = OS_FILE_CLOSED) : m_file(file)
88 #ifdef UNIV_PFS_IO
89 	, m_psi(NULL)
90 #endif
91 	{}
92 
93 	/** The wrapped file handle */
94 	os_file_t   m_file;
95 #ifdef UNIV_PFS_IO
96 	/** PERFORMANCE_SCHEMA descriptor */
97 	struct PSI_file *m_psi;
98 #endif
99 	/** Implicit type conversion.
100 	@return the wrapped file handle */
os_file_tpfs_os_file_t101 	operator os_file_t() const { return m_file; }
102 	/** Assignment operator.
103 	@param[in]	file	file handle to be assigned */
104 	void operator=(os_file_t file) { m_file = file; }
105 };
106 
107 /** The next value should be smaller or equal to the smallest sector size used
108 on any disk. A log block is required to be a portion of disk which is written
109 so that if the start and the end of a block get written to disk, then the
110 whole block gets written. This should be true even in most cases of a crash:
111 if this fails for a log block, then it is equivalent to a media failure in the
112 log. */
113 
114 #define OS_FILE_LOG_BLOCK_SIZE		512U
115 
116 /** Options for os_file_create_func @{ */
117 enum os_file_create_t {
118 	OS_FILE_OPEN = 51,		/*!< to open an existing file (if
119 					doesn't exist, error) */
120 	OS_FILE_CREATE,			/*!< to create new file (if
121 					exists, error) */
122 	OS_FILE_OVERWRITE,		/*!< to create a new file, if exists
123 					the overwrite old file */
124 	OS_FILE_OPEN_RAW,		/*!< to open a raw device or disk
125 					partition */
126 	OS_FILE_CREATE_PATH,		/*!< to create the directories */
127 	OS_FILE_OPEN_RETRY,		/*!< open with retry */
128 
129 	/** Flags that can be combined with the above values. Please ensure
130 	that the above values stay below 128. */
131 
132 	OS_FILE_ON_ERROR_NO_EXIT = 128,	/*!< do not exit on unknown errors */
133 	OS_FILE_ON_ERROR_SILENT = 256	/*!< don't print diagnostic messages to
134 					the log unless it is a fatal error,
135 					this flag is only used if
136 					ON_ERROR_NO_EXIT is set */
137 };
138 
139 static const ulint OS_FILE_READ_ONLY = 333;
140 static const ulint OS_FILE_READ_WRITE = 444;
141 
142 /** Used by MySQLBackup */
143 static const ulint OS_FILE_READ_ALLOW_DELETE = 555;
144 
145 /* Options for file_create */
146 static const ulint OS_FILE_AIO = 61;
147 static const ulint OS_FILE_NORMAL = 62;
148 /* @} */
149 
150 /** Types for file create @{ */
151 static const ulint OS_DATA_FILE = 100;
152 static const ulint OS_LOG_FILE = 101;
153 static const ulint OS_DATA_FILE_NO_O_DIRECT = 103;
154 /* @} */
155 
156 /** Error codes from os_file_get_last_error @{ */
157 static const ulint OS_FILE_NAME_TOO_LONG = 36;
158 static const ulint OS_FILE_NOT_FOUND = 71;
159 static const ulint OS_FILE_DISK_FULL = 72;
160 static const ulint OS_FILE_ALREADY_EXISTS = 73;
161 static const ulint OS_FILE_PATH_ERROR = 74;
162 
163 /** wait for OS aio resources to become available again */
164 static const ulint OS_FILE_AIO_RESOURCES_RESERVED = 75;
165 
166 static const ulint OS_FILE_SHARING_VIOLATION = 76;
167 static const ulint OS_FILE_ERROR_NOT_SPECIFIED = 77;
168 static const ulint OS_FILE_INSUFFICIENT_RESOURCE = 78;
169 static const ulint OS_FILE_AIO_INTERRUPTED = 79;
170 static const ulint OS_FILE_OPERATION_ABORTED = 80;
171 static const ulint OS_FILE_ACCESS_VIOLATION = 81;
172 static const ulint OS_FILE_OPERATION_NOT_SUPPORTED = 125;
173 static const ulint OS_FILE_ERROR_MAX = 200;
174 /* @} */
175 
176 /** Types for AIO operations @{ */
177 
178 /** No transformations during read/write, write as is. */
179 #define IORequestRead		IORequest(IORequest::READ)
180 #define IORequestWrite		IORequest(IORequest::WRITE)
181 #define IORequestLogRead	IORequest(IORequest::LOG | IORequest::READ)
182 #define IORequestLogWrite	IORequest(IORequest::LOG | IORequest::WRITE)
183 
184 
185 
186 /**
187 The IO Context that is passed down to the low level IO code */
188 class IORequest {
189 public:
190 	/** Flags passed in the request, they can be ORred together. */
191 	enum {
192 		READ = 1,
193 		WRITE = 2,
194 
195 		/** Double write buffer recovery. */
196 		DBLWR_RECOVER = 4,
197 
198 		/** Enumarations below can be ORed to READ/WRITE above*/
199 
200 		/** Data file */
201 		DATA_FILE = 8,
202 
203 		/** Log file request*/
204 		LOG = 16,
205 
206 		/** Disable partial read warnings */
207 		DISABLE_PARTIAL_IO_WARNINGS = 32,
208 
209 		/** Do not to wake i/o-handler threads, but the caller will do
210 		the waking explicitly later, in this way the caller can post
211 		several requests in a batch; NOTE that the batch must not be
212 		so big that it exhausts the slots in AIO arrays! NOTE that
213 		a simulated batch may introduce hidden chances of deadlocks,
214 		because I/Os are not actually handled until all
215 		have been posted: use with great caution! */
216 		DO_NOT_WAKE = 64,
217 
218 		/** Ignore failed reads of non-existent pages */
219 		IGNORE_MISSING = 128,
220 
221 		/** Use punch hole if available*/
222 		PUNCH_HOLE = 256,
223 	};
224 
225 	/** Default constructor */
IORequest()226 	IORequest()
227 		:
228 		m_bpage(NULL),
229 		m_fil_node(NULL),
230 		m_type(READ)
231 	{
232 		/* No op */
233 	}
234 
235 	/**
236 	@param[in]	type		Request type, can be a value that is
237 					ORed from the above enum */
IORequest(ulint type)238 	explicit IORequest(ulint type)
239 		:
240 		m_bpage(NULL),
241 		m_fil_node(NULL),
242 		m_type(static_cast<uint16_t>(type))
243 	{
244 		if (!is_punch_hole_supported()) {
245 			clear_punch_hole();
246 		}
247 	}
248 
249 	/**
250 	@param[in]	type		Request type, can be a value that is
251 					ORed from the above enum
252 	@param[in]	bpage		Page to be written */
IORequest(ulint type,buf_page_t * bpage)253 	IORequest(ulint type, buf_page_t* bpage)
254 		:
255 		m_bpage(bpage),
256 		m_fil_node(NULL),
257 		m_type(static_cast<uint16_t>(type))
258 	{
259 		if (bpage && buf_page_should_punch_hole(bpage)) {
260 			set_punch_hole();
261 		}
262 
263 		if (!is_punch_hole_supported()) {
264 			clear_punch_hole();
265 		}
266 	}
267 
268 	/** Destructor */
~IORequest()269 	~IORequest() { }
270 
271 	/** @return true if ignore missing flag is set */
ignore_missing(ulint type)272 	static bool ignore_missing(ulint type)
273 		MY_ATTRIBUTE((warn_unused_result))
274 	{
275 		return((type & IGNORE_MISSING) == IGNORE_MISSING);
276 	}
277 
278 	/** @return true if it is a read request */
is_read()279 	bool is_read() const
280 		MY_ATTRIBUTE((warn_unused_result))
281 	{
282 		return((m_type & READ) == READ);
283 	}
284 
285 	/** @return true if it is a write request */
is_write()286 	bool is_write() const
287 		MY_ATTRIBUTE((warn_unused_result))
288 	{
289 		return((m_type & WRITE) == WRITE);
290 	}
291 
292 	/** @return true if it is a redo log write */
is_log()293 	bool is_log() const
294 		MY_ATTRIBUTE((warn_unused_result))
295 	{
296 		return((m_type & LOG) == LOG);
297 	}
298 
299 	/** @return true if the simulated AIO thread should be woken up */
is_wake()300 	bool is_wake() const
301 		MY_ATTRIBUTE((warn_unused_result))
302 	{
303 		return((m_type & DO_NOT_WAKE) == 0);
304 	}
305 
306 	/** Clear the punch hole flag */
clear_punch_hole()307 	void clear_punch_hole()
308 	{
309 		m_type &= ~PUNCH_HOLE;
310 	}
311 
312 	/** @return true if partial read warning disabled */
is_partial_io_warning_disabled()313 	bool is_partial_io_warning_disabled() const
314 		MY_ATTRIBUTE((warn_unused_result))
315 	{
316 		return((m_type & DISABLE_PARTIAL_IO_WARNINGS)
317 		       == DISABLE_PARTIAL_IO_WARNINGS);
318 	}
319 
320 	/** Disable partial read warnings */
disable_partial_io_warnings()321 	void disable_partial_io_warnings()
322 	{
323 		m_type |= DISABLE_PARTIAL_IO_WARNINGS;
324 	}
325 
326 	/** @return true if missing files should be ignored */
ignore_missing()327 	bool ignore_missing() const
328 		MY_ATTRIBUTE((warn_unused_result))
329 	{
330 		return(ignore_missing(m_type));
331 	}
332 
333 	/** @return true if punch hole should be used */
punch_hole()334 	bool punch_hole() const
335 		MY_ATTRIBUTE((warn_unused_result))
336 	{
337 		return((m_type & PUNCH_HOLE) == PUNCH_HOLE);
338 	}
339 
340 	/** @return true if the read should be validated */
validate()341 	bool validate() const
342 		MY_ATTRIBUTE((warn_unused_result))
343 	{
344 		return(is_read() ^ is_write());
345 	}
346 
347 	/** Set the punch hole flag */
set_punch_hole()348 	void set_punch_hole()
349 	{
350 		if (is_punch_hole_supported()) {
351 			m_type |= PUNCH_HOLE;
352 		}
353 	}
354 
355 	/** Clear the do not wake flag */
clear_do_not_wake()356 	void clear_do_not_wake()
357 	{
358 		m_type &= ~DO_NOT_WAKE;
359 	}
360 
361 	/** Set the pointer to file node for IO
362 	@param[in] node			File node */
set_fil_node(fil_node_t * node)363 	void set_fil_node(fil_node_t* node)
364 	{
365 		if (node && !fil_node_should_punch_hole(node)) {
366 			clear_punch_hole();
367 		}
368 
369 		m_fil_node = node;
370 	}
371 
372 	/** Compare two requests
373 	@reutrn true if the are equal */
374 	bool operator==(const IORequest& rhs) const
375 	{
376 		return(m_type == rhs.m_type);
377 	}
378 
379 	/** Note that the IO is for double write recovery. */
dblwr_recover()380 	void dblwr_recover()
381 	{
382 		m_type |= DBLWR_RECOVER;
383 	}
384 
385 	/** @return true if the request is from the dblwr recovery */
is_dblwr_recover()386 	bool is_dblwr_recover() const
387 		MY_ATTRIBUTE((warn_unused_result))
388 	{
389 		return((m_type & DBLWR_RECOVER) == DBLWR_RECOVER);
390 	}
391 
392 	/** @return true if punch hole is supported */
is_punch_hole_supported()393 	static bool is_punch_hole_supported()
394 	{
395 
396 		/* In this debugging mode, we act as if punch hole is supported,
397 		and then skip any calls to actually punch a hole here.
398 		In this way, Transparent Page Compression is still being tested. */
399 		DBUG_EXECUTE_IF("ignore_punch_hole",
400 			return(true);
401 		);
402 
403 #if defined(HAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE) || defined(_WIN32)
404 		return(true);
405 #else
406 		return(false);
407 #endif /* HAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE || _WIN32 */
408 	}
409 
get_trim_length(ulint write_length)410 	ulint get_trim_length(ulint write_length) const
411 	{
412 		return (m_bpage ?
413 			buf_page_get_trim_length(m_bpage, write_length)
414 			: 0);
415 	}
416 
should_punch_hole()417 	bool should_punch_hole() const {
418 		return (m_fil_node ?
419 			fil_node_should_punch_hole(m_fil_node)
420 			: false);
421 	}
422 
space_no_punch_hole()423 	void space_no_punch_hole() const {
424 		if (m_fil_node) {
425 			fil_space_set_punch_hole(m_fil_node, false);
426 		}
427 	}
428 
429 	/** Free storage space associated with a section of the file.
430 	@param[in]	fh		Open file handle
431 	@param[in]	off		Starting offset (SEEK_SET)
432 	@param[in]	len		Size of the hole
433 	@return DB_SUCCESS or error code */
434 	dberr_t punch_hole(os_file_t fh, os_offset_t off, ulint len);
435 
436 private:
437 	/** Page to be written on write operation. */
438 	buf_page_t*		m_bpage;
439 
440 	/** File node */
441 	fil_node_t*		m_fil_node;
442 
443 	/** Request type bit flags */
444 	uint16_t		m_type;
445 };
446 
447 /* @} */
448 
449 /** Sparse file size information. */
450 struct os_file_size_t {
451 	/** Total size of file in bytes */
452 	os_offset_t	m_total_size;
453 
454 	/** If it is a sparse file then this is the number of bytes
455 	actually allocated for the file. */
456 	os_offset_t	m_alloc_size;
457 };
458 
459 /** Win NT does not allow more than 64 */
460 static const ulint OS_AIO_N_PENDING_IOS_PER_THREAD = 32;
461 
462 /** Modes for aio operations @{ */
463 /** Normal asynchronous i/o not for ibuf pages or ibuf bitmap pages */
464 static const ulint OS_AIO_NORMAL = 21;
465 
466 /**  Asynchronous i/o for ibuf pages or ibuf bitmap pages */
467 static const ulint OS_AIO_IBUF = 22;
468 
469 /** Asynchronous i/o for the log */
470 static const ulint OS_AIO_LOG = 23;
471 
472 /** Asynchronous i/o where the calling thread will itself wait for
473 the i/o to complete, doing also the job of the i/o-handler thread;
474 can be used for any pages, ibuf or non-ibuf.  This is used to save
475 CPU time, as we can do with fewer thread switches. Plain synchronous
476 I/O is not as good, because it must serialize the file seek and read
477 or write, causing a bottleneck for parallelism. */
478 static const ulint OS_AIO_SYNC = 24;
479 /* @} */
480 
481 extern ulint	os_n_file_reads;
482 extern ulint	os_n_file_writes;
483 extern ulint	os_n_fsyncs;
484 
485 /* File types for directory entry data type */
486 
487 enum os_file_type_t {
488 	OS_FILE_TYPE_UNKNOWN = 0,
489 	OS_FILE_TYPE_FILE,			/* regular file */
490 	OS_FILE_TYPE_DIR,			/* directory */
491 	OS_FILE_TYPE_LINK,			/* symbolic link */
492 	OS_FILE_TYPE_BLOCK			/* block device */
493 };
494 
495 /* Maximum path string length in bytes when referring to tables with in the
496 './databasename/tablename.ibd' path format; we can allocate at least 2 buffers
497 of this size from the thread stack; that is why this should not be made much
498 bigger than 4000 bytes.  The maximum path length used by any storage engine
499 in the server must be at least this big. */
500 
501 /* MySQL 5.7 my_global.h */
502 #ifndef FN_REFLEN_SE
503 #define FN_REFLEN_SE        4000
504 #endif
505 
506 #define OS_FILE_MAX_PATH	4000
507 #if (FN_REFLEN_SE < OS_FILE_MAX_PATH)
508 # error "(FN_REFLEN_SE < OS_FILE_MAX_PATH)"
509 #endif
510 
511 /** Struct used in fetching information of a file in a directory */
512 struct os_file_stat_t {
513 	char		name[OS_FILE_MAX_PATH];	/*!< path to a file */
514 	os_file_type_t	type;			/*!< file type */
515 	os_offset_t	size;			/*!< file size in bytes */
516 	os_offset_t	alloc_size;		/*!< Allocated size for
517 						sparse files in bytes */
518 	size_t		block_size;		/*!< Block size to use for IO
519 						in bytes*/
520 	time_t		ctime;			/*!< creation time */
521 	time_t		mtime;			/*!< modification time */
522 	time_t		atime;			/*!< access time */
523 	bool		rw_perm;		/*!< true if can be opened
524 						in read-write mode. Only valid
525 						if type == OS_FILE_TYPE_FILE */
526 };
527 
528 /** Create a temporary file. This function is like tmpfile(3), but
529 the temporary file is created in the in the mysql server configuration
530 parameter (--tmpdir).
531 @return temporary file handle, or NULL on error */
532 FILE*
533 os_file_create_tmpfile();
534 
535 /** The os_file_opendir() function opens a directory stream corresponding to the
536 directory named by the dirname argument. The directory stream is positioned
537 at the first entry. In both Unix and Windows we automatically skip the '.'
538 and '..' items at the start of the directory listing.
539 
540 @param[in]	dirname		directory name; it must not contain a trailing
541 				'\' or '/'
542 @param[in]	is_fatal	true if we should treat an error as a fatal
543 				error; if we try to open symlinks then we do
544 				not wish a fatal error if it happens not to be
545 				a directory
546 @return directory stream, NULL if error */
547 os_file_dir_t
548 os_file_opendir(
549 	const char*	dirname,
550 	bool		is_fatal);
551 
552 /**
553 Closes a directory stream.
554 @param[in] dir	directory stream
555 @return 0 if success, -1 if failure */
556 int
557 os_file_closedir(
558 	os_file_dir_t	dir);
559 
560 /** This function returns information of the next file in the directory. We jump
561 over the '.' and '..' entries in the directory.
562 @param[in]	dirname		directory name or path
563 @param[in]	dir		directory stream
564 @param[out]	info		buffer where the info is returned
565 @return 0 if ok, -1 if error, 1 if at the end of the directory */
566 int
567 os_file_readdir_next_file(
568 	const char*	dirname,
569 	os_file_dir_t	dir,
570 	os_file_stat_t*	info);
571 
572 /**
573 This function attempts to create a directory named pathname. The new directory
574 gets default permissions. On Unix, the permissions are (0770 & ~umask). If the
575 directory exists already, nothing is done and the call succeeds, unless the
576 fail_if_exists arguments is true.
577 
578 @param[in]	pathname	directory name as null-terminated string
579 @param[in]	fail_if_exists	if true, pre-existing directory is treated
580 				as an error.
581 @return true if call succeeds, false on error */
582 bool
583 os_file_create_directory(
584 	const char*	pathname,
585 	bool		fail_if_exists);
586 
587 /** NOTE! Use the corresponding macro os_file_create_simple(), not directly
588 this function!
589 A simple function to open or create a file.
590 @param[in]	name		name of the file or path as a null-terminated
591 				string
592 @param[in]	create_mode	create mode
593 @param[in]	access_type	OS_FILE_READ_ONLY or OS_FILE_READ_WRITE
594 @param[in]	read_only	if true read only mode checks are enforced
595 @param[out]	success		true if succeed, false if error
596 @return own: handle to the file, not defined if error, error number
597 	can be retrieved with os_file_get_last_error */
598 pfs_os_file_t
599 os_file_create_simple_func(
600 	const char*	name,
601 	ulint		create_mode,
602 	ulint		access_type,
603 	bool		read_only,
604 	bool*		success);
605 
606 /** NOTE! Use the corresponding macro
607 os_file_create_simple_no_error_handling(), not directly this function!
608 A simple function to open or create a file.
609 @param[in]	name		name of the file or path as a null-terminated string
610 @param[in]	create_mode	create mode
611 @param[in]	access_type	OS_FILE_READ_ONLY, OS_FILE_READ_WRITE, or
612 				OS_FILE_READ_ALLOW_DELETE; the last option
613 				is used by a backup program reading the file
614 @param[in]	read_only	if true read only mode checks are enforced
615 @param[out]	success		true if succeeded
616 @return own: handle to the file, not defined if error, error number
617 	can be retrieved with os_file_get_last_error */
618 pfs_os_file_t
619 os_file_create_simple_no_error_handling_func(
620 	const char*	name,
621 	ulint		create_mode,
622 	ulint		access_type,
623 	bool		read_only,
624 	bool*		success)
625 	MY_ATTRIBUTE((warn_unused_result));
626 
627 #ifdef  _WIN32
628 #define os_file_set_nocache(fd, file_name, operation_name) do{}while(0)
629 #else
630 /** Tries to disable OS caching on an opened file descriptor.
631 @param[in]	fd		file descriptor to alter
632 @param[in]	file_name	file name, used in the diagnostic message
633 @param[in]	name		"open" or "create"; used in the diagnostic
634 				message */
635 void
636 os_file_set_nocache(
637 /*================*/
638 	int	fd,		/*!< in: file descriptor to alter */
639 	const char*	file_name,
640 	const char*	operation_name);
641 #endif
642 
643 /** NOTE! Use the corresponding macro os_file_create(), not directly
644 this function!
645 Opens an existing file or creates a new.
646 @param[in]	name		name of the file or path as a null-terminated
647 				string
648 @param[in]	create_mode	create mode
649 @param[in]	purpose		OS_FILE_AIO, if asynchronous, non-buffered I/O
650 				is desired, OS_FILE_NORMAL, if any normal file;
651 				NOTE that it also depends on type, os_aio_..
652 				and srv_.. variables whether we really use
653 				async I/O or unbuffered I/O: look in the
654 				function source code for the exact rules
655 @param[in]	type		OS_DATA_FILE or OS_LOG_FILE
656 @param[in]	read_only	if true read only mode checks are enforced
657 @param[in]	success		true if succeeded
658 @return own: handle to the file, not defined if error, error number
659 	can be retrieved with os_file_get_last_error */
660 pfs_os_file_t
661 os_file_create_func(
662 	const char*	name,
663 	ulint		create_mode,
664 	ulint		purpose,
665 	ulint		type,
666 	bool		read_only,
667 	bool*		success)
668 	MY_ATTRIBUTE((warn_unused_result));
669 
670 /** Deletes a file. The file has to be closed before calling this.
671 @param[in]	name		file path as a null-terminated string
672 @return true if success */
673 bool
674 os_file_delete_func(const char* name);
675 
676 /** Deletes a file if it exists. The file has to be closed before calling this.
677 @param[in]	name		file path as a null-terminated string
678 @param[out]	exist		indicate if file pre-exist
679 @return true if success */
680 bool
681 os_file_delete_if_exists_func(const char* name, bool* exist);
682 
683 /** NOTE! Use the corresponding macro os_file_rename(), not directly
684 this function!
685 Renames a file (can also move it to another directory). It is safest that the
686 file is closed before calling this function.
687 @param[in]	oldpath		old file path as a null-terminated string
688 @param[in]	newpath		new file path
689 @return true if success */
690 bool
691 os_file_rename_func(const char* oldpath, const char* newpath);
692 
693 /** NOTE! Use the corresponding macro os_file_close(), not directly this
694 function!
695 Closes a file handle. In case of error, error number can be retrieved with
696 os_file_get_last_error.
697 @param[in]	file		own: handle to a file
698 @return true if success */
699 bool
700 os_file_close_func(os_file_t file);
701 
702 #ifdef UNIV_PFS_IO
703 
704 /* Keys to register InnoDB I/O with performance schema */
705 extern mysql_pfs_key_t	innodb_data_file_key;
706 extern mysql_pfs_key_t	innodb_log_file_key;
707 extern mysql_pfs_key_t	innodb_temp_file_key;
708 
709 /* Following four macros are instumentations to register
710 various file I/O operations with performance schema.
711 1) register_pfs_file_open_begin() and register_pfs_file_open_end() are
712 used to register file creation, opening, closing and renaming.
713 2) register_pfs_file_rename_begin() and  register_pfs_file_rename_end()
714 are used to register file renaming
715 2) register_pfs_file_io_begin() and register_pfs_file_io_end() are
716 used to register actual file read, write and flush
717 3) register_pfs_file_close_begin() and register_pfs_file_close_end()
718 are used to register file deletion operations*/
719 # define register_pfs_file_open_begin(state, locker, key, op, name,	\
720 				      src_file, src_line)		\
721 do {									\
722 	locker = PSI_FILE_CALL(get_thread_file_name_locker)(		\
723 		state, key, op, name, &locker);				\
724 	if (locker != NULL) {						\
725 		PSI_FILE_CALL(start_file_open_wait)(			\
726 			locker, src_file, src_line);			\
727 	}								\
728 } while (0)
729 
730 # define register_pfs_file_open_end(locker, file, result)		\
731 do {									\
732 	if (locker != NULL) {						\
733 		file.m_psi = PSI_FILE_CALL(end_file_open_wait)(	\
734 			locker, result);				\
735 	}								\
736 } while (0)
737 
738 # define register_pfs_file_rename_begin(state, locker, key, op, name,	\
739 				src_file, src_line)			\
740 	register_pfs_file_open_begin(state, locker, key, op, name,	\
741 					src_file, src_line)		\
742 
743 # define register_pfs_file_rename_end(locker, result)			\
744 do {									\
745 	if (locker != NULL) {				\
746 		PSI_FILE_CALL(end_file_open_wait)(locker, result);	\
747 	}								\
748 } while (0)
749 
750 # define register_pfs_file_close_begin(state, locker, key, op, name,	\
751 				      src_file, src_line)		\
752 do {									\
753 	locker = PSI_FILE_CALL(get_thread_file_name_locker)(		\
754 		state, key, op, name, &locker);				\
755 	if (locker != NULL) {						\
756 		PSI_FILE_CALL(start_file_close_wait)(			\
757 			locker, src_file, src_line);			\
758 	}								\
759 } while (0)
760 
761 # define register_pfs_file_close_end(locker, result)			\
762 do {									\
763 	if (locker != NULL) {						\
764 		PSI_FILE_CALL(end_file_close_wait)(			\
765 			locker, result);				\
766 	}								\
767 } while (0)
768 
769 # define register_pfs_file_io_begin(state, locker, file, count, op,	\
770 				    src_file, src_line)			\
771 do {									\
772 	locker = PSI_FILE_CALL(get_thread_file_stream_locker)(		\
773 		state, file.m_psi, op);					\
774 	if (locker != NULL) {						\
775 		PSI_FILE_CALL(start_file_wait)(				\
776 			locker, count, src_file, src_line);		\
777 	}								\
778 } while (0)
779 
780 # define register_pfs_file_io_end(locker, count)			\
781 do {									\
782 	if (locker != NULL) {						\
783 		PSI_FILE_CALL(end_file_wait)(locker, count);		\
784 	}								\
785 } while (0)
786 
787 /* Following macros/functions are file I/O APIs that would be performance
788 schema instrumented if "UNIV_PFS_IO" is defined. They would point to
789 wrapper functions with performance schema instrumentation in such case.
790 
791 os_file_create
792 os_file_create_simple
793 os_file_create_simple_no_error_handling
794 os_file_close
795 os_file_rename
796 os_aio
797 os_file_read
798 os_file_read_no_error_handling
799 os_file_write
800 
801 The wrapper functions have the prefix of "innodb_". */
802 
803 # define os_file_create(key, name, create, purpose, type, read_only,	\
804 			success)					\
805 	pfs_os_file_create_func(key, name, create, purpose,	type,	\
806 		read_only, success, __FILE__, __LINE__)
807 
808 # define os_file_create_simple(key, name, create, access,		\
809 		read_only, success)					\
810 	pfs_os_file_create_simple_func(key, name, create, access,	\
811 		read_only, success, __FILE__, __LINE__)
812 
813 # define os_file_create_simple_no_error_handling(			\
814 	key, name, create_mode, access, read_only, success)		\
815 	pfs_os_file_create_simple_no_error_handling_func(		\
816 		key, name, create_mode, access,				\
817 		read_only, success, __FILE__, __LINE__)
818 
819 # define os_file_close(file)						\
820 	pfs_os_file_close_func(file, __FILE__, __LINE__)
821 
822 # define os_aio(type, mode, name, file, buf, offset,		\
823 	n, read_only, message1, message2)			\
824 	pfs_os_aio_func(type, mode, name, file, buf, offset,	\
825 		n, read_only, message1, message2,		\
826 			__FILE__, __LINE__)
827 
828 # define os_file_read(type, file, buf, offset, n)			\
829 	pfs_os_file_read_func(type, file, buf, offset, n, __FILE__, __LINE__)
830 
831 # define os_file_read_no_error_handling(type, file, buf, offset, n, o)	\
832 	pfs_os_file_read_no_error_handling_func(			\
833 		type, file, buf, offset, n, o, __FILE__, __LINE__)
834 
835 # define os_file_write(type, name, file, buf, offset, n)	\
836 	pfs_os_file_write_func(type, name, file, buf, offset,	\
837 			       n, __FILE__, __LINE__)
838 
839 # define os_file_flush(file)					\
840 	pfs_os_file_flush_func(file, __FILE__, __LINE__)
841 
842 # define os_file_rename(key, oldpath, newpath)				\
843 	pfs_os_file_rename_func(key, oldpath, newpath, __FILE__, __LINE__)
844 
845 # define os_file_delete(key, name)					\
846 	pfs_os_file_delete_func(key, name, __FILE__, __LINE__)
847 
848 # define os_file_delete_if_exists(key, name, exist)			\
849 	pfs_os_file_delete_if_exists_func(key, name, exist, __FILE__, __LINE__)
850 
851 /** NOTE! Please use the corresponding macro os_file_create_simple(),
852 not directly this function!
853 A performance schema instrumented wrapper function for
854 os_file_create_simple() which opens or creates a file.
855 @param[in]	key		Performance Schema Key
856 @param[in]	name		name of the file or path as a null-terminated
857 				string
858 @param[in]	create_mode	create mode
859 @param[in]	access_type	OS_FILE_READ_ONLY or OS_FILE_READ_WRITE
860 @param[in]	read_only	if true read only mode checks are enforced
861 @param[out]	success		true if succeeded
862 @param[in]	src_file	file name where func invoked
863 @param[in]	src_line	line where the func invoked
864 @return own: handle to the file, not defined if error, error number
865 	can be retrieved with os_file_get_last_error */
866 UNIV_INLINE
867 pfs_os_file_t
868 pfs_os_file_create_simple_func(
869 	mysql_pfs_key_t key,
870 	const char*	name,
871 	ulint		create_mode,
872 	ulint		access_type,
873 	bool		read_only,
874 	bool*		success,
875 	const char*	src_file,
876 	uint		src_line)
877 	MY_ATTRIBUTE((warn_unused_result));
878 
879 /** NOTE! Please use the corresponding macro
880 os_file_create_simple_no_error_handling(), not directly this function!
881 A performance schema instrumented wrapper function for
882 os_file_create_simple_no_error_handling(). Add instrumentation to
883 monitor file creation/open.
884 @param[in]	key		Performance Schema Key
885 @param[in]	name		name of the file or path as a null-terminated
886 				string
887 @param[in]	create_mode	create mode
888 @param[in]	access_type	OS_FILE_READ_ONLY, OS_FILE_READ_WRITE, or
889 				OS_FILE_READ_ALLOW_DELETE; the last option is
890 				used by a backup program reading the file
891 @param[in]	read_only	if true read only mode checks are enforced
892 @param[out]	success		true if succeeded
893 @param[in]	src_file	file name where func invoked
894 @param[in]	src_line	line where the func invoked
895 @return own: handle to the file, not defined if error, error number
896 	can be retrieved with os_file_get_last_error */
897 UNIV_INLINE
898 pfs_os_file_t
899 pfs_os_file_create_simple_no_error_handling_func(
900 	mysql_pfs_key_t key,
901 	const char*	name,
902 	ulint		create_mode,
903 	ulint		access_type,
904 	bool		read_only,
905 	bool*		success,
906 	const char*	src_file,
907 	uint		src_line)
908 	MY_ATTRIBUTE((warn_unused_result));
909 
910 /** NOTE! Please use the corresponding macro os_file_create(), not directly
911 this function!
912 A performance schema wrapper function for os_file_create().
913 Add instrumentation to monitor file creation/open.
914 @param[in]	key		Performance Schema Key
915 @param[in]	name		name of the file or path as a null-terminated
916 				string
917 @param[in]	create_mode	create mode
918 @param[in]	purpose		OS_FILE_AIO, if asynchronous, non-buffered I/O
919 				is desired, OS_FILE_NORMAL, if any normal file;
920 				NOTE that it also depends on type, os_aio_..
921 				and srv_.. variables whether we really use
922 				async I/O or unbuffered I/O: look in the
923 				function source code for the exact rules
924 @param[in]	read_only	if true read only mode checks are enforced
925 @param[out]	success		true if succeeded
926 @param[in]	src_file	file name where func invoked
927 @param[in]	src_line	line where the func invoked
928 @return own: handle to the file, not defined if error, error number
929 	can be retrieved with os_file_get_last_error */
930 UNIV_INLINE
931 pfs_os_file_t
932 pfs_os_file_create_func(
933 	mysql_pfs_key_t key,
934 	const char*	name,
935 	ulint		create_mode,
936 	ulint		purpose,
937 	ulint		type,
938 	bool		read_only,
939 	bool*		success,
940 	const char*	src_file,
941 	uint		src_line)
942 	MY_ATTRIBUTE((warn_unused_result));
943 
944 /** NOTE! Please use the corresponding macro os_file_close(), not directly
945 this function!
946 A performance schema instrumented wrapper function for os_file_close().
947 @param[in]	file		handle to a file
948 @param[in]	src_file	file name where func invoked
949 @param[in]	src_line	line where the func invoked
950 @return true if success */
951 UNIV_INLINE
952 bool
953 pfs_os_file_close_func(
954 	pfs_os_file_t	file,
955 	const char*	src_file,
956 	uint		src_line);
957 
958 /** NOTE! Please use the corresponding macro os_file_read(), not directly
959 this function!
960 This is the performance schema instrumented wrapper function for
961 os_file_read() which requests a synchronous read operation.
962 @param[in]	type		IO request context
963 @param[in]	file		Open file handle
964 @param[out]	buf		buffer where to read
965 @param[in]	offset		file offset where to read
966 @param[in]	n		number of bytes to read
967 @param[in]	src_file	file name where func invoked
968 @param[in]	src_line	line where the func invoked
969 @return DB_SUCCESS if request was successful */
970 UNIV_INLINE
971 dberr_t
972 pfs_os_file_read_func(
973 	const IORequest&	type,
974 	pfs_os_file_t		file,
975 	void*			buf,
976 	os_offset_t		offset,
977 	ulint			n,
978 	const char*		src_file,
979 	uint			src_line);
980 
981 /** NOTE! Please use the corresponding macro os_file_read_no_error_handling(),
982 not directly this function!
983 This is the performance schema instrumented wrapper function for
984 os_file_read_no_error_handling_func() which requests a synchronous
985 read operation.
986 @param[in]	type		IO request context
987 @param[in]	file		Open file handle
988 @param[out]	buf		buffer where to read
989 @param[in]	offset		file offset where to read
990 @param[in]	n		number of bytes to read
991 @param[out]	o		number of bytes actually read
992 @param[in]	src_file	file name where func invoked
993 @param[in]	src_line	line where the func invoked
994 @return DB_SUCCESS if request was successful */
995 UNIV_INLINE
996 dberr_t
997 pfs_os_file_read_no_error_handling_func(
998 	const IORequest&	type,
999 	pfs_os_file_t		file,
1000 	void*			buf,
1001 	os_offset_t		offset,
1002 	ulint			n,
1003 	ulint*			o,
1004 	const char*		src_file,
1005 	uint			src_line);
1006 
1007 /** NOTE! Please use the corresponding macro os_aio(), not directly this
1008 function!
1009 Performance schema wrapper function of os_aio() which requests
1010 an asynchronous I/O operation.
1011 @param[in,out]	type		IO request context
1012 @param[in]	mode		IO mode
1013 @param[in]	name		Name of the file or path as NUL terminated
1014 				string
1015 @param[in]	file		Open file handle
1016 @param[out]	buf		buffer where to read
1017 @param[in]	offset		file offset where to read
1018 @param[in]	n		number of bytes to read
1019 @param[in]	read_only	if true read only mode checks are enforced
1020 @param[in,out]	m1		Message for the AIO handler, (can be used to
1021 				identify a completed AIO operation); ignored
1022 				if mode is OS_AIO_SYNC
1023 @param[in,out]	m2		message for the AIO handler (can be used to
1024 				identify a completed AIO operation); ignored
1025 				if mode is OS_AIO_SYNC
1026 @param[in]	src_file	file name where func invoked
1027 @param[in]	src_line	line where the func invoked
1028 @return DB_SUCCESS if request was queued successfully, FALSE if fail */
1029 UNIV_INLINE
1030 dberr_t
1031 pfs_os_aio_func(
1032 	IORequest&	type,
1033 	ulint		mode,
1034 	const char*	name,
1035 	pfs_os_file_t	file,
1036 	void*		buf,
1037 	os_offset_t	offset,
1038 	ulint		n,
1039 	bool		read_only,
1040 	fil_node_t*	m1,
1041 	void*		m2,
1042 	const char*	src_file,
1043 	uint		src_line);
1044 
1045 /** NOTE! Please use the corresponding macro os_file_write(), not directly
1046 this function!
1047 This is the performance schema instrumented wrapper function for
1048 os_file_write() which requests a synchronous write operation.
1049 @param[in]	type		IO request context
1050 @param[in]	name		Name of the file or path as NUL terminated
1051 				string
1052 @param[in]	file		Open file handle
1053 @param[out]	buf		buffer where to read
1054 @param[in]	offset		file offset where to read
1055 @param[in]	n		number of bytes to read
1056 @param[in]	src_file	file name where func invoked
1057 @param[in]	src_line	line where the func invoked
1058 @return DB_SUCCESS if request was successful */
1059 UNIV_INLINE
1060 dberr_t
1061 pfs_os_file_write_func(
1062 	const IORequest&	type,
1063 	const char*		name,
1064 	pfs_os_file_t		file,
1065 	const void*		buf,
1066 	os_offset_t		offset,
1067 	ulint			n,
1068 	const char*		src_file,
1069 	uint			src_line);
1070 
1071 /** NOTE! Please use the corresponding macro os_file_flush(), not directly
1072 this function!
1073 This is the performance schema instrumented wrapper function for
1074 os_file_flush() which flushes the write buffers of a given file to the disk.
1075 Flushes the write buffers of a given file to the disk.
1076 @param[in]	file		Open file handle
1077 @param[in]	src_file	file name where func invoked
1078 @param[in]	src_line	line where the func invoked
1079 @return TRUE if success */
1080 UNIV_INLINE
1081 bool
1082 pfs_os_file_flush_func(
1083 	pfs_os_file_t	file,
1084 	const char*	src_file,
1085 	uint		src_line);
1086 
1087 /** NOTE! Please use the corresponding macro os_file_rename(), not directly
1088 this function!
1089 This is the performance schema instrumented wrapper function for
1090 os_file_rename()
1091 @param[in]	key		Performance Schema Key
1092 @param[in]	oldpath		old file path as a null-terminated string
1093 @param[in]	newpath		new file path
1094 @param[in]	src_file	file name where func invoked
1095 @param[in]	src_line	line where the func invoked
1096 @return true if success */
1097 UNIV_INLINE
1098 bool
1099 pfs_os_file_rename_func(
1100 	mysql_pfs_key_t	key,
1101 	const char*	oldpath,
1102 	const char*	newpath,
1103 	const char*	src_file,
1104 	uint		src_line);
1105 
1106 /**
1107 NOTE! Please use the corresponding macro os_file_delete(), not directly
1108 this function!
1109 This is the performance schema instrumented wrapper function for
1110 os_file_delete()
1111 @param[in]	key		Performance Schema Key
1112 @param[in]	name		old file path as a null-terminated string
1113 @param[in]	src_file	file name where func invoked
1114 @param[in]	src_line	line where the func invoked
1115 @return true if success */
1116 UNIV_INLINE
1117 bool
1118 pfs_os_file_delete_func(
1119 	mysql_pfs_key_t	key,
1120 	const char*	name,
1121 	const char*	src_file,
1122 	uint		src_line);
1123 
1124 /**
1125 NOTE! Please use the corresponding macro os_file_delete_if_exists(), not
1126 directly this function!
1127 This is the performance schema instrumented wrapper function for
1128 os_file_delete_if_exists()
1129 @param[in]	key		Performance Schema Key
1130 @param[in]	name		old file path as a null-terminated string
1131 @param[in]	exist		indicate if file pre-exist
1132 @param[in]	src_file	file name where func invoked
1133 @param[in]	src_line	line where the func invoked
1134 @return true if success */
1135 UNIV_INLINE
1136 bool
1137 pfs_os_file_delete_if_exists_func(
1138 	mysql_pfs_key_t	key,
1139 	const char*	name,
1140 	bool*		exist,
1141 	const char*	src_file,
1142 	uint		src_line);
1143 
1144 #else /* UNIV_PFS_IO */
1145 
1146 /* If UNIV_PFS_IO is not defined, these I/O APIs point
1147 to original un-instrumented file I/O APIs */
1148 # define os_file_create(key, name, create, purpose, type, read_only,	\
1149 			success)					\
1150 	os_file_create_func(name, create, purpose, type, read_only,	\
1151 			success)
1152 
1153 # define os_file_create_simple(key, name, create_mode, access,		\
1154 		read_only, success)					\
1155 	os_file_create_simple_func(name, create_mode, access,		\
1156 		read_only, success)
1157 
1158 # define os_file_create_simple_no_error_handling(			\
1159 	key, name, create_mode, access, read_only, success)		\
1160 	os_file_create_simple_no_error_handling_func(			\
1161 		name, create_mode, access, read_only, success)
1162 
1163 # define os_file_close(file)	os_file_close_func(file)
1164 
1165 # define os_aio(type, mode, name, file, buf, offset,			\
1166 	n, read_only, message1, message2)			\
1167 	os_aio_func(type, mode, name, file, buf, offset,		\
1168 		n, read_only, message1, message2)
1169 
1170 # define os_file_read(type, file, buf, offset, n)			\
1171 	os_file_read_func(type, file, buf, offset, n)
1172 
1173 # define os_file_read_no_error_handling(type, file, buf, offset, n, o)	\
1174 	os_file_read_no_error_handling_func(type, file, buf, offset, n, o)
1175 
1176 # define os_file_write(type, name, file, buf, offset, n)	\
1177 	os_file_write_func(type, name, file, buf, offset, n)
1178 
1179 # define os_file_flush(file)	os_file_flush_func(file)
1180 
1181 # define os_file_rename(key, oldpath, newpath)				\
1182 	os_file_rename_func(oldpath, newpath)
1183 
1184 # define os_file_delete(key, name)	os_file_delete_func(name)
1185 
1186 # define os_file_delete_if_exists(key, name, exist)			\
1187 	os_file_delete_if_exists_func(name, exist)
1188 
1189 #endif	/* UNIV_PFS_IO */
1190 
1191 /** Gets a file size.
1192 @param[in]	file		handle to a file
1193 @return file size if OK, else set m_total_size to ~0 and m_alloc_size
1194 	to errno */
1195 os_file_size_t
1196 os_file_get_size(
1197 	const char*	filename)
1198 	MY_ATTRIBUTE((warn_unused_result));
1199 
1200 /** Gets a file size.
1201 @param[in]	file		handle to a file
1202 @return file size, or (os_offset_t) -1 on failure */
1203 os_offset_t
1204 os_file_get_size(
1205 	os_file_t	file)
1206 	MY_ATTRIBUTE((warn_unused_result));
1207 
1208 /** Extend a file.
1209 
1210 On Windows, extending a file allocates blocks for the file,
1211 unless the file is sparse.
1212 
1213 On Unix, we will extend the file with ftruncate(), if
1214 file needs to be sparse. Otherwise posix_fallocate() is used
1215 when available, and if not, binary zeroes are added to the end
1216 of file.
1217 
1218 @param[in]	name	file name
1219 @param[in]	file	file handle
1220 @param[in]	size	desired file size
1221 @param[in]	sparse	whether to create a sparse file (no preallocating)
1222 @return	whether the operation succeeded */
1223 bool
1224 os_file_set_size(
1225 	const char*	name,
1226 	os_file_t	file,
1227 	os_offset_t	size,
1228 	bool		is_sparse = false)
1229 	MY_ATTRIBUTE((warn_unused_result));
1230 
1231 /** Truncates a file at its current position.
1232 @param[in/out]	file	file to be truncated
1233 @return true if success */
1234 bool
1235 os_file_set_eof(
1236 	FILE*		file);	/*!< in: file to be truncated */
1237 
1238 /** Truncate a file to a specified size in bytes.
1239 @param[in]	pathname	file path
1240 @param[in]	file		file to be truncated
1241 @param[in]	size		size preserved in bytes
1242 @param[in]	allow_shrink	whether to allow the file to become smaller
1243 @return true if success */
1244 bool
1245 os_file_truncate(
1246 	const char*	pathname,
1247 	os_file_t	file,
1248 	os_offset_t	size,
1249 	bool		allow_shrink = false);
1250 
1251 /** NOTE! Use the corresponding macro os_file_flush(), not directly this
1252 function!
1253 Flushes the write buffers of a given file to the disk.
1254 @param[in]	file		handle to a file
1255 @return true if success */
1256 bool
1257 os_file_flush_func(
1258 	os_file_t	file);
1259 
1260 /** Retrieves the last error number if an error occurs in a file io function.
1261 The number should be retrieved before any other OS calls (because they may
1262 overwrite the error number). If the number is not known to this program,
1263 the OS error number + 100 is returned.
1264 @param[in]	report		true if we want an error message printed
1265 				for all errors
1266 @return error number, or OS error number + 100 */
1267 ulint
1268 os_file_get_last_error(
1269 	bool		report);
1270 
1271 /** NOTE! Use the corresponding macro os_file_read(), not directly this
1272 function!
1273 Requests a synchronous read operation.
1274 @param[in]	type		IO request context
1275 @param[in]	file		Open file handle
1276 @param[out]	buf		buffer where to read
1277 @param[in]	offset		file offset where to read
1278 @param[in]	n		number of bytes to read
1279 @return DB_SUCCESS if request was successful */
1280 dberr_t
1281 os_file_read_func(
1282 	const IORequest&	type,
1283 	os_file_t		file,
1284 	void*			buf,
1285 	os_offset_t		offset,
1286 	ulint			n)
1287 	MY_ATTRIBUTE((warn_unused_result));
1288 
1289 /** Rewind file to its start, read at most size - 1 bytes from it to str, and
1290 NUL-terminate str. All errors are silently ignored. This function is
1291 mostly meant to be used with temporary files.
1292 @param[in,out]	file		file to read from
1293 @param[in,out]	str		buffer where to read
1294 @param[in]	size		size of buffer */
1295 void
1296 os_file_read_string(
1297 	FILE*		file,
1298 	char*		str,
1299 	ulint		size);
1300 
1301 /** NOTE! Use the corresponding macro os_file_read_no_error_handling(),
1302 not directly this function!
1303 Requests a synchronous positioned read operation. This function does not do
1304 any error handling. In case of error it returns FALSE.
1305 @param[in]	type		IO request context
1306 @param[in]	file		Open file handle
1307 @param[out]	buf		buffer where to read
1308 @param[in]	offset		file offset where to read
1309 @param[in]	n		number of bytes to read
1310 @param[out]	o		number of bytes actually read
1311 @return DB_SUCCESS or error code */
1312 dberr_t
1313 os_file_read_no_error_handling_func(
1314 	const IORequest&	type,
1315 	os_file_t		file,
1316 	void*			buf,
1317 	os_offset_t		offset,
1318 	ulint			n,
1319 	ulint*			o)
1320 	MY_ATTRIBUTE((warn_unused_result));
1321 
1322 /** NOTE! Use the corresponding macro os_file_write(), not directly this
1323 function!
1324 Requests a synchronous write operation.
1325 @param[in]	type		IO request context
1326 @param[in]	file		Open file handle
1327 @param[out]	buf		buffer where to read
1328 @param[in]	offset		file offset where to read
1329 @param[in]	n		number of bytes to read
1330 @return DB_SUCCESS if request was successful */
1331 dberr_t
1332 os_file_write_func(
1333 	const IORequest&	type,
1334 	const char*		name,
1335 	os_file_t		file,
1336 	const void*		buf,
1337 	os_offset_t		offset,
1338 	ulint			n)
1339 	MY_ATTRIBUTE((warn_unused_result));
1340 
1341 /** Check the existence and type of the given file.
1342 @param[in]	path		pathname of the file
1343 @param[out]	exists		true if file exists
1344 @param[out]	type		type of the file (if it exists)
1345 @return true if call succeeded */
1346 bool
1347 os_file_status(
1348 	const char*	path,
1349 	bool*		exists,
1350 	os_file_type_t* type);
1351 
1352 /** This function returns a new path name after replacing the basename
1353 in an old path with a new basename.  The old_path is a full path
1354 name including the extension.  The tablename is in the normal
1355 form "databasename/tablename".  The new base name is found after
1356 the forward slash.  Both input strings are null terminated.
1357 
1358 This function allocates memory to be returned.  It is the callers
1359 responsibility to free the return value after it is no longer needed.
1360 
1361 @param[in]	old_path		pathname
1362 @param[in]	new_name		new file name
1363 @return own: new full pathname */
1364 char*
1365 os_file_make_new_pathname(
1366 	const char*	old_path,
1367 	const char*	new_name);
1368 
1369 /** This function reduces a null-terminated full remote path name into
1370 the path that is sent by MySQL for DATA DIRECTORY clause.  It replaces
1371 the 'databasename/tablename.ibd' found at the end of the path with just
1372 'tablename'.
1373 
1374 Since the result is always smaller than the path sent in, no new memory
1375 is allocated. The caller should allocate memory for the path sent in.
1376 This function manipulates that path in place.
1377 
1378 If the path format is not as expected, just return.  The result is used
1379 to inform a SHOW CREATE TABLE command.
1380 @param[in,out]	data_dir_path		Full path/data_dir_path */
1381 void
1382 os_file_make_data_dir_path(
1383 	char*	data_dir_path);
1384 
1385 /** Create all missing subdirectories along the given path.
1386 @return DB_SUCCESS if OK, otherwise error code. */
1387 dberr_t
1388 os_file_create_subdirs_if_needed(
1389 	const char*	path);
1390 
1391 #ifdef UNIV_ENABLE_UNIT_TEST_GET_PARENT_DIR
1392 /* Test the function os_file_get_parent_dir. */
1393 void
1394 unit_test_os_file_get_parent_dir();
1395 #endif /* UNIV_ENABLE_UNIT_TEST_GET_PARENT_DIR */
1396 
1397 /** Initializes the asynchronous io system. Creates one array each for ibuf
1398 and log i/o. Also creates one array each for read and write where each
1399 array is divided logically into n_read_segs and n_write_segs
1400 respectively. The caller must create an i/o handler thread for each
1401 segment in these arrays. This function also creates the sync array.
1402 No i/o handler thread needs to be created for that
1403 @param[in]	n_read_segs	number of reader threads
1404 @param[in]	n_write_segs	number of writer threads
1405 @param[in]	n_slots_sync	number of slots in the sync aio array */
1406 
1407 bool
1408 os_aio_init(
1409 	ulint		n_read_segs,
1410 	ulint		n_write_segs,
1411 	ulint		n_slots_sync);
1412 
1413 /**
1414 Frees the asynchronous io system. */
1415 void
1416 os_aio_free();
1417 
1418 /**
1419 NOTE! Use the corresponding macro os_aio(), not directly this function!
1420 Requests an asynchronous i/o operation.
1421 @param[in,out]	type		IO request context
1422 @param[in]	mode		IO mode
1423 @param[in]	name		Name of the file or path as NUL terminated
1424 				string
1425 @param[in]	file		Open file handle
1426 @param[out]	buf		buffer where to read
1427 @param[in]	offset		file offset where to read
1428 @param[in]	n		number of bytes to read
1429 @param[in]	read_only	if true read only mode checks are enforced
1430 @param[in,out]	m1		Message for the AIO handler, (can be used to
1431 				identify a completed AIO operation); ignored
1432 				if mode is OS_AIO_SYNC
1433 @param[in,out]	m2		message for the AIO handler (can be used to
1434 				identify a completed AIO operation); ignored
1435 				if mode is OS_AIO_SYNC
1436 @return DB_SUCCESS or error code */
1437 dberr_t
1438 os_aio_func(
1439 	IORequest&	type,
1440 	ulint		mode,
1441 	const char*	name,
1442 	pfs_os_file_t	file,
1443 	void*		buf,
1444 	os_offset_t	offset,
1445 	ulint		n,
1446 	bool		read_only,
1447 	fil_node_t*	m1,
1448 	void*		m2);
1449 
1450 /** Wakes up all async i/o threads so that they know to exit themselves in
1451 shutdown. */
1452 void
1453 os_aio_wake_all_threads_at_shutdown();
1454 
1455 /** Waits until there are no pending writes in os_aio_write_array. There can
1456 be other, synchronous, pending writes. */
1457 void
1458 os_aio_wait_until_no_pending_writes();
1459 
1460 /** Wakes up simulated aio i/o-handler threads if they have something to do. */
1461 void
1462 os_aio_simulated_wake_handler_threads();
1463 
1464 #ifdef _WIN32
1465 /** This function can be called if one wants to post a batch of reads and
1466 prefers an i/o-handler thread to handle them all at once later. You must
1467 call os_aio_simulated_wake_handler_threads later to ensure the threads
1468 are not left sleeping! */
1469 void
1470 os_aio_simulated_put_read_threads_to_sleep();
1471 #else /* _WIN32 */
1472 # define os_aio_simulated_put_read_threads_to_sleep()
1473 #endif /* _WIN32 */
1474 
1475 /** This is the generic AIO handler interface function.
1476 Waits for an aio operation to complete. This function is used to wait the
1477 for completed requests. The AIO array of pending requests is divided
1478 into segments. The thread specifies which segment or slot it wants to wait
1479 for. NOTE: this function will also take care of freeing the aio slot,
1480 therefore no other thread is allowed to do the freeing!
1481 @param[in]	segment		the number of the segment in the aio arrays to
1482 				wait for; segment 0 is the ibuf I/O thread,
1483 				segment 1 the log I/O thread, then follow the
1484 				non-ibuf read threads, and as the last are the
1485 				non-ibuf write threads; if this is
1486 				ULINT_UNDEFINED, then it means that sync AIO
1487 				is used, and this parameter is ignored
1488 @param[out]	m1		the messages passed with the AIO request;
1489 				note that also in the case where the AIO
1490 				operation failed, these output parameters
1491 				are valid and can be used to restart the
1492 				operation, for example
1493 @param[out]	m2		callback message
1494 @param[out]	type		OS_FILE_WRITE or ..._READ
1495 @return DB_SUCCESS or error code */
1496 dberr_t
1497 os_aio_handler(
1498 	ulint		segment,
1499 	fil_node_t**	m1,
1500 	void**		m2,
1501 	IORequest*	type);
1502 
1503 /** Prints info of the aio arrays.
1504 @param[in/out]	file		file where to print */
1505 void
1506 os_aio_print(FILE* file);
1507 
1508 /** Refreshes the statistics used to print per-second averages. */
1509 void
1510 os_aio_refresh_stats();
1511 
1512 /** Checks that all slots in the system have been freed, that is, there are
1513 no pending io operations. */
1514 bool
1515 os_aio_all_slots_free();
1516 
1517 #ifdef UNIV_DEBUG
1518 
1519 /** Prints all pending IO
1520 @param[in]	file	file where to print */
1521 void
1522 os_aio_print_pending_io(FILE* file);
1523 
1524 #endif /* UNIV_DEBUG */
1525 
1526 /** This function returns information about the specified file
1527 @param[in]	path		pathname of the file
1528 @param[in]	stat_info	information of a file in a directory
1529 @param[in]	check_rw_perm	for testing whether the file can be opened
1530 				in RW mode
1531 @param[in]	read_only	if true read only mode checks are enforced
1532 @return DB_SUCCESS if all OK */
1533 dberr_t
1534 os_file_get_status(
1535 	const char*	path,
1536 	os_file_stat_t* stat_info,
1537 	bool		check_rw_perm,
1538 	bool		read_only);
1539 
1540 /** Creates a temporary file in the location specified by the parameter
1541 path. If the path is NULL then it will be created on --tmpdir location.
1542 This function is defined in ha_innodb.cc.
1543 @param[in]	path	location for creating temporary file
1544 @return temporary file descriptor, or < 0 on error */
1545 os_file_t
1546 innobase_mysql_tmpfile(
1547 	const char*	path);
1548 
1549 /** Set the file create umask
1550 @param[in]	umask		The umask to use for file creation. */
1551 void
1552 os_file_set_umask(ulint umask);
1553 
1554 #ifdef _WIN32
1555 
1556 /**
1557 Make file sparse, on Windows.
1558 
1559 @param[in]	file  file handle
1560 @param[in]	is_sparse if true, make file sparse,
1561 			otherwise "unsparse" the file
1562 @return true on success, false on error */
1563 bool os_file_set_sparse_win32(os_file_t file, bool is_sparse = true);
1564 
1565 /**
1566 Changes file size on Windows
1567 
1568 If file is extended, following happens  the bytes between
1569 old and new EOF are zeros.
1570 
1571 If file is sparse, "virtual" block is added at the end of
1572 allocated area.
1573 
1574 If file is normal, file system allocates storage.
1575 
1576 @param[in]	pathname	file path
1577 @param[in]	file		file handle
1578 @param[in]	size		size to preserve in bytes
1579 @return true if success */
1580 bool
1581 os_file_change_size_win32(
1582 	const char*	pathname,
1583 	os_file_t	file,
1584 	os_offset_t	size);
1585 
1586 #endif /*_WIN32 */
1587 
1588 /** Check if the file system supports sparse files.
1589 
1590 Warning: On POSIX systems we try and punch a hole from offset 0 to
1591 the system configured page size. This should only be called on an empty
1592 file.
1593 
1594 @param[in]	fh		File handle for the file - if opened
1595 @return true if the file system supports sparse files */
1596 bool
1597 os_is_sparse_file_supported(
1598 	os_file_t	fh)
1599 	MY_ATTRIBUTE((warn_unused_result));
1600 
1601 /** Free storage space associated with a section of the file.
1602 @param[in]	fh		Open file handle
1603 @param[in]	off		Starting offset (SEEK_SET)
1604 @param[in]	len		Size of the hole
1605 @return DB_SUCCESS or error code */
1606 dberr_t
1607 os_file_punch_hole(
1608 	os_file_t	fh,
1609 	os_offset_t	off,
1610 	os_offset_t	len)
1611 	MY_ATTRIBUTE((warn_unused_result));
1612 
1613 /** Normalizes a directory path for the current OS:
1614 On Windows, we convert '/' to '\', else we convert '\' to '/'.
1615 @param[in,out] str A null-terminated directory and file path */
1616 void os_normalize_path(char*	str);
1617 
1618 /* Determine if a path is an absolute path or not.
1619 @param[in]	OS directory or file path to evaluate
1620 @retval true if an absolute path
1621 @retval false if a relative path */
1622 UNIV_INLINE
1623 bool
is_absolute_path(const char * path)1624 is_absolute_path(
1625 	const char*	path)
1626 {
1627 	if (path[0] == OS_PATH_SEPARATOR) {
1628 		return(true);
1629 	}
1630 
1631 #ifdef _WIN32
1632 	if (path[1] == ':' && path[2] == OS_PATH_SEPARATOR) {
1633 		return(true);
1634 	}
1635 #endif /* _WIN32 */
1636 
1637 	return(false);
1638 }
1639 
1640 /***********************************************************************//**
1641 Try to get number of bytes per sector from file system.
1642 @return	file block size */
1643 UNIV_INTERN
1644 ulint
1645 os_file_get_block_size(
1646 /*===================*/
1647 	os_file_t	file,	/*!< in: handle to a file */
1648 	const char*	name);	/*!< in: file name */
1649 
1650 #include "os0file.inl"
1651 
1652 #endif /* os0file_h */
1653