os0file.c 138 KB
Newer Older
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1 2
/***********************************************************************

3
Copyright (c) 1995, 2010, Innobase Oy. All Rights Reserved.
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Copyright (c) 2009, Percona Inc.

Portions of this file contain modifications contributed and copyrighted
by Percona Inc.. Those modifications are
gratefully acknowledged and are described briefly in the InnoDB
documentation. The contributions by Percona Inc. are incorporated with
their permission, and subject to the conditions contained in the file
COPYING.Percona.

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.
Vadim Tkachenko's avatar
Vadim Tkachenko committed
21

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
22 23 24 25 26 27 28 29
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

***********************************************************************/

/**************************************************//**
@file os/os0file.c
30 31 32 33 34 35
The interface to the operating system file i/o primitives

Created 10/21/1995 Heikki Tuuri
*******************************************************/

#include "os0file.h"
Sergei Golubchik's avatar
Sergei Golubchik committed
36 37 38 39

#ifdef UNIV_NONINL
#include "os0file.ic"
#endif
Sergei Golubchik's avatar
Sergei Golubchik committed
40
#include "ha_prototypes.h"
41 42 43 44 45
#include "ut0mem.h"
#include "srv0srv.h"
#include "srv0start.h"
#include "fil0fil.h"
#include "buf0buf.h"
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
46
#include "trx0sys.h"
47 48
#include "trx0trx.h"
#include "log0recv.h"
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
49 50 51 52 53
#ifndef UNIV_HOTBACKUP
# include "os0sync.h"
# include "os0thread.h"
#else /* !UNIV_HOTBACKUP */
# ifdef __WIN__
54
/* Add includes for the _stat() call to compile on Windows */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
55 56 57 58 59
#  include <sys/types.h>
#  include <sys/stat.h>
#  include <errno.h>
# endif /* __WIN__ */
#endif /* !UNIV_HOTBACKUP */
60

Sergei Golubchik's avatar
Sergei Golubchik committed
61 62 63 64
#if defined(LINUX_NATIVE_AIO)
#include <libaio.h>
#endif

65 66 67 68
#ifdef _WIN32
#define IOCP_SHUTDOWN_KEY (ULONG_PTR)-1
#endif

69 70 71 72 73
/* This specifies the file permissions InnoDB uses when it creates files in
Unix; the value of os_innodb_umask is initialized in ha_innodb.cc to
my_umask */

#ifndef __WIN__
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
74
/** Umask for creating files */
75 76 77
UNIV_INTERN ulint	os_innodb_umask
			= S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
#else
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
78
/** Umask for creating files */
79 80 81 82 83 84 85 86 87 88 89
UNIV_INTERN ulint	os_innodb_umask		= 0;
#endif

#ifdef UNIV_DO_FLUSH
/* If the following is set to TRUE, we do not call os_file_flush in every
os_file_write. We can set this TRUE when the doublewrite buffer is used. */
UNIV_INTERN ibool	os_do_not_call_flush_at_each_write	= FALSE;
#else
/* We do not call os_file_flush in every os_file_write. */
#endif /* UNIV_DO_FLUSH */

Sergei Golubchik's avatar
Sergei Golubchik committed
90
#ifndef UNIV_HOTBACKUP
91 92 93 94 95 96 97 98
/* We use these mutexes to protect lseek + file i/o operation, if the
OS does not provide an atomic pread or pwrite, or similar */
#define OS_FILE_N_SEEK_MUTEXES	16
UNIV_INTERN os_mutex_t	os_file_seek_mutexes[OS_FILE_N_SEEK_MUTEXES];

/* In simulated aio, merge at most this many consecutive i/os */
#define OS_AIO_MERGE_N_CONSECUTIVE	64

Sergei Golubchik's avatar
Sergei Golubchik committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
/**********************************************************************

InnoDB AIO Implementation:
=========================

We support native AIO for windows and linux. For rest of the platforms
we simulate AIO by special io-threads servicing the IO-requests.

Simulated AIO:
==============

In platforms where we 'simulate' AIO following is a rough explanation
of the high level design.
There are four io-threads (for ibuf, log, read, write).
All synchronous IO requests are serviced by the calling thread using
os_file_write/os_file_read. The Asynchronous requests are queued up
in an array (there are four such arrays) by the calling thread. 
Later these requests are picked up by the io-thread and are serviced
synchronously.

Windows native AIO:
==================

If srv_use_native_aio is not set then windows follow the same
code as simulated AIO. If the flag is set then native AIO interface
is used. On windows, one of the limitation is that if a file is opened
for AIO no synchronous IO can be done on it. Therefore we have an
extra fifth array to queue up synchronous IO requests.
There are innodb_file_io_threads helper threads. These threads work
on the four arrays mentioned above in Simulated AIO. No thread is
required for the sync array.
If a synchronous IO request is made, it is first queued in the sync
array. Then the calling thread itself waits on the request, thus
making the call synchronous.
If an AIO request is made the calling thread not only queues it in the
array but also submits the requests. The helper thread then collects
the completed IO request and calls completion routine on it.

Linux native AIO:
=================

If we have libaio installed on the system and innodb_use_native_aio
is set to TRUE we follow the code path of native AIO, otherwise we
do simulated AIO.
There are innodb_file_io_threads helper threads. These threads work
on the four arrays mentioned above in Simulated AIO.
If a synchronous IO request is made, it is handled by calling
os_file_write/os_file_read.
If an AIO request is made the calling thread not only queues it in the
array but also submits the requests. The helper thread then collects
the completed IO request and calls completion routine on it.

**********************************************************************/
152

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
153
/** Flag: enable debug printout for asynchronous i/o */
154 155
UNIV_INTERN ibool	os_aio_print_debug	= FALSE;

Sergei Golubchik's avatar
Sergei Golubchik committed
156 157 158 159 160
#ifdef UNIV_PFS_IO
/* Keys to register InnoDB I/O with performance schema */
UNIV_INTERN mysql_pfs_key_t  innodb_file_data_key;
UNIV_INTERN mysql_pfs_key_t  innodb_file_log_key;
UNIV_INTERN mysql_pfs_key_t  innodb_file_temp_key;
161
UNIV_INTERN mysql_pfs_key_t  innodb_file_bmp_key;
Sergei Golubchik's avatar
Sergei Golubchik committed
162
#endif /* UNIV_PFS_IO */
Vadim Tkachenko's avatar
Vadim Tkachenko committed
163

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
164
/** The asynchronous i/o array slot structure */
165 166
typedef struct os_aio_slot_struct	os_aio_slot_t;

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
167
/** The asynchronous i/o array slot structure */
168
struct os_aio_slot_struct{
169 170 171 172 173 174
#ifdef WIN_ASYNC_IO
	OVERLAPPED	control;	/*!< Windows control block for the
					aio request, MUST be first element in the structure*/
	void *arr;				/*!< Array this slot belongs to*/
#endif

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
175 176
	ibool		is_read;	/*!< TRUE if a read operation */
	ulint		pos;		/*!< index of the slot in the aio
177
					array */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
178 179 180
	ibool		reserved;	/*!< TRUE if this slot is reserved */
	time_t		reservation_time;/*!< time when reserved */
	ulint		len;		/*!< length of the block to read or
181
					write */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
182 183 184
	byte*		buf;		/*!< buffer used in i/o */
	ulint		type;		/*!< OS_FILE_READ or OS_FILE_WRITE */
	ulint		offset;		/*!< 32 low bits of file offset in
185
					bytes */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
186 187 188
	ulint		offset_high;	/*!< 32 high bits of file offset */
	os_file_t	file;		/*!< file where to read or write */
	const char*	name;		/*!< file name or path */
Sergei Golubchik's avatar
Sergei Golubchik committed
189 190 191 192 193
	ibool		io_already_done;/*!< used only in simulated aio:
					TRUE if the physical i/o already
					made and only the slot message
					needs to be passed to the caller
					of os_aio_simulated_handle */
194
	ulint		space_id;
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
195 196
	fil_node_t*	message1;	/*!< message which is given by the */
	void*		message2;	/*!< the requester of an aio operation
197 198 199
					and which can be used to identify
					which pending aio operation was
					completed */
Sergei Golubchik's avatar
Sergei Golubchik committed
200
#ifdef LINUX_NATIVE_AIO
Sergei Golubchik's avatar
Sergei Golubchik committed
201 202 203
	struct iocb	control;	/* Linux control block for aio */
	int		n_bytes;	/* bytes written/read. */
	int		ret;		/* AIO return code */
204 205 206
#endif
};

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
207
/** The asynchronous i/o array structure */
208 209
typedef struct os_aio_array_struct	os_aio_array_t;

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
210
/** The asynchronous i/o array structure */
211
struct os_aio_array_struct{
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	os_mutex_t	mutex;	/*!< the mutex protecting the aio array */
	os_event_t	not_full;
				/*!< The event which is set to the
				signaled state when there is space in
				the aio outside the ibuf segment */
	os_event_t	is_empty;
				/*!< The event which is set to the
				signaled state when there are no
				pending i/os in this array */
	ulint		n_slots;/*!< Total number of slots in the aio
				array.  This must be divisible by
				n_threads. */
	ulint		n_segments;
				/*!< Number of segments in the aio
				array of pending aio requests. A
				thread can wait separately for any one
				of the segments. */
Sergei Golubchik's avatar
Sergei Golubchik committed
229 230 231 232
	ulint		cur_seg;/*!< We reserve IO requests in round
				robin fashion to different segments.
				This points to the segment that is to
				be used to service next IO request. */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
233 234 235 236
	ulint		n_reserved;
				/*!< Number of reserved slots in the
				aio array outside the ibuf segment */
	os_aio_slot_t*	slots;	/*!< Pointer to the slots in the array */
Sergei Golubchik's avatar
Sergei Golubchik committed
237 238 239 240 241 242 243 244 245 246 247 248

#if defined(LINUX_NATIVE_AIO)
	io_context_t*		aio_ctx;
				/* completion queue for IO. There is 
				one such queue per segment. Each thread
				will work on one ctx exclusively. */
	struct io_event*	aio_events;
				/* The array to collect completed IOs.
				There is one such event for each
				possible pending IO. The size of the
				array is equal to n_slots. */
#endif
249 250
};

Sergei Golubchik's avatar
Sergei Golubchik committed
251 252 253
#if defined(LINUX_NATIVE_AIO)
/** timeout for each io_getevents() call = 500ms. */
#define OS_AIO_REAP_TIMEOUT	(500000000UL)
254

Sergei Golubchik's avatar
Sergei Golubchik committed
255 256
/** time to sleep, in microseconds if io_setup() returns EAGAIN. */
#define OS_AIO_IO_SETUP_RETRY_SLEEP	(500000UL)
Vadim Tkachenko's avatar
Vadim Tkachenko committed
257

Sergei Golubchik's avatar
Sergei Golubchik committed
258 259 260 261 262 263
/** number of attempts before giving up on io_setup(). */
#define OS_AIO_IO_SETUP_RETRY_ATTEMPTS	5
#endif

/** Array of events used in simulated aio */
static os_event_t*	os_aio_segment_wait_events	= NULL;
Vadim Tkachenko's avatar
Vadim Tkachenko committed
264

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
265 266 267 268 269 270 271 272
/** The aio arrays for non-ibuf i/o and ibuf i/o, as well as sync aio. These
are NULL when the module has not yet been initialized. @{ */
static os_aio_array_t*	os_aio_read_array	= NULL;	/*!< Reads */
static os_aio_array_t*	os_aio_write_array	= NULL;	/*!< Writes */
static os_aio_array_t*	os_aio_ibuf_array	= NULL;	/*!< Insert buffer */
static os_aio_array_t*	os_aio_log_array	= NULL;	/*!< Redo log */
static os_aio_array_t*	os_aio_sync_array	= NULL;	/*!< Synchronous I/O */
/* @} */
273

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
274
/** Number of asynchronous I/O segments.  Set by os_aio_init(). */
275 276
static ulint	os_aio_n_segments	= ULINT_UNDEFINED;

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
277
/** If the following is TRUE, read i/o handler threads try to
278
wait until a batch of new read requests have been posted */
Sergei Golubchik's avatar
Sergei Golubchik committed
279 280
static ibool	os_aio_recommend_sleep_for_read_threads	= FALSE;
#endif /* !UNIV_HOTBACKUP */
281 282 283 284 285 286 287 288 289 290 291 292

UNIV_INTERN ulint	os_n_file_reads		= 0;
UNIV_INTERN ulint	os_bytes_read_since_printout = 0;
UNIV_INTERN ulint	os_n_file_writes	= 0;
UNIV_INTERN ulint	os_n_fsyncs		= 0;
UNIV_INTERN ulint	os_n_file_reads_old	= 0;
UNIV_INTERN ulint	os_n_file_writes_old	= 0;
UNIV_INTERN ulint	os_n_fsyncs_old		= 0;
UNIV_INTERN time_t	os_last_printout;

UNIV_INTERN ibool	os_has_said_disk_full	= FALSE;

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
293 294
#ifndef UNIV_HOTBACKUP
/** The mutex protecting the following counts of pending I/O operations */
295
static os_mutex_t	os_file_count_mutex;
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
296 297
#endif /* !UNIV_HOTBACKUP */
/** Number of pending os_file_pread() operations */
298
UNIV_INTERN ulint	os_file_n_pending_preads  = 0;
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
299
/** Number of pending os_file_pwrite() operations */
300
UNIV_INTERN ulint	os_file_n_pending_pwrites = 0;
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
301
/** Number of pending write operations */
302
UNIV_INTERN ulint	os_n_pending_writes = 0;
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
303
/** Number of pending read operations */
304 305
UNIV_INTERN ulint	os_n_pending_reads = 0;

Sergei Golubchik's avatar
Sergei Golubchik committed
306
#ifdef UNIV_DEBUG
307
# ifndef UNIV_HOTBACKUP
Sergei Golubchik's avatar
Sergei Golubchik committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
/**********************************************************************//**
Validates the consistency the aio system some of the time.
@return	TRUE if ok or the check was skipped */
UNIV_INTERN
ibool
os_aio_validate_skip(void)
/*======================*/
{
/** Try os_aio_validate() every this many times */
# define OS_AIO_VALIDATE_SKIP	13

	/** The os_aio_validate() call skip counter.
	Use a signed type because of the race condition below. */
	static int os_aio_validate_count = OS_AIO_VALIDATE_SKIP;

	/* There is a race condition below, but it does not matter,
	because this call is only for heuristic purposes. We want to
	reduce the call frequency of the costly os_aio_validate()
	check in debug builds. */
	if (--os_aio_validate_count > 0) {
		return(TRUE);
	}

	os_aio_validate_count = OS_AIO_VALIDATE_SKIP;
	return(os_aio_validate());
}
334
# endif /* !UNIV_HOTBACKUP */
Sergei Golubchik's avatar
Sergei Golubchik committed
335 336
#endif /* UNIV_DEBUG */

337 338 339
#ifdef _WIN32
/** IO completion port used by background io threads */
static HANDLE completion_port;
340 341
/** IO completion port used by background io READ threads */
static HANDLE read_completion_port;
342 343 344 345
/** Thread local storage index for the per-thread event used for synchronous IO */
static DWORD tls_sync_io = TLS_OUT_OF_INDEXES;
#endif

Sergei Golubchik's avatar
Sergei Golubchik committed
346
#ifdef __WIN__
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
347 348
/***********************************************************************//**
Gets the operating system version. Currently works only on Windows.
Sergei Golubchik's avatar
Sergei Golubchik committed
349 350
@return	OS_WIN95, OS_WIN31, OS_WINNT, OS_WIN2000, OS_WINXP, OS_WINVISTA,
OS_WIN7. */
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
UNIV_INTERN
ulint
os_get_os_version(void)
/*===================*/
{
	OSVERSIONINFO	  os_info;

	os_info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

	ut_a(GetVersionEx(&os_info));

	if (os_info.dwPlatformId == VER_PLATFORM_WIN32s) {
		return(OS_WIN31);
	} else if (os_info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
		return(OS_WIN95);
	} else if (os_info.dwPlatformId == VER_PLATFORM_WIN32_NT) {
Sergei Golubchik's avatar
Sergei Golubchik committed
367 368 369 370 371 372 373 374 375 376 377 378
		switch (os_info.dwMajorVersion) {
		case 3:
		case 4:
			return OS_WINNT;
		case 5:
			return (os_info.dwMinorVersion == 0) ? OS_WIN2000
							     : OS_WINXP;
		case 6:
			return (os_info.dwMinorVersion == 0) ? OS_WINVISTA
							     : OS_WIN7;
		default:
			return OS_WIN7;
379 380 381 382 383 384
		}
	} else {
		ut_error;
		return(0);
	}
}
Sergei Golubchik's avatar
Sergei Golubchik committed
385
#endif /* __WIN__ */
386

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466

#ifdef _WIN32
/*
Windows : Handling synchronous IO on files opened asynchronously.

If file is opened for asynchronous IO (FILE_FLAG_OVERLAPPED) and also bound to 
a completion port, then every IO on this file would normally be enqueued to the
completion port. Sometimes however we would like to do a synchronous IO. This is
possible if we initialitze have overlapped.hEvent with a valid event and set its
lowest order bit to 1 (see MSDN ReadFile and WriteFile description for more info)

We'll create this special event once for each thread and store in thread local 
storage.
*/


/***********************************************************************//**
Initialize tls index.for event handle used for synchronized IO on files that 
might be opened with FILE_FLAG_OVERLAPPED.
*/
static void win_init_syncio_event()
{
	tls_sync_io = TlsAlloc();
	ut_a(tls_sync_io != TLS_OUT_OF_INDEXES);
}

/***********************************************************************//**
Retrieve per-thread event for doing synchronous io on asyncronously opened files
*/
static HANDLE win_get_syncio_event()
{
	HANDLE h;
	if(tls_sync_io == TLS_OUT_OF_INDEXES){
		win_init_syncio_event();
	}

	h = (HANDLE)TlsGetValue(tls_sync_io);
	if (h)
		return h;
	h = CreateEventA(NULL, FALSE, FALSE, NULL);
	ut_a(h);
	h = (HANDLE)((uintptr_t)h | 1);
	TlsSetValue(tls_sync_io, h);
	return h;
}

/*
  TLS destructor, inspired by Chromium code
  http://src.chromium.org/svn/trunk/src/base/threading/thread_local_storage_win.cc
*/

static void win_free_syncio_event()
{
	HANDLE h = win_get_syncio_event();
	if (h) {
		CloseHandle(h);
	}
}

static void NTAPI win_tls_thread_exit(PVOID module, DWORD reason, PVOID reserved) {
	if (DLL_THREAD_DETACH == reason || DLL_PROCESS_DETACH == reason)
		win_free_syncio_event();
}

#ifdef _WIN64
#pragma comment(linker, "/INCLUDE:_tls_used")
#pragma comment(linker, "/INCLUDE:p_thread_callback_base")
#pragma const_seg(".CRT$XLB")
extern const PIMAGE_TLS_CALLBACK p_thread_callback_base;
const PIMAGE_TLS_CALLBACK p_thread_callback_base = win_tls_thread_exit;
#pragma data_seg()
#else
#pragma comment(linker, "/INCLUDE:__tls_used")
#pragma comment(linker, "/INCLUDE:_p_thread_callback_base")
#pragma data_seg(".CRT$XLB")
PIMAGE_TLS_CALLBACK p_thread_callback_base = win_tls_thread_exit;
#pragma data_seg()
#endif 
#endif /*_WIN32 */

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
467
/***********************************************************************//**
468 469 470
Retrieves the last error number if an error occurs in a file io function.
The number should be retrieved before any other OS calls (because they may
overwrite the error number). If the number is not known to this program,
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
471 472
the OS error number + 100 is returned.
@return	error number, or OS error number + 100 */
473 474 475 476
UNIV_INTERN
ulint
os_file_get_last_error(
/*===================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
477
	ibool	report_all_errors)	/*!< in: TRUE if we want an error message
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
					printed of all errors */
{
	ulint	err;

#ifdef __WIN__

	err = (ulint) GetLastError();

	if (report_all_errors
	    || (err != ERROR_DISK_FULL && err != ERROR_FILE_EXISTS)) {

		ut_print_timestamp(stderr);
		fprintf(stderr,
			"  InnoDB: Operating system error number %lu"
			" in a file operation.\n", (ulong) err);

		if (err == ERROR_PATH_NOT_FOUND) {
			fprintf(stderr,
				"InnoDB: The error means the system"
				" cannot find the path specified.\n");

			if (srv_is_being_started) {
				fprintf(stderr,
					"InnoDB: If you are installing InnoDB,"
					" remember that you must create\n"
					"InnoDB: directories yourself, InnoDB"
					" does not create them.\n");
			}
		} else if (err == ERROR_ACCESS_DENIED) {
			fprintf(stderr,
				"InnoDB: The error means mysqld does not have"
				" the access rights to\n"
				"InnoDB: the directory. It may also be"
				" you have created a subdirectory\n"
				"InnoDB: of the same name as a data file.\n");
		} else if (err == ERROR_SHARING_VIOLATION
			   || err == ERROR_LOCK_VIOLATION) {
			fprintf(stderr,
				"InnoDB: The error means that another program"
				" is using InnoDB's files.\n"
				"InnoDB: This might be a backup or antivirus"
				" software or another instance\n"
				"InnoDB: of MySQL."
				" Please close it to get rid of this error.\n");
522 523 524 525 526 527 528 529 530 531 532 533 534
		} else if (err == ERROR_WORKING_SET_QUOTA
			   || err == ERROR_NO_SYSTEM_RESOURCES) {
			fprintf(stderr,
				"InnoDB: The error means that there are no"
				" sufficient system resources or quota to"
				" complete the operation.\n");
		} else if (err == ERROR_OPERATION_ABORTED) {
			fprintf(stderr,
				"InnoDB: The error means that the I/O"
				" operation has been aborted\n"
				"InnoDB: because of either a thread exit"
				" or an application request.\n"
				"InnoDB: Retry attempt is made.\n");
535 536 537 538 539
		} else {
			fprintf(stderr,
				"InnoDB: Some operating system error numbers"
				" are described at\n"
				"InnoDB: "
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
540
				REFMAN
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
				"operating-system-error-codes.html\n");
		}
	}

	fflush(stderr);

	if (err == ERROR_FILE_NOT_FOUND) {
		return(OS_FILE_NOT_FOUND);
	} else if (err == ERROR_DISK_FULL) {
		return(OS_FILE_DISK_FULL);
	} else if (err == ERROR_FILE_EXISTS) {
		return(OS_FILE_ALREADY_EXISTS);
	} else if (err == ERROR_SHARING_VIOLATION
		   || err == ERROR_LOCK_VIOLATION) {
		return(OS_FILE_SHARING_VIOLATION);
556 557 558 559 560
	} else if (err == ERROR_WORKING_SET_QUOTA
		   || err == ERROR_NO_SYSTEM_RESOURCES) {
		return(OS_FILE_INSUFFICIENT_RESOURCE);
	} else if (err == ERROR_OPERATION_ABORTED) {
		return(OS_FILE_OPERATION_ABORTED);
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
	} else {
		return(100 + err);
	}
#else
	err = (ulint) errno;

	if (report_all_errors
	    || (err != ENOSPC && err != EEXIST)) {

		ut_print_timestamp(stderr);
		fprintf(stderr,
			"  InnoDB: Operating system error number %lu"
			" in a file operation.\n", (ulong) err);

		if (err == ENOENT) {
			fprintf(stderr,
				"InnoDB: The error means the system"
				" cannot find the path specified.\n");

			if (srv_is_being_started) {
				fprintf(stderr,
					"InnoDB: If you are installing InnoDB,"
					" remember that you must create\n"
					"InnoDB: directories yourself, InnoDB"
					" does not create them.\n");
			}
		} else if (err == EACCES) {
			fprintf(stderr,
				"InnoDB: The error means mysqld does not have"
				" the access rights to\n"
				"InnoDB: the directory.\n");
		} else {
			if (strerror((int)err) != NULL) {
				fprintf(stderr,
					"InnoDB: Error number %lu"
					" means '%s'.\n",
					err, strerror((int)err));
			}

			fprintf(stderr,
				"InnoDB: Some operating system"
				" error numbers are described at\n"
				"InnoDB: "
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
604
				REFMAN
605 606 607 608 609 610
				"operating-system-error-codes.html\n");
		}
	}

	fflush(stderr);

Sergei Golubchik's avatar
Sergei Golubchik committed
611 612
	switch (err) {
	case ENOSPC:
613
		return(OS_FILE_DISK_FULL);
Sergei Golubchik's avatar
Sergei Golubchik committed
614
	case ENOENT:
615
		return(OS_FILE_NOT_FOUND);
Sergei Golubchik's avatar
Sergei Golubchik committed
616
	case EEXIST:
617
		return(OS_FILE_ALREADY_EXISTS);
Sergei Golubchik's avatar
Sergei Golubchik committed
618 619 620
	case EXDEV:
	case ENOTDIR:
	case EISDIR:
621
		return(OS_FILE_PATH_ERROR);
Sergei Golubchik's avatar
Sergei Golubchik committed
622 623 624 625 626 627 628 629 630 631
	case EAGAIN:
		if (srv_use_native_aio) {
			return(OS_FILE_AIO_RESOURCES_RESERVED);
		}
		break;
	case EINTR:
		if (srv_use_native_aio) {
			return(OS_FILE_AIO_INTERRUPTED);
		}
		break;
632
	}
Sergei Golubchik's avatar
Sergei Golubchik committed
633
	return(100 + err);
634 635 636
#endif
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
637
/****************************************************************//**
638 639
Does error handling when a file operation fails.
Conditionally exits (calling exit(3)) based on should_exit value and the
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
640 641
error type
@return	TRUE if we should retry the operation */
642 643 644 645
static
ibool
os_file_handle_error_cond_exit(
/*===========================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
646 647 648
	const char*	name,		/*!< in: name of a file or NULL */
	const char*	operation,	/*!< in: operation */
	ibool		should_exit)	/*!< in: call exit(3) if unknown error
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
					and this parameter is TRUE */
{
	ulint	err;

	err = os_file_get_last_error(FALSE);

	if (err == OS_FILE_DISK_FULL) {
		/* We only print a warning about disk full once */

		if (os_has_said_disk_full) {

			return(FALSE);
		}

		if (name) {
			ut_print_timestamp(stderr);
			fprintf(stderr,
				"  InnoDB: Encountered a problem with"
				" file %s\n", name);
		}

		ut_print_timestamp(stderr);
		fprintf(stderr,
			"  InnoDB: Disk is full. Try to clean the disk"
			" to free space.\n");

		os_has_said_disk_full = TRUE;

		fflush(stderr);

		return(FALSE);
	} else if (err == OS_FILE_AIO_RESOURCES_RESERVED) {

Sergei Golubchik's avatar
Sergei Golubchik committed
682 683 684
		return(TRUE);
	} else if (err == OS_FILE_AIO_INTERRUPTED) {

685 686 687 688 689 690 691 692 693
		return(TRUE);
	} else if (err == OS_FILE_ALREADY_EXISTS
		   || err == OS_FILE_PATH_ERROR) {

		return(FALSE);
	} else if (err == OS_FILE_SHARING_VIOLATION) {

		os_thread_sleep(10000000);  /* 10 sec */
		return(TRUE);
694 695 696 697 698 699 700 701
	} else if (err == OS_FILE_INSUFFICIENT_RESOURCE) {

		os_thread_sleep(100000);	/* 100 ms */
		return(TRUE);
	} else if (err == OS_FILE_OPERATION_ABORTED) {

		os_thread_sleep(100000);	/* 100 ms */
		return(TRUE);
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
	} else {
		if (name) {
			fprintf(stderr, "InnoDB: File name %s\n", name);
		}

		fprintf(stderr, "InnoDB: File operation call: '%s'.\n",
			operation);

		if (should_exit) {
			fprintf(stderr, "InnoDB: Cannot continue operation.\n");

			fflush(stderr);

			exit(1);
		}
	}

	return(FALSE);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
722 723 724
/****************************************************************//**
Does error handling when a file operation fails.
@return	TRUE if we should retry the operation */
725 726 727 728
static
ibool
os_file_handle_error(
/*=================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
729 730
	const char*	name,	/*!< in: name of a file or NULL */
	const char*	operation)/*!< in: operation */
731 732 733 734 735
{
	/* exit in case of unknown error */
	return(os_file_handle_error_cond_exit(name, operation, TRUE));
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
736 737 738
/****************************************************************//**
Does error handling when a file operation fails.
@return	TRUE if we should retry the operation */
739 740 741 742
static
ibool
os_file_handle_error_no_exit(
/*=========================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
743 744
	const char*	name,	/*!< in: name of a file or NULL */
	const char*	operation)/*!< in: operation */
745 746 747 748 749 750 751
{
	/* don't exit in case of unknown error */
	return(os_file_handle_error_cond_exit(name, operation, FALSE));
}

#undef USE_FILE_LOCK
#define USE_FILE_LOCK
Sergei Golubchik's avatar
Sergei Golubchik committed
752
#if defined(UNIV_HOTBACKUP) || defined(__WIN__)
753 754 755 756 757 758
/* InnoDB Hot Backup does not lock the data files.
 * On Windows, mandatory locking is used.
 */
# undef USE_FILE_LOCK
#endif
#ifdef USE_FILE_LOCK
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
759 760 761
/****************************************************************//**
Obtain an exclusive lock on a file.
@return	0 on success */
762 763 764 765
static
int
os_file_lock(
/*=========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
766 767
	int		fd,	/*!< in: file descriptor */
	const char*	name)	/*!< in: file name */
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
{
	struct flock lk;
	lk.l_type = F_WRLCK;
	lk.l_whence = SEEK_SET;
	lk.l_start = lk.l_len = 0;
	if (fcntl(fd, F_SETLK, &lk) == -1) {
		fprintf(stderr,
			"InnoDB: Unable to lock %s, error: %d\n", name, errno);

		if (errno == EAGAIN || errno == EACCES) {
			fprintf(stderr,
				"InnoDB: Check that you do not already have"
				" another mysqld process\n"
				"InnoDB: using the same InnoDB data"
				" or log files.\n");
		}

		return(-1);
	}

	return(0);
}
#endif /* USE_FILE_LOCK */

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
792 793
#ifndef UNIV_HOTBACKUP
/****************************************************************//**
794 795 796 797 798 799 800 801
Creates the seek mutexes used in positioned reads and writes. */
UNIV_INTERN
void
os_io_init_simple(void)
/*===================*/
{
	ulint	i;

Sergei Golubchik's avatar
Sergei Golubchik committed
802
	os_file_count_mutex = os_mutex_create();
803 804

	for (i = 0; i < OS_FILE_N_SEEK_MUTEXES; i++) {
Sergei Golubchik's avatar
Sergei Golubchik committed
805
		os_file_seek_mutexes[i] = os_mutex_create();
806
	}
807 808 809
#ifdef _WIN32
	win_init_syncio_event();
#endif
810 811
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
812
/***********************************************************************//**
813 814
Creates a temporary file.  This function is like tmpfile(3), but
the temporary file is created in the MySQL temporary directory.
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
815
@return	temporary file handle, or NULL on error */
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
UNIV_INTERN
FILE*
os_file_create_tmpfile(void)
/*========================*/
{
	FILE*	file	= NULL;
	int	fd	= innobase_mysql_tmpfile();

	if (fd >= 0) {
		file = fdopen(fd, "w+b");
	}

	if (!file) {
		ut_print_timestamp(stderr);
		fprintf(stderr,
			"  InnoDB: Error: unable to create temporary file;"
			" errno: %d\n", errno);
		if (fd >= 0) {
			close(fd);
		}
	}

	return(file);
}
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
840
#endif /* !UNIV_HOTBACKUP */
841

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
842
/***********************************************************************//**
843 844 845
The os_file_opendir() function opens a directory stream corresponding to the
directory named by the dirname argument. The directory stream is positioned
at the first entry. In both Unix and Windows we automatically skip the '.'
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
846 847
and '..' items at the start of the directory listing.
@return	directory stream, NULL if error */
848 849 850 851
UNIV_INTERN
os_file_dir_t
os_file_opendir(
/*============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
852
	const char*	dirname,	/*!< in: directory name; it must not
853
					contain a trailing '\' or '/' */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
854
	ibool		error_is_fatal)	/*!< in: TRUE if we should treat an
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
					error as a fatal error; if we try to
					open symlinks then we do not wish a
					fatal error if it happens not to be
					a directory */
{
	os_file_dir_t		dir;
#ifdef __WIN__
	LPWIN32_FIND_DATA	lpFindFileData;
	char			path[OS_FILE_MAX_PATH + 3];

	ut_a(strlen(dirname) < OS_FILE_MAX_PATH);

	strcpy(path, dirname);
	strcpy(path + strlen(path), "\\*");

	/* Note that in Windows opening the 'directory stream' also retrieves
	the first entry in the directory. Since it is '.', that is no problem,
	as we will skip over the '.' and '..' entries anyway. */

	lpFindFileData = ut_malloc(sizeof(WIN32_FIND_DATA));

	dir = FindFirstFile((LPCTSTR) path, lpFindFileData);

	ut_free(lpFindFileData);

	if (dir == INVALID_HANDLE_VALUE) {

		if (error_is_fatal) {
			os_file_handle_error(dirname, "opendir");
		}

		return(NULL);
	}

	return(dir);
#else
	dir = opendir(dirname);

	if (dir == NULL && error_is_fatal) {
		os_file_handle_error(dirname, "opendir");
	}

	return(dir);
#endif
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
901 902 903
/***********************************************************************//**
Closes a directory stream.
@return	0 if success, -1 if failure */
904 905 906 907
UNIV_INTERN
int
os_file_closedir(
/*=============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
908
	os_file_dir_t	dir)	/*!< in: directory stream */
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
{
#ifdef __WIN__
	BOOL		ret;

	ret = FindClose(dir);

	if (!ret) {
		os_file_handle_error_no_exit(NULL, "closedir");

		return(-1);
	}

	return(0);
#else
	int	ret;

	ret = closedir(dir);

	if (ret) {
		os_file_handle_error_no_exit(NULL, "closedir");
	}

	return(ret);
#endif
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
935
/***********************************************************************//**
936
This function returns information of the next file in the directory. We jump
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
937 938
over the '.' and '..' entries in the directory.
@return	0 if ok, -1 if error, 1 if at the end of the directory */
939 940 941 942
UNIV_INTERN
int
os_file_readdir_next_file(
/*======================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
943 944 945
	const char*	dirname,/*!< in: directory name or path */
	os_file_dir_t	dir,	/*!< in: directory stream */
	os_file_stat_t*	info)	/*!< in/out: buffer where the info is returned */
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
{
#ifdef __WIN__
	LPWIN32_FIND_DATA	lpFindFileData;
	BOOL			ret;

	lpFindFileData = ut_malloc(sizeof(WIN32_FIND_DATA));
next_file:
	ret = FindNextFile(dir, lpFindFileData);

	if (ret) {
		ut_a(strlen((char *) lpFindFileData->cFileName)
		     < OS_FILE_MAX_PATH);

		if (strcmp((char *) lpFindFileData->cFileName, ".") == 0
		    || strcmp((char *) lpFindFileData->cFileName, "..") == 0) {

			goto next_file;
		}

		strcpy(info->name, (char *) lpFindFileData->cFileName);

		info->size = (ib_int64_t)(lpFindFileData->nFileSizeLow)
			+ (((ib_int64_t)(lpFindFileData->nFileSizeHigh))
			   << 32);

		if (lpFindFileData->dwFileAttributes
		    & FILE_ATTRIBUTE_REPARSE_POINT) {
			/* TODO: test Windows symlinks */
			/* TODO: MySQL has apparently its own symlink
			implementation in Windows, dbname.sym can
			redirect a database directory:
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
977
			REFMAN "windows-symbolic-links.html" */
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
			info->type = OS_FILE_TYPE_LINK;
		} else if (lpFindFileData->dwFileAttributes
			   & FILE_ATTRIBUTE_DIRECTORY) {
			info->type = OS_FILE_TYPE_DIR;
		} else {
			/* It is probably safest to assume that all other
			file types are normal. Better to check them rather
			than blindly skip them. */

			info->type = OS_FILE_TYPE_FILE;
		}
	}

	ut_free(lpFindFileData);

	if (ret) {
		return(0);
	} else if (GetLastError() == ERROR_NO_MORE_FILES) {

		return(1);
	} else {
		os_file_handle_error_no_exit(dirname,
					     "readdir_next_file");
		return(-1);
	}
#else
	struct dirent*	ent;
	char*		full_path;
	int		ret;
	struct stat	statinfo;
#ifdef HAVE_READDIR_R
	char		dirent_buf[sizeof(struct dirent)
				   + _POSIX_PATH_MAX + 100];
	/* In /mysys/my_lib.c, _POSIX_PATH_MAX + 1 is used as
	the max file name len; but in most standards, the
	length is NAME_MAX; we add 100 to be even safer */
#endif

next_file:

#ifdef HAVE_READDIR_R
	ret = readdir_r(dir, (struct dirent*)dirent_buf, &ent);

1021 1022 1023 1024 1025 1026 1027 1028 1029
	if (ret != 0
#ifdef UNIV_AIX
	    /* On AIX, only if we got non-NULL 'ent' (result) value and
	    a non-zero 'ret' (return) value, it indicates a failed
	    readdir_r() call. An NULL 'ent' with an non-zero 'ret'
	    would indicate the "end of the directory" is reached. */
	    && ent != NULL
#endif
	   ) {
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
		fprintf(stderr,
			"InnoDB: cannot read directory %s, error %lu\n",
			dirname, (ulong)ret);

		return(-1);
	}

	if (ent == NULL) {
		/* End of directory */

		return(1);
	}

	ut_a(strlen(ent->d_name) < _POSIX_PATH_MAX + 100 - 1);
#else
	ent = readdir(dir);

	if (ent == NULL) {

		return(1);
	}
#endif
	ut_a(strlen(ent->d_name) < OS_FILE_MAX_PATH);

	if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {

		goto next_file;
	}

	strcpy(info->name, ent->d_name);

	full_path = ut_malloc(strlen(dirname) + strlen(ent->d_name) + 10);

	sprintf(full_path, "%s/%s", dirname, ent->d_name);

	ret = stat(full_path, &statinfo);

	if (ret) {
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084

		if (errno == ENOENT) {
			/* readdir() returned a file that does not exist,
			it must have been deleted in the meantime. Do what
			would have happened if the file was deleted before
			readdir() - ignore and go to the next entry.
			If this is the last entry then info->name will still
			contain the name of the deleted file when this
			function returns, but this is not an issue since the
			caller shouldn't be looking at info when end of
			directory is returned. */

			ut_free(full_path);

			goto next_file;
		}

1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
		os_file_handle_error_no_exit(full_path, "stat");

		ut_free(full_path);

		return(-1);
	}

	info->size = (ib_int64_t)statinfo.st_size;

	if (S_ISDIR(statinfo.st_mode)) {
		info->type = OS_FILE_TYPE_DIR;
	} else if (S_ISLNK(statinfo.st_mode)) {
		info->type = OS_FILE_TYPE_LINK;
	} else if (S_ISREG(statinfo.st_mode)) {
		info->type = OS_FILE_TYPE_FILE;
	} else {
		info->type = OS_FILE_TYPE_UNKNOWN;
	}

	ut_free(full_path);

	return(0);
#endif
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1110
/*****************************************************************//**
1111 1112 1113
This function attempts to create a directory named pathname. The new directory
gets default permissions. On Unix the permissions are (0770 & ~umask). If the
directory exists already, nothing is done and the call succeeds, unless the
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1114 1115
fail_if_exists arguments is true.
@return	TRUE if call succeeds, FALSE on error */
1116 1117 1118 1119
UNIV_INTERN
ibool
os_file_create_directory(
/*=====================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1120
	const char*	pathname,	/*!< in: directory name as
1121
					null-terminated string */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1122
	ibool		fail_if_exists)	/*!< in: if TRUE, pre-existing directory
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
					is treated as an error. */
{
#ifdef __WIN__
	BOOL	rcode;

	rcode = CreateDirectory((LPCTSTR) pathname, NULL);
	if (!(rcode != 0
	      || (GetLastError() == ERROR_ALREADY_EXISTS
		  && !fail_if_exists))) {
		/* failure */
		os_file_handle_error(pathname, "CreateDirectory");

		return(FALSE);
	}

	return (TRUE);
#else
	int	rcode;

	rcode = mkdir(pathname, 0770);

	if (!(rcode == 0 || (errno == EEXIST && !fail_if_exists))) {
		/* failure */
		os_file_handle_error(pathname, "mkdir");

		return(FALSE);
	}

	return (TRUE);
#endif
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1155
/****************************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
1156 1157
NOTE! Use the corresponding macro os_file_create_simple(), not directly
this function!
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1158 1159 1160
A simple function to open or create a file.
@return own: handle to the file, not defined if error, error number
can be retrieved with os_file_get_last_error */
1161 1162
UNIV_INTERN
os_file_t
Sergei Golubchik's avatar
Sergei Golubchik committed
1163 1164
os_file_create_simple_func(
/*=======================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1165
	const char*	name,	/*!< in: name of the file or path as a
1166
				null-terminated string */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1167
	ulint		create_mode,/*!< in: OS_FILE_OPEN if an existing file is
1168 1169 1170 1171 1172 1173
				opened (if does not exist, error), or
				OS_FILE_CREATE if a new file is created
				(if exists, error), or
				OS_FILE_CREATE_PATH if new file
				(if exists, error) and subdirectories along
				its path are created (if needed)*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1174
	ulint		access_type,/*!< in: OS_FILE_READ_ONLY or
1175
				OS_FILE_READ_WRITE */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1176
	ibool*		success)/*!< out: TRUE if succeed, FALSE if error */
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
{
#ifdef __WIN__
	os_file_t	file;
	DWORD		create_flag;
	DWORD		access;
	DWORD		attributes	= 0;
	ibool		retry;

try_again:
	ut_a(name);

	if (create_mode == OS_FILE_OPEN) {
		create_flag = OPEN_EXISTING;
	} else if (create_mode == OS_FILE_CREATE) {
		create_flag = CREATE_NEW;
	} else if (create_mode == OS_FILE_CREATE_PATH) {
		/* create subdirs along the path if needed  */
		*success = os_file_create_subdirs_if_needed(name);
		if (!*success) {
			ut_error;
		}
		create_flag = CREATE_NEW;
		create_mode = OS_FILE_CREATE;
	} else {
		create_flag = 0;
		ut_error;
	}

	if (access_type == OS_FILE_READ_ONLY) {
		access = GENERIC_READ;
	} else if (access_type == OS_FILE_READ_WRITE) {
		access = GENERIC_READ | GENERIC_WRITE;
	} else {
		access = 0;
		ut_error;
	}

	file = CreateFile((LPCTSTR) name,
			  access,
			  FILE_SHARE_READ | FILE_SHARE_WRITE,
			  /* file can be read and written also
			  by other processes */
			  NULL,	/* default security attributes */
			  create_flag,
			  attributes,
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1222
			  NULL);	/*!< no template file */
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297

	if (file == INVALID_HANDLE_VALUE) {
		*success = FALSE;

		retry = os_file_handle_error(name,
					     create_mode == OS_FILE_OPEN ?
					     "open" : "create");
		if (retry) {
			goto try_again;
		}
	} else {
		*success = TRUE;
	}

	return(file);
#else /* __WIN__ */
	os_file_t	file;
	int		create_flag;
	ibool		retry;

try_again:
	ut_a(name);

	if (create_mode == OS_FILE_OPEN) {
		if (access_type == OS_FILE_READ_ONLY) {
			create_flag = O_RDONLY;
		} else {
			create_flag = O_RDWR;
		}
	} else if (create_mode == OS_FILE_CREATE) {
		create_flag = O_RDWR | O_CREAT | O_EXCL;
	} else if (create_mode == OS_FILE_CREATE_PATH) {
		/* create subdirs along the path if needed  */
		*success = os_file_create_subdirs_if_needed(name);
		if (!*success) {
			return (-1);
		}
		create_flag = O_RDWR | O_CREAT | O_EXCL;
		create_mode = OS_FILE_CREATE;
	} else {
		create_flag = 0;
		ut_error;
	}

	if (create_mode == OS_FILE_CREATE) {
		file = open(name, create_flag, S_IRUSR | S_IWUSR
			    | S_IRGRP | S_IWGRP);
	} else {
		file = open(name, create_flag);
	}

	if (file == -1) {
		*success = FALSE;

		retry = os_file_handle_error(name,
					     create_mode == OS_FILE_OPEN ?
					     "open" : "create");
		if (retry) {
			goto try_again;
		}
#ifdef USE_FILE_LOCK
	} else if (access_type == OS_FILE_READ_WRITE
		   && os_file_lock(file, name)) {
		*success = FALSE;
		close(file);
		file = -1;
#endif
	} else {
		*success = TRUE;
	}

	return(file);
#endif /* __WIN__ */
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1298
/****************************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
1299 1300
NOTE! Use the corresponding macro
os_file_create_simple_no_error_handling(), not directly this function!
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1301 1302 1303
A simple function to open or create a file.
@return own: handle to the file, not defined if error, error number
can be retrieved with os_file_get_last_error */
1304 1305
UNIV_INTERN
os_file_t
Sergei Golubchik's avatar
Sergei Golubchik committed
1306 1307
os_file_create_simple_no_error_handling_func(
/*=========================================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1308
	const char*	name,	/*!< in: name of the file or path as a
1309
				null-terminated string */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1310
	ulint		create_mode,/*!< in: OS_FILE_OPEN if an existing file
1311 1312 1313
				is opened (if does not exist, error), or
				OS_FILE_CREATE if a new file is created
				(if exists, error) */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1314
	ulint		access_type,/*!< in: OS_FILE_READ_ONLY,
1315 1316 1317
				OS_FILE_READ_WRITE, or
				OS_FILE_READ_ALLOW_DELETE; the last option is
				used by a backup program reading the file */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1318
	ibool*		success)/*!< out: TRUE if succeed, FALSE if error */
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
{
#ifdef __WIN__
	os_file_t	file;
	DWORD		create_flag;
	DWORD		access;
	DWORD		attributes	= 0;
	DWORD		share_mode	= FILE_SHARE_READ | FILE_SHARE_WRITE;

	ut_a(name);

	if (create_mode == OS_FILE_OPEN) {
		create_flag = OPEN_EXISTING;
	} else if (create_mode == OS_FILE_CREATE) {
		create_flag = CREATE_NEW;
	} else {
		create_flag = 0;
		ut_error;
	}

	if (access_type == OS_FILE_READ_ONLY) {
		access = GENERIC_READ;
	} else if (access_type == OS_FILE_READ_WRITE) {
		access = GENERIC_READ | GENERIC_WRITE;
	} else if (access_type == OS_FILE_READ_ALLOW_DELETE) {
		access = GENERIC_READ;
		share_mode = FILE_SHARE_DELETE | FILE_SHARE_READ
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1345
			| FILE_SHARE_WRITE;	/*!< A backup program has to give
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
						mysqld the maximum freedom to
						do what it likes with the
						file */
	} else {
		access = 0;
		ut_error;
	}

	file = CreateFile((LPCTSTR) name,
			  access,
			  share_mode,
			  NULL,	/* default security attributes */
			  create_flag,
			  attributes,
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1360
			  NULL);	/*!< no template file */
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411

	if (file == INVALID_HANDLE_VALUE) {
		*success = FALSE;
	} else {
		*success = TRUE;
	}

	return(file);
#else /* __WIN__ */
	os_file_t	file;
	int		create_flag;

	ut_a(name);

	if (create_mode == OS_FILE_OPEN) {
		if (access_type == OS_FILE_READ_ONLY) {
			create_flag = O_RDONLY;
		} else {
			create_flag = O_RDWR;
		}
	} else if (create_mode == OS_FILE_CREATE) {
		create_flag = O_RDWR | O_CREAT | O_EXCL;
	} else {
		create_flag = 0;
		ut_error;
	}

	if (create_mode == OS_FILE_CREATE) {
		file = open(name, create_flag, S_IRUSR | S_IWUSR
			    | S_IRGRP | S_IWGRP);
	} else {
		file = open(name, create_flag);
	}

	if (file == -1) {
		*success = FALSE;
#ifdef USE_FILE_LOCK
	} else if (access_type == OS_FILE_READ_WRITE
		   && os_file_lock(file, name)) {
		*success = FALSE;
		close(file);
		file = -1;
#endif
	} else {
		*success = TRUE;
	}

	return(file);
#endif /* __WIN__ */
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1412
/****************************************************************//**
1413 1414 1415 1416 1417
Tries to disable OS caching on an opened file descriptor. */
UNIV_INTERN
void
os_file_set_nocache(
/*================*/
1418 1419 1420 1421 1422 1423
	int		fd		/*!< in: file descriptor to alter */
	__attribute__((unused)),
	const char*	file_name	/*!< in: used in the diagnostic message */
	__attribute__((unused)),
	const char*	operation_name __attribute__((unused)))
					/*!< in: "open" or "create"; used in the
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
					diagnostic message */
{
	/* some versions of Solaris may not have DIRECTIO_ON */
#if defined(UNIV_SOLARIS) && defined(DIRECTIO_ON)
	if (directio(fd, DIRECTIO_ON) == -1) {
		int	errno_save;
		errno_save = (int)errno;
		ut_print_timestamp(stderr);
		fprintf(stderr,
			"  InnoDB: Failed to set DIRECTIO_ON "
			"on file %s: %s: %s, continuing anyway\n",
			file_name, operation_name, strerror(errno_save));
	}
#elif defined(O_DIRECT)
	if (fcntl(fd, F_SETFL, O_DIRECT) == -1) {
		int	errno_save;
		errno_save = (int)errno;
		ut_print_timestamp(stderr);
		fprintf(stderr,
			"  InnoDB: Failed to set O_DIRECT "
			"on file %s: %s: %s, continuing anyway\n",
			file_name, operation_name, strerror(errno_save));
		if (errno_save == EINVAL) {
			ut_print_timestamp(stderr);
			fprintf(stderr,
				"  InnoDB: O_DIRECT is known to result in "
				"'Invalid argument' on Linux on tmpfs, "
				"see MySQL Bug#26662\n");
		}
	}
#endif
}

1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477

#ifdef __linux__
#include <sys/ioctl.h>
#ifndef DFS_IOCTL_ATOMIC_WRITE_SET 
#define DFS_IOCTL_ATOMIC_WRITE_SET _IOW(0x95, 2, uint)
#endif
static int os_file_set_atomic_writes(os_file_t file, const char *name) 
{
	int atomic_option = 1;

	int ret = ioctl (file, DFS_IOCTL_ATOMIC_WRITE_SET, &atomic_option);

	if (ret) {
		fprintf(stderr, 
		"InnoDB : can't use atomic write on %s, errno %d\n",
		name, errno);
		return ret;
	}
	return ret;
}
#else 
1478 1479
static int os_file_set_atomic_writes(os_file_t file __attribute__ ((unused)),
                                     const char *name) 
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
{
	fprintf(stderr,
	"InnoDB : can't use atomic writes on %s - not implemented on this platform."
	"innodb_use_atomic_writes needs to be 0.\n", 
	name);
#ifdef _WIN32
	SetLastError(ERROR_INVALID_FUNCTION);
#else
	errno = EINVAL;
#endif
	return -1;
}
#endif

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1494
/****************************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
1495 1496
NOTE! Use the corresponding macro os_file_create(), not directly
this function!
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1497 1498 1499
Opens an existing file or creates a new.
@return own: handle to the file, not defined if error, error number
can be retrieved with os_file_get_last_error */
1500 1501
UNIV_INTERN
os_file_t
Sergei Golubchik's avatar
Sergei Golubchik committed
1502 1503
os_file_create_func(
/*================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1504
	const char*	name,	/*!< in: name of the file or path as a
1505
				null-terminated string */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1506
	ulint		create_mode,/*!< in: OS_FILE_OPEN if an existing file
1507 1508 1509 1510 1511 1512 1513
				is opened (if does not exist, error), or
				OS_FILE_CREATE if a new file is created
				(if exists, error),
				OS_FILE_OVERWRITE if a new file is created
				or an old overwritten;
				OS_FILE_OPEN_RAW, if a raw device or disk
				partition should be opened */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1514
	ulint		purpose,/*!< in: OS_FILE_AIO, if asynchronous,
1515 1516 1517 1518 1519 1520
				non-buffered i/o is desired,
				OS_FILE_NORMAL, if any normal file;
				NOTE that it also depends on type, os_aio_..
				and srv_.. variables whether we really use
				async i/o or unbuffered i/o: look in the
				function source code for the exact rules */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1521 1522
	ulint		type,	/*!< in: OS_DATA_FILE or OS_LOG_FILE */
	ibool*		success)/*!< out: TRUE if succeed, FALSE if error */
1523 1524 1525 1526 1527 1528 1529
{
#ifdef __WIN__
	os_file_t	file;
	DWORD		share_mode	= FILE_SHARE_READ;
	DWORD		create_flag;
	DWORD		attributes;
	ibool		retry;
1530 1531 1532 1533 1534 1535 1536

	DBUG_EXECUTE_IF(
		"ib_create_table_fail_disk_full",
		*success = FALSE;
		SetLastError(ERROR_DISK_FULL);
		return((os_file_t) -1);
	);
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
try_again:
	ut_a(name);

	if (create_mode == OS_FILE_OPEN_RAW) {
		create_flag = OPEN_EXISTING;
		share_mode = FILE_SHARE_WRITE;
	} else if (create_mode == OS_FILE_OPEN
		   || create_mode == OS_FILE_OPEN_RETRY) {
		create_flag = OPEN_EXISTING;
	} else if (create_mode == OS_FILE_CREATE) {
		create_flag = CREATE_NEW;
	} else if (create_mode == OS_FILE_OVERWRITE) {
		create_flag = CREATE_ALWAYS;
	} else {
		create_flag = 0;
		ut_error;
	}

	if (purpose == OS_FILE_AIO) {
		/* If specified, use asynchronous (overlapped) io and no
		buffering of writes in the OS */
		attributes = 0;
#ifdef WIN_ASYNC_IO
Sergei Golubchik's avatar
Sergei Golubchik committed
1560
		if (srv_use_native_aio) {
1561 1562 1563 1564
			attributes = attributes | FILE_FLAG_OVERLAPPED;
		}
#endif
#ifdef UNIV_NON_BUFFERED_IO
1565
# ifndef UNIV_HOTBACKUP
Sergei Golubchik's avatar
Sergei Golubchik committed
1566
		if (type == OS_LOG_FILE && thd_flush_log_at_trx_commit(NULL) == 2) {
1567 1568 1569 1570 1571 1572 1573
			/* Do not use unbuffered i/o to log files because
			value 2 denotes that we do not flush the log at every
			commit, but only once per second */
		} else if (srv_win_file_flush_method
			   == SRV_WIN_IO_UNBUFFERED) {
			attributes = attributes | FILE_FLAG_NO_BUFFERING;
		}
1574 1575 1576 1577
# else /* !UNIV_HOTBACKUP */
		attributes = attributes | FILE_FLAG_NO_BUFFERING;
# endif /* !UNIV_HOTBACKUP */
#endif /* UNIV_NON_BUFFERED_IO */
1578 1579 1580
	} else if (purpose == OS_FILE_NORMAL) {
		attributes = 0;
#ifdef UNIV_NON_BUFFERED_IO
1581
# ifndef UNIV_HOTBACKUP
Sergei Golubchik's avatar
Sergei Golubchik committed
1582
		if (type == OS_LOG_FILE && thd_flush_log_at_trx_commit(NULL) == 2) {
1583 1584 1585 1586 1587 1588 1589
			/* Do not use unbuffered i/o to log files because
			value 2 denotes that we do not flush the log at every
			commit, but only once per second */
		} else if (srv_win_file_flush_method
			   == SRV_WIN_IO_UNBUFFERED) {
			attributes = attributes | FILE_FLAG_NO_BUFFERING;
		}
1590 1591 1592 1593
# else /* !UNIV_HOTBACKUP */
		attributes = attributes | FILE_FLAG_NO_BUFFERING;
# endif /* !UNIV_HOTBACKUP */
#endif /* UNIV_NON_BUFFERED_IO */
1594 1595 1596 1597 1598
	} else {
		attributes = 0;
		ut_error;
	}

1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
	if (type == OS_LOG_FILE) {
		if (srv_unix_file_flush_method == SRV_UNIX_O_DSYNC) {
			/* Map O_DSYNC to WRITE_THROUGH */
			attributes |= FILE_FLAG_WRITE_THROUGH;
		} else if (srv_unix_file_flush_method == SRV_UNIX_ALL_O_DIRECT) {
			/* Open log file without buffering */
			attributes |= FILE_FLAG_NO_BUFFERING;
		}
	}

1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
	file = CreateFile((LPCTSTR) name,
			  GENERIC_READ | GENERIC_WRITE, /* read and write
							access */
			  share_mode,	/* File can be read also by other
					processes; we must give the read
					permission because of ibbackup. We do
					not give the write permission to
					others because if one would succeed to
					start 2 instances of mysqld on the
					SAME files, that could cause severe
					database corruption! When opening
					raw disk partitions, Microsoft manuals
					say that we must give also the write
					permission. */
			  NULL,	/* default security attributes */
			  create_flag,
			  attributes,
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1626
			  NULL);	/*!< no template file */
1627 1628 1629 1630

	if (file == INVALID_HANDLE_VALUE) {
		*success = FALSE;

Vadim Tkachenko's avatar
Vadim Tkachenko committed
1631 1632
		/* When srv_file_per_table is on, file creation failure may not
		be critical to the whole instance. Do not crash the server in
1633 1634 1635 1636 1637
		case of unknown errors.
		Please note "srv_file_per_table" is a global variable with
		no explicit synchronization protection. It could be
		changed during this execution path. It might not have the
		same value as the one when building the table definition */
Vadim Tkachenko's avatar
Vadim Tkachenko committed
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
		if (srv_file_per_table) {
			retry = os_file_handle_error_no_exit(name,
						create_mode == OS_FILE_CREATE ?
						"create" : "open");
		} else {
			retry = os_file_handle_error(name,
						create_mode == OS_FILE_CREATE ?
						"create" : "open");
		}

1648 1649 1650 1651 1652
		if (retry) {
			goto try_again;
		}
	} else {
		*success = TRUE;
Sergei Golubchik's avatar
Sergei Golubchik committed
1653
		if (srv_use_native_aio && ((attributes & FILE_FLAG_OVERLAPPED) != 0)) {
1654 1655
			ut_a(CreateIoCompletionPort(file, completion_port, 0, 0));
		}
1656 1657
	}

1658 1659 1660 1661 1662 1663 1664
	if (srv_use_atomic_writes && type == OS_DATA_FILE && 
		os_file_set_atomic_writes(file, name)) {
			 CloseHandle(file);
			*success = FALSE;
			file = INVALID_HANDLE_VALUE;
	}

1665 1666 1667 1668 1669 1670 1671
	return(file);
#else /* __WIN__ */
	os_file_t	file;
	int		create_flag;
	ibool		retry;
	const char*	mode_str	= NULL;

1672 1673 1674 1675 1676 1677
	DBUG_EXECUTE_IF(
		"ib_create_table_fail_disk_full",
		*success = FALSE;
		errno = ENOSPC;
		return((os_file_t) -1);
	);
1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
try_again:
	ut_a(name);

	if (create_mode == OS_FILE_OPEN || create_mode == OS_FILE_OPEN_RAW
	    || create_mode == OS_FILE_OPEN_RETRY) {
		mode_str = "OPEN";
		create_flag = O_RDWR;
	} else if (create_mode == OS_FILE_CREATE) {
		mode_str = "CREATE";
		create_flag = O_RDWR | O_CREAT | O_EXCL;
	} else if (create_mode == OS_FILE_OVERWRITE) {
		mode_str = "OVERWRITE";
		create_flag = O_RDWR | O_CREAT | O_TRUNC;
	} else {
		create_flag = 0;
		ut_error;
	}

1696 1697
	ut_a(type == OS_LOG_FILE || type == OS_DATA_FILE);
	ut_a(purpose == OS_FILE_AIO || purpose == OS_FILE_NORMAL);
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718

#ifdef O_SYNC
	/* We let O_SYNC only affect log files; note that we map O_DSYNC to
	O_SYNC because the datasync options seemed to corrupt files in 2001
	in both Linux and Solaris */
	if (type == OS_LOG_FILE
	    && srv_unix_file_flush_method == SRV_UNIX_O_DSYNC) {

# if 0
		fprintf(stderr, "Using O_SYNC for file %s\n", name);
# endif

		create_flag = create_flag | O_SYNC;
	}
#endif /* O_SYNC */

	file = open(name, create_flag, os_innodb_umask);

	if (file == -1) {
		*success = FALSE;

Vadim Tkachenko's avatar
Vadim Tkachenko committed
1719 1720
		/* When srv_file_per_table is on, file creation failure may not
		be critical to the whole instance. Do not crash the server in
1721 1722 1723 1724 1725
		case of unknown errors.
		Please note "srv_file_per_table" is a global variable with
		no explicit synchronization protection. It could be
		changed during this execution path. It might not have the
		same value as the one when building the table definition */
Vadim Tkachenko's avatar
Vadim Tkachenko committed
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
		if (srv_file_per_table) {
			retry = os_file_handle_error_no_exit(name,
						create_mode == OS_FILE_CREATE ?
						"create" : "open");
		} else {
			retry = os_file_handle_error(name,
						create_mode == OS_FILE_CREATE ?
						"create" : "open");
		}

1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
		if (retry) {
			goto try_again;
		} else {
			return(file /* -1 */);
		}
	}
	/* else */

	*success = TRUE;

	/* We disable OS caching (O_DIRECT) only on data files */
	if (type != OS_LOG_FILE
	    && srv_unix_file_flush_method == SRV_UNIX_O_DIRECT) {
		
		os_file_set_nocache(file, name, mode_str);
	}

1753 1754 1755 1756 1757
	/* ALL_O_DIRECT: O_DIRECT also for transaction log file */
	if (srv_unix_file_flush_method == SRV_UNIX_ALL_O_DIRECT) {
		os_file_set_nocache(file, name, mode_str);
	}

1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
#ifdef USE_FILE_LOCK
	if (create_mode != OS_FILE_OPEN_RAW && os_file_lock(file, name)) {

		if (create_mode == OS_FILE_OPEN_RETRY) {
			int i;
			ut_print_timestamp(stderr);
			fputs("  InnoDB: Retrying to lock"
			      " the first data file\n",
			      stderr);
			for (i = 0; i < 100; i++) {
				os_thread_sleep(1000000);
				if (!os_file_lock(file, name)) {
					*success = TRUE;
					return(file);
				}
			}
			ut_print_timestamp(stderr);
			fputs("  InnoDB: Unable to open the first data file\n",
			      stderr);
		}

		*success = FALSE;
		close(file);
		file = -1;
	}
#endif /* USE_FILE_LOCK */
1784 1785 1786 1787 1788 1789
	if (srv_use_atomic_writes && type == OS_DATA_FILE 
		&& os_file_set_atomic_writes(file, name)) {
			close(file);
			*success = FALSE;
			file = -1;
	}
1790 1791 1792 1793 1794

	return(file);
#endif /* __WIN__ */
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1795 1796 1797
/***********************************************************************//**
Deletes a file if it exists. The file has to be closed before calling this.
@return	TRUE if success */
1798 1799 1800 1801
UNIV_INTERN
ibool
os_file_delete_if_exists(
/*=====================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1802
	const char*	name)	/*!< in: file path as a null-terminated string */
1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
{
#ifdef __WIN__
	BOOL	ret;
	ulint	count	= 0;
loop:
	/* In Windows, deleting an .ibd file may fail if ibbackup is copying
	it */

	ret = DeleteFile((LPCTSTR)name);

	if (ret) {
		return(TRUE);
	}

	if (GetLastError() == ERROR_FILE_NOT_FOUND) {
		/* the file does not exist, this not an error */

		return(TRUE);
	}

	count++;

	if (count > 100 && 0 == (count % 10)) {
		fprintf(stderr,
			"InnoDB: Warning: cannot delete file %s\n"
			"InnoDB: Are you running ibbackup"
			" to back up the file?\n", name);

		os_file_get_last_error(TRUE); /* print error information */
	}

	os_thread_sleep(1000000);	/* sleep for a second */

	if (count > 2000) {

		return(FALSE);
	}

	goto loop;
#else
	int	ret;

	ret = unlink(name);

	if (ret != 0 && errno != ENOENT) {
		os_file_handle_error_no_exit(name, "delete");

		return(FALSE);
	}

	return(TRUE);
#endif
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1857 1858 1859
/***********************************************************************//**
Deletes a file. The file has to be closed before calling this.
@return	TRUE if success */
1860 1861 1862 1863
UNIV_INTERN
ibool
os_file_delete(
/*===========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1864
	const char*	name)	/*!< in: file path as a null-terminated string */
1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
{
#ifdef __WIN__
	BOOL	ret;
	ulint	count	= 0;
loop:
	/* In Windows, deleting an .ibd file may fail if ibbackup is copying
	it */

	ret = DeleteFile((LPCTSTR)name);

	if (ret) {
		return(TRUE);
	}

	if (GetLastError() == ERROR_FILE_NOT_FOUND) {
		/* If the file does not exist, we classify this as a 'mild'
		error and return */

		return(FALSE);
	}

	count++;

	if (count > 100 && 0 == (count % 10)) {
		fprintf(stderr,
			"InnoDB: Warning: cannot delete file %s\n"
			"InnoDB: Are you running ibbackup"
			" to back up the file?\n", name);

		os_file_get_last_error(TRUE); /* print error information */
	}

	os_thread_sleep(1000000);	/* sleep for a second */

	if (count > 2000) {

		return(FALSE);
	}

	goto loop;
#else
	int	ret;

	ret = unlink(name);

	if (ret != 0) {
		os_file_handle_error_no_exit(name, "delete");

		return(FALSE);
	}

	return(TRUE);
#endif
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1920
/***********************************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
1921
NOTE! Use the corresponding macro os_file_rename(), not directly this function!
1922
Renames a file (can also move it to another directory). It is safest that the
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1923 1924
file is closed before calling this function.
@return	TRUE if success */
1925 1926
UNIV_INTERN
ibool
Sergei Golubchik's avatar
Sergei Golubchik committed
1927 1928
os_file_rename_func(
/*================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1929
	const char*	oldpath,/*!< in: old file path as a null-terminated
1930
				string */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1931
	const char*	newpath)/*!< in: new file path */
1932 1933 1934 1935
{
#ifdef __WIN__
	BOOL	ret;

1936
	ret = MoveFileEx((LPCTSTR)oldpath, (LPCTSTR)newpath, MOVEFILE_REPLACE_EXISTING);
1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959

	if (ret) {
		return(TRUE);
	}

	os_file_handle_error_no_exit(oldpath, "rename");

	return(FALSE);
#else
	int	ret;

	ret = rename(oldpath, newpath);

	if (ret != 0) {
		os_file_handle_error_no_exit(oldpath, "rename");

		return(FALSE);
	}

	return(TRUE);
#endif
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1960
/***********************************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
1961
NOTE! Use the corresponding macro os_file_close(), not directly this function!
1962
Closes a file handle. In case of error, error number can be retrieved with
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1963 1964
os_file_get_last_error.
@return	TRUE if success */
1965 1966
UNIV_INTERN
ibool
Sergei Golubchik's avatar
Sergei Golubchik committed
1967 1968
os_file_close_func(
/*===============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
1969
	os_file_t	file)	/*!< in, own: handle to a file */
1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
{
#ifdef __WIN__
	BOOL	ret;

	ut_a(file);

	ret = CloseHandle(file);

	if (ret) {
		return(TRUE);
	}

	os_file_handle_error(NULL, "close");

	return(FALSE);
#else
	int	ret;

	ret = close(file);

	if (ret == -1) {
		os_file_handle_error(NULL, "close");

		return(FALSE);
	}

	return(TRUE);
#endif
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2000 2001 2002 2003
#ifdef UNIV_HOTBACKUP
/***********************************************************************//**
Closes a file handle.
@return	TRUE if success */
2004 2005 2006 2007
UNIV_INTERN
ibool
os_file_close_no_error_handling(
/*============================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2008
	os_file_t	file)	/*!< in, own: handle to a file */
2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
{
#ifdef __WIN__
	BOOL	ret;

	ut_a(file);

	ret = CloseHandle(file);

	if (ret) {
		return(TRUE);
	}

	return(FALSE);
#else
	int	ret;

	ret = close(file);

	if (ret == -1) {

		return(FALSE);
	}

	return(TRUE);
#endif
}
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2035
#endif /* UNIV_HOTBACKUP */
2036

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2037 2038 2039
/***********************************************************************//**
Gets a file size.
@return	TRUE if success */
2040 2041 2042 2043
UNIV_INTERN
ibool
os_file_get_size(
/*=============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2044 2045
	os_file_t	file,	/*!< in: handle to a file */
	ulint*		size,	/*!< out: least significant 32 bits of file
2046
				size */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2047
	ulint*		size_high)/*!< out: most significant 32 bits of size */
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084
{
#ifdef __WIN__
	DWORD	high;
	DWORD	low;

	low = GetFileSize(file, &high);

	if ((low == 0xFFFFFFFF) && (GetLastError() != NO_ERROR)) {
		return(FALSE);
	}

	*size = low;
	*size_high = high;

	return(TRUE);
#else
	off_t	offs;

	offs = lseek(file, 0, SEEK_END);

	if (offs == ((off_t)-1)) {

		return(FALSE);
	}

	if (sizeof(off_t) > 4) {
		*size = (ulint)(offs & 0xFFFFFFFFUL);
		*size_high = (ulint)(offs >> 32);
	} else {
		*size = (ulint) offs;
		*size_high = 0;
	}

	return(TRUE);
#endif
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2085 2086 2087
/***********************************************************************//**
Gets file size as a 64-bit integer ib_int64_t.
@return	size in bytes, -1 if error */
2088 2089 2090 2091
UNIV_INTERN
ib_int64_t
os_file_get_size_as_iblonglong(
/*===========================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2092
	os_file_t	file)	/*!< in: handle to a file */
2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107
{
	ulint	size;
	ulint	size_high;
	ibool	success;

	success = os_file_get_size(file, &size, &size_high);

	if (!success) {

		return(-1);
	}

	return((((ib_int64_t)size_high) << 32) + (ib_int64_t)size);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2108 2109 2110
/***********************************************************************//**
Write the specified number of zeros to a newly created file.
@return	TRUE if success */
2111 2112 2113 2114
UNIV_INTERN
ibool
os_file_set_size(
/*=============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2115
	const char*	name,	/*!< in: name of the file or path as a
2116
				null-terminated string */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2117 2118
	os_file_t	file,	/*!< in: handle to a file */
	ulint		size,	/*!< in: least significant 32 bits of file
2119
				size */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2120
	ulint		size_high)/*!< in: most significant 32 bits of size */
2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
{
	ib_int64_t	current_size;
	ib_int64_t	desired_size;
	ibool		ret;
	byte*		buf;
	byte*		buf2;
	ulint		buf_size;

	ut_a(size == (size & 0xFFFFFFFF));

	current_size = 0;
	desired_size = (ib_int64_t)size + (((ib_int64_t)size_high) << 32);

2134 2135 2136 2137 2138 2139 2140
#ifdef HAVE_POSIX_FALLOCATE
        if (srv_use_posix_fallocate) {
		if (posix_fallocate(file, current_size, desired_size) == -1) {
			fprintf(stderr,
		 	"InnoDB: Error: preallocating data for"
			" file %s failed at\n"
			"InnoDB: offset 0 size %lld %lld. Operating system"
2141
			" error number %d.\n"
2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
			"InnoDB: Check that the disk is not full"
			" or a disk quota exceeded.\n"
			"InnoDB: Some operating system error numbers"
			" are described at\n"
			"InnoDB: "
			REFMAN "operating-system-error-codes.html\n",
			name,  (long long)size_high,  (long long)size, errno);

			return (FALSE);
		}
		return (TRUE);
	}
#endif

2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208
	/* Write up to 1 megabyte at a time. */
	buf_size = ut_min(64, (ulint) (desired_size / UNIV_PAGE_SIZE))
		* UNIV_PAGE_SIZE;
	buf2 = ut_malloc(buf_size + UNIV_PAGE_SIZE);

	/* Align the buffer for possible raw i/o */
	buf = ut_align(buf2, UNIV_PAGE_SIZE);

	/* Write buffer full of zeros */
	memset(buf, 0, buf_size);

	if (desired_size >= (ib_int64_t)(100 * 1024 * 1024)) {

		fprintf(stderr, "InnoDB: Progress in MB:");
	}

	while (current_size < desired_size) {
		ulint	n_bytes;

		if (desired_size - current_size < (ib_int64_t) buf_size) {
			n_bytes = (ulint) (desired_size - current_size);
		} else {
			n_bytes = buf_size;
		}

		ret = os_file_write(name, file, buf,
				    (ulint)(current_size & 0xFFFFFFFF),
				    (ulint)(current_size >> 32),
				    n_bytes);
		if (!ret) {
			ut_free(buf2);
			goto error_handling;
		}

		/* Print about progress for each 100 MB written */
		if ((ib_int64_t) (current_size + n_bytes) / (ib_int64_t)(100 * 1024 * 1024)
		    != current_size / (ib_int64_t)(100 * 1024 * 1024)) {

			fprintf(stderr, " %lu00",
				(ulong) ((current_size + n_bytes)
					 / (ib_int64_t)(100 * 1024 * 1024)));
		}

		current_size += n_bytes;
	}

	if (desired_size >= (ib_int64_t)(100 * 1024 * 1024)) {

		fprintf(stderr, "\n");
	}

	ut_free(buf2);

2209
	ret = os_file_flush(file, TRUE);
2210 2211 2212 2213 2214 2215 2216 2217 2218

	if (ret) {
		return(TRUE);
	}

error_handling:
	return(FALSE);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2219 2220 2221
/***********************************************************************//**
Truncates a file at its current position.
@return	TRUE if success */
2222 2223 2224 2225
UNIV_INTERN
ibool
os_file_set_eof(
/*============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2226
	FILE*		file)	/*!< in: file to be truncated */
2227 2228 2229 2230 2231 2232 2233 2234 2235
{
#ifdef __WIN__
	HANDLE h = (HANDLE) _get_osfhandle(fileno(file));
	return(SetEndOfFile(h));
#else /* __WIN__ */
	return(!ftruncate(fileno(file), ftell(file)));
#endif /* __WIN__ */
}

2236 2237 2238 2239 2240 2241 2242 2243 2244 2245
/***********************************************************************//**
Truncates a file at the specified position.
@return TRUE if success */
UNIV_INTERN
ibool
os_file_set_eof_at(
	os_file_t	file, /*!< in: handle to a file */
	ib_uint64_t	new_len)/*!< in: new file length */
{
#ifdef __WIN__
2246 2247 2248 2249
	LARGE_INTEGER li, li2;
	li.QuadPart = new_len;
	return(SetFilePointerEx(file, li, &li2,FILE_BEGIN)
	       && SetEndOfFile(file));
2250 2251 2252 2253 2254 2255 2256
#else
	/* TODO: works only with -D_FILE_OFFSET_BITS=64 ? */
	return(!ftruncate(file, new_len));
#endif
}


2257
#ifndef __WIN__
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2258
/***********************************************************************//**
2259 2260
Wrapper to fsync(2) that retries the call on some errors.
Returns the value 0 if successful; otherwise the value -1 is returned and
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2261 2262
the global variable errno is set to indicate the error.
@return	0 if success, -1 otherwise */
2263 2264 2265 2266 2267

static
int
os_file_fsync(
/*==========*/
2268 2269
	os_file_t	file,	/*!< in: handle to a file */
	ibool		metadata)
2270 2271 2272 2273 2274 2275 2276 2277
{
	int	ret;
	int	failures;
	ibool	retry;

	failures = 0;

	do {
2278 2279 2280 2281 2282 2283 2284 2285
#if defined(HAVE_FDATASYNC) && HAVE_DECL_FDATASYNC
		if (metadata) {
			ret = fsync(file);
		} else {
			ret = fdatasync(file);
		}
#else
		(void) metadata;
2286
		ret = fsync(file);
2287
#endif
2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304

		os_n_fsyncs++;

		if (ret == -1 && errno == ENOLCK) {

			if (failures % 100 == 0) {

				ut_print_timestamp(stderr);
				fprintf(stderr,
					"  InnoDB: fsync(): "
					"No locks available; retrying\n");
			}

			os_thread_sleep(200000 /* 0.2 sec */);

			failures++;

Sergei Golubchik's avatar
Sergei Golubchik committed
2305 2306 2307
			retry = TRUE;
		} else if (ret == -1 && errno == EINTR) {
			/* Handle signal interruptions correctly */
2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318
			retry = TRUE;
		} else {

			retry = FALSE;
		}
	} while (retry);

	return(ret);
}
#endif /* !__WIN__ */

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2319
/***********************************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
2320
NOTE! Use the corresponding macro os_file_flush(), not directly this function!
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2321 2322
Flushes the write buffers of a given file to the disk.
@return	TRUE if success */
2323 2324
UNIV_INTERN
ibool
Sergei Golubchik's avatar
Sergei Golubchik committed
2325 2326
os_file_flush_func(
/*===============*/
2327 2328
	os_file_t	file,	/*!< in, own: handle to a file */
	ibool		metadata)
2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377
{
#ifdef __WIN__
	BOOL	ret;

	ut_a(file);

	os_n_fsyncs++;

	ret = FlushFileBuffers(file);

	if (ret) {
		return(TRUE);
	}

	/* Since Windows returns ERROR_INVALID_FUNCTION if the 'file' is
	actually a raw device, we choose to ignore that error if we are using
	raw disks */

	if (srv_start_raw_disk_in_use && GetLastError()
	    == ERROR_INVALID_FUNCTION) {
		return(TRUE);
	}

	os_file_handle_error(NULL, "flush");

	/* It is a fatal error if a file flush does not succeed, because then
	the database can get corrupt on disk */
	ut_error;

	return(FALSE);
#else
	int	ret;

#if defined(HAVE_DARWIN_THREADS)
# ifndef F_FULLFSYNC
	/* The following definition is from the Mac OS X 10.3 <sys/fcntl.h> */
#  define F_FULLFSYNC 51 /* fsync + ask the drive to flush to the media */
# elif F_FULLFSYNC != 51
#  error "F_FULLFSYNC != 51: ABI incompatibility with Mac OS X 10.3"
# endif
	/* Apple has disabled fsync() for internal disk drives in OS X. That
	caused corruption for a user when he tested a power outage. Let us in
	OS X use a nonstandard flush method recommended by an Apple
	engineer. */

	if (!srv_have_fullfsync) {
		/* If we are not on an operating system that supports this,
		then fall back to a plain fsync. */

2378
		ret = os_file_fsync(file, metadata);
2379 2380 2381 2382 2383 2384
	} else {
		ret = fcntl(file, F_FULLFSYNC, NULL);

		if (ret) {
			/* If we are not on a file system that supports this,
			then fall back to a plain fsync. */
2385
			ret = os_file_fsync(file, metadata);
2386 2387 2388
		}
	}
#else
2389
	ret = os_file_fsync(file, metadata);
2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419
#endif

	if (ret == 0) {
		return(TRUE);
	}

	/* Since Linux returns EINVAL if the 'file' is actually a raw device,
	we choose to ignore that error if we are using raw disks */

	if (srv_start_raw_disk_in_use && errno == EINVAL) {

		return(TRUE);
	}

	ut_print_timestamp(stderr);

	fprintf(stderr,
		"  InnoDB: Error: the OS said file flush did not succeed\n");

	os_file_handle_error(NULL, "flush");

	/* It is a fatal error if a file flush does not succeed, because then
	the database can get corrupt on disk */
	ut_error;

	return(FALSE);
#endif
}

#ifndef __WIN__
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2420 2421 2422
/*******************************************************************//**
Does a synchronous read operation in Posix.
@return	number of bytes read, -1 if error */
2423 2424
static
ssize_t
Sergei Golubchik's avatar
Sergei Golubchik committed
2425
os_file_pread(
2426
/*==========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2427 2428 2429 2430
	os_file_t	file,	/*!< in: handle to a file */
	void*		buf,	/*!< in: buffer where to read */
	ulint		n,	/*!< in: number of bytes to read */
	ulint		offset,	/*!< in: least significant 32 bits of file
2431
				offset from where to read */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2432
	ulint		offset_high, /*!< in: most significant 32 bits of
2433
				offset */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2434
	trx_t*		trx)
2435 2436
{
	off_t	offs;
2437
#if defined(HAVE_PREAD) && !defined(HAVE_BROKEN_PREAD)
2438
	ssize_t	n_bytes;
Sergei Golubchik's avatar
Sergei Golubchik committed
2439
	ssize_t n_read;
2440 2441 2442 2443 2444
#endif /* HAVE_PREAD && !HAVE_BROKEN_PREAD */
	ulint		sec;
	ulint		ms;
	ib_uint64_t	start_time;
	ib_uint64_t	finish_time;
2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464

	ut_a((offset & 0xFFFFFFFFUL) == offset);

	/* If off_t is > 4 bytes in size, then we assume we can pass a
	64-bit address */

	if (sizeof(off_t) > 4) {
		offs = (off_t)offset + (((off_t)offset_high) << 32);

	} else {
		offs = (off_t)offset;

		if (offset_high > 0) {
			fprintf(stderr,
				"InnoDB: Error: file read at offset > 4 GB\n");
		}
	}

	os_n_file_reads++;

2465
	if (UNIV_UNLIKELY(trx && trx->take_stats))
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2466 2467 2468 2469 2470 2471 2472 2473
	{
	        trx->io_reads++;
		trx->io_read += n;
		ut_usectime(&sec, &ms);
		start_time = (ib_uint64_t)sec * 1000000 + ms;
	} else {
		start_time = 0;
	}
2474 2475 2476 2477 2478 2479
#if defined(HAVE_PREAD) && !defined(HAVE_BROKEN_PREAD)
	os_mutex_enter(os_file_count_mutex);
	os_file_n_pending_preads++;
	os_n_pending_reads++;
	os_mutex_exit(os_file_count_mutex);

Sergei Golubchik's avatar
Sergei Golubchik committed
2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491
	/* Handle signal interruptions correctly */
	for (n_bytes = 0; n_bytes < (ssize_t) n; ) {
		n_read = pread(file, buf, (ssize_t)n, offs);
		if (n_read > 0) {
			n_bytes += n_read;
			offs += n_read;
		} else if (n_read == -1 && errno == EINTR) {
			continue;
		} else {
			break;
		}
	}
2492 2493 2494 2495 2496 2497

	os_mutex_enter(os_file_count_mutex);
	os_file_n_pending_preads--;
	os_n_pending_reads--;
	os_mutex_exit(os_file_count_mutex);

2498
	if (UNIV_UNLIKELY(start_time != 0))
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2499 2500 2501 2502 2503 2504
	{
		ut_usectime(&sec, &ms);
		finish_time = (ib_uint64_t)sec * 1000000 + ms;
		trx->io_reads_wait_timer += (ulint)(finish_time - start_time);
	}

2505 2506 2507 2508 2509
	return(n_bytes);
#else
	{
		off_t	ret_offset;
		ssize_t	ret;
Sergei Golubchik's avatar
Sergei Golubchik committed
2510
		ssize_t n_read;
2511
#ifndef UNIV_HOTBACKUP
2512
		ulint	i;
2513
#endif /* !UNIV_HOTBACKUP */
2514 2515 2516 2517 2518

		os_mutex_enter(os_file_count_mutex);
		os_n_pending_reads++;
		os_mutex_exit(os_file_count_mutex);

2519
#ifndef UNIV_HOTBACKUP
2520 2521 2522 2523
		/* Protect the seek / read operation with a mutex */
		i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES;

		os_mutex_enter(os_file_seek_mutexes[i]);
2524
#endif /* !UNIV_HOTBACKUP */
2525 2526 2527 2528 2529 2530

		ret_offset = lseek(file, offs, SEEK_SET);

		if (ret_offset < 0) {
			ret = -1;
		} else {
Sergei Golubchik's avatar
Sergei Golubchik committed
2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541
			/* Handle signal interruptions correctly */
			for (ret = 0; ret < (ssize_t) n; ) {
				n_read = read(file, buf, (ssize_t)n);
				if (n_read > 0) {
					ret += n_read;
				} else if (n_read == -1 && errno == EINTR) {
					continue;
				} else {
					break;
				}
			}
2542 2543
		}

2544
#ifndef UNIV_HOTBACKUP
2545
		os_mutex_exit(os_file_seek_mutexes[i]);
2546
#endif /* !UNIV_HOTBACKUP */
2547 2548 2549 2550 2551

		os_mutex_enter(os_file_count_mutex);
		os_n_pending_reads--;
		os_mutex_exit(os_file_count_mutex);

2552
		if (UNIV_UNLIKELY(start_time != 0)
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2553 2554 2555 2556 2557 2558
		{
			ut_usectime(&sec, &ms);
			finish_time = (ib_uint64_t)sec * 1000000 + ms;
			trx->io_reads_wait_timer += (ulint)(finish_time - start_time);
		}

2559 2560 2561 2562 2563
		return(ret);
	}
#endif
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2564 2565 2566
/*******************************************************************//**
Does a synchronous write operation in Posix.
@return	number of bytes written, -1 if error */
2567 2568 2569 2570
static
ssize_t
os_file_pwrite(
/*===========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2571 2572 2573 2574
	os_file_t	file,	/*!< in: handle to a file */
	const void*	buf,	/*!< in: buffer from where to write */
	ulint		n,	/*!< in: number of bytes to write */
	ulint		offset,	/*!< in: least significant 32 bits of file
2575
				offset where to write */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2576
	ulint		offset_high) /*!< in: most significant 32 bits of
2577 2578 2579
				offset */
{
	ssize_t	ret;
Sergei Golubchik's avatar
Sergei Golubchik committed
2580
	ssize_t n_written;
2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607
	off_t	offs;

	ut_a((offset & 0xFFFFFFFFUL) == offset);

	/* If off_t is > 4 bytes in size, then we assume we can pass a
	64-bit address */

	if (sizeof(off_t) > 4) {
		offs = (off_t)offset + (((off_t)offset_high) << 32);
	} else {
		offs = (off_t)offset;

		if (offset_high > 0) {
			fprintf(stderr,
				"InnoDB: Error: file write"
				" at offset > 4 GB\n");
		}
	}

	os_n_file_writes++;

#if defined(HAVE_PWRITE) && !defined(HAVE_BROKEN_PREAD)
	os_mutex_enter(os_file_count_mutex);
	os_file_n_pending_pwrites++;
	os_n_pending_writes++;
	os_mutex_exit(os_file_count_mutex);

Sergei Golubchik's avatar
Sergei Golubchik committed
2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619
	/* Handle signal interruptions correctly */
	for (ret = 0; ret < (ssize_t) n; ) {
		n_written = pwrite(file, buf, (ssize_t)n, offs);
		if (n_written > 0) {
			ret += n_written;
			offs += n_written;
		} else if (n_written == -1 && errno == EINTR) {
			continue;
		} else {
			break;
		}
	}
2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634

	os_mutex_enter(os_file_count_mutex);
	os_file_n_pending_pwrites--;
	os_n_pending_writes--;
	os_mutex_exit(os_file_count_mutex);

# ifdef UNIV_DO_FLUSH
	if (srv_unix_file_flush_method != SRV_UNIX_LITTLESYNC
	    && srv_unix_file_flush_method != SRV_UNIX_NOSYNC
	    && !os_do_not_call_flush_at_each_write) {

		/* Always do fsync to reduce the probability that when
		the OS crashes, a database page is only partially
		physically written to disk. */

2635
		ut_a(TRUE == os_file_flush(file, TRUE));
2636 2637 2638 2639 2640 2641 2642
	}
# endif /* UNIV_DO_FLUSH */

	return(ret);
#else
	{
		off_t	ret_offset;
2643
# ifndef UNIV_HOTBACKUP
2644
		ulint	i;
2645
# endif /* !UNIV_HOTBACKUP */
2646 2647 2648 2649 2650

		os_mutex_enter(os_file_count_mutex);
		os_n_pending_writes++;
		os_mutex_exit(os_file_count_mutex);

2651
# ifndef UNIV_HOTBACKUP
2652 2653 2654 2655
		/* Protect the seek / write operation with a mutex */
		i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES;

		os_mutex_enter(os_file_seek_mutexes[i]);
2656
# endif /* UNIV_HOTBACKUP */
2657 2658 2659 2660 2661 2662 2663 2664 2665

		ret_offset = lseek(file, offs, SEEK_SET);

		if (ret_offset < 0) {
			ret = -1;

			goto func_exit;
		}

Sergei Golubchik's avatar
Sergei Golubchik committed
2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676
		/* Handle signal interruptions correctly */
		for (ret = 0; ret < (ssize_t) n; ) {
			n_written = write(file, buf, (ssize_t)n);
			if (n_written > 0) {
				ret += n_written;
			} else if (n_written == -1 && errno == EINTR) {
				continue;
			} else {
				break;
			}
		}
2677 2678 2679 2680 2681 2682 2683 2684 2685 2686

# ifdef UNIV_DO_FLUSH
		if (srv_unix_file_flush_method != SRV_UNIX_LITTLESYNC
		    && srv_unix_file_flush_method != SRV_UNIX_NOSYNC
		    && !os_do_not_call_flush_at_each_write) {

			/* Always do fsync to reduce the probability that when
			the OS crashes, a database page is only partially
			physically written to disk. */

2687
			ut_a(TRUE == os_file_flush(file, TRUE));
2688 2689 2690 2691
		}
# endif /* UNIV_DO_FLUSH */

func_exit:
2692
# ifndef UNIV_HOTBACKUP
2693
		os_mutex_exit(os_file_seek_mutexes[i]);
2694
# endif /* !UNIV_HOTBACKUP */
2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705

		os_mutex_enter(os_file_count_mutex);
		os_n_pending_writes--;
		os_mutex_exit(os_file_count_mutex);

		return(ret);
	}
#endif
}
#endif

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2706
/*******************************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
2707 2708
NOTE! Use the corresponding macro os_file_read(), not directly this
function!
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2709 2710
Requests a synchronous positioned read operation.
@return	TRUE if request was successful, FALSE if fail */
2711 2712
UNIV_INTERN
ibool
Sergei Golubchik's avatar
Sergei Golubchik committed
2713 2714
os_file_read_func(
/*==============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2715 2716 2717
	os_file_t	file,	/*!< in: handle to a file */
	void*		buf,	/*!< in: buffer where to read */
	ulint		offset,	/*!< in: least significant 32 bits of file
2718
				offset where to read */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2719
	ulint		offset_high, /*!< in: most significant 32 bits of
2720
				offset */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2721 2722
	ulint		n,	/*!< in: number of bytes to read */
	trx_t*		trx)
2723 2724 2725 2726 2727
{
#ifdef __WIN__
	BOOL		ret;
	DWORD		len;
	ibool		retry;
2728
	OVERLAPPED overlapped;
Vladislav Vaintroub's avatar
Vladislav Vaintroub committed
2729

2730

2731 2732
	/* On 64-bit Windows, ulint is 64 bits. But offset and n should be
	no more than 32 bits. */
2733
	ut_a((offset & 0xFFFFFFFFUL) == offset);
2734
	ut_a((n & 0xFFFFFFFFUL) == n);
2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747

	os_n_file_reads++;
	os_bytes_read_since_printout += n;

try_again:
	ut_ad(file);
	ut_ad(buf);
	ut_ad(n > 0);

	os_mutex_enter(os_file_count_mutex);
	os_n_pending_reads++;
	os_mutex_exit(os_file_count_mutex);

2748
	memset (&overlapped, 0, sizeof (overlapped));
Vladislav Vaintroub's avatar
Vladislav Vaintroub committed
2749 2750
	overlapped.Offset = (DWORD)offset;
	overlapped.OffsetHigh = (DWORD)offset_high;
2751 2752 2753 2754
	overlapped.hEvent = win_get_syncio_event();
	ret = ReadFile(file, buf, n, NULL, &overlapped);
	if (ret) {
		ret = GetOverlappedResult(file, &overlapped, (DWORD *)&len, FALSE);
2755
	}
2756 2757
	else if(GetLastError() == ERROR_IO_PENDING) {
		ret = GetOverlappedResult(file, &overlapped, (DWORD *)&len, TRUE);
Vladislav Vaintroub's avatar
Vladislav Vaintroub committed
2758
  }
2759 2760 2761 2762 2763 2764 2765
	os_mutex_enter(os_file_count_mutex);
	os_n_pending_reads--;
	os_mutex_exit(os_file_count_mutex);

	if (ret && len == n) {
		return(TRUE);
	}
2766
#else /* __WIN__ */
2767 2768 2769 2770 2771 2772
	ibool	retry;
	ssize_t	ret;

	os_bytes_read_since_printout += n;

try_again:
Sergei Golubchik's avatar
Sergei Golubchik committed
2773
	ret = os_file_pread(file, buf, n, offset, offset_high, trx);
2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784

	if ((ulint)ret == n) {

		return(TRUE);
	}

	fprintf(stderr,
		"InnoDB: Error: tried to read %lu bytes at offset %lu %lu.\n"
		"InnoDB: Was only able to read %ld.\n",
		(ulong)n, (ulong)offset_high,
		(ulong)offset, (long)ret);
2785
#endif /* __WIN__ */
2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807
	retry = os_file_handle_error(NULL, "read");

	if (retry) {
		goto try_again;
	}

	fprintf(stderr,
		"InnoDB: Fatal error: cannot read from file."
		" OS error number %lu.\n",
#ifdef __WIN__
		(ulong) GetLastError()
#else
		(ulong) errno
#endif
		);
	fflush(stderr);

	ut_error;

	return(FALSE);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2808
/*******************************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
2809 2810
NOTE! Use the corresponding macro os_file_read_no_error_handling(),
not directly this function!
2811
Requests a synchronous positioned read operation. This function does not do
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2812 2813
any error handling. In case of error it returns FALSE.
@return	TRUE if request was successful, FALSE if fail */
2814 2815
UNIV_INTERN
ibool
Sergei Golubchik's avatar
Sergei Golubchik committed
2816 2817
os_file_read_no_error_handling_func(
/*================================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2818 2819 2820
	os_file_t	file,	/*!< in: handle to a file */
	void*		buf,	/*!< in: buffer where to read */
	ulint		offset,	/*!< in: least significant 32 bits of file
2821
				offset where to read */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2822
	ulint		offset_high, /*!< in: most significant 32 bits of
2823
				offset */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2824
	ulint		n)	/*!< in: number of bytes to read */
2825 2826 2827 2828 2829
{
#ifdef __WIN__
	BOOL		ret;
	DWORD		len;
	ibool		retry;
2830 2831 2832 2833
	OVERLAPPED overlapped;
	overlapped.Offset = (DWORD)offset;
	overlapped.OffsetHigh = (DWORD)offset_high;

2834

2835 2836
	/* On 64-bit Windows, ulint is 64 bits. But offset and n should be
	no more than 32 bits. */
2837
	ut_a((offset & 0xFFFFFFFFUL) == offset);
2838
	ut_a((n & 0xFFFFFFFFUL) == n);
2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851

	os_n_file_reads++;
	os_bytes_read_since_printout += n;

try_again:
	ut_ad(file);
	ut_ad(buf);
	ut_ad(n > 0);

	os_mutex_enter(os_file_count_mutex);
	os_n_pending_reads++;
	os_mutex_exit(os_file_count_mutex);

2852
	memset (&overlapped, 0, sizeof (overlapped));
Vladislav Vaintroub's avatar
Vladislav Vaintroub committed
2853 2854
	overlapped.Offset = (DWORD)offset;
	overlapped.OffsetHigh = (DWORD)offset_high;
2855 2856 2857 2858
	overlapped.hEvent = win_get_syncio_event();
	ret = ReadFile(file, buf, n, NULL, &overlapped);
	if (ret) {
		ret = GetOverlappedResult(file, &overlapped, (DWORD *)&len, FALSE);
2859
	}
2860 2861
	else if(GetLastError() == ERROR_IO_PENDING) {
		ret = GetOverlappedResult(file, &overlapped, (DWORD *)&len, TRUE);
Vladislav Vaintroub's avatar
Vladislav Vaintroub committed
2862
  }
2863 2864 2865 2866 2867 2868 2869
	os_mutex_enter(os_file_count_mutex);
	os_n_pending_reads--;
	os_mutex_exit(os_file_count_mutex);

	if (ret && len == n) {
		return(TRUE);
	}
2870
#else /* __WIN__ */
2871 2872 2873 2874 2875 2876
	ibool	retry;
	ssize_t	ret;

	os_bytes_read_since_printout += n;

try_again:
Sergei Golubchik's avatar
Sergei Golubchik committed
2877
	ret = os_file_pread(file, buf, n, offset, offset_high, NULL);
2878 2879 2880 2881 2882

	if ((ulint)ret == n) {

		return(TRUE);
	}
2883
#endif /* __WIN__ */
2884 2885 2886 2887 2888 2889 2890 2891 2892
	retry = os_file_handle_error_no_exit(NULL, "read");

	if (retry) {
		goto try_again;
	}

	return(FALSE);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2893
/*******************************************************************//**
2894 2895 2896 2897 2898 2899 2900
Rewind file to its start, read at most size - 1 bytes from it to str, and
NUL-terminate str. All errors are silently ignored. This function is
mostly meant to be used with temporary files. */
UNIV_INTERN
void
os_file_read_string(
/*================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2901 2902 2903
	FILE*	file,	/*!< in: file to read from */
	char*	str,	/*!< in: buffer where to read */
	ulint	size)	/*!< in: size of buffer */
2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915
{
	size_t	flen;

	if (size == 0) {
		return;
	}

	rewind(file);
	flen = fread(str, 1, size - 1, file);
	str[flen] = '\0';
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2916
/*******************************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
2917 2918
NOTE! Use the corresponding macro os_file_write(), not directly
this function!
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2919 2920
Requests a synchronous write operation.
@return	TRUE if request was successful, FALSE if fail */
2921 2922
UNIV_INTERN
ibool
Sergei Golubchik's avatar
Sergei Golubchik committed
2923 2924
os_file_write_func(
/*===============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2925
	const char*	name,	/*!< in: name of the file or path as a
2926
				null-terminated string */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2927 2928 2929
	os_file_t	file,	/*!< in: handle to a file */
	const void*	buf,	/*!< in: buffer from which to write */
	ulint		offset,	/*!< in: least significant 32 bits of file
2930
				offset where to write */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2931
	ulint		offset_high, /*!< in: most significant 32 bits of
2932
				offset */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
2933
	ulint		n)	/*!< in: number of bytes to write */
2934 2935 2936 2937 2938 2939
{
#ifdef __WIN__
	BOOL		ret;
	DWORD		len;
	ulint		n_retries	= 0;
	ulint		err;
2940
	OVERLAPPED overlapped;
2941

2942 2943 2944 2945
	/* On 64-bit Windows, ulint is 64 bits. But offset and n should be
	no more than 32 bits. */
	ut_a((offset & 0xFFFFFFFFUL) == offset);
	ut_a((n & 0xFFFFFFFFUL) == n);
2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957

	os_n_file_writes++;

	ut_ad(file);
	ut_ad(buf);
	ut_ad(n > 0);
retry:

	os_mutex_enter(os_file_count_mutex);
	os_n_pending_writes++;
	os_mutex_exit(os_file_count_mutex);

2958
	memset (&overlapped, 0, sizeof (overlapped));
Vladislav Vaintroub's avatar
Vladislav Vaintroub committed
2959 2960
	overlapped.Offset = (DWORD)offset;
	overlapped.OffsetHigh = (DWORD)offset_high;
2961

2962 2963 2964 2965
	overlapped.hEvent = win_get_syncio_event();
	ret = WriteFile(file, buf, n, NULL, &overlapped);
	if (ret) {
		ret = GetOverlappedResult(file, &overlapped, (DWORD *)&len, FALSE);
Vladislav Vaintroub's avatar
Vladislav Vaintroub committed
2966
	}
2967 2968
	else if(GetLastError() == ERROR_IO_PENDING) {
		ret = GetOverlappedResult(file, &overlapped, (DWORD *)&len, TRUE);
2969 2970 2971 2972
	}

# ifdef UNIV_DO_FLUSH
	if (!os_do_not_call_flush_at_each_write) {
Sergei Golubchik's avatar
Sergei Golubchik committed
2973
		ut_a(TRUE == os_file_flush(file));
2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015
	}
# endif /* UNIV_DO_FLUSH */

	os_mutex_enter(os_file_count_mutex);
	os_n_pending_writes--;
	os_mutex_exit(os_file_count_mutex);

	if (ret && len == n) {

		return(TRUE);
	}

	/* If some background file system backup tool is running, then, at
	least in Windows 2000, we may get here a specific error. Let us
	retry the operation 100 times, with 1 second waits. */

	if (GetLastError() == ERROR_LOCK_VIOLATION && n_retries < 100) {

		os_thread_sleep(1000000);

		n_retries++;

		goto retry;
	}

	if (!os_has_said_disk_full) {

		err = (ulint)GetLastError();

		ut_print_timestamp(stderr);

		fprintf(stderr,
			"  InnoDB: Error: Write to file %s failed"
			" at offset %lu %lu.\n"
			"InnoDB: %lu bytes should have been written,"
			" only %lu were written.\n"
			"InnoDB: Operating system error number %lu.\n"
			"InnoDB: Check that your OS and file system"
			" support files of this size.\n"
			"InnoDB: Check also that the disk is not full"
			" or a disk quota exceeded.\n",
			name, (ulong) offset_high, (ulong) offset,
3016
			(ulong) n, ret ? len : 0, (ulong) err);
3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027

		if (strerror((int)err) != NULL) {
			fprintf(stderr,
				"InnoDB: Error number %lu means '%s'.\n",
				(ulong) err, strerror((int)err));
		}

		fprintf(stderr,
			"InnoDB: Some operating system error numbers"
			" are described at\n"
			"InnoDB: "
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3028
			REFMAN "operating-system-error-codes.html\n");
3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069

		os_has_said_disk_full = TRUE;
	}

	return(FALSE);
#else
	ssize_t	ret;

	ret = os_file_pwrite(file, buf, n, offset, offset_high);

	if ((ulint)ret == n) {

		return(TRUE);
	}

	if (!os_has_said_disk_full) {

		ut_print_timestamp(stderr);

		fprintf(stderr,
			"  InnoDB: Error: Write to file %s failed"
			" at offset %lu %lu.\n"
			"InnoDB: %lu bytes should have been written,"
			" only %ld were written.\n"
			"InnoDB: Operating system error number %lu.\n"
			"InnoDB: Check that your OS and file system"
			" support files of this size.\n"
			"InnoDB: Check also that the disk is not full"
			" or a disk quota exceeded.\n",
			name, offset_high, offset, n, (long int)ret,
			(ulint)errno);
		if (strerror(errno) != NULL) {
			fprintf(stderr,
				"InnoDB: Error number %lu means '%s'.\n",
				(ulint)errno, strerror(errno));
		}

		fprintf(stderr,
			"InnoDB: Some operating system error numbers"
			" are described at\n"
			"InnoDB: "
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3070
			REFMAN "operating-system-error-codes.html\n");
3071 3072 3073 3074 3075 3076 3077 3078

		os_has_said_disk_full = TRUE;
	}

	return(FALSE);
#endif
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3079 3080 3081
/*******************************************************************//**
Check the existence and type of the given file.
@return	TRUE if call succeeded */
3082 3083 3084 3085
UNIV_INTERN
ibool
os_file_status(
/*===========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3086 3087 3088
	const char*	path,	/*!< in:	pathname of the file */
	ibool*		exists,	/*!< out: TRUE if file exists */
	os_file_type_t* type)	/*!< out: type of the file (if it exists) */
3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150
{
#ifdef __WIN__
	int		ret;
	struct _stat	statinfo;

	ret = _stat(path, &statinfo);
	if (ret && (errno == ENOENT || errno == ENOTDIR)) {
		/* file does not exist */
		*exists = FALSE;
		return(TRUE);
	} else if (ret) {
		/* file exists, but stat call failed */

		os_file_handle_error_no_exit(path, "stat");

		return(FALSE);
	}

	if (_S_IFDIR & statinfo.st_mode) {
		*type = OS_FILE_TYPE_DIR;
	} else if (_S_IFREG & statinfo.st_mode) {
		*type = OS_FILE_TYPE_FILE;
	} else {
		*type = OS_FILE_TYPE_UNKNOWN;
	}

	*exists = TRUE;

	return(TRUE);
#else
	int		ret;
	struct stat	statinfo;

	ret = stat(path, &statinfo);
	if (ret && (errno == ENOENT || errno == ENOTDIR)) {
		/* file does not exist */
		*exists = FALSE;
		return(TRUE);
	} else if (ret) {
		/* file exists, but stat call failed */

		os_file_handle_error_no_exit(path, "stat");

		return(FALSE);
	}

	if (S_ISDIR(statinfo.st_mode)) {
		*type = OS_FILE_TYPE_DIR;
	} else if (S_ISLNK(statinfo.st_mode)) {
		*type = OS_FILE_TYPE_LINK;
	} else if (S_ISREG(statinfo.st_mode)) {
		*type = OS_FILE_TYPE_FILE;
	} else {
		*type = OS_FILE_TYPE_UNKNOWN;
	}

	*exists = TRUE;

	return(TRUE);
#endif
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3151 3152 3153
/*******************************************************************//**
This function returns information about the specified file
@return	TRUE if stat information found */
3154 3155 3156 3157
UNIV_INTERN
ibool
os_file_get_status(
/*===============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3158 3159
	const char*	path,		/*!< in:	pathname of the file */
	os_file_stat_t* stat_info)	/*!< information of a file in a
3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235
					directory */
{
#ifdef __WIN__
	int		ret;
	struct _stat	statinfo;

	ret = _stat(path, &statinfo);
	if (ret && (errno == ENOENT || errno == ENOTDIR)) {
		/* file does not exist */

		return(FALSE);
	} else if (ret) {
		/* file exists, but stat call failed */

		os_file_handle_error_no_exit(path, "stat");

		return(FALSE);
	}
	if (_S_IFDIR & statinfo.st_mode) {
		stat_info->type = OS_FILE_TYPE_DIR;
	} else if (_S_IFREG & statinfo.st_mode) {
		stat_info->type = OS_FILE_TYPE_FILE;
	} else {
		stat_info->type = OS_FILE_TYPE_UNKNOWN;
	}

	stat_info->ctime = statinfo.st_ctime;
	stat_info->atime = statinfo.st_atime;
	stat_info->mtime = statinfo.st_mtime;
	stat_info->size	 = statinfo.st_size;

	return(TRUE);
#else
	int		ret;
	struct stat	statinfo;

	ret = stat(path, &statinfo);

	if (ret && (errno == ENOENT || errno == ENOTDIR)) {
		/* file does not exist */

		return(FALSE);
	} else if (ret) {
		/* file exists, but stat call failed */

		os_file_handle_error_no_exit(path, "stat");

		return(FALSE);
	}

	if (S_ISDIR(statinfo.st_mode)) {
		stat_info->type = OS_FILE_TYPE_DIR;
	} else if (S_ISLNK(statinfo.st_mode)) {
		stat_info->type = OS_FILE_TYPE_LINK;
	} else if (S_ISREG(statinfo.st_mode)) {
		stat_info->type = OS_FILE_TYPE_FILE;
	} else {
		stat_info->type = OS_FILE_TYPE_UNKNOWN;
	}

	stat_info->ctime = statinfo.st_ctime;
	stat_info->atime = statinfo.st_atime;
	stat_info->mtime = statinfo.st_mtime;
	stat_info->size	 = statinfo.st_size;

	return(TRUE);
#endif
}

/* path name separator character */
#ifdef __WIN__
#  define OS_FILE_PATH_SEPARATOR	'\\'
#else
#  define OS_FILE_PATH_SEPARATOR	'/'
#endif

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3236
/****************************************************************//**
3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261
The function os_file_dirname returns a directory component of a
null-terminated pathname string.  In the usual case, dirname returns
the string up to, but not including, the final '/', and basename
is the component following the final '/'.  Trailing '/' charac­
ters are not counted as part of the pathname.

If path does not contain a slash, dirname returns the string ".".

Concatenating the string returned by dirname, a "/", and the basename
yields a complete pathname.

The return value is  a copy of the directory component of the pathname.
The copy is allocated from heap. It is the caller responsibility
to free it after it is no longer needed.

The following list of examples (taken from SUSv2) shows the strings
returned by dirname and basename for different paths:

       path	      dirname	     basename
       "/usr/lib"     "/usr"	     "lib"
       "/usr/"	      "/"	     "usr"
       "usr"	      "."	     "usr"
       "/"	      "/"	     "/"
       "."	      "."	     "."
       ".."	      "."	     ".."
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3262 3263

@return	own: directory component of the pathname */
3264 3265 3266 3267
UNIV_INTERN
char*
os_file_dirname(
/*============*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3268
	const char*	path)	/*!< in: pathname */
3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290
{
	/* Find the offset of the last slash */
	const char* last_slash = strrchr(path, OS_FILE_PATH_SEPARATOR);
	if (!last_slash) {
		/* No slash in the path, return "." */

		return(mem_strdup("."));
	}

	/* Ok, there is a slash */

	if (last_slash == path) {
		/* last slash is the first char of the path */

		return(mem_strdup("/"));
	}

	/* Non-trivial directory component */

	return(mem_strdupl(path, last_slash - path));
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3291 3292 3293
/****************************************************************//**
Creates all missing subdirectories along the given path.
@return	TRUE if call succeeded FALSE otherwise */
3294 3295 3296 3297
UNIV_INTERN
ibool
os_file_create_subdirs_if_needed(
/*=============================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3298
	const char*	path)	/*!< in: path name */
3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330
{
	char*		subdir;
	ibool		success, subdir_exists;
	os_file_type_t	type;

	subdir = os_file_dirname(path);
	if (strlen(subdir) == 1
	    && (*subdir == OS_FILE_PATH_SEPARATOR || *subdir == '.')) {
		/* subdir is root or cwd, nothing to do */
		mem_free(subdir);

		return(TRUE);
	}

	/* Test if subdir exists */
	success = os_file_status(subdir, &subdir_exists, &type);
	if (success && !subdir_exists) {
		/* subdir does not exist, create it */
		success = os_file_create_subdirs_if_needed(subdir);
		if (!success) {
			mem_free(subdir);

			return(FALSE);
		}
		success = os_file_create_directory(subdir, FALSE);
	}

	mem_free(subdir);

	return(success);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3331 3332 3333 3334
#ifndef UNIV_HOTBACKUP
/****************************************************************//**
Returns a pointer to the nth slot in the aio array.
@return	pointer to slot */
3335 3336 3337 3338
static
os_aio_slot_t*
os_aio_array_get_nth_slot(
/*======================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3339 3340
	os_aio_array_t*		array,	/*!< in: aio array */
	ulint			index)	/*!< in: index of the slot */
3341 3342 3343 3344 3345 3346
{
	ut_a(index < array->n_slots);

	return((array->slots) + index);
}

Sergei Golubchik's avatar
Sergei Golubchik committed
3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429
#if defined(LINUX_NATIVE_AIO)
/******************************************************************//**
Creates an io_context for native linux AIO.
@return	TRUE on success. */
static
ibool
os_aio_linux_create_io_ctx(
/*=======================*/
	ulint		max_events,	/*!< in: number of events. */
	io_context_t*	io_ctx)		/*!< out: io_ctx to initialize. */
{
	int	ret;
	ulint	retries = 0;

retry:
	memset(io_ctx, 0x0, sizeof(*io_ctx));

	/* Initialize the io_ctx. Tell it how many pending
	IO requests this context will handle. */

	ret = io_setup(max_events, io_ctx);
	if (ret == 0) {
#if defined(UNIV_AIO_DEBUG)
		fprintf(stderr,
			"InnoDB: Linux native AIO:"
			" initialized io_ctx for segment\n");
#endif
		/* Success. Return now. */
		return(TRUE);
	}

	/* If we hit EAGAIN we'll make a few attempts before failing. */

	switch (ret) {
	case -EAGAIN:
		if (retries == 0) {
			/* First time around. */
			ut_print_timestamp(stderr);
			fprintf(stderr,
				"  InnoDB: Warning: io_setup() failed"
				" with EAGAIN. Will make %d attempts"
				" before giving up.\n",
				OS_AIO_IO_SETUP_RETRY_ATTEMPTS);
		}

		if (retries < OS_AIO_IO_SETUP_RETRY_ATTEMPTS) {
			++retries;
			fprintf(stderr,
				"InnoDB: Warning: io_setup() attempt"
				" %lu failed.\n",
				retries);
			os_thread_sleep(OS_AIO_IO_SETUP_RETRY_SLEEP);
			goto retry;
		}

		/* Have tried enough. Better call it a day. */
		ut_print_timestamp(stderr);
		fprintf(stderr,
			"  InnoDB: Error: io_setup() failed"
			" with EAGAIN after %d attempts.\n",
			OS_AIO_IO_SETUP_RETRY_ATTEMPTS);
		break;

	case -ENOSYS:
		ut_print_timestamp(stderr);
		fprintf(stderr,
			"  InnoDB: Error: Linux Native AIO interface"
			" is not supported on this platform. Please"
			" check your OS documentation and install"
			" appropriate binary of InnoDB.\n");

		break;

	default:
		ut_print_timestamp(stderr);
		fprintf(stderr,
			"  InnoDB: Error: Linux Native AIO setup"
			" returned following error[%d]\n", -ret);
		break;
	}

	fprintf(stderr,
		"InnoDB: You can disable Linux Native AIO by"
3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514
		" setting innodb_use_native_aio = 0 in my.cnf\n");
	return(FALSE);
}

/******************************************************************//**
Checks if the system supports native linux aio. On some kernel
versions where native aio is supported it won't work on tmpfs. In such
cases we can't use native aio as it is not possible to mix simulated
and native aio.
@return: TRUE if supported, FALSE otherwise. */
static
ibool
os_aio_native_aio_supported(void)
/*=============================*/
{
	int			fd;
	byte*			buf;
	byte*			ptr;
	struct io_event		io_event;
	io_context_t		io_ctx;
	struct iocb		iocb;
	struct iocb*		p_iocb;
	int			err;

	if (!os_aio_linux_create_io_ctx(1, &io_ctx)) {
		/* The platform does not support native aio. */
		return(FALSE);
	}

	/* Now check if tmpdir supports native aio ops. */
	fd = innobase_mysql_tmpfile();

	if (fd < 0) {
		ut_print_timestamp(stderr);
		fprintf(stderr, " InnoDB: Error: unable to create "
			"temp file to check native AIO support.\n");

		return(FALSE);
	}

	memset(&io_event, 0x0, sizeof(io_event));

	buf = (byte*) ut_malloc(UNIV_PAGE_SIZE * 2);
	ptr = (byte*) ut_align(buf, UNIV_PAGE_SIZE);

	/* Suppress valgrind warning. */
	memset(buf, 0x00, UNIV_PAGE_SIZE * 2);

	memset(&iocb, 0x0, sizeof(iocb));
	p_iocb = &iocb;
	io_prep_pwrite(p_iocb, fd, ptr, UNIV_PAGE_SIZE, 0);

	err = io_submit(io_ctx, 1, &p_iocb);
	if (err >= 1) {
		/* Now collect the submitted IO request. */
		err = io_getevents(io_ctx, 1, 1, &io_event, NULL);
	}

	ut_free(buf);
	close(fd);

	switch (err) {
	case 1:
		return(TRUE);

	case -EINVAL:
	case -ENOSYS:
		ut_print_timestamp(stderr);
		fprintf(stderr,
			" InnoDB: Error: Linux Native AIO is not"
			" supported on tmpdir.\n"
			"InnoDB: You can either move tmpdir to a"
			" file system that supports native AIO\n"
			"InnoDB: or you can set"
			" innodb_use_native_aio to FALSE to avoid"
			" this message.\n");

		/* fall through. */
	default:
		ut_print_timestamp(stderr);
		fprintf(stderr,
			" InnoDB: Error: Linux Native AIO check"
			" on tmpdir returned error[%d]\n", -err);
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
3515 3516 3517 3518 3519 3520 3521 3522 3523
	return(FALSE);
}
#endif /* LINUX_NATIVE_AIO */

/******************************************************************//**
Creates an aio wait array. Note that we return NULL in case of failure.
We don't care about freeing memory here because we assume that a
failure will result in server refusing to start up.
@return	own: aio array, NULL on failure */
3524 3525 3526 3527
static
os_aio_array_t*
os_aio_array_create(
/*================*/
Sergei Golubchik's avatar
Sergei Golubchik committed
3528 3529 3530
	ulint	n,		/*!< in: maximum number of pending aio
				operations allowed; n must be
				divisible by n_segments */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3531
	ulint	n_segments)	/*!< in: number of segments in the aio array */
3532 3533 3534 3535
{
	os_aio_array_t*	array;
	ulint		i;
	os_aio_slot_t*	slot;
Sergei Golubchik's avatar
Sergei Golubchik committed
3536
#ifdef LINUX_NATIVE_AIO
Sergei Golubchik's avatar
Sergei Golubchik committed
3537
	struct io_event*	io_event = NULL;
3538 3539 3540 3541 3542 3543
#endif
	ut_a(n > 0);
	ut_a(n_segments > 0);

	array = ut_malloc(sizeof(os_aio_array_t));

Sergei Golubchik's avatar
Sergei Golubchik committed
3544
	array->mutex		= os_mutex_create();
3545 3546 3547 3548 3549 3550 3551 3552
	array->not_full		= os_event_create(NULL);
	array->is_empty		= os_event_create(NULL);

	os_event_set(array->is_empty);

	array->n_slots		= n;
	array->n_segments	= n_segments;
	array->n_reserved	= 0;
Sergei Golubchik's avatar
Sergei Golubchik committed
3553
	array->cur_seg		= 0;
3554
	array->slots		= ut_malloc(n * sizeof(os_aio_slot_t));
Sergei Golubchik's avatar
Sergei Golubchik committed
3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574

#if defined(LINUX_NATIVE_AIO)
	array->aio_ctx = NULL;
	array->aio_events = NULL;

	/* If we are not using native aio interface then skip this
	part of initialization. */
	if (!srv_use_native_aio) {
		goto skip_native_aio;
	}

	/* Initialize the io_context array. One io_context
	per segment in the array. */

	array->aio_ctx = ut_malloc(n_segments *
				   sizeof(*array->aio_ctx));
	for (i = 0; i < n_segments; ++i) {
		if (!os_aio_linux_create_io_ctx(n/n_segments,
					   &array->aio_ctx[i])) {
			/* If something bad happened during aio setup
3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591
			we disable linux native aio.
                        The disadvantage will be a small memory leak
                        at shutdown but that's ok compared to a crash
                        or a not working server.
                        This frequently happens when running the test suite
                        with many threads on a system with low fs.aio-max-nr!
                        */

                        fprintf(stderr,
                                "  InnoDB: Warning: Linux Native AIO disabled "
                                "because os_aio_linux_create_io_ctx() "
                                "failed. To get rid of this warning you can "
                                "try increasing system "
                                "fs.aio-max-nr to 1048576 or larger or "
                                "setting innodb_use_native_aio = 0 in my.cnf\n");
                        srv_use_native_aio = FALSE;
			goto skip_native_aio;
Sergei Golubchik's avatar
Sergei Golubchik committed
3592 3593 3594 3595 3596 3597 3598 3599 3600 3601
		}
	}

	/* Initialize the event array. One event per slot. */
	io_event = ut_malloc(n * sizeof(*io_event));
	memset(io_event, 0x0, sizeof(*io_event) * n);
	array->aio_events = io_event;

skip_native_aio:
#endif /* LINUX_NATIVE_AIO */
3602 3603 3604 3605
	for (i = 0; i < n; i++) {
		slot = os_aio_array_get_nth_slot(array, i);
		slot->pos = i;
		slot->reserved = FALSE;
Sergei Golubchik's avatar
Sergei Golubchik committed
3606
#ifdef LINUX_NATIVE_AIO
3607

Sergei Golubchik's avatar
Sergei Golubchik committed
3608 3609 3610
		memset(&slot->control, 0x0, sizeof(slot->control));
		slot->n_bytes = 0;
		slot->ret = 0;
3611 3612 3613 3614 3615 3616
#endif
	}

	return(array);
}

3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628
/************************************************************************//**
Frees an aio wait array. */
static
void
os_aio_array_free(
/*==============*/
	os_aio_array_t*	array)	/*!< in, own: array to free */
{
	os_mutex_free(array->mutex);
	os_event_free(array->not_full);
	os_event_free(array->is_empty);

Sergei Golubchik's avatar
Sergei Golubchik committed
3629 3630 3631 3632 3633 3634 3635
#if defined(LINUX_NATIVE_AIO)
	if (srv_use_native_aio) {
		ut_free(array->aio_events);
		ut_free(array->aio_ctx);
	}
#endif /* LINUX_NATIVE_AIO */

3636 3637 3638 3639
	ut_free(array->slots);
	ut_free(array);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3640 3641 3642 3643 3644 3645 3646
/***********************************************************************
Initializes the asynchronous io system. Creates one array each for ibuf
and log i/o. Also creates one array each for read and write where each
array is divided logically into n_read_segs and n_write_segs
respectively. The caller must create an i/o handler thread for each
segment in these arrays. This function also creates the sync array.
No i/o handler thread needs to be created for that */
3647
UNIV_INTERN
Sergei Golubchik's avatar
Sergei Golubchik committed
3648
ibool
3649 3650
os_aio_init(
/*========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3651 3652 3653 3654 3655 3656
	ulint	n_per_seg,	/*<! in: maximum number of pending aio
				operations allowed per segment */
	ulint	n_read_segs,	/*<! in: number of reader threads */
	ulint	n_write_segs,	/*<! in: number of writer threads */
	ulint	n_slots_sync)	/*<! in: number of slots in the sync aio
				array */
3657 3658
{
	ulint	i;
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3659
	ulint 	n_segments = 2 + n_read_segs + n_write_segs;
Vadim Tkachenko's avatar
Vadim Tkachenko committed
3660

3661 3662 3663 3664
	ut_ad(n_segments >= 4);

	os_io_init_simple();

3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677
#if defined(LINUX_NATIVE_AIO)
	/* Check if native aio is supported on this system and tmpfs */
	if (srv_use_native_aio
	    && !os_aio_native_aio_supported()) {

		ut_print_timestamp(stderr);
		fprintf(stderr,
			" InnoDB: Warning: Linux Native AIO"
			" disabled.\n");
		srv_use_native_aio = FALSE;
	}
#endif /* LINUX_NATIVE_AIO */

3678 3679 3680 3681 3682 3683 3684 3685
	for (i = 0; i < n_segments; i++) {
		srv_set_io_thread_op_info(i, "not started yet");
	}


	/* fprintf(stderr, "Array n per seg %lu\n", n_per_seg); */

	os_aio_ibuf_array = os_aio_array_create(n_per_seg, 1);
Sergei Golubchik's avatar
Sergei Golubchik committed
3686 3687 3688
	if (os_aio_ibuf_array == NULL) {
		goto err_exit;
	}
3689 3690 3691 3692

	srv_io_thread_function[0] = "insert buffer thread";

	os_aio_log_array = os_aio_array_create(n_per_seg, 1);
Sergei Golubchik's avatar
Sergei Golubchik committed
3693 3694 3695
	if (os_aio_log_array == NULL) {
		goto err_exit;
	}
3696 3697 3698

	srv_io_thread_function[1] = "log thread";

Sergei Golubchik's avatar
Sergei Golubchik committed
3699
	os_aio_read_array = os_aio_array_create(n_read_segs * n_per_seg,
3700
						n_read_segs);
Sergei Golubchik's avatar
Sergei Golubchik committed
3701 3702 3703 3704
	if (os_aio_read_array == NULL) {
		goto err_exit;
	}

3705 3706 3707 3708 3709
	for (i = 2; i < 2 + n_read_segs; i++) {
		ut_a(i < SRV_MAX_N_IO_THREADS);
		srv_io_thread_function[i] = "read thread";
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
3710
	os_aio_write_array = os_aio_array_create(n_write_segs * n_per_seg,
3711
						 n_write_segs);
Sergei Golubchik's avatar
Sergei Golubchik committed
3712 3713 3714 3715
	if (os_aio_write_array == NULL) {
		goto err_exit;
	}

3716 3717 3718 3719 3720 3721
	for (i = 2 + n_read_segs; i < n_segments; i++) {
		ut_a(i < SRV_MAX_N_IO_THREADS);
		srv_io_thread_function[i] = "write thread";
	}

	os_aio_sync_array = os_aio_array_create(n_slots_sync, 1);
Sergei Golubchik's avatar
Sergei Golubchik committed
3722 3723 3724 3725
	if (os_aio_sync_array == NULL) {
		goto err_exit;
	}

3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738

	os_aio_n_segments = n_segments;

	os_aio_validate();

	os_aio_segment_wait_events = ut_malloc(n_segments * sizeof(void*));

	for (i = 0; i < n_segments; i++) {
		os_aio_segment_wait_events[i] = os_event_create(NULL);
	}

	os_last_printout = time(NULL);

3739
#ifdef _WIN32
3740
	ut_a(completion_port == 0 && read_completion_port == 0);
3741
	completion_port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
3742 3743
	read_completion_port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
	ut_a(completion_port && read_completion_port);
3744
#endif
Sergei Golubchik's avatar
Sergei Golubchik committed
3745

Sergei Golubchik's avatar
Sergei Golubchik committed
3746 3747 3748 3749
	return(TRUE);

err_exit:
	return(FALSE);
Vadim Tkachenko's avatar
Vadim Tkachenko committed
3750
}
3751

3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780
/***********************************************************************
Frees the asynchronous io system. */
UNIV_INTERN
void
os_aio_free(void)
/*=============*/
{
	ulint	i;

	os_aio_array_free(os_aio_ibuf_array);
	os_aio_ibuf_array = NULL;
	os_aio_array_free(os_aio_log_array);
	os_aio_log_array = NULL;
	os_aio_array_free(os_aio_read_array);
	os_aio_read_array = NULL;
	os_aio_array_free(os_aio_write_array);
	os_aio_write_array = NULL;
	os_aio_array_free(os_aio_sync_array);
	os_aio_sync_array = NULL;

	for (i = 0; i < os_aio_n_segments; i++) {
		os_event_free(os_aio_segment_wait_events[i]);
	}

	ut_free(os_aio_segment_wait_events);
	os_aio_segment_wait_events = 0;
	os_aio_n_segments = 0;
}

3781
#ifdef WIN_ASYNC_IO
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3782
/************************************************************************//**
3783 3784 3785 3786 3787 3788
Wakes up all async i/o threads in the array in Windows async i/o at
shutdown. */
static
void
os_aio_array_wake_win_aio_at_shutdown(
/*==================================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3789
	os_aio_array_t*	array)	/*!< in: aio array */
3790
{
3791 3792
	if(completion_port)
	{
3793
		PostQueuedCompletionStatus(completion_port, 0, IOCP_SHUTDOWN_KEY, NULL);
3794
		PostQueuedCompletionStatus(read_completion_port, 0, IOCP_SHUTDOWN_KEY, NULL);
3795 3796 3797 3798
	}
}
#endif

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3799
/************************************************************************//**
3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814
Wakes up all async i/o threads so that they know to exit themselves in
shutdown. */
UNIV_INTERN
void
os_aio_wake_all_threads_at_shutdown(void)
/*=====================================*/
{
	ulint	i;

#ifdef WIN_ASYNC_IO
	/* This code wakes up all ai/o threads in Windows native aio */
	os_aio_array_wake_win_aio_at_shutdown(os_aio_read_array);
	os_aio_array_wake_win_aio_at_shutdown(os_aio_write_array);
	os_aio_array_wake_win_aio_at_shutdown(os_aio_ibuf_array);
	os_aio_array_wake_win_aio_at_shutdown(os_aio_log_array);
Sergei Golubchik's avatar
Sergei Golubchik committed
3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827

#elif defined(LINUX_NATIVE_AIO)

	/* When using native AIO interface the io helper threads
	wait on io_getevents with a timeout value of 500ms. At
	each wake up these threads check the server status.
	No need to do anything to wake them up. */

	if (srv_use_native_aio) {
		return;
	}
	/* Fall through to simulated AIO handler wakeup if we are
	not using native AIO. */
3828 3829 3830 3831 3832 3833 3834 3835 3836
#endif
	/* This loop wakes up all simulated ai/o threads */

	for (i = 0; i < os_aio_n_segments; i++) {

		os_event_set(os_aio_segment_wait_events[i]);
	}
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3837
/************************************************************************//**
3838 3839 3840 3841 3842 3843 3844 3845 3846 3847
Waits until there are no pending writes in os_aio_write_array. There can
be other, synchronous, pending writes. */
UNIV_INTERN
void
os_aio_wait_until_no_pending_writes(void)
/*=====================================*/
{
	os_event_wait(os_aio_write_array->is_empty);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3848 3849 3850 3851
/**********************************************************************//**
Calculates segment number for a slot.
@return segment number (which is the number used by, for example,
i/o-handler threads) */
3852 3853 3854 3855
static
ulint
os_aio_get_segment_no_from_slot(
/*============================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3856 3857
	os_aio_array_t*	array,	/*!< in: aio wait array */
	os_aio_slot_t*	slot)	/*!< in: slot in this array */
3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884
{
	ulint	segment;
	ulint	seg_len;

	if (array == os_aio_ibuf_array) {
		segment = 0;

	} else if (array == os_aio_log_array) {
		segment = 1;

	} else if (array == os_aio_read_array) {
		seg_len = os_aio_read_array->n_slots
			/ os_aio_read_array->n_segments;

		segment = 2 + slot->pos / seg_len;
	} else {
		ut_a(array == os_aio_write_array);
		seg_len = os_aio_write_array->n_slots
			/ os_aio_write_array->n_segments;

		segment = os_aio_read_array->n_segments + 2
			+ slot->pos / seg_len;
	}

	return(segment);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3885 3886 3887
/**********************************************************************//**
Calculates local segment number and aio array from global segment number.
@return	local segment number within the aio array */
3888 3889 3890 3891
static
ulint
os_aio_get_array_and_local_segment(
/*===============================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3892 3893
	os_aio_array_t** array,		/*!< out: aio wait array */
	ulint		 global_segment)/*!< in: global segment number */
3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919
{
	ulint	segment;

	ut_a(global_segment < os_aio_n_segments);

	if (global_segment == 0) {
		*array = os_aio_ibuf_array;
		segment = 0;

	} else if (global_segment == 1) {
		*array = os_aio_log_array;
		segment = 0;

	} else if (global_segment < os_aio_read_array->n_segments + 2) {
		*array = os_aio_read_array;

		segment = global_segment - 2;
	} else {
		*array = os_aio_write_array;

		segment = global_segment - (os_aio_read_array->n_segments + 2);
	}

	return(segment);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3920
/*******************************************************************//**
3921
Requests for a slot in the aio array. If no slot is available, waits until
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3922 3923
not_full-event becomes signaled.
@return	pointer to slot */
3924 3925 3926 3927
static
os_aio_slot_t*
os_aio_array_reserve_slot(
/*======================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3928 3929 3930
	ulint		type,	/*!< in: OS_FILE_READ or OS_FILE_WRITE */
	os_aio_array_t*	array,	/*!< in: aio array */
	fil_node_t*	message1,/*!< in: message to be passed along with
3931
				the aio operation */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3932
	void*		message2,/*!< in: message to be passed along with
3933
				the aio operation */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3934 3935
	os_file_t	file,	/*!< in: file handle */
	const char*	name,	/*!< in: name of the file or path as a
3936
				null-terminated string */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3937
	void*		buf,	/*!< in: buffer where to read or from which
3938
				to write */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3939
	ulint		offset,	/*!< in: least significant 32 bits of file
3940
				offset */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3941
	ulint		offset_high, /*!< in: most significant 32 bits of
3942
				offset */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3943
	ulint		len,	/*!< in: length of the block to read or write */
3944
	ulint		space_id)
3945
{
Sergei Golubchik's avatar
Sergei Golubchik committed
3946 3947 3948 3949 3950 3951 3952 3953 3954 3955
	os_aio_slot_t*	slot = NULL;
#ifdef WIN_ASYNC_IO
	OVERLAPPED*	control;

#elif defined(LINUX_NATIVE_AIO)

	struct iocb*	iocb;
	off_t		aio_offset;

#endif
3956
	ulint		i;
Sergei Golubchik's avatar
Sergei Golubchik committed
3957
	ulint		counter;
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3958 3959
	ulint		slots_per_seg;
	ulint		local_seg;
3960

Sergei Golubchik's avatar
Sergei Golubchik committed
3961
#ifdef WIN_ASYNC_IO
3962 3963
	ut_a((len & 0xFFFFFFFFUL) == len);
#endif
3964

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
3965 3966 3967 3968 3969 3970 3971 3972
	/* No need of a mutex. Only reading constant fields */
	slots_per_seg = array->n_slots / array->n_segments;

	/* We attempt to keep adjacent blocks in the same local
	segment. This can help in merging IO requests when we are
	doing simulated AIO */
	local_seg = (offset >> (UNIV_PAGE_SIZE_SHIFT + 6))
		    % array->n_segments;
3973

3974 3975 3976 3977 3978 3979
loop:
	os_mutex_enter(array->mutex);

	if (array->n_reserved == array->n_slots) {
		os_mutex_exit(array->mutex);

Sergei Golubchik's avatar
Sergei Golubchik committed
3980
		if (!srv_use_native_aio) {
3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991
			/* If the handler threads are suspended, wake them
			so that we get more slots */

			os_aio_simulated_wake_handler_threads();
		}

		os_event_wait(array->not_full);

		goto loop;
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
3992 3993 3994 3995 3996
	/* We start our search for an available slot from our preferred
	local segment and do a full scan of the array. We are
	guaranteed to find a slot in full scan. */
	for (i = local_seg * slots_per_seg, counter = 0;
	     counter < array->n_slots; i++, counter++) {
3997

Sergei Golubchik's avatar
Sergei Golubchik committed
3998
		i %= array->n_slots;
3999 4000 4001
		slot = os_aio_array_get_nth_slot(array, i);

		if (slot->reserved == FALSE) {
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4002
			goto found;
4003 4004 4005
		}
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
4006 4007 4008
	/* We MUST always be able to get hold of a reserved slot. */
	ut_error;

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4009 4010
found:
	ut_a(slot->reserved == FALSE);
4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031
	array->n_reserved++;

	if (array->n_reserved == 1) {
		os_event_reset(array->is_empty);
	}

	if (array->n_reserved == array->n_slots) {
		os_event_reset(array->not_full);
	}

	slot->reserved = TRUE;
	slot->reservation_time = time(NULL);
	slot->message1 = message1;
	slot->message2 = message2;
	slot->file     = file;
	slot->name     = name;
	slot->len      = len;
	slot->type     = type;
	slot->buf      = buf;
	slot->offset   = offset;
	slot->offset_high = offset_high;
Sergei Golubchik's avatar
Sergei Golubchik committed
4032
	slot->io_already_done = FALSE;
4033
	slot->space_id = space_id;
4034 4035 4036 4037 4038

#ifdef WIN_ASYNC_IO
	control = &(slot->control);
	control->Offset = (DWORD)offset;
	control->OffsetHigh = (DWORD)offset_high;
4039 4040
	control->hEvent = 0;
	slot->arr = array;
Sergei Golubchik's avatar
Sergei Golubchik committed
4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067

#elif defined(LINUX_NATIVE_AIO)

	/* If we are not using native AIO skip this part. */
	if (!srv_use_native_aio) {
		goto skip_native_aio;
	}

	/* Check if we are dealing with 64 bit arch.
	If not then make sure that offset fits in 32 bits. */
	if (sizeof(aio_offset) == 8) {
		aio_offset = offset_high;
		aio_offset <<= 32;
		aio_offset += offset;
	} else {
		ut_a(offset_high == 0);
		aio_offset = offset;
	}

	iocb = &slot->control;

	if (type == OS_FILE_READ) {
		io_prep_pread(iocb, file, buf, len, aio_offset);
	} else {
		ut_a(type == OS_FILE_WRITE);
		io_prep_pwrite(iocb, file, buf, len, aio_offset);
	}
Vadim Tkachenko's avatar
Vadim Tkachenko committed
4068

Sergei Golubchik's avatar
Sergei Golubchik committed
4069 4070 4071 4072 4073 4074 4075 4076
	iocb->data = (void*)slot;
	slot->n_bytes = 0;
	slot->ret = 0;
	/*fprintf(stderr, "Filled up Linux native iocb.\n");*/
	

skip_native_aio:
#endif /* LINUX_NATIVE_AIO */
4077 4078 4079 4080 4081
	os_mutex_exit(array->mutex);

	return(slot);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4082
/*******************************************************************//**
4083 4084 4085 4086 4087
Frees a slot in the aio array. */
static
void
os_aio_array_free_slot(
/*===================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4088 4089
	os_aio_array_t*	array,	/*!< in: aio array */
	os_aio_slot_t*	slot)	/*!< in: pointer to slot */
4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109
{
	ut_ad(array);
	ut_ad(slot);

	os_mutex_enter(array->mutex);

	ut_ad(slot->reserved);

	slot->reserved = FALSE;

	array->n_reserved--;

	if (array->n_reserved == array->n_slots - 1) {
		os_event_set(array->not_full);
	}

	if (array->n_reserved == 0) {
		os_event_set(array->is_empty);
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
4110
#ifdef LINUX_NATIVE_AIO
Sergei Golubchik's avatar
Sergei Golubchik committed
4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123

	if (srv_use_native_aio) {
		memset(&slot->control, 0x0, sizeof(slot->control));
		slot->n_bytes = 0;
		slot->ret = 0;
		/*fprintf(stderr, "Freed up Linux native slot.\n");*/
	} else {
		/* These fields should not be used if we are not
		using native AIO. */
		ut_ad(slot->n_bytes == 0);
		ut_ad(slot->ret == 0);
	}

4124 4125 4126 4127
#endif
	os_mutex_exit(array->mutex);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4128
/**********************************************************************//**
4129 4130 4131 4132 4133
Wakes up a simulated aio i/o-handler thread if it has something to do. */
static
void
os_aio_simulated_wake_handler_thread(
/*=================================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4134
	ulint	global_segment)	/*!< in: the number of the segment in the aio
4135 4136 4137 4138
				arrays */
{
	os_aio_array_t*	array;
	os_aio_slot_t*	slot;
4139
	ulint		segment __attribute__ ((unused));
4140 4141 4142
	ulint		n;
	ulint		i;

Sergei Golubchik's avatar
Sergei Golubchik committed
4143
	ut_ad(!srv_use_native_aio);
4144 4145 4146

	segment = os_aio_get_array_and_local_segment(&array, global_segment);

Sergei Golubchik's avatar
Sergei Golubchik committed
4147
	n = array->n_slots / array->n_segments;
4148 4149 4150 4151 4152 4153

	/* Look through n slots after the segment * n'th slot */

	os_mutex_enter(array->mutex);

	for (i = 0; i < n; i++) {
Sergei Golubchik's avatar
Sergei Golubchik committed
4154
		slot = os_aio_array_get_nth_slot(array, i + segment * n);
4155

Sergei Golubchik's avatar
Sergei Golubchik committed
4156
		if (slot->reserved) {
4157 4158 4159 4160 4161 4162 4163 4164 4165
			/* Found an i/o request */

			break;
		}
	}

	os_mutex_exit(array->mutex);

	if (i < n) {
Sergei Golubchik's avatar
Sergei Golubchik committed
4166
		os_event_set(os_aio_segment_wait_events[global_segment]);
4167 4168 4169
	}
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4170
/**********************************************************************//**
4171 4172 4173 4174 4175 4176
Wakes up simulated aio i/o-handler threads if they have something to do. */
UNIV_INTERN
void
os_aio_simulated_wake_handler_threads(void)
/*=======================================*/
{
Sergei Golubchik's avatar
Sergei Golubchik committed
4177 4178 4179
	ulint	i;

	if (srv_use_native_aio) {
4180 4181 4182 4183 4184 4185 4186
		/* We do not use simulated aio: do nothing */

		return;
	}

	os_aio_recommend_sleep_for_read_threads	= FALSE;

Sergei Golubchik's avatar
Sergei Golubchik committed
4187 4188 4189
	for (i = 0; i < os_aio_n_segments; i++) {
		os_aio_simulated_wake_handler_thread(i);
	}
4190 4191
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4192
/**********************************************************************//**
4193 4194 4195 4196 4197 4198 4199 4200 4201
This function can be called if one wants to post a batch of reads and
prefers an i/o-handler thread to handle them all at once later. You must
call os_aio_simulated_wake_handler_threads later to ensure the threads
are not left sleeping! */
UNIV_INTERN
void
os_aio_simulated_put_read_threads_to_sleep(void)
/*============================================*/
{
4202 4203 4204 4205 4206 4207

/* The idea of putting background IO threads to sleep is only for
Windows when using simulated AIO. Windows XP seems to schedule
background threads too eagerly to allow for coalescing during
readahead requests. */
#ifdef __WIN__
4208 4209 4210
	os_aio_array_t*	array;
	ulint		g;

Sergei Golubchik's avatar
Sergei Golubchik committed
4211
	if (srv_use_native_aio) {
4212 4213 4214 4215 4216
		/* We do not use simulated aio: do nothing */

		return;
	}

4217 4218 4219 4220 4221 4222 4223 4224 4225 4226
	os_aio_recommend_sleep_for_read_threads	= TRUE;

	for (g = 0; g < os_aio_n_segments; g++) {
		os_aio_get_array_and_local_segment(&array, g);

		if (array == os_aio_read_array) {

			os_event_reset(os_aio_segment_wait_events[g]);
		}
	}
4227
#endif /* __WIN__ */
4228 4229
}

Sergei Golubchik's avatar
Sergei Golubchik committed
4230
#if defined(LINUX_NATIVE_AIO)
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4231
/*******************************************************************//**
Sergei Golubchik's avatar
Sergei Golubchik committed
4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279
Dispatch an AIO request to the kernel.
@return	TRUE on success. */
static
ibool
os_aio_linux_dispatch(
/*==================*/
	os_aio_array_t*	array,	/*!< in: io request array. */
	os_aio_slot_t*	slot)	/*!< in: an already reserved slot. */
{
	int		ret;
	ulint		io_ctx_index;
	struct iocb*	iocb;

	ut_ad(slot != NULL);
	ut_ad(array);

	ut_a(slot->reserved);

	/* Find out what we are going to work with.
	The iocb struct is directly in the slot.
	The io_context is one per segment. */

	iocb = &slot->control;
	io_ctx_index = (slot->pos * array->n_segments) / array->n_slots;

	ret = io_submit(array->aio_ctx[io_ctx_index], 1, &iocb);

#if defined(UNIV_AIO_DEBUG)
	fprintf(stderr,
		"io_submit[%c] ret[%d]: slot[%p] ctx[%p] seg[%lu]\n",
		(slot->type == OS_FILE_WRITE) ? 'w' : 'r', ret, slot,
		array->aio_ctx[io_ctx_index], (ulong)io_ctx_index);
#endif

	/* io_submit returns number of successfully
	queued requests or -errno. */
	if (UNIV_UNLIKELY(ret != 1)) {
		errno = -ret;
		return(FALSE);
	}

	return(TRUE);
}
#endif /* LINUX_NATIVE_AIO */


/*******************************************************************//**
NOTE! Use the corresponding macro os_aio(), not directly this function!
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4280 4281
Requests an asynchronous i/o operation.
@return	TRUE if request was queued successfully, FALSE if fail */
4282 4283
UNIV_INTERN
ibool
Sergei Golubchik's avatar
Sergei Golubchik committed
4284 4285
os_aio_func(
/*========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4286 4287
	ulint		type,	/*!< in: OS_FILE_READ or OS_FILE_WRITE */
	ulint		mode,	/*!< in: OS_AIO_NORMAL, ..., possibly ORed
4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299
				to OS_AIO_SIMULATED_WAKE_LATER: the
				last flag advises this function not to wake
				i/o-handler threads, but the caller will
				do the waking explicitly later, in this
				way the caller can post several requests in
				a batch; NOTE that the batch must not be
				so big that it exhausts the slots in aio
				arrays! NOTE that a simulated batch
				may introduce hidden chances of deadlocks,
				because i/os are not actually handled until
				all have been posted: use with great
				caution! */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4300
	const char*	name,	/*!< in: name of the file or path as a
4301
				null-terminated string */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4302 4303
	os_file_t	file,	/*!< in: handle to a file */
	void*		buf,	/*!< in: buffer where to read or from which
4304
				to write */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4305
	ulint		offset,	/*!< in: least significant 32 bits of file
4306
				offset where to read or write */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4307
	ulint		offset_high, /*!< in: most significant 32 bits of
4308
				offset */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4309 4310 4311 4312 4313
	ulint		n,	/*!< in: number of bytes to read or write */
	fil_node_t*	message1,/*!< in: message for the aio handler
				(can be used to identify a completed
				aio operation); ignored if mode is
				OS_AIO_SYNC */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4314
	void*		message2,/*!< in: message for the aio handler
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4315 4316 4317
				(can be used to identify a completed
				aio operation); ignored if mode is
				OS_AIO_SYNC */
4318
	ulint		space_id,
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4319
	trx_t*		trx)
4320 4321 4322 4323 4324
{
	os_aio_array_t*	array;
	os_aio_slot_t*	slot;
#ifdef WIN_ASYNC_IO
	DWORD		len		= (DWORD) n;
4325
	BOOL	ret;
4326 4327 4328 4329 4330 4331 4332
#endif
	ibool		retry;
	ulint		wake_later;

	ut_ad(file);
	ut_ad(buf);
	ut_ad(n > 0);
4333 4334
	ut_ad(n % OS_MIN_LOG_BLOCK_SIZE == 0);
	ut_ad(offset % OS_MIN_LOG_BLOCK_SIZE == 0);
Sergei Golubchik's avatar
Sergei Golubchik committed
4335
	ut_ad(os_aio_validate_skip());
4336 4337 4338
#ifdef WIN_ASYNC_IO
	ut_ad((n & 0xFFFFFFFFUL) == n);
#endif
4339 4340 4341 4342

	wake_later = mode & OS_AIO_SIMULATED_WAKE_LATER;
	mode = mode & (~OS_AIO_SIMULATED_WAKE_LATER);

4343 4344 4345
	if (mode == OS_AIO_SYNC) 
	{
		ibool ret;
4346
		/* This is actually an ordinary synchronous read or write:
4347
		no need to use an i/o-handler thread */
4348 4349

		if (type == OS_FILE_READ) {
Sergei Golubchik's avatar
Sergei Golubchik committed
4350
			ret = os_file_read_func(file, buf, offset,
4351
							offset_high, n, trx);
4352
		}
4353 4354
		else {
			ut_a(type == OS_FILE_WRITE);
4355

4356 4357 4358 4359
			ret = os_file_write(name, file, buf, offset, offset_high, n);
		}
		ut_a(ret);
		return ret;
4360 4361 4362
	}

try_again:
Sergei Golubchik's avatar
Sergei Golubchik committed
4363 4364 4365 4366 4367 4368 4369
	switch (mode) {
	case OS_AIO_NORMAL:
		array = (type == OS_FILE_READ)
			? os_aio_read_array
			: os_aio_write_array;
		break;
	case OS_AIO_IBUF:
4370 4371 4372 4373 4374 4375 4376
		ut_ad(type == OS_FILE_READ);
		/* Reduce probability of deadlock bugs in connection with ibuf:
		do not let the ibuf i/o handler sleep */

		wake_later = FALSE;

		array = os_aio_ibuf_array;
Sergei Golubchik's avatar
Sergei Golubchik committed
4377 4378
		break;
	case OS_AIO_LOG:
4379
		array = os_aio_log_array;
Sergei Golubchik's avatar
Sergei Golubchik committed
4380 4381
		break;
	case OS_AIO_SYNC:
4382
		array = os_aio_sync_array;
Sergei Golubchik's avatar
Sergei Golubchik committed
4383 4384 4385 4386 4387 4388 4389

#if defined(LINUX_NATIVE_AIO)
		/* In Linux native AIO we don't use sync IO array. */
		ut_a(!srv_use_native_aio);
#endif /* LINUX_NATIVE_AIO */
		break;
	default:
4390
		ut_error;
Sergei Golubchik's avatar
Sergei Golubchik committed
4391
		array = NULL; /* Eliminate compiler warning */
4392 4393
	}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4394 4395 4396 4397 4398
	if (trx && type == OS_FILE_READ)
	{
		trx->io_reads++;
		trx->io_read += n;
	}
4399
	slot = os_aio_array_reserve_slot(type, array, message1, message2, file,
4400
					 name, buf, offset, offset_high, n, space_id);
4401
	if (type == OS_FILE_READ) {
Sergei Golubchik's avatar
Sergei Golubchik committed
4402
		if (srv_use_native_aio) {
4403
			os_n_file_reads++;
Sergei Golubchik's avatar
Sergei Golubchik committed
4404 4405
			os_bytes_read_since_printout += n;
#ifdef WIN_ASYNC_IO
4406 4407
			ret = ReadFile(file, buf, (DWORD)n, &len,
				       &(slot->control));
4408
			if(!ret && GetLastError() != ERROR_IO_PENDING)
Sergei Golubchik's avatar
Sergei Golubchik committed
4409
				goto err_exit;
Sergei Golubchik's avatar
Sergei Golubchik committed
4410 4411 4412 4413 4414

#elif defined(LINUX_NATIVE_AIO)
			if (!os_aio_linux_dispatch(array, slot)) {
				goto err_exit;
			}
4415 4416 4417 4418 4419 4420 4421 4422 4423
#endif
		} else {
			if (!wake_later) {
				os_aio_simulated_wake_handler_thread(
					os_aio_get_segment_no_from_slot(
						array, slot));
			}
		}
	} else if (type == OS_FILE_WRITE) {
Sergei Golubchik's avatar
Sergei Golubchik committed
4424
		if (srv_use_native_aio) {
4425
			os_n_file_writes++;
Sergei Golubchik's avatar
Sergei Golubchik committed
4426
#ifdef WIN_ASYNC_IO
4427 4428
			ret = WriteFile(file, buf, (DWORD)n, &len,
					&(slot->control));
Sergei Golubchik's avatar
Sergei Golubchik committed
4429

4430
			if(!ret && GetLastError() != ERROR_IO_PENDING)
Sergei Golubchik's avatar
Sergei Golubchik committed
4431
				goto err_exit;
Sergei Golubchik's avatar
Sergei Golubchik committed
4432 4433 4434 4435
#elif defined(LINUX_NATIVE_AIO)
			if (!os_aio_linux_dispatch(array, slot)) {
				goto err_exit;
			}
4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447
#endif
		} else {
			if (!wake_later) {
				os_aio_simulated_wake_handler_thread(
					os_aio_get_segment_no_from_slot(
						array, slot));
			}
		}
	} else {
		ut_error;
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
4448 4449
	/* aio was queued successfully! */
	return(TRUE);
4450

Sergei Golubchik's avatar
Sergei Golubchik committed
4451 4452 4453
#if defined LINUX_NATIVE_AIO || defined WIN_ASYNC_IO
err_exit:
#endif /* LINUX_NATIVE_AIO || WIN_ASYNC_IO */
4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467
	os_aio_array_free_slot(array, slot);

	retry = os_file_handle_error(name,
				     type == OS_FILE_READ
				     ? "aio read" : "aio write");
	if (retry) {

		goto try_again;
	}

	return(FALSE);
}

#ifdef WIN_ASYNC_IO
4468 4469 4470
#define READ_SEGMENT(x) (x < srv_n_read_io_threads)
#define WRITE_SEGMENT(x) !READ_SEGMENT(x)

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4471
/**********************************************************************//**
4472 4473 4474 4475 4476
This function is only used in Windows asynchronous i/o.
Waits for an aio operation to complete. This function is used to wait the
for completed requests. The aio array of pending requests is divided
into segments. The thread specifies which segment or slot it wants to wait
for. NOTE: this function will also take care of freeing the aio slot,
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4477 4478
therefore no other thread is allowed to do the freeing!
@return	TRUE if the aio operation succeeded */
4479 4480 4481 4482
UNIV_INTERN
ibool
os_aio_windows_handle(
/*==================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4483
	ulint	segment,	/*!< in: the number of the segment in the aio
4484 4485 4486 4487 4488 4489 4490
				arrays to wait for; segment 0 is the ibuf
				i/o thread, segment 1 the log i/o thread,
				then follow the non-ibuf read threads, and as
				the last are the non-ibuf write threads; if
				this is ULINT_UNDEFINED, then it means that
				sync aio is used, and this parameter is
				ignored */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4491
	ulint	pos,		/*!< this parameter is used only in sync aio:
4492
				wait for the aio slot at this position */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4493
	fil_node_t**message1,	/*!< out: the messages passed with the aio
4494 4495 4496 4497 4498
				request; note that also in the case where
				the aio operation failed, these output
				parameters are valid and can be used to
				restart the operation, for example */
	void**	message2,
4499 4500
	ulint*	type,		/*!< out: OS_FILE_WRITE or ..._READ */
	ulint*	space_id)
4501 4502 4503 4504 4505 4506
{
	ulint		orig_seg	= segment;
	os_aio_slot_t*	slot;
	ibool		ret_val;
	BOOL		ret;
	DWORD		len;
4507
	BOOL		retry		= FALSE;
4508
	ULONG_PTR key;
4509
	HANDLE port = READ_SEGMENT(segment)? read_completion_port : completion_port;
4510

4511 4512 4513
	for(;;) {
		ret = GetQueuedCompletionStatus(port, &len, &key, 
			(OVERLAPPED **)&slot, INFINITE);
Sergei Golubchik's avatar
Sergei Golubchik committed
4514

4515 4516 4517 4518 4519 4520 4521 4522 4523
		/* If shutdown key was received, repost the shutdown message and exit */
		if (ret && (key == IOCP_SHUTDOWN_KEY)) {
			PostQueuedCompletionStatus(port, 0, key, NULL);
			os_thread_exit(NULL);
		}

		if (srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS) {
			os_thread_exit(NULL);
		}
Sergei Golubchik's avatar
Sergei Golubchik committed
4524

4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547
		if(WRITE_SEGMENT(segment)&& slot->type == OS_FILE_READ) {
			/*
			Redirect read completions  to the dedicated completion port 
			and thread. We need to split read and write threads. If we do not
			do that, and just allow all io threads process all IO, it is possible 
			to get stuck in a deadlock in buffer pool code,

			Currently, the problem is solved this way - "write io" threads  
			always get all completion notifications, from both async reads and
			writes. Write completion is handled in the same thread that gets it.
			Read completion is forwarded via PostQueueCompletionStatus())
			to the second completion port dedicated solely to reads. One of the
			"read io" threads waiting on this port will finally handle the IO.

			Forwarding IO completion this way costs a context switch , and this 
			seems tolerable  since asynchronous reads are by far less frequent.
			*/
			ut_a(PostQueuedCompletionStatus(read_completion_port, len, key,
				&slot->control));
		}
		else {
			break;
		}
4548 4549 4550 4551 4552 4553
	}

	*message1 = slot->message1;
	*message2 = slot->message2;

	*type = slot->type;
4554
	*space_id = slot->space_id;
4555 4556 4557 4558

	if (ret && len == slot->len) {
		ret_val = TRUE;

Vadim Tkachenko's avatar
Vadim Tkachenko committed
4559
#ifdef UNIV_DO_FLUSH
4560 4561
		if (slot->type == OS_FILE_WRITE
		    && !os_do_not_call_flush_at_each_write) {
4562
			if (!os_file_flush(slot->file, TRUE)) {
Sergei Golubchik's avatar
Sergei Golubchik committed
4563 4564
				ut_error;
			}
4565
		}
Vadim Tkachenko's avatar
Vadim Tkachenko committed
4566
#endif /* UNIV_DO_FLUSH */
4567 4568 4569
	} else if (os_file_handle_error(slot->name, "Windows aio")) {

		retry = TRUE;
4570 4571 4572 4573 4574
	} else {

		ret_val = FALSE;
	}

4575 4576 4577 4578
	if (retry) {
		/* retry failed read/write operation synchronously.
		No need to hold array->mutex. */

Sergei Golubchik's avatar
Sergei Golubchik committed
4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590
#ifdef UNIV_PFS_IO
		/* This read/write does not go through os_file_read
		and os_file_write APIs, need to register with
		performance schema explicitly here. */
		struct PSI_file_locker* locker = NULL;
		register_pfs_file_io_begin(locker, slot->file, slot->len,
					   (slot->type == OS_FILE_WRITE)
						? PSI_FILE_WRITE
						: PSI_FILE_READ,
					    __FILE__, __LINE__);
#endif

4591 4592
		ut_a((slot->len & 0xFFFFFFFFUL) == slot->len);

4593 4594
		switch (slot->type) {
		case OS_FILE_WRITE:
4595 4596
			ret_val = os_file_write(slot->name, slot->file, slot->buf, 
				slot->control.Offset, slot->control.OffsetHigh, slot->len);
4597 4598
			break;
		case OS_FILE_READ:
4599 4600
			ret_val = os_file_read(slot->file, slot->buf, 
				 slot->control.Offset, slot->control.OffsetHigh, slot->len);
4601 4602 4603 4604 4605
			break;
		default:
			ut_error;
		}

Sergei Golubchik's avatar
Sergei Golubchik committed
4606 4607 4608 4609
#ifdef UNIV_PFS_IO
		register_pfs_file_io_end(locker, len);
#endif

4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624
		if (!ret && GetLastError() == ERROR_IO_PENDING) {
			/* aio was queued successfully!
			We want a synchronous i/o operation on a
			file where we also use async i/o: in Windows
			we must use the same wait mechanism as for
			async i/o */

			ret = GetOverlappedResult(slot->file,
						  &(slot->control),
						  &len, TRUE);
		}

		ret_val = ret && len == slot->len;
	}

4625
	os_aio_array_free_slot((os_aio_array_t *)slot->arr, slot);
4626 4627 4628 4629 4630

	return(ret_val);
}
#endif

Sergei Golubchik's avatar
Sergei Golubchik committed
4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858
#if defined(LINUX_NATIVE_AIO)
/******************************************************************//**
This function is only used in Linux native asynchronous i/o. This is
called from within the io-thread. If there are no completed IO requests
in the slot array, the thread calls this function to collect more
requests from the kernel.
The io-thread waits on io_getevents(), which is a blocking call, with
a timeout value. Unless the system is very heavy loaded, keeping the
io-thread very busy, the io-thread will spend most of its time waiting
in this function.
The io-thread also exits in this function. It checks server status at
each wakeup and that is why we use timed wait in io_getevents(). */
static
void
os_aio_linux_collect(
/*=================*/
	os_aio_array_t* array,		/*!< in/out: slot array. */
	ulint		segment,	/*!< in: local segment no. */
	ulint		seg_size)	/*!< in: segment size. */
{
	int			i;
	int			ret;
	ulint			start_pos;
	ulint			end_pos;
	struct timespec		timeout;
	struct io_event*	events;
	struct io_context*	io_ctx;

	/* sanity checks. */
	ut_ad(array != NULL);
	ut_ad(seg_size > 0);
	ut_ad(segment < array->n_segments);

	/* Which part of event array we are going to work on. */
	events = &array->aio_events[segment * seg_size];

	/* Which io_context we are going to use. */
	io_ctx = array->aio_ctx[segment];

	/* Starting point of the segment we will be working on. */
	start_pos = segment * seg_size;

	/* End point. */
	end_pos = start_pos + seg_size;

retry:

	/* Initialize the events. The timeout value is arbitrary.
	We probably need to experiment with it a little. */
	memset(events, 0, sizeof(*events) * seg_size);
	timeout.tv_sec = 0;
	timeout.tv_nsec = OS_AIO_REAP_TIMEOUT;

	ret = io_getevents(io_ctx, 1, seg_size, events, &timeout);

	if (ret > 0) {
		for (i = 0; i < ret; i++) {
			os_aio_slot_t*	slot;
			struct iocb*	control;

			control = (struct iocb *)events[i].obj;
			ut_a(control != NULL);

			slot = (os_aio_slot_t *) control->data;

			/* Some sanity checks. */
			ut_a(slot != NULL);
			ut_a(slot->reserved);

#if defined(UNIV_AIO_DEBUG)
			fprintf(stderr,
				"io_getevents[%c]: slot[%p] ctx[%p]"
				" seg[%lu]\n",
				(slot->type == OS_FILE_WRITE) ? 'w' : 'r',
				slot, io_ctx, segment);
#endif

			/* We are not scribbling previous segment. */
			ut_a(slot->pos >= start_pos);

			/* We have not overstepped to next segment. */
			ut_a(slot->pos < end_pos);

			/* Mark this request as completed. The error handling
			will be done in the calling function. */
			os_mutex_enter(array->mutex);
			slot->n_bytes = events[i].res;
			slot->ret = events[i].res2;
			slot->io_already_done = TRUE;
			os_mutex_exit(array->mutex);
		}
		return;
	}

	if (UNIV_UNLIKELY(srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS)) {
		return;
	}

	/* This error handling is for any error in collecting the
	IO requests. The errors, if any, for any particular IO
	request are simply passed on to the calling routine. */

	switch (ret) {
	case -EAGAIN:
		/* Not enough resources! Try again. */
	case -EINTR:
		/* Interrupted! I have tested the behaviour in case of an
		interrupt. If we have some completed IOs available then
		the return code will be the number of IOs. We get EINTR only
		if there are no completed IOs and we have been interrupted. */
	case 0:
		/* No pending request! Go back and check again. */
		goto retry;
	}

	/* All other errors should cause a trap for now. */
	ut_print_timestamp(stderr);
	fprintf(stderr,
		"  InnoDB: unexpected ret_code[%d] from io_getevents()!\n",
		ret);
	ut_error;
}

/**********************************************************************//**
This function is only used in Linux native asynchronous i/o.
Waits for an aio operation to complete. This function is used to wait for
the completed requests. The aio array of pending requests is divided
into segments. The thread specifies which segment or slot it wants to wait
for. NOTE: this function will also take care of freeing the aio slot,
therefore no other thread is allowed to do the freeing!
@return	TRUE if the IO was successful */
UNIV_INTERN
ibool
os_aio_linux_handle(
/*================*/
	ulint	global_seg,	/*!< in: segment number in the aio array
				to wait for; segment 0 is the ibuf
				i/o thread, segment 1 is log i/o thread,
				then follow the non-ibuf read threads,
				and the last are the non-ibuf write
				threads. */
	fil_node_t**message1,	/*!< out: the messages passed with the */
	void**	message2,	/*!< aio request; note that in case the
				aio operation failed, these output
				parameters are valid and can be used to
				restart the operation. */
	ulint*	type,		/*!< out: OS_FILE_WRITE or ..._READ */
	ulint*	space_id)
{
	ulint		segment;
	os_aio_array_t*	array;
	os_aio_slot_t*	slot;
	ulint		n;
	ulint		i;
	ibool		ret = FALSE;

	/* Should never be doing Sync IO here. */
	ut_a(global_seg != ULINT_UNDEFINED);

	/* Find the array and the local segment. */
	segment = os_aio_get_array_and_local_segment(&array, global_seg);
	n = array->n_slots / array->n_segments;

	/* Loop until we have found a completed request. */
	for (;;) {
		ibool	any_reserved = FALSE;
		os_mutex_enter(array->mutex);
		for (i = 0; i < n; ++i) {
			slot = os_aio_array_get_nth_slot(
				array, i + segment * n);
			if (!slot->reserved) {
				continue;
			} else if (slot->io_already_done) {
				/* Something for us to work on. */
				goto found;
			} else {
				any_reserved = TRUE;
			}
		}

		os_mutex_exit(array->mutex);

		/* There is no completed request.
		If there is no pending request at all,
		and the system is being shut down, exit. */
		if (UNIV_UNLIKELY
		    (!any_reserved
		     && srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS)) {
			*message1 = NULL;
			*message2 = NULL;
			return(TRUE);
		}

		/* Wait for some request. Note that we return
		from wait iff we have found a request. */

		srv_set_io_thread_op_info(global_seg,
			"waiting for completed aio requests");
		os_aio_linux_collect(array, segment, n);
	}

found:
	/* Note that it may be that there are more then one completed
	IO requests. We process them one at a time. We may have a case
	here to improve the performance slightly by dealing with all
	requests in one sweep. */
	srv_set_io_thread_op_info(global_seg,
				"processing completed aio requests");

	/* Ensure that we are scribbling only our segment. */
	ut_a(i < n);

	ut_ad(slot != NULL);
	ut_ad(slot->reserved);
	ut_ad(slot->io_already_done);

	*message1 = slot->message1;
	*message2 = slot->message2;

	*type = slot->type;
	*space_id = slot->space_id;

	if ((slot->ret == 0) && (slot->n_bytes == (long)slot->len)) {
		ret = TRUE;

#ifdef UNIV_DO_FLUSH
		if (slot->type == OS_FILE_WRITE
		    && !os_do_not_call_flush_at_each_write)
4859
		    && !os_file_flush(slot->file, TRUE) {
Sergei Golubchik's avatar
Sergei Golubchik committed
4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885
			ut_error;
		}
#endif /* UNIV_DO_FLUSH */
	} else {
		errno = -slot->ret;

		/* os_file_handle_error does tell us if we should retry
		this IO. As it stands now, we don't do this retry when
		reaping requests from a different context than
		the dispatcher. This non-retry logic is the same for
		windows and linux native AIO.
		We should probably look into this to transparently
		re-submit the IO. */
		os_file_handle_error(slot->name, "Linux aio");

		ret = FALSE;
	}

	os_mutex_exit(array->mutex);

	os_aio_array_free_slot(array, slot);

	return(ret);
}
#endif /* LINUX_NATIVE_AIO */

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4886
/**********************************************************************//**
4887
Does simulated aio. This function should be called by an i/o-handler
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4888 4889
thread.
@return	TRUE if the aio operation succeeded */
4890 4891 4892 4893
UNIV_INTERN
ibool
os_aio_simulated_handle(
/*====================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4894
	ulint	global_segment,	/*!< in: the number of the segment in the aio
4895 4896 4897 4898
				arrays to wait for; segment 0 is the ibuf
				i/o thread, segment 1 the log i/o thread,
				then follow the non-ibuf read threads, and as
				the last are the non-ibuf write threads */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
4899
	fil_node_t**message1,	/*!< out: the messages passed with the aio
4900 4901 4902 4903 4904
				request; note that also in the case where
				the aio operation failed, these output
				parameters are valid and can be used to
				restart the operation, for example */
	void**	message2,
4905 4906
	ulint*	type,		/*!< out: OS_FILE_WRITE or ..._READ */
	ulint*	space_id)
4907 4908
{
	os_aio_array_t*	array;
4909
	ulint		segment __attribute__ ((unused));
4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921
	os_aio_slot_t*	slot;
	os_aio_slot_t*	slot2;
	os_aio_slot_t*	consecutive_ios[OS_AIO_MERGE_N_CONSECUTIVE];
	ulint		n_consecutive;
	ulint		total_len;
	ulint		offs;
	ulint		lowest_offset;
	ulint		biggest_age;
	ulint		age;
	byte*		combined_buf;
	byte*		combined_buf2;
	ibool		ret;
Sergei Golubchik's avatar
Sergei Golubchik committed
4922
	ibool		any_reserved;
4923 4924 4925
	ulint		n;
	ulint		i;

4926 4927 4928
	/* Fix compiler warning */
	*consecutive_ios = NULL;

4929 4930 4931 4932 4933 4934 4935 4936
	segment = os_aio_get_array_and_local_segment(&array, global_segment);

restart:
	/* NOTE! We only access constant fields in os_aio_array. Therefore
	we do not have to acquire the protecting mutex yet */

	srv_set_io_thread_op_info(global_segment,
				  "looking for i/o requests (a)");
Sergei Golubchik's avatar
Sergei Golubchik committed
4937
	ut_ad(os_aio_validate_skip());
4938 4939
	ut_ad(segment < array->n_segments);

Sergei Golubchik's avatar
Sergei Golubchik committed
4940
	n = array->n_slots / array->n_segments;
4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957

	/* Look through n slots after the segment * n'th slot */

	if (array == os_aio_read_array
	    && os_aio_recommend_sleep_for_read_threads) {

		/* Give other threads chance to add several i/os to the array
		at once. */

		goto recommended_sleep;
	}

	srv_set_io_thread_op_info(global_segment,
				  "looking for i/o requests (b)");

	/* Check if there is a slot for which the i/o has already been
	done */
Sergei Golubchik's avatar
Sergei Golubchik committed
4958 4959 4960
	any_reserved = FALSE;

	os_mutex_enter(array->mutex);
4961 4962

	for (i = 0; i < n; i++) {
Sergei Golubchik's avatar
Sergei Golubchik committed
4963
		slot = os_aio_array_get_nth_slot(array, i + segment * n);
4964

Sergei Golubchik's avatar
Sergei Golubchik committed
4965 4966 4967
		if (!slot->reserved) {
			continue;
		} else if (slot->io_already_done) {
4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978

			if (os_aio_print_debug) {
				fprintf(stderr,
					"InnoDB: i/o for slot %lu"
					" already done, returning\n",
					(ulong) i);
			}

			ret = TRUE;

			goto slot_io_done;
Sergei Golubchik's avatar
Sergei Golubchik committed
4979 4980
		} else {
			any_reserved = TRUE;
4981 4982 4983
		}
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995
	/* There is no completed request.
	If there is no pending request at all,
	and the system is being shut down, exit. */
	if (UNIV_UNLIKELY
	    (!any_reserved
	     && srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS)) {
		os_mutex_exit(array->mutex);
		*message1 = NULL;
		*message2 = NULL;
		return(TRUE);
	}

4996 4997 4998 4999 5000 5001 5002
	n_consecutive = 0;

	/* If there are at least 2 seconds old requests, then pick the oldest
	one to prevent starvation. If several requests have the same age,
	then pick the one at the lowest offset. */

	biggest_age = 0;
Sergei Golubchik's avatar
Sergei Golubchik committed
5003
	lowest_offset = ULINT_MAX;
5004 5005

	for (i = 0; i < n; i++) {
Sergei Golubchik's avatar
Sergei Golubchik committed
5006
		slot = os_aio_array_get_nth_slot(array, i + segment * n);
5007

Sergei Golubchik's avatar
Sergei Golubchik committed
5008 5009 5010
		if (slot->reserved) {
			age = (ulint)difftime(time(NULL),
					      slot->reservation_time);
5011 5012 5013

			if ((age >= 2 && age > biggest_age)
			    || (age >= 2 && age == biggest_age
Sergei Golubchik's avatar
Sergei Golubchik committed
5014
				&& slot->offset < lowest_offset)) {
5015 5016

				/* Found an i/o request */
Sergei Golubchik's avatar
Sergei Golubchik committed
5017 5018 5019 5020
				consecutive_ios[0] = slot;

				n_consecutive = 1;

5021
				biggest_age = age;
Sergei Golubchik's avatar
Sergei Golubchik committed
5022
				lowest_offset = slot->offset;
5023
			}
Sergei Golubchik's avatar
Sergei Golubchik committed
5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038
		}
	}

	if (n_consecutive == 0) {
		/* There were no old requests. Look for an i/o request at the
		lowest offset in the array (we ignore the high 32 bits of the
		offset in these heuristics) */

		lowest_offset = ULINT_MAX;

		for (i = 0; i < n; i++) {
			slot = os_aio_array_get_nth_slot(array,
							 i + segment * n);

			if (slot->reserved && slot->offset < lowest_offset) {
5039 5040

				/* Found an i/o request */
Sergei Golubchik's avatar
Sergei Golubchik committed
5041 5042 5043 5044
				consecutive_ios[0] = slot;

				n_consecutive = 1;

5045 5046 5047 5048 5049
				lowest_offset = slot->offset;
			}
		}
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
5050
	if (n_consecutive == 0) {
5051 5052 5053 5054 5055 5056

		/* No i/o requested at the moment */

		goto wait_for_io;
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
5057 5058 5059 5060 5061 5062
	/* if n_consecutive != 0, then we have assigned
	something valid to consecutive_ios[0] */
	ut_ad(n_consecutive != 0);
	ut_ad(consecutive_ios[0] != NULL);

	slot = consecutive_ios[0];
5063 5064 5065 5066 5067

	/* Check if there are several consecutive blocks to read or write */

consecutive_loop:
	for (i = 0; i < n; i++) {
Sergei Golubchik's avatar
Sergei Golubchik committed
5068
		slot2 = os_aio_array_get_nth_slot(array, i + segment * n);
5069 5070 5071 5072 5073 5074 5075

		if (slot2->reserved && slot2 != slot
		    && slot2->offset == slot->offset + slot->len
		    /* check that sum does not wrap over */
		    && slot->offset + slot->len > slot->offset
		    && slot2->offset_high == slot->offset_high
		    && slot2->type == slot->type
Sergei Golubchik's avatar
Sergei Golubchik committed
5076
		    && slot2->file == slot->file) {
5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111

			/* Found a consecutive i/o request */

			consecutive_ios[n_consecutive] = slot2;
			n_consecutive++;

			slot = slot2;

			if (n_consecutive < OS_AIO_MERGE_N_CONSECUTIVE) {

				goto consecutive_loop;
			} else {
				break;
			}
		}
	}

	srv_set_io_thread_op_info(global_segment, "consecutive i/o requests");

	/* We have now collected n_consecutive i/o requests in the array;
	allocate a single buffer which can hold all data, and perform the
	i/o */

	total_len = 0;
	slot = consecutive_ios[0];

	for (i = 0; i < n_consecutive; i++) {
		total_len += consecutive_ios[i]->len;
	}

	if (n_consecutive == 1) {
		/* We can use the buffer of the i/o request */
		combined_buf = slot->buf;
		combined_buf2 = NULL;
	} else {
Sergei Golubchik's avatar
Sergei Golubchik committed
5112
		combined_buf2 = ut_malloc(total_len + UNIV_PAGE_SIZE);
5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177

		ut_a(combined_buf2);

		combined_buf = ut_align(combined_buf2, UNIV_PAGE_SIZE);
	}

	/* We release the array mutex for the time of the i/o: NOTE that
	this assumes that there is just one i/o-handler thread serving
	a single segment of slots! */

	os_mutex_exit(array->mutex);

	if (slot->type == OS_FILE_WRITE && n_consecutive > 1) {
		/* Copy the buffers to the combined buffer */
		offs = 0;

		for (i = 0; i < n_consecutive; i++) {

			ut_memcpy(combined_buf + offs, consecutive_ios[i]->buf,
				  consecutive_ios[i]->len);
			offs += consecutive_ios[i]->len;
		}
	}

	srv_set_io_thread_op_info(global_segment, "doing file i/o");

	if (os_aio_print_debug) {
		fprintf(stderr,
			"InnoDB: doing i/o of type %lu at offset %lu %lu,"
			" length %lu\n",
			(ulong) slot->type, (ulong) slot->offset_high,
			(ulong) slot->offset, (ulong) total_len);
	}

	/* Do the i/o with ordinary, synchronous i/o functions: */
	if (slot->type == OS_FILE_WRITE) {
		ret = os_file_write(slot->name, slot->file, combined_buf,
				    slot->offset, slot->offset_high,
				    total_len);
	} else {
		ret = os_file_read(slot->file, combined_buf,
				   slot->offset, slot->offset_high, total_len);
	}

	ut_a(ret);
	srv_set_io_thread_op_info(global_segment, "file i/o done");

#if 0
	fprintf(stderr,
		"aio: %lu consecutive %lu:th segment, first offs %lu blocks\n",
		n_consecutive, global_segment, slot->offset / UNIV_PAGE_SIZE);
#endif

	if (slot->type == OS_FILE_READ && n_consecutive > 1) {
		/* Copy the combined buffer to individual buffers */
		offs = 0;

		for (i = 0; i < n_consecutive; i++) {

			ut_memcpy(consecutive_ios[i]->buf, combined_buf + offs,
				  consecutive_ios[i]->len);
			offs += consecutive_ios[i]->len;
		}
	}

Sergei Golubchik's avatar
Sergei Golubchik committed
5178 5179
	if (combined_buf2) {
		ut_free(combined_buf2);
5180 5181
	}

5182 5183 5184 5185 5186
	os_mutex_enter(array->mutex);

	/* Mark the i/os done in slots */

	for (i = 0; i < n_consecutive; i++) {
Sergei Golubchik's avatar
Sergei Golubchik committed
5187
		consecutive_ios[i]->io_already_done = TRUE;
5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201
	}

	/* We return the messages for the first slot now, and if there were
	several slots, the messages will be returned with subsequent calls
	of this function */

slot_io_done:

	ut_a(slot->reserved);

	*message1 = slot->message1;
	*message2 = slot->message2;

	*type = slot->type;
5202
	*space_id = slot->space_id;
5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234

	os_mutex_exit(array->mutex);

	os_aio_array_free_slot(array, slot);

	return(ret);

wait_for_io:
	srv_set_io_thread_op_info(global_segment, "resetting wait event");

	/* We wait here until there again can be i/os in the segment
	of this thread */

	os_event_reset(os_aio_segment_wait_events[global_segment]);

	os_mutex_exit(array->mutex);

recommended_sleep:
	srv_set_io_thread_op_info(global_segment, "waiting for i/o request");

	os_event_wait(os_aio_segment_wait_events[global_segment]);

	if (os_aio_print_debug) {
		fprintf(stderr,
			"InnoDB: i/o handler thread for i/o"
			" segment %lu wakes up\n",
			(ulong) global_segment);
	}

	goto restart;
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
5235 5236 5237
/**********************************************************************//**
Validates the consistency of an aio array.
@return	TRUE if ok */
5238 5239 5240 5241
static
ibool
os_aio_array_validate(
/*==================*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
5242
	os_aio_array_t*	array)	/*!< in: aio wait array */
5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270
{
	os_aio_slot_t*	slot;
	ulint		n_reserved	= 0;
	ulint		i;

	ut_a(array);

	os_mutex_enter(array->mutex);

	ut_a(array->n_slots > 0);
	ut_a(array->n_segments > 0);

	for (i = 0; i < array->n_slots; i++) {
		slot = os_aio_array_get_nth_slot(array, i);

		if (slot->reserved) {
			n_reserved++;
			ut_a(slot->len > 0);
		}
	}

	ut_a(array->n_reserved == n_reserved);

	os_mutex_exit(array->mutex);

	return(TRUE);
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
5271 5272 5273
/**********************************************************************//**
Validates the consistency the aio system.
@return	TRUE if ok */
5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287
UNIV_INTERN
ibool
os_aio_validate(void)
/*=================*/
{
	os_aio_array_validate(os_aio_read_array);
	os_aio_array_validate(os_aio_write_array);
	os_aio_array_validate(os_aio_ibuf_array);
	os_aio_array_validate(os_aio_log_array);
	os_aio_array_validate(os_aio_sync_array);

	return(TRUE);
}

Sergei Golubchik's avatar
Sergei Golubchik committed
5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321
/**********************************************************************//**
Prints pending IO requests per segment of an aio array.
We probably don't need per segment statistics but they can help us
during development phase to see if the IO requests are being
distributed as expected. */
static
void
os_aio_print_segment_info(
/*======================*/
	FILE*		file,	/*!< in: file where to print */
	ulint*		n_seg,	/*!< in: pending IO array */
	os_aio_array_t*	array)	/*!< in: array to process */
{
	ulint	i;

	ut_ad(array);
	ut_ad(n_seg);
	ut_ad(array->n_segments > 0);

	if (array->n_segments == 1) {
		return;
	}

	fprintf(file, " [");
	for (i = 0; i < array->n_segments; i++) {
		if (i != 0) {
			fprintf(file, ", ");
		}

		fprintf(file, "%lu", n_seg[i]);
	}
	fprintf(file, "] ");
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
5322
/**********************************************************************//**
5323 5324 5325 5326 5327
Prints info of the aio arrays. */
UNIV_INTERN
void
os_aio_print(
/*=========*/
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
5328
	FILE*	file)	/*!< in: file where to print */
5329 5330 5331 5332
{
	os_aio_array_t*	array;
	os_aio_slot_t*	slot;
	ulint		n_reserved;
Sergei Golubchik's avatar
Sergei Golubchik committed
5333
	ulint		n_res_seg[SRV_MAX_N_IO_THREADS];
5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365
	time_t		current_time;
	double		time_elapsed;
	double		avg_bytes_read;
	ulint		i;

	for (i = 0; i < srv_n_file_io_threads; i++) {
		fprintf(file, "I/O thread %lu state: %s (%s)", (ulong) i,
			srv_io_thread_op_info[i],
			srv_io_thread_function[i]);

#ifndef __WIN__
		if (os_aio_segment_wait_events[i]->is_set) {
			fprintf(file, " ev set");
		}
#endif

		fprintf(file, "\n");
	}

	fputs("Pending normal aio reads:", file);

	array = os_aio_read_array;
loop:
	ut_a(array);

	os_mutex_enter(array->mutex);

	ut_a(array->n_slots > 0);
	ut_a(array->n_segments > 0);

	n_reserved = 0;

Sergei Golubchik's avatar
Sergei Golubchik committed
5366 5367
	memset(n_res_seg, 0x0, sizeof(n_res_seg));

5368
	for (i = 0; i < array->n_slots; i++) {
Sergei Golubchik's avatar
Sergei Golubchik committed
5369 5370
		ulint	seg_no;

5371 5372
		slot = os_aio_array_get_nth_slot(array, i);

Sergei Golubchik's avatar
Sergei Golubchik committed
5373
		seg_no = (i * array->n_segments) / array->n_slots;
5374 5375
		if (slot->reserved) {
			n_reserved++;
Sergei Golubchik's avatar
Sergei Golubchik committed
5376
			n_res_seg[seg_no]++;
5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389
#if 0
			fprintf(stderr, "Reserved slot, messages %p %p\n",
				(void*) slot->message1,
				(void*) slot->message2);
#endif
			ut_a(slot->len > 0);
		}
	}

	ut_a(array->n_reserved == n_reserved);

	fprintf(file, " %lu", (ulong) n_reserved);

Sergei Golubchik's avatar
Sergei Golubchik committed
5390 5391
	os_aio_print_segment_info(file, n_res_seg, array);

5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467
	os_mutex_exit(array->mutex);

	if (array == os_aio_read_array) {
		fputs(", aio writes:", file);

		array = os_aio_write_array;

		goto loop;
	}

	if (array == os_aio_write_array) {
		fputs(",\n ibuf aio reads:", file);
		array = os_aio_ibuf_array;

		goto loop;
	}

	if (array == os_aio_ibuf_array) {
		fputs(", log i/o's:", file);
		array = os_aio_log_array;

		goto loop;
	}

	if (array == os_aio_log_array) {
		fputs(", sync i/o's:", file);
		array = os_aio_sync_array;

		goto loop;
	}

	putc('\n', file);
	current_time = time(NULL);
	time_elapsed = 0.001 + difftime(current_time, os_last_printout);

	fprintf(file,
		"Pending flushes (fsync) log: %lu; buffer pool: %lu\n"
		"%lu OS file reads, %lu OS file writes, %lu OS fsyncs\n",
		(ulong) fil_n_pending_log_flushes,
		(ulong) fil_n_pending_tablespace_flushes,
		(ulong) os_n_file_reads, (ulong) os_n_file_writes,
		(ulong) os_n_fsyncs);

	if (os_file_n_pending_preads != 0 || os_file_n_pending_pwrites != 0) {
		fprintf(file,
			"%lu pending preads, %lu pending pwrites\n",
			(ulong) os_file_n_pending_preads,
			(ulong) os_file_n_pending_pwrites);
	}

	if (os_n_file_reads == os_n_file_reads_old) {
		avg_bytes_read = 0.0;
	} else {
		avg_bytes_read = (double) os_bytes_read_since_printout
			/ (os_n_file_reads - os_n_file_reads_old);
	}

	fprintf(file,
		"%.2f reads/s, %lu avg bytes/read,"
		" %.2f writes/s, %.2f fsyncs/s\n",
		(os_n_file_reads - os_n_file_reads_old)
		/ time_elapsed,
		(ulong)avg_bytes_read,
		(os_n_file_writes - os_n_file_writes_old)
		/ time_elapsed,
		(os_n_fsyncs - os_n_fsyncs_old)
		/ time_elapsed);

	os_n_file_reads_old = os_n_file_reads;
	os_n_file_writes_old = os_n_file_writes;
	os_n_fsyncs_old = os_n_fsyncs;
	os_bytes_read_since_printout = 0;

	os_last_printout = current_time;
}

Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
5468
/**********************************************************************//**
5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483
Refreshes the statistics used to print per-second averages. */
UNIV_INTERN
void
os_aio_refresh_stats(void)
/*======================*/
{
	os_n_file_reads_old = os_n_file_reads;
	os_n_file_writes_old = os_n_file_writes;
	os_n_fsyncs_old = os_n_fsyncs;
	os_bytes_read_since_printout = 0;

	os_last_printout = time(NULL);
}

#ifdef UNIV_DEBUG
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
5484
/**********************************************************************//**
5485
Checks that all slots in the system have been freed, that is, there are
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
5486 5487
no pending io operations.
@return	TRUE if all free */
5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543
UNIV_INTERN
ibool
os_aio_all_slots_free(void)
/*=======================*/
{
	os_aio_array_t*	array;
	ulint		n_res	= 0;

	array = os_aio_read_array;

	os_mutex_enter(array->mutex);

	n_res += array->n_reserved;

	os_mutex_exit(array->mutex);

	array = os_aio_write_array;

	os_mutex_enter(array->mutex);

	n_res += array->n_reserved;

	os_mutex_exit(array->mutex);

	array = os_aio_ibuf_array;

	os_mutex_enter(array->mutex);

	n_res += array->n_reserved;

	os_mutex_exit(array->mutex);

	array = os_aio_log_array;

	os_mutex_enter(array->mutex);

	n_res += array->n_reserved;

	os_mutex_exit(array->mutex);

	array = os_aio_sync_array;

	os_mutex_enter(array->mutex);

	n_res += array->n_reserved;

	os_mutex_exit(array->mutex);

	if (n_res == 0) {

		return(TRUE);
	}

	return(FALSE);
}
#endif /* UNIV_DEBUG */
Aleksandr Kuzminsky's avatar
Aleksandr Kuzminsky committed
5544 5545

#endif /* !UNIV_HOTBACKUP */