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