drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
os0file.cc
1/*****************************************************************************
2
3Copyright (C) 1995, 2010, Innobase Oy. All Rights Reserved.
4Copyright (C) 2009, Percona Inc.
5
6Portions of this file contain modifications contributed and copyrighted
7by Percona Inc.. Those modifications are
8gratefully acknowledged and are described briefly in the InnoDB
9documentation. The contributions by Percona Inc. are incorporated with
10their permission, and subject to the conditions contained in the file
11COPYING.Percona.
12
13This program is free software; you can redistribute it and/or modify it under
14the terms of the GNU General Public License as published by the Free Software
15Foundation; version 2 of the License.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
23St, Fifth Floor, Boston, MA 02110-1301 USA
24
25*****************************************************************************/
26
27/**************************************************/
33
34#include "os0file.h"
35
36#ifdef UNIV_NONINL
37#include "os0file.ic"
38#endif
39
40#include "ut0mem.h"
41#include "srv0srv.h"
42#include "srv0start.h"
43#include "fil0fil.h"
44#include "buf0buf.h"
45#include <errno.h>
46#include <fcntl.h>
47#include <limits.h>
48#include <unistd.h>
49#ifndef UNIV_HOTBACKUP
50# include "os0sync.h"
51# include "os0thread.h"
52#else /* !UNIV_HOTBACKUP */
53# ifdef __WIN__
54/* Add includes for the _stat() call to compile on Windows */
55# include <sys/types.h>
56# include <sys/stat.h>
57# endif /* __WIN__ */
58#endif /* !UNIV_HOTBACKUP */
59
60#if defined(LINUX_NATIVE_AIO)
61#include <libaio.h>
62#endif
63
64/* This specifies the file permissions InnoDB uses when it creates files in
65Unix; the value of os_innodb_umask is initialized in ha_innodb.cc to
66my_umask */
67
68#ifndef __WIN__
70UNIV_INTERN ulint os_innodb_umask
71 = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
72#else
74UNIV_INTERN ulint os_innodb_umask = 0;
75#endif
76
77#ifdef UNIV_DO_FLUSH
78/* If the following is set to TRUE, we do not call os_file_flush in every
79os_file_write. We can set this TRUE when the doublewrite buffer is used. */
80UNIV_INTERN ibool os_do_not_call_flush_at_each_write = FALSE;
81#else
82/* We do not call os_file_flush in every os_file_write. */
83#endif /* UNIV_DO_FLUSH */
84
85#ifndef UNIV_HOTBACKUP
86/* We use these mutexes to protect lseek + file i/o operation, if the
87OS does not provide an atomic pread or pwrite, or similar */
88#define OS_FILE_N_SEEK_MUTEXES 16
89UNIV_INTERN os_mutex_t os_file_seek_mutexes[OS_FILE_N_SEEK_MUTEXES];
90
91/* In simulated aio, merge at most this many consecutive i/os */
92#define OS_AIO_MERGE_N_CONSECUTIVE 64
93
94/**********************************************************************
95
96InnoDB AIO Implementation:
97=========================
98
99We support native AIO for windows and linux. For rest of the platforms
100we simulate AIO by special io-threads servicing the IO-requests.
101
102Simulated AIO:
103==============
104
105In platforms where we 'simulate' AIO following is a rough explanation
106of the high level design.
107There are four io-threads (for ibuf, log, read, write).
108All synchronous IO requests are serviced by the calling thread using
109os_file_write/os_file_read. The Asynchronous requests are queued up
110in an array (there are four such arrays) by the calling thread.
111Later these requests are picked up by the io-thread and are serviced
112synchronously.
113
114Windows native AIO:
115==================
116
117If srv_use_native_aio is not set then windows follow the same
118code as simulated AIO. If the flag is set then native AIO interface
119is used. On windows, one of the limitation is that if a file is opened
120for AIO no synchronous IO can be done on it. Therefore we have an
121extra fifth array to queue up synchronous IO requests.
122There are innodb_file_io_threads helper threads. These threads work
123on the four arrays mentioned above in Simulated AIO. No thread is
124required for the sync array.
125If a synchronous IO request is made, it is first queued in the sync
126array. Then the calling thread itself waits on the request, thus
127making the call synchronous.
128If an AIO request is made the calling thread not only queues it in the
129array but also submits the requests. The helper thread then collects
130the completed IO request and calls completion routine on it.
131
132Linux native AIO:
133=================
134
135If we have libaio installed on the system and innodb_use_native_aio
136is set to TRUE we follow the code path of native AIO, otherwise we
137do simulated AIO.
138There are innodb_file_io_threads helper threads. These threads work
139on the four arrays mentioned above in Simulated AIO.
140If a synchronous IO request is made, it is handled by calling
141os_file_write/os_file_read.
142If an AIO request is made the calling thread not only queues it in the
143array but also submits the requests. The helper thread then collects
144the completed IO request and calls completion routine on it.
145
146**********************************************************************/
147
149UNIV_INTERN ibool os_aio_print_debug = FALSE;
150
151#ifdef UNIV_PFS_IO
152/* Keys to register InnoDB I/O with performance schema */
153UNIV_INTERN mysql_pfs_key_t innodb_file_data_key;
154UNIV_INTERN mysql_pfs_key_t innodb_file_log_key;
155UNIV_INTERN mysql_pfs_key_t innodb_file_temp_key;
156#endif /* UNIV_PFS_IO */
157
159typedef struct os_aio_slot_struct os_aio_slot_t;
160
163 ibool is_read;
164 ulint pos;
166 ibool reserved;
168 ulint len;
170 byte* buf;
171 ulint type;
172 ulint offset;
176 const char* name;
183 void* message2;
187#ifdef WIN_ASYNC_IO
188 HANDLE handle;
190 OVERLAPPED control;
192#elif defined(LINUX_NATIVE_AIO)
193 struct iocb control; /* Linux control block for aio */
194 int n_bytes; /* bytes written/read. */
195 int ret; /* AIO return code */
196#endif
197};
198
200typedef struct os_aio_array_struct os_aio_array_t;
201
213 ulint n_slots;
221 ulint cur_seg;
228 os_aio_slot_t* slots;
229#ifdef __WIN__
230 HANDLE* handles;
237#endif
238
239#if defined(LINUX_NATIVE_AIO)
240 io_context_t* aio_ctx;
241 /* completion queue for IO. There is
242 one such queue per segment. Each thread
243 will work on one ctx exclusively. */
244 struct io_event* aio_events;
245 /* The array to collect completed IOs.
246 There is one such event for each
247 possible pending IO. The size of the
248 array is equal to n_slots. */
249#endif
250};
251
252#if defined(LINUX_NATIVE_AIO)
254#define OS_AIO_REAP_TIMEOUT (500000000UL)
255
257#define OS_AIO_IO_SETUP_RETRY_SLEEP (500000UL)
258
260#define OS_AIO_IO_SETUP_RETRY_ATTEMPTS 5
261#endif
262
264static os_event_t* os_aio_segment_wait_events = NULL;
265
268static os_aio_array_t* os_aio_read_array = NULL;
269static os_aio_array_t* os_aio_write_array = NULL;
270static os_aio_array_t* os_aio_ibuf_array = NULL;
271static os_aio_array_t* os_aio_log_array = NULL;
272static os_aio_array_t* os_aio_sync_array = NULL;
273/* @} */
274
276static ulint os_aio_n_segments = ULINT_UNDEFINED;
277
280static ibool os_aio_recommend_sleep_for_read_threads = FALSE;
281#endif /* !UNIV_HOTBACKUP */
282
283UNIV_INTERN ulint os_n_file_reads = 0;
284UNIV_INTERN ulint os_bytes_read_since_printout = 0;
285UNIV_INTERN ulint os_n_file_writes = 0;
286UNIV_INTERN ulint os_n_fsyncs = 0;
287UNIV_INTERN ulint os_n_file_reads_old = 0;
288UNIV_INTERN ulint os_n_file_writes_old = 0;
289UNIV_INTERN ulint os_n_fsyncs_old = 0;
290UNIV_INTERN time_t os_last_printout;
291
292UNIV_INTERN ibool os_has_said_disk_full = FALSE;
293
294#ifndef UNIV_HOTBACKUP
296static os_mutex_t os_file_count_mutex;
297#endif /* !UNIV_HOTBACKUP */
299UNIV_INTERN ulint os_file_n_pending_preads = 0;
301UNIV_INTERN ulint os_file_n_pending_pwrites = 0;
303UNIV_INTERN ulint os_n_pending_writes = 0;
305UNIV_INTERN ulint os_n_pending_reads = 0;
306
307#ifdef UNIV_DEBUG
308/**********************************************************************/
311UNIV_INTERN
312ibool
313os_aio_validate_skip(void)
314/*======================*/
315{
317# define OS_AIO_VALIDATE_SKIP 13
318
321 static int os_aio_validate_count = OS_AIO_VALIDATE_SKIP;
322
323 /* There is a race condition below, but it does not matter,
324 because this call is only for heuristic purposes. We want to
325 reduce the call frequency of the costly os_aio_validate()
326 check in debug builds. */
327 if (--os_aio_validate_count > 0) {
328 return(TRUE);
329 }
330
331 os_aio_validate_count = OS_AIO_VALIDATE_SKIP;
332 return(os_aio_validate());
333}
334#endif /* UNIV_DEBUG */
335
336#ifdef __WIN__
337/***********************************************************************/
341UNIV_INTERN
342ulint
343os_get_os_version(void)
344/*===================*/
345{
346 OSVERSIONINFO os_info;
347
348 os_info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
349
350 ut_a(GetVersionEx(&os_info));
351
352 if (os_info.dwPlatformId == VER_PLATFORM_WIN32s) {
353 return(OS_WIN31);
354 } else if (os_info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
355 return(OS_WIN95);
356 } else if (os_info.dwPlatformId == VER_PLATFORM_WIN32_NT) {
357 switch (os_info.dwMajorVersion) {
358 case 3:
359 case 4:
360 return OS_WINNT;
361 case 5:
362 return (os_info.dwMinorVersion == 0) ? OS_WIN2000
363 : OS_WINXP;
364 case 6:
365 return (os_info.dwMinorVersion == 0) ? OS_WINVISTA
366 : OS_WIN7;
367 default:
368 return OS_WIN7;
369 }
370 } else {
371 ut_error;
372 return(0);
373 }
374}
375#endif /* __WIN__ */
376
377/***********************************************************************/
383UNIV_INTERN
384ulint
386/*===================*/
387 ibool report_all_errors)
389{
390 ulint err;
391
392#ifdef __WIN__
393
394 err = (ulint) GetLastError();
395
396 if (report_all_errors
397 || (err != ERROR_DISK_FULL && err != ERROR_FILE_EXISTS)) {
398
399 ut_print_timestamp(stderr);
400 fprintf(stderr,
401 " InnoDB: Operating system error number %lu"
402 " in a file operation.\n", (ulong) err);
403
404 if (err == ERROR_PATH_NOT_FOUND) {
405 fprintf(stderr,
406 "InnoDB: The error means the system"
407 " cannot find the path specified.\n");
408
410 fprintf(stderr,
411 "InnoDB: If you are installing InnoDB,"
412 " remember that you must create\n"
413 "InnoDB: directories yourself, InnoDB"
414 " does not create them.\n");
415 }
416 } else if (err == ERROR_ACCESS_DENIED) {
417 fprintf(stderr,
418 "InnoDB: The error means mysqld does not have"
419 " the access rights to\n"
420 "InnoDB: the directory. It may also be"
421 " you have created a subdirectory\n"
422 "InnoDB: of the same name as a data file.\n");
423 } else if (err == ERROR_SHARING_VIOLATION
424 || err == ERROR_LOCK_VIOLATION) {
425 fprintf(stderr,
426 "InnoDB: The error means that another program"
427 " is using InnoDB's files.\n"
428 "InnoDB: This might be a backup or antivirus"
429 " software or another instance\n"
430 "InnoDB: of MySQL."
431 " Please close it to get rid of this error.\n");
432 } else if (err == ERROR_WORKING_SET_QUOTA
433 || err == ERROR_NO_SYSTEM_RESOURCES) {
434 fprintf(stderr,
435 "InnoDB: The error means that there are no"
436 " sufficient system resources or quota to"
437 " complete the operation.\n");
438 } else if (err == ERROR_OPERATION_ABORTED) {
439 fprintf(stderr,
440 "InnoDB: The error means that the I/O"
441 " operation has been aborted\n"
442 "InnoDB: because of either a thread exit"
443 " or an application request.\n"
444 "InnoDB: Retry attempt is made.\n");
445 } else {
446 fprintf(stderr,
447 "InnoDB: Some operating system error numbers"
448 " are described at\n"
449 "InnoDB: "
450 REFMAN
451 "operating-system-error-codes.html\n");
452 }
453 }
454
455 fflush(stderr);
456
457 if (err == ERROR_FILE_NOT_FOUND) {
458 return(OS_FILE_NOT_FOUND);
459 } else if (err == ERROR_DISK_FULL) {
460 return(OS_FILE_DISK_FULL);
461 } else if (err == ERROR_FILE_EXISTS) {
462 return(OS_FILE_ALREADY_EXISTS);
463 } else if (err == ERROR_SHARING_VIOLATION
464 || err == ERROR_LOCK_VIOLATION) {
465 return(OS_FILE_SHARING_VIOLATION);
466 } else if (err == ERROR_WORKING_SET_QUOTA
467 || err == ERROR_NO_SYSTEM_RESOURCES) {
468 return(OS_FILE_INSUFFICIENT_RESOURCE);
469 } else if (err == ERROR_OPERATION_ABORTED) {
470 return(OS_FILE_OPERATION_ABORTED);
471 } else {
472 return(100 + err);
473 }
474#else
475 err = (ulint) errno;
476
477 if (report_all_errors
478 || (err != ENOSPC && err != EEXIST)) {
479
480 ut_print_timestamp(stderr);
481 fprintf(stderr,
482 " InnoDB: Operating system error number %lu"
483 " in a file operation.\n", (ulong) err);
484
485 if (err == ENOENT) {
486 fprintf(stderr,
487 "InnoDB: The error means the system"
488 " cannot find the path specified.\n");
489
491 fprintf(stderr,
492 "InnoDB: If you are installing InnoDB,"
493 " remember that you must create\n"
494 "InnoDB: directories yourself, InnoDB"
495 " does not create them.\n");
496 }
497 } else if (err == EACCES) {
498 fprintf(stderr,
499 "InnoDB: The error means mysqld does not have"
500 " the access rights to\n"
501 "InnoDB: the directory.\n");
502 } else {
503 if (strerror((int)err) != NULL) {
504 fprintf(stderr,
505 "InnoDB: Error number %lu"
506 " means '%s'.\n",
507 err, strerror((int)err));
508 }
509
510 fprintf(stderr,
511 "InnoDB: Some operating system"
512 " error numbers are described at\n"
513 "InnoDB: "
514 REFMAN
515 "operating-system-error-codes.html\n");
516 }
517 }
518
519 fflush(stderr);
520
521 switch (err) {
522 case ENOSPC:
523 return(OS_FILE_DISK_FULL);
524 case ENOENT:
525 return(OS_FILE_NOT_FOUND);
526 case EEXIST:
527 return(OS_FILE_ALREADY_EXISTS);
528 case EXDEV:
529 case ENOTDIR:
530 case EISDIR:
531 return(OS_FILE_PATH_ERROR);
532 case EAGAIN:
533 if (srv_use_native_aio) {
534 return(OS_FILE_AIO_RESOURCES_RESERVED);
535 }
536 break;
537 case EINTR:
538 if (srv_use_native_aio) {
539 return(OS_FILE_AIO_INTERRUPTED);
540 }
541 break;
542 }
543 return(100 + err);
544#endif
545}
546
547/****************************************************************/
552static
553ibool
554os_file_handle_error_cond_exit(
555/*===========================*/
556 const char* name,
557 const char* operation,
558 ibool should_exit)
560{
561 ulint err;
562
563 err = os_file_get_last_error(FALSE);
564
565 if (err == OS_FILE_DISK_FULL) {
566 /* We only print a warning about disk full once */
567
568 if (os_has_said_disk_full) {
569
570 return(FALSE);
571 }
572
573 if (name) {
574 ut_print_timestamp(stderr);
575 fprintf(stderr,
576 " InnoDB: Encountered a problem with"
577 " file %s\n", name);
578 }
579
580 ut_print_timestamp(stderr);
581 fprintf(stderr,
582 " InnoDB: Disk is full. Try to clean the disk"
583 " to free space.\n");
584
585 os_has_said_disk_full = TRUE;
586
587 fflush(stderr);
588
589 return(FALSE);
590 } else if (err == OS_FILE_AIO_RESOURCES_RESERVED) {
591
592 return(TRUE);
593 } else if (err == OS_FILE_AIO_INTERRUPTED) {
594
595 return(TRUE);
596 } else if (err == OS_FILE_ALREADY_EXISTS
597 || err == OS_FILE_PATH_ERROR) {
598
599 return(FALSE);
600 } else if (err == OS_FILE_SHARING_VIOLATION) {
601
602 os_thread_sleep(10000000); /* 10 sec */
603 return(TRUE);
604 } else if (err == OS_FILE_INSUFFICIENT_RESOURCE) {
605
606 os_thread_sleep(100000); /* 100 ms */
607 return(TRUE);
608 } else if (err == OS_FILE_OPERATION_ABORTED) {
609
610 os_thread_sleep(100000); /* 100 ms */
611 return(TRUE);
612 } else {
613 if (name) {
614 fprintf(stderr, "InnoDB: File name %s\n", name);
615 }
616
617 fprintf(stderr, "InnoDB: File operation call: '%s'.\n",
618 operation);
619
620 if (should_exit) {
621 fprintf(stderr, "InnoDB: Cannot continue operation.\n");
622
623 fflush(stderr);
624
625 exit(1);
626 }
627 }
628
629 return(FALSE);
630}
631
632/****************************************************************/
635static
636ibool
637os_file_handle_error(
638/*=================*/
639 const char* name,
640 const char* operation)
641{
642 /* exit in case of unknown error */
643 return(os_file_handle_error_cond_exit(name, operation, TRUE));
644}
645
646/****************************************************************/
649static
650ibool
651os_file_handle_error_no_exit(
652/*=========================*/
653 const char* name,
654 const char* operation)
655{
656 /* don't exit in case of unknown error */
657 return(os_file_handle_error_cond_exit(name, operation, FALSE));
658}
659
660#undef USE_FILE_LOCK
661#define USE_FILE_LOCK
662#if defined(UNIV_HOTBACKUP) || defined(__WIN__)
663/* InnoDB Hot Backup does not lock the data files.
664 * On Windows, mandatory locking is used.
665 */
666# undef USE_FILE_LOCK
667#endif
668#ifdef USE_FILE_LOCK
669/****************************************************************/
672static
673int
674os_file_lock(
675/*=========*/
676 int fd,
677 const char* name)
678{
679 struct flock lk;
680
681 if (srv_read_only)
682 return 0;
683
684 lk.l_type = F_WRLCK;
685 lk.l_whence = SEEK_SET;
686 lk.l_start = lk.l_len = 0;
687 if (fcntl(fd, F_SETLK, &lk) == -1) {
688 fprintf(stderr,
689 "InnoDB: Unable to lock %s, error: %d\n", name, errno);
690
691 if (errno == EAGAIN || errno == EACCES) {
692 fprintf(stderr,
693 "InnoDB: Check that you do not already have"
694 " another drizzled process\n"
695 "InnoDB: using the same InnoDB data"
696 " or log files.\n");
697 }
698
699 return(-1);
700 }
701
702 return(0);
703}
704#endif /* USE_FILE_LOCK */
705
706#ifndef UNIV_HOTBACKUP
707/****************************************************************/
709UNIV_INTERN
710void
712/*===================*/
713{
714 ulint i;
715
716 os_file_count_mutex = os_mutex_create();
717
718 for (i = 0; i < OS_FILE_N_SEEK_MUTEXES; i++) {
719 os_file_seek_mutexes[i] = os_mutex_create();
720 }
721}
722
723/***********************************************************************/
727UNIV_INTERN
728FILE*
730/*========================*/
731{
732 FILE* file = NULL;
733 int fd = innobase_mysql_tmpfile();
734
735 if (fd >= 0) {
736 file = fdopen(fd, "w+b");
737 }
738
739 if (!file) {
740 ut_print_timestamp(stderr);
741 fprintf(stderr,
742 " InnoDB: Error: unable to create temporary file;"
743 " errno: %d\n", errno);
744 if (fd >= 0) {
745 close(fd);
746 }
747 }
748
749 return(file);
750}
751#endif /* !UNIV_HOTBACKUP */
752
753/***********************************************************************/
759UNIV_INTERN
762/*============*/
763 const char* dirname,
765 ibool error_is_fatal)
770{
771 os_file_dir_t dir;
772#ifdef __WIN__
773 LPWIN32_FIND_DATA lpFindFileData;
774 char path[OS_FILE_MAX_PATH + 3];
775
776 ut_a(strlen(dirname) < OS_FILE_MAX_PATH);
777
778 strcpy(path, dirname);
779 strcpy(path + strlen(path), "\\*");
780
781 /* Note that in Windows opening the 'directory stream' also retrieves
782 the first entry in the directory. Since it is '.', that is no problem,
783 as we will skip over the '.' and '..' entries anyway. */
784
785 lpFindFileData = ut_malloc(sizeof(WIN32_FIND_DATA));
786
787 dir = FindFirstFile((LPCTSTR) path, lpFindFileData);
788
789 ut_free(lpFindFileData);
790
791 if (dir == INVALID_HANDLE_VALUE) {
792
793 if (error_is_fatal) {
794 os_file_handle_error(dirname, "opendir");
795 }
796
797 return(NULL);
798 }
799
800 return(dir);
801#else
802 dir = opendir(dirname);
803
804 if (dir == NULL && error_is_fatal) {
805 os_file_handle_error(dirname, "opendir");
806 }
807
808 return(dir);
809#endif
810}
811
812/***********************************************************************/
815UNIV_INTERN
816int
818/*=============*/
819 os_file_dir_t dir)
820{
821#ifdef __WIN__
822 BOOL ret;
823
824 ret = FindClose(dir);
825
826 if (!ret) {
827 os_file_handle_error_no_exit(NULL, "closedir");
828
829 return(-1);
830 }
831
832 return(0);
833#else
834 int ret;
835
836 ret = closedir(dir);
837
838 if (ret) {
839 os_file_handle_error_no_exit(NULL, "closedir");
840 }
841
842 return(ret);
843#endif
844}
845
846/***********************************************************************/
850UNIV_INTERN
851int
853/*======================*/
854 const char* dirname,
855 os_file_dir_t dir,
856 os_file_stat_t* info)
857{
858#ifdef __WIN__
859 LPWIN32_FIND_DATA lpFindFileData;
860 BOOL ret;
861
862 lpFindFileData = ut_malloc(sizeof(WIN32_FIND_DATA));
863next_file:
864 ret = FindNextFile(dir, lpFindFileData);
865
866 if (ret) {
867 ut_a(strlen((char *) lpFindFileData->cFileName)
868 < OS_FILE_MAX_PATH);
869
870 if (strcmp((char *) lpFindFileData->cFileName, ".") == 0
871 || strcmp((char *) lpFindFileData->cFileName, "..") == 0) {
872
873 goto next_file;
874 }
875
876 strcpy(info->name, (char *) lpFindFileData->cFileName);
877
878 info->size = (ib_int64_t)(lpFindFileData->nFileSizeLow)
879 + (((ib_int64_t)(lpFindFileData->nFileSizeHigh))
880 << 32);
881
882 if (lpFindFileData->dwFileAttributes
883 & FILE_ATTRIBUTE_REPARSE_POINT) {
884 /* TODO: test Windows symlinks */
885 /* TODO: MySQL has apparently its own symlink
886 implementation in Windows, dbname.sym can
887 redirect a database directory:
888 REFMAN "windows-symbolic-links.html" */
889 info->type = OS_FILE_TYPE_LINK;
890 } else if (lpFindFileData->dwFileAttributes
891 & FILE_ATTRIBUTE_DIRECTORY) {
892 info->type = OS_FILE_TYPE_DIR;
893 } else {
894 /* It is probably safest to assume that all other
895 file types are normal. Better to check them rather
896 than blindly skip them. */
897
898 info->type = OS_FILE_TYPE_FILE;
899 }
900 }
901
902 ut_free(lpFindFileData);
903
904 if (ret) {
905 return(0);
906 } else if (GetLastError() == ERROR_NO_MORE_FILES) {
907
908 return(1);
909 } else {
910 os_file_handle_error_no_exit(dirname,
911 "readdir_next_file");
912 return(-1);
913 }
914#else
915 struct dirent* ent;
916 char* full_path;
917 int ret;
918 struct stat statinfo;
919#ifdef HAVE_READDIR_R
920 char dirent_buf[sizeof(struct dirent)
921 + _POSIX_PATH_MAX + 100];
922 /* In /mysys/my_lib.c, _POSIX_PATH_MAX + 1 is used as
923 the max file name len; but in most standards, the
924 length is NAME_MAX; we add 100 to be even safer */
925#endif
926
927next_file:
928
929#ifdef HAVE_READDIR_R
930 ret = readdir_r(dir, (struct dirent*)dirent_buf, &ent);
931
932 if (ret != 0
933#ifdef UNIV_AIX
934 /* On AIX, only if we got non-NULL 'ent' (result) value and
935 a non-zero 'ret' (return) value, it indicates a failed
936 readdir_r() call. An NULL 'ent' with an non-zero 'ret'
937 would indicate the "end of the directory" is reached. */
938 && ent != NULL
939#endif
940 ) {
941 fprintf(stderr,
942 "InnoDB: cannot read directory %s, error %lu\n",
943 dirname, (ulong)ret);
944
945 return(-1);
946 }
947
948 if (ent == NULL) {
949 /* End of directory */
950
951 return(1);
952 }
953
954 ut_a(strlen(ent->d_name) < _POSIX_PATH_MAX + 100 - 1);
955#else
956 ent = readdir(dir);
957
958 if (ent == NULL) {
959
960 return(1);
961 }
962#endif
963 ut_a(strlen(ent->d_name) < OS_FILE_MAX_PATH);
964
965 if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
966
967 goto next_file;
968 }
969
970 strcpy(info->name, ent->d_name);
971
972 full_path = static_cast<char* >(ut_malloc(strlen(dirname) + strlen(ent->d_name) + 10));
973
974 sprintf(full_path, "%s/%s", dirname, ent->d_name);
975
976 ret = stat(full_path, &statinfo);
977
978 if (ret) {
979
980 if (errno == ENOENT) {
981 /* readdir() returned a file that does not exist,
982 it must have been deleted in the meantime. Do what
983 would have happened if the file was deleted before
984 readdir() - ignore and go to the next entry.
985 If this is the last entry then info->name will still
986 contain the name of the deleted file when this
987 function returns, but this is not an issue since the
988 caller shouldn't be looking at info when end of
989 directory is returned. */
990
991 ut_free(full_path);
992
993 goto next_file;
994 }
995
996 os_file_handle_error_no_exit(full_path, "stat");
997
998 ut_free(full_path);
999
1000 return(-1);
1001 }
1002
1003 info->size = (ib_int64_t)statinfo.st_size;
1004
1005 if (S_ISDIR(statinfo.st_mode)) {
1006 info->type = OS_FILE_TYPE_DIR;
1007 } else if (S_ISLNK(statinfo.st_mode)) {
1008 info->type = OS_FILE_TYPE_LINK;
1009 } else if (S_ISREG(statinfo.st_mode)) {
1010 info->type = OS_FILE_TYPE_FILE;
1011 } else {
1012 info->type = OS_FILE_TYPE_UNKNOWN;
1013 }
1014
1015 ut_free(full_path);
1016
1017 return(0);
1018#endif
1019}
1020
1021/*****************************************************************/
1027UNIV_INTERN
1028ibool
1030/*=====================*/
1031 const char* pathname,
1033 ibool fail_if_exists)
1035{
1036#ifdef __WIN__
1037 BOOL rcode;
1038
1039 rcode = CreateDirectory((LPCTSTR) pathname, NULL);
1040 if (!(rcode != 0
1041 || (GetLastError() == ERROR_ALREADY_EXISTS
1042 && !fail_if_exists))) {
1043 /* failure */
1044 os_file_handle_error(pathname, "CreateDirectory");
1045
1046 return(FALSE);
1047 }
1048
1049 return (TRUE);
1050#else
1051 int rcode;
1052
1053 rcode = mkdir(pathname, 0770);
1054
1055 if (!(rcode == 0 || (errno == EEXIST && !fail_if_exists))) {
1056 /* failure */
1057 os_file_handle_error(pathname, "mkdir");
1058
1059 return(FALSE);
1060 }
1061
1062 return (TRUE);
1063#endif
1064}
1065
1066/****************************************************************/
1072UNIV_INTERN
1075/*=======================*/
1076 const char* name,
1078 ulint create_mode,
1085 ulint access_type,
1087 ibool* success)
1088{
1089#ifdef __WIN__
1090 os_file_t file;
1091 DWORD create_flag;
1092 DWORD access;
1093 DWORD attributes = 0;
1094 ibool retry;
1095
1096try_again:
1097 ut_a(name);
1098
1099 if (create_mode == OS_FILE_OPEN) {
1100 create_flag = OPEN_EXISTING;
1101 } else if (create_mode == OS_FILE_CREATE) {
1102 create_flag = CREATE_NEW;
1103 } else if (create_mode == OS_FILE_CREATE_PATH) {
1104 /* create subdirs along the path if needed */
1105 *success = os_file_create_subdirs_if_needed(name);
1106 if (!*success) {
1107 ut_error;
1108 }
1109 create_flag = CREATE_NEW;
1110 create_mode = OS_FILE_CREATE;
1111 } else {
1112 create_flag = 0;
1113 ut_error;
1114 }
1115
1116 if (access_type == OS_FILE_READ_ONLY) {
1117 access = GENERIC_READ;
1118 } else if (access_type == OS_FILE_READ_WRITE) {
1119 access = GENERIC_READ | GENERIC_WRITE;
1120 } else {
1121 access = 0;
1122 ut_error;
1123 }
1124
1125 file = CreateFile((LPCTSTR) name,
1126 access,
1127 FILE_SHARE_READ | FILE_SHARE_WRITE,
1128 /* file can be read and written also
1129 by other processes */
1130 NULL, /* default security attributes */
1131 create_flag,
1132 attributes,
1133 NULL);
1134
1135 if (file == INVALID_HANDLE_VALUE) {
1136 *success = FALSE;
1137
1138 retry = os_file_handle_error(name,
1139 create_mode == OS_FILE_OPEN ?
1140 "open" : "create");
1141 if (retry) {
1142 goto try_again;
1143 }
1144 } else {
1145 *success = TRUE;
1146 }
1147
1148 return(file);
1149#else /* __WIN__ */
1150 os_file_t file;
1151 int create_flag;
1152 ibool retry;
1153
1154try_again:
1155 ut_a(name);
1156
1157 if (create_mode == OS_FILE_OPEN) {
1158 if (access_type == OS_FILE_READ_ONLY) {
1159 create_flag = O_RDONLY;
1160 } else {
1161 create_flag = O_RDWR;
1162 }
1163 } else if (create_mode == OS_FILE_CREATE) {
1164 create_flag = O_RDWR | O_CREAT | O_EXCL;
1165 } else if (create_mode == OS_FILE_CREATE_PATH) {
1166 /* create subdirs along the path if needed */
1167 *success = os_file_create_subdirs_if_needed(name);
1168 if (!*success) {
1169 return (-1);
1170 }
1171 create_flag = O_RDWR | O_CREAT | O_EXCL;
1172 create_mode = OS_FILE_CREATE;
1173 } else {
1174 create_flag = 0;
1175 ut_error;
1176 }
1177
1178 if (create_mode == OS_FILE_CREATE) {
1179 file = open(name, create_flag, S_IRUSR | S_IWUSR
1180 | S_IRGRP | S_IWGRP);
1181 } else {
1182 file = open(name, create_flag);
1183 }
1184
1185 if (file == -1) {
1186 *success = FALSE;
1187
1188 retry = os_file_handle_error(name,
1189 create_mode == OS_FILE_OPEN ?
1190 "open" : "create");
1191 if (retry) {
1192 goto try_again;
1193 }
1194#ifdef USE_FILE_LOCK
1195 } else if (access_type == OS_FILE_READ_WRITE
1196 && os_file_lock(file, name)) {
1197 *success = FALSE;
1198 close(file);
1199 file = -1;
1200#endif
1201 } else {
1202 *success = TRUE;
1203 }
1204
1205 return(file);
1206#endif /* __WIN__ */
1207}
1208
1209/****************************************************************/
1215UNIV_INTERN
1218/*=========================================*/
1219 const char* name,
1221 ulint create_mode,
1225 ulint access_type,
1229 ibool* success)
1230{
1231#ifdef __WIN__
1232 os_file_t file;
1233 DWORD create_flag;
1234 DWORD access;
1235 DWORD attributes = 0;
1236 DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
1237
1238 ut_a(name);
1239
1240 if (create_mode == OS_FILE_OPEN) {
1241 create_flag = OPEN_EXISTING;
1242 } else if (create_mode == OS_FILE_CREATE) {
1243 create_flag = CREATE_NEW;
1244 } else {
1245 create_flag = 0;
1246 ut_error;
1247 }
1248
1249 if (access_type == OS_FILE_READ_ONLY) {
1250 access = GENERIC_READ;
1251 } else if (access_type == OS_FILE_READ_WRITE) {
1252 access = GENERIC_READ | GENERIC_WRITE;
1253 } else if (access_type == OS_FILE_READ_ALLOW_DELETE) {
1254 access = GENERIC_READ;
1255 share_mode = FILE_SHARE_DELETE | FILE_SHARE_READ
1256 | FILE_SHARE_WRITE;
1260 } else {
1261 access = 0;
1262 ut_error;
1263 }
1264
1265 file = CreateFile((LPCTSTR) name,
1266 access,
1267 share_mode,
1268 NULL, /* default security attributes */
1269 create_flag,
1270 attributes,
1271 NULL);
1272
1273 if (file == INVALID_HANDLE_VALUE) {
1274 *success = FALSE;
1275 } else {
1276 *success = TRUE;
1277 }
1278
1279 return(file);
1280#else /* __WIN__ */
1281 os_file_t file;
1282 int create_flag;
1283
1284 ut_a(name);
1285
1286 if (create_mode == OS_FILE_OPEN) {
1287 if (access_type == OS_FILE_READ_ONLY) {
1288 create_flag = O_RDONLY;
1289 } else {
1290 create_flag = O_RDWR;
1291 }
1292 } else if (create_mode == OS_FILE_CREATE) {
1293 create_flag = O_RDWR | O_CREAT | O_EXCL;
1294 } else {
1295 create_flag = 0;
1296 ut_error;
1297 }
1298
1299 if (create_mode == OS_FILE_CREATE) {
1300 file = open(name, create_flag, S_IRUSR | S_IWUSR
1301 | S_IRGRP | S_IWGRP);
1302 } else {
1303 file = open(name, create_flag);
1304 }
1305
1306 if (file == -1) {
1307 *success = FALSE;
1308#ifdef USE_FILE_LOCK
1309 } else if (access_type == OS_FILE_READ_WRITE
1310 && os_file_lock(file, name)) {
1311 *success = FALSE;
1312 close(file);
1313 file = -1;
1314#endif
1315 } else {
1316 *success = TRUE;
1317 }
1318
1319 return(file);
1320#endif /* __WIN__ */
1321}
1322
1323/****************************************************************/
1325UNIV_INTERN
1326void
1328/*================*/
1329 int fd,
1330 const char* file_name,
1331 const char* operation_name)
1334{
1335 /* some versions of Solaris may not have DIRECTIO_ON */
1336#if defined(UNIV_SOLARIS) && defined(DIRECTIO_ON)
1337 if (directio(fd, DIRECTIO_ON) == -1) {
1338 int errno_save;
1339 errno_save = (int)errno;
1340 ut_print_timestamp(stderr);
1341 fprintf(stderr,
1342 " InnoDB: Failed to set DIRECTIO_ON "
1343 "on file %s: %s: %s, continuing anyway\n",
1344 file_name, operation_name, strerror(errno_save));
1345 }
1346#elif defined(O_DIRECT)
1347 if (fcntl(fd, F_SETFL, O_DIRECT) == -1) {
1348 int errno_save;
1349 errno_save = (int)errno;
1350 ut_print_timestamp(stderr);
1351 fprintf(stderr,
1352 " InnoDB: Failed to set O_DIRECT "
1353 "on file %s: %s: %s, continuing anyway\n",
1354 file_name, operation_name, strerror(errno_save));
1355 if (errno_save == EINVAL) {
1356 ut_print_timestamp(stderr);
1357 fprintf(stderr,
1358 " InnoDB: O_DIRECT is known to result in "
1359 "'Invalid argument' on Linux on tmpfs, "
1360 "see MySQL Bug#26662\n");
1361 }
1362 }
1363#else /* Required for OSX */
1364 (void)fd;
1365 (void)file_name;
1366 (void)operation_name;
1367#endif
1368}
1369
1370/****************************************************************/
1376UNIV_INTERN
1379/*================*/
1380 const char* name,
1382 ulint create_mode,
1390 ulint purpose,
1397 ulint type,
1398 ibool* success)
1399{
1400#ifdef __WIN__
1401 os_file_t file;
1402 DWORD share_mode = FILE_SHARE_READ;
1403 DWORD create_flag;
1404 DWORD attributes;
1405 ibool retry;
1406try_again:
1407 ut_a(name);
1408
1409 if (create_mode == OS_FILE_OPEN_RAW) {
1410 create_flag = OPEN_EXISTING;
1411 share_mode = FILE_SHARE_WRITE;
1412 } else if (create_mode == OS_FILE_OPEN
1413 || create_mode == OS_FILE_OPEN_RETRY) {
1414 create_flag = OPEN_EXISTING;
1415 } else if (create_mode == OS_FILE_CREATE) {
1416 create_flag = CREATE_NEW;
1417 } else if (create_mode == OS_FILE_OVERWRITE) {
1418 create_flag = CREATE_ALWAYS;
1419 } else {
1420 create_flag = 0;
1421 ut_error;
1422 }
1423
1424 if (purpose == OS_FILE_AIO) {
1425 /* If specified, use asynchronous (overlapped) io and no
1426 buffering of writes in the OS */
1427 attributes = 0;
1428#ifdef WIN_ASYNC_IO
1429 if (srv_use_native_aio) {
1430 attributes = attributes | FILE_FLAG_OVERLAPPED;
1431 }
1432#endif
1433#ifdef UNIV_NON_BUFFERED_IO
1434# ifndef UNIV_HOTBACKUP
1435 if (type == OS_LOG_FILE && srv_flush_log_at_trx_commit == 2) {
1436 /* Do not use unbuffered i/o to log files because
1437 value 2 denotes that we do not flush the log at every
1438 commit, but only once per second */
1439 } else if (srv_win_file_flush_method
1441 attributes = attributes | FILE_FLAG_NO_BUFFERING;
1442 }
1443# else /* !UNIV_HOTBACKUP */
1444 attributes = attributes | FILE_FLAG_NO_BUFFERING;
1445# endif /* !UNIV_HOTBACKUP */
1446#endif /* UNIV_NON_BUFFERED_IO */
1447 } else if (purpose == OS_FILE_NORMAL) {
1448 attributes = 0;
1449#ifdef UNIV_NON_BUFFERED_IO
1450# ifndef UNIV_HOTBACKUP
1451 if (type == OS_LOG_FILE && srv_flush_log_at_trx_commit == 2) {
1452 /* Do not use unbuffered i/o to log files because
1453 value 2 denotes that we do not flush the log at every
1454 commit, but only once per second */
1455 } else if (srv_win_file_flush_method
1457 attributes = attributes | FILE_FLAG_NO_BUFFERING;
1458 }
1459# else /* !UNIV_HOTBACKUP */
1460 attributes = attributes | FILE_FLAG_NO_BUFFERING;
1461# endif /* !UNIV_HOTBACKUP */
1462#endif /* UNIV_NON_BUFFERED_IO */
1463 } else {
1464 attributes = 0;
1465 ut_error;
1466 }
1467
1468 file = CreateFile((LPCTSTR) name,
1469 GENERIC_READ | GENERIC_WRITE, /* read and write
1470 access */
1471 share_mode, /* File can be read also by other
1472 processes; we must give the read
1473 permission because of ibbackup. We do
1474 not give the write permission to
1475 others because if one would succeed to
1476 start 2 instances of mysqld on the
1477 SAME files, that could cause severe
1478 database corruption! When opening
1479 raw disk partitions, Microsoft manuals
1480 say that we must give also the write
1481 permission. */
1482 NULL, /* default security attributes */
1483 create_flag,
1484 attributes,
1485 NULL);
1486
1487 if (file == INVALID_HANDLE_VALUE) {
1488 *success = FALSE;
1489
1490 /* When srv_file_per_table is on, file creation failure may not
1491 be critical to the whole instance. Do not crash the server in
1492 case of unknown errors.
1493 Please note "srv_file_per_table" is a global variable with
1494 no explicit synchronization protection. It could be
1495 changed during this execution path. It might not have the
1496 same value as the one when building the table definition */
1497 if (srv_file_per_table) {
1498 retry = os_file_handle_error_no_exit(name,
1499 create_mode == OS_FILE_CREATE ?
1500 "create" : "open");
1501 } else {
1502 retry = os_file_handle_error(name,
1503 create_mode == OS_FILE_CREATE ?
1504 "create" : "open");
1505 }
1506
1507 if (retry) {
1508 goto try_again;
1509 }
1510 } else {
1511 *success = TRUE;
1512 }
1513
1514 return(file);
1515#else /* __WIN__ */
1516 os_file_t file;
1517 int create_flag;
1518 ibool retry;
1519 const char* mode_str = NULL;
1520
1521try_again:
1522 ut_a(name);
1523
1524 if (create_mode == OS_FILE_OPEN || create_mode == OS_FILE_OPEN_RAW
1525 || create_mode == OS_FILE_OPEN_RETRY) {
1526 mode_str = "OPEN";
1527 if (srv_read_only)
1528 create_flag = O_RDONLY;
1529 else
1530 create_flag = O_RDWR;
1531 } else if (create_mode == OS_FILE_CREATE) {
1532 mode_str = "CREATE";
1533 create_flag = O_RDWR | O_CREAT | O_EXCL;
1534 } else if (create_mode == OS_FILE_OVERWRITE) {
1535 mode_str = "OVERWRITE";
1536 create_flag = O_RDWR | O_CREAT | O_TRUNC;
1537 } else {
1538 create_flag = 0;
1539 ut_error;
1540 }
1541
1542 ut_a(type == OS_LOG_FILE || type == OS_DATA_FILE);
1543 ut_a(purpose == OS_FILE_AIO || purpose == OS_FILE_NORMAL);
1544
1545#ifdef O_SYNC
1546 /* We let O_SYNC only affect log files; note that we map O_DSYNC to
1547 O_SYNC because the datasync options seemed to corrupt files in 2001
1548 in both Linux and Solaris */
1549 if (type == OS_LOG_FILE
1550 && srv_unix_file_flush_method == SRV_UNIX_O_DSYNC) {
1551
1552# if 0
1553 fprintf(stderr, "Using O_SYNC for file %s\n", name);
1554# endif
1555
1556 create_flag = create_flag | O_SYNC;
1557 }
1558#endif /* O_SYNC */
1559
1560 file = open(name, create_flag, os_innodb_umask);
1561
1562 if (file == -1) {
1563 *success = FALSE;
1564
1565 /* When srv_file_per_table is on, file creation failure may not
1566 be critical to the whole instance. Do not crash the server in
1567 case of unknown errors.
1568 Please note "srv_file_per_table" is a global variable with
1569 no explicit synchronization protection. It could be
1570 changed during this execution path. It might not have the
1571 same value as the one when building the table definition */
1572 if (srv_file_per_table) {
1573 retry = os_file_handle_error_no_exit(name,
1574 create_mode == OS_FILE_CREATE ?
1575 "create" : "open");
1576 } else {
1577 retry = os_file_handle_error(name,
1578 create_mode == OS_FILE_CREATE ?
1579 "create" : "open");
1580 }
1581
1582 if (retry) {
1583 goto try_again;
1584 } else {
1585 return(file /* -1 */);
1586 }
1587 }
1588 /* else */
1589
1590 *success = TRUE;
1591
1592 /* We disable OS caching (O_DIRECT) only on data files */
1593 if (type != OS_LOG_FILE
1594 && srv_unix_file_flush_method == SRV_UNIX_O_DIRECT) {
1595
1596 os_file_set_nocache(file, name, mode_str);
1597 }
1598
1599 /* With ALL_O_DIRECT we disable OS caching for trx log file too */
1600 if (srv_unix_file_flush_method == SRV_UNIX_ALL_O_DIRECT) {
1601 os_file_set_nocache(file, name, mode_str);
1602 }
1603
1604#ifdef USE_FILE_LOCK
1605 if (create_mode != OS_FILE_OPEN_RAW && os_file_lock(file, name)) {
1606
1607 if (create_mode == OS_FILE_OPEN_RETRY) {
1608 int i;
1609 ut_print_timestamp(stderr);
1610 fputs(" InnoDB: Retrying to lock"
1611 " the first data file\n",
1612 stderr);
1613 for (i = 0; i < 100; i++) {
1614 os_thread_sleep(1000000);
1615 if (!os_file_lock(file, name)) {
1616 *success = TRUE;
1617 return(file);
1618 }
1619 }
1620 ut_print_timestamp(stderr);
1621 fputs(" InnoDB: Unable to open the first data file\n",
1622 stderr);
1623 }
1624
1625 *success = FALSE;
1626 close(file);
1627 file = -1;
1628 }
1629#endif /* USE_FILE_LOCK */
1630
1631 return(file);
1632#endif /* __WIN__ */
1633}
1634
1635/***********************************************************************/
1638UNIV_INTERN
1639ibool
1641/*=====================*/
1642 const char* name)
1643{
1644#ifdef __WIN__
1645 BOOL ret;
1646 ulint count = 0;
1647loop:
1648 /* In Windows, deleting an .ibd file may fail if ibbackup is copying
1649 it */
1650
1651 ret = DeleteFile((LPCTSTR)name);
1652
1653 if (ret) {
1654 return(TRUE);
1655 }
1656
1657 if (GetLastError() == ERROR_FILE_NOT_FOUND) {
1658 /* the file does not exist, this not an error */
1659
1660 return(TRUE);
1661 }
1662
1663 count++;
1664
1665 if (count > 100 && 0 == (count % 10)) {
1666 fprintf(stderr,
1667 "InnoDB: Warning: cannot delete file %s\n"
1668 "InnoDB: Are you running ibbackup"
1669 " to back up the file?\n", name);
1670
1671 os_file_get_last_error(TRUE); /* print error information */
1672 }
1673
1674 os_thread_sleep(1000000); /* sleep for a second */
1675
1676 if (count > 2000) {
1677
1678 return(FALSE);
1679 }
1680
1681 goto loop;
1682#else
1683 int ret;
1684
1685 ret = unlink(name);
1686
1687 if (ret != 0 && errno != ENOENT) {
1688 os_file_handle_error_no_exit(name, "delete");
1689
1690 return(FALSE);
1691 }
1692
1693 return(TRUE);
1694#endif
1695}
1696
1697/***********************************************************************/
1700UNIV_INTERN
1701ibool
1703/*===========*/
1704 const char* name)
1705{
1706#ifdef __WIN__
1707 BOOL ret;
1708 ulint count = 0;
1709loop:
1710 /* In Windows, deleting an .ibd file may fail if ibbackup is copying
1711 it */
1712
1713 ret = DeleteFile((LPCTSTR)name);
1714
1715 if (ret) {
1716 return(TRUE);
1717 }
1718
1719 if (GetLastError() == ERROR_FILE_NOT_FOUND) {
1720 /* If the file does not exist, we classify this as a 'mild'
1721 error and return */
1722
1723 return(FALSE);
1724 }
1725
1726 count++;
1727
1728 if (count > 100 && 0 == (count % 10)) {
1729 fprintf(stderr,
1730 "InnoDB: Warning: cannot delete file %s\n"
1731 "InnoDB: Are you running ibbackup"
1732 " to back up the file?\n", name);
1733
1734 os_file_get_last_error(TRUE); /* print error information */
1735 }
1736
1737 os_thread_sleep(1000000); /* sleep for a second */
1738
1739 if (count > 2000) {
1740
1741 return(FALSE);
1742 }
1743
1744 goto loop;
1745#else
1746 int ret;
1747
1748 ret = unlink(name);
1749
1750 if (ret != 0) {
1751 os_file_handle_error_no_exit(name, "delete");
1752
1753 return(FALSE);
1754 }
1755
1756 return(TRUE);
1757#endif
1758}
1759
1760/***********************************************************************/
1765UNIV_INTERN
1766ibool
1768/*================*/
1769 const char* oldpath,
1771 const char* newpath)
1772{
1773#ifdef __WIN__
1774 BOOL ret;
1775
1776 ret = MoveFile((LPCTSTR)oldpath, (LPCTSTR)newpath);
1777
1778 if (ret) {
1779 return(TRUE);
1780 }
1781
1782 os_file_handle_error_no_exit(oldpath, "rename");
1783
1784 return(FALSE);
1785#else
1786 int ret;
1787
1788 ret = rename(oldpath, newpath);
1789
1790 if (ret != 0) {
1791 os_file_handle_error_no_exit(oldpath, "rename");
1792
1793 return(FALSE);
1794 }
1795
1796 return(TRUE);
1797#endif
1798}
1799
1800/***********************************************************************/
1805UNIV_INTERN
1806ibool
1808/*===============*/
1809 os_file_t file)
1810{
1811#ifdef __WIN__
1812 BOOL ret;
1813
1814 ut_a(file);
1815
1816 ret = CloseHandle(file);
1817
1818 if (ret) {
1819 return(TRUE);
1820 }
1821
1822 os_file_handle_error(NULL, "close");
1823
1824 return(FALSE);
1825#else
1826 int ret;
1827
1828 ret = close(file);
1829
1830 if (ret == -1) {
1831 os_file_handle_error(NULL, "close");
1832
1833 return(FALSE);
1834 }
1835
1836 return(TRUE);
1837#endif
1838}
1839
1840#ifdef UNIV_HOTBACKUP
1841/***********************************************************************/
1844UNIV_INTERN
1845ibool
1846os_file_close_no_error_handling(
1847/*============================*/
1848 os_file_t file)
1849{
1850#ifdef __WIN__
1851 BOOL ret;
1852
1853 ut_a(file);
1854
1855 ret = CloseHandle(file);
1856
1857 if (ret) {
1858 return(TRUE);
1859 }
1860
1861 return(FALSE);
1862#else
1863 int ret;
1864
1865 ret = close(file);
1866
1867 if (ret == -1) {
1868
1869 return(FALSE);
1870 }
1871
1872 return(TRUE);
1873#endif
1874}
1875#endif /* UNIV_HOTBACKUP */
1876
1877/***********************************************************************/
1880UNIV_INTERN
1881ibool
1883/*=============*/
1884 os_file_t file,
1885 ulint* size,
1887 ulint* size_high)
1888{
1889#ifdef __WIN__
1890 DWORD high;
1891 DWORD low;
1892
1893 low = GetFileSize(file, &high);
1894
1895 if ((low == 0xFFFFFFFF) && (GetLastError() != NO_ERROR)) {
1896 return(FALSE);
1897 }
1898
1899 *size = low;
1900 *size_high = high;
1901
1902 return(TRUE);
1903#else
1904 off_t offs;
1905
1906 offs = lseek(file, 0, SEEK_END);
1907
1908 if (offs == ((off_t)-1)) {
1909
1910 return(FALSE);
1911 }
1912
1913 if (sizeof(off_t) > 4) {
1914 *size = (ulint)(offs & 0xFFFFFFFFUL);
1915 *size_high = (ulint)(offs >> 32);
1916 } else {
1917 *size = (ulint) offs;
1918 *size_high = 0;
1919 }
1920
1921 return(TRUE);
1922#endif
1923}
1924
1925/***********************************************************************/
1928UNIV_INTERN
1929ib_int64_t
1931/*===========================*/
1932 os_file_t file)
1933{
1934 ulint size;
1935 ulint size_high;
1936 ibool success;
1937
1938 success = os_file_get_size(file, &size, &size_high);
1939
1940 if (!success) {
1941
1942 return(-1);
1943 }
1944
1945 return((((ib_int64_t)size_high) << 32) + (ib_int64_t)size);
1946}
1947
1948/***********************************************************************/
1951UNIV_INTERN
1952ibool
1954/*=============*/
1955 const char* name,
1957 os_file_t file,
1958 ulint size,
1960 ulint size_high)
1961{
1962 ib_int64_t current_size;
1963 ib_int64_t desired_size;
1964 ibool ret;
1965 byte* buf;
1966 byte* buf2;
1967 ulint buf_size;
1968
1969 ut_a(size == (size & 0xFFFFFFFF));
1970
1971 current_size = 0;
1972 desired_size = (ib_int64_t)size + (((ib_int64_t)size_high) << 32);
1973
1974 /* Write up to 1 megabyte at a time. */
1975 buf_size = ut_min(64, (ulint) (desired_size / UNIV_PAGE_SIZE))
1976 * UNIV_PAGE_SIZE;
1977 buf2 = static_cast<unsigned char *>(ut_malloc(buf_size + UNIV_PAGE_SIZE));
1978
1979 /* Align the buffer for possible raw i/o */
1980 buf = static_cast<unsigned char *>(ut_align(buf2, UNIV_PAGE_SIZE));
1981
1982 /* Write buffer full of zeros */
1983 memset(buf, 0, buf_size);
1984
1985 if (desired_size >= (ib_int64_t)(100 * 1024 * 1024)) {
1986
1987 fprintf(stderr, "InnoDB: Progress in MB:");
1988 }
1989
1990 while (current_size < desired_size) {
1991 ulint n_bytes;
1992
1993 if (desired_size - current_size < (ib_int64_t) buf_size) {
1994 n_bytes = (ulint) (desired_size - current_size);
1995 } else {
1996 n_bytes = buf_size;
1997 }
1998
1999 ret = os_file_write(name, file, buf,
2000 (ulint)(current_size & 0xFFFFFFFF),
2001 (ulint)(current_size >> 32),
2002 n_bytes);
2003 if (!ret) {
2004 ut_free(buf2);
2005 goto error_handling;
2006 }
2007
2008 /* Print about progress for each 100 MB written */
2009 if ((ib_int64_t) (current_size + n_bytes) / (ib_int64_t)(100 * 1024 * 1024)
2010 != current_size / (ib_int64_t)(100 * 1024 * 1024)) {
2011
2012 fprintf(stderr, " %lu00",
2013 (ulong) ((current_size + n_bytes)
2014 / (ib_int64_t)(100 * 1024 * 1024)));
2015 }
2016
2017 current_size += n_bytes;
2018 }
2019
2020 if (desired_size >= (ib_int64_t)(100 * 1024 * 1024)) {
2021
2022 fprintf(stderr, "\n");
2023 }
2024
2025 ut_free(buf2);
2026
2027 ret = os_file_flush(file);
2028
2029 if (ret) {
2030 return(TRUE);
2031 }
2032
2033error_handling:
2034 return(FALSE);
2035}
2036
2037/***********************************************************************/
2040UNIV_INTERN
2041ibool
2043/*============*/
2044 FILE* file)
2045{
2046#ifdef __WIN__
2047 HANDLE h = (HANDLE) _get_osfhandle(fileno(file));
2048 return(SetEndOfFile(h));
2049#else /* __WIN__ */
2050 return(!ftruncate(fileno(file), ftell(file)));
2051#endif /* __WIN__ */
2052}
2053
2054#ifndef __WIN__
2055/***********************************************************************/
2060
2061static
2062int
2063os_file_fsync(
2064/*==========*/
2065 os_file_t file)
2066{
2067 int ret;
2068 int failures;
2069 ibool retry;
2070
2071 failures = 0;
2072
2073 do {
2074 ret = fsync(file);
2075
2076 os_n_fsyncs++;
2077
2078 if (ret == -1 && errno == ENOLCK) {
2079
2080 if (failures % 100 == 0) {
2081
2082 ut_print_timestamp(stderr);
2083 fprintf(stderr,
2084 " InnoDB: fsync(): "
2085 "No locks available; retrying\n");
2086 }
2087
2088 os_thread_sleep(200000 /* 0.2 sec */);
2089
2090 failures++;
2091
2092 retry = TRUE;
2093 } else {
2094
2095 retry = FALSE;
2096 }
2097 } while (retry);
2098
2099 return(ret);
2100}
2101#endif /* !__WIN__ */
2102
2103/***********************************************************************/
2107UNIV_INTERN
2108ibool
2110/*===============*/
2111 os_file_t file)
2112{
2113#ifdef __WIN__
2114 BOOL ret;
2115
2116 ut_a(file);
2117
2118 os_n_fsyncs++;
2119
2120 ret = FlushFileBuffers(file);
2121
2122 if (ret) {
2123 return(TRUE);
2124 }
2125
2126 /* Since Windows returns ERROR_INVALID_FUNCTION if the 'file' is
2127 actually a raw device, we choose to ignore that error if we are using
2128 raw disks */
2129
2130 if (srv_start_raw_disk_in_use && GetLastError()
2131 == ERROR_INVALID_FUNCTION) {
2132 return(TRUE);
2133 }
2134
2135 os_file_handle_error(NULL, "flush");
2136
2137 /* It is a fatal error if a file flush does not succeed, because then
2138 the database can get corrupt on disk */
2139 ut_error;
2140
2141 return(FALSE);
2142#else
2143 int ret;
2144
2145#if defined(HAVE_DARWIN_THREADS)
2146# ifndef F_FULLFSYNC
2147 /* The following definition is from the Mac OS X 10.3 <sys/fcntl.h> */
2148# define F_FULLFSYNC 51 /* fsync + ask the drive to flush to the media */
2149# elif F_FULLFSYNC != 51
2150# error "F_FULLFSYNC != 51: ABI incompatibility with Mac OS X 10.3"
2151# endif
2152 /* Apple has disabled fsync() for internal disk drives in OS X. That
2153 caused corruption for a user when he tested a power outage. Let us in
2154 OS X use a nonstandard flush method recommended by an Apple
2155 engineer. */
2156
2157 if (!srv_have_fullfsync) {
2158 /* If we are not on an operating system that supports this,
2159 then fall back to a plain fsync. */
2160
2161 ret = os_file_fsync(file);
2162 } else {
2163 ret = fcntl(file, F_FULLFSYNC, NULL);
2164
2165 if (ret) {
2166 /* If we are not on a file system that supports this,
2167 then fall back to a plain fsync. */
2168 ret = os_file_fsync(file);
2169 }
2170 }
2171#else
2172 ret = os_file_fsync(file);
2173#endif
2174
2175 if (ret == 0) {
2176 return(TRUE);
2177 }
2178
2179 /* Since Linux returns EINVAL if the 'file' is actually a raw device,
2180 we choose to ignore that error if we are using raw disks */
2181
2182 if (srv_start_raw_disk_in_use && errno == EINVAL) {
2183
2184 return(TRUE);
2185 }
2186
2187 ut_print_timestamp(stderr);
2188
2189 fprintf(stderr,
2190 " InnoDB: Error: the OS said file flush did not succeed\n");
2191
2192 os_file_handle_error(NULL, "flush");
2193
2194 /* It is a fatal error if a file flush does not succeed, because then
2195 the database can get corrupt on disk */
2196 ut_error;
2197
2198 return(FALSE);
2199#endif
2200}
2201
2202#ifndef __WIN__
2203/*******************************************************************/
2206static
2207ssize_t
2208os_file_pread(
2209/*==========*/
2210 os_file_t file,
2211 void* buf,
2212 ulint n,
2213 ulint offset,
2215 ulint offset_high)
2217{
2218 off_t offs;
2219#if defined(HAVE_PREAD) && !defined(HAVE_BROKEN_PREAD)
2220 ssize_t n_bytes;
2221#endif /* HAVE_PREAD && !HAVE_BROKEN_PREAD */
2222
2223 ut_a((offset & 0xFFFFFFFFUL) == offset);
2224
2225 /* If off_t is > 4 bytes in size, then we assume we can pass a
2226 64-bit address */
2227
2228 if (sizeof(off_t) > 4) {
2229 offs = (off_t)offset + (((off_t)offset_high) << 32);
2230
2231 } else {
2232 offs = (off_t)offset;
2233
2234 if (offset_high > 0) {
2235 fprintf(stderr,
2236 "InnoDB: Error: file read at offset > 4 GB\n");
2237 }
2238 }
2239
2240 os_n_file_reads++;
2241
2242#if defined(HAVE_PREAD) && !defined(HAVE_BROKEN_PREAD)
2243 os_mutex_enter(os_file_count_mutex);
2246 os_mutex_exit(os_file_count_mutex);
2247
2248 n_bytes = pread(file, buf, (ssize_t)n, offs);
2249
2250 os_mutex_enter(os_file_count_mutex);
2253 os_mutex_exit(os_file_count_mutex);
2254
2255 return(n_bytes);
2256#else
2257 {
2258 off_t ret_offset;
2259 ssize_t ret;
2260#ifndef UNIV_HOTBACKUP
2261 ulint i;
2262#endif /* !UNIV_HOTBACKUP */
2263
2264 os_mutex_enter(os_file_count_mutex);
2266 os_mutex_exit(os_file_count_mutex);
2267
2268#ifndef UNIV_HOTBACKUP
2269 /* Protect the seek / read operation with a mutex */
2270 i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES;
2271
2272 os_mutex_enter(os_file_seek_mutexes[i]);
2273#endif /* !UNIV_HOTBACKUP */
2274
2275 ret_offset = lseek(file, offs, SEEK_SET);
2276
2277 if (ret_offset < 0) {
2278 ret = -1;
2279 } else {
2280 ret = read(file, buf, (ssize_t)n);
2281 }
2282
2283#ifndef UNIV_HOTBACKUP
2284 os_mutex_exit(os_file_seek_mutexes[i]);
2285#endif /* !UNIV_HOTBACKUP */
2286
2287 os_mutex_enter(os_file_count_mutex);
2289 os_mutex_exit(os_file_count_mutex);
2290
2291 return(ret);
2292 }
2293#endif
2294}
2295
2296/*******************************************************************/
2299static
2300ssize_t
2301os_file_pwrite(
2302/*===========*/
2303 os_file_t file,
2304 const void* buf,
2305 ulint n,
2306 ulint offset,
2308 ulint offset_high)
2310{
2311 ssize_t ret;
2312 off_t offs;
2313
2314 ut_a((offset & 0xFFFFFFFFUL) == offset);
2315
2316 /* If off_t is > 4 bytes in size, then we assume we can pass a
2317 64-bit address */
2318
2319 if (sizeof(off_t) > 4) {
2320 offs = (off_t)offset + (((off_t)offset_high) << 32);
2321 } else {
2322 offs = (off_t)offset;
2323
2324 if (offset_high > 0) {
2325 fprintf(stderr,
2326 "InnoDB: Error: file write"
2327 " at offset > 4 GB\n");
2328 }
2329 }
2330
2331 if (srv_fake_write)
2332 return(TRUE);
2333
2334 os_n_file_writes++;
2335
2336#if defined(HAVE_PWRITE) && !defined(HAVE_BROKEN_PREAD)
2337 os_mutex_enter(os_file_count_mutex);
2340 os_mutex_exit(os_file_count_mutex);
2341
2342 ret = pwrite(file, buf, (ssize_t)n, offs);
2343
2344 os_mutex_enter(os_file_count_mutex);
2347 os_mutex_exit(os_file_count_mutex);
2348
2349# ifdef UNIV_DO_FLUSH
2350 if (srv_unix_file_flush_method != SRV_UNIX_LITTLESYNC
2351 && srv_unix_file_flush_method != SRV_UNIX_NOSYNC
2352 && !os_do_not_call_flush_at_each_write) {
2353
2354 /* Always do fsync to reduce the probability that when
2355 the OS crashes, a database page is only partially
2356 physically written to disk. */
2357
2358 ut_a(TRUE == os_file_flush(file));
2359 }
2360# endif /* UNIV_DO_FLUSH */
2361
2362 return(ret);
2363#else
2364 {
2365 off_t ret_offset;
2366# ifndef UNIV_HOTBACKUP
2367 ulint i;
2368# endif /* !UNIV_HOTBACKUP */
2369
2370 os_mutex_enter(os_file_count_mutex);
2372 os_mutex_exit(os_file_count_mutex);
2373
2374# ifndef UNIV_HOTBACKUP
2375 /* Protect the seek / write operation with a mutex */
2376 i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES;
2377
2378 os_mutex_enter(os_file_seek_mutexes[i]);
2379# endif /* UNIV_HOTBACKUP */
2380
2381 ret_offset = lseek(file, offs, SEEK_SET);
2382
2383 if (ret_offset < 0) {
2384 ret = -1;
2385
2386 goto func_exit;
2387 }
2388
2389 ret = write(file, buf, (ssize_t)n);
2390
2391# ifdef UNIV_DO_FLUSH
2392 if (srv_unix_file_flush_method != SRV_UNIX_LITTLESYNC
2393 && srv_unix_file_flush_method != SRV_UNIX_NOSYNC
2394 && !os_do_not_call_flush_at_each_write) {
2395
2396 /* Always do fsync to reduce the probability that when
2397 the OS crashes, a database page is only partially
2398 physically written to disk. */
2399
2400 ut_a(TRUE == os_file_flush(file));
2401 }
2402# endif /* UNIV_DO_FLUSH */
2403
2404func_exit:
2405# ifndef UNIV_HOTBACKUP
2406 os_mutex_exit(os_file_seek_mutexes[i]);
2407# endif /* !UNIV_HOTBACKUP */
2408
2409 os_mutex_enter(os_file_count_mutex);
2411 os_mutex_exit(os_file_count_mutex);
2412
2413 return(ret);
2414 }
2415#endif
2416}
2417#endif
2418
2419/*******************************************************************/
2424UNIV_INTERN
2425ibool
2427/*==============*/
2428 os_file_t file,
2429 void* buf,
2430 ulint offset,
2432 ulint offset_high,
2434 ulint n)
2435{
2436#ifdef __WIN__
2437 BOOL ret;
2438 DWORD len;
2439 DWORD ret2;
2440 DWORD low;
2441 DWORD high;
2442 ibool retry;
2443#ifndef UNIV_HOTBACKUP
2444 ulint i;
2445#endif /* !UNIV_HOTBACKUP */
2446
2447 /* On 64-bit Windows, ulint is 64 bits. But offset and n should be
2448 no more than 32 bits. */
2449 ut_a((offset & 0xFFFFFFFFUL) == offset);
2450 ut_a((n & 0xFFFFFFFFUL) == n);
2451
2452 os_n_file_reads++;
2453 os_bytes_read_since_printout += n;
2454
2455try_again:
2456 ut_ad(file);
2457 ut_ad(buf);
2458 ut_ad(n > 0);
2459
2460 low = (DWORD) offset;
2461 high = (DWORD) offset_high;
2462
2463 os_mutex_enter(os_file_count_mutex);
2465 os_mutex_exit(os_file_count_mutex);
2466
2467#ifndef UNIV_HOTBACKUP
2468 /* Protect the seek / read operation with a mutex */
2469 i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES;
2470
2471 os_mutex_enter(os_file_seek_mutexes[i]);
2472#endif /* !UNIV_HOTBACKUP */
2473
2474 ret2 = SetFilePointer(file, low, &high, FILE_BEGIN);
2475
2476 if (ret2 == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
2477
2478#ifndef UNIV_HOTBACKUP
2479 os_mutex_exit(os_file_seek_mutexes[i]);
2480#endif /* !UNIV_HOTBACKUP */
2481
2482 os_mutex_enter(os_file_count_mutex);
2484 os_mutex_exit(os_file_count_mutex);
2485
2486 goto error_handling;
2487 }
2488
2489 ret = ReadFile(file, buf, (DWORD) n, &len, NULL);
2490
2491#ifndef UNIV_HOTBACKUP
2492 os_mutex_exit(os_file_seek_mutexes[i]);
2493#endif /* !UNIV_HOTBACKUP */
2494
2495 os_mutex_enter(os_file_count_mutex);
2497 os_mutex_exit(os_file_count_mutex);
2498
2499 if (ret && len == n) {
2500 return(TRUE);
2501 }
2502#else /* __WIN__ */
2503 ibool retry;
2504 ssize_t ret;
2505
2506 os_bytes_read_since_printout += n;
2507
2508try_again:
2509 ret = os_file_pread(file, buf, n, offset, offset_high);
2510
2511 if ((ulint)ret == n) {
2512
2513 return(TRUE);
2514 }
2515
2516 fprintf(stderr,
2517 "InnoDB: Error: tried to read %lu bytes at offset %lu %lu.\n"
2518 "InnoDB: Was only able to read %ld.\n",
2519 (ulong)n, (ulong)offset_high,
2520 (ulong)offset, (long)ret);
2521#endif /* __WIN__ */
2522#ifdef __WIN__
2523error_handling:
2524#endif
2525 retry = os_file_handle_error(NULL, "read");
2526
2527 if (retry) {
2528 goto try_again;
2529 }
2530
2531 fprintf(stderr,
2532 "InnoDB: Fatal error: cannot read from file."
2533 " OS error number %lu.\n",
2534#ifdef __WIN__
2535 (ulong) GetLastError()
2536#else
2537 (ulong) errno
2538#endif
2539 );
2540 fflush(stderr);
2541
2542 ut_error;
2543
2544 return(FALSE);
2545}
2546
2547/*******************************************************************/
2553UNIV_INTERN
2554ibool
2556/*================================*/
2557 os_file_t file,
2558 void* buf,
2559 ulint offset,
2561 ulint offset_high,
2563 ulint n)
2564{
2565#ifdef __WIN__
2566 BOOL ret;
2567 DWORD len;
2568 DWORD ret2;
2569 DWORD low;
2570 DWORD high;
2571 ibool retry;
2572#ifndef UNIV_HOTBACKUP
2573 ulint i;
2574#endif /* !UNIV_HOTBACKUP */
2575
2576 /* On 64-bit Windows, ulint is 64 bits. But offset and n should be
2577 no more than 32 bits. */
2578 ut_a((offset & 0xFFFFFFFFUL) == offset);
2579 ut_a((n & 0xFFFFFFFFUL) == n);
2580
2581 os_n_file_reads++;
2582 os_bytes_read_since_printout += n;
2583
2584try_again:
2585 ut_ad(file);
2586 ut_ad(buf);
2587 ut_ad(n > 0);
2588
2589 low = (DWORD) offset;
2590 high = (DWORD) offset_high;
2591
2592 os_mutex_enter(os_file_count_mutex);
2594 os_mutex_exit(os_file_count_mutex);
2595
2596#ifndef UNIV_HOTBACKUP
2597 /* Protect the seek / read operation with a mutex */
2598 i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES;
2599
2600 os_mutex_enter(os_file_seek_mutexes[i]);
2601#endif /* !UNIV_HOTBACKUP */
2602
2603 ret2 = SetFilePointer(file, low, &high, FILE_BEGIN);
2604
2605 if (ret2 == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
2606
2607#ifndef UNIV_HOTBACKUP
2608 os_mutex_exit(os_file_seek_mutexes[i]);
2609#endif /* !UNIV_HOTBACKUP */
2610
2611 os_mutex_enter(os_file_count_mutex);
2613 os_mutex_exit(os_file_count_mutex);
2614
2615 goto error_handling;
2616 }
2617
2618 ret = ReadFile(file, buf, (DWORD) n, &len, NULL);
2619
2620#ifndef UNIV_HOTBACKUP
2621 os_mutex_exit(os_file_seek_mutexes[i]);
2622#endif /* !UNIV_HOTBACKUP */
2623
2624 os_mutex_enter(os_file_count_mutex);
2626 os_mutex_exit(os_file_count_mutex);
2627
2628 if (ret && len == n) {
2629 return(TRUE);
2630 }
2631#else /* __WIN__ */
2632 ibool retry;
2633 ssize_t ret;
2634
2635 os_bytes_read_since_printout += n;
2636
2637try_again:
2638 ret = os_file_pread(file, buf, n, offset, offset_high);
2639
2640 if ((ulint)ret == n) {
2641
2642 return(TRUE);
2643 }
2644#endif /* __WIN__ */
2645#ifdef __WIN__
2646error_handling:
2647#endif
2648 retry = os_file_handle_error_no_exit(NULL, "read");
2649
2650 if (retry) {
2651 goto try_again;
2652 }
2653
2654 return(FALSE);
2655}
2656
2657/*******************************************************************/
2661UNIV_INTERN
2662void
2664/*================*/
2665 FILE* file,
2666 char* str,
2667 ulint size)
2668{
2669 size_t flen;
2670
2671 if (size == 0) {
2672 return;
2673 }
2674
2675 rewind(file);
2676 flen = fread(str, 1, size - 1, file);
2677 str[flen] = '\0';
2678}
2679
2680/*******************************************************************/
2685UNIV_INTERN
2686ibool
2688/*===============*/
2689 const char* name,
2691 os_file_t file,
2692 const void* buf,
2693 ulint offset,
2695 ulint offset_high,
2697 ulint n)
2698{
2699#ifdef __WIN__
2700 BOOL ret;
2701 DWORD len;
2702 DWORD ret2;
2703 DWORD low;
2704 DWORD high;
2705 ulint n_retries = 0;
2706 ulint err;
2707#ifndef UNIV_HOTBACKUP
2708 ulint i;
2709#endif /* !UNIV_HOTBACKUP */
2710
2711 /* On 64-bit Windows, ulint is 64 bits. But offset and n should be
2712 no more than 32 bits. */
2713 ut_a((offset & 0xFFFFFFFFUL) == offset);
2714 ut_a((n & 0xFFFFFFFFUL) == n);
2715
2716 if (srv_fake_write)
2717 return(TRUE);
2718
2719 os_n_file_writes++;
2720
2721 ut_ad(file);
2722 ut_ad(buf);
2723 ut_ad(n > 0);
2724retry:
2725 low = (DWORD) offset;
2726 high = (DWORD) offset_high;
2727
2728 os_mutex_enter(os_file_count_mutex);
2730 os_mutex_exit(os_file_count_mutex);
2731
2732#ifndef UNIV_HOTBACKUP
2733 /* Protect the seek / write operation with a mutex */
2734 i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES;
2735
2736 os_mutex_enter(os_file_seek_mutexes[i]);
2737#endif /* !UNIV_HOTBACKUP */
2738
2739 ret2 = SetFilePointer(file, low, &high, FILE_BEGIN);
2740
2741 if (ret2 == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
2742
2743#ifndef UNIV_HOTBACKUP
2744 os_mutex_exit(os_file_seek_mutexes[i]);
2745#endif /* !UNIV_HOTBACKUP */
2746
2747 os_mutex_enter(os_file_count_mutex);
2749 os_mutex_exit(os_file_count_mutex);
2750
2751 ut_print_timestamp(stderr);
2752
2753 fprintf(stderr,
2754 " InnoDB: Error: File pointer positioning to"
2755 " file %s failed at\n"
2756 "InnoDB: offset %lu %lu. Operating system"
2757 " error number %lu.\n"
2758 "InnoDB: Some operating system error numbers"
2759 " are described at\n"
2760 "InnoDB: "
2761 REFMAN "operating-system-error-codes.html\n",
2762 name, (ulong) offset_high, (ulong) offset,
2763 (ulong) GetLastError());
2764
2765 return(FALSE);
2766 }
2767
2768 ret = WriteFile(file, buf, (DWORD) n, &len, NULL);
2769
2770 /* Always do fsync to reduce the probability that when the OS crashes,
2771 a database page is only partially physically written to disk. */
2772
2773# ifdef UNIV_DO_FLUSH
2774 if (!os_do_not_call_flush_at_each_write) {
2775 ut_a(TRUE == os_file_flush(file));
2776 }
2777# endif /* UNIV_DO_FLUSH */
2778
2779#ifndef UNIV_HOTBACKUP
2780 os_mutex_exit(os_file_seek_mutexes[i]);
2781#endif /* !UNIV_HOTBACKUP */
2782
2783 os_mutex_enter(os_file_count_mutex);
2785 os_mutex_exit(os_file_count_mutex);
2786
2787 if (ret && len == n) {
2788
2789 return(TRUE);
2790 }
2791
2792 /* If some background file system backup tool is running, then, at
2793 least in Windows 2000, we may get here a specific error. Let us
2794 retry the operation 100 times, with 1 second waits. */
2795
2796 if (GetLastError() == ERROR_LOCK_VIOLATION && n_retries < 100) {
2797
2798 os_thread_sleep(1000000);
2799
2800 n_retries++;
2801
2802 goto retry;
2803 }
2804
2805 if (!os_has_said_disk_full) {
2806
2807 err = (ulint)GetLastError();
2808
2809 ut_print_timestamp(stderr);
2810
2811 fprintf(stderr,
2812 " InnoDB: Error: Write to file %s failed"
2813 " at offset %lu %lu.\n"
2814 "InnoDB: %lu bytes should have been written,"
2815 " only %lu were written.\n"
2816 "InnoDB: Operating system error number %lu.\n"
2817 "InnoDB: Check that your OS and file system"
2818 " support files of this size.\n"
2819 "InnoDB: Check also that the disk is not full"
2820 " or a disk quota exceeded.\n",
2821 name, (ulong) offset_high, (ulong) offset,
2822 (ulong) n, (ulong) len, (ulong) err);
2823
2824 if (strerror((int)err) != NULL) {
2825 fprintf(stderr,
2826 "InnoDB: Error number %lu means '%s'.\n",
2827 (ulong) err, strerror((int)err));
2828 }
2829
2830 fprintf(stderr,
2831 "InnoDB: Some operating system error numbers"
2832 " are described at\n"
2833 "InnoDB: "
2834 REFMAN "operating-system-error-codes.html\n");
2835
2836 os_has_said_disk_full = TRUE;
2837 }
2838
2839 return(FALSE);
2840#else
2841 ssize_t ret;
2842
2843 ret = os_file_pwrite(file, buf, n, offset, offset_high);
2844
2845 if ((ulint)ret == n) {
2846
2847 return(TRUE);
2848 }
2849
2850 if (!os_has_said_disk_full) {
2851
2852 ut_print_timestamp(stderr);
2853
2854 fprintf(stderr,
2855 " InnoDB: Error: Write to file %s failed"
2856 " at offset %lu %lu.\n"
2857 "InnoDB: %lu bytes should have been written,"
2858 " only %ld were written.\n"
2859 "InnoDB: Operating system error number %lu.\n"
2860 "InnoDB: Check that your OS and file system"
2861 " support files of this size.\n"
2862 "InnoDB: Check also that the disk is not full"
2863 " or a disk quota exceeded.\n",
2864 name, offset_high, offset, n, (long int)ret,
2865 (ulint)errno);
2866 if (strerror(errno) != NULL) {
2867 fprintf(stderr,
2868 "InnoDB: Error number %lu means '%s'.\n",
2869 (ulint)errno, strerror(errno));
2870 }
2871
2872 fprintf(stderr,
2873 "InnoDB: Some operating system error numbers"
2874 " are described at\n"
2875 "InnoDB: "
2876 REFMAN "operating-system-error-codes.html\n");
2877
2878 os_has_said_disk_full = TRUE;
2879 }
2880
2881 return(FALSE);
2882#endif
2883}
2884
2885/*******************************************************************/
2888UNIV_INTERN
2889ibool
2891/*===========*/
2892 const char* path,
2893 ibool* exists,
2894 os_file_type_t* type)
2895{
2896#ifdef __WIN__
2897 int ret;
2898 struct _stat statinfo;
2899
2900 ret = _stat(path, &statinfo);
2901 if (ret && (errno == ENOENT || errno == ENOTDIR)) {
2902 /* file does not exist */
2903 *exists = FALSE;
2904 return(TRUE);
2905 } else if (ret) {
2906 /* file exists, but stat call failed */
2907
2908 os_file_handle_error_no_exit(path, "stat");
2909
2910 return(FALSE);
2911 }
2912
2913 if (_S_IFDIR & statinfo.st_mode) {
2914 *type = OS_FILE_TYPE_DIR;
2915 } else if (_S_IFREG & statinfo.st_mode) {
2916 *type = OS_FILE_TYPE_FILE;
2917 } else {
2918 *type = OS_FILE_TYPE_UNKNOWN;
2919 }
2920
2921 *exists = TRUE;
2922
2923 return(TRUE);
2924#else
2925 int ret;
2926 struct stat statinfo;
2927
2928 ret = stat(path, &statinfo);
2929 if (ret && (errno == ENOENT || errno == ENOTDIR)) {
2930 /* file does not exist */
2931 *exists = FALSE;
2932 return(TRUE);
2933 } else if (ret) {
2934 /* file exists, but stat call failed */
2935
2936 os_file_handle_error_no_exit(path, "stat");
2937
2938 return(FALSE);
2939 }
2940
2941 if (S_ISDIR(statinfo.st_mode)) {
2942 *type = OS_FILE_TYPE_DIR;
2943 } else if (S_ISLNK(statinfo.st_mode)) {
2944 *type = OS_FILE_TYPE_LINK;
2945 } else if (S_ISREG(statinfo.st_mode)) {
2946 *type = OS_FILE_TYPE_FILE;
2947 } else {
2948 *type = OS_FILE_TYPE_UNKNOWN;
2949 }
2950
2951 *exists = TRUE;
2952
2953 return(TRUE);
2954#endif
2955}
2956
2957/*******************************************************************/
2960UNIV_INTERN
2961ibool
2963/*===============*/
2964 const char* path,
2965 os_file_stat_t* stat_info)
2967{
2968#ifdef __WIN__
2969 int ret;
2970 struct _stat statinfo;
2971
2972 ret = _stat(path, &statinfo);
2973 if (ret && (errno == ENOENT || errno == ENOTDIR)) {
2974 /* file does not exist */
2975
2976 return(FALSE);
2977 } else if (ret) {
2978 /* file exists, but stat call failed */
2979
2980 os_file_handle_error_no_exit(path, "stat");
2981
2982 return(FALSE);
2983 }
2984 if (_S_IFDIR & statinfo.st_mode) {
2985 stat_info->type = OS_FILE_TYPE_DIR;
2986 } else if (_S_IFREG & statinfo.st_mode) {
2987 stat_info->type = OS_FILE_TYPE_FILE;
2988 } else {
2989 stat_info->type = OS_FILE_TYPE_UNKNOWN;
2990 }
2991
2992 stat_info->ctime = statinfo.st_ctime;
2993 stat_info->atime = statinfo.st_atime;
2994 stat_info->mtime = statinfo.st_mtime;
2995 stat_info->size = statinfo.st_size;
2996
2997 return(TRUE);
2998#else
2999 int ret;
3000 struct stat statinfo;
3001
3002 ret = stat(path, &statinfo);
3003
3004 if (ret && (errno == ENOENT || errno == ENOTDIR)) {
3005 /* file does not exist */
3006
3007 return(FALSE);
3008 } else if (ret) {
3009 /* file exists, but stat call failed */
3010
3011 os_file_handle_error_no_exit(path, "stat");
3012
3013 return(FALSE);
3014 }
3015
3016 if (S_ISDIR(statinfo.st_mode)) {
3017 stat_info->type = OS_FILE_TYPE_DIR;
3018 } else if (S_ISLNK(statinfo.st_mode)) {
3019 stat_info->type = OS_FILE_TYPE_LINK;
3020 } else if (S_ISREG(statinfo.st_mode)) {
3021 stat_info->type = OS_FILE_TYPE_FILE;
3022 } else {
3023 stat_info->type = OS_FILE_TYPE_UNKNOWN;
3024 }
3025
3026 stat_info->ctime = statinfo.st_ctime;
3027 stat_info->atime = statinfo.st_atime;
3028 stat_info->mtime = statinfo.st_mtime;
3029 stat_info->size = statinfo.st_size;
3030
3031 return(TRUE);
3032#endif
3033}
3034
3035/* path name separator character */
3036#ifdef __WIN__
3037# define OS_FILE_PATH_SEPARATOR '\\'
3038#else
3039# define OS_FILE_PATH_SEPARATOR '/'
3040#endif
3041
3042/****************************************************************/
3070UNIV_INTERN
3071char*
3073/*============*/
3074 const char* path)
3075{
3076 /* Find the offset of the last slash */
3077 const char* last_slash = strrchr(path, OS_FILE_PATH_SEPARATOR);
3078 if (!last_slash) {
3079 /* No slash in the path, return "." */
3080
3081 return(mem_strdup("."));
3082 }
3083
3084 /* Ok, there is a slash */
3085
3086 if (last_slash == path) {
3087 /* last slash is the first char of the path */
3088
3089 return(mem_strdup("/"));
3090 }
3091
3092 /* Non-trivial directory component */
3093
3094 return(mem_strdupl(path, last_slash - path));
3095}
3096
3097/****************************************************************/
3100UNIV_INTERN
3101ibool
3103/*=============================*/
3104 const char* path)
3105{
3106 char* subdir;
3107 ibool success, subdir_exists;
3108 os_file_type_t type;
3109
3110 subdir = os_file_dirname(path);
3111 if (strlen(subdir) == 1
3112 && (*subdir == OS_FILE_PATH_SEPARATOR || *subdir == '.')) {
3113 /* subdir is root or cwd, nothing to do */
3114 mem_free(subdir);
3115
3116 return(TRUE);
3117 }
3118
3119 /* Test if subdir exists */
3120 success = os_file_status(subdir, &subdir_exists, &type);
3121 if (success && !subdir_exists) {
3122 /* subdir does not exist, create it */
3123 success = os_file_create_subdirs_if_needed(subdir);
3124 if (!success) {
3125 mem_free(subdir);
3126
3127 return(FALSE);
3128 }
3129 success = os_file_create_directory(subdir, FALSE);
3130 }
3131
3132 mem_free(subdir);
3133
3134 return(success);
3135}
3136
3137#ifndef UNIV_HOTBACKUP
3138/****************************************************************/
3141static
3142os_aio_slot_t*
3143os_aio_array_get_nth_slot(
3144/*======================*/
3145 os_aio_array_t* array,
3146 ulint index)
3147{
3148 ut_a(index < array->n_slots);
3149
3150 return((array->slots) + index);
3151}
3152
3153#if defined(LINUX_NATIVE_AIO)
3154/******************************************************************/
3157static
3158ibool
3159os_aio_linux_create_io_ctx(
3160/*=======================*/
3161 ulint max_events,
3162 io_context_t* io_ctx)
3163{
3164 int ret;
3165 ulint retries = 0;
3166
3167retry:
3168 memset(io_ctx, 0x0, sizeof(*io_ctx));
3169
3170 /* Initialize the io_ctx. Tell it how many pending
3171 IO requests this context will handle. */
3172
3173 ret = io_setup(max_events, io_ctx);
3174 if (ret == 0) {
3175#if defined(UNIV_AIO_DEBUG)
3176 fprintf(stderr,
3177 "InnoDB: Linux native AIO:"
3178 " initialized io_ctx for segment\n");
3179#endif
3180 /* Success. Return now. */
3181 return(TRUE);
3182 }
3183
3184 /* If we hit EAGAIN we'll make a few attempts before failing. */
3185
3186 switch (ret) {
3187 case -EAGAIN:
3188 if (retries == 0) {
3189 /* First time around. */
3190 ut_print_timestamp(stderr);
3191 fprintf(stderr,
3192 " InnoDB: Warning: io_setup() failed"
3193 " with EAGAIN. Will make %d attempts"
3194 " before giving up.\n",
3195 OS_AIO_IO_SETUP_RETRY_ATTEMPTS);
3196 }
3197
3198 if (retries < OS_AIO_IO_SETUP_RETRY_ATTEMPTS) {
3199 ++retries;
3200 fprintf(stderr,
3201 "InnoDB: Warning: io_setup() attempt"
3202 " %lu failed.\n",
3203 retries);
3204 os_thread_sleep(OS_AIO_IO_SETUP_RETRY_SLEEP);
3205 goto retry;
3206 }
3207
3208 /* Have tried enough. Better call it a day. */
3209 ut_print_timestamp(stderr);
3210 fprintf(stderr,
3211 " InnoDB: Error: io_setup() failed"
3212 " with EAGAIN after %d attempts.\n",
3213 OS_AIO_IO_SETUP_RETRY_ATTEMPTS);
3214 break;
3215
3216 case -ENOSYS:
3217 ut_print_timestamp(stderr);
3218 fprintf(stderr,
3219 " InnoDB: Error: Linux Native AIO interface"
3220 " is not supported on this platform. Please"
3221 " check your OS documentation and install"
3222 " appropriate binary of InnoDB.\n");
3223
3224 break;
3225
3226 default:
3227 ut_print_timestamp(stderr);
3228 fprintf(stderr,
3229 " InnoDB: Error: Linux Native AIO setup"
3230 " returned following error[%d]\n", -ret);
3231 break;
3232 }
3233
3234 fprintf(stderr,
3235 "InnoDB: You can disable Linux Native AIO by"
3236 " setting innodb_native_aio = off in my.cnf\n");
3237 return(FALSE);
3238}
3239#endif /* LINUX_NATIVE_AIO */
3240
3241/******************************************************************/
3246static
3247os_aio_array_t*
3248os_aio_array_create(
3249/*================*/
3250 ulint n,
3253 ulint n_segments)
3254{
3255 os_aio_array_t* array;
3256 ulint i;
3257 os_aio_slot_t* slot;
3258#ifdef WIN_ASYNC_IO
3259 OVERLAPPED* over;
3260#elif defined(LINUX_NATIVE_AIO)
3261 struct io_event* aio_event = NULL;
3262#endif
3263 ut_a(n > 0);
3264 ut_a(n_segments > 0);
3265
3266 array = static_cast<os_aio_array_t *>(ut_malloc(sizeof(os_aio_array_t)));
3267
3268 array->mutex = os_mutex_create();
3269 array->not_full = os_event_create(NULL);
3270 array->is_empty = os_event_create(NULL);
3271
3272 os_event_set(array->is_empty);
3273
3274 array->n_slots = n;
3275 array->n_segments = n_segments;
3276 array->n_reserved = 0;
3277 array->cur_seg = 0;
3278 array->slots = static_cast<os_aio_slot_t *>(ut_malloc(n * sizeof(os_aio_slot_t)));
3279#ifdef __WIN__
3280 array->handles = ut_malloc(n * sizeof(HANDLE));
3281#endif
3282
3283#if defined(LINUX_NATIVE_AIO)
3284 array->aio_ctx = NULL;
3285 array->aio_events = NULL;
3286
3287 /* If we are not using native aio interface then skip this
3288 part of initialization. */
3289 if (!srv_use_native_aio) {
3290 goto skip_native_aio;
3291 }
3292
3293 /* Initialize the io_context array. One io_context
3294 per segment in the array. */
3295
3296 array->aio_ctx = (io_context**) ut_malloc(n_segments *
3297 sizeof(*array->aio_ctx));
3298 for (i = 0; i < n_segments; ++i) {
3299 if (!os_aio_linux_create_io_ctx(n/n_segments,
3300 &array->aio_ctx[i])) {
3301 /* If something bad happened during aio setup
3302 we should call it a day and return right away.
3303 We don't care about any leaks because a failure
3304 to initialize the io subsystem means that the
3305 server (or atleast the innodb storage engine)
3306 is not going to startup. */
3307 return(NULL);
3308 }
3309 }
3310
3311 /* Initialize the event array. One event per slot. */
3312 aio_event = (io_event*) ut_malloc(n * sizeof(io_event));
3313 memset(aio_event, 0x0, sizeof(io_event) * n);
3314 array->aio_events = aio_event;
3315
3316skip_native_aio:
3317#endif /* LINUX_NATIVE_AIO */
3318 for (i = 0; i < n; i++) {
3319 slot = os_aio_array_get_nth_slot(array, i);
3320
3321 slot->pos = i;
3322 slot->reserved = FALSE;
3323#ifdef WIN_ASYNC_IO
3324 slot->handle = CreateEvent(NULL,TRUE, FALSE, NULL);
3325
3326 over = &(slot->control);
3327
3328 over->hEvent = slot->handle;
3329
3330 *((array->handles) + i) = over->hEvent;
3331
3332#elif defined(LINUX_NATIVE_AIO)
3333
3334 memset(&slot->control, 0x0, sizeof(slot->control));
3335 slot->n_bytes = 0;
3336 slot->ret = 0;
3337#endif
3338 }
3339
3340 return(array);
3341}
3342
3343/************************************************************************/
3345static
3346void
3347os_aio_array_free(
3348/*==============*/
3349 os_aio_array_t* array)
3350{
3351#ifdef WIN_ASYNC_IO
3352 ulint i;
3353
3354 for (i = 0; i < array->n_slots; i++) {
3355 os_aio_slot_t* slot = os_aio_array_get_nth_slot(array, i);
3356 CloseHandle(slot->handle);
3357 }
3358#endif /* WIN_ASYNC_IO */
3359
3360#ifdef __WIN__
3361 ut_free(array->handles);
3362#endif /* __WIN__ */
3363 os_mutex_free(array->mutex);
3364 os_event_free(array->not_full);
3365 os_event_free(array->is_empty);
3366
3367#if defined(LINUX_NATIVE_AIO)
3368 if (srv_use_native_aio) {
3369 ut_free(array->aio_events);
3370 ut_free(array->aio_ctx);
3371 }
3372#endif /* LINUX_NATIVE_AIO */
3373
3374 ut_free(array->slots);
3375 ut_free(array);
3376}
3377
3378/***********************************************************************
3379Initializes the asynchronous io system. Creates one array each for ibuf
3380and log i/o. Also creates one array each for read and write where each
3381array is divided logically into n_read_segs and n_write_segs
3382respectively. The caller must create an i/o handler thread for each
3383segment in these arrays. This function also creates the sync array.
3384No i/o handler thread needs to be created for that */
3385UNIV_INTERN
3386ibool
3387os_aio_init(
3388/*========*/
3389 ulint n_per_seg, /*<! in: maximum number of pending aio
3390 operations allowed per segment */
3391 ulint n_read_segs, /*<! in: number of reader threads */
3392 ulint n_write_segs, /*<! in: number of writer threads */
3393 ulint n_slots_sync) /*<! in: number of slots in the sync aio
3394 array */
3395{
3396 ulint i;
3397 ulint n_segments = 2 + n_read_segs + n_write_segs;
3398
3399 ut_ad(n_segments >= 4);
3400
3402
3403 for (i = 0; i < n_segments; i++) {
3404 srv_set_io_thread_op_info(i, "not started yet");
3405 }
3406
3407
3408 /* fprintf(stderr, "Array n per seg %lu\n", n_per_seg); */
3409
3410 os_aio_ibuf_array = os_aio_array_create(n_per_seg, 1);
3411 if (os_aio_ibuf_array == NULL) {
3412 goto err_exit;
3413 }
3414
3415 srv_io_thread_function[0] = "insert buffer thread";
3416
3417 os_aio_log_array = os_aio_array_create(n_per_seg, 1);
3418 if (os_aio_log_array == NULL) {
3419 goto err_exit;
3420 }
3421
3422 srv_io_thread_function[1] = "log thread";
3423
3424 os_aio_read_array = os_aio_array_create(n_read_segs * n_per_seg,
3425 n_read_segs);
3426 if (os_aio_read_array == NULL) {
3427 goto err_exit;
3428 }
3429
3430 for (i = 2; i < 2 + n_read_segs; i++) {
3431 ut_a(i < SRV_MAX_N_IO_THREADS);
3432 srv_io_thread_function[i] = "read thread";
3433 }
3434
3435 os_aio_write_array = os_aio_array_create(n_write_segs * n_per_seg,
3436 n_write_segs);
3437 if (os_aio_write_array == NULL) {
3438 goto err_exit;
3439 }
3440
3441 for (i = 2 + n_read_segs; i < n_segments; i++) {
3442 ut_a(i < SRV_MAX_N_IO_THREADS);
3443 srv_io_thread_function[i] = "write thread";
3444 }
3445
3446 os_aio_sync_array = os_aio_array_create(n_slots_sync, 1);
3447 if (os_aio_sync_array == NULL) {
3448 goto err_exit;
3449 }
3450
3451
3452 os_aio_n_segments = n_segments;
3453
3455
3456 os_aio_segment_wait_events = static_cast<os_event_t *>(ut_malloc(n_segments * sizeof(void*)));
3457
3458 for (i = 0; i < n_segments; i++) {
3459 os_aio_segment_wait_events[i] = os_event_create(NULL);
3460 }
3461
3462 os_last_printout = time(NULL);
3463
3464 return(TRUE);
3465
3466err_exit:
3467 return(FALSE);
3468
3469}
3470
3471/***********************************************************************
3472Frees the asynchronous io system. */
3473UNIV_INTERN
3474void
3475os_aio_free(void)
3476/*=============*/
3477{
3478 ulint i;
3479
3480 os_aio_array_free(os_aio_ibuf_array);
3481 os_aio_ibuf_array = NULL;
3482 os_aio_array_free(os_aio_log_array);
3483 os_aio_log_array = NULL;
3484 os_aio_array_free(os_aio_read_array);
3485 os_aio_read_array = NULL;
3486 os_aio_array_free(os_aio_write_array);
3487 os_aio_write_array = NULL;
3488 os_aio_array_free(os_aio_sync_array);
3489 os_aio_sync_array = NULL;
3490
3491 for (i = 0; i < os_aio_n_segments; i++) {
3492 os_event_free(os_aio_segment_wait_events[i]);
3493 }
3494
3495 ut_free(os_aio_segment_wait_events);
3496 os_aio_segment_wait_events = 0;
3497 os_aio_n_segments = 0;
3498}
3499
3500#ifdef WIN_ASYNC_IO
3501/************************************************************************/
3504static
3505void
3506os_aio_array_wake_win_aio_at_shutdown(
3507/*==================================*/
3508 os_aio_array_t* array)
3509{
3510 ulint i;
3511
3512 for (i = 0; i < array->n_slots; i++) {
3513
3514 SetEvent((array->slots + i)->handle);
3515 }
3516}
3517#endif
3518
3519/************************************************************************/
3522UNIV_INTERN
3523void
3525/*=====================================*/
3526{
3527 ulint i;
3528
3529#ifdef WIN_ASYNC_IO
3530 /* This code wakes up all ai/o threads in Windows native aio */
3531 os_aio_array_wake_win_aio_at_shutdown(os_aio_read_array);
3532 os_aio_array_wake_win_aio_at_shutdown(os_aio_write_array);
3533 os_aio_array_wake_win_aio_at_shutdown(os_aio_ibuf_array);
3534 os_aio_array_wake_win_aio_at_shutdown(os_aio_log_array);
3535
3536#elif defined(LINUX_NATIVE_AIO)
3537
3538 /* When using native AIO interface the io helper threads
3539 wait on io_getevents with a timeout value of 500ms. At
3540 each wake up these threads check the server status.
3541 No need to do anything to wake them up. */
3542
3543 if (srv_use_native_aio) {
3544 return;
3545 }
3546 /* Fall through to simulated AIO handler wakeup if we are
3547 not using native AIO. */
3548#endif
3549 /* This loop wakes up all simulated ai/o threads */
3550
3551 for (i = 0; i < os_aio_n_segments; i++) {
3552
3553 os_event_set(os_aio_segment_wait_events[i]);
3554 }
3555}
3556
3557/************************************************************************/
3560UNIV_INTERN
3561void
3563/*=====================================*/
3564{
3565 os_event_wait(os_aio_write_array->is_empty);
3566}
3567
3568/**********************************************************************/
3572static
3573ulint
3574os_aio_get_segment_no_from_slot(
3575/*============================*/
3576 os_aio_array_t* array,
3577 os_aio_slot_t* slot)
3578{
3579 ulint segment;
3580 ulint seg_len;
3581
3582 if (array == os_aio_ibuf_array) {
3583 segment = 0;
3584
3585 } else if (array == os_aio_log_array) {
3586 segment = 1;
3587
3588 } else if (array == os_aio_read_array) {
3589 seg_len = os_aio_read_array->n_slots
3590 / os_aio_read_array->n_segments;
3591
3592 segment = 2 + slot->pos / seg_len;
3593 } else {
3594 ut_a(array == os_aio_write_array);
3595 seg_len = os_aio_write_array->n_slots
3596 / os_aio_write_array->n_segments;
3597
3598 segment = os_aio_read_array->n_segments + 2
3599 + slot->pos / seg_len;
3600 }
3601
3602 return(segment);
3603}
3604
3605/**********************************************************************/
3608static
3609ulint
3610os_aio_get_array_and_local_segment(
3611/*===============================*/
3612 os_aio_array_t** array,
3613 ulint global_segment)
3614{
3615 ulint segment;
3616
3617 ut_a(global_segment < os_aio_n_segments);
3618
3619 if (global_segment == 0) {
3620 *array = os_aio_ibuf_array;
3621 segment = 0;
3622
3623 } else if (global_segment == 1) {
3624 *array = os_aio_log_array;
3625 segment = 0;
3626
3627 } else if (global_segment < os_aio_read_array->n_segments + 2) {
3628 *array = os_aio_read_array;
3629
3630 segment = global_segment - 2;
3631 } else {
3632 *array = os_aio_write_array;
3633
3634 segment = global_segment - (os_aio_read_array->n_segments + 2);
3635 }
3636
3637 return(segment);
3638}
3639
3640/*******************************************************************/
3644static
3645os_aio_slot_t*
3646os_aio_array_reserve_slot(
3647/*======================*/
3648 ulint type,
3649 os_aio_array_t* array,
3650 fil_node_t* message1,
3652 void* message2,
3654 os_file_t file,
3655 const char* name,
3657 void* buf,
3659 ulint offset,
3661 ulint offset_high,
3663 ulint len)
3664{
3665 os_aio_slot_t* slot = NULL;
3666#ifdef WIN_ASYNC_IO
3667 OVERLAPPED* control;
3668
3669#elif defined(LINUX_NATIVE_AIO)
3670
3671 struct iocb* iocb;
3672 off_t aio_offset;
3673
3674#endif
3675 ulint i;
3676 ulint counter;
3677 ulint slots_per_seg;
3678 ulint local_seg;
3679
3680#ifdef WIN_ASYNC_IO
3681 ut_a((len & 0xFFFFFFFFUL) == len);
3682#endif
3683
3684 /* No need of a mutex. Only reading constant fields */
3685 slots_per_seg = array->n_slots / array->n_segments;
3686
3687 /* We attempt to keep adjacent blocks in the same local
3688 segment. This can help in merging IO requests when we are
3689 doing simulated AIO */
3690 local_seg = (offset >> (UNIV_PAGE_SIZE_SHIFT + 6))
3691 % array->n_segments;
3692
3693loop:
3694 os_mutex_enter(array->mutex);
3695
3696 if (array->n_reserved == array->n_slots) {
3697 os_mutex_exit(array->mutex);
3698
3699 if (!srv_use_native_aio) {
3700 /* If the handler threads are suspended, wake them
3701 so that we get more slots */
3702
3704 }
3705
3706 os_event_wait(array->not_full);
3707
3708 goto loop;
3709 }
3710
3711 /* We start our search for an available slot from our preferred
3712 local segment and do a full scan of the array. We are
3713 guaranteed to find a slot in full scan. */
3714 for (i = local_seg * slots_per_seg, counter = 0;
3715 counter < array->n_slots; i++, counter++) {
3716
3717 i %= array->n_slots;
3718 slot = os_aio_array_get_nth_slot(array, i);
3719
3720 if (slot->reserved == FALSE) {
3721 goto found;
3722 }
3723 }
3724
3725 /* We MUST always be able to get hold of a reserved slot. */
3726 ut_error;
3727
3728found:
3729 ut_a(slot->reserved == FALSE);
3730 array->n_reserved++;
3731
3732 if (array->n_reserved == 1) {
3733 os_event_reset(array->is_empty);
3734 }
3735
3736 if (array->n_reserved == array->n_slots) {
3737 os_event_reset(array->not_full);
3738 }
3739
3740 slot->reserved = TRUE;
3741 slot->reservation_time = time(NULL);
3742 slot->message1 = message1;
3743 slot->message2 = message2;
3744 slot->file = file;
3745 slot->name = name;
3746 slot->len = len;
3747 slot->type = type;
3748 slot->buf = static_cast<unsigned char *>(buf);
3749 slot->offset = offset;
3750 slot->offset_high = offset_high;
3751 slot->io_already_done = FALSE;
3752
3753#ifdef WIN_ASYNC_IO
3754 control = &(slot->control);
3755 control->Offset = (DWORD)offset;
3756 control->OffsetHigh = (DWORD)offset_high;
3757 ResetEvent(slot->handle);
3758
3759#elif defined(LINUX_NATIVE_AIO)
3760
3761 /* If we are not using native AIO skip this part. */
3762 if (!srv_use_native_aio) {
3763 goto skip_native_aio;
3764 }
3765
3766 /* Check if we are dealing with 64 bit arch.
3767 If not then make sure that offset fits in 32 bits. */
3768 if (sizeof(aio_offset) == 8) {
3769 aio_offset = offset_high;
3770 aio_offset <<= 32;
3771 aio_offset += offset;
3772 } else {
3773 ut_a(offset_high == 0);
3774 aio_offset = offset;
3775 }
3776
3777 iocb = &slot->control;
3778
3779 if (type == OS_FILE_READ) {
3780 io_prep_pread(iocb, file, buf, len, aio_offset);
3781 } else {
3782 ut_a(type == OS_FILE_WRITE);
3783 io_prep_pwrite(iocb, file, buf, len, aio_offset);
3784 }
3785
3786 iocb->data = (void*)slot;
3787 slot->n_bytes = 0;
3788 slot->ret = 0;
3789 /*fprintf(stderr, "Filled up Linux native iocb.\n");*/
3790
3791
3792skip_native_aio:
3793#endif /* LINUX_NATIVE_AIO */
3794 os_mutex_exit(array->mutex);
3795
3796 return(slot);
3797}
3798
3799/*******************************************************************/
3801static
3802void
3803os_aio_array_free_slot(
3804/*===================*/
3805 os_aio_array_t* array,
3806 os_aio_slot_t* slot)
3807{
3808 ut_ad(array);
3809 ut_ad(slot);
3810
3811 os_mutex_enter(array->mutex);
3812
3813 ut_ad(slot->reserved);
3814
3815 slot->reserved = FALSE;
3816
3817 array->n_reserved--;
3818
3819 if (array->n_reserved == array->n_slots - 1) {
3820 os_event_set(array->not_full);
3821 }
3822
3823 if (array->n_reserved == 0) {
3824 os_event_set(array->is_empty);
3825 }
3826
3827#ifdef WIN_ASYNC_IO
3828
3829 ResetEvent(slot->handle);
3830
3831#elif defined(LINUX_NATIVE_AIO)
3832
3833 if (srv_use_native_aio) {
3834 memset(&slot->control, 0x0, sizeof(slot->control));
3835 slot->n_bytes = 0;
3836 slot->ret = 0;
3837 /*fprintf(stderr, "Freed up Linux native slot.\n");*/
3838 } else {
3839 /* These fields should not be used if we are not
3840 using native AIO. */
3841 ut_ad(slot->n_bytes == 0);
3842 ut_ad(slot->ret == 0);
3843 }
3844
3845#endif
3846 os_mutex_exit(array->mutex);
3847}
3848
3849/**********************************************************************/
3851static
3852void
3853os_aio_simulated_wake_handler_thread(
3854/*=================================*/
3855 ulint global_segment)
3857{
3858 os_aio_array_t* array;
3859 os_aio_slot_t* slot;
3860 ulint segment;
3861 ulint n;
3862 ulint i;
3863
3864 ut_ad(!srv_use_native_aio);
3865
3866 segment = os_aio_get_array_and_local_segment(&array, global_segment);
3867
3868 n = array->n_slots / array->n_segments;
3869
3870 /* Look through n slots after the segment * n'th slot */
3871
3872 os_mutex_enter(array->mutex);
3873
3874 for (i = 0; i < n; i++) {
3875 slot = os_aio_array_get_nth_slot(array, i + segment * n);
3876
3877 if (slot->reserved) {
3878 /* Found an i/o request */
3879
3880 break;
3881 }
3882 }
3883
3884 os_mutex_exit(array->mutex);
3885
3886 if (i < n) {
3887 os_event_set(os_aio_segment_wait_events[global_segment]);
3888 }
3889}
3890
3891/**********************************************************************/
3893UNIV_INTERN
3894void
3896/*=======================================*/
3897{
3898 ulint i;
3899
3900 if (srv_use_native_aio) {
3901 /* We do not use simulated aio: do nothing */
3902
3903 return;
3904 }
3905
3906 os_aio_recommend_sleep_for_read_threads = FALSE;
3907
3908 for (i = 0; i < os_aio_n_segments; i++) {
3909 os_aio_simulated_wake_handler_thread(i);
3910 }
3911}
3912
3913/**********************************************************************/
3918UNIV_INTERN
3919void
3921/*============================================*/
3922{
3923
3924/* The idea of putting background IO threads to sleep is only for
3925Windows when using simulated AIO. Windows XP seems to schedule
3926background threads too eagerly to allow for coalescing during
3927readahead requests. */
3928#ifdef __WIN__
3929 os_aio_array_t* array;
3930 ulint g;
3931
3932 if (srv_use_native_aio) {
3933 /* We do not use simulated aio: do nothing */
3934
3935 return;
3936 }
3937
3938 os_aio_recommend_sleep_for_read_threads = TRUE;
3939
3940 for (g = 0; g < os_aio_n_segments; g++) {
3941 os_aio_get_array_and_local_segment(&array, g);
3942
3943 if (array == os_aio_read_array) {
3944
3945 os_event_reset(os_aio_segment_wait_events[g]);
3946 }
3947 }
3948#endif /* __WIN__ */
3949}
3950
3951#if defined(LINUX_NATIVE_AIO)
3952/*******************************************************************/
3955static
3956ibool
3957os_aio_linux_dispatch(
3958/*==================*/
3959 os_aio_array_t* array,
3960 os_aio_slot_t* slot)
3961{
3962 int ret;
3963 ulint io_ctx_index;
3964 struct iocb* iocb;
3965
3966 ut_ad(slot != NULL);
3967 ut_ad(array);
3968
3969 ut_a(slot->reserved);
3970
3971 /* Find out what we are going to work with.
3972 The iocb struct is directly in the slot.
3973 The io_context is one per segment. */
3974
3975 iocb = &slot->control;
3976 io_ctx_index = (slot->pos * array->n_segments) / array->n_slots;
3977
3978 ret = io_submit(array->aio_ctx[io_ctx_index], 1, &iocb);
3979
3980#if defined(UNIV_AIO_DEBUG)
3981 fprintf(stderr,
3982 "io_submit[%c] ret[%d]: slot[%p] ctx[%p] seg[%lu]\n",
3983 (slot->type == OS_FILE_WRITE) ? 'w' : 'r', ret, slot,
3984 array->aio_ctx[io_ctx_index], (ulong)io_ctx_index);
3985#endif
3986
3987 /* io_submit returns number of successfully
3988 queued requests or -errno. */
3989 if (UNIV_UNLIKELY(ret != 1)) {
3990 errno = -ret;
3991 return(FALSE);
3992 }
3993
3994 return(TRUE);
3995}
3996#endif /* LINUX_NATIVE_AIO */
3997
3998
3999/*******************************************************************/
4003UNIV_INTERN
4004ibool
4006/*========*/
4007 ulint type,
4008 ulint mode,
4021 const char* name,
4023 os_file_t file,
4024 void* buf,
4026 ulint offset,
4028 ulint offset_high,
4030 ulint n,
4031 fil_node_t* message1,
4035 void* message2)
4039{
4040 os_aio_array_t* array;
4041 os_aio_slot_t* slot;
4042#ifdef WIN_ASYNC_IO
4043 ibool retval;
4044 BOOL ret = TRUE;
4045 DWORD len = (DWORD) n;
4046 struct fil_node_struct * dummy_mess1;
4047 void* dummy_mess2;
4048 ulint dummy_type;
4049#endif /* WIN_ASYNC_IO */
4050#if defined LINUX_NATIVE_AIO || defined WIN_ASYNC_IO
4051 ibool retry;
4052#endif
4053 ulint wake_later;
4054
4055 ut_ad(file);
4056 ut_ad(buf);
4057 ut_ad(n > 0);
4058 ut_ad(n % OS_FILE_LOG_BLOCK_SIZE == 0);
4059 ut_ad(offset % OS_FILE_LOG_BLOCK_SIZE == 0);
4060 ut_ad(os_aio_validate_skip());
4061#ifdef WIN_ASYNC_IO
4062 ut_ad((n & 0xFFFFFFFFUL) == n);
4063#endif
4064
4065 wake_later = mode & OS_AIO_SIMULATED_WAKE_LATER;
4066 mode = mode & (~OS_AIO_SIMULATED_WAKE_LATER);
4067
4068 if (mode == OS_AIO_SYNC
4069#ifdef WIN_ASYNC_IO
4070 && !srv_use_native_aio
4071#endif /* WIN_ASYNC_IO */
4072 ) {
4073 /* This is actually an ordinary synchronous read or write:
4074 no need to use an i/o-handler thread. NOTE that if we use
4075 Windows async i/o, Windows does not allow us to use
4076 ordinary synchronous os_file_read etc. on the same file,
4077 therefore we have built a special mechanism for synchronous
4078 wait in the Windows case. */
4079
4080 if (type == OS_FILE_READ) {
4081 return(os_file_read(file, buf, offset,
4082 offset_high, n));
4083 }
4084
4085 ut_a(type == OS_FILE_WRITE);
4086
4087 return(os_file_write(name, file, buf, offset, offset_high, n));
4088 }
4089
4090#if defined LINUX_NATIVE_AIO || defined WIN_ASYNC_IO
4091try_again:
4092#endif
4093 if (mode == OS_AIO_NORMAL) {
4094 if (type == OS_FILE_READ) {
4095 array = os_aio_read_array;
4096 } else {
4097 array = os_aio_write_array;
4098 }
4099 } else if (mode == OS_AIO_IBUF) {
4100 ut_ad(type == OS_FILE_READ);
4101 /* Reduce probability of deadlock bugs in connection with ibuf:
4102 do not let the ibuf i/o handler sleep */
4103
4104 wake_later = FALSE;
4105
4106 array = os_aio_ibuf_array;
4107 } else if (mode == OS_AIO_LOG) {
4108
4109 array = os_aio_log_array;
4110 } else if (mode == OS_AIO_SYNC) {
4111 array = os_aio_sync_array;
4112
4113#if defined(LINUX_NATIVE_AIO)
4114 /* In Linux native AIO we don't use sync IO array. */
4115 ut_a(!srv_use_native_aio);
4116#endif /* LINUX_NATIVE_AIO */
4117 } else {
4118 array = NULL; /* Eliminate compiler warning */
4119 ut_error;
4120 }
4121
4122 slot = os_aio_array_reserve_slot(type, array, message1, message2, file,
4123 name, buf, offset, offset_high, n);
4124 if (type == OS_FILE_READ) {
4125 if (srv_use_native_aio) {
4126 os_n_file_reads++;
4127 os_bytes_read_since_printout += n;
4128#ifdef WIN_ASYNC_IO
4129 ret = ReadFile(file, buf, (DWORD)n, &len,
4130 &(slot->control));
4131
4132#elif defined(LINUX_NATIVE_AIO)
4133 if (!os_aio_linux_dispatch(array, slot)) {
4134 goto err_exit;
4135 }
4136#endif
4137 } else {
4138 if (!wake_later) {
4139 os_aio_simulated_wake_handler_thread(
4140 os_aio_get_segment_no_from_slot(
4141 array, slot));
4142 }
4143 }
4144 } else if (type == OS_FILE_WRITE) {
4145 if (srv_use_native_aio) {
4146 os_n_file_writes++;
4147#ifdef WIN_ASYNC_IO
4148 ret = WriteFile(file, buf, (DWORD)n, &len,
4149 &(slot->control));
4150
4151#elif defined(LINUX_NATIVE_AIO)
4152 if (!os_aio_linux_dispatch(array, slot)) {
4153 goto err_exit;
4154 }
4155#endif
4156 } else {
4157 if (!wake_later) {
4158 os_aio_simulated_wake_handler_thread(
4159 os_aio_get_segment_no_from_slot(
4160 array, slot));
4161 }
4162 }
4163 } else {
4164 ut_error;
4165 }
4166
4167#ifdef WIN_ASYNC_IO
4168 if (srv_use_native_aio) {
4169 if ((ret && len == n)
4170 || (!ret && GetLastError() == ERROR_IO_PENDING)) {
4171 /* aio was queued successfully! */
4172
4173 if (mode == OS_AIO_SYNC) {
4174 /* We want a synchronous i/o operation on a
4175 file where we also use async i/o: in Windows
4176 we must use the same wait mechanism as for
4177 async i/o */
4178
4179 retval = os_aio_windows_handle(ULINT_UNDEFINED,
4180 slot->pos,
4181 &dummy_mess1,
4182 &dummy_mess2,
4183 &dummy_type);
4184
4185 return(retval);
4186 }
4187
4188 return(TRUE);
4189 }
4190
4191 goto err_exit;
4192 }
4193#endif /* WIN_ASYNC_IO */
4194 /* aio was queued successfully! */
4195 return(TRUE);
4196
4197#if defined LINUX_NATIVE_AIO || defined WIN_ASYNC_IO
4198err_exit:
4199 os_aio_array_free_slot(array, slot);
4200
4201 retry = os_file_handle_error(name,
4202 type == OS_FILE_READ
4203 ? "aio read" : "aio write");
4204 if (retry) {
4205
4206 goto try_again;
4207 }
4208
4209 return(FALSE);
4210#endif /* LINUX_NATIVE_AIO || WIN_ASYNC_IO */
4211}
4212
4213#ifdef WIN_ASYNC_IO
4214/**********************************************************************/
4222UNIV_INTERN
4223ibool
4224os_aio_windows_handle(
4225/*==================*/
4226 ulint segment,
4234 ulint pos,
4236 fil_node_t**message1,
4241 void** message2,
4242 ulint* type)
4243{
4244 ulint orig_seg = segment;
4245 os_aio_array_t* array;
4246 os_aio_slot_t* slot;
4247 ulint n;
4248 ulint i;
4249 ibool ret_val;
4250 BOOL ret;
4251 DWORD len;
4252 BOOL retry = FALSE;
4253
4254 if (segment == ULINT_UNDEFINED) {
4255 array = os_aio_sync_array;
4256 segment = 0;
4257 } else {
4258 segment = os_aio_get_array_and_local_segment(&array, segment);
4259 }
4260
4261 /* NOTE! We only access constant fields in os_aio_array. Therefore
4262 we do not have to acquire the protecting mutex yet */
4263
4264 ut_ad(os_aio_validate_skip());
4265 ut_ad(segment < array->n_segments);
4266
4267 n = array->n_slots / array->n_segments;
4268
4269 if (array == os_aio_sync_array) {
4270 WaitForSingleObject(
4271 os_aio_array_get_nth_slot(array, pos)->handle,
4272 INFINITE);
4273 i = pos;
4274 } else {
4275 srv_set_io_thread_op_info(orig_seg, "wait Windows aio");
4276 i = WaitForMultipleObjects((DWORD) n,
4277 array->handles + segment * n,
4278 FALSE,
4279 INFINITE);
4280 }
4281
4283 os_thread_exit(NULL);
4284 }
4285
4286 os_mutex_enter(array->mutex);
4287
4288 slot = os_aio_array_get_nth_slot(array, i + segment * n);
4289
4290 ut_a(slot->reserved);
4291
4292 if (orig_seg != ULINT_UNDEFINED) {
4294 "get windows aio return value");
4295 }
4296
4297 ret = GetOverlappedResult(slot->file, &(slot->control), &len, TRUE);
4298
4299 *message1 = slot->message1;
4300 *message2 = slot->message2;
4301
4302 *type = slot->type;
4303
4304 if (ret && len == slot->len) {
4305 ret_val = TRUE;
4306
4307#ifdef UNIV_DO_FLUSH
4308 if (slot->type == OS_FILE_WRITE
4309 && !os_do_not_call_flush_at_each_write) {
4310 if (!os_file_flush(slot->file)) {
4311 ut_error;
4312 }
4313 }
4314#endif /* UNIV_DO_FLUSH */
4315 } else if (os_file_handle_error(slot->name, "Windows aio")) {
4316
4317 retry = TRUE;
4318 } else {
4319
4320 ret_val = FALSE;
4321 }
4322
4323 os_mutex_exit(array->mutex);
4324
4325 if (retry) {
4326 /* retry failed read/write operation synchronously.
4327 No need to hold array->mutex. */
4328
4329#ifdef UNIV_PFS_IO
4330 /* This read/write does not go through os_file_read
4331 and os_file_write APIs, need to register with
4332 performance schema explicitly here. */
4333 struct PSI_file_locker* locker = NULL;
4334 register_pfs_file_io_begin(locker, slot->file, slot->len,
4335 (slot->type == OS_FILE_WRITE)
4336 ? PSI_FILE_WRITE
4337 : PSI_FILE_READ,
4338 __FILE__, __LINE__);
4339#endif
4340
4341 ut_a((slot->len & 0xFFFFFFFFUL) == slot->len);
4342
4343 switch (slot->type) {
4344 case OS_FILE_WRITE:
4345 ret = WriteFile(slot->file, slot->buf,
4346 (DWORD) slot->len, &len,
4347 &(slot->control));
4348
4349 break;
4350 case OS_FILE_READ:
4351 ret = ReadFile(slot->file, slot->buf,
4352 (DWORD) slot->len, &len,
4353 &(slot->control));
4354
4355 break;
4356 default:
4357 ut_error;
4358 }
4359
4360#ifdef UNIV_PFS_IO
4361 register_pfs_file_io_end(locker, len);
4362#endif
4363
4364 if (!ret && GetLastError() == ERROR_IO_PENDING) {
4365 /* aio was queued successfully!
4366 We want a synchronous i/o operation on a
4367 file where we also use async i/o: in Windows
4368 we must use the same wait mechanism as for
4369 async i/o */
4370
4371 ret = GetOverlappedResult(slot->file,
4372 &(slot->control),
4373 &len, TRUE);
4374 }
4375
4376 ret_val = ret && len == slot->len;
4377 }
4378
4379 os_aio_array_free_slot(array, slot);
4380
4381 return(ret_val);
4382}
4383#endif
4384
4385#if defined(LINUX_NATIVE_AIO)
4386/******************************************************************/
4397static
4398void
4399os_aio_linux_collect(
4400/*=================*/
4401 os_aio_array_t* array,
4402 ulint segment,
4403 ulint seg_size)
4404{
4405 int i;
4406 int ret;
4407 ulint start_pos;
4408 ulint end_pos;
4409 struct timespec timeout;
4410 struct io_event* events;
4411 struct io_context* io_ctx;
4412
4413 /* sanity checks. */
4414 ut_ad(array != NULL);
4415 ut_ad(seg_size > 0);
4416 ut_ad(segment < array->n_segments);
4417
4418 /* Which part of event array we are going to work on. */
4419 events = &array->aio_events[segment * seg_size];
4420
4421 /* Which io_context we are going to use. */
4422 io_ctx = array->aio_ctx[segment];
4423
4424 /* Starting point of the segment we will be working on. */
4425 start_pos = segment * seg_size;
4426
4427 /* End point. */
4428 end_pos = start_pos + seg_size;
4429
4430retry:
4431
4432 /* Go down if we are in shutdown mode.
4433 In case of srv_fast_shutdown == 2, there may be pending
4434 IO requests but that should be OK as we essentially treat
4435 that as a crash of InnoDB. */
4437 os_thread_exit(NULL);
4438 }
4439
4440 /* Initialize the events. The timeout value is arbitrary.
4441 We probably need to experiment with it a little. */
4442 memset(events, 0, sizeof(*events) * seg_size);
4443 timeout.tv_sec = 0;
4444 timeout.tv_nsec = OS_AIO_REAP_TIMEOUT;
4445
4446 ret = io_getevents(io_ctx, 1, seg_size, events, &timeout);
4447
4448 /* This error handling is for any error in collecting the
4449 IO requests. The errors, if any, for any particular IO
4450 request are simply passed on to the calling routine. */
4451
4452 /* Not enough resources! Try again. */
4453 if (ret == -EAGAIN) {
4454 goto retry;
4455 }
4456
4457 /* Interrupted! I have tested the behaviour in case of an
4458 interrupt. If we have some completed IOs available then
4459 the return code will be the number of IOs. We get EINTR only
4460 if there are no completed IOs and we have been interrupted. */
4461 if (ret == -EINTR) {
4462 goto retry;
4463 }
4464
4465 /* No pending request! Go back and check again. */
4466 if (ret == 0) {
4467 goto retry;
4468 }
4469
4470 /* All other errors! should cause a trap for now. */
4471 if (UNIV_UNLIKELY(ret < 0)) {
4472 ut_print_timestamp(stderr);
4473 fprintf(stderr,
4474 " InnoDB: unexpected ret_code[%d] from"
4475 " io_getevents()!\n", ret);
4476 ut_error;
4477 }
4478
4479 ut_a(ret > 0);
4480
4481 for (i = 0; i < ret; i++) {
4482 os_aio_slot_t* slot;
4483 struct iocb* control;
4484
4485 control = (struct iocb *)events[i].obj;
4486 ut_a(control != NULL);
4487
4488 slot = (os_aio_slot_t *) control->data;
4489
4490 /* Some sanity checks. */
4491 ut_a(slot != NULL);
4492 ut_a(slot->reserved);
4493
4494#if defined(UNIV_AIO_DEBUG)
4495 fprintf(stderr,
4496 "io_getevents[%c]: slot[%p] ctx[%p]"
4497 " seg[%lu]\n",
4498 (slot->type == OS_FILE_WRITE) ? 'w' : 'r',
4499 slot, io_ctx, segment);
4500#endif
4501
4502 /* We are not scribbling previous segment. */
4503 ut_a(slot->pos >= start_pos);
4504
4505 /* We have not overstepped to next segment. */
4506 ut_a(slot->pos < end_pos);
4507
4508 /* Mark this request as completed. The error handling
4509 will be done in the calling function. */
4510 os_mutex_enter(array->mutex);
4511 slot->n_bytes = events[i].res;
4512 slot->ret = events[i].res2;
4513 slot->io_already_done = TRUE;
4514 os_mutex_exit(array->mutex);
4515 }
4516
4517 return;
4518}
4519
4520/**********************************************************************/
4528UNIV_INTERN
4529ibool
4530os_aio_linux_handle(
4531/*================*/
4532 ulint global_seg,
4538 fil_node_t**message1,
4539 void** message2,
4543 ulint* type)
4544{
4545 ulint segment;
4546 os_aio_array_t* array;
4547 os_aio_slot_t* slot;
4548 ulint n;
4549 ulint i;
4550 ibool ret = FALSE;
4551
4552 /* Should never be doing Sync IO here. */
4553 ut_a(global_seg != ULINT_UNDEFINED);
4554
4555 /* Find the array and the local segment. */
4556 segment = os_aio_get_array_and_local_segment(&array, global_seg);
4557 n = array->n_slots / array->n_segments;
4558
4559 /* Loop until we have found a completed request. */
4560 for (;;) {
4561 os_mutex_enter(array->mutex);
4562 for (i = 0; i < n; ++i) {
4563 slot = os_aio_array_get_nth_slot(
4564 array, i + segment * n);
4565 if (slot->reserved && slot->io_already_done) {
4566 /* Something for us to work on. */
4567 goto found;
4568 }
4569 }
4570
4571 os_mutex_exit(array->mutex);
4572
4573 /* We don't have any completed request.
4574 Wait for some request. Note that we return
4575 from wait iff we have found a request. */
4576
4577 srv_set_io_thread_op_info(global_seg,
4578 "waiting for completed aio requests");
4579 os_aio_linux_collect(array, segment, n);
4580 }
4581
4582found:
4583 /* Note that it may be that there are more then one completed
4584 IO requests. We process them one at a time. We may have a case
4585 here to improve the performance slightly by dealing with all
4586 requests in one sweep. */
4587 srv_set_io_thread_op_info(global_seg,
4588 "processing completed aio requests");
4589
4590 /* Ensure that we are scribbling only our segment. */
4591 ut_a(i < n);
4592
4593 ut_ad(slot != NULL);
4594 ut_ad(slot->reserved);
4595 ut_ad(slot->io_already_done);
4596
4597 *message1 = slot->message1;
4598 *message2 = slot->message2;
4599
4600 *type = slot->type;
4601
4602 if ((slot->ret == 0) && (slot->n_bytes == (long)slot->len)) {
4603 ret = TRUE;
4604
4605#ifdef UNIV_DO_FLUSH
4606 if (slot->type == OS_FILE_WRITE
4607 && !os_do_not_call_flush_at_each_write)
4608 && !os_file_flush(slot->file) {
4609 ut_error;
4610 }
4611#endif /* UNIV_DO_FLUSH */
4612 } else {
4613 errno = -slot->ret;
4614
4615 /* os_file_handle_error does tell us if we should retry
4616 this IO. As it stands now, we don't do this retry when
4617 reaping requests from a different context than
4618 the dispatcher. This non-retry logic is the same for
4619 windows and linux native AIO.
4620 We should probably look into this to transparently
4621 re-submit the IO. */
4622 os_file_handle_error(slot->name, "Linux aio");
4623
4624 ret = FALSE;
4625 }
4626
4627 os_mutex_exit(array->mutex);
4628
4629 os_aio_array_free_slot(array, slot);
4630
4631 return(ret);
4632}
4633#endif /* LINUX_NATIVE_AIO */
4634
4635/**********************************************************************/
4639UNIV_INTERN
4640ibool
4642/*====================*/
4643 ulint global_segment,
4648 fil_node_t**message1,
4653 void** message2,
4654 ulint* type)
4655{
4656 os_aio_array_t* array;
4657 ulint segment;
4658 os_aio_slot_t* slot;
4659 os_aio_slot_t* slot2;
4660 os_aio_slot_t* consecutive_ios[OS_AIO_MERGE_N_CONSECUTIVE];
4661 ulint n_consecutive;
4662 ulint total_len;
4663 ulint offs;
4664 ulint lowest_offset;
4665 ulint biggest_age;
4666 ulint age;
4667 byte* combined_buf;
4668 byte* combined_buf2;
4669 ibool ret;
4670 ulint n;
4671 ulint i;
4672
4673 /* Fix compiler warning */
4674 *consecutive_ios = NULL;
4675
4676 memset(consecutive_ios, 0, sizeof(os_aio_slot_t*) * OS_AIO_MERGE_N_CONSECUTIVE);
4677 segment = os_aio_get_array_and_local_segment(&array, global_segment);
4678
4679restart:
4680 /* NOTE! We only access constant fields in os_aio_array. Therefore
4681 we do not have to acquire the protecting mutex yet */
4682
4683 srv_set_io_thread_op_info(global_segment,
4684 "looking for i/o requests (a)");
4685 ut_ad(os_aio_validate_skip());
4686 ut_ad(segment < array->n_segments);
4687
4688 n = array->n_slots / array->n_segments;
4689
4690 /* Look through n slots after the segment * n'th slot */
4691
4692 if (array == os_aio_read_array
4693 && os_aio_recommend_sleep_for_read_threads) {
4694
4695 /* Give other threads chance to add several i/os to the array
4696 at once. */
4697
4698 goto recommended_sleep;
4699 }
4700
4701 os_mutex_enter(array->mutex);
4702
4703 srv_set_io_thread_op_info(global_segment,
4704 "looking for i/o requests (b)");
4705
4706 /* Check if there is a slot for which the i/o has already been
4707 done */
4708
4709 for (i = 0; i < n; i++) {
4710 slot = os_aio_array_get_nth_slot(array, i + segment * n);
4711
4712 if (slot->reserved && slot->io_already_done) {
4713
4714 if (os_aio_print_debug) {
4715 fprintf(stderr,
4716 "InnoDB: i/o for slot %lu"
4717 " already done, returning\n",
4718 (ulong) i);
4719 }
4720
4721 ret = TRUE;
4722
4723 goto slot_io_done;
4724 }
4725 }
4726
4727 n_consecutive = 0;
4728
4729 /* If there are at least 2 seconds old requests, then pick the oldest
4730 one to prevent starvation. If several requests have the same age,
4731 then pick the one at the lowest offset. */
4732
4733 biggest_age = 0;
4734 lowest_offset = ULINT_MAX;
4735
4736 for (i = 0; i < n; i++) {
4737 slot = os_aio_array_get_nth_slot(array, i + segment * n);
4738
4739 if (slot->reserved) {
4740 age = (ulint)difftime(time(NULL),
4741 slot->reservation_time);
4742
4743 if ((age >= 2 && age > biggest_age)
4744 || (age >= 2 && age == biggest_age
4745 && slot->offset < lowest_offset)) {
4746
4747 /* Found an i/o request */
4748 consecutive_ios[0] = slot;
4749
4750 n_consecutive = 1;
4751
4752 biggest_age = age;
4753 lowest_offset = slot->offset;
4754 }
4755 }
4756 }
4757
4758 if (n_consecutive == 0) {
4759 /* There were no old requests. Look for an i/o request at the
4760 lowest offset in the array (we ignore the high 32 bits of the
4761 offset in these heuristics) */
4762
4763 lowest_offset = ULINT_MAX;
4764
4765 for (i = 0; i < n; i++) {
4766 slot = os_aio_array_get_nth_slot(array,
4767 i + segment * n);
4768
4769 if (slot->reserved && slot->offset < lowest_offset) {
4770
4771 /* Found an i/o request */
4772 consecutive_ios[0] = slot;
4773
4774 n_consecutive = 1;
4775
4776 lowest_offset = slot->offset;
4777 }
4778 }
4779 }
4780
4781 if (n_consecutive == 0) {
4782
4783 /* No i/o requested at the moment */
4784
4785 goto wait_for_io;
4786 }
4787
4788 /* if n_consecutive != 0, then we have assigned
4789 something valid to consecutive_ios[0] */
4790 ut_ad(n_consecutive != 0);
4791 ut_ad(consecutive_ios[0] != NULL);
4792
4793 slot = consecutive_ios[0];
4794
4795 /* Check if there are several consecutive blocks to read or write */
4796
4797consecutive_loop:
4798 for (i = 0; i < n; i++) {
4799 slot2 = os_aio_array_get_nth_slot(array, i + segment * n);
4800
4801 if (slot2->reserved && slot2 != slot
4802 && slot2->offset == slot->offset + slot->len
4803 /* check that sum does not wrap over */
4804 && slot->offset + slot->len > slot->offset
4805 && slot2->offset_high == slot->offset_high
4806 && slot2->type == slot->type
4807 && slot2->file == slot->file) {
4808
4809 /* Found a consecutive i/o request */
4810
4811 consecutive_ios[n_consecutive] = slot2;
4812 n_consecutive++;
4813
4814 slot = slot2;
4815
4816 if (n_consecutive < OS_AIO_MERGE_N_CONSECUTIVE) {
4817
4818 goto consecutive_loop;
4819 } else {
4820 break;
4821 }
4822 }
4823 }
4824
4825 srv_set_io_thread_op_info(global_segment, "consecutive i/o requests");
4826
4827 /* We have now collected n_consecutive i/o requests in the array;
4828 allocate a single buffer which can hold all data, and perform the
4829 i/o */
4830
4831 total_len = 0;
4832 slot = consecutive_ios[0];
4833
4834 for (i = 0; i < n_consecutive; i++) {
4835 total_len += consecutive_ios[i]->len;
4836 }
4837
4838 if (n_consecutive == 1) {
4839 /* We can use the buffer of the i/o request */
4840 combined_buf = slot->buf;
4841 combined_buf2 = NULL;
4842 } else {
4843 combined_buf2 = static_cast<unsigned char *>(ut_malloc(total_len + UNIV_PAGE_SIZE));
4844
4845 ut_a(combined_buf2);
4846
4847 combined_buf = static_cast<unsigned char *>(ut_align(combined_buf2, UNIV_PAGE_SIZE));
4848 }
4849
4850 /* We release the array mutex for the time of the i/o: NOTE that
4851 this assumes that there is just one i/o-handler thread serving
4852 a single segment of slots! */
4853
4854 os_mutex_exit(array->mutex);
4855
4856 if (slot->type == OS_FILE_WRITE && n_consecutive > 1) {
4857 /* Copy the buffers to the combined buffer */
4858 offs = 0;
4859
4860 for (i = 0; i < n_consecutive; i++) {
4861
4862 ut_memcpy(combined_buf + offs, consecutive_ios[i]->buf,
4863 consecutive_ios[i]->len);
4864 offs += consecutive_ios[i]->len;
4865 }
4866 }
4867
4868 srv_set_io_thread_op_info(global_segment, "doing file i/o");
4869
4870 if (os_aio_print_debug) {
4871 fprintf(stderr,
4872 "InnoDB: doing i/o of type %lu at offset %lu %lu,"
4873 " length %lu\n",
4874 (ulong) slot->type, (ulong) slot->offset_high,
4875 (ulong) slot->offset, (ulong) total_len);
4876 }
4877
4878 /* Do the i/o with ordinary, synchronous i/o functions: */
4879 if (slot->type == OS_FILE_WRITE) {
4880 ret = os_file_write(slot->name, slot->file, combined_buf,
4881 slot->offset, slot->offset_high,
4882 total_len);
4883 } else {
4884 ret = os_file_read(slot->file, combined_buf,
4885 slot->offset, slot->offset_high, total_len);
4886 }
4887
4888 ut_a(ret);
4889 srv_set_io_thread_op_info(global_segment, "file i/o done");
4890
4891#if 0
4892 fprintf(stderr,
4893 "aio: %lu consecutive %lu:th segment, first offs %lu blocks\n",
4894 n_consecutive, global_segment, slot->offset / UNIV_PAGE_SIZE);
4895#endif
4896
4897 if (slot->type == OS_FILE_READ && n_consecutive > 1) {
4898 /* Copy the combined buffer to individual buffers */
4899 offs = 0;
4900
4901 for (i = 0; i < n_consecutive; i++) {
4902
4903 ut_memcpy(consecutive_ios[i]->buf, combined_buf + offs,
4904 consecutive_ios[i]->len);
4905 offs += consecutive_ios[i]->len;
4906 }
4907 }
4908
4909 if (combined_buf2) {
4910 ut_free(combined_buf2);
4911 }
4912
4913 os_mutex_enter(array->mutex);
4914
4915 /* Mark the i/os done in slots */
4916
4917 for (i = 0; i < n_consecutive; i++) {
4918 consecutive_ios[i]->io_already_done = TRUE;
4919 }
4920
4921 /* We return the messages for the first slot now, and if there were
4922 several slots, the messages will be returned with subsequent calls
4923 of this function */
4924
4925slot_io_done:
4926
4927 ut_a(slot->reserved);
4928
4929 *message1 = slot->message1;
4930 *message2 = slot->message2;
4931
4932 *type = slot->type;
4933
4934 os_mutex_exit(array->mutex);
4935
4936 os_aio_array_free_slot(array, slot);
4937
4938 return(ret);
4939
4940wait_for_io:
4941 srv_set_io_thread_op_info(global_segment, "resetting wait event");
4942
4943 /* We wait here until there again can be i/os in the segment
4944 of this thread */
4945
4946 os_event_reset(os_aio_segment_wait_events[global_segment]);
4947
4948 os_mutex_exit(array->mutex);
4949
4950recommended_sleep:
4951 srv_set_io_thread_op_info(global_segment, "waiting for i/o request");
4952
4953 os_event_wait(os_aio_segment_wait_events[global_segment]);
4954
4955 if (os_aio_print_debug) {
4956 fprintf(stderr,
4957 "InnoDB: i/o handler thread for i/o"
4958 " segment %lu wakes up\n",
4959 (ulong) global_segment);
4960 }
4961
4962 goto restart;
4963}
4964
4965/**********************************************************************/
4968static
4969ibool
4970os_aio_array_validate(
4971/*==================*/
4972 os_aio_array_t* array)
4973{
4974 os_aio_slot_t* slot;
4975 ulint n_reserved = 0;
4976 ulint i;
4977
4978 ut_a(array);
4979
4980 os_mutex_enter(array->mutex);
4981
4982 ut_a(array->n_slots > 0);
4983 ut_a(array->n_segments > 0);
4984
4985 for (i = 0; i < array->n_slots; i++) {
4986 slot = os_aio_array_get_nth_slot(array, i);
4987
4988 if (slot->reserved) {
4989 n_reserved++;
4990 ut_a(slot->len > 0);
4991 }
4992 }
4993
4994 ut_a(array->n_reserved == n_reserved);
4995
4996 os_mutex_exit(array->mutex);
4997
4998 return(TRUE);
4999}
5000
5001/**********************************************************************/
5004UNIV_INTERN
5005ibool
5007/*=================*/
5008{
5009 os_aio_array_validate(os_aio_read_array);
5010 os_aio_array_validate(os_aio_write_array);
5011 os_aio_array_validate(os_aio_ibuf_array);
5012 os_aio_array_validate(os_aio_log_array);
5013 os_aio_array_validate(os_aio_sync_array);
5014
5015 return(TRUE);
5016}
5017
5018/**********************************************************************/
5023static
5024void
5025os_aio_print_segment_info(
5026/*======================*/
5027 FILE* file,
5028 ulint* n_seg,
5029 os_aio_array_t* array)
5030{
5031 ulint i;
5032
5033 ut_ad(array);
5034 ut_ad(n_seg);
5035 ut_ad(array->n_segments > 0);
5036
5037 if (array->n_segments == 1) {
5038 return;
5039 }
5040
5041 fprintf(file, " [");
5042 for (i = 0; i < array->n_segments; i++) {
5043 if (i != 0) {
5044 fprintf(file, ", ");
5045 }
5046
5047 fprintf(file, "%lu", n_seg[i]);
5048 }
5049 fprintf(file, "] ");
5050}
5051
5052/**********************************************************************/
5054UNIV_INTERN
5055void
5057/*=========*/
5058 FILE* file)
5059{
5060 os_aio_array_t* array;
5061 os_aio_slot_t* slot;
5062 ulint n_reserved;
5063 ulint n_res_seg[SRV_MAX_N_IO_THREADS];
5064 time_t current_time;
5065 double time_elapsed;
5066 double avg_bytes_read;
5067 ulint i;
5068
5069 for (i = 0; i < srv_n_file_io_threads; i++) {
5070 fprintf(file, "I/O thread %lu state: %s (%s)", (ulong) i,
5071 srv_io_thread_op_info[i],
5072 srv_io_thread_function[i]);
5073
5074#ifndef __WIN__
5075 if (os_aio_segment_wait_events[i]->is_set) {
5076 fprintf(file, " ev set");
5077 }
5078#endif
5079
5080 fprintf(file, "\n");
5081 }
5082
5083 fputs("Pending normal aio reads:", file);
5084
5085 array = os_aio_read_array;
5086loop:
5087 ut_a(array);
5088
5089 os_mutex_enter(array->mutex);
5090
5091 ut_a(array->n_slots > 0);
5092 ut_a(array->n_segments > 0);
5093
5094 n_reserved = 0;
5095
5096 memset(n_res_seg, 0x0, sizeof(n_res_seg));
5097
5098 for (i = 0; i < array->n_slots; i++) {
5099 ulint seg_no;
5100
5101 slot = os_aio_array_get_nth_slot(array, i);
5102
5103 seg_no = (i * array->n_segments) / array->n_slots;
5104 if (slot->reserved) {
5105 n_reserved++;
5106 n_res_seg[seg_no]++;
5107#if 0
5108 fprintf(stderr, "Reserved slot, messages %p %p\n",
5109 (void*) slot->message1,
5110 (void*) slot->message2);
5111#endif
5112 ut_a(slot->len > 0);
5113 }
5114 }
5115
5116 ut_a(array->n_reserved == n_reserved);
5117
5118 fprintf(file, " %lu", (ulong) n_reserved);
5119
5120 os_aio_print_segment_info(file, n_res_seg, array);
5121
5122 os_mutex_exit(array->mutex);
5123
5124 if (array == os_aio_read_array) {
5125 fputs(", aio writes:", file);
5126
5127 array = os_aio_write_array;
5128
5129 goto loop;
5130 }
5131
5132 if (array == os_aio_write_array) {
5133 fputs(",\n ibuf aio reads:", file);
5134 array = os_aio_ibuf_array;
5135
5136 goto loop;
5137 }
5138
5139 if (array == os_aio_ibuf_array) {
5140 fputs(", log i/o's:", file);
5141 array = os_aio_log_array;
5142
5143 goto loop;
5144 }
5145
5146 if (array == os_aio_log_array) {
5147 fputs(", sync i/o's:", file);
5148 array = os_aio_sync_array;
5149
5150 goto loop;
5151 }
5152
5153 putc('\n', file);
5154 current_time = time(NULL);
5155 time_elapsed = 0.001 + difftime(current_time, os_last_printout);
5156
5157 fprintf(file,
5158 "Pending flushes (fsync) log: %lu; buffer pool: %lu\n"
5159 "%lu OS file reads, %lu OS file writes, %lu OS fsyncs\n",
5160 (ulong) fil_n_pending_log_flushes,
5161 (ulong) fil_n_pending_tablespace_flushes,
5162 (ulong) os_n_file_reads, (ulong) os_n_file_writes,
5163 (ulong) os_n_fsyncs);
5164
5166 fprintf(file,
5167 "%lu pending preads, %lu pending pwrites\n",
5170 }
5171
5172 if (os_n_file_reads == os_n_file_reads_old) {
5173 avg_bytes_read = 0.0;
5174 } else {
5175 avg_bytes_read = (double) os_bytes_read_since_printout
5176 / (os_n_file_reads - os_n_file_reads_old);
5177 }
5178
5179 fprintf(file,
5180 "%.2f reads/s, %lu avg bytes/read,"
5181 " %.2f writes/s, %.2f fsyncs/s\n",
5182 (os_n_file_reads - os_n_file_reads_old)
5183 / time_elapsed,
5184 (ulong)avg_bytes_read,
5185 (os_n_file_writes - os_n_file_writes_old)
5186 / time_elapsed,
5187 (os_n_fsyncs - os_n_fsyncs_old)
5188 / time_elapsed);
5189
5190 os_n_file_reads_old = os_n_file_reads;
5191 os_n_file_writes_old = os_n_file_writes;
5192 os_n_fsyncs_old = os_n_fsyncs;
5193 os_bytes_read_since_printout = 0;
5194
5195 os_last_printout = current_time;
5196}
5197
5198/**********************************************************************/
5200UNIV_INTERN
5201void
5203/*======================*/
5204{
5205 os_n_file_reads_old = os_n_file_reads;
5206 os_n_file_writes_old = os_n_file_writes;
5207 os_n_fsyncs_old = os_n_fsyncs;
5208 os_bytes_read_since_printout = 0;
5209
5210 os_last_printout = time(NULL);
5211}
5212
5213#ifdef UNIV_DEBUG
5214/**********************************************************************/
5218UNIV_INTERN
5219ibool
5220os_aio_all_slots_free(void)
5221/*=======================*/
5222{
5223 os_aio_array_t* array;
5224 ulint n_res = 0;
5225
5226 array = os_aio_read_array;
5227
5228 os_mutex_enter(array->mutex);
5229
5230 n_res += array->n_reserved;
5231
5232 os_mutex_exit(array->mutex);
5233
5234 array = os_aio_write_array;
5235
5236 os_mutex_enter(array->mutex);
5237
5238 n_res += array->n_reserved;
5239
5240 os_mutex_exit(array->mutex);
5241
5242 array = os_aio_ibuf_array;
5243
5244 os_mutex_enter(array->mutex);
5245
5246 n_res += array->n_reserved;
5247
5248 os_mutex_exit(array->mutex);
5249
5250 array = os_aio_log_array;
5251
5252 os_mutex_enter(array->mutex);
5253
5254 n_res += array->n_reserved;
5255
5256 os_mutex_exit(array->mutex);
5257
5258 array = os_aio_sync_array;
5259
5260 os_mutex_enter(array->mutex);
5261
5262 n_res += array->n_reserved;
5263
5264 os_mutex_exit(array->mutex);
5265
5266 if (n_res == 0) {
5267
5268 return(TRUE);
5269 }
5270
5271 return(FALSE);
5272}
5273#endif /* UNIV_DEBUG */
5274
5275#endif /* !UNIV_HOTBACKUP */
UNIV_INTERN int innobase_mysql_tmpfile(void)
#define mem_free(PTR)
Definition mem0mem.h:243
UNIV_INLINE char * mem_strdup(const char *str)
UNIV_INLINE char * mem_strdupl(const char *str, ulint len)
UNIV_INTERN ibool os_file_read_no_error_handling_func(os_file_t file, void *buf, ulint offset, ulint offset_high, ulint n)
Definition os0file.cc:2555
UNIV_INTERN ibool os_file_status(const char *path, ibool *exists, os_file_type_t *type)
Definition os0file.cc:2890
#define OS_DATA_FILE
Definition os0file.h:124
#define OS_WIN31
Definition os0file.h:175
UNIV_INTERN ulint os_file_get_last_error(ibool report_all_errors)
Definition os0file.cc:385
#define OS_AIO_LOG
Definition os0file.h:156
UNIV_INTERN int os_file_closedir(os_file_dir_t dir)
Definition os0file.cc:817
#define OS_AIO_SYNC
Definition os0file.h:157
#define OS_WINNT
Definition os0file.h:177
UNIV_INTERN ibool os_file_write_func(const char *name, os_file_t file, const void *buf, ulint offset, ulint offset_high, ulint n)
Definition os0file.cc:2687
UNIV_INTERN char * os_file_dirname(const char *path)
Definition os0file.cc:3072
struct fil_node_struct fil_node_t
Definition os0file.h:48
#define OS_WIN2000
Definition os0file.h:178
UNIV_INTERN ibool os_aio_simulated_handle(ulint segment, fil_node_t **message1, void **message2, ulint *type)
Definition os0file.cc:4641
UNIV_INTERN ibool os_file_close_func(os_file_t file)
Definition os0file.cc:1807
UNIV_INTERN ibool os_file_read_func(os_file_t file, void *buf, ulint offset, ulint offset_high, ulint n)
Definition os0file.cc:2426
UNIV_INTERN void os_file_set_nocache(int fd, const char *file_name, const char *operation_name)
Definition os0file.cc:1327
UNIV_INTERN os_file_t os_file_create_func(const char *name, ulint create_mode, ulint purpose, ulint type, ibool *success)
Definition os0file.cc:1378
UNIV_INTERN void os_aio_simulated_put_read_threads_to_sleep(void)
Definition os0file.cc:3920
UNIV_INTERN void os_aio_simulated_wake_handler_threads(void)
Definition os0file.cc:3895
#define OS_AIO_NORMAL
Definition os0file.h:152
FILE * os_file_create_tmpfile(void)
Definition os0file.cc:729
UNIV_INTERN os_file_t os_file_create_simple_func(const char *name, ulint create_mode, ulint access_type, ibool *success)
Definition os0file.cc:1074
UNIV_INTERN ibool os_file_rename_func(const char *oldpath, const char *newpath)
Definition os0file.cc:1767
UNIV_INTERN int os_file_readdir_next_file(const char *dirname, os_file_dir_t dir, os_file_stat_t *info)
Definition os0file.cc:852
UNIV_INTERN void os_aio_print(FILE *file)
Definition os0file.cc:5056
int os_file_t
Definition os0file.h:87
UNIV_INTERN void os_aio_wait_until_no_pending_writes(void)
Definition os0file.cc:3562
#define OS_WIN95
Definition os0file.h:176
ulint os_file_n_pending_pwrites
Definition os0file.cc:301
UNIV_INTERN os_file_t os_file_create_simple_no_error_handling_func(const char *name, ulint create_mode, ulint access_type, ibool *success)
Definition os0file.cc:1217
UNIV_INTERN void os_aio_wake_all_threads_at_shutdown(void)
Definition os0file.cc:3524
UNIV_INTERN ibool os_file_get_size(os_file_t file, ulint *size, ulint *size_high)
Definition os0file.cc:1882
ulint os_n_pending_writes
Definition os0file.cc:303
UNIV_INTERN ibool os_file_flush_func(os_file_t file)
Definition os0file.cc:2109
UNIV_INTERN ibool os_aio_func(ulint type, ulint mode, const char *name, os_file_t file, void *buf, ulint offset, ulint offset_high, ulint n, fil_node_t *message1, void *message2)
Definition os0file.cc:4005
#define OS_FILE_LOG_BLOCK_SIZE
Definition os0file.h:104
UNIV_INTERN void os_file_read_string(FILE *file, char *str, ulint size)
Definition os0file.cc:2663
UNIV_INTERN ibool os_file_create_subdirs_if_needed(const char *path)
Definition os0file.cc:3102
#define OS_WINVISTA
Definition os0file.h:181
DIR * os_file_dir_t
Definition os0file.h:374
UNIV_INTERN ibool os_file_set_size(const char *name, os_file_t file, ulint size, ulint size_high)
Definition os0file.cc:1953
ibool os_aio_print_debug
Definition os0file.cc:149
UNIV_INTERN ibool os_file_set_eof(FILE *file)
Definition os0file.cc:2042
UNIV_INTERN ibool os_file_get_status(const char *path, os_file_stat_t *stat_info)
Definition os0file.cc:2962
UNIV_INTERN ibool os_file_delete_if_exists(const char *name)
Definition os0file.cc:1640
ulint os_file_n_pending_preads
Definition os0file.cc:299
ulint os_n_pending_reads
Definition os0file.cc:305
#define OS_FILE_NOT_FOUND
Definition os0file.h:129
#define OS_FILE_OPEN
Definition os0file.h:107
#define OS_WIN7
Definition os0file.h:183
#define OS_AIO_SIMULATED_WAKE_LATER
Definition os0file.h:167
#define OS_WINXP
Definition os0file.h:179
UNIV_INTERN void os_aio_refresh_stats(void)
Definition os0file.cc:5202
UNIV_INTERN ibool os_file_delete(const char *name)
Definition os0file.cc:1702
UNIV_INTERN void os_io_init_simple(void)
Definition os0file.cc:711
ulint os_innodb_umask
Definition os0file.cc:71
UNIV_INTERN ibool os_aio_validate(void)
Definition os0file.cc:5006
#define OS_FILE_READ
Definition os0file.h:142
UNIV_INTERN ib_int64_t os_file_get_size_as_iblonglong(os_file_t file)
Definition os0file.cc:1930
UNIV_INTERN ibool os_file_create_directory(const char *pathname, ibool fail_if_exists)
Definition os0file.cc:1029
UNIV_INTERN os_file_dir_t os_file_opendir(const char *dirname, ibool error_is_fatal)
Definition os0file.cc:761
#define OS_AIO_IBUF
Definition os0file.h:154
UNIV_INTERN ib_int64_t os_event_reset(os_event_t event)
Definition os0sync.cc:472
UNIV_INTERN void os_mutex_enter(os_mutex_t mutex)
Definition os0sync.cc:809
UNIV_INTERN os_event_t os_event_create(const char *name)
Definition os0sync.cc:365
UNIV_INTERN void os_event_set(os_event_t event)
Definition os0sync.cc:434
UNIV_INTERN void os_event_free(os_event_t event)
Definition os0sync.cc:535
os_event_struct_t * os_event_t
Definition os0sync.h:58
UNIV_INTERN os_mutex_t os_mutex_create(void)
Definition os0sync.cc:774
os_mutex_str_t * os_mutex_t
Definition os0sync.h:89
UNIV_INTERN void os_mutex_exit(os_mutex_t mutex)
Definition os0sync.cc:824
UNIV_INTERN void os_mutex_free(os_mutex_t mutex)
Definition os0sync.cc:840
UNIV_INTERN void os_thread_exit(void *exit_value)
Definition os0thread.cc:199
UNIV_INTERN void os_thread_sleep(ulint tm)
Definition os0thread.cc:265
@ SRV_UNIX_ALL_O_DIRECT
Definition srv0srv.h:423
@ SRV_UNIX_O_DSYNC
Definition srv0srv.h:416
@ SRV_UNIX_NOSYNC
Definition srv0srv.h:420
@ SRV_UNIX_LITTLESYNC
Definition srv0srv.h:417
@ SRV_UNIX_O_DIRECT
Definition srv0srv.h:421
@ SRV_WIN_IO_UNBUFFERED
Definition srv0srv.h:429
my_bool srv_file_per_table
Definition srv0srv.cc:125
UNIV_INTERN void srv_set_io_thread_op_info(ulint i, const char *str)
Definition srv0srv.cc:841
srv_shutdown_state
Definition srv0start.h:113
@ SRV_SHUTDOWN_EXIT_THREADS
Definition srv0start.h:120
ibool srv_is_being_started
Definition srv0start.cc:116
ibool srv_start_raw_disk_in_use
Definition srv0start.cc:110
os_file_t handle
Definition fil0fil.cc:140
os_aio_slot_t * slots
Definition os0file.cc:228
os_event_t not_full
Definition os0file.cc:205
os_mutex_t mutex
Definition os0file.cc:204
os_event_t is_empty
Definition os0file.cc:209
const char * name
Definition os0file.cc:176
time_t reservation_time
Definition os0file.cc:167
fil_node_t * message1
Definition os0file.cc:182
ib_int64_t size
Definition os0file.h:364
char name[OS_FILE_MAX_PATH]
Definition os0file.h:362
os_file_type_t type
Definition os0file.h:363
UNIV_INLINE void * ut_align(const void *ptr, ulint align_no)
#define ut_error
Definition ut0dbg.h:115
#define ut_ad(EXPR)
Definition ut0dbg.h:127
#define ut_a(EXPR)
Definition ut0dbg.h:105
UNIV_INLINE void * ut_memcpy(void *dest, const void *sour, ulint n)
UNIV_INTERN void ut_free(void *ptr)
Definition ut0mem.cc:294
UNIV_INTERN void * ut_malloc(ulint n)
Definition ut0mem.cc:235
UNIV_INLINE ulint ut_min(ulint n1, ulint n2)
UNIV_INTERN void ut_print_timestamp(FILE *file)
Definition ut0ut.cc:239