| 1 |
/*********************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1995, 2026, Oracle and/or its affiliates. |
| 4 |
Copyright (c) 2009, Percona Inc. |
| 5 |
|
| 6 |
Portions of this file contain modifications contributed and copyrighted |
| 7 |
by Percona Inc.. Those modifications are |
| 8 |
gratefully acknowledged and are described briefly in the InnoDB |
| 9 |
documentation. The contributions by Percona Inc. are incorporated with |
| 10 |
their permission, and subject to the conditions contained in the file |
| 11 |
COPYING.Percona. |
| 12 |
|
| 13 |
This program is free software; you can redistribute it and/or modify |
| 14 |
it under the terms of the GNU General Public License, version 2.0, |
| 15 |
as published by the Free Software Foundation. |
| 16 |
|
| 17 |
This program is designed to work with certain software (including |
| 18 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 19 |
as designated in a particular file or component or in included license |
| 20 |
documentation. The authors of MySQL hereby grant you an additional |
| 21 |
permission to link the program and your derivative works with the |
| 22 |
separately licensed software that they have either included with |
| 23 |
the program or referenced in the documentation. |
| 24 |
|
| 25 |
This program is distributed in the hope that it will be useful, |
| 26 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 27 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 28 |
GNU General Public License, version 2.0, for more details. |
| 29 |
|
| 30 |
You should have received a copy of the GNU General Public License |
| 31 |
along with this program; if not, write to the Free Software |
| 32 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 33 |
|
| 34 |
***********************************************************************/ |
| 35 |
|
| 36 |
/** @file os/os0file.cc |
| 37 |
The interface to the operating system file i/o primitives |
| 38 |
|
| 39 |
Created 10/21/1995 Heikki Tuuri |
| 40 |
*******************************************************/ |
| 41 |
|
| 42 |
#include "os0file.h" |
| 43 |
#include "fil0fil.h" |
| 44 |
#include "ha_prototypes.h" |
| 45 |
#include "log0write.h" |
| 46 |
#include "my_dbug.h" |
| 47 |
#include "my_io.h" |
| 48 |
|
| 49 |
#include "fil0fil.h" |
| 50 |
#include "ha_prototypes.h" |
| 51 |
#include "os0file.h" |
| 52 |
#include "sql_const.h" |
| 53 |
#include "srv0srv.h" |
| 54 |
#include "srv0start.h" |
| 55 |
#ifndef UNIV_HOTBACKUP |
| 56 |
#include "os0event.h" |
| 57 |
#include "os0thread.h" |
| 58 |
#endif /* !UNIV_HOTBACKUP */ |
| 59 |
|
| 60 |
#ifdef _WIN32 |
| 61 |
|
| 62 |
#include <errno.h> |
| 63 |
#include <mbstring.h> |
| 64 |
#include <sys/stat.h> |
| 65 |
#include <tchar.h> |
| 66 |
#include <codecvt> |
| 67 |
|
| 68 |
#endif /* _WIN32 */ |
| 69 |
|
| 70 |
#ifdef __linux__ |
| 71 |
#include <sys/sendfile.h> |
| 72 |
#endif /* __linux__ */ |
| 73 |
|
| 74 |
#ifdef LINUX_NATIVE_AIO |
| 75 |
#ifndef UNIV_HOTBACKUP |
| 76 |
#include <libaio.h> |
| 77 |
#else /* UNIV_HOTBACKUP */ |
| 78 |
#undef LINUX_NATIVE_AIO |
| 79 |
#endif /* UNIV_HOTBACKUP */ |
| 80 |
#endif /* LINUX_NATIVE_AIO */ |
| 81 |
|
| 82 |
#ifdef HAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE |
| 83 |
#include <fcntl.h> |
| 84 |
#include <linux/falloc.h> |
| 85 |
#endif /* HAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE */ |
| 86 |
|
| 87 |
#include <errno.h> |
| 88 |
#include <lz4.h> |
| 89 |
#include "my_aes.h" |
| 90 |
#include "my_rnd.h" |
| 91 |
#include "mysql/service_mysql_keyring.h" |
| 92 |
#include "mysqld.h" |
| 93 |
|
| 94 |
#include <sys/types.h> |
| 95 |
#include <zlib.h> |
| 96 |
#include <ctime> |
| 97 |
#include <functional> |
| 98 |
#include <new> |
| 99 |
#include <ostream> |
| 100 |
#include <vector> |
| 101 |
|
| 102 |
#ifdef UNIV_HOTBACKUP |
| 103 |
#include <data0type.h> |
| 104 |
#endif /* UNIV_HOTBACKUP */ |
| 105 |
|
| 106 |
#ifndef _WIN32 |
| 107 |
static_assert(OS_FILE_CLOSED == -1, |
| 108 |
"Our implementation for OSes other than Windows assumes an " |
| 109 |
"invalid handle is indicated by -1"); |
| 110 |
#endif |
| 111 |
|
| 112 |
/* Flush after each os_fsync_threshold bytes */ |
| 113 |
unsigned long long os_fsync_threshold = 0; |
| 114 |
|
| 115 |
/** Insert buffer segment id */ |
| 116 |
static const ulint IO_IBUF_SEGMENT = 0; |
| 117 |
|
| 118 |
/** Number of retries for partial I/O's */ |
| 119 |
static const ulint NUM_RETRIES_ON_PARTIAL_IO = 10; |
| 120 |
|
| 121 |
/** For storing the allocated blocks */ |
| 122 |
using Blocks = std::vector<file::Block>; |
| 123 |
|
| 124 |
/** Block collection */ |
| 125 |
static Blocks *block_cache; |
| 126 |
|
| 127 |
/** Number of blocks to allocate for sync read/writes */ |
| 128 |
static const size_t MAX_BLOCKS = 128; |
| 129 |
|
| 130 |
/** Block buffer size */ |
| 131 |
#define BUFFER_BLOCK_SIZE ((ulint)(UNIV_PAGE_SIZE * 1.3)) |
| 132 |
|
| 133 |
/** Disk sector size of aligning write buffer for DIRECT_IO */ |
| 134 |
static ulint os_io_ptr_align = UNIV_SECTOR_SIZE; |
| 135 |
|
| 136 |
/** Determine if O_DIRECT is supported |
| 137 |
@retval true if O_DIRECT is supported. |
| 138 |
@retval false if O_DIRECT is not supported. */ |
| 139 |
bool os_is_o_direct_supported() { |
| 140 |
#if !defined(NO_FALLOCATE) && defined(UNIV_LINUX) |
| 141 |
char *path = srv_data_home; |
| 142 |
char *file_name; |
| 143 |
os_file_t file_handle; |
| 144 |
ulint dir_len; |
| 145 |
ulint path_len; |
| 146 |
bool add_os_path_separator = false; |
| 147 |
|
| 148 |
/* If the srv_data_home is empty, set the path to current dir. */ |
| 149 |
char current_dir[3]; |
| 150 |
if (*path == 0) { |
| 151 |
current_dir[0] = FN_CURLIB; |
| 152 |
current_dir[1] = FN_LIBCHAR; |
| 153 |
current_dir[2] = 0; |
| 154 |
path = current_dir; |
| 155 |
} |
| 156 |
|
| 157 |
/* Get the path length. */ |
| 158 |
if (path[strlen(path) - 1] == OS_PATH_SEPARATOR) { |
| 159 |
/* path is ended with OS_PATH_SEPARATOR */ |
| 160 |
dir_len = strlen(path); |
| 161 |
} else { |
| 162 |
/* path is not ended with OS_PATH_SEPARATOR */ |
| 163 |
dir_len = strlen(path) + 1; |
| 164 |
add_os_path_separator = true; |
| 165 |
} |
| 166 |
|
| 167 |
/* Allocate a new path and move the directory path to it. */ |
| 168 |
path_len = dir_len + sizeof "o_direct_test"; |
| 169 |
file_name = static_cast<char *>( |
| 170 |
ut::zalloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, path_len)); |
| 171 |
if (add_os_path_separator == true) { |
| 172 |
memcpy(file_name, path, dir_len - 1); |
| 173 |
file_name[dir_len - 1] = OS_PATH_SEPARATOR; |
| 174 |
} else { |
| 175 |
memcpy(file_name, path, dir_len); |
| 176 |
} |
| 177 |
|
| 178 |
/* Construct a temp file name. */ |
| 179 |
strcat(file_name + dir_len, "o_direct_test"); |
| 180 |
|
| 181 |
/* Try to create a temp file with O_DIRECT flag. */ |
| 182 |
file_handle = |
| 183 |
::open(file_name, O_CREAT | O_TRUNC | O_WRONLY | O_DIRECT, S_IRWXU); |
| 184 |
|
| 185 |
/* If Failed */ |
| 186 |
if (file_handle == -1) { |
| 187 |
ut::free(file_name); |
| 188 |
return (false); |
| 189 |
} |
| 190 |
|
| 191 |
::close(file_handle); |
| 192 |
unlink(file_name); |
| 193 |
ut::free(file_name); |
| 194 |
|
| 195 |
return (true); |
| 196 |
#else /* !UNIV_LINUX */ |
| 197 |
return (false); |
| 198 |
#endif /* !UNIV_LINUX */ |
| 199 |
} |
| 200 |
|
| 201 |
#ifndef _WIN32 |
| 202 |
/** This specifies the file permissions InnoDB uses when it creates files in |
| 203 |
Unix; the value of os_innodb_umask is initialized in ha_innodb.cc to my_umask. |
| 204 |
It is a global value and can't be modified once it is set. */ |
| 205 |
static mode_t os_innodb_umask = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; |
| 206 |
#else /* _WIN32 */ |
| 207 |
/** On Windows when using native AIO the number of AIO requests |
| 208 |
that a thread can handle at a given time is limited to 32 |
| 209 |
i.e.: SRV_N_PENDING_IOS_PER_THREAD */ |
| 210 |
constexpr uint32_t SRV_N_PENDING_IOS_PER_THREAD = |
| 211 |
OS_AIO_N_PENDING_IOS_PER_THREAD; |
| 212 |
#endif /* _WIN32 */ |
| 213 |
|
| 214 |
/** In simulated aio, merge at most this many consecutive i/os */ |
| 215 |
static const ulint OS_AIO_MERGE_N_CONSECUTIVE = 64; |
| 216 |
|
| 217 |
/** Checks if the page_cleaner is in active state. */ |
| 218 |
bool buf_flush_page_cleaner_is_active(); |
| 219 |
|
| 220 |
#ifndef UNIV_HOTBACKUP |
| 221 |
/********************************************************************** |
| 222 |
|
| 223 |
InnoDB AIO Implementation: |
| 224 |
========================= |
| 225 |
|
| 226 |
We support native AIO for Windows and Linux. For rest of the platforms |
| 227 |
we simulate AIO by special IO-threads servicing the IO-requests. |
| 228 |
Therefore we have three specialized implementations. |
| 229 |
|
| 230 |
What is common to all three of them, is that they use AIO array objects to keep |
| 231 |
track of ongoing requests: |
| 232 |
- s_ibuf for reads of IBUF pages |
| 233 |
- s_reads for reads of other pages |
| 234 |
- s_writes for writes. |
| 235 |
Another common trait, is that there are threads running io_handler_thread() tied |
| 236 |
to these arrays: |
| 237 |
- 1 thread for s_ibuf array, |
| 238 |
- innodb_read_io_threads for s_reads array, |
| 239 |
- innodb_write_io_threads for s_writes array. |
| 240 |
The s_ibuf helps avoid a deadlock when completing a read of regular page |
| 241 |
requires performing IBUF merge, which in turn needs read request for IBUF page, |
| 242 |
which might be blocked if we run out of free slots. |
| 243 |
As IBUF needs to be empty when starting InnoDB in read-only mode, this mechanism |
| 244 |
is not needed in read-only mode, so s_ibuf and associated thread are not |
| 245 |
initialized then. |
| 246 |
|
| 247 |
The differences between these three implementations - mostly in what role the |
| 248 |
io_handler_thread() play in conducting the IO operations - are described below. |
| 249 |
|
| 250 |
Simulated AIO: |
| 251 |
============== |
| 252 |
|
| 253 |
On platforms where we 'simulate' AIO, the following is a rough explanation |
| 254 |
of the high level design. |
| 255 |
All synchronous IO requests are serviced by the calling thread using |
| 256 |
os_file_write/os_file_read. The Asynchronous requests are queued up |
| 257 |
in an array by the calling thread. |
| 258 |
Later these requests are picked up by the IO-thread and are serviced |
| 259 |
synchronously. |
| 260 |
|
| 261 |
|
| 262 |
Windows native AIO: |
| 263 |
================== |
| 264 |
|
| 265 |
If srv_use_native_aio is not set then Windows follow the same |
| 266 |
code as Simulated AIO. If the flag is set then native AIO interface |
| 267 |
is used. On Windows, one limitation is that if a file is opened |
| 268 |
for AIO no synchronous IO can be done on it. The os_file_write/os_file_read take |
| 269 |
this into account. |
| 270 |
If an asynchronous request is made the calling thread not only queues it in the |
| 271 |
array but also submits the requests to OS using OVERLAPPED ReadFile/WriteFile(). |
| 272 |
The io_handler_thread() thread then collects the completed IO request from the |
| 273 |
segment of SRV_N_PENDING_IOS_PER_THREAD slots of the array it is responsible for |
| 274 |
with WaitForMultipleObjects() and calls completion routine on it. |
| 275 |
|
| 276 |
|
| 277 |
Linux native AIO: |
| 278 |
================= |
| 279 |
|
| 280 |
If we have libaio installed on the system and innodb_use_native_aio |
| 281 |
is set to true we follow the code path of native AIO, otherwise we |
| 282 |
do Simulated AIO. |
| 283 |
If a synchronous IO request is made, it is handled by calling |
| 284 |
os_file_write/os_file_read. |
| 285 |
If an asynchronous request is made the calling thread not only queues it in the |
| 286 |
array but also submits the requests to OS using io_submit(). |
| 287 |
The io_handler_thread() thread then collects the completed IO request from the |
| 288 |
segment of 8 * OS_AIO_N_PENDING_IOS_PER_THREAD slots of the array it is |
| 289 |
responsible for with io_getevents() and calls completion routine on it. |
| 290 |
|
| 291 |
**********************************************************************/ |
| 292 |
|
| 293 |
#ifdef UNIV_PFS_IO |
| 294 |
/* Keys to register InnoDB I/O with performance schema */ |
| 295 |
mysql_pfs_key_t innodb_log_file_key; |
| 296 |
mysql_pfs_key_t innodb_data_file_key; |
| 297 |
mysql_pfs_key_t innodb_temp_file_key; |
| 298 |
mysql_pfs_key_t innodb_dblwr_file_key; |
| 299 |
mysql_pfs_key_t innodb_arch_file_key; |
| 300 |
mysql_pfs_key_t innodb_clone_file_key; |
| 301 |
#endif /* UNIV_PFS_IO */ |
| 302 |
|
| 303 |
/** The asynchronous I/O context */ |
| 304 |
struct Slot { |
| 305 |
/** Default constructor/assignment etc. are OK */ |
| 306 |
|
| 307 |
/** index of the slot in the aio array */ |
| 308 |
uint16_t pos{0}; |
| 309 |
|
| 310 |
/** true if this slot is reserved */ |
| 311 |
bool is_reserved{false}; |
| 312 |
|
| 313 |
/** time when reserved */ |
| 314 |
std::chrono::steady_clock::time_point reservation_time; |
| 315 |
|
| 316 |
/** buffer used in i/o */ |
| 317 |
byte *buf{nullptr}; |
| 318 |
|
| 319 |
/** Buffer pointer used for actual IO. We advance this |
| 320 |
when partial IO is required and not buf */ |
| 321 |
byte *ptr{nullptr}; |
| 322 |
|
| 323 |
/** OS_FILE_READ or OS_FILE_WRITE */ |
| 324 |
IORequest type{IORequest::UNSET}; |
| 325 |
|
| 326 |
/** file offset in bytes */ |
| 327 |
os_offset_t offset{0}; |
| 328 |
|
| 329 |
/** file where to read or write */ |
| 330 |
pfs_os_file_t file{ |
| 331 |
#ifdef UNIV_PFS_IO |
| 332 |
nullptr, // m_psi |
| 333 |
#endif /* UNIV_PFS_IO */ |
| 334 |
0 // m_file |
| 335 |
}; |
| 336 |
|
| 337 |
/** file name or path */ |
| 338 |
const char *name{nullptr}; |
| 339 |
|
| 340 |
/** used only in simulated aio: true if the physical i/o |
| 341 |
already made and only the slot message needs to be passed |
| 342 |
to the caller of os_aio_simulated_handler */ |
| 343 |
bool io_already_done{false}; |
| 344 |
|
| 345 |
/** The file node for which the IO is requested. */ |
| 346 |
fil_node_t *m1{nullptr}; |
| 347 |
|
| 348 |
/** the requester of an aio operation and which can be used |
| 349 |
to identify which pending aio operation was completed */ |
| 350 |
void *m2{nullptr}; |
| 351 |
|
| 352 |
/** AIO completion status */ |
| 353 |
dberr_t err{DB_ERROR_UNSET}; |
| 354 |
|
| 355 |
#ifdef _WIN32 |
| 356 |
/** handle object to Event that we need in the OVERLAPPED struct for kernel to |
| 357 |
signal async operation completion. */ |
| 358 |
HANDLE handle{INVALID_HANDLE_VALUE}; |
| 359 |
|
| 360 |
/** Windows control block for the aio request */ |
| 361 |
OVERLAPPED control{0, 0, {{0, 0}}, nullptr}; |
| 362 |
|
| 363 |
/** bytes written/read */ |
| 364 |
DWORD n_bytes{0}; |
| 365 |
|
| 366 |
/** length of the block to read or write */ |
| 367 |
DWORD len{0}; |
| 368 |
|
| 369 |
#elif defined(LINUX_NATIVE_AIO) |
| 370 |
/** Linux control block for aio */ |
| 371 |
struct iocb control; |
| 372 |
|
| 373 |
/** AIO return code */ |
| 374 |
int ret{0}; |
| 375 |
|
| 376 |
/** bytes written/read. */ |
| 377 |
ssize_t n_bytes{0}; |
| 378 |
|
| 379 |
/** length of the block to read or write */ |
| 380 |
ulint len{0}; |
| 381 |
#else /* !_WIN32 && !LINUX_NATIVE_AIO */ |
| 382 |
/** length of the block to read or write */ |
| 383 |
ulint len{0}; |
| 384 |
|
| 385 |
/** bytes written/read. */ |
| 386 |
ulint n_bytes{0}; |
| 387 |
#endif /* !_WIN32 && !LINUX_NATIVE_AIO */ |
| 388 |
|
| 389 |
/** Buffer block for compressed pages or encrypted pages */ |
| 390 |
file::Block *buf_block{nullptr}; |
| 391 |
|
| 392 |
/** true, if we shouldn't punch a hole after writing the page */ |
| 393 |
bool skip_punch_hole{false}; |
| 394 |
|
| 395 |
Slot() { |
| 396 |
#if defined(LINUX_NATIVE_AIO) |
| 397 |
memset(&control, 0, sizeof(control)); |
| 398 |
#endif /* LINUX_NATIVE_AIO */ |
| 399 |
} |
| 400 |
|
| 401 |
/** Serialize the object into JSON format. |
| 402 |
@return the object in JSON format. */ |
| 403 |
[[nodiscard]] std::string to_json() const noexcept; |
| 404 |
|
| 405 |
/** Print this object into the given output stream. |
| 406 |
@return the output stream into which object was printed. */ |
| 407 |
std::ostream &print(std::ostream &out) const noexcept; |
| 408 |
}; |
| 409 |
|
| 410 |
std::string Slot::to_json() const noexcept { |
| 411 |
std::ostringstream out; |
| 412 |
out << "{"; |
| 413 |
out << "\"className\": \"Slot\","; |
| 414 |
out << "\"objectPtr\": \"" << (void *)this << "\","; |
| 415 |
out << "\"buf_block\": \"" << (void *)buf_block << "\""; |
| 416 |
out << "}"; |
| 417 |
return out.str(); |
| 418 |
} |
| 419 |
|
| 420 |
std::ostream &Slot::print(std::ostream &out) const noexcept { |
| 421 |
out << to_json(); |
| 422 |
return (out); |
| 423 |
} |
| 424 |
|
| 425 |
inline std::ostream &operator<<(std::ostream &out, const Slot &obj) noexcept { |
| 426 |
return (obj.print(out)); |
| 427 |
} |
| 428 |
|
| 429 |
/** The asynchronous i/o array structure */ |
| 430 |
class AIO { |
| 431 |
public: |
| 432 |
/** Constructor |
| 433 |
@param[in] id The latch ID |
| 434 |
@param[in] n Number of AIO slots |
| 435 |
@param[in] segments Number of segments */ |
| 436 |
AIO(latch_id_t id, ulint n, ulint segments); |
| 437 |
|
| 438 |
/** Destructor */ |
| 439 |
~AIO(); |
| 440 |
|
| 441 |
/** Initialize the instance |
| 442 |
@return DB_SUCCESS or error code */ |
| 443 |
dberr_t init(); |
| 444 |
|
| 445 |
/** Requests for a slot in the aio array. If no slot is available, waits |
| 446 |
until not_full-event becomes signaled. |
| 447 |
|
| 448 |
@param[in,out] type IO context |
| 449 |
@param[in,out] m1 message to be passed along with AIO operation |
| 450 |
@param[in,out] m2 message to be passed along with AIO operation |
| 451 |
@param[in] file file handle |
| 452 |
@param[in] name name of the file or path as a null-terminated string |
| 453 |
@param[in,out] buf buffer where to read or from which to write |
| 454 |
@param[in] offset file offset, where to read from or start writing |
| 455 |
@param[in] len length of the block to read or write |
| 456 |
@param[in] e_block Encrypted block or nullptr. |
| 457 |
@return pointer to slot */ |
| 458 |
[[nodiscard]] Slot *reserve_slot(IORequest &type, fil_node_t *m1, void *m2, |
| 459 |
pfs_os_file_t file, const char *name, |
| 460 |
void *buf, os_offset_t offset, ulint len, |
| 461 |
const file::Block *e_block); |
| 462 |
|
| 463 |
/** @return number of reserved slots */ |
| 464 |
ulint pending_io_count() const; |
| 465 |
|
| 466 |
/** Returns a pointer to the nth slot in the aio array. |
| 467 |
@param[in] i Index of the slot in the array |
| 468 |
@return pointer to slot */ |
| 469 |
[[nodiscard]] const Slot *at(ulint i) const { |
| 470 |
ut_a(i < m_slots.size()); |
| 471 |
|
| 472 |
return (&m_slots[i]); |
| 473 |
} |
| 474 |
|
| 475 |
/** Non const version */ |
| 476 |
[[nodiscard]] Slot *at(ulint i) { |
| 477 |
if (i >= m_slots.size()) { |
| 478 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_1357) |
| 479 |
<< "i: " << i << " slots: " << m_slots.size(); |
| 480 |
} |
| 481 |
|
| 482 |
return (&m_slots[i]); |
| 483 |
} |
| 484 |
|
| 485 |
/** Frees a slot in the aio array. Assumes caller owns the mutex. |
| 486 |
@param[in,out] slot Slot to release */ |
| 487 |
void release(Slot *slot); |
| 488 |
|
| 489 |
/** Frees a slot in the AIO array. Assumes caller doesn't own the mutex. |
| 490 |
@param[in,out] slot Slot to release */ |
| 491 |
void release_with_mutex(Slot *slot); |
| 492 |
|
| 493 |
/** Prints info about the aio array. |
| 494 |
@param[in,out] file Where to print */ |
| 495 |
void print(FILE *file); |
| 496 |
|
| 497 |
/** @return the number of slots per segment */ |
| 498 |
[[nodiscard]] ulint slots_per_segment() const { |
| 499 |
return (m_slots.size() / m_n_segments); |
| 500 |
} |
| 501 |
|
| 502 |
/** @return accessor for n_segments */ |
| 503 |
[[nodiscard]] ulint get_n_segments() const { return (m_n_segments); } |
| 504 |
|
| 505 |
#ifdef UNIV_DEBUG |
| 506 |
/** @return true if the thread owns the mutex */ |
| 507 |
[[nodiscard]] bool is_mutex_owned() const { return (mutex_own(&m_mutex)); } |
| 508 |
#endif /* UNIV_DEBUG */ |
| 509 |
|
| 510 |
/** Acquire the mutex */ |
| 511 |
void acquire() const { mutex_enter(&m_mutex); } |
| 512 |
|
| 513 |
/** Release the mutex */ |
| 514 |
void release() const { mutex_exit(&m_mutex); } |
| 515 |
|
| 516 |
/** Prints all pending IO for the array |
| 517 |
@param[in,out] file file where to print */ |
| 518 |
void to_file(FILE *file) const; |
| 519 |
|
| 520 |
#ifdef LINUX_NATIVE_AIO |
| 521 |
/** Dispatch an AIO request to the kernel. |
| 522 |
@param[in,out] slot an already reserved slot |
| 523 |
@return true on success. */ |
| 524 |
[[nodiscard]] bool linux_dispatch(Slot *slot); |
| 525 |
|
| 526 |
/** Accessor for an AIO event |
| 527 |
@param[in] index Index into the array |
| 528 |
@return the event at the index */ |
| 529 |
[[nodiscard]] io_event *io_events(ulint index) { |
| 530 |
ut_a(index < m_events.size()); |
| 531 |
|
| 532 |
return (&m_events[index]); |
| 533 |
} |
| 534 |
|
| 535 |
/** Accessor for the AIO context |
| 536 |
@param[in] segment Segment for which to get the context |
| 537 |
@return the AIO context for the segment */ |
| 538 |
[[nodiscard]] io_context *io_ctx(ulint segment) { |
| 539 |
ut_ad(segment < get_n_segments()); |
| 540 |
|
| 541 |
return (m_aio_ctx[segment]); |
| 542 |
} |
| 543 |
|
| 544 |
/** Creates an io_context for native linux AIO. |
| 545 |
@param[in] max_events number of events |
| 546 |
@param[out] io_ctx io_ctx to initialize. |
| 547 |
@return true on success. */ |
| 548 |
[[nodiscard]] static bool linux_create_io_ctx(ulint max_events, |
| 549 |
io_context_t *io_ctx); |
| 550 |
|
| 551 |
/** Checks if the system supports native linux aio. On some kernel |
| 552 |
versions where native aio is supported it won't work on tmpfs. In such |
| 553 |
cases we can't use native aio as it is not possible to mix simulated |
| 554 |
and native aio. |
| 555 |
@return true if supported, false otherwise. */ |
| 556 |
[[nodiscard]] static bool is_linux_native_aio_supported(); |
| 557 |
#endif /* LINUX_NATIVE_AIO */ |
| 558 |
|
| 559 |
#ifdef _WIN32 |
| 560 |
/** Wakes up all async i/o threads in the array in Windows async I/O at |
| 561 |
shutdown. */ |
| 562 |
void signal() { |
| 563 |
for (ulint i = 0; i < m_slots.size(); ++i) { |
| 564 |
SetEvent(m_slots[i].handle); |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
/** Wake up all AIO threads in Windows native aio */ |
| 569 |
static void wake_at_shutdown() { |
| 570 |
s_reads->signal(); |
| 571 |
|
| 572 |
if (s_writes != NULL) { |
| 573 |
s_writes->signal(); |
| 574 |
} |
| 575 |
|
| 576 |
if (s_ibuf != NULL) { |
| 577 |
s_ibuf->signal(); |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
/** This function can be called if one wants to post a batch of reads |
| 582 |
and prefers an I/O - handler thread to handle them all at once later.You |
| 583 |
must call os_aio_simulated_wake_handler_threads later to ensure the |
| 584 |
threads are not left sleeping! */ |
| 585 |
static void simulated_put_read_threads_to_sleep(); |
| 586 |
|
| 587 |
/** |
| 588 |
Get the pointer to array of AIO handles of Events for a given segment. |
| 589 |
@param[in] segment The local segment. |
| 590 |
@return the handles of Events for the segment. */ |
| 591 |
[[nodiscard]] HANDLE *handles(ulint segment) { |
| 592 |
ut_ad(segment < m_handles->size() / slots_per_segment()); |
| 593 |
|
| 594 |
return (&(*m_handles)[segment * slots_per_segment()]); |
| 595 |
} |
| 596 |
|
| 597 |
/** @return true if no slots are reserved */ |
| 598 |
[[nodiscard]] bool is_empty() const { |
| 599 |
ut_ad(is_mutex_owned()); |
| 600 |
return (m_n_reserved == 0); |
| 601 |
} |
| 602 |
#endif /* _WIN32 */ |
| 603 |
|
| 604 |
/** Creates an aio wait array. Note that we return NULL in case of failure. |
| 605 |
We don't care about freeing memory here because we assume that a |
| 606 |
failure will result in server refusing to start up. |
| 607 |
@param[in] id Latch ID |
| 608 |
@param[in] n maximum number of pending AIO operations |
| 609 |
allowed; n must be divisible by m_n_segments |
| 610 |
@param[in] n_segments number of segments in the AIO array |
| 611 |
@return own: AIO array, NULL on failure */ |
| 612 |
[[nodiscard]] static AIO *create(latch_id_t id, ulint n, ulint n_segments); |
| 613 |
|
| 614 |
/** Initializes the asynchronous io system. Creates one array for ibuf I/O. |
| 615 |
Also creates one array each for read and write |
| 616 |
where each array is divided logically into n_readers and n_writers |
| 617 |
respectively. The caller must create an i/o handler thread for each |
| 618 |
segment in these arrays by calling start_threads(). |
| 619 |
@param[in] n_per_seg maximum number of pending aio |
| 620 |
operations allowed per segment |
| 621 |
@param[in] n_readers number of reader threads |
| 622 |
@param[in] n_writers number of writer threads |
| 623 |
@return true if AIO sub-system was started successfully */ |
| 624 |
[[nodiscard]] static bool start(ulint n_per_seg, ulint n_readers, |
| 625 |
ulint n_writers); |
| 626 |
|
| 627 |
/** Starts a thread for each segment */ |
| 628 |
static void start_threads(); |
| 629 |
|
| 630 |
/** Free the AIO arrays */ |
| 631 |
static void shutdown(); |
| 632 |
|
| 633 |
/** Print all the AIO segments |
| 634 |
@param[in,out] file Where to print */ |
| 635 |
static void print_all(FILE *file); |
| 636 |
|
| 637 |
/** Calculates local segment number and aio array from global |
| 638 |
segment number. |
| 639 |
@param[out] array AIO wait array |
| 640 |
@param[in] segment global segment number |
| 641 |
@return local segment number within the aio array */ |
| 642 |
[[nodiscard]] static ulint get_array_and_local_segment(AIO *&array, |
| 643 |
ulint segment); |
| 644 |
|
| 645 |
/** Select the IO slot array |
| 646 |
@param[in,out] type Type of IO, READ or WRITE |
| 647 |
@param[in] read_only true if running in read-only mode |
| 648 |
@param[in] aio_mode IO mode |
| 649 |
@return slot array or NULL if invalid mode specified */ |
| 650 |
[[nodiscard]] static AIO *select_slot_array(IORequest &type, bool read_only, |
| 651 |
AIO_mode aio_mode); |
| 652 |
|
| 653 |
/** Calculates segment number for a slot. |
| 654 |
@param[in] array AIO wait array |
| 655 |
@param[in] slot slot in this array |
| 656 |
@return segment number (which is the number used by, for example, |
| 657 |
I/O handler threads) */ |
| 658 |
[[nodiscard]] static ulint get_segment_no_from_slot(const AIO *array, |
| 659 |
const Slot *slot); |
| 660 |
|
| 661 |
/** Wakes up a simulated AIO I/O handler thread if it has something to do. |
| 662 |
@param[in] global_segment The number of the segment in the AIO arrays */ |
| 663 |
static void wake_simulated_handler_thread(ulint global_segment); |
| 664 |
|
| 665 |
/** Check if it is a read request |
| 666 |
@param[in] aio The AIO instance to check |
| 667 |
@return true if the AIO instance is for reading. */ |
| 668 |
[[nodiscard]] static bool is_read(const AIO *aio) { return (s_reads == aio); } |
| 669 |
|
| 670 |
/** Wait on an event until no pending writes */ |
| 671 |
static void wait_until_no_pending_writes() { |
| 672 |
os_event_wait(AIO::s_writes->m_is_empty); |
| 673 |
} |
| 674 |
|
| 675 |
/** Print to file |
| 676 |
@param[in] file File to write to */ |
| 677 |
static void print_to_file(FILE *file); |
| 678 |
|
| 679 |
/** Check for pending IO. Gets the count and also validates the |
| 680 |
data structures. |
| 681 |
@return count of pending IO requests */ |
| 682 |
static ulint total_pending_io_count(); |
| 683 |
|
| 684 |
private: |
| 685 |
/** Returns the number of arrays other than n_readers and n_writers that |
| 686 |
start() will create. In srv_read_only_mode this is 0. Otherwise this is |
| 687 |
just one for ibuf i/o.*/ |
| 688 |
static size_t number_of_extra_threads(); |
| 689 |
|
| 690 |
/** Initialise the slots |
| 691 |
@return DB_SUCCESS or error code */ |
| 692 |
[[nodiscard]] dberr_t init_slots(); |
| 693 |
|
| 694 |
/** Wakes up a simulated AIO I/O-handler thread if it has something to do |
| 695 |
for a local segment in the AIO array. |
| 696 |
@param[in] global_segment The number of the segment in the AIO arrays |
| 697 |
@param[in] segment The local segment in the AIO array */ |
| 698 |
void wake_simulated_handler_thread(ulint global_segment, ulint segment); |
| 699 |
|
| 700 |
/** Prints pending IO requests per segment of an aio array. |
| 701 |
We probably don't need per segment statistics but they can help us |
| 702 |
during development phase to see if the IO requests are being |
| 703 |
distributed as expected. |
| 704 |
@param[in,out] file File where to print |
| 705 |
@param[in] segments Pending IO array */ |
| 706 |
void print_segment_info(FILE *file, const ulint *segments); |
| 707 |
|
| 708 |
#ifdef LINUX_NATIVE_AIO |
| 709 |
/** Initialise the Linux native AIO data structures |
| 710 |
@return DB_SUCCESS or error code */ |
| 711 |
[[nodiscard]] dberr_t init_linux_native_aio(); |
| 712 |
#endif /* LINUX_NATIVE_AIO */ |
| 713 |
|
| 714 |
private: |
| 715 |
typedef std::vector<Slot> Slots; |
| 716 |
|
| 717 |
/** the mutex protecting the aio array */ |
| 718 |
mutable SysMutex m_mutex; |
| 719 |
|
| 720 |
/** Pointer to the slots in the array. |
| 721 |
Number of elements must be divisible by n_threads. */ |
| 722 |
Slots m_slots; |
| 723 |
|
| 724 |
/** Number of segments in the aio array of pending aio requests. |
| 725 |
A thread can wait separately for any one of the segments. */ |
| 726 |
ulint m_n_segments; |
| 727 |
|
| 728 |
/** The event which is set to the signaled state when |
| 729 |
there is space in the aio outside the ibuf segment */ |
| 730 |
os_event_t m_not_full; |
| 731 |
|
| 732 |
/** The event which is set to the signaled state when |
| 733 |
there are no pending i/os in this array */ |
| 734 |
os_event_t m_is_empty; |
| 735 |
|
| 736 |
/** Number of reserved slots in the AIO array outside |
| 737 |
the ibuf segment */ |
| 738 |
ulint m_n_reserved; |
| 739 |
|
| 740 |
/** The index of last slot used to reserve. This is used to balance the |
| 741 |
incoming requests more evenly throughout the segments. |
| 742 |
This field is not guarded by any lock. |
| 743 |
This is only used as a heuristic and any value read or written to it is OK. |
| 744 |
It is atomic as it is accesses without any latches from multiple threads. */ |
| 745 |
std::atomic_size_t m_last_slot_used; |
| 746 |
|
| 747 |
#ifdef _WIN32 |
| 748 |
typedef std::vector<HANDLE, ut::allocator<HANDLE>> Handles; |
| 749 |
|
| 750 |
/** Pointer to an array of OS native event handles where |
| 751 |
we copied the handles from slots, in the same order. This |
| 752 |
can be used in WaitForMultipleObjects; used only in Windows */ |
| 753 |
Handles *m_handles; |
| 754 |
#endif /* _WIN32 */ |
| 755 |
|
| 756 |
#if defined(LINUX_NATIVE_AIO) |
| 757 |
typedef std::vector<io_event> IOEvents; |
| 758 |
|
| 759 |
/** completion queue for IO. There is one such queue per |
| 760 |
segment. Each thread will work on one ctx exclusively. */ |
| 761 |
io_context_t *m_aio_ctx; |
| 762 |
|
| 763 |
/** The array to collect completed IOs. There is one such |
| 764 |
event for each possible pending IO. The size of the array |
| 765 |
is equal to m_slots.size(). */ |
| 766 |
IOEvents m_events; |
| 767 |
#endif /* LINUX_NATIVE_AIO */ |
| 768 |
|
| 769 |
/** The aio arrays for non-ibuf i/o and ibuf i/o. These are NULL when the |
| 770 |
module has not yet been initialized. */ |
| 771 |
|
| 772 |
/** Insert buffer */ |
| 773 |
static AIO *s_ibuf; |
| 774 |
|
| 775 |
/** Reads */ |
| 776 |
static AIO *s_reads; |
| 777 |
|
| 778 |
/** Writes */ |
| 779 |
static AIO *s_writes; |
| 780 |
}; |
| 781 |
|
| 782 |
/** Static declarations */ |
| 783 |
AIO *AIO::s_reads; |
| 784 |
AIO *AIO::s_writes; |
| 785 |
AIO *AIO::s_ibuf; |
| 786 |
|
| 787 |
#if defined(LINUX_NATIVE_AIO) |
| 788 |
/** timeout for each io_getevents() call = 500ms. */ |
| 789 |
static constexpr uint64_t OS_AIO_REAP_TIMEOUT = 500000000UL; |
| 790 |
|
| 791 |
/** time to sleep if io_setup() returns EAGAIN. */ |
| 792 |
static constexpr std::chrono::milliseconds OS_AIO_IO_SETUP_RETRY_SLEEP{500}; |
| 793 |
|
| 794 |
/** number of attempts before giving up on io_setup(). */ |
| 795 |
static const int OS_AIO_IO_SETUP_RETRY_ATTEMPTS = 5; |
| 796 |
#endif /* LINUX_NATIVE_AIO */ |
| 797 |
|
| 798 |
/** Array of events used in simulated AIO */ |
| 799 |
static os_event_t *os_aio_segment_wait_events = nullptr; |
| 800 |
|
| 801 |
/** Number of asynchronous I/O segments. Set by os_aio_init(). */ |
| 802 |
static ulint os_aio_n_segments = ULINT_UNDEFINED; |
| 803 |
|
| 804 |
/** If the following is true, read i/o handler threads try to |
| 805 |
wait until a batch of new read requests have been posted */ |
| 806 |
static bool os_aio_recommend_sleep_for_read_threads = false; |
| 807 |
|
| 808 |
#endif /* !UNIV_HOTBACKUP */ |
| 809 |
|
| 810 |
ulint os_n_file_reads = 0; |
| 811 |
static ulint os_bytes_read_since_printout = 0; |
| 812 |
ulint os_n_file_writes = 0; |
| 813 |
ulint os_n_fsyncs = 0; |
| 814 |
static ulint os_n_file_reads_old = 0; |
| 815 |
static ulint os_n_file_writes_old = 0; |
| 816 |
static ulint os_n_fsyncs_old = 0; |
| 817 |
|
| 818 |
/** Number of pending write operations */ |
| 819 |
std::atomic<ulint> os_n_pending_writes{0}; |
| 820 |
/** Number of pending read operations */ |
| 821 |
std::atomic<ulint> os_n_pending_reads{0}; |
| 822 |
|
| 823 |
static std::chrono::steady_clock::time_point os_last_printout; |
| 824 |
bool os_has_said_disk_full = false; |
| 825 |
|
| 826 |
#ifndef UNIV_HOTBACKUP |
| 827 |
|
| 828 |
/** Default Zip compression level */ |
| 829 |
extern uint page_zip_level; |
| 830 |
|
| 831 |
static_assert(DATA_TRX_ID_LEN <= 6, "COMPRESSION_ALGORITHM will not fit!"); |
| 832 |
|
| 833 |
/** Validates the consistency of the aio system. |
| 834 |
@return true if ok */ |
| 835 |
static bool os_aio_validate(); |
| 836 |
#endif /* !UNIV_HOTBACKUP */ |
| 837 |
|
| 838 |
/** Does error handling when a file operation fails. |
| 839 |
@param[in] name File name or NULL |
| 840 |
@param[in] operation Name of operation e.g., "read", "write" |
| 841 |
@return true if we should retry the operation */ |
| 842 |
static bool os_file_handle_error(const char *name, const char *operation); |
| 843 |
|
| 844 |
/** Free storage space associated with a section of the file. |
| 845 |
@param[in] fh Open file handle |
| 846 |
@param[in] off Starting offset (SEEK_SET) |
| 847 |
@param[in] len Size of the hole |
| 848 |
@return DB_SUCCESS or error code */ |
| 849 |
dberr_t os_file_punch_hole(os_file_t fh, os_offset_t off, os_offset_t len); |
| 850 |
|
| 851 |
/** Does error handling when a file operation fails. |
| 852 |
@param[in] name File name or NULL |
| 853 |
@param[in] operation Name of operation e.g., "read", "write" |
| 854 |
@param[in] on_error_silent if true then don't print any message to the log. |
| 855 |
@return true if we should retry the operation */ |
| 856 |
static bool os_file_handle_error_no_exit(const char *name, |
| 857 |
const char *operation, |
| 858 |
bool on_error_silent); |
| 859 |
|
| 860 |
/** Decompress after a read and punch a hole in the file if it was a write |
| 861 |
@param[in] type IO context |
| 862 |
@param[in] fh Open file handle |
| 863 |
@param[in,out] buf Buffer to transform |
| 864 |
@param[in] src_len Length of the buffer before compression |
| 865 |
@param[in] offset file offset from the start where to read |
| 866 |
@param[in] len Compressed buffer length for write and size |
| 867 |
of buf len for read |
| 868 |
@return DB_SUCCESS or error code */ |
| 869 |
static dberr_t os_file_io_complete(const IORequest &type, os_file_t fh, |
| 870 |
byte *buf, ulint src_len, os_offset_t offset, |
| 871 |
ulint len); |
| 872 |
|
| 873 |
#ifndef UNIV_HOTBACKUP |
| 874 |
/** Does simulated AIO. This function should be called by an i/o-handler |
| 875 |
thread. |
| 876 |
|
| 877 |
@param[in] global_segment The number of the segment in the aio arrays to |
| 878 |
await for; segment 0 is the ibuf i/o thread, |
| 879 |
then follow the non-ibuf read threads, |
| 880 |
and as the last are the non-ibuf write threads |
| 881 |
@param[out] m1 the messages passed with the AIO request; note |
| 882 |
that also in the case where the AIO operation |
| 883 |
failed, these output parameters are valid and |
| 884 |
can be used to restart the operation, for |
| 885 |
example |
| 886 |
@param[out] m2 Callback argument |
| 887 |
@param[in] type IO context |
| 888 |
@return DB_SUCCESS or error code */ |
| 889 |
static dberr_t os_aio_simulated_handler(ulint global_segment, fil_node_t **m1, |
| 890 |
void **m2, IORequest *type); |
| 891 |
#ifdef _WIN32 |
| 892 |
/** This function is only used in Windows asynchronous i/o. |
| 893 |
Waits for an aio operation to complete. This function is used to wait |
| 894 |
for completed requests. The aio array of pending requests is divided |
| 895 |
into segments. The thread specifies which segment or slot it wants to wait |
| 896 |
for. NOTE: this function will also take care of freeing the aio slot, |
| 897 |
therefore no other thread is allowed to do the freeing! |
| 898 |
@param[in] segment The number of the segment in the aio arrays to |
| 899 |
wait for; segment 0 is the ibuf I/O thread, |
| 900 |
then follow the non-ibuf read threads, |
| 901 |
and as the last are the non-ibuf write threads |
| 902 |
@param[out] m1 the messages passed with the AIO request; note |
| 903 |
that also in the case where the AIO operation |
| 904 |
failed, these output parameters are valid and |
| 905 |
can be used to restart the operation, |
| 906 |
for example |
| 907 |
@param[out] m2 callback message |
| 908 |
@param[out] type OS_FILE_WRITE or ..._READ |
| 909 |
@return DB_SUCCESS or error code */ |
| 910 |
static dberr_t os_aio_windows_handler(ulint segment, fil_node_t **m1, void **m2, |
| 911 |
IORequest *type); |
| 912 |
#endif /* _WIN32 */ |
| 913 |
|
| 914 |
#endif /* !UNIV_HOTBACKUP */ |
| 915 |
|
| 916 |
/** Check the file type and determine if it can be deleted. |
| 917 |
@param[in] name Filename/Path to check |
| 918 |
@return true if it's a file or a symlink and can be deleted */ |
| 919 |
static bool os_file_can_delete(const char *name) { |
| 920 |
switch (Fil_path::get_file_type(name)) { |
| 921 |
case OS_FILE_TYPE_FILE: |
| 922 |
case OS_FILE_TYPE_LINK: |
| 923 |
return (true); |
| 924 |
|
| 925 |
case OS_FILE_TYPE_DIR: |
| 926 |
|
| 927 |
ib::warn(ER_IB_MSG_743) << "'" << name << "'" |
| 928 |
<< " is a directory, can't delete!"; |
| 929 |
break; |
| 930 |
|
| 931 |
case OS_FILE_TYPE_BLOCK: |
| 932 |
|
| 933 |
ib::warn(ER_IB_MSG_744) << "'" << name << "'" |
| 934 |
<< " is a block device, can't delete!"; |
| 935 |
break; |
| 936 |
|
| 937 |
case OS_FILE_TYPE_FAILED: |
| 938 |
|
| 939 |
ib::warn(ER_IB_MSG_745) << "'" << name << "'" |
| 940 |
<< " get file type failed, won't delete!"; |
| 941 |
break; |
| 942 |
|
| 943 |
case OS_FILE_TYPE_UNKNOWN: |
| 944 |
|
| 945 |
ib::warn(ER_IB_MSG_746) << "'" << name << "'" |
| 946 |
<< " unknown file type, won't delete!"; |
| 947 |
break; |
| 948 |
|
| 949 |
case OS_FILE_TYPE_NAME_TOO_LONG: |
| 950 |
|
| 951 |
ib::warn(ER_IB_MSG_747) << "'" << name << "'" |
| 952 |
<< " name too long, can't delete!"; |
| 953 |
break; |
| 954 |
|
| 955 |
case OS_FILE_PERMISSION_ERROR: |
| 956 |
ib::warn(ER_IB_MSG_748) << "'" << name << "'" |
| 957 |
<< " permission error, can't delete!"; |
| 958 |
break; |
| 959 |
|
| 960 |
case OS_FILE_TYPE_MISSING: |
| 961 |
break; |
| 962 |
} |
| 963 |
|
| 964 |
return (false); |
| 965 |
} |
| 966 |
|
| 967 |
byte *os_block_get_frame(const file::Block *block) noexcept { |
| 968 |
return (static_cast<byte *>(ut_align(block->m_ptr, os_io_ptr_align))); |
| 969 |
} |
| 970 |
|
| 971 |
file::Block *os_alloc_block() noexcept { |
| 972 |
size_t pos; |
| 973 |
Blocks &blocks = *block_cache; |
| 974 |
size_t i = static_cast<size_t>(my_timer_cycles()); |
| 975 |
const size_t size = blocks.size(); |
| 976 |
ulint retry = 0; |
| 977 |
file::Block *block; |
| 978 |
|
| 979 |
DBUG_EXECUTE_IF("os_block_cache_busy", retry = MAX_BLOCKS * 3;); |
| 980 |
|
| 981 |
for (;;) { |
| 982 |
/* After go through the block cache for 3 times, |
| 983 |
allocate a new temporary block. */ |
| 984 |
if (retry == MAX_BLOCKS * 3) { |
| 985 |
byte *ptr; |
| 986 |
|
| 987 |
ptr = static_cast<byte *>(ut::malloc_withkey( |
| 988 |
UT_NEW_THIS_FILE_PSI_KEY, sizeof(*block) + BUFFER_BLOCK_SIZE)); |
| 989 |
|
| 990 |
block = new (ptr) file::Block(); |
| 991 |
block->m_ptr = static_cast<byte *>(ptr + sizeof(*block)); |
| 992 |
block->m_in_use = true; |
| 993 |
|
| 994 |
break; |
| 995 |
} |
| 996 |
|
| 997 |
pos = i++ % size; |
| 998 |
|
| 999 |
if (blocks[pos].m_in_use.exchange(true) == false) { |
| 1000 |
block = &blocks[pos]; |
| 1001 |
break; |
| 1002 |
} |
| 1003 |
|
| 1004 |
std::this_thread::yield(); |
| 1005 |
|
| 1006 |
++retry; |
| 1007 |
} |
| 1008 |
|
| 1009 |
ut_a(block->m_in_use); |
| 1010 |
|
| 1011 |
return (block); |
| 1012 |
} |
| 1013 |
|
| 1014 |
void os_free_block(file::Block *block) noexcept { |
| 1015 |
ut_ad(block->m_in_use); |
| 1016 |
|
| 1017 |
block->m_in_use.store(false); |
| 1018 |
|
| 1019 |
/* When this block is not in the block cache, and it's |
| 1020 |
a temporary block, we need to free it directly. */ |
| 1021 |
if (std::less<file::Block *>()(block, &block_cache->front()) || |
| 1022 |
std::greater<file::Block *>()(block, &block_cache->back())) { |
| 1023 |
ut::free(block); |
| 1024 |
} |
| 1025 |
} |
| 1026 |
#ifndef UNIV_HOTBACKUP |
| 1027 |
|
| 1028 |
/** Generic AIO Handler methods. Currently handles IO post processing. */ |
| 1029 |
class AIOHandler { |
| 1030 |
public: |
| 1031 |
/** Do any post processing after a read/write |
| 1032 |
@return DB_SUCCESS or error code. */ |
| 1033 |
static dberr_t post_io_processing(Slot *slot); |
| 1034 |
|
| 1035 |
/** Decompress after a read and punch a hole in the file if |
| 1036 |
it was a write */ |
| 1037 |
static dberr_t io_complete(const Slot *slot) { |
| 1038 |
ut_a(slot->offset > 0); |
| 1039 |
ut_a(slot->type.is_read() || !slot->skip_punch_hole); |
| 1040 |
return (os_file_io_complete(slot->type, slot->file.m_file, slot->buf, |
| 1041 |
slot->type.get_original_size(), slot->offset, |
| 1042 |
slot->len)); |
| 1043 |
} |
| 1044 |
|
| 1045 |
private: |
| 1046 |
/** Check whether the page was encrypted. |
| 1047 |
@param[in] slot The slot that contains the IO request |
| 1048 |
@return true if it was an encrypted page */ |
| 1049 |
static bool is_encrypted_page(const Slot *slot) { |
| 1050 |
return (Encryption::is_encrypted_page(slot->buf)); |
| 1051 |
} |
| 1052 |
|
| 1053 |
/** Check whether the page was compressed. |
| 1054 |
@param[in] slot The slot that contains the IO request |
| 1055 |
@return true if it was a compressed page */ |
| 1056 |
static bool is_compressed_page(const Slot *slot) { |
| 1057 |
const byte *src = slot->buf; |
| 1058 |
|
| 1059 |
ulint page_type = mach_read_from_2(src + FIL_PAGE_TYPE); |
| 1060 |
|
| 1061 |
return (page_type == FIL_PAGE_COMPRESSED); |
| 1062 |
} |
| 1063 |
|
| 1064 |
/** Get the compressed page size. |
| 1065 |
@param[in] slot The slot that contains the IO request |
| 1066 |
@return number of bytes to read for a successful decompress */ |
| 1067 |
static ulint compressed_page_size(const Slot *slot) { |
| 1068 |
ut_ad(slot->type.is_read()); |
| 1069 |
ut_ad(is_compressed_page(slot)); |
| 1070 |
|
| 1071 |
ulint size; |
| 1072 |
const byte *src = slot->buf; |
| 1073 |
|
| 1074 |
size = mach_read_from_2(src + FIL_PAGE_COMPRESS_SIZE_V1); |
| 1075 |
|
| 1076 |
return (size + FIL_PAGE_DATA); |
| 1077 |
} |
| 1078 |
|
| 1079 |
/** Check if the page contents can be decompressed. |
| 1080 |
@param[in] slot The slot that contains the IO request |
| 1081 |
@return true if the data read has all the compressed data */ |
| 1082 |
static bool can_decompress(const Slot *slot) { |
| 1083 |
ut_ad(slot->type.is_read()); |
| 1084 |
ut_ad(is_compressed_page(slot)); |
| 1085 |
|
| 1086 |
ulint version; |
| 1087 |
const byte *src = slot->buf; |
| 1088 |
|
| 1089 |
version = mach_read_from_1(src + FIL_PAGE_VERSION); |
| 1090 |
ut_a(Compression::is_valid_page_version(version)); |
| 1091 |
|
| 1092 |
/* Includes the page header size too */ |
| 1093 |
ulint size = compressed_page_size(slot); |
| 1094 |
|
| 1095 |
return (size <= (slot->ptr - slot->buf) + (ulint)slot->n_bytes); |
| 1096 |
} |
| 1097 |
|
| 1098 |
/** Check if we need to read some more data. |
| 1099 |
@param[in] slot The slot that contains the IO request |
| 1100 |
@param[in] n_bytes Total bytes read so far |
| 1101 |
@return DB_SUCCESS or error code */ |
| 1102 |
static dberr_t check_read(Slot *slot, ulint n_bytes); |
| 1103 |
}; |
| 1104 |
#endif /* !UNIV_HOTBACKUP */ |
| 1105 |
|
| 1106 |
/** Helper class for doing synchronous file IO. Currently, the objective |
| 1107 |
is to hide the OS specific code, so that the higher level functions aren't |
| 1108 |
peppered with "#ifdef". Makes the code flow difficult to follow. */ |
| 1109 |
class SyncFileIO { |
| 1110 |
public: |
| 1111 |
/** Constructor |
| 1112 |
@param[in] fh File handle |
| 1113 |
@param[in,out] buf Buffer to read/write |
| 1114 |
@param[in] n Number of bytes to read/write |
| 1115 |
@param[in] offset Offset where to read or write */ |
| 1116 |
SyncFileIO(os_file_t fh, void *buf, ulint n, os_offset_t offset) |
| 1117 |
: m_fh(fh), m_buf(buf), m_n(static_cast<ssize_t>(n)), m_offset(offset) { |
| 1118 |
ut_ad(m_n > 0); |
| 1119 |
} |
| 1120 |
|
| 1121 |
/** Destructor */ |
| 1122 |
~SyncFileIO() = default; |
| 1123 |
|
| 1124 |
/** Do the read/write |
| 1125 |
@param[in] request The IO context and type |
| 1126 |
@return the number of bytes read/written or negative value on error */ |
| 1127 |
ssize_t execute(const IORequest &request); |
| 1128 |
|
| 1129 |
/** Move the read/write offset up to where the partial IO succeeded. |
| 1130 |
@param[in] n_bytes The number of bytes to advance */ |
| 1131 |
void advance(ssize_t n_bytes) { |
| 1132 |
m_offset += n_bytes; |
| 1133 |
|
| 1134 |
ut_ad(m_n >= n_bytes); |
| 1135 |
|
| 1136 |
m_n -= n_bytes; |
| 1137 |
|
| 1138 |
m_buf = reinterpret_cast<uchar *>(m_buf) + n_bytes; |
| 1139 |
} |
| 1140 |
|
| 1141 |
private: |
| 1142 |
/** Open file handle */ |
| 1143 |
os_file_t m_fh; |
| 1144 |
|
| 1145 |
/** Buffer to read/write */ |
| 1146 |
void *m_buf; |
| 1147 |
|
| 1148 |
/** Number of bytes to read/write */ |
| 1149 |
ssize_t m_n; |
| 1150 |
|
| 1151 |
/** Offset from where to read/write */ |
| 1152 |
os_offset_t m_offset; |
| 1153 |
}; |
| 1154 |
|
| 1155 |
/** If it is a compressed page return the compressed page data + footer size |
| 1156 |
@param[in] buf Buffer to check, must include header + 10 bytes |
| 1157 |
@return ULINT_UNDEFINED if the page is not a compressed page or length |
| 1158 |
of the compressed data (including footer) if it is a compressed page */ |
| 1159 |
ulint os_file_compressed_page_size(const byte *buf) { |
| 1160 |
ulint type = mach_read_from_2(buf + FIL_PAGE_TYPE); |
| 1161 |
|
| 1162 |
if (type == FIL_PAGE_COMPRESSED) { |
| 1163 |
ulint version = mach_read_from_1(buf + FIL_PAGE_VERSION); |
| 1164 |
ut_a(Compression::is_valid_page_version(version)); |
| 1165 |
return (mach_read_from_2(buf + FIL_PAGE_COMPRESS_SIZE_V1)); |
| 1166 |
} |
| 1167 |
|
| 1168 |
return (ULINT_UNDEFINED); |
| 1169 |
} |
| 1170 |
|
| 1171 |
/** If it is a compressed page return the original page data + footer size |
| 1172 |
@param[in] buf Buffer to check, must include header + 10 bytes |
| 1173 |
@return ULINT_UNDEFINED if the page is not a compressed page or length |
| 1174 |
of the original data + footer if it is a compressed page */ |
| 1175 |
ulint os_file_original_page_size(const byte *buf) { |
| 1176 |
ulint type = mach_read_from_2(buf + FIL_PAGE_TYPE); |
| 1177 |
|
| 1178 |
if (type == FIL_PAGE_COMPRESSED) { |
| 1179 |
ulint version = mach_read_from_1(buf + FIL_PAGE_VERSION); |
| 1180 |
ut_a(Compression::is_valid_page_version(version)); |
| 1181 |
|
| 1182 |
return (mach_read_from_2(buf + FIL_PAGE_ORIGINAL_SIZE_V1)); |
| 1183 |
} |
| 1184 |
|
| 1185 |
return (ULINT_UNDEFINED); |
| 1186 |
} |
| 1187 |
#ifndef UNIV_HOTBACKUP |
| 1188 |
/** Check if we need to read some more data. |
| 1189 |
@param[in] slot The slot that contains the IO request |
| 1190 |
@param[in] n_bytes Total bytes read so far |
| 1191 |
@return DB_SUCCESS or error code */ |
| 1192 |
dberr_t AIOHandler::check_read(Slot *slot, ulint n_bytes) { |
| 1193 |
dberr_t err; |
| 1194 |
ut_a(!slot->type.is_log()); |
| 1195 |
ut_ad(slot->type.is_read()); |
| 1196 |
ut_ad(slot->type.get_original_size() > slot->len); |
| 1197 |
|
| 1198 |
if (is_compressed_page(slot)) { |
| 1199 |
if (can_decompress(slot)) { |
| 1200 |
ut_a(slot->offset > 0); |
| 1201 |
|
| 1202 |
slot->len = slot->type.get_original_size(); |
| 1203 |
#ifdef _WIN32 |
| 1204 |
slot->n_bytes = static_cast<DWORD>(n_bytes); |
| 1205 |
#else /* !_WIN32 */ |
| 1206 |
slot->n_bytes = static_cast<ulint>(n_bytes); |
| 1207 |
#endif /* !_WIN32 */ |
| 1208 |
|
| 1209 |
err = io_complete(slot); |
| 1210 |
ut_a(err == DB_SUCCESS); |
| 1211 |
} else { |
| 1212 |
/* Read the next block in */ |
| 1213 |
ut_ad(compressed_page_size(slot) >= n_bytes); |
| 1214 |
|
| 1215 |
err = DB_FAIL; |
| 1216 |
} |
| 1217 |
} else if (is_encrypted_page(slot)) { |
| 1218 |
ut_a(slot->offset > 0); |
| 1219 |
|
| 1220 |
slot->len = slot->type.get_original_size(); |
| 1221 |
#ifdef _WIN32 |
| 1222 |
slot->n_bytes = static_cast<DWORD>(n_bytes); |
| 1223 |
#else /* !_WIN32 */ |
| 1224 |
slot->n_bytes = static_cast<ulint>(n_bytes); |
| 1225 |
#endif /* !_WIN32 */ |
| 1226 |
|
| 1227 |
err = io_complete(slot); |
| 1228 |
ut_a(err == DB_SUCCESS); |
| 1229 |
|
| 1230 |
} else { |
| 1231 |
err = DB_FAIL; |
| 1232 |
} |
| 1233 |
|
| 1234 |
if (slot->buf_block != nullptr) { |
| 1235 |
os_free_block(slot->buf_block); |
| 1236 |
slot->buf_block = nullptr; |
| 1237 |
} |
| 1238 |
|
| 1239 |
return (err); |
| 1240 |
} |
| 1241 |
|
| 1242 |
/** Do any post processing after a read/write |
| 1243 |
@return DB_SUCCESS or error code. */ |
| 1244 |
dberr_t AIOHandler::post_io_processing(Slot *slot) { |
| 1245 |
dberr_t err; |
| 1246 |
ut_a(!slot->type.is_log()); |
| 1247 |
ut_ad(slot->is_reserved); |
| 1248 |
|
| 1249 |
/* Total bytes read so far */ |
| 1250 |
ulint n_bytes = (slot->ptr - slot->buf) + slot->n_bytes; |
| 1251 |
|
| 1252 |
/* Compressed writes can be smaller than the original length. |
| 1253 |
Therefore they can be processed without further IO. */ |
| 1254 |
if (n_bytes == slot->type.get_original_size() || |
| 1255 |
(slot->type.is_write() && slot->type.is_compressed() && |
| 1256 |
slot->len == static_cast<ulint>(slot->n_bytes))) { |
| 1257 |
if (is_compressed_page(slot) || is_encrypted_page(slot)) { |
| 1258 |
ut_a(slot->offset > 0); |
| 1259 |
|
| 1260 |
if (slot->type.is_read()) { |
| 1261 |
slot->len = slot->type.get_original_size(); |
| 1262 |
} |
| 1263 |
|
| 1264 |
/* The punch hole has been done on collect() */ |
| 1265 |
|
| 1266 |
if (slot->type.is_read()) { |
| 1267 |
err = io_complete(slot); |
| 1268 |
} else { |
| 1269 |
err = DB_SUCCESS; |
| 1270 |
} |
| 1271 |
|
| 1272 |
ut_ad(err == DB_SUCCESS || err == DB_UNSUPPORTED || |
| 1273 |
err == DB_CORRUPTION || err == DB_IO_DECOMPRESS_FAIL || |
| 1274 |
err == DB_IO_DECRYPT_FAIL); |
| 1275 |
} else { |
| 1276 |
err = DB_SUCCESS; |
| 1277 |
} |
| 1278 |
|
| 1279 |
if (slot->buf_block != nullptr) { |
| 1280 |
os_free_block(slot->buf_block); |
| 1281 |
slot->buf_block = nullptr; |
| 1282 |
} |
| 1283 |
|
| 1284 |
} else if ((ulint)slot->n_bytes == (ulint)slot->len) { |
| 1285 |
/* It *must* be a partial read. */ |
| 1286 |
ut_ad(slot->len < slot->type.get_original_size()); |
| 1287 |
|
| 1288 |
/* Has to be a read request, if it is less than |
| 1289 |
the original length. */ |
| 1290 |
ut_ad(slot->type.is_read()); |
| 1291 |
err = check_read(slot, n_bytes); |
| 1292 |
|
| 1293 |
} else { |
| 1294 |
err = DB_FAIL; |
| 1295 |
} |
| 1296 |
|
| 1297 |
return (err); |
| 1298 |
} |
| 1299 |
|
| 1300 |
/** Count the number of free slots |
| 1301 |
@return number of reserved slots */ |
| 1302 |
ulint AIO::pending_io_count() const { |
| 1303 |
acquire(); |
| 1304 |
|
| 1305 |
#ifdef UNIV_DEBUG |
| 1306 |
ut_a(m_n_segments > 0); |
| 1307 |
ut_a(!m_slots.empty()); |
| 1308 |
|
| 1309 |
ulint count = 0; |
| 1310 |
|
| 1311 |
for (ulint i = 0; i < m_slots.size(); ++i) { |
| 1312 |
const Slot &slot = m_slots[i]; |
| 1313 |
|
| 1314 |
if (slot.is_reserved) { |
| 1315 |
++count; |
| 1316 |
ut_a(slot.len > 0); |
| 1317 |
} |
| 1318 |
} |
| 1319 |
|
| 1320 |
ut_a(m_n_reserved == count); |
| 1321 |
#endif /* UNIV_DEBUG */ |
| 1322 |
|
| 1323 |
ulint reserved = m_n_reserved; |
| 1324 |
|
| 1325 |
release(); |
| 1326 |
|
| 1327 |
return (reserved); |
| 1328 |
} |
| 1329 |
#endif /* !UNIV_HOTBACKUP */ |
| 1330 |
|
| 1331 |
/** Compress a data page |
| 1332 |
@param[in] compression Compression algorithm |
| 1333 |
@param[in] block_size File system block size |
| 1334 |
@param[in] src Source contents to compress |
| 1335 |
@param[in] src_len Length in bytes of the source |
| 1336 |
@param[out] dst Compressed page contents |
| 1337 |
@param[out] dst_len Length in bytes of dst contents |
| 1338 |
@return buffer data, dst_len will have the length of the data */ |
| 1339 |
byte *os_file_compress_page(Compression compression, ulint block_size, |
| 1340 |
byte *src, ulint src_len, byte *dst, |
| 1341 |
ulint *dst_len) { |
| 1342 |
ulint len = 0; |
| 1343 |
ulint compression_level = page_zip_level; |
| 1344 |
ulint page_type = mach_read_from_2(src + FIL_PAGE_TYPE); |
| 1345 |
|
| 1346 |
/* The page size must be a multiple of the OS punch hole size. */ |
| 1347 |
ut_ad(!(src_len % block_size)); |
| 1348 |
|
| 1349 |
/* Shouldn't compress an already compressed page. */ |
| 1350 |
ut_ad(page_type != FIL_PAGE_COMPRESSED); |
| 1351 |
ut_ad(page_type != FIL_PAGE_ENCRYPTED); |
| 1352 |
ut_ad(page_type != FIL_PAGE_COMPRESSED_AND_ENCRYPTED); |
| 1353 |
|
| 1354 |
/* The page must be at least twice as large as the file system |
| 1355 |
block size if we are to save any space. Ignore R-Tree pages for now, |
| 1356 |
they repurpose the same 8 bytes in the page header. No point in |
| 1357 |
compressing if the file system block size >= our page size. */ |
| 1358 |
|
| 1359 |
if (page_type == FIL_PAGE_RTREE || block_size == ULINT_UNDEFINED || |
| 1360 |
compression.m_type == Compression::NONE || src_len < block_size * 2) { |
| 1361 |
*dst_len = src_len; |
| 1362 |
|
| 1363 |
return (src); |
| 1364 |
} |
| 1365 |
|
| 1366 |
/* Leave the header alone when compressing. */ |
| 1367 |
ut_ad(block_size >= FIL_PAGE_DATA * 2); |
| 1368 |
|
| 1369 |
ut_ad(src_len > FIL_PAGE_DATA + block_size); |
| 1370 |
|
| 1371 |
/* Must compress to <= N-1 FS blocks. */ |
| 1372 |
ulint out_len = src_len - (FIL_PAGE_DATA + block_size); |
| 1373 |
|
| 1374 |
/* This is the original data page size - the page header. */ |
| 1375 |
ulint content_len = src_len - FIL_PAGE_DATA; |
| 1376 |
|
| 1377 |
ut_ad(out_len >= block_size - FIL_PAGE_DATA); |
| 1378 |
ut_ad(out_len <= src_len - (block_size + FIL_PAGE_DATA)); |
| 1379 |
|
| 1380 |
/* Only compress the data + trailer, leave the header alone */ |
| 1381 |
|
| 1382 |
switch (compression.m_type) { |
| 1383 |
case Compression::NONE: |
| 1384 |
ut_error; |
| 1385 |
|
| 1386 |
case Compression::ZLIB: { |
| 1387 |
uLongf zlen = static_cast<uLongf>(out_len); |
| 1388 |
|
| 1389 |
if (compress2(dst + FIL_PAGE_DATA, &zlen, src + FIL_PAGE_DATA, |
| 1390 |
static_cast<uLong>(content_len), |
| 1391 |
static_cast<int>(compression_level)) != Z_OK) { |
| 1392 |
*dst_len = src_len; |
| 1393 |
|
| 1394 |
return (src); |
| 1395 |
} |
| 1396 |
|
| 1397 |
len = static_cast<ulint>(zlen); |
| 1398 |
|
| 1399 |
break; |
| 1400 |
} |
| 1401 |
|
| 1402 |
case Compression::LZ4: |
| 1403 |
|
| 1404 |
len = LZ4_compress_default(reinterpret_cast<char *>(src) + FIL_PAGE_DATA, |
| 1405 |
reinterpret_cast<char *>(dst) + FIL_PAGE_DATA, |
| 1406 |
static_cast<int>(content_len), |
| 1407 |
static_cast<int>(out_len)); |
| 1408 |
|
| 1409 |
ut_a(len <= src_len - FIL_PAGE_DATA); |
| 1410 |
|
| 1411 |
if (len == 0 || len >= out_len) { |
| 1412 |
*dst_len = src_len; |
| 1413 |
|
| 1414 |
return (src); |
| 1415 |
} |
| 1416 |
|
| 1417 |
break; |
| 1418 |
|
| 1419 |
default: |
| 1420 |
*dst_len = src_len; |
| 1421 |
return (src); |
| 1422 |
} |
| 1423 |
|
| 1424 |
ut_a(len <= out_len); |
| 1425 |
|
| 1426 |
ut_ad(memcmp(src + FIL_PAGE_LSN + 4, |
| 1427 |
src + src_len - FIL_PAGE_END_LSN_OLD_CHKSUM + 4, 4) == 0); |
| 1428 |
|
| 1429 |
/* Copy the header as is. */ |
| 1430 |
memmove(dst, src, FIL_PAGE_DATA); |
| 1431 |
|
| 1432 |
/* Add compression control information. Required for decompressing. */ |
| 1433 |
mach_write_to_2(dst + FIL_PAGE_TYPE, FIL_PAGE_COMPRESSED); |
| 1434 |
|
| 1435 |
mach_write_to_1(dst + FIL_PAGE_VERSION, Compression::FIL_PAGE_VERSION_2); |
| 1436 |
|
| 1437 |
mach_write_to_1(dst + FIL_PAGE_ALGORITHM_V1, compression.m_type); |
| 1438 |
|
| 1439 |
mach_write_to_2(dst + FIL_PAGE_ORIGINAL_TYPE_V1, page_type); |
| 1440 |
|
| 1441 |
mach_write_to_2(dst + FIL_PAGE_ORIGINAL_SIZE_V1, content_len); |
| 1442 |
|
| 1443 |
mach_write_to_2(dst + FIL_PAGE_COMPRESS_SIZE_V1, len); |
| 1444 |
|
| 1445 |
/* Round to the next full block size */ |
| 1446 |
|
| 1447 |
len += FIL_PAGE_DATA; |
| 1448 |
|
| 1449 |
*dst_len = ut_calc_align(len, block_size); |
| 1450 |
|
| 1451 |
ut_ad(*dst_len >= len && *dst_len <= out_len + FIL_PAGE_DATA); |
| 1452 |
|
| 1453 |
/* Clear out the unused portion of the page. */ |
| 1454 |
if (len % block_size) { |
| 1455 |
memset(dst + len, 0x0, block_size - (len % block_size)); |
| 1456 |
} |
| 1457 |
|
| 1458 |
return (dst); |
| 1459 |
} |
| 1460 |
|
| 1461 |
#ifdef UNIV_DEBUG |
| 1462 |
#ifndef UNIV_HOTBACKUP |
| 1463 |
/** Validates the consistency the aio system some of the time. |
| 1464 |
@return true if ok or the check was skipped */ |
| 1465 |
static bool os_aio_validate_skip() { |
| 1466 |
/** Try os_aio_validate() every this many times */ |
| 1467 |
constexpr uint32_t OS_AIO_VALIDATE_SKIP = 13; |
| 1468 |
|
| 1469 |
/** The os_aio_validate() call skip counter. |
| 1470 |
Use a signed type because of the race condition below. */ |
| 1471 |
static int os_aio_validate_count = OS_AIO_VALIDATE_SKIP; |
| 1472 |
|
| 1473 |
/* There is a race condition below, but it does not matter, |
| 1474 |
because this call is only for heuristic purposes. We want to |
| 1475 |
reduce the call frequency of the costly os_aio_validate() |
| 1476 |
check in debug builds. */ |
| 1477 |
--os_aio_validate_count; |
| 1478 |
|
| 1479 |
if (os_aio_validate_count > 0) { |
| 1480 |
return (true); |
| 1481 |
} |
| 1482 |
|
| 1483 |
os_aio_validate_count = OS_AIO_VALIDATE_SKIP; |
| 1484 |
return (os_aio_validate()); |
| 1485 |
} |
| 1486 |
#endif /* !UNIV_HOTBACKUP */ |
| 1487 |
#endif /* UNIV_DEBUG */ |
| 1488 |
|
| 1489 |
#undef USE_FILE_LOCK |
| 1490 |
#define USE_FILE_LOCK |
| 1491 |
#if defined(UNIV_HOTBACKUP) || defined(_WIN32) |
| 1492 |
/* InnoDB Hot Backup does not lock the data files. |
| 1493 |
On Windows, mandatory locking is used. |
| 1494 |
*/ |
| 1495 |
#undef USE_FILE_LOCK |
| 1496 |
#endif /* UNIV_HOTBACKUP || _WIN32 */ |
| 1497 |
#ifdef USE_FILE_LOCK |
| 1498 |
/** Obtain an exclusive lock on a file. |
| 1499 |
@param[in] fd file descriptor |
| 1500 |
@param[in] name file name |
| 1501 |
@return 0 on success */ |
| 1502 |
static int os_file_lock(int fd, const char *name) { |
| 1503 |
struct flock lk; |
| 1504 |
|
| 1505 |
lk.l_type = F_WRLCK; |
| 1506 |
lk.l_whence = SEEK_SET; |
| 1507 |
lk.l_start = lk.l_len = 0; |
| 1508 |
|
| 1509 |
if (fcntl(fd, F_SETLK, &lk) == -1) { |
| 1510 |
ib::error(ER_IB_MSG_749) |
| 1511 |
<< "Unable to lock " << name << " error: " << errno; |
| 1512 |
|
| 1513 |
if (errno == EAGAIN || errno == EACCES) { |
| 1514 |
ib::info(ER_IB_MSG_750) << "Check that you do not already have" |
| 1515 |
" another mysqld process using the" |
| 1516 |
" same InnoDB data or log files."; |
| 1517 |
} |
| 1518 |
|
| 1519 |
return (-1); |
| 1520 |
} |
| 1521 |
|
| 1522 |
return (0); |
| 1523 |
} |
| 1524 |
#endif /* USE_FILE_LOCK */ |
| 1525 |
|
| 1526 |
#ifndef UNIV_HOTBACKUP |
| 1527 |
|
| 1528 |
/** Calculates local segment number and aio array from global |
| 1529 |
segment number. |
| 1530 |
@param[out] array AIO wait array |
| 1531 |
@param[in] segment global segment number |
| 1532 |
@return local segment number within the aio array */ |
| 1533 |
ulint AIO::get_array_and_local_segment(AIO *&array, ulint segment) { |
| 1534 |
const auto extra = number_of_extra_threads(); |
| 1535 |
ut_a(segment < os_aio_n_segments); |
| 1536 |
|
| 1537 |
if (segment < extra) { |
| 1538 |
/* We don't support ibuf IO during read only mode. */ |
| 1539 |
ut_ad(!srv_read_only_mode); |
| 1540 |
ut_a(segment == IO_IBUF_SEGMENT); |
| 1541 |
ut_ad(s_ibuf != nullptr); |
| 1542 |
ut_ad(s_ibuf->get_n_segments() == 1); |
| 1543 |
ut_ad(s_ibuf->get_n_segments() == extra); |
| 1544 |
array = s_ibuf; |
| 1545 |
|
| 1546 |
return 0; |
| 1547 |
} |
| 1548 |
segment -= extra; |
| 1549 |
|
| 1550 |
if (segment < s_reads->m_n_segments) { |
| 1551 |
array = s_reads; |
| 1552 |
|
| 1553 |
return segment; |
| 1554 |
} |
| 1555 |
segment -= s_reads->m_n_segments; |
| 1556 |
|
| 1557 |
ut_a(segment < s_writes->m_n_segments); |
| 1558 |
|
| 1559 |
array = s_writes; |
| 1560 |
|
| 1561 |
return segment; |
| 1562 |
} |
| 1563 |
|
| 1564 |
/** Frees a slot in the aio array. Assumes caller owns the mutex. |
| 1565 |
@param[in,out] slot Slot to release */ |
| 1566 |
void AIO::release(Slot *slot) { |
| 1567 |
ut_ad(is_mutex_owned()); |
| 1568 |
|
| 1569 |
ut_ad(slot->is_reserved); |
| 1570 |
|
| 1571 |
slot->is_reserved = false; |
| 1572 |
|
| 1573 |
--m_n_reserved; |
| 1574 |
|
| 1575 |
if (m_n_reserved == m_slots.size() - 1) { |
| 1576 |
os_event_set(m_not_full); |
| 1577 |
} |
| 1578 |
|
| 1579 |
if (m_n_reserved == 0) { |
| 1580 |
os_event_set(m_is_empty); |
| 1581 |
} |
| 1582 |
|
| 1583 |
#ifdef _WIN32 |
| 1584 |
|
| 1585 |
ResetEvent(slot->handle); |
| 1586 |
|
| 1587 |
#elif defined(LINUX_NATIVE_AIO) |
| 1588 |
|
| 1589 |
if (srv_use_native_aio) { |
| 1590 |
memset(&slot->control, 0x0, sizeof(slot->control)); |
| 1591 |
slot->ret = 0; |
| 1592 |
slot->n_bytes = 0; |
| 1593 |
} else { |
| 1594 |
/* These fields should not be used if we are not |
| 1595 |
using native AIO. */ |
| 1596 |
ut_ad(slot->n_bytes == 0); |
| 1597 |
ut_ad(slot->ret == 0); |
| 1598 |
} |
| 1599 |
|
| 1600 |
#endif /* !_WIN32 && LINUX_NATIVE_AIO */ |
| 1601 |
} |
| 1602 |
|
| 1603 |
/** Frees a slot in the AIO array. Assumes caller doesn't own the mutex. |
| 1604 |
@param[in,out] slot Slot to release */ |
| 1605 |
void AIO::release_with_mutex(Slot *slot) { |
| 1606 |
acquire(); |
| 1607 |
|
| 1608 |
release(slot); |
| 1609 |
|
| 1610 |
release(); |
| 1611 |
} |
| 1612 |
|
| 1613 |
FILE *os_file_create_tmpfile() { |
| 1614 |
FILE *file = nullptr; |
| 1615 |
int fd = innobase_mysql_tmpfile(mysql_tmpdir); |
| 1616 |
|
| 1617 |
if (fd >= 0) { |
| 1618 |
file = fdopen(fd, "w+b"); |
| 1619 |
} |
| 1620 |
|
| 1621 |
if (file == nullptr) { |
| 1622 |
ib::error(ER_IB_MSG_751) << "Unable to create temporary file inside \"" |
| 1623 |
<< mysql_tmpdir << "\"; errno: " << errno; |
| 1624 |
|
| 1625 |
if (fd >= 0) { |
| 1626 |
close(fd); |
| 1627 |
} |
| 1628 |
} |
| 1629 |
|
| 1630 |
return (file); |
| 1631 |
} |
| 1632 |
#endif /* !UNIV_HOTBACKUP */ |
| 1633 |
|
| 1634 |
/** Rewind file to its start, read at most size - 1 bytes from it to str, and |
| 1635 |
NUL-terminate str. All errors are silently ignored. This function is |
| 1636 |
mostly meant to be used with temporary files. |
| 1637 |
@param[in,out] file File to read from |
| 1638 |
@param[in,out] str Buffer where to read |
| 1639 |
@param[in] size Size of buffer */ |
| 1640 |
void os_file_read_string(FILE *file, char *str, ulint size) { |
| 1641 |
if (size != 0) { |
| 1642 |
rewind(file); |
| 1643 |
|
| 1644 |
size_t flen = fread(str, 1, size - 1, file); |
| 1645 |
|
| 1646 |
str[flen] = '\0'; |
| 1647 |
} |
| 1648 |
} |
| 1649 |
|
| 1650 |
static dberr_t os_file_io_complete(const IORequest &type, os_file_t fh, |
| 1651 |
byte *buf, ulint src_len, os_offset_t offset, |
| 1652 |
ulint len) { |
| 1653 |
dberr_t ret = DB_SUCCESS; |
| 1654 |
|
| 1655 |
/* We never compress/decompress the first page */ |
| 1656 |
ut_a(offset > 0); |
| 1657 |
ut_ad(type.validate()); |
| 1658 |
|
| 1659 |
if (!type.is_compression_enabled()) { |
| 1660 |
if (type.is_log() && offset >= LOG_FILE_HDR_SIZE) { |
| 1661 |
ret = type.encryption_algorithm().decrypt_log(buf, src_len); |
| 1662 |
} |
| 1663 |
|
| 1664 |
return (ret); |
| 1665 |
} else if (type.is_read()) { |
| 1666 |
ut_ad(!type.is_row_log()); |
| 1667 |
Encryption encryption(type.encryption_algorithm()); |
| 1668 |
|
| 1669 |
ret = encryption.decrypt(type, buf, src_len, nullptr, 0); |
| 1670 |
|
| 1671 |
if (ret == DB_SUCCESS) { |
| 1672 |
return (os_file_decompress_page(type.is_dblwr(), buf, nullptr, 0)); |
| 1673 |
} else { |
| 1674 |
return (ret); |
| 1675 |
} |
| 1676 |
} else if (type.punch_hole()) { |
| 1677 |
ut_ad(len <= src_len); |
| 1678 |
ut_ad(!type.is_log()); |
| 1679 |
ut_ad(type.is_write()); |
| 1680 |
ut_ad(type.is_compressed()); |
| 1681 |
|
| 1682 |
/* Nothing to do. */ |
| 1683 |
if (len == src_len) { |
| 1684 |
return (DB_SUCCESS); |
| 1685 |
} |
| 1686 |
|
| 1687 |
#ifdef UNIV_DEBUG |
| 1688 |
const ulint block_size = type.block_size(); |
| 1689 |
#endif /* UNIV_DEBUG */ |
| 1690 |
|
| 1691 |
/* We don't support multiple page sizes in the server |
| 1692 |
at the moment. */ |
| 1693 |
ut_ad(src_len == srv_page_size); |
| 1694 |
|
| 1695 |
/* Must be a multiple of the compression unit size. */ |
| 1696 |
ut_ad((len % block_size) == 0); |
| 1697 |
ut_ad((offset % block_size) == 0); |
| 1698 |
|
| 1699 |
ut_ad(len + block_size <= src_len); |
| 1700 |
|
| 1701 |
offset += len; |
| 1702 |
|
| 1703 |
return (os_file_punch_hole(fh, offset, src_len - len)); |
| 1704 |
} |
| 1705 |
|
| 1706 |
ut_ad(!type.is_log()); |
| 1707 |
|
| 1708 |
return (DB_SUCCESS); |
| 1709 |
} |
| 1710 |
|
| 1711 |
/** Check if the path refers to the root of a drive using a pointer |
| 1712 |
to the last directory separator that the caller has fixed. |
| 1713 |
@param[in] path path name |
| 1714 |
@param[in] last_slash last directory separator in the path |
| 1715 |
@return true if this path is a drive root, false if not */ |
| 1716 |
static inline bool os_file_is_root(const char *path, const char *last_slash) { |
| 1717 |
return ( |
| 1718 |
#ifdef _WIN32 |
| 1719 |
(last_slash == path + 2 && path[1] == ':') || |
| 1720 |
#endif /* _WIN32 */ |
| 1721 |
last_slash == path); |
| 1722 |
} |
| 1723 |
|
| 1724 |
/** Return the parent directory component of a null-terminated path. |
| 1725 |
Return a new buffer containing the string up to, but not including, |
| 1726 |
the final component of the path. |
| 1727 |
The path returned will not contain a trailing separator. |
| 1728 |
Do not return a root path, return NULL instead. |
| 1729 |
The final component trimmed off may be a filename or a directory name. |
| 1730 |
If the final component is the only component of the path, return NULL. |
| 1731 |
It is the caller's responsibility to free the returned string after it |
| 1732 |
is no longer needed. |
| 1733 |
@param[in] path Path name |
| 1734 |
@return own: parent directory of the path */ |
| 1735 |
static char *os_file_get_parent_dir(const char *path) { |
| 1736 |
bool has_trailing_slash = false; |
| 1737 |
|
| 1738 |
/* Find the offset of the last slash */ |
| 1739 |
const char *last_slash = strrchr(path, OS_PATH_SEPARATOR); |
| 1740 |
|
| 1741 |
if (!last_slash) { |
| 1742 |
/* No slash in the path, return NULL */ |
| 1743 |
return (nullptr); |
| 1744 |
} |
| 1745 |
|
| 1746 |
/* Ok, there is a slash. Is there anything after it? */ |
| 1747 |
if (static_cast<size_t>(last_slash - path + 1) == strlen(path)) { |
| 1748 |
has_trailing_slash = true; |
| 1749 |
} |
| 1750 |
|
| 1751 |
/* Reduce repetitive slashes. */ |
| 1752 |
while (last_slash > path && last_slash[-1] == OS_PATH_SEPARATOR) { |
| 1753 |
last_slash--; |
| 1754 |
} |
| 1755 |
|
| 1756 |
/* Check for the root of a drive. */ |
| 1757 |
if (os_file_is_root(path, last_slash)) { |
| 1758 |
return (nullptr); |
| 1759 |
} |
| 1760 |
|
| 1761 |
/* If a trailing slash prevented the first strrchr() from trimming |
| 1762 |
the last component of the path, trim that component now. */ |
| 1763 |
if (has_trailing_slash) { |
| 1764 |
/* Back up to the previous slash. */ |
| 1765 |
last_slash--; |
| 1766 |
while (last_slash > path && last_slash[0] != OS_PATH_SEPARATOR) { |
| 1767 |
last_slash--; |
| 1768 |
} |
| 1769 |
|
| 1770 |
/* Reduce repetitive slashes. */ |
| 1771 |
while (last_slash > path && last_slash[-1] == OS_PATH_SEPARATOR) { |
| 1772 |
last_slash--; |
| 1773 |
} |
| 1774 |
} |
| 1775 |
|
| 1776 |
/* Check for the root of a drive. */ |
| 1777 |
if (os_file_is_root(path, last_slash)) { |
| 1778 |
return (nullptr); |
| 1779 |
} |
| 1780 |
|
| 1781 |
if (last_slash - path < 0) { |
| 1782 |
/* Sanity check, it prevents gcc from trying to handle this case which |
| 1783 |
results in warnings for some optimized builds */ |
| 1784 |
return (nullptr); |
| 1785 |
} |
| 1786 |
|
| 1787 |
/* Non-trivial directory component */ |
| 1788 |
|
| 1789 |
return (mem_strdupl(path, last_slash - path)); |
| 1790 |
} |
| 1791 |
#ifdef UNIV_ENABLE_UNIT_TEST_GET_PARENT_DIR |
| 1792 |
|
| 1793 |
/* Test the function os_file_get_parent_dir. */ |
| 1794 |
void test_os_file_get_parent_dir(const char *child_dir, |
| 1795 |
const char *expected_dir) { |
| 1796 |
char *child = mem_strdup(child_dir); |
| 1797 |
char *expected = expected_dir == NULL ? NULL : mem_strdup(expected_dir); |
| 1798 |
|
| 1799 |
/* os_file_get_parent_dir() assumes that separators are |
| 1800 |
converted to OS_PATH_SEPARATOR. */ |
| 1801 |
Fil_path::normalize(child); |
| 1802 |
Fil_path::normalize(expected); |
| 1803 |
|
| 1804 |
char *parent = os_file_get_parent_dir(child); |
| 1805 |
|
| 1806 |
bool unexpected = |
| 1807 |
(expected == NULL ? (parent != NULL) : (0 != strcmp(parent, expected))); |
| 1808 |
if (unexpected) { |
| 1809 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_752) |
| 1810 |
<< "os_file_get_parent_dir('" << child << "') returned '" << parent |
| 1811 |
<< "', instead of '" << expected << "'."; |
| 1812 |
} |
| 1813 |
ut::free(parent); |
| 1814 |
ut::free(child); |
| 1815 |
ut::free(expected); |
| 1816 |
} |
| 1817 |
|
| 1818 |
/* Test the function os_file_get_parent_dir. */ |
| 1819 |
void unit_test_os_file_get_parent_dir() { |
| 1820 |
test_os_file_get_parent_dir("/usr/lib/a", "/usr/lib"); |
| 1821 |
test_os_file_get_parent_dir("/usr/", NULL); |
| 1822 |
test_os_file_get_parent_dir("//usr//", NULL); |
| 1823 |
test_os_file_get_parent_dir("usr", NULL); |
| 1824 |
test_os_file_get_parent_dir("usr//", NULL); |
| 1825 |
test_os_file_get_parent_dir("/", NULL); |
| 1826 |
test_os_file_get_parent_dir("//", NULL); |
| 1827 |
test_os_file_get_parent_dir(".", NULL); |
| 1828 |
test_os_file_get_parent_dir("..", NULL); |
| 1829 |
#ifdef _WIN32 |
| 1830 |
test_os_file_get_parent_dir("D:", NULL); |
| 1831 |
test_os_file_get_parent_dir("D:/", NULL); |
| 1832 |
test_os_file_get_parent_dir("D:\\", NULL); |
| 1833 |
test_os_file_get_parent_dir("D:/data", NULL); |
| 1834 |
test_os_file_get_parent_dir("D:/data/", NULL); |
| 1835 |
test_os_file_get_parent_dir("D:\\data\\", NULL); |
| 1836 |
test_os_file_get_parent_dir("D:///data/////", NULL); |
| 1837 |
test_os_file_get_parent_dir("D:\\\\\\data\\\\\\\\", NULL); |
| 1838 |
test_os_file_get_parent_dir("D:/data//a", "D:/data"); |
| 1839 |
test_os_file_get_parent_dir("D:\\data\\\\a", "D:\\data"); |
| 1840 |
test_os_file_get_parent_dir("D:///data//a///b/", "D:///data//a"); |
| 1841 |
test_os_file_get_parent_dir("D:\\\\\\data\\\\a\\\\\\b\\", |
| 1842 |
"D:\\\\\\data\\\\a"); |
| 1843 |
#endif /* _WIN32 */ |
| 1844 |
} |
| 1845 |
#endif /* UNIV_ENABLE_UNIT_TEST_GET_PARENT_DIR */ |
| 1846 |
|
| 1847 |
/** Creates all missing subdirectories along the given path. |
| 1848 |
@param[in] path Path name |
| 1849 |
@return DB_SUCCESS if OK, otherwise error code. */ |
| 1850 |
dberr_t os_file_create_subdirs_if_needed(const char *path) { |
| 1851 |
if (srv_read_only_mode) { |
| 1852 |
ib::error(ER_IB_MSG_753) << "read only mode set. Can't create " |
| 1853 |
<< "subdirectories '" << path << "'"; |
| 1854 |
|
| 1855 |
return (DB_READ_ONLY); |
| 1856 |
} |
| 1857 |
|
| 1858 |
char *subdir = os_file_get_parent_dir(path); |
| 1859 |
|
| 1860 |
if (subdir == nullptr) { |
| 1861 |
/* subdir is root or cwd, nothing to do */ |
| 1862 |
return (DB_SUCCESS); |
| 1863 |
} |
| 1864 |
|
| 1865 |
/* Test if subdir exists */ |
| 1866 |
os_file_type_t type; |
| 1867 |
bool subdir_exists; |
| 1868 |
bool success = os_file_status(subdir, &subdir_exists, &type); |
| 1869 |
|
| 1870 |
if (success && !subdir_exists) { |
| 1871 |
/* Subdir does not exist, create it */ |
| 1872 |
dberr_t err = os_file_create_subdirs_if_needed(subdir); |
| 1873 |
|
| 1874 |
if (err != DB_SUCCESS) { |
| 1875 |
ut::free(subdir); |
| 1876 |
|
| 1877 |
return (err); |
| 1878 |
} |
| 1879 |
|
| 1880 |
success = os_file_create_directory(subdir, false); |
| 1881 |
} |
| 1882 |
|
| 1883 |
ut::free(subdir); |
| 1884 |
|
| 1885 |
return (success ? DB_SUCCESS : DB_ERROR); |
| 1886 |
} |
| 1887 |
|
| 1888 |
file::Block *os_file_compress_page(IORequest &type, void *&buf, ulint *n) { |
| 1889 |
ut_ad(!type.is_log()); |
| 1890 |
ut_ad(type.is_write()); |
| 1891 |
ut_ad(type.is_compressed()); |
| 1892 |
|
| 1893 |
#ifdef UNIV_DEBUG |
| 1894 |
/* Uncompressed length. */ |
| 1895 |
const ulint buf_len = *n; |
| 1896 |
{ |
| 1897 |
Fil_page_header fph(reinterpret_cast<byte *>(buf)); |
| 1898 |
space_id_t space_id = fph.get_space_id(); |
| 1899 |
page_no_t page_no = fph.get_page_no(); |
| 1900 |
fil_space_t *space = fil_space_get(space_id); |
| 1901 |
if (space != nullptr) { |
| 1902 |
fil_node_t *node = space->get_file_node(&page_no); |
| 1903 |
ut_ad(node->block_size == type.block_size()); |
| 1904 |
/* The page size must be a multiple of the OS punch hole size. */ |
| 1905 |
ut_ad(!(*n % node->block_size)); |
| 1906 |
ut_ad( |
| 1907 |
BlockReporter::is_lsn_valid(reinterpret_cast<byte *>(buf), buf_len)); |
| 1908 |
} |
| 1909 |
} |
| 1910 |
#endif /* UNIV_DEBUG */ |
| 1911 |
|
| 1912 |
ulint n_alloc = *n * 2; |
| 1913 |
|
| 1914 |
ut_a(n_alloc <= UNIV_PAGE_SIZE_MAX * 2); |
| 1915 |
ut_a(type.compression_algorithm().m_type != Compression::LZ4 || |
| 1916 |
static_cast<ulint>(LZ4_COMPRESSBOUND(*n)) < n_alloc); |
| 1917 |
|
| 1918 |
auto block = os_alloc_block(); |
| 1919 |
|
| 1920 |
ulint old_compressed_len; |
| 1921 |
ulint compressed_len = *n; |
| 1922 |
|
| 1923 |
old_compressed_len = mach_read_from_2(reinterpret_cast<byte *>(buf) + |
| 1924 |
FIL_PAGE_COMPRESS_SIZE_V1); |
| 1925 |
|
| 1926 |
if (old_compressed_len > 0) { |
| 1927 |
old_compressed_len = |
| 1928 |
ut_calc_align(old_compressed_len + FIL_PAGE_DATA, type.block_size()); |
| 1929 |
} else { |
| 1930 |
old_compressed_len = *n; |
| 1931 |
} |
| 1932 |
|
| 1933 |
byte *compressed_page; |
| 1934 |
|
| 1935 |
compressed_page = |
| 1936 |
static_cast<byte *>(ut_align(block->m_ptr, os_io_ptr_align)); |
| 1937 |
|
| 1938 |
byte *buf_ptr; |
| 1939 |
|
| 1940 |
buf_ptr = os_file_compress_page( |
| 1941 |
type.compression_algorithm(), type.block_size(), |
| 1942 |
reinterpret_cast<byte *>(buf), *n, compressed_page, &compressed_len); |
| 1943 |
|
| 1944 |
if (buf_ptr != buf) { |
| 1945 |
/* Set new compressed size to uncompressed page. */ |
| 1946 |
memcpy(reinterpret_cast<byte *>(buf) + FIL_PAGE_COMPRESS_SIZE_V1, |
| 1947 |
buf_ptr + FIL_PAGE_COMPRESS_SIZE_V1, 2); |
| 1948 |
|
| 1949 |
buf = buf_ptr; |
| 1950 |
*n = compressed_len; |
| 1951 |
|
| 1952 |
if (compressed_len >= old_compressed_len && |
| 1953 |
!type.is_punch_hole_optimisation_disabled()) { |
| 1954 |
ut_ad(old_compressed_len <= UNIV_PAGE_SIZE); |
| 1955 |
|
| 1956 |
type.clear_punch_hole(); |
| 1957 |
} |
| 1958 |
} |
| 1959 |
|
| 1960 |
return (block); |
| 1961 |
} |
| 1962 |
|
| 1963 |
file::Block *os_file_encrypt_page(const IORequest &type, void *&buf, ulint n) { |
| 1964 |
byte *encrypted_page; |
| 1965 |
ulint encrypted_len = n; |
| 1966 |
byte *buf_ptr; |
| 1967 |
Encryption encryption(type.encryption_algorithm()); |
| 1968 |
|
| 1969 |
ut_ad(type.is_write()); |
| 1970 |
ut_ad(type.is_encrypted()); |
| 1971 |
|
| 1972 |
auto block = os_alloc_block(); |
| 1973 |
|
| 1974 |
encrypted_page = static_cast<byte *>(ut_align(block->m_ptr, os_io_ptr_align)); |
| 1975 |
|
| 1976 |
buf_ptr = encryption.encrypt(type, reinterpret_cast<byte *>(buf), n, |
| 1977 |
encrypted_page, &encrypted_len); |
| 1978 |
block->m_size = encrypted_len; |
| 1979 |
|
| 1980 |
bool encrypted = buf_ptr != buf; |
| 1981 |
|
| 1982 |
if (encrypted) { |
| 1983 |
buf = buf_ptr; |
| 1984 |
} |
| 1985 |
|
| 1986 |
return (block); |
| 1987 |
} |
| 1988 |
|
| 1989 |
/** Encrypt log blocks provided in first n bytes of buf. |
| 1990 |
If encryption is successful then buf will be repointed to the encrypted redo log |
| 1991 |
of length n. |
| 1992 |
If encryption fails then buf is not modified. |
| 1993 |
The encrypted redo log will be stored in a newly allocated block returned from |
| 1994 |
the function or in a newly allocated memory pointed by scratch. |
| 1995 |
The caller takes ownership of the returned block and memory pointed by scratch, |
| 1996 |
and when the buf is no longer needed, it should free the block using |
| 1997 |
os_free_block(block) and scratch memory by using ut::aligned_free(scratch). |
| 1998 |
@param[in] type IO flags |
| 1999 |
@param[in,out] buf before the call should contain unencrypted data, |
| 2000 |
after the call will point to encrypted data, or |
| 2001 |
to the original unencrypted data on failure |
| 2002 |
@param[in,out] scratch if not null contains the buf, and should be |
| 2003 |
freed using ut::aligned_free |
| 2004 |
@param[in] n number of bytes in buf (encryption does not |
| 2005 |
change the length) |
| 2006 |
@return if not null, then it's the block which contain the buf, and should be |
| 2007 |
freed using os_free_block(block). */ |
| 2008 |
static file::Block *os_file_encrypt_log(const IORequest &type, void *&buf, |
| 2009 |
byte *&scratch, ulint n) { |
| 2010 |
byte *buf_ptr; |
| 2011 |
Encryption encryption(type.encryption_algorithm()); |
| 2012 |
file::Block *block{}; |
| 2013 |
|
| 2014 |
ut_ad(type.is_write() && type.is_encrypted() && type.is_log()); |
| 2015 |
ut_ad(n % OS_FILE_LOG_BLOCK_SIZE == 0); |
| 2016 |
|
| 2017 |
if (n <= BUFFER_BLOCK_SIZE - os_io_ptr_align) { |
| 2018 |
block = os_alloc_block(); |
| 2019 |
buf_ptr = static_cast<byte *>(ut_align(block->m_ptr, os_io_ptr_align)); |
| 2020 |
scratch = nullptr; |
| 2021 |
block->m_size = n; |
| 2022 |
} else { |
| 2023 |
buf_ptr = static_cast<byte *>(ut::aligned_alloc(n, os_io_ptr_align)); |
| 2024 |
scratch = buf_ptr; |
| 2025 |
} |
| 2026 |
|
| 2027 |
if (!encryption.encrypt_log(reinterpret_cast<byte *>(buf), n, buf_ptr)) { |
| 2028 |
if (block) { |
| 2029 |
os_free_block(block); |
| 2030 |
} else { |
| 2031 |
ut::aligned_free(scratch); |
| 2032 |
scratch = nullptr; |
| 2033 |
} |
| 2034 |
return nullptr; |
| 2035 |
} |
| 2036 |
buf = buf_ptr; |
| 2037 |
return block; |
| 2038 |
} |
| 2039 |
|
| 2040 |
#ifndef _WIN32 |
| 2041 |
|
| 2042 |
/** Do the read/write |
| 2043 |
@param[in] request The IO context and type |
| 2044 |
@return the number of bytes read/written or negative value on error */ |
| 2045 |
ssize_t SyncFileIO::execute(const IORequest &request) { |
| 2046 |
ssize_t n_bytes; |
| 2047 |
|
| 2048 |
if (request.is_read()) { |
| 2049 |
n_bytes = pread(m_fh, m_buf, m_n, m_offset); |
| 2050 |
} else { |
| 2051 |
ut_ad(request.is_write()); |
| 2052 |
n_bytes = pwrite(m_fh, m_buf, m_n, m_offset); |
| 2053 |
} |
| 2054 |
|
| 2055 |
return (n_bytes); |
| 2056 |
} |
| 2057 |
|
| 2058 |
/** Free storage space associated with a section of the file. |
| 2059 |
@param[in] fh Open file handle |
| 2060 |
@param[in] off Starting offset (SEEK_SET) |
| 2061 |
@param[in] len Size of the hole |
| 2062 |
@return DB_SUCCESS or error code */ |
| 2063 |
static dberr_t os_file_punch_hole_posix(os_file_t fh, os_offset_t off, |
| 2064 |
os_offset_t len) { |
| 2065 |
#ifdef HAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE |
| 2066 |
const int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; |
| 2067 |
|
| 2068 |
int ret = fallocate(fh, mode, off, len); |
| 2069 |
|
| 2070 |
if (ret == 0) { |
| 2071 |
return (DB_SUCCESS); |
| 2072 |
} |
| 2073 |
|
| 2074 |
ut_a(ret == -1); |
| 2075 |
|
| 2076 |
if (errno == ENOTSUP) { |
| 2077 |
return (DB_IO_NO_PUNCH_HOLE); |
| 2078 |
} |
| 2079 |
|
| 2080 |
ib::warn(ER_IB_MSG_754) << "fallocate(" << fh |
| 2081 |
<< ", FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, " |
| 2082 |
<< off << ", " << len |
| 2083 |
<< ") returned errno: " << errno; |
| 2084 |
|
| 2085 |
return (DB_IO_ERROR); |
| 2086 |
|
| 2087 |
#elif defined(UNIV_SOLARIS) |
| 2088 |
|
| 2089 |
// Use F_FREESP |
| 2090 |
|
| 2091 |
#endif /* !HAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE && UNIV_SOLARIS */ |
| 2092 |
|
| 2093 |
return (DB_IO_NO_PUNCH_HOLE); |
| 2094 |
} |
| 2095 |
|
| 2096 |
#if defined(LINUX_NATIVE_AIO) |
| 2097 |
|
| 2098 |
/** Linux native AIO handler */ |
| 2099 |
class LinuxAIOHandler { |
| 2100 |
public: |
| 2101 |
/** |
| 2102 |
@param[in] global_segment The global segment*/ |
| 2103 |
LinuxAIOHandler(ulint global_segment) : m_global_segment(global_segment) { |
| 2104 |
/* Should never be doing Sync IO here. */ |
| 2105 |
ut_a(m_global_segment != ULINT_UNDEFINED); |
| 2106 |
|
| 2107 |
/* Find the array and the local segment. */ |
| 2108 |
|
| 2109 |
m_segment = AIO::get_array_and_local_segment(m_array, m_global_segment); |
| 2110 |
|
| 2111 |
m_n_slots = m_array->slots_per_segment(); |
| 2112 |
} |
| 2113 |
|
| 2114 |
/** Destructor */ |
| 2115 |
~LinuxAIOHandler() { |
| 2116 |
// No op |
| 2117 |
} |
| 2118 |
|
| 2119 |
/** |
| 2120 |
Process a Linux AIO request |
| 2121 |
@param[out] m1 the messages passed with the |
| 2122 |
@param[out] m2 AIO request; note that in case the |
| 2123 |
AIO operation failed, these output |
| 2124 |
parameters are valid and can be used to |
| 2125 |
restart the operation. |
| 2126 |
@param[out] request IO context |
| 2127 |
@return DB_SUCCESS or error code */ |
| 2128 |
dberr_t poll(fil_node_t **m1, void **m2, IORequest *request); |
| 2129 |
|
| 2130 |
private: |
| 2131 |
/** Resubmit an IO request that was only partially successful |
| 2132 |
@param[in,out] slot Request to resubmit |
| 2133 |
@return DB_SUCCESS or DB_FAIL if the IO resubmit request failed */ |
| 2134 |
dberr_t resubmit(Slot *slot); |
| 2135 |
|
| 2136 |
/** Check if the AIO succeeded |
| 2137 |
@param[in,out] slot The slot to check |
| 2138 |
@return DB_SUCCESS, DB_FAIL if the operation should be retried or |
| 2139 |
DB_IO_ERROR on all other errors */ |
| 2140 |
dberr_t check_state(Slot *slot); |
| 2141 |
|
| 2142 |
/** @return true if a shutdown was detected */ |
| 2143 |
bool is_shutdown() const { |
| 2144 |
return (srv_shutdown_state.load() == SRV_SHUTDOWN_EXIT_THREADS && |
| 2145 |
!buf_flush_page_cleaner_is_active()); |
| 2146 |
} |
| 2147 |
|
| 2148 |
/** If no slot was found then the m_array->m_mutex will be released. |
| 2149 |
@param[out] n_pending The number of pending IOs |
| 2150 |
@return NULL or a slot that has completed IO */ |
| 2151 |
Slot *find_completed_slot(ulint *n_pending); |
| 2152 |
|
| 2153 |
/** This is called from within the IO-thread. If there are no completed |
| 2154 |
IO requests in the slot array, the thread calls this function to |
| 2155 |
collect more requests from the Linux kernel. |
| 2156 |
The IO-thread waits on io_getevents(), which is a blocking call, with |
| 2157 |
a timeout value. Unless the system is very heavy loaded, keeping the |
| 2158 |
IO-thread very busy, the io-thread will spend most of its time waiting |
| 2159 |
in this function. |
| 2160 |
The IO-thread also exits in this function. It checks server status at |
| 2161 |
each wakeup and that is why we use timed wait in io_getevents(). */ |
| 2162 |
void collect(); |
| 2163 |
|
| 2164 |
private: |
| 2165 |
/** Slot array */ |
| 2166 |
AIO *m_array; |
| 2167 |
|
| 2168 |
/** Number of slots in the local segment */ |
| 2169 |
ulint m_n_slots; |
| 2170 |
|
| 2171 |
/** The local segment to check */ |
| 2172 |
ulint m_segment; |
| 2173 |
|
| 2174 |
/** The global segment */ |
| 2175 |
ulint m_global_segment; |
| 2176 |
}; |
| 2177 |
|
| 2178 |
/** Resubmit an IO request that was only partially successful |
| 2179 |
@param[in,out] slot Request to resubmit |
| 2180 |
@return DB_SUCCESS or DB_FAIL if the IO resubmit request failed */ |
| 2181 |
dberr_t LinuxAIOHandler::resubmit(Slot *slot) { |
| 2182 |
#ifdef UNIV_DEBUG |
| 2183 |
/* Bytes already read/written out */ |
| 2184 |
ulint n_bytes = slot->ptr - slot->buf; |
| 2185 |
|
| 2186 |
ut_ad(m_array->is_mutex_owned()); |
| 2187 |
|
| 2188 |
ut_ad(n_bytes < slot->type.get_original_size()); |
| 2189 |
ut_ad(static_cast<ulint>(slot->n_bytes) < |
| 2190 |
slot->type.get_original_size() - n_bytes); |
| 2191 |
/* Partial read or write scenario */ |
| 2192 |
ut_ad(slot->len >= static_cast<ulint>(slot->n_bytes)); |
| 2193 |
#endif /* UNIV_DEBUG */ |
| 2194 |
|
| 2195 |
slot->len -= slot->n_bytes; |
| 2196 |
slot->ptr += slot->n_bytes; |
| 2197 |
slot->offset += slot->n_bytes; |
| 2198 |
|
| 2199 |
/* Resetting the bytes read/written */ |
| 2200 |
slot->n_bytes = 0; |
| 2201 |
slot->io_already_done = false; |
| 2202 |
|
| 2203 |
/* make sure that slot->offset fits in off_t */ |
| 2204 |
ut_ad(sizeof(off_t) >= sizeof(os_offset_t)); |
| 2205 |
struct iocb *iocb = &slot->control; |
| 2206 |
if (slot->type.is_read()) { |
| 2207 |
io_prep_pread(iocb, slot->file.m_file, slot->ptr, slot->len, slot->offset); |
| 2208 |
|
| 2209 |
} else { |
| 2210 |
ut_a(slot->type.is_write()); |
| 2211 |
|
| 2212 |
io_prep_pwrite(iocb, slot->file.m_file, slot->ptr, slot->len, slot->offset); |
| 2213 |
} |
| 2214 |
iocb->data = slot; |
| 2215 |
|
| 2216 |
/* Resubmit an I/O request */ |
| 2217 |
int ret = io_submit(m_array->io_ctx(m_segment), 1, &iocb); |
| 2218 |
|
| 2219 |
if (ret < -1) { |
| 2220 |
errno = -ret; |
| 2221 |
} |
| 2222 |
|
| 2223 |
return (ret < 0 ? DB_IO_PARTIAL_FAILED : DB_SUCCESS); |
| 2224 |
} |
| 2225 |
|
| 2226 |
/** Check if the AIO succeeded |
| 2227 |
@param[in,out] slot The slot to check |
| 2228 |
@return DB_SUCCESS, DB_FAIL if the operation should be retried or |
| 2229 |
DB_IO_ERROR on all other errors */ |
| 2230 |
dberr_t LinuxAIOHandler::check_state(Slot *slot) { |
| 2231 |
ut_ad(m_array->is_mutex_owned()); |
| 2232 |
|
| 2233 |
/* Note that it may be that there is more then one completed |
| 2234 |
IO requests. We process them one at a time. We may have a case |
| 2235 |
here to improve the performance slightly by dealing with all |
| 2236 |
requests in one sweep. */ |
| 2237 |
|
| 2238 |
srv_set_io_thread_op_info(m_global_segment, |
| 2239 |
"processing completed aio requests"); |
| 2240 |
|
| 2241 |
ut_ad(slot->io_already_done); |
| 2242 |
|
| 2243 |
dberr_t err; |
| 2244 |
|
| 2245 |
if (slot->ret == 0) { |
| 2246 |
err = AIOHandler::post_io_processing(slot); |
| 2247 |
|
| 2248 |
} else { |
| 2249 |
errno = -slot->ret; |
| 2250 |
|
| 2251 |
/* os_file_handle_error does tell us if we should retry |
| 2252 |
this IO. As it stands now, we don't do this retry when |
| 2253 |
reaping requests from a different context than |
| 2254 |
the dispatcher. This non-retry logic is the same for |
| 2255 |
Windows and Linux native AIO. |
| 2256 |
We should probably look into this to transparently |
| 2257 |
re-submit the IO. */ |
| 2258 |
os_file_handle_error(slot->name, "Linux aio"); |
| 2259 |
|
| 2260 |
err = DB_IO_ERROR; |
| 2261 |
} |
| 2262 |
|
| 2263 |
return (err); |
| 2264 |
} |
| 2265 |
|
| 2266 |
/** If no slot was found then the m_array->m_mutex will be released. |
| 2267 |
@param[out] n_pending The number of pending IOs |
| 2268 |
@return NULL or a slot that has completed IO */ |
| 2269 |
Slot *LinuxAIOHandler::find_completed_slot(ulint *n_pending) { |
| 2270 |
ulint offset = m_n_slots * m_segment; |
| 2271 |
|
| 2272 |
*n_pending = 0; |
| 2273 |
|
| 2274 |
m_array->acquire(); |
| 2275 |
|
| 2276 |
Slot *slot = m_array->at(offset); |
| 2277 |
|
| 2278 |
for (ulint i = 0; i < m_n_slots; ++i, ++slot) { |
| 2279 |
if (slot->is_reserved) { |
| 2280 |
++*n_pending; |
| 2281 |
|
| 2282 |
if (slot->io_already_done) { |
| 2283 |
/* Something for us to work on. |
| 2284 |
Note: We don't release the mutex. */ |
| 2285 |
return (slot); |
| 2286 |
} |
| 2287 |
} |
| 2288 |
} |
| 2289 |
|
| 2290 |
m_array->release(); |
| 2291 |
|
| 2292 |
return (nullptr); |
| 2293 |
} |
| 2294 |
|
| 2295 |
/** This function is only used in Linux native asynchronous i/o. This is |
| 2296 |
called from within the io-thread. If there are no completed IO requests |
| 2297 |
in the slot array, the thread calls this function to collect more |
| 2298 |
requests from the kernel. |
| 2299 |
The io-thread waits on io_getevents(), which is a blocking call, with |
| 2300 |
a timeout value. Unless the system is very heavy loaded, keeping the |
| 2301 |
io-thread very busy, the io-thread will spend most of its time waiting |
| 2302 |
in this function. |
| 2303 |
The io-thread also exits in this function. It checks server status at |
| 2304 |
each wakeup and that is why we use timed wait in io_getevents(). */ |
| 2305 |
void LinuxAIOHandler::collect() { |
| 2306 |
ut_ad(m_n_slots > 0); |
| 2307 |
ut_ad(m_segment < m_array->get_n_segments()); |
| 2308 |
|
| 2309 |
/* Which io_context we are going to use. */ |
| 2310 |
io_context *io_ctx = m_array->io_ctx(m_segment); |
| 2311 |
|
| 2312 |
/* Starting point of the m_segment we will be working on. */ |
| 2313 |
ulint start_pos = m_segment * m_n_slots; |
| 2314 |
|
| 2315 |
/* End point. */ |
| 2316 |
ulint end_pos = start_pos + m_n_slots; |
| 2317 |
|
| 2318 |
for (;;) { |
| 2319 |
struct io_event *events; |
| 2320 |
|
| 2321 |
/* Which part of event array we are going to work on. */ |
| 2322 |
events = m_array->io_events(m_segment * m_n_slots); |
| 2323 |
|
| 2324 |
/* Initialize the events. */ |
| 2325 |
memset(events, 0, sizeof(*events) * m_n_slots); |
| 2326 |
|
| 2327 |
/* The timeout value is arbitrary. We probably need |
| 2328 |
to experiment with it a little. */ |
| 2329 |
struct timespec timeout; |
| 2330 |
|
| 2331 |
timeout.tv_sec = 0; |
| 2332 |
timeout.tv_nsec = OS_AIO_REAP_TIMEOUT; |
| 2333 |
|
| 2334 |
auto ret = io_getevents(io_ctx, 1, m_n_slots, events, &timeout); |
| 2335 |
|
| 2336 |
for (int i = 0; i < ret; ++i) { |
| 2337 |
auto iocb = reinterpret_cast<struct iocb *>(events[i].obj); |
| 2338 |
ut_a(iocb != nullptr); |
| 2339 |
|
| 2340 |
auto slot = reinterpret_cast<Slot *>(iocb->data); |
| 2341 |
|
| 2342 |
/* Some sanity checks. */ |
| 2343 |
ut_a(slot != nullptr); |
| 2344 |
ut_a(slot->is_reserved); |
| 2345 |
|
| 2346 |
/* We are not scribbling previous segment. */ |
| 2347 |
ut_a(slot->pos >= start_pos); |
| 2348 |
|
| 2349 |
/* We have not overstepped to next segment. */ |
| 2350 |
ut_a(slot->pos < end_pos); |
| 2351 |
|
| 2352 |
/** If write of the page is compressed (compression is enabled, it is not |
| 2353 |
the first page, it is not a redolog, not a doublewrite buffer) and punch |
| 2354 |
holes are enabled, call AIOHandler::io_complete to check if hole punching |
| 2355 |
is needed. |
| 2356 |
Keep in sync with os_aio_windows_handler(). */ |
| 2357 |
if (slot->offset > 0 && !slot->skip_punch_hole && |
| 2358 |
slot->type.is_compression_enabled() && !slot->type.is_log() && |
| 2359 |
slot->type.is_write() && slot->type.is_compressed() && |
| 2360 |
slot->type.punch_hole() && !slot->type.is_dblwr()) { |
| 2361 |
slot->err = AIOHandler::io_complete(slot); |
| 2362 |
} else { |
| 2363 |
slot->err = DB_SUCCESS; |
| 2364 |
} |
| 2365 |
|
| 2366 |
/* Mark this request as completed. The error handling |
| 2367 |
will be done in the calling function. */ |
| 2368 |
m_array->acquire(); |
| 2369 |
|
| 2370 |
/* events[i].res2 should always be ZERO */ |
| 2371 |
ut_ad(events[i].res2 == 0); |
| 2372 |
slot->io_already_done = true; |
| 2373 |
|
| 2374 |
/* Even though events[i].res is an unsigned number in libaio, it is |
| 2375 |
used to return a negative value (negated errno value) to indicate |
| 2376 |
error and a positive value to indicate number of bytes read or |
| 2377 |
written. */ |
| 2378 |
|
| 2379 |
if (events[i].res > slot->len) { |
| 2380 |
/* failure */ |
| 2381 |
slot->n_bytes = 0; |
| 2382 |
slot->ret = events[i].res; |
| 2383 |
} else { |
| 2384 |
/* success */ |
| 2385 |
slot->n_bytes = events[i].res; |
| 2386 |
slot->ret = 0; |
| 2387 |
} |
| 2388 |
m_array->release(); |
| 2389 |
} |
| 2390 |
|
| 2391 |
if (srv_shutdown_state.load() == SRV_SHUTDOWN_EXIT_THREADS || |
| 2392 |
!buf_flush_page_cleaner_is_active() || ret > 0) { |
| 2393 |
break; |
| 2394 |
} |
| 2395 |
|
| 2396 |
/* This error handling is for any error in collecting the |
| 2397 |
IO requests. The errors, if any, for any particular IO |
| 2398 |
request are simply passed on to the calling routine. */ |
| 2399 |
|
| 2400 |
switch (ret) { |
| 2401 |
case -EAGAIN: |
| 2402 |
/* Not enough resources! Try again. */ |
| 2403 |
|
| 2404 |
case -EINTR: |
| 2405 |
/* Interrupted! The behaviour in case of an interrupt. |
| 2406 |
If we have some completed IOs available then the |
| 2407 |
return code will be the number of IOs. We get EINTR |
| 2408 |
only if there are no completed IOs and we have been |
| 2409 |
interrupted. */ |
| 2410 |
|
| 2411 |
case 0: |
| 2412 |
/* No pending request! Go back and check again. */ |
| 2413 |
|
| 2414 |
continue; |
| 2415 |
} |
| 2416 |
|
| 2417 |
/* All other errors should cause a trap for now. */ |
| 2418 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_755) |
| 2419 |
<< "Unexpected ret_code[" << ret << "] from io_getevents()!"; |
| 2420 |
|
| 2421 |
break; |
| 2422 |
} |
| 2423 |
} |
| 2424 |
|
| 2425 |
/** Process a Linux AIO request |
| 2426 |
@param[out] m1 the messages passed with the |
| 2427 |
@param[out] m2 AIO request; note that in case the |
| 2428 |
AIO operation failed, these output |
| 2429 |
parameters are valid and can be used to |
| 2430 |
restart the operation. |
| 2431 |
@param[out] request IO context |
| 2432 |
@return DB_SUCCESS or error code */ |
| 2433 |
dberr_t LinuxAIOHandler::poll(fil_node_t **m1, void **m2, IORequest *request) { |
| 2434 |
dberr_t err; |
| 2435 |
Slot *slot; |
| 2436 |
|
| 2437 |
/* Loop until we have found a completed request. */ |
| 2438 |
for (;;) { |
| 2439 |
ulint n_pending; |
| 2440 |
|
| 2441 |
slot = find_completed_slot(&n_pending); |
| 2442 |
|
| 2443 |
if (slot != nullptr) { |
| 2444 |
ut_ad(m_array->is_mutex_owned()); |
| 2445 |
|
| 2446 |
err = check_state(slot); |
| 2447 |
|
| 2448 |
/* DB_FAIL is not a hard error, we should retry */ |
| 2449 |
if (err != DB_FAIL) { |
| 2450 |
break; |
| 2451 |
} |
| 2452 |
|
| 2453 |
/* Partial IO, resubmit request for |
| 2454 |
remaining bytes to read/write */ |
| 2455 |
err = resubmit(slot); |
| 2456 |
|
| 2457 |
if (err != DB_SUCCESS) { |
| 2458 |
break; |
| 2459 |
} |
| 2460 |
|
| 2461 |
m_array->release(); |
| 2462 |
|
| 2463 |
} else if (is_shutdown() && n_pending == 0) { |
| 2464 |
/* There is no completed request. If there is |
| 2465 |
no pending request at all, and the system is |
| 2466 |
being shut down, exit. */ |
| 2467 |
|
| 2468 |
*m1 = nullptr; |
| 2469 |
*m2 = nullptr; |
| 2470 |
|
| 2471 |
return (DB_SUCCESS); |
| 2472 |
|
| 2473 |
} else { |
| 2474 |
/* Wait for some request. Note that we return |
| 2475 |
from wait if we have found a request. */ |
| 2476 |
|
| 2477 |
srv_set_io_thread_op_info(m_global_segment, |
| 2478 |
"waiting for completed aio requests"); |
| 2479 |
|
| 2480 |
collect(); |
| 2481 |
} |
| 2482 |
} |
| 2483 |
|
| 2484 |
if (err == DB_IO_PARTIAL_FAILED) { |
| 2485 |
/* Aborting in case of submit failure */ |
| 2486 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_756) |
| 2487 |
<< "Native Linux AIO interface. " |
| 2488 |
"io_submit() call failed when " |
| 2489 |
"resubmitting a partial I/O " |
| 2490 |
"request on the file " |
| 2491 |
<< slot->name << "."; |
| 2492 |
} |
| 2493 |
|
| 2494 |
*m1 = slot->m1; |
| 2495 |
*m2 = slot->m2; |
| 2496 |
|
| 2497 |
*request = slot->type; |
| 2498 |
|
| 2499 |
m_array->release(slot); |
| 2500 |
|
| 2501 |
m_array->release(); |
| 2502 |
|
| 2503 |
return (err); |
| 2504 |
} |
| 2505 |
|
| 2506 |
/** This function is only used in Linux native asynchronous i/o. |
| 2507 |
Waits for an aio operation to complete. This function is used to wait for |
| 2508 |
the completed requests. The aio array of pending requests is divided |
| 2509 |
into segments. The thread specifies which segment or slot it wants to wait |
| 2510 |
for. NOTE: this function will also take care of freeing the aio slot, |
| 2511 |
therefore no other thread is allowed to do the freeing! |
| 2512 |
|
| 2513 |
@param[in] global_segment segment number in the aio array |
| 2514 |
to wait for; segment 0 is the ibuf i/o thread, |
| 2515 |
then follow the non-ibuf read threads, |
| 2516 |
and the last are the non-ibuf write threads. |
| 2517 |
@param[out] m1 the messages passed with the |
| 2518 |
@param[out] m2 AIO request; note that in case the |
| 2519 |
AIO operation failed, these output |
| 2520 |
parameters are valid and can be used to |
| 2521 |
restart the operation. |
| 2522 |
@param[out] request IO context |
| 2523 |
@return DB_SUCCESS if the IO was successful */ |
| 2524 |
static dberr_t os_aio_linux_handler(ulint global_segment, fil_node_t **m1, |
| 2525 |
void **m2, IORequest *request) { |
| 2526 |
LinuxAIOHandler handler(global_segment); |
| 2527 |
|
| 2528 |
dberr_t err = handler.poll(m1, m2, request); |
| 2529 |
|
| 2530 |
if (err == DB_IO_NO_PUNCH_HOLE) { |
| 2531 |
if (!request->is_dblwr()) { |
| 2532 |
fil_no_punch_hole(*m1); |
| 2533 |
err = DB_SUCCESS; |
| 2534 |
} |
| 2535 |
} |
| 2536 |
|
| 2537 |
return (err); |
| 2538 |
} |
| 2539 |
|
| 2540 |
/** Dispatch an AIO request to the kernel. |
| 2541 |
@param[in,out] slot an already reserved slot |
| 2542 |
@return true on success. */ |
| 2543 |
bool AIO::linux_dispatch(Slot *slot) { |
| 2544 |
ut_a(slot->is_reserved); |
| 2545 |
ut_ad(slot->type.validate()); |
| 2546 |
|
| 2547 |
/* Find out what we are going to work with. |
| 2548 |
The iocb struct is directly in the slot. |
| 2549 |
The io_context is one per segment. */ |
| 2550 |
|
| 2551 |
ulint io_ctx_index; |
| 2552 |
struct iocb *iocb = &slot->control; |
| 2553 |
|
| 2554 |
io_ctx_index = (slot->pos * m_n_segments) / m_slots.size(); |
| 2555 |
|
| 2556 |
int ret = io_submit(m_aio_ctx[io_ctx_index], 1, &iocb); |
| 2557 |
|
| 2558 |
/* io_submit() returns number of successfully queued requests |
| 2559 |
or -errno. */ |
| 2560 |
|
| 2561 |
if (ret != 1) { |
| 2562 |
errno = -ret; |
| 2563 |
} |
| 2564 |
|
| 2565 |
return (ret == 1); |
| 2566 |
} |
| 2567 |
|
| 2568 |
/** Creates an io_context for native linux AIO. |
| 2569 |
@param[in] max_events number of events |
| 2570 |
@param[out] io_ctx io_ctx to initialize. |
| 2571 |
@return true on success. */ |
| 2572 |
bool AIO::linux_create_io_ctx(ulint max_events, io_context_t *io_ctx) { |
| 2573 |
ssize_t n_retries = 0; |
| 2574 |
|
| 2575 |
for (;;) { |
| 2576 |
memset(io_ctx, 0x0, sizeof(*io_ctx)); |
| 2577 |
|
| 2578 |
/* Initialize the io_ctx. Tell it how many pending |
| 2579 |
IO requests this context will handle. */ |
| 2580 |
|
| 2581 |
int ret = io_setup(max_events, io_ctx); |
| 2582 |
|
| 2583 |
if (ret == 0) { |
| 2584 |
/* Success. Return now. */ |
| 2585 |
return (true); |
| 2586 |
} |
| 2587 |
|
| 2588 |
/* If we hit EAGAIN we'll make a few attempts before failing. */ |
| 2589 |
|
| 2590 |
switch (ret) { |
| 2591 |
case -EAGAIN: |
| 2592 |
if (n_retries == 0) { |
| 2593 |
/* First time around. */ |
| 2594 |
ib::warn(ER_IB_MSG_757) << "io_setup() failed with EAGAIN." |
| 2595 |
" Will make " |
| 2596 |
<< OS_AIO_IO_SETUP_RETRY_ATTEMPTS |
| 2597 |
<< " attempts before giving up."; |
| 2598 |
} |
| 2599 |
|
| 2600 |
if (n_retries < OS_AIO_IO_SETUP_RETRY_ATTEMPTS) { |
| 2601 |
++n_retries; |
| 2602 |
|
| 2603 |
ib::warn(ER_IB_MSG_758) << "io_setup() attempt " << n_retries << "."; |
| 2604 |
|
| 2605 |
std::this_thread::sleep_for(OS_AIO_IO_SETUP_RETRY_SLEEP); |
| 2606 |
|
| 2607 |
continue; |
| 2608 |
} |
| 2609 |
|
| 2610 |
/* Have tried enough. Better call it a day. */ |
| 2611 |
ib::error(ER_IB_MSG_759) |
| 2612 |
<< "io_setup() failed with EAGAIN after " |
| 2613 |
<< OS_AIO_IO_SETUP_RETRY_ATTEMPTS << " attempts."; |
| 2614 |
break; |
| 2615 |
|
| 2616 |
case -ENOSYS: |
| 2617 |
ib::error(ER_IB_MSG_760) << "Linux Native AIO interface" |
| 2618 |
" is not supported on this platform. Please" |
| 2619 |
" check your OS documentation and install" |
| 2620 |
" appropriate binary of InnoDB."; |
| 2621 |
|
| 2622 |
break; |
| 2623 |
|
| 2624 |
default: |
| 2625 |
ib::error(ER_IB_MSG_761) << "Linux Native AIO setup" |
| 2626 |
<< " returned following error[" << ret << "]"; |
| 2627 |
break; |
| 2628 |
} |
| 2629 |
|
| 2630 |
ib::info(ER_IB_MSG_762) << "You can disable Linux Native AIO by" |
| 2631 |
" setting innodb_use_native_aio = 0 in my.cnf"; |
| 2632 |
|
| 2633 |
break; |
| 2634 |
} |
| 2635 |
|
| 2636 |
return (false); |
| 2637 |
} |
| 2638 |
|
| 2639 |
/** Checks if the system supports native linux aio. On some kernel |
| 2640 |
versions where native aio is supported it won't work on tmpfs. In such |
| 2641 |
cases we can't use native aio as it is not possible to mix simulated |
| 2642 |
and native aio. |
| 2643 |
@return: true if supported, false otherwise. */ |
| 2644 |
bool AIO::is_linux_native_aio_supported() { |
| 2645 |
int fd; |
| 2646 |
io_context_t io_ctx; |
| 2647 |
const char *name; |
| 2648 |
|
| 2649 |
if (!linux_create_io_ctx(1, &io_ctx)) { |
| 2650 |
/* The platform does not support native aio. */ |
| 2651 |
|
| 2652 |
return (false); |
| 2653 |
|
| 2654 |
} else if (!srv_read_only_mode) { |
| 2655 |
/* Now check if tmpdir supports native aio ops. */ |
| 2656 |
fd = innobase_mysql_tmpfile(nullptr); |
| 2657 |
|
| 2658 |
if (fd < 0) { |
| 2659 |
ib::warn(ER_IB_MSG_763) << "Unable to create temp file to check" |
| 2660 |
" native AIO support."; |
| 2661 |
|
| 2662 |
return (false); |
| 2663 |
} |
| 2664 |
name = "tmpdir"; |
| 2665 |
} else { |
| 2666 |
const auto file_path = srv_sys_space.first_datafile()->filepath(); |
| 2667 |
|
| 2668 |
fd = ::open(file_path, O_RDONLY); |
| 2669 |
|
| 2670 |
if (fd == -1) { |
| 2671 |
ib::warn(ER_IB_MSG_764) << "Unable to open" |
| 2672 |
<< " \"" << file_path << "\" to check native" |
| 2673 |
<< " AIO read support."; |
| 2674 |
|
| 2675 |
return (false); |
| 2676 |
} |
| 2677 |
name = file_path; |
| 2678 |
} |
| 2679 |
|
| 2680 |
struct io_event io_event; |
| 2681 |
|
| 2682 |
memset(&io_event, 0x0, sizeof(io_event)); |
| 2683 |
|
| 2684 |
byte *buf = |
| 2685 |
static_cast<byte *>(ut::aligned_zalloc(UNIV_PAGE_SIZE, UNIV_PAGE_SIZE)); |
| 2686 |
|
| 2687 |
struct iocb iocb; |
| 2688 |
|
| 2689 |
/* Suppress valgrind warning. */ |
| 2690 |
memset(&iocb, 0x0, sizeof(iocb)); |
| 2691 |
|
| 2692 |
struct iocb *p_iocb = &iocb; |
| 2693 |
|
| 2694 |
if (!srv_read_only_mode) { |
| 2695 |
io_prep_pwrite(p_iocb, fd, buf, UNIV_PAGE_SIZE, 0); |
| 2696 |
|
| 2697 |
} else { |
| 2698 |
ut_a(UNIV_PAGE_SIZE >= 512); |
| 2699 |
io_prep_pread(p_iocb, fd, buf, 512, 0); |
| 2700 |
} |
| 2701 |
|
| 2702 |
int err = io_submit(io_ctx, 1, &p_iocb); |
| 2703 |
|
| 2704 |
if (err >= 1) { |
| 2705 |
/* Now collect the submitted IO request. */ |
| 2706 |
err = io_getevents(io_ctx, 1, 1, &io_event, nullptr); |
| 2707 |
} |
| 2708 |
|
| 2709 |
ut::aligned_free(buf); |
| 2710 |
close(fd); |
| 2711 |
|
| 2712 |
switch (err) { |
| 2713 |
case 1: |
| 2714 |
return (true); |
| 2715 |
|
| 2716 |
case -EINVAL: |
| 2717 |
case -ENOSYS: |
| 2718 |
ib::error(ER_IB_MSG_765) |
| 2719 |
<< "Linux Native AIO not supported. You can either" |
| 2720 |
" move " |
| 2721 |
<< (srv_read_only_mode ? name : "tmpdir") |
| 2722 |
<< " to a file system that supports native" |
| 2723 |
" AIO or you can set innodb_use_native_aio to" |
| 2724 |
" false to avoid this message."; |
| 2725 |
|
| 2726 |
[[fallthrough]]; |
| 2727 |
default: |
| 2728 |
ib::error(ER_IB_MSG_766) << "Linux Native AIO check on " << name |
| 2729 |
<< "returned error[" << -err << "]"; |
| 2730 |
} |
| 2731 |
|
| 2732 |
return (false); |
| 2733 |
} |
| 2734 |
|
| 2735 |
#endif /* LINUX_NATIVE_AIO */ |
| 2736 |
|
| 2737 |
/** Retrieves the last error number if an error occurs in a file io function. |
| 2738 |
The number should be retrieved before any other OS calls (because they may |
| 2739 |
overwrite the error number). If the number is not known to this program, |
| 2740 |
the OS error number + 100 is returned. |
| 2741 |
@param[in] report_all_errors true if we want an error message |
| 2742 |
printed of all errors |
| 2743 |
@param[in] on_error_silent true then don't print any diagnostic |
| 2744 |
to the log |
| 2745 |
@return error number, or OS error number + 100 */ |
| 2746 |
static ulint os_file_get_last_error_low(bool report_all_errors, |
| 2747 |
bool on_error_silent) { |
| 2748 |
int err = errno; |
| 2749 |
|
| 2750 |
if (err == 0) { |
| 2751 |
return 0; |
| 2752 |
} |
| 2753 |
|
| 2754 |
if (report_all_errors || |
| 2755 |
(err != ENOSPC && err != EEXIST && !on_error_silent)) { |
| 2756 |
ib::error(ER_IB_MSG_767) |
| 2757 |
<< "Operating system error number " << err << " in a file operation."; |
| 2758 |
|
| 2759 |
if (err == ENOENT) { |
| 2760 |
ib::error(ER_IB_MSG_768) << "The error means the system" |
| 2761 |
" cannot find the path specified."; |
| 2762 |
|
| 2763 |
#ifndef UNIV_HOTBACKUP |
| 2764 |
if (srv_is_being_started) { |
| 2765 |
ib::error(ER_IB_MSG_769) << "If you are installing InnoDB," |
| 2766 |
" remember that you must create" |
| 2767 |
" directories yourself, InnoDB" |
| 2768 |
" does not create them."; |
| 2769 |
} |
| 2770 |
#endif /* !UNIV_HOTBACKUP */ |
| 2771 |
} else if (err == EACCES) { |
| 2772 |
ib::error(ER_IB_MSG_770) << "The error means mysqld does not have" |
| 2773 |
" the access rights to the directory."; |
| 2774 |
|
| 2775 |
} else { |
| 2776 |
if (strerror(err) != nullptr) { |
| 2777 |
ib::error(ER_IB_MSG_771) |
| 2778 |
<< "Error number " << err << " means '" << strerror(err) << "'"; |
| 2779 |
} |
| 2780 |
|
| 2781 |
ib::info(ER_IB_MSG_772) << OPERATING_SYSTEM_ERROR_MSG; |
| 2782 |
} |
| 2783 |
} |
| 2784 |
|
| 2785 |
switch (err) { |
| 2786 |
case ENOSPC: |
| 2787 |
return OS_FILE_DISK_FULL; |
| 2788 |
case ENOENT: |
| 2789 |
return OS_FILE_NOT_FOUND; |
| 2790 |
case EEXIST: |
| 2791 |
return OS_FILE_ALREADY_EXISTS; |
| 2792 |
case EXDEV: |
| 2793 |
case ENOTDIR: |
| 2794 |
case EISDIR: |
| 2795 |
return OS_FILE_PATH_ERROR; |
| 2796 |
case EAGAIN: |
| 2797 |
if (srv_use_native_aio) { |
| 2798 |
return OS_FILE_AIO_RESOURCES_RESERVED; |
| 2799 |
} |
| 2800 |
break; |
| 2801 |
case EINTR: |
| 2802 |
if (srv_use_native_aio) { |
| 2803 |
return OS_FILE_AIO_INTERRUPTED; |
| 2804 |
} |
| 2805 |
break; |
| 2806 |
case EACCES: |
| 2807 |
return OS_FILE_ACCESS_VIOLATION; |
| 2808 |
case ENAMETOOLONG: |
| 2809 |
return OS_FILE_NAME_TOO_LONG; |
| 2810 |
case EMFILE: |
| 2811 |
return OS_FILE_TOO_MANY_OPENED; |
| 2812 |
} |
| 2813 |
return OS_FILE_ERROR_MAX + err; |
| 2814 |
} |
| 2815 |
|
| 2816 |
/** Wrapper to fsync(2)/fdatasync(2) that retries the call on some errors. |
| 2817 |
Returns the value 0 if successful; otherwise the value -1 is returned and |
| 2818 |
the global variable errno is set to indicate the error. srv_use_fdatasync |
| 2819 |
determines whether fsync or fdatasync will be used. (true -> fdatasync) |
| 2820 |
@param[in] file open file handle |
| 2821 |
@return 0 if success, -1 otherwise */ |
| 2822 |
static int os_file_fsync_posix(os_file_t file) { |
| 2823 |
ulint failures = 0; |
| 2824 |
#ifdef UNIV_HOTBACKUP |
| 2825 |
static meb::Mutex meb_mutex; |
| 2826 |
#endif /* UNIV_HOTBACKUP */ |
| 2827 |
|
| 2828 |
for (;;) { |
| 2829 |
#ifdef UNIV_HOTBACKUP |
| 2830 |
meb_mutex.lock(); |
| 2831 |
#endif /* UNIV_HOTBACKUP */ |
| 2832 |
++os_n_fsyncs; |
| 2833 |
#ifdef UNIV_HOTBACKUP |
| 2834 |
meb_mutex.unlock(); |
| 2835 |
#endif /* UNIV_HOTBACKUP */ |
| 2836 |
|
| 2837 |
#if defined(HAVE_FDATASYNC) && defined(HAVE_DECL_FDATASYNC) |
| 2838 |
const auto ret = srv_use_fdatasync ? fdatasync(file) : fsync(file); |
| 2839 |
#else /* !(HAVE_FDATASYNC && HAVE_DECL_FDATASYNC) */ |
| 2840 |
const auto ret = fsync(file); |
| 2841 |
#endif /* !(HAVE_FDATASYNC && HAVE_DECL_FDATASYNC) */ |
| 2842 |
|
| 2843 |
if (ret == 0) { |
| 2844 |
return (ret); |
| 2845 |
} |
| 2846 |
|
| 2847 |
switch (errno) { |
| 2848 |
case ENOLCK: |
| 2849 |
|
| 2850 |
++failures; |
| 2851 |
ut_a(failures < 1000); |
| 2852 |
|
| 2853 |
if (!(failures % 100)) { |
| 2854 |
ib::warn(ER_IB_MSG_773) << "fsync(): " |
| 2855 |
<< "No locks available; retrying"; |
| 2856 |
} |
| 2857 |
|
| 2858 |
/* 0.2 sec */ |
| 2859 |
std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
| 2860 |
break; |
| 2861 |
|
| 2862 |
case EIO: |
| 2863 |
|
| 2864 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_1358) |
| 2865 |
<< "fsync() returned EIO, aborting."; |
| 2866 |
break; |
| 2867 |
|
| 2868 |
case EINTR: |
| 2869 |
|
| 2870 |
++failures; |
| 2871 |
ut_a(failures < 2000); |
| 2872 |
break; |
| 2873 |
|
| 2874 |
default: |
| 2875 |
ut_error; |
| 2876 |
break; |
| 2877 |
} |
| 2878 |
} |
| 2879 |
|
| 2880 |
ut_error; |
| 2881 |
|
| 2882 |
return (-1); |
| 2883 |
} |
| 2884 |
|
| 2885 |
/** fsync the parent directory of a path. Useful following rename, unlink, etc.. |
| 2886 |
@param[in] path path of file */ |
| 2887 |
static void os_parent_dir_fsync_posix(const char *path) { |
| 2888 |
ut_a(path[0] != '\0'); |
| 2889 |
|
| 2890 |
auto parent_in_path = os_file_get_parent_dir(path); |
| 2891 |
const char *parent_dir = parent_in_path; |
| 2892 |
if (parent_in_path == nullptr) { |
| 2893 |
/** if there is no parent dir in the path, then the real parent is |
| 2894 |
either the current directory, or the root directory */ |
| 2895 |
if (path[0] == '/') { |
| 2896 |
parent_dir = "/"; |
| 2897 |
} else { |
| 2898 |
parent_dir = "."; |
| 2899 |
} |
| 2900 |
} |
| 2901 |
|
| 2902 |
/* Open the parent directory */ |
| 2903 |
auto dir_fd = ::open(parent_dir, O_RDONLY); |
| 2904 |
|
| 2905 |
ut_a(dir_fd != -1); |
| 2906 |
|
| 2907 |
if (parent_in_path != nullptr) { |
| 2908 |
ut::free(parent_in_path); |
| 2909 |
} |
| 2910 |
|
| 2911 |
/** Using fsync even when --innodb_use_fdatasync=ON |
| 2912 |
since this operation is not very frequent, but WSL1 does |
| 2913 |
not support fdatasync on directories. */ |
| 2914 |
auto ret = ::fsync(dir_fd); |
| 2915 |
ut_a(ret == 0); |
| 2916 |
|
| 2917 |
::close(dir_fd); |
| 2918 |
} |
| 2919 |
|
| 2920 |
/** Check the existence and type of the given file. |
| 2921 |
@param[in] path path name of file |
| 2922 |
@param[out] exists true if the file exists |
| 2923 |
@param[out] type Type of the file, if it exists |
| 2924 |
@return true if call succeeded */ |
| 2925 |
static bool os_file_status_posix(const char *path, bool *exists, |
| 2926 |
os_file_type_t *type) { |
| 2927 |
struct stat statinfo; |
| 2928 |
|
| 2929 |
int ret = stat(path, &statinfo); |
| 2930 |
|
| 2931 |
if (exists != nullptr) { |
| 2932 |
*exists = !ret; |
| 2933 |
} |
| 2934 |
|
| 2935 |
if (ret == 0) { |
| 2936 |
/* file exists, everything OK */ |
| 2937 |
|
| 2938 |
} else if (errno == ENOENT || errno == ENOTDIR) { |
| 2939 |
if (exists != nullptr) { |
| 2940 |
*exists = false; |
| 2941 |
} |
| 2942 |
|
| 2943 |
/* file does not exist */ |
| 2944 |
*type = OS_FILE_TYPE_MISSING; |
| 2945 |
return (true); |
| 2946 |
|
| 2947 |
} else if (errno == ENAMETOOLONG) { |
| 2948 |
*type = OS_FILE_TYPE_NAME_TOO_LONG; |
| 2949 |
return (false); |
| 2950 |
} else if (errno == EACCES) { |
| 2951 |
*type = OS_FILE_PERMISSION_ERROR; |
| 2952 |
return (false); |
| 2953 |
} else { |
| 2954 |
*type = OS_FILE_TYPE_FAILED; |
| 2955 |
|
| 2956 |
/* The stat() call failed with some other error. */ |
| 2957 |
os_file_handle_error_no_exit(path, "file_status_posix_stat", false); |
| 2958 |
return (false); |
| 2959 |
} |
| 2960 |
|
| 2961 |
if (exists != nullptr) { |
| 2962 |
*exists = true; |
| 2963 |
} |
| 2964 |
|
| 2965 |
if (S_ISDIR(statinfo.st_mode)) { |
| 2966 |
*type = OS_FILE_TYPE_DIR; |
| 2967 |
|
| 2968 |
} else if (S_ISLNK(statinfo.st_mode)) { |
| 2969 |
*type = OS_FILE_TYPE_LINK; |
| 2970 |
|
| 2971 |
} else if (S_ISREG(statinfo.st_mode)) { |
| 2972 |
*type = OS_FILE_TYPE_FILE; |
| 2973 |
|
| 2974 |
} else { |
| 2975 |
*type = OS_FILE_TYPE_UNKNOWN; |
| 2976 |
} |
| 2977 |
|
| 2978 |
return (true); |
| 2979 |
} |
| 2980 |
|
| 2981 |
/** Check the existence and usefulness of a given path. |
| 2982 |
@param[in] path path name |
| 2983 |
@retval true if the path exists and can be used |
| 2984 |
@retval false if the path does not exist or if the path is |
| 2985 |
unusable to get to a possibly existing file or directory. */ |
| 2986 |
static bool os_file_exists_posix(const char *path) { |
| 2987 |
struct stat statinfo; |
| 2988 |
|
| 2989 |
int ret = stat(path, &statinfo); |
| 2990 |
|
| 2991 |
if (ret == 0) { |
| 2992 |
return (true); |
| 2993 |
} |
| 2994 |
|
| 2995 |
if (!(errno == ENOENT || errno == ENOTDIR || errno == ENAMETOOLONG || |
| 2996 |
errno == EACCES)) { |
| 2997 |
os_file_handle_error_no_exit(path, "file_exists_posix_stat", false); |
| 2998 |
} |
| 2999 |
|
| 3000 |
return (false); |
| 3001 |
} |
| 3002 |
|
| 3003 |
/** NOTE! Use the corresponding macro os_file_flush(), not directly this |
| 3004 |
function! |
| 3005 |
Flushes the write buffers of a given file to the disk. |
| 3006 |
@param[in] file handle to a file |
| 3007 |
@return true if success */ |
| 3008 |
bool os_file_flush_func(os_file_t file) { |
| 3009 |
int ret; |
| 3010 |
|
| 3011 |
ret = os_file_fsync_posix(file); |
| 3012 |
|
| 3013 |
if (ret == 0) { |
| 3014 |
return (true); |
| 3015 |
} |
| 3016 |
|
| 3017 |
/* Since Linux returns EINVAL if the 'file' is actually a raw device, |
| 3018 |
we choose to ignore that error if we are using raw disks */ |
| 3019 |
|
| 3020 |
if (srv_start_raw_disk_in_use && errno == EINVAL) { |
| 3021 |
return (true); |
| 3022 |
} |
| 3023 |
|
| 3024 |
ib::error(ER_IB_MSG_775) << "The OS said file flush did not succeed"; |
| 3025 |
|
| 3026 |
os_file_handle_error(nullptr, "flush"); |
| 3027 |
|
| 3028 |
/* It is a fatal error if a file flush does not succeed, because then |
| 3029 |
the database can get corrupt on disk */ |
| 3030 |
ut_error; |
| 3031 |
|
| 3032 |
return (false); |
| 3033 |
} |
| 3034 |
|
| 3035 |
/** This function attempts to create a directory named pathname. The new |
| 3036 |
directory gets default permissions. On Unix the permissions are |
| 3037 |
(0770 & ~umask). If the directory exists already, nothing is done and |
| 3038 |
the call succeeds, unless the fail_if_exists arguments is true. |
| 3039 |
If another error occurs, such as a permission error, this does not crash, |
| 3040 |
but reports the error and returns false. |
| 3041 |
@param[in] pathname directory name as null-terminated string |
| 3042 |
@param[in] fail_if_exists if true, pre-existing directory is treated as |
| 3043 |
an error. |
| 3044 |
@return true if call succeeds, false on error */ |
| 3045 |
bool os_file_create_directory(const char *pathname, bool fail_if_exists) { |
| 3046 |
int rcode = mkdir(pathname, 0770); |
| 3047 |
|
| 3048 |
if (!(rcode == 0 || (errno == EEXIST && !fail_if_exists))) { |
| 3049 |
/* failure */ |
| 3050 |
os_file_handle_error_no_exit(pathname, "mkdir", false); |
| 3051 |
|
| 3052 |
return (false); |
| 3053 |
} |
| 3054 |
|
| 3055 |
if (rcode == 0) { |
| 3056 |
os_parent_dir_fsync_posix(pathname); |
| 3057 |
} |
| 3058 |
|
| 3059 |
return (true); |
| 3060 |
} |
| 3061 |
|
| 3062 |
/** This function scans the contents of a directory and invokes the callback |
| 3063 |
for each entry. |
| 3064 |
@param[in] path directory name as null-terminated string |
| 3065 |
@param[in] scan_cbk use callback to be called for each entry |
| 3066 |
@param[in] is_drop attempt to drop the directory after scan |
| 3067 |
@return true if call succeeds, false on error */ |
| 3068 |
bool os_file_scan_directory(const char *path, os_dir_cbk_t scan_cbk, |
| 3069 |
bool is_drop) { |
| 3070 |
DIR *directory; |
| 3071 |
dirent *entry; |
| 3072 |
|
| 3073 |
directory = opendir(path); |
| 3074 |
|
| 3075 |
if (directory == nullptr) { |
| 3076 |
os_file_handle_error_no_exit(path, "opendir", false); |
| 3077 |
return (false); |
| 3078 |
} |
| 3079 |
|
| 3080 |
entry = readdir(directory); |
| 3081 |
|
| 3082 |
while (entry != nullptr) { |
| 3083 |
scan_cbk(path, entry->d_name); |
| 3084 |
entry = readdir(directory); |
| 3085 |
} |
| 3086 |
|
| 3087 |
closedir(directory); |
| 3088 |
|
| 3089 |
if (is_drop) { |
| 3090 |
int err; |
| 3091 |
err = rmdir(path); |
| 3092 |
|
| 3093 |
if (err != 0) { |
| 3094 |
os_file_handle_error_no_exit(path, "rmdir", false); |
| 3095 |
return (false); |
| 3096 |
} |
| 3097 |
} |
| 3098 |
|
| 3099 |
return (true); |
| 3100 |
} |
| 3101 |
|
| 3102 |
pfs_os_file_t os_file_create_func(const char *name, ulint create_mode, |
| 3103 |
ulint purpose, bool read_only, |
| 3104 |
bool *success) { |
| 3105 |
bool on_error_no_exit; |
| 3106 |
bool on_error_silent; |
| 3107 |
pfs_os_file_t file; |
| 3108 |
|
| 3109 |
*success = false; |
| 3110 |
|
| 3111 |
DBUG_EXECUTE_IF("ib_create_table_fail_disk_full", *success = false; |
| 3112 |
errno = ENOSPC; file.m_file = OS_FILE_CLOSED; return (file);); |
| 3113 |
|
| 3114 |
int create_flag; |
| 3115 |
const char *mode_str = nullptr; |
| 3116 |
|
| 3117 |
on_error_no_exit = create_mode & OS_FILE_ON_ERROR_NO_EXIT ? true : false; |
| 3118 |
on_error_silent = create_mode & OS_FILE_ON_ERROR_SILENT ? true : false; |
| 3119 |
|
| 3120 |
create_mode &= ~OS_FILE_ON_ERROR_NO_EXIT; |
| 3121 |
create_mode &= ~OS_FILE_ON_ERROR_SILENT; |
| 3122 |
|
| 3123 |
if (create_mode == OS_FILE_OPEN || create_mode == OS_FILE_OPEN_RAW || |
| 3124 |
create_mode == OS_FILE_OPEN_RETRY) { |
| 3125 |
mode_str = "OPEN"; |
| 3126 |
|
| 3127 |
create_flag = read_only ? O_RDONLY : O_RDWR; |
| 3128 |
|
| 3129 |
} else if (read_only) { |
| 3130 |
mode_str = "OPEN"; |
| 3131 |
|
| 3132 |
create_flag = O_RDONLY; |
| 3133 |
|
| 3134 |
} else if (create_mode == OS_FILE_CREATE) { |
| 3135 |
mode_str = "CREATE"; |
| 3136 |
create_flag = O_RDWR | O_CREAT | O_EXCL; |
| 3137 |
|
| 3138 |
} else if (create_mode == OS_FILE_CREATE_PATH) { |
| 3139 |
mode_str = "CREATE"; |
| 3140 |
/* Create subdirs along the path if needed. */ |
| 3141 |
dberr_t err; |
| 3142 |
|
| 3143 |
err = os_file_create_subdirs_if_needed(name); |
| 3144 |
|
| 3145 |
if (err != DB_SUCCESS) { |
| 3146 |
*success = false; |
| 3147 |
ib::error(ER_IB_MSG_778) |
| 3148 |
<< "Unable to create subdirectories '" << name << "'"; |
| 3149 |
|
| 3150 |
file.m_file = OS_FILE_CLOSED; |
| 3151 |
return (file); |
| 3152 |
} |
| 3153 |
|
| 3154 |
create_flag = O_RDWR | O_CREAT | O_EXCL; |
| 3155 |
create_mode = OS_FILE_CREATE; |
| 3156 |
|
| 3157 |
} else { |
| 3158 |
ib::error(ER_IB_MSG_779) |
| 3159 |
<< "Unknown file create mode (" << create_mode << ")" |
| 3160 |
<< " for file '" << name << "'"; |
| 3161 |
|
| 3162 |
file.m_file = OS_FILE_CLOSED; |
| 3163 |
return (file); |
| 3164 |
} |
| 3165 |
|
| 3166 |
ut_a(purpose == OS_LOG_FILE || purpose == OS_LOG_FILE_RESIZING || |
| 3167 |
purpose == OS_DATA_FILE || purpose == OS_DBLWR_FILE || |
| 3168 |
purpose == OS_CLONE_DATA_FILE || purpose == OS_CLONE_LOG_FILE || |
| 3169 |
purpose == OS_BUFFERED_FILE || OS_DATA_FILE_FOR_SPACE_ID_READ); |
| 3170 |
|
| 3171 |
#ifdef O_SYNC |
| 3172 |
/* We let O_SYNC only affect log files; note that we map O_DSYNC to |
| 3173 |
O_SYNC because the datasync options seemed to corrupt files in 2001 |
| 3174 |
in both Linux and Solaris */ |
| 3175 |
|
| 3176 |
if (!read_only && purpose == OS_LOG_FILE && |
| 3177 |
srv_unix_file_flush_method == SRV_UNIX_O_DSYNC) { |
| 3178 |
create_flag |= O_SYNC; |
| 3179 |
} |
| 3180 |
#endif /* O_SYNC */ |
| 3181 |
|
| 3182 |
bool retry; |
| 3183 |
|
| 3184 |
do { |
| 3185 |
file.m_file = ::open(name, create_flag, os_innodb_umask); |
| 3186 |
|
| 3187 |
if (file.m_file == -1) { |
| 3188 |
const char *operation; |
| 3189 |
|
| 3190 |
operation = |
| 3191 |
(create_mode == OS_FILE_CREATE && !read_only) ? "create" : "open"; |
| 3192 |
|
| 3193 |
*success = false; |
| 3194 |
|
| 3195 |
if (on_error_no_exit) { |
| 3196 |
retry = os_file_handle_error_no_exit(name, operation, on_error_silent); |
| 3197 |
} else { |
| 3198 |
retry = os_file_handle_error(name, operation); |
| 3199 |
} |
| 3200 |
} else { |
| 3201 |
*success = true; |
| 3202 |
retry = false; |
| 3203 |
} |
| 3204 |
|
| 3205 |
} while (retry); |
| 3206 |
|
| 3207 |
if (!*success) { |
| 3208 |
ut_a(file.m_file == OS_FILE_CLOSED); |
| 3209 |
return file; |
| 3210 |
} |
| 3211 |
|
| 3212 |
bool use_odirect = false; |
| 3213 |
|
| 3214 |
if ((!read_only || purpose == OS_CLONE_DATA_FILE) && |
| 3215 |
(purpose == OS_DATA_FILE || purpose == OS_CLONE_DATA_FILE || |
| 3216 |
purpose == OS_DBLWR_FILE) && |
| 3217 |
(srv_unix_file_flush_method == SRV_UNIX_O_DIRECT || |
| 3218 |
srv_unix_file_flush_method == SRV_UNIX_O_DIRECT_NO_FSYNC)) { |
| 3219 |
use_odirect = true; |
| 3220 |
} |
| 3221 |
|
| 3222 |
/* We also set O_DIRECT for files which are opened at startup to |
| 3223 |
read their space ID from the page header */ |
| 3224 |
if (purpose == OS_DATA_FILE_FOR_SPACE_ID_READ) { |
| 3225 |
use_odirect = true; |
| 3226 |
} |
| 3227 |
|
| 3228 |
if (use_odirect) { |
| 3229 |
/* Flag for executing os_file_set_nocache() without printing error |
| 3230 |
messages */ |
| 3231 |
const bool on_error_silent_for_nocache = |
| 3232 |
(purpose == OS_DATA_FILE_FOR_SPACE_ID_READ); |
| 3233 |
os_file_set_nocache(file.m_file, name, mode_str, |
| 3234 |
on_error_silent_for_nocache); |
| 3235 |
} |
| 3236 |
|
| 3237 |
#ifdef USE_FILE_LOCK |
| 3238 |
if (!read_only && create_mode != OS_FILE_OPEN_RAW && |
| 3239 |
/* Don't acquire file lock while cloning files. */ |
| 3240 |
purpose != OS_CLONE_DATA_FILE && purpose != OS_CLONE_LOG_FILE && |
| 3241 |
os_file_lock(file.m_file, name)) { |
| 3242 |
if (create_mode == OS_FILE_OPEN_RETRY) { |
| 3243 |
ib::info(ER_IB_MSG_780) << "Retrying to lock the first data file"; |
| 3244 |
|
| 3245 |
for (int i = 0; i < 100; i++) { |
| 3246 |
std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 3247 |
|
| 3248 |
if (!os_file_lock(file.m_file, name)) { |
| 3249 |
*success = true; |
| 3250 |
return (file); |
| 3251 |
} |
| 3252 |
} |
| 3253 |
|
| 3254 |
ib::info(ER_IB_MSG_781) << "Unable to open the first data file"; |
| 3255 |
} |
| 3256 |
|
| 3257 |
*success = false; |
| 3258 |
close(file.m_file); |
| 3259 |
file.m_file = -1; |
| 3260 |
} |
| 3261 |
#endif /* USE_FILE_LOCK */ |
| 3262 |
|
| 3263 |
if (*success && (create_flag & O_CREAT) != 0) { |
| 3264 |
os_parent_dir_fsync_posix(name); |
| 3265 |
} |
| 3266 |
|
| 3267 |
return (file); |
| 3268 |
} |
| 3269 |
|
| 3270 |
pfs_os_file_t os_file_create_simple_no_error_handling_func( |
| 3271 |
const char *name, ulint create_mode, ulint access_type, bool read_only, |
| 3272 |
mode_t umask, bool *success) { |
| 3273 |
pfs_os_file_t file; |
| 3274 |
int create_flag; |
| 3275 |
|
| 3276 |
if (umask == os_innodb_umask_default) { |
| 3277 |
umask = os_innodb_umask; |
| 3278 |
} |
| 3279 |
|
| 3280 |
ut_a(!(create_mode & OS_FILE_ON_ERROR_SILENT)); |
| 3281 |
ut_a(!(create_mode & OS_FILE_ON_ERROR_NO_EXIT)); |
| 3282 |
|
| 3283 |
*success = false; |
| 3284 |
|
| 3285 |
if (create_mode == OS_FILE_OPEN) { |
| 3286 |
if (access_type == OS_FILE_READ_ONLY) { |
| 3287 |
create_flag = O_RDONLY; |
| 3288 |
|
| 3289 |
} else if (read_only) { |
| 3290 |
create_flag = O_RDONLY; |
| 3291 |
|
| 3292 |
} else { |
| 3293 |
ut_a(access_type == OS_FILE_READ_WRITE || |
| 3294 |
access_type == OS_FILE_READ_ALLOW_DELETE); |
| 3295 |
|
| 3296 |
create_flag = O_RDWR; |
| 3297 |
} |
| 3298 |
|
| 3299 |
} else if (read_only) { |
| 3300 |
create_flag = O_RDONLY; |
| 3301 |
|
| 3302 |
} else if (create_mode == OS_FILE_CREATE) { |
| 3303 |
create_flag = O_RDWR | O_CREAT | O_EXCL; |
| 3304 |
|
| 3305 |
} else { |
| 3306 |
ib::error(ER_IB_MSG_782) << "Unknown file create mode " << create_mode |
| 3307 |
<< " for file '" << name << "'"; |
| 3308 |
file.m_file = OS_FILE_CLOSED; |
| 3309 |
return (file); |
| 3310 |
} |
| 3311 |
|
| 3312 |
file.m_file = ::open(name, create_flag, umask); |
| 3313 |
|
| 3314 |
*success = (file.m_file != -1); |
| 3315 |
|
| 3316 |
#ifdef USE_FILE_LOCK |
| 3317 |
if (!read_only && *success && access_type == OS_FILE_READ_WRITE && |
| 3318 |
os_file_lock(file.m_file, name)) { |
| 3319 |
*success = false; |
| 3320 |
close(file.m_file); |
| 3321 |
file.m_file = -1; |
| 3322 |
} |
| 3323 |
#endif /* USE_FILE_LOCK */ |
| 3324 |
|
| 3325 |
if (*success && create_mode == OS_FILE_CREATE) { |
| 3326 |
os_parent_dir_fsync_posix(name); |
| 3327 |
} |
| 3328 |
|
| 3329 |
return (file); |
| 3330 |
} |
| 3331 |
|
| 3332 |
/** Deletes a file if it exists. The file has to be closed before calling this. |
| 3333 |
@param[in] name file path as a null-terminated string |
| 3334 |
@param[out] exist indicate if file pre-exist |
| 3335 |
@return true if success */ |
| 3336 |
bool os_file_delete_if_exists_func(const char *name, bool *exist) { |
| 3337 |
if (Fil_path::get_file_type(name) == OS_FILE_TYPE_MISSING) { |
| 3338 |
if (exist != nullptr) { |
| 3339 |
*exist = false; |
| 3340 |
} |
| 3341 |
return true; |
| 3342 |
} |
| 3343 |
|
| 3344 |
if (!os_file_can_delete(name)) { |
| 3345 |
return (false); |
| 3346 |
} |
| 3347 |
|
| 3348 |
if (exist != nullptr) { |
| 3349 |
*exist = true; |
| 3350 |
} |
| 3351 |
|
| 3352 |
if (unlink(name) != 0) { // couldn't unlink |
| 3353 |
if (errno == ENOENT) { // that's because file was missing, not an error |
| 3354 |
if (exist != nullptr) { |
| 3355 |
*exist = false; |
| 3356 |
} |
| 3357 |
return true; |
| 3358 |
} |
| 3359 |
// it's some other problem, report it, but don't crash |
| 3360 |
os_file_handle_error_no_exit(name, "delete", false); |
| 3361 |
return false; |
| 3362 |
} |
| 3363 |
|
| 3364 |
// persist the change of the directory's content |
| 3365 |
os_parent_dir_fsync_posix(name); |
| 3366 |
return true; |
| 3367 |
} |
| 3368 |
|
| 3369 |
/** Deletes a file. The file has to be closed before calling this. |
| 3370 |
@param[in] name file path as a null-terminated string |
| 3371 |
@return true if success */ |
| 3372 |
bool os_file_delete_func(const char *name) { |
| 3373 |
int ret = unlink(name); |
| 3374 |
|
| 3375 |
if (ret != 0) { |
| 3376 |
os_file_handle_error_no_exit(name, "delete", false); |
| 3377 |
|
| 3378 |
return (false); |
| 3379 |
} |
| 3380 |
|
| 3381 |
os_parent_dir_fsync_posix(name); |
| 3382 |
|
| 3383 |
return (true); |
| 3384 |
} |
| 3385 |
|
| 3386 |
/** NOTE! Use the corresponding macro os_file_rename(), not directly this |
| 3387 |
function! |
| 3388 |
Renames a file (can also move it to another directory). It is safest that the |
| 3389 |
file is closed before calling this function. |
| 3390 |
@param[in] oldpath old file path as a null-terminated string |
| 3391 |
@param[in] newpath new file path |
| 3392 |
@return true if success */ |
| 3393 |
bool os_file_rename_func(const char *oldpath, const char *newpath) { |
| 3394 |
#ifdef UNIV_DEBUG |
| 3395 |
/* New path must be valid but not exist. */ |
| 3396 |
os_file_type_t type; |
| 3397 |
bool exists; |
| 3398 |
ut_ad(os_file_status(newpath, &exists, &type)); |
| 3399 |
ut_ad(!exists); |
| 3400 |
|
| 3401 |
/* Old path must exist. */ |
| 3402 |
ut_ad(os_file_exists(oldpath)); |
| 3403 |
#endif /* UNIV_DEBUG */ |
| 3404 |
|
| 3405 |
int ret = rename(oldpath, newpath); |
| 3406 |
|
| 3407 |
if (ret != 0) { |
| 3408 |
os_file_handle_error_no_exit(oldpath, "rename", false); |
| 3409 |
|
| 3410 |
return (false); |
| 3411 |
} |
| 3412 |
|
| 3413 |
os_parent_dir_fsync_posix(newpath); |
| 3414 |
|
| 3415 |
return (true); |
| 3416 |
} |
| 3417 |
|
| 3418 |
/** NOTE! Use the corresponding macro os_file_close(), not directly |
| 3419 |
this function! |
| 3420 |
Closes a file handle. In case of error, error number can be retrieved with |
| 3421 |
os_file_get_last_error. |
| 3422 |
@param[in] file Handle to a file |
| 3423 |
@return true if success */ |
| 3424 |
bool os_file_close_func(os_file_t file) { |
| 3425 |
int ret = close(file); |
| 3426 |
|
| 3427 |
if (ret == -1) { |
| 3428 |
os_file_handle_error(nullptr, "close"); |
| 3429 |
|
| 3430 |
return (false); |
| 3431 |
} |
| 3432 |
|
| 3433 |
return (true); |
| 3434 |
} |
| 3435 |
|
| 3436 |
/** Gets a file size. |
| 3437 |
@param[in] file Handle to a file |
| 3438 |
@return file size, or (os_offset_t) -1 on failure */ |
| 3439 |
os_offset_t os_file_get_size(pfs_os_file_t file) { |
| 3440 |
/* Store current position */ |
| 3441 |
os_offset_t pos = lseek(file.m_file, 0, SEEK_CUR); |
| 3442 |
os_offset_t file_size = lseek(file.m_file, 0, SEEK_END); |
| 3443 |
/* Restore current position as the function should not change it */ |
| 3444 |
lseek(file.m_file, pos, SEEK_SET); |
| 3445 |
return (file_size); |
| 3446 |
} |
| 3447 |
|
| 3448 |
os_file_size_t os_file_get_size(const char *filename) { |
| 3449 |
struct stat s; |
| 3450 |
os_file_size_t file_size; |
| 3451 |
|
| 3452 |
int ret = stat(filename, &s); |
| 3453 |
|
| 3454 |
if (ret == 0) { |
| 3455 |
file_size.m_total_size = s.st_size; |
| 3456 |
/* st_blocks is in 512 byte sized blocks */ |
| 3457 |
file_size.m_alloc_size = s.st_blocks * 512; |
| 3458 |
} else { |
| 3459 |
file_size.m_total_size = ~0; |
| 3460 |
file_size.m_alloc_size = (os_offset_t)errno; |
| 3461 |
} |
| 3462 |
|
| 3463 |
return (file_size); |
| 3464 |
} |
| 3465 |
|
| 3466 |
/** Get available free space on disk |
| 3467 |
@param[in] path pathname of a directory or file in disk |
| 3468 |
@param[out] free_space free space available in bytes |
| 3469 |
@return DB_SUCCESS if all OK */ |
| 3470 |
static dberr_t os_get_free_space_posix(const char *path, uint64_t &free_space) { |
| 3471 |
struct statvfs stat; |
| 3472 |
auto ret = statvfs(path, &stat); |
| 3473 |
|
| 3474 |
if (ret && (errno == ENOENT || errno == ENOTDIR)) { |
| 3475 |
/* file or directory does not exist */ |
| 3476 |
return (DB_NOT_FOUND); |
| 3477 |
|
| 3478 |
} else if (ret) { |
| 3479 |
/* file exists, but stat call failed */ |
| 3480 |
os_file_handle_error_no_exit(path, "statvfs", false); |
| 3481 |
return (DB_FAIL); |
| 3482 |
} |
| 3483 |
|
| 3484 |
free_space = stat.f_bsize; |
| 3485 |
free_space *= stat.f_bavail; |
| 3486 |
return (DB_SUCCESS); |
| 3487 |
} |
| 3488 |
|
| 3489 |
/** This function returns information about the specified file |
| 3490 |
@param[in] path pathname of the file |
| 3491 |
@param[out] stat_info information of a file in a directory |
| 3492 |
@param[in,out] statinfo information of a file in a directory |
| 3493 |
@param[in] check_rw_perm for testing whether the file can be opened |
| 3494 |
in RW mode |
| 3495 |
@param[in] read_only if true read only mode checks are enforced |
| 3496 |
@return DB_SUCCESS if all OK */ |
| 3497 |
static dberr_t os_file_get_status_posix(const char *path, |
| 3498 |
os_file_stat_t *stat_info, |
| 3499 |
struct stat *statinfo, |
| 3500 |
bool check_rw_perm, bool read_only) { |
| 3501 |
int ret = stat(path, statinfo); |
| 3502 |
|
| 3503 |
if (ret && (errno == ENOENT || errno == ENOTDIR)) { |
| 3504 |
/* file does not exist */ |
| 3505 |
|
| 3506 |
return (DB_NOT_FOUND); |
| 3507 |
|
| 3508 |
} else if (ret) { |
| 3509 |
/* file exists, but stat call failed */ |
| 3510 |
|
| 3511 |
os_file_handle_error_no_exit(path, "stat", false); |
| 3512 |
|
| 3513 |
return (DB_FAIL); |
| 3514 |
} |
| 3515 |
|
| 3516 |
switch (statinfo->st_mode & S_IFMT) { |
| 3517 |
case S_IFDIR: |
| 3518 |
stat_info->type = OS_FILE_TYPE_DIR; |
| 3519 |
break; |
| 3520 |
case S_IFLNK: |
| 3521 |
stat_info->type = OS_FILE_TYPE_LINK; |
| 3522 |
break; |
| 3523 |
case S_IFBLK: |
| 3524 |
/* Handle block device as regular file. */ |
| 3525 |
case S_IFCHR: |
| 3526 |
/* Handle character device as regular file. */ |
| 3527 |
case S_IFREG: |
| 3528 |
stat_info->type = OS_FILE_TYPE_FILE; |
| 3529 |
break; |
| 3530 |
default: |
| 3531 |
stat_info->type = OS_FILE_TYPE_UNKNOWN; |
| 3532 |
} |
| 3533 |
|
| 3534 |
stat_info->size = statinfo->st_size; |
| 3535 |
stat_info->block_size = statinfo->st_blksize; |
| 3536 |
stat_info->alloc_size = statinfo->st_blocks * 512; |
| 3537 |
|
| 3538 |
if (check_rw_perm && (stat_info->type == OS_FILE_TYPE_FILE || |
| 3539 |
stat_info->type == OS_FILE_TYPE_BLOCK)) { |
| 3540 |
int access = !read_only ? O_RDWR : O_RDONLY; |
| 3541 |
int fh = ::open(path, access, os_innodb_umask); |
| 3542 |
|
| 3543 |
if (fh == -1) { |
| 3544 |
stat_info->rw_perm = false; |
| 3545 |
} else { |
| 3546 |
stat_info->rw_perm = true; |
| 3547 |
close(fh); |
| 3548 |
} |
| 3549 |
} |
| 3550 |
|
| 3551 |
return (DB_SUCCESS); |
| 3552 |
} |
| 3553 |
|
| 3554 |
/** Truncates a file to a specified size in bytes. |
| 3555 |
Do nothing if the size to preserve is greater or equal to the current |
| 3556 |
size of the file. |
| 3557 |
@param[in] pathname file path |
| 3558 |
@param[in] file file to be truncated |
| 3559 |
@param[in] size size to preserve in bytes |
| 3560 |
@return true if success */ |
| 3561 |
static bool os_file_truncate_posix(const char *pathname, pfs_os_file_t file, |
| 3562 |
os_offset_t size) { |
| 3563 |
int res = ftruncate(file.m_file, size); |
| 3564 |
if (res == -1) { |
| 3565 |
bool retry; |
| 3566 |
|
| 3567 |
retry = os_file_handle_error_no_exit(pathname, "truncate", false); |
| 3568 |
|
| 3569 |
if (retry) { |
| 3570 |
ib::warn(ER_IB_MSG_783) << "Truncate failed for '" << pathname << "'"; |
| 3571 |
} |
| 3572 |
} |
| 3573 |
|
| 3574 |
return (res == 0); |
| 3575 |
} |
| 3576 |
|
| 3577 |
/** Truncates a file at its current position. |
| 3578 |
@return true if success */ |
| 3579 |
bool os_file_set_eof(FILE *file) /*!< in: file to be truncated */ |
| 3580 |
{ |
| 3581 |
return (!ftruncate(fileno(file), ftell(file))); |
| 3582 |
} |
| 3583 |
|
| 3584 |
#ifdef UNIV_HOTBACKUP |
| 3585 |
/** Closes a file handle. |
| 3586 |
@param[in] file Handle to a file |
| 3587 |
@return true if success */ |
| 3588 |
bool os_file_close_no_error_handling(os_file_t file) { |
| 3589 |
return (close(file) != -1); |
| 3590 |
} |
| 3591 |
#endif /* UNIV_HOTBACKUP */ |
| 3592 |
|
| 3593 |
/** This function can be called if one wants to post a batch of reads and |
| 3594 |
prefers an i/o-handler thread to handle them all at once later. You must |
| 3595 |
call os_aio_simulated_wake_handler_threads later to ensure the threads |
| 3596 |
are not left sleeping! */ |
| 3597 |
void os_aio_simulated_put_read_threads_to_sleep() { /* No op on non Windows */ |
| 3598 |
} |
| 3599 |
|
| 3600 |
/** Depth first traversal of the directory starting from basedir |
| 3601 |
@param[in] basedir Start scanning from this directory |
| 3602 |
@param[in] recursive `true` if scan should be recursive |
| 3603 |
@param[in] f Function to call for each entry */ |
| 3604 |
void Dir_Walker::walk_posix(const Path &basedir, bool recursive, Function &&f) { |
| 3605 |
using Stack = std::stack<Entry>; |
| 3606 |
|
| 3607 |
Stack directories; |
| 3608 |
|
| 3609 |
directories.push(Entry(basedir, 0)); |
| 3610 |
|
| 3611 |
while (!directories.empty()) { |
| 3612 |
Entry current = directories.top(); |
| 3613 |
|
| 3614 |
directories.pop(); |
| 3615 |
|
| 3616 |
/* Ignore hidden directories and files. */ |
| 3617 |
if (Fil_path::is_hidden(current.m_path)) { |
| 3618 |
ib::info(ER_IB_MSG_SKIP_HIDDEN_DIR, current.m_path.c_str()); |
| 3619 |
continue; |
| 3620 |
} |
| 3621 |
|
| 3622 |
DIR *parent = opendir(current.m_path.c_str()); |
| 3623 |
|
| 3624 |
if (parent == nullptr) { |
| 3625 |
ib::info(ER_IB_MSG_784) << "Failed to walk directory" |
| 3626 |
<< " '" << current.m_path << "'"; |
| 3627 |
|
| 3628 |
continue; |
| 3629 |
} |
| 3630 |
|
| 3631 |
if (!is_directory(current.m_path)) { |
| 3632 |
f(current.m_path, current.m_depth); |
| 3633 |
} |
| 3634 |
|
| 3635 |
struct dirent *dirent = nullptr; |
| 3636 |
|
| 3637 |
for (;;) { |
| 3638 |
dirent = readdir(parent); |
| 3639 |
|
| 3640 |
if (dirent == nullptr) { |
| 3641 |
break; |
| 3642 |
} |
| 3643 |
|
| 3644 |
if (strcmp(dirent->d_name, ".") == 0 || |
| 3645 |
strcmp(dirent->d_name, "..") == 0) { |
| 3646 |
continue; |
| 3647 |
} |
| 3648 |
|
| 3649 |
Path path(current.m_path); |
| 3650 |
|
| 3651 |
if (path.back() != '/' && path.back() != '\\') { |
| 3652 |
path += OS_PATH_SEPARATOR; |
| 3653 |
} |
| 3654 |
|
| 3655 |
path.append(dirent->d_name); |
| 3656 |
|
| 3657 |
/* Ignore hidden subdirectories and files. */ |
| 3658 |
if (Fil_path::is_hidden(path)) { |
| 3659 |
ib::info(ER_IB_MSG_SKIP_HIDDEN_DIR, path.c_str()); |
| 3660 |
continue; |
| 3661 |
} |
| 3662 |
|
| 3663 |
if (is_directory(path) && recursive) { |
| 3664 |
directories.push(Entry(path, current.m_depth + 1)); |
| 3665 |
} else { |
| 3666 |
f(path, current.m_depth + 1); |
| 3667 |
} |
| 3668 |
} |
| 3669 |
|
| 3670 |
closedir(parent); |
| 3671 |
} |
| 3672 |
} |
| 3673 |
|
| 3674 |
#else /* _WIN32 */ |
| 3675 |
|
| 3676 |
#include <WinIoCtl.h> |
| 3677 |
|
| 3678 |
/** Do the read/write |
| 3679 |
@param[in] request The IO context and type |
| 3680 |
@return the number of bytes read/written or negative value on error */ |
| 3681 |
ssize_t SyncFileIO::execute(const IORequest &request) { |
| 3682 |
OVERLAPPED overlapped{}; |
| 3683 |
|
| 3684 |
/* We need a fresh, not shared instance of Event for the OVERLAPPED structure. |
| 3685 |
Both are stopped being used at most at the end of this method, as we wait for |
| 3686 |
the result with GetOverlappedResult. Otherwise the kernel would be modifying |
| 3687 |
the structure after we leave this method and if the overlapped struct was |
| 3688 |
allocated on stack, it would corrupt the stack. |
| 3689 |
To not create a fresh Event each time this method is called, we will use a |
| 3690 |
static one, that is initialized once on first usage and is destroyed at latest |
| 3691 |
at program exit. |
| 3692 |
To make it not being used concurrently, we make it thread_local (which implies |
| 3693 |
static) - this way each invocation will have its own Event not used by anyone |
| 3694 |
else. The event will be destroyed at thread exit. */ |
| 3695 |
thread_local Scoped_event local_event; |
| 3696 |
|
| 3697 |
overlapped.hEvent = local_event.get_handle(); |
| 3698 |
overlapped.Offset = (DWORD)m_offset & 0xFFFFFFFF; |
| 3699 |
overlapped.OffsetHigh = (DWORD)(m_offset >> 32); |
| 3700 |
|
| 3701 |
ut_a(overlapped.hEvent != NULL); |
| 3702 |
|
| 3703 |
BOOL result; |
| 3704 |
DWORD n_bytes_transfered = 0; |
| 3705 |
DWORD n_bytes_transfered_sync; |
| 3706 |
|
| 3707 |
if (request.is_read()) { |
| 3708 |
result = ReadFile(m_fh, m_buf, static_cast<DWORD>(m_n), |
| 3709 |
&n_bytes_transfered_sync, &overlapped); |
| 3710 |
|
| 3711 |
} else { |
| 3712 |
ut_ad(request.is_write()); |
| 3713 |
result = WriteFile(m_fh, m_buf, static_cast<DWORD>(m_n), |
| 3714 |
&n_bytes_transfered_sync, &overlapped); |
| 3715 |
} |
| 3716 |
|
| 3717 |
if (!result) { |
| 3718 |
if (GetLastError() == ERROR_IO_PENDING) { |
| 3719 |
result = |
| 3720 |
GetOverlappedResult(m_fh, &overlapped, &n_bytes_transfered, true); |
| 3721 |
} |
| 3722 |
} else { |
| 3723 |
/* The IO was executed synchronously (this can happen even for files opened |
| 3724 |
for async operations). The value returned in pointer to ReadFile/WriteFile |
| 3725 |
has the correct number of bytes transferred. This fact for the files opened |
| 3726 |
for async operations is not in the documentation, but it is showed in |
| 3727 |
example usage and notes on |
| 3728 |
https://docs.microsoft.com/en-US/troubleshoot/windows/win32/asynchronous-disk-io-synchronous |
| 3729 |
*/ |
| 3730 |
n_bytes_transfered = n_bytes_transfered_sync; |
| 3731 |
} |
| 3732 |
return (result ? static_cast<ssize_t>(n_bytes_transfered) : -1); |
| 3733 |
} |
| 3734 |
|
| 3735 |
/** Free storage space associated with a section of the file. |
| 3736 |
@param[in] fh Open file handle |
| 3737 |
@param[in] page_size Tablespace page size |
| 3738 |
@param[in] block_size File system block size |
| 3739 |
@param[in] off Starting offset (SEEK_SET) |
| 3740 |
@param[in] len Size of the hole |
| 3741 |
@return 0 on success or errno */ |
| 3742 |
static dberr_t os_file_punch_hole_win32(os_file_t fh, os_offset_t off, |
| 3743 |
os_offset_t len) { |
| 3744 |
FILE_ZERO_DATA_INFORMATION punch; |
| 3745 |
|
| 3746 |
punch.FileOffset.QuadPart = off; |
| 3747 |
punch.BeyondFinalZero.QuadPart = off + len; |
| 3748 |
|
| 3749 |
/* We need a fresh, not shared instance of Event for the OVERLAPPED structure. |
| 3750 |
Both are stopped being used at most at the end of this method, as we wait for |
| 3751 |
the result with GetOverlappedResult. Otherwise the kernel would be modifying |
| 3752 |
the structure after we leave this method and if the overlapped struct was |
| 3753 |
allocated on stack, it would corrupt the stack. |
| 3754 |
To not create a fresh Event each time this method is called, we will use a |
| 3755 |
static one, that is initialized once on first usage and is destroyed at latest |
| 3756 |
at program exit. |
| 3757 |
To make it not being used concurrently, we make it thread_local (which implies |
| 3758 |
static) - this way each invocation will have its own Event not used by anyone |
| 3759 |
else. The event will be destroyed at thread exit. */ |
| 3760 |
thread_local Scoped_event local_event; |
| 3761 |
|
| 3762 |
OVERLAPPED overlapped{}; |
| 3763 |
overlapped.hEvent = local_event.get_handle(); |
| 3764 |
|
| 3765 |
ut_a(overlapped.hEvent != NULL); |
| 3766 |
|
| 3767 |
DWORD temp; |
| 3768 |
|
| 3769 |
BOOL result = DeviceIoControl(fh, FSCTL_SET_ZERO_DATA, &punch, sizeof(punch), |
| 3770 |
NULL, 0, &temp, &overlapped); |
| 3771 |
|
| 3772 |
if (!result) { |
| 3773 |
if (GetLastError() == ERROR_IO_PENDING) { |
| 3774 |
result = GetOverlappedResult(fh, &overlapped, &temp, true); |
| 3775 |
} |
| 3776 |
} |
| 3777 |
|
| 3778 |
return (!result ? DB_IO_NO_PUNCH_HOLE : DB_SUCCESS); |
| 3779 |
} |
| 3780 |
|
| 3781 |
/** Check the existence and type of a given path. |
| 3782 |
@param[in] path pathname of the file |
| 3783 |
@param[out] exists true if file exists |
| 3784 |
@param[out] type type of the file (if it exists) |
| 3785 |
@return true if call succeeded */ |
| 3786 |
static bool os_file_status_win32(const char *path, bool *exists, |
| 3787 |
os_file_type_t *type) { |
| 3788 |
struct _stat64 statinfo; |
| 3789 |
|
| 3790 |
int ret = _stat64(path, &statinfo); |
| 3791 |
|
| 3792 |
if (exists != nullptr) { |
| 3793 |
*exists = !ret; |
| 3794 |
} |
| 3795 |
|
| 3796 |
if (ret == 0) { |
| 3797 |
/* file exists, everything OK */ |
| 3798 |
|
| 3799 |
} else if (errno == ENOENT || errno == ENOTDIR) { |
| 3800 |
*type = OS_FILE_TYPE_MISSING; |
| 3801 |
|
| 3802 |
/* file does not exist */ |
| 3803 |
|
| 3804 |
if (exists != nullptr) { |
| 3805 |
*exists = false; |
| 3806 |
} |
| 3807 |
|
| 3808 |
return (true); |
| 3809 |
|
| 3810 |
} else if (errno == EACCES) { |
| 3811 |
*type = OS_FILE_PERMISSION_ERROR; |
| 3812 |
return (false); |
| 3813 |
|
| 3814 |
} else { |
| 3815 |
*type = OS_FILE_TYPE_FAILED; |
| 3816 |
|
| 3817 |
/* The _stat64() call failed with some other error */ |
| 3818 |
os_file_handle_error_no_exit(path, "file_status_win_stat64", false); |
| 3819 |
return (false); |
| 3820 |
} |
| 3821 |
|
| 3822 |
if (exists != nullptr) { |
| 3823 |
*exists = true; |
| 3824 |
} |
| 3825 |
|
| 3826 |
if (_S_IFDIR & statinfo.st_mode) { |
| 3827 |
*type = OS_FILE_TYPE_DIR; |
| 3828 |
|
| 3829 |
} else if (_S_IFREG & statinfo.st_mode) { |
| 3830 |
*type = OS_FILE_TYPE_FILE; |
| 3831 |
|
| 3832 |
} else { |
| 3833 |
*type = OS_FILE_TYPE_UNKNOWN; |
| 3834 |
} |
| 3835 |
|
| 3836 |
return (true); |
| 3837 |
} |
| 3838 |
|
| 3839 |
/** Check the existence and usefulness of a given path. |
| 3840 |
@param[in] path path name |
| 3841 |
@retval true if the path exists and can be used |
| 3842 |
@retval false if the path does not exist or if the path is |
| 3843 |
unusable to get to a possibly existing file or directory. */ |
| 3844 |
static bool os_file_exists_win32(const char *path) { |
| 3845 |
struct _stat64 statinfo; |
| 3846 |
|
| 3847 |
int ret = _stat64(path, &statinfo); |
| 3848 |
|
| 3849 |
if (ret == 0) { |
| 3850 |
return (true); |
| 3851 |
} |
| 3852 |
|
| 3853 |
if (!(errno == ENOENT || errno == EINVAL || errno == EACCES)) { |
| 3854 |
/* The _stat64() call failed with an unknown error */ |
| 3855 |
os_file_handle_error_no_exit(path, "file_exists_win_stat64", false); |
| 3856 |
} |
| 3857 |
|
| 3858 |
return (false); |
| 3859 |
} |
| 3860 |
|
| 3861 |
/** NOTE! Use the corresponding macro os_file_flush(), not directly this |
| 3862 |
function! |
| 3863 |
Flushes the write buffers of a given file to the disk. |
| 3864 |
@param[in] file handle to a file |
| 3865 |
@return true if success */ |
| 3866 |
bool os_file_flush_func(os_file_t file) { |
| 3867 |
++os_n_fsyncs; |
| 3868 |
|
| 3869 |
BOOL ret = FlushFileBuffers(file); |
| 3870 |
|
| 3871 |
if (ret) { |
| 3872 |
return (true); |
| 3873 |
} |
| 3874 |
|
| 3875 |
/* Since Windows returns ERROR_INVALID_FUNCTION if the 'file' is |
| 3876 |
actually a raw device, we choose to ignore that error if we are using |
| 3877 |
raw disks */ |
| 3878 |
|
| 3879 |
if (srv_start_raw_disk_in_use && GetLastError() == ERROR_INVALID_FUNCTION) { |
| 3880 |
return (true); |
| 3881 |
} |
| 3882 |
|
| 3883 |
os_file_handle_error(NULL, "flush"); |
| 3884 |
|
| 3885 |
/* It is a fatal error if a file flush does not succeed, because then |
| 3886 |
the database can get corrupt on disk */ |
| 3887 |
ut_error; |
| 3888 |
} |
| 3889 |
|
| 3890 |
/** Retrieves the last error number if an error occurs in a file io function. |
| 3891 |
The number should be retrieved before any other OS calls (because they may |
| 3892 |
overwrite the error number). If the number is not known to this program, |
| 3893 |
the OS error number + 100 is returned. |
| 3894 |
@param[in] report_all_errors true if we want an error message printed |
| 3895 |
of all errors |
| 3896 |
@param[in] on_error_silent true then don't print any diagnostic |
| 3897 |
to the log |
| 3898 |
@return error number, or OS error number + 100 */ |
| 3899 |
static ulint os_file_get_last_error_low(bool report_all_errors, |
| 3900 |
bool on_error_silent) { |
| 3901 |
ulint err = (ulint)GetLastError(); |
| 3902 |
|
| 3903 |
if (err == ERROR_SUCCESS) { |
| 3904 |
return 0; |
| 3905 |
} |
| 3906 |
|
| 3907 |
if (report_all_errors || (!on_error_silent && err != ERROR_DISK_FULL && |
| 3908 |
err != ERROR_FILE_EXISTS)) { |
| 3909 |
if (err == ERROR_OPERATION_ABORTED) { |
| 3910 |
ib::info(ER_IB_MSG_786) |
| 3911 |
<< "Operating system error number " << err << " in a file operation."; |
| 3912 |
} else { |
| 3913 |
ib::error(ER_IB_MSG_786) |
| 3914 |
<< "Operating system error number " << err << " in a file operation."; |
| 3915 |
} |
| 3916 |
|
| 3917 |
if (err == ERROR_PATH_NOT_FOUND) { |
| 3918 |
ib::error(ER_IB_MSG_787) << "The error means the system cannot find" |
| 3919 |
" the path specified. It might be too long" |
| 3920 |
" or it might not exist."; |
| 3921 |
|
| 3922 |
#ifndef UNIV_HOTBACKUP |
| 3923 |
if (srv_is_being_started) { |
| 3924 |
ib::error(ER_IB_MSG_788) << "If you are installing InnoDB," |
| 3925 |
" remember that you must create" |
| 3926 |
" directories yourself, InnoDB" |
| 3927 |
" does not create them."; |
| 3928 |
} |
| 3929 |
#endif /* !UNIV_HOTBACKUP */ |
| 3930 |
|
| 3931 |
} else if (err == ERROR_ACCESS_DENIED) { |
| 3932 |
ib::error(ER_IB_MSG_789) << "The error means mysqld does not have" |
| 3933 |
" the access rights to" |
| 3934 |
" the directory. It may also be" |
| 3935 |
" you have created a subdirectory" |
| 3936 |
" of the same name as a data file."; |
| 3937 |
|
| 3938 |
} else if (err == ERROR_SHARING_VIOLATION || err == ERROR_LOCK_VIOLATION) { |
| 3939 |
ib::error(ER_IB_MSG_790) << "The error means that another program" |
| 3940 |
" is using InnoDB's files." |
| 3941 |
" This might be a backup or antivirus" |
| 3942 |
" software or another instance" |
| 3943 |
" of MySQL." |
| 3944 |
" Please close it to get rid of this error."; |
| 3945 |
|
| 3946 |
} else if (err == ERROR_WORKING_SET_QUOTA || |
| 3947 |
err == ERROR_NO_SYSTEM_RESOURCES) { |
| 3948 |
ib::error(ER_IB_MSG_791) << "The error means that there are no" |
| 3949 |
" sufficient system resources or quota to" |
| 3950 |
" complete the operation."; |
| 3951 |
|
| 3952 |
} else if (err == ERROR_OPERATION_ABORTED) { |
| 3953 |
ib::info(ER_IB_MSG_792) << "The error means that the I/O" |
| 3954 |
" operation has been aborted" |
| 3955 |
" because of either a thread exit" |
| 3956 |
" or an application request." |
| 3957 |
" Retry attempt is made."; |
| 3958 |
} else { |
| 3959 |
ib::info(ER_IB_MSG_793) << OPERATING_SYSTEM_ERROR_MSG; |
| 3960 |
} |
| 3961 |
} |
| 3962 |
|
| 3963 |
if (err == ERROR_FILE_NOT_FOUND) { |
| 3964 |
return OS_FILE_NOT_FOUND; |
| 3965 |
} else if (err == ERROR_PATH_NOT_FOUND) { |
| 3966 |
return OS_FILE_NAME_TOO_LONG; |
| 3967 |
} else if (err == ERROR_DISK_FULL) { |
| 3968 |
return OS_FILE_DISK_FULL; |
| 3969 |
} else if (err == ERROR_FILE_EXISTS) { |
| 3970 |
return OS_FILE_ALREADY_EXISTS; |
| 3971 |
} else if (err == ERROR_SHARING_VIOLATION || err == ERROR_LOCK_VIOLATION) { |
| 3972 |
return OS_FILE_SHARING_VIOLATION; |
| 3973 |
} else if (err == ERROR_WORKING_SET_QUOTA || |
| 3974 |
err == ERROR_NO_SYSTEM_RESOURCES) { |
| 3975 |
return OS_FILE_INSUFFICIENT_RESOURCE; |
| 3976 |
} else if (err == ERROR_OPERATION_ABORTED) { |
| 3977 |
return OS_FILE_OPERATION_ABORTED; |
| 3978 |
} else if (err == ERROR_ACCESS_DENIED) { |
| 3979 |
return OS_FILE_ACCESS_VIOLATION; |
| 3980 |
} else if (err == |
| 3981 |
|
| 3982 |
ERROR_TOO_MANY_OPEN_FILES) { |
| 3983 |
return OS_FILE_TOO_MANY_OPENED; |
| 3984 |
} |
| 3985 |
|
| 3986 |
return OS_FILE_ERROR_MAX + err; |
| 3987 |
} |
| 3988 |
|
| 3989 |
/** This function attempts to create a directory named pathname. The new |
| 3990 |
directory gets default permissions. On Unix the permissions are |
| 3991 |
(0770 & ~umask). If the directory exists already, nothing is done and |
| 3992 |
the call succeeds, unless the fail_if_exists arguments is true. |
| 3993 |
If another error occurs, such as a permission error, this does not crash, |
| 3994 |
but reports the error and returns false. |
| 3995 |
@param[in] pathname directory name as null-terminated string |
| 3996 |
@param[in] fail_if_exists if true, pre-existing directory is treated as |
| 3997 |
an error. |
| 3998 |
@return true if call succeeds, false on error */ |
| 3999 |
bool os_file_create_directory(const char *pathname, bool fail_if_exists) { |
| 4000 |
BOOL rcode; |
| 4001 |
|
| 4002 |
rcode = CreateDirectory((LPCTSTR)pathname, NULL); |
| 4003 |
if (!(rcode != 0 || |
| 4004 |
(GetLastError() == ERROR_ALREADY_EXISTS && !fail_if_exists))) { |
| 4005 |
os_file_handle_error_no_exit(pathname, "CreateDirectory", false); |
| 4006 |
|
| 4007 |
return (false); |
| 4008 |
} |
| 4009 |
|
| 4010 |
return (true); |
| 4011 |
} |
| 4012 |
|
| 4013 |
/** This function scans the contents of a directory and invokes the callback |
| 4014 |
for each entry. |
| 4015 |
@param[in] path directory name as null-terminated string |
| 4016 |
@param[in] scan_cbk use callback to be called for each entry |
| 4017 |
@param[in] is_drop attempt to drop the directory after scan |
| 4018 |
@return true if call succeeds, false on error */ |
| 4019 |
bool os_file_scan_directory(const char *path, os_dir_cbk_t scan_cbk, |
| 4020 |
bool is_drop) { |
| 4021 |
bool file_found; |
| 4022 |
HANDLE find_hdl; |
| 4023 |
WIN32_FIND_DATA find_data; |
| 4024 |
char wild_card_path[MAX_PATH]; |
| 4025 |
|
| 4026 |
snprintf(wild_card_path, MAX_PATH, "%s\\*", path); |
| 4027 |
|
| 4028 |
find_hdl = FindFirstFile((LPCTSTR)wild_card_path, &find_data); |
| 4029 |
|
| 4030 |
if (find_hdl == INVALID_HANDLE_VALUE) { |
| 4031 |
os_file_handle_error_no_exit(path, "FindFirstFile", false); |
| 4032 |
return (false); |
| 4033 |
} |
| 4034 |
|
| 4035 |
do { |
| 4036 |
scan_cbk(path, find_data.cFileName); |
| 4037 |
file_found = FindNextFile(find_hdl, &find_data); |
| 4038 |
|
| 4039 |
} while (file_found); |
| 4040 |
|
| 4041 |
FindClose(find_hdl); |
| 4042 |
|
| 4043 |
if (is_drop) { |
| 4044 |
bool ret; |
| 4045 |
|
| 4046 |
ret = RemoveDirectory((LPCSTR)path); |
| 4047 |
|
| 4048 |
if (!ret) { |
| 4049 |
os_file_handle_error_no_exit(path, "RemoveDirectory", false); |
| 4050 |
return (false); |
| 4051 |
} |
| 4052 |
} |
| 4053 |
|
| 4054 |
return (true); |
| 4055 |
} |
| 4056 |
|
| 4057 |
pfs_os_file_t os_file_create_func(const char *name, ulint create_mode, |
| 4058 |
ulint purpose, bool read_only, |
| 4059 |
bool *success) { |
| 4060 |
pfs_os_file_t file; |
| 4061 |
bool retry; |
| 4062 |
bool on_error_no_exit; |
| 4063 |
bool on_error_silent; |
| 4064 |
|
| 4065 |
*success = false; |
| 4066 |
|
| 4067 |
DBUG_EXECUTE_IF("ib_create_table_fail_disk_full", *success = false; |
| 4068 |
SetLastError(ERROR_DISK_FULL); file.m_file = OS_FILE_CLOSED; |
| 4069 |
return (file);); |
| 4070 |
|
| 4071 |
DWORD create_flag; |
| 4072 |
DWORD share_mode = FILE_SHARE_READ; |
| 4073 |
|
| 4074 |
on_error_no_exit = create_mode & OS_FILE_ON_ERROR_NO_EXIT ? true : false; |
| 4075 |
|
| 4076 |
on_error_silent = create_mode & OS_FILE_ON_ERROR_SILENT ? true : false; |
| 4077 |
|
| 4078 |
create_mode &= ~OS_FILE_ON_ERROR_NO_EXIT; |
| 4079 |
create_mode &= ~OS_FILE_ON_ERROR_SILENT; |
| 4080 |
|
| 4081 |
if (create_mode == OS_FILE_OPEN_RAW) { |
| 4082 |
ut_a(!read_only); |
| 4083 |
|
| 4084 |
create_flag = OPEN_EXISTING; |
| 4085 |
|
| 4086 |
/* On Windows Physical devices require admin privileges and |
| 4087 |
have to have the write-share mode set. See the remarks |
| 4088 |
section for the CreateFile() function documentation in MSDN. */ |
| 4089 |
|
| 4090 |
share_mode |= FILE_SHARE_WRITE; |
| 4091 |
|
| 4092 |
} else if (create_mode == OS_FILE_OPEN || create_mode == OS_FILE_OPEN_RETRY) { |
| 4093 |
create_flag = OPEN_EXISTING; |
| 4094 |
|
| 4095 |
} else if (read_only) { |
| 4096 |
create_flag = OPEN_EXISTING; |
| 4097 |
|
| 4098 |
} else if (create_mode == OS_FILE_CREATE) { |
| 4099 |
create_flag = CREATE_NEW; |
| 4100 |
|
| 4101 |
} else if (create_mode == OS_FILE_CREATE_PATH) { |
| 4102 |
/* Create subdirs along the path if needed. */ |
| 4103 |
dberr_t err; |
| 4104 |
|
| 4105 |
err = os_file_create_subdirs_if_needed(name); |
| 4106 |
|
| 4107 |
if (err != DB_SUCCESS) { |
| 4108 |
*success = false; |
| 4109 |
ib::error(ER_IB_MSG_798) |
| 4110 |
<< "Unable to create subdirectories '" << name << "'"; |
| 4111 |
|
| 4112 |
file.m_file = OS_FILE_CLOSED; |
| 4113 |
return (file); |
| 4114 |
} |
| 4115 |
|
| 4116 |
create_flag = CREATE_NEW; |
| 4117 |
create_mode = OS_FILE_CREATE; |
| 4118 |
|
| 4119 |
} else { |
| 4120 |
ib::error(ER_IB_MSG_799) |
| 4121 |
<< "Unknown file create mode (" << create_mode << ") " |
| 4122 |
<< " for file '" << name << "'"; |
| 4123 |
|
| 4124 |
file.m_file = OS_FILE_CLOSED; |
| 4125 |
return (file); |
| 4126 |
} |
| 4127 |
|
| 4128 |
DWORD attributes = 0; |
| 4129 |
|
| 4130 |
#ifdef UNIV_HOTBACKUP |
| 4131 |
attributes |= FILE_FLAG_NO_BUFFERING; |
| 4132 |
#else /* !UNIV_HOTBACKUP */ |
| 4133 |
|
| 4134 |
/* By default we use asynchronous (Overlapped) IO that allows many threads to |
| 4135 |
access file concurrently, this is similar to what POSIX offers. Opening file |
| 4136 |
for non-Overlapped, synchronous access is additionally causing all file |
| 4137 |
accesses to be serialized (including `os_file_flush_func()`), which can |
| 4138 |
easily cause huge performance regressions. It may be up to 5% faster, but |
| 4139 |
only if there is exactly one thread accessing the file. When using this mode |
| 4140 |
for anything else than Clone files or redolog files resizing, one should |
| 4141 |
comment on proof there is exactly one thread doing IO on this file and that |
| 4142 |
turning this mode actually gives any performance advantage in testing. Do not |
| 4143 |
use this for premature optimization, as incorrect usage of `true` can lead to |
| 4144 |
huge regressions, and three usages in the past already did. Note that we |
| 4145 |
serialize writes to the redolog files on `log_sys->writer_mutex`, however we |
| 4146 |
really don't want to use the non-Overlapped mode as the `FlushFileBuffers` are |
| 4147 |
called from the log flusher thread concurrently and |
| 4148 |
using non-Overlapped mode brings huge performance regressions. */ |
| 4149 |
if (purpose != OS_CLONE_LOG_FILE && purpose != OS_CLONE_DATA_FILE && |
| 4150 |
purpose != OS_LOG_FILE_RESIZING) { |
| 4151 |
if (srv_use_native_aio) { |
| 4152 |
attributes |= FILE_FLAG_OVERLAPPED; |
| 4153 |
} |
| 4154 |
} |
| 4155 |
|
| 4156 |
#ifdef UNIV_NON_BUFFERED_IO |
| 4157 |
// TODO: Create a bug, this looks wrong. The flush log |
| 4158 |
// parameter is dynamic. |
| 4159 |
if (purpose == OS_BUFFERED_FILE || purpose == OS_CLONE_LOG_FILE || |
| 4160 |
purpose == OS_LOG_FILE) { |
| 4161 |
/* Do not use unbuffered i/o for the log files because |
| 4162 |
we write really a lot and we have log flusher for fsyncs. */ |
| 4163 |
|
| 4164 |
} else if (srv_win_file_flush_method == SRV_WIN_IO_UNBUFFERED || |
| 4165 |
purpose == OS_DATA_FILE_FOR_SPACE_ID_READ) { |
| 4166 |
/* We use unbuffered access only when allowed by the |
| 4167 |
'srv_win_file_flush_method', unless the purpose is |
| 4168 |
to read the space ID on the startup, in which case |
| 4169 |
we should always use the unbuffered access */ |
| 4170 |
attributes |= FILE_FLAG_NO_BUFFERING; |
| 4171 |
} |
| 4172 |
#endif /* UNIV_NON_BUFFERED_IO */ |
| 4173 |
|
| 4174 |
#endif /* !UNIV_HOTBACKUP */ |
| 4175 |
DWORD access = GENERIC_READ; |
| 4176 |
|
| 4177 |
if (!read_only) { |
| 4178 |
access |= GENERIC_WRITE; |
| 4179 |
} |
| 4180 |
|
| 4181 |
/* Clone and redo log must allow concurrent write to file. */ |
| 4182 |
if (purpose == OS_CLONE_LOG_FILE || purpose == OS_CLONE_DATA_FILE || |
| 4183 |
purpose == OS_LOG_FILE) { |
| 4184 |
share_mode |= FILE_SHARE_WRITE; |
| 4185 |
} |
| 4186 |
|
| 4187 |
do { |
| 4188 |
/* Use default security attributes and no template file. */ |
| 4189 |
file.m_file = CreateFile((LPCTSTR)name, access, share_mode, NULL, |
| 4190 |
create_flag, attributes, NULL); |
| 4191 |
|
| 4192 |
if (file.m_file == INVALID_HANDLE_VALUE) { |
| 4193 |
const char *operation; |
| 4194 |
|
| 4195 |
operation = |
| 4196 |
(create_mode == OS_FILE_CREATE && !read_only) ? "create" : "open"; |
| 4197 |
|
| 4198 |
*success = false; |
| 4199 |
|
| 4200 |
if (on_error_no_exit) { |
| 4201 |
retry = os_file_handle_error_no_exit(name, operation, on_error_silent); |
| 4202 |
} else { |
| 4203 |
retry = os_file_handle_error(name, operation); |
| 4204 |
} |
| 4205 |
} else { |
| 4206 |
retry = false; |
| 4207 |
|
| 4208 |
*success = true; |
| 4209 |
|
| 4210 |
DWORD temp; |
| 4211 |
|
| 4212 |
/* This is a best effort use case, if it fails then |
| 4213 |
we will find out when we try and punch the hole. */ |
| 4214 |
DeviceIoControl(file.m_file, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, &temp, |
| 4215 |
NULL); |
| 4216 |
} |
| 4217 |
|
| 4218 |
} while (retry); |
| 4219 |
|
| 4220 |
return (file); |
| 4221 |
} |
| 4222 |
|
| 4223 |
pfs_os_file_t os_file_create_simple_no_error_handling_func(const char *name, |
| 4224 |
ulint create_mode, |
| 4225 |
ulint access_type, |
| 4226 |
bool read_only, |
| 4227 |
bool *success) { |
| 4228 |
pfs_os_file_t file; |
| 4229 |
|
| 4230 |
*success = false; |
| 4231 |
|
| 4232 |
DWORD access; |
| 4233 |
DWORD create_flag; |
| 4234 |
DWORD attributes = 0; |
| 4235 |
DWORD share_mode = FILE_SHARE_READ; |
| 4236 |
|
| 4237 |
#ifdef UNIV_HOTBACKUP |
| 4238 |
share_mode |= FILE_SHARE_WRITE; |
| 4239 |
#endif /* UNIV_HOTBACKUP */ |
| 4240 |
|
| 4241 |
ut_a(name); |
| 4242 |
|
| 4243 |
ut_a(!(create_mode & OS_FILE_ON_ERROR_SILENT)); |
| 4244 |
ut_a(!(create_mode & OS_FILE_ON_ERROR_NO_EXIT)); |
| 4245 |
|
| 4246 |
if (create_mode == OS_FILE_OPEN) { |
| 4247 |
create_flag = OPEN_EXISTING; |
| 4248 |
|
| 4249 |
} else if (read_only) { |
| 4250 |
create_flag = OPEN_EXISTING; |
| 4251 |
|
| 4252 |
} else if (create_mode == OS_FILE_CREATE) { |
| 4253 |
create_flag = CREATE_NEW; |
| 4254 |
|
| 4255 |
} else { |
| 4256 |
ib::error(ER_IB_MSG_801) |
| 4257 |
<< "Unknown file create mode (" << create_mode << ") " |
| 4258 |
<< " for file '" << name << "'"; |
| 4259 |
|
| 4260 |
file.m_file = OS_FILE_CLOSED; |
| 4261 |
return (file); |
| 4262 |
} |
| 4263 |
|
| 4264 |
if (access_type == OS_FILE_READ_ONLY) { |
| 4265 |
access = GENERIC_READ; |
| 4266 |
|
| 4267 |
} else if (read_only) { |
| 4268 |
access = GENERIC_READ; |
| 4269 |
|
| 4270 |
} else if (access_type == OS_FILE_READ_WRITE) { |
| 4271 |
access = GENERIC_READ | GENERIC_WRITE; |
| 4272 |
|
| 4273 |
} else if (access_type == OS_FILE_READ_ALLOW_DELETE) { |
| 4274 |
ut_a(!read_only); |
| 4275 |
|
| 4276 |
access = GENERIC_READ; |
| 4277 |
|
| 4278 |
/* A backup program has to give mysqld the maximum |
| 4279 |
freedom to do what it likes with the file */ |
| 4280 |
|
| 4281 |
share_mode |= FILE_SHARE_DELETE | FILE_SHARE_WRITE; |
| 4282 |
} else { |
| 4283 |
ib::error(ER_IB_MSG_802) |
| 4284 |
<< "Unknown file access type (" << access_type << ") " |
| 4285 |
<< "for file '" << name << "'"; |
| 4286 |
|
| 4287 |
file.m_file = OS_FILE_CLOSED; |
| 4288 |
return (file); |
| 4289 |
} |
| 4290 |
|
| 4291 |
file.m_file = CreateFile((LPCTSTR)name, access, share_mode, |
| 4292 |
NULL, // Security attributes |
| 4293 |
create_flag, attributes, |
| 4294 |
NULL); // No template file |
| 4295 |
|
| 4296 |
*success = (file.m_file != INVALID_HANDLE_VALUE); |
| 4297 |
|
| 4298 |
if (*success) { |
| 4299 |
DWORD temp; |
| 4300 |
/* This is a best effort use case, if it fails then we will find out when |
| 4301 |
we try and punch the hole. */ |
| 4302 |
DeviceIoControl(file.m_file, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, &temp, |
| 4303 |
NULL); |
| 4304 |
} |
| 4305 |
|
| 4306 |
return (file); |
| 4307 |
} |
| 4308 |
|
| 4309 |
/** Deletes a file if it exists. The file has to be closed before calling this. |
| 4310 |
@param[in] name file path as a null-terminated string |
| 4311 |
@param[out] exist indicate if file pre-exist |
| 4312 |
@return true if success */ |
| 4313 |
bool os_file_delete_if_exists_func(const char *name, bool *exist) { |
| 4314 |
if (!os_file_can_delete(name)) { |
| 4315 |
return (false); |
| 4316 |
} |
| 4317 |
|
| 4318 |
if (exist != nullptr) { |
| 4319 |
*exist = true; |
| 4320 |
} |
| 4321 |
|
| 4322 |
char name_to_delete[MAX_PATH + 8]; |
| 4323 |
|
| 4324 |
uint32_t count = 0; |
| 4325 |
/* On Windows, deleting a file may fail if some other process uses it. |
| 4326 |
However, the file might have been opened with FILE_SHARE_DELETE mode, in |
| 4327 |
which case the delete will succeed, but the file will not be deleted, |
| 4328 |
only marked for deletion when all handles are closed. To work around this, we |
| 4329 |
first move the file to new randomized name, which will not collide with a real |
| 4330 |
name. */ |
| 4331 |
for (DWORD random_id = GetTickCount(); count < 1000; ++count, ++random_id) { |
| 4332 |
random_id &= 0xFFFF; |
| 4333 |
sprintf(name_to_delete, "%s.%04lX.d", name, random_id); |
| 4334 |
if (MoveFile(name, name_to_delete)) break; |
| 4335 |
auto err = GetLastError(); |
| 4336 |
/* We have chosen the "random" value that is already being used. Try another |
| 4337 |
one. */ |
| 4338 |
if (err == ERROR_ALREADY_EXISTS) continue; |
| 4339 |
|
| 4340 |
if (err == ERROR_ACCESS_DENIED) continue; |
| 4341 |
|
| 4342 |
/* We just failed to move the file. It may be being used without |
| 4343 |
FILE_SHARE_DELETE mode. We just try to delete the original filename.*/ |
| 4344 |
sprintf(name_to_delete, "%s", name); |
| 4345 |
|
| 4346 |
break; |
| 4347 |
} |
| 4348 |
|
| 4349 |
count = 0; |
| 4350 |
for (;;) { |
| 4351 |
bool ret = DeleteFile((LPCTSTR)name_to_delete); |
| 4352 |
|
| 4353 |
if (ret) { |
| 4354 |
return (true); |
| 4355 |
} |
| 4356 |
|
| 4357 |
DWORD lasterr = GetLastError(); |
| 4358 |
|
| 4359 |
if (lasterr == ERROR_FILE_NOT_FOUND || lasterr == ERROR_PATH_NOT_FOUND) { |
| 4360 |
/* The file does not exist, this not an error */ |
| 4361 |
if (exist != NULL) { |
| 4362 |
*exist = false; |
| 4363 |
} |
| 4364 |
|
| 4365 |
return (true); |
| 4366 |
} |
| 4367 |
|
| 4368 |
++count; |
| 4369 |
|
| 4370 |
if (count % 10 == 0) { |
| 4371 |
/* Print error information */ |
| 4372 |
os_file_get_last_error(true); |
| 4373 |
|
| 4374 |
if (strcmp(name, name_to_delete) == 0) { |
| 4375 |
ib::warn(ER_IB_MSG_803) |
| 4376 |
<< "Failed to delete file '" << name_to_delete |
| 4377 |
<< "'. Please check if any other process is using it."; |
| 4378 |
} else { |
| 4379 |
ib::warn(ER_IB_MSG_803) |
| 4380 |
<< "Failed to delete file '" << name_to_delete |
| 4381 |
<< "', which was renamed from '" << name |
| 4382 |
<< "'. Please check if any other process is using it."; |
| 4383 |
} |
| 4384 |
} |
| 4385 |
|
| 4386 |
/* Sleep for a 0.1 second */ |
| 4387 |
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 4388 |
|
| 4389 |
if (count > 20) { |
| 4390 |
return (false); |
| 4391 |
} |
| 4392 |
} |
| 4393 |
} |
| 4394 |
|
| 4395 |
/** Deletes a file. The file has to be closed before calling this. |
| 4396 |
@param[in] name File path as NUL terminated string |
| 4397 |
@return true if success */ |
| 4398 |
bool os_file_delete_func(const char *name) { |
| 4399 |
bool existed; |
| 4400 |
if (os_file_delete_if_exists_func(name, &existed)) { |
| 4401 |
/* File did not exist already, this is an error. */ |
| 4402 |
return existed; |
| 4403 |
} else { |
| 4404 |
return false; |
| 4405 |
} |
| 4406 |
} |
| 4407 |
|
| 4408 |
/** NOTE! Use the corresponding macro os_file_rename(), not directly this |
| 4409 |
function! |
| 4410 |
Renames a file (can also move it to another directory). It is safest that the |
| 4411 |
file is closed before calling this function. |
| 4412 |
@param[in] oldpath old file path as a null-terminated string |
| 4413 |
@param[in] newpath new file path |
| 4414 |
@return true if success */ |
| 4415 |
bool os_file_rename_func(const char *oldpath, const char *newpath) { |
| 4416 |
#ifdef UNIV_DEBUG |
| 4417 |
/* New path must be valid but not exist. */ |
| 4418 |
os_file_type_t type; |
| 4419 |
bool exists; |
| 4420 |
ut_ad(os_file_status(newpath, &exists, &type)); |
| 4421 |
ut_ad(!exists); |
| 4422 |
|
| 4423 |
/* Old path must exist. */ |
| 4424 |
ut_ad(os_file_exists(oldpath)); |
| 4425 |
#endif /* UNIV_DEBUG */ |
| 4426 |
|
| 4427 |
if (MoveFileExA(oldpath, newpath, MOVEFILE_WRITE_THROUGH)) { |
| 4428 |
return true; |
| 4429 |
} |
| 4430 |
|
| 4431 |
os_file_handle_error_no_exit(oldpath, "rename", false); |
| 4432 |
|
| 4433 |
return false; |
| 4434 |
} |
| 4435 |
|
| 4436 |
/** NOTE! Use the corresponding macro os_file_close(), not directly |
| 4437 |
this function! |
| 4438 |
Closes a file handle. In case of error, error number can be retrieved with |
| 4439 |
os_file_get_last_error. |
| 4440 |
@param[in] file Handle to a file |
| 4441 |
@return true if success */ |
| 4442 |
bool os_file_close_func(os_file_t file) { |
| 4443 |
ut_a(file != INVALID_HANDLE_VALUE); |
| 4444 |
|
| 4445 |
if (CloseHandle(file)) { |
| 4446 |
return (true); |
| 4447 |
} |
| 4448 |
|
| 4449 |
os_file_handle_error(NULL, "close"); |
| 4450 |
|
| 4451 |
return (false); |
| 4452 |
} |
| 4453 |
|
| 4454 |
/** Gets a file size. |
| 4455 |
@param[in] file Handle to a file |
| 4456 |
@return file size, or (os_offset_t) -1 on failure */ |
| 4457 |
os_offset_t os_file_get_size(pfs_os_file_t file) { |
| 4458 |
DWORD high; |
| 4459 |
DWORD low; |
| 4460 |
|
| 4461 |
low = GetFileSize(file.m_file, &high); |
| 4462 |
if (low == 0xFFFFFFFF && GetLastError() != NO_ERROR) { |
| 4463 |
return ((os_offset_t)-1); |
| 4464 |
} |
| 4465 |
|
| 4466 |
return (os_offset_t(low | (os_offset_t(high) << 32))); |
| 4467 |
} |
| 4468 |
|
| 4469 |
os_file_size_t os_file_get_size(const char *filename) { |
| 4470 |
struct __stat64 s; |
| 4471 |
os_file_size_t file_size; |
| 4472 |
|
| 4473 |
int ret = _stat64(filename, &s); |
| 4474 |
|
| 4475 |
if (ret == 0) { |
| 4476 |
file_size.m_total_size = s.st_size; |
| 4477 |
|
| 4478 |
DWORD low_size; |
| 4479 |
DWORD high_size; |
| 4480 |
|
| 4481 |
low_size = GetCompressedFileSize(filename, &high_size); |
| 4482 |
|
| 4483 |
if (low_size != INVALID_FILE_SIZE || GetLastError() == NO_ERROR) { |
| 4484 |
file_size.m_alloc_size = high_size; |
| 4485 |
file_size.m_alloc_size <<= 32; |
| 4486 |
file_size.m_alloc_size |= low_size; |
| 4487 |
} else { |
| 4488 |
file_size.m_total_size = ~0ULL; |
| 4489 |
file_size.m_alloc_size = (os_offset_t)errno; |
| 4490 |
} |
| 4491 |
} else { |
| 4492 |
file_size.m_total_size = ~0ULL; |
| 4493 |
file_size.m_alloc_size = (os_offset_t)ret; |
| 4494 |
} |
| 4495 |
|
| 4496 |
return (file_size); |
| 4497 |
} |
| 4498 |
|
| 4499 |
/** Get available free space on disk |
| 4500 |
@param[in] path pathname of a directory or file in disk |
| 4501 |
@param[out] block_size Block size to use for IO in bytes |
| 4502 |
@param[out] free_space free space available in bytes |
| 4503 |
@return DB_SUCCESS if all OK */ |
| 4504 |
static dberr_t os_get_free_space_win32(const char *path, uint32_t &block_size, |
| 4505 |
uint64_t &free_space) { |
| 4506 |
char volname[MAX_PATH]; |
| 4507 |
BOOL result = GetVolumePathName(path, volname, MAX_PATH); |
| 4508 |
|
| 4509 |
if (!result) { |
| 4510 |
ib::error(ER_IB_MSG_806) |
| 4511 |
<< "os_file_get_status_win32: " |
| 4512 |
<< "Failed to get the volume path name for: " << path |
| 4513 |
<< "- OS error number " << GetLastError(); |
| 4514 |
|
| 4515 |
return (DB_FAIL); |
| 4516 |
} |
| 4517 |
|
| 4518 |
DWORD sectorsPerCluster; |
| 4519 |
DWORD bytesPerSector; |
| 4520 |
DWORD numberOfFreeClusters; |
| 4521 |
DWORD totalNumberOfClusters; |
| 4522 |
|
| 4523 |
result = |
| 4524 |
GetDiskFreeSpace((LPCSTR)volname, §orsPerCluster, &bytesPerSector, |
| 4525 |
&numberOfFreeClusters, &totalNumberOfClusters); |
| 4526 |
|
| 4527 |
if (!result) { |
| 4528 |
ib::error(ER_IB_MSG_807) << "GetDiskFreeSpace(" << volname << ",...) " |
| 4529 |
<< "failed " |
| 4530 |
<< "- OS error number " << GetLastError(); |
| 4531 |
|
| 4532 |
return (DB_FAIL); |
| 4533 |
} |
| 4534 |
|
| 4535 |
block_size = bytesPerSector * sectorsPerCluster; |
| 4536 |
|
| 4537 |
free_space = static_cast<uint64_t>(block_size); |
| 4538 |
free_space *= numberOfFreeClusters; |
| 4539 |
|
| 4540 |
return (DB_SUCCESS); |
| 4541 |
} |
| 4542 |
|
| 4543 |
/** This function returns information about the specified file |
| 4544 |
@param[in] path pathname of the file |
| 4545 |
@param[out] stat_info information of a file in a directory |
| 4546 |
@param[in,out] statinfo information of a file in a directory |
| 4547 |
@param[in] check_rw_perm for testing whether the file can be opened |
| 4548 |
in RW mode |
| 4549 |
@param[in] read_only true if the file is opened in read-only mode |
| 4550 |
@return DB_SUCCESS if all OK */ |
| 4551 |
static dberr_t os_file_get_status_win32(const char *path, |
| 4552 |
os_file_stat_t *stat_info, |
| 4553 |
struct _stat64 *statinfo, |
| 4554 |
bool check_rw_perm, bool read_only) { |
| 4555 |
int ret = _stat64(path, statinfo); |
| 4556 |
|
| 4557 |
if (ret && (errno == ENOENT || errno == ENOTDIR)) { |
| 4558 |
/* file does not exist */ |
| 4559 |
|
| 4560 |
return (DB_NOT_FOUND); |
| 4561 |
|
| 4562 |
} else if (ret) { |
| 4563 |
/* file exists, but stat call failed */ |
| 4564 |
|
| 4565 |
os_file_handle_error_no_exit(path, "stat", false); |
| 4566 |
|
| 4567 |
return (DB_FAIL); |
| 4568 |
|
| 4569 |
} else if (_S_IFDIR & statinfo->st_mode) { |
| 4570 |
stat_info->type = OS_FILE_TYPE_DIR; |
| 4571 |
|
| 4572 |
} else if (_S_IFREG & statinfo->st_mode) { |
| 4573 |
DWORD access = GENERIC_READ; |
| 4574 |
|
| 4575 |
if (!read_only) { |
| 4576 |
access |= GENERIC_WRITE; |
| 4577 |
} |
| 4578 |
|
| 4579 |
stat_info->type = OS_FILE_TYPE_FILE; |
| 4580 |
|
| 4581 |
/* Check if we can open it in read-only mode. */ |
| 4582 |
|
| 4583 |
if (check_rw_perm) { |
| 4584 |
HANDLE fh; |
| 4585 |
|
| 4586 |
fh = CreateFile((LPCTSTR)path, // File to open |
| 4587 |
access, FILE_SHARE_READ, |
| 4588 |
NULL, // Default security |
| 4589 |
OPEN_EXISTING, // Existing file only |
| 4590 |
FILE_ATTRIBUTE_NORMAL, // Normal file |
| 4591 |
NULL); // No attr. template |
| 4592 |
|
| 4593 |
if (fh == INVALID_HANDLE_VALUE) { |
| 4594 |
stat_info->rw_perm = false; |
| 4595 |
} else { |
| 4596 |
stat_info->rw_perm = true; |
| 4597 |
CloseHandle(fh); |
| 4598 |
} |
| 4599 |
} |
| 4600 |
|
| 4601 |
uint64_t free_space; |
| 4602 |
auto err = os_get_free_space_win32(path, stat_info->block_size, free_space); |
| 4603 |
|
| 4604 |
if (err != DB_SUCCESS) { |
| 4605 |
return (err); |
| 4606 |
} |
| 4607 |
/* On Windows the block size is not used as the allocation |
| 4608 |
unit for sparse files. The underlying infra-structure for |
| 4609 |
sparse files is based on NTFS compression. The punch hole |
| 4610 |
is done on a "compression unit". This compression unit |
| 4611 |
is based on the cluster size. You cannot punch a hole if |
| 4612 |
the cluster size >= 8K. For smaller sizes the table is |
| 4613 |
as follows: |
| 4614 |
|
| 4615 |
Cluster Size Compression Unit |
| 4616 |
512 Bytes 8 KB |
| 4617 |
1 KB 16 KB |
| 4618 |
2 KB 32 KB |
| 4619 |
4 KB 64 KB |
| 4620 |
|
| 4621 |
Default NTFS cluster size is 4K, compression unit size of 64K. |
| 4622 |
Therefore unless the user has created the file system with |
| 4623 |
a smaller cluster size and used larger page sizes there is |
| 4624 |
little benefit from compression out of the box. */ |
| 4625 |
|
| 4626 |
stat_info->block_size = (stat_info->block_size <= 4096) |
| 4627 |
? stat_info->block_size * 16 |
| 4628 |
: UINT32_UNDEFINED; |
| 4629 |
} else { |
| 4630 |
stat_info->type = OS_FILE_TYPE_UNKNOWN; |
| 4631 |
} |
| 4632 |
|
| 4633 |
return (DB_SUCCESS); |
| 4634 |
} |
| 4635 |
|
| 4636 |
/** Truncates a file to a specified size in bytes. |
| 4637 |
Do nothing if the size to preserve is greater or equal to the current |
| 4638 |
size of the file. |
| 4639 |
@param[in] pathname file path |
| 4640 |
@param[in] file file to be truncated |
| 4641 |
@param[in] size size to preserve in bytes |
| 4642 |
@return true if success */ |
| 4643 |
static bool os_file_truncate_win32(const char *pathname, pfs_os_file_t file, |
| 4644 |
os_offset_t size) { |
| 4645 |
LARGE_INTEGER length; |
| 4646 |
|
| 4647 |
length.QuadPart = size; |
| 4648 |
|
| 4649 |
BOOL success = SetFilePointerEx(file.m_file, length, NULL, FILE_BEGIN); |
| 4650 |
|
| 4651 |
if (!success) { |
| 4652 |
os_file_handle_error_no_exit(pathname, "SetFilePointerEx", false); |
| 4653 |
} else { |
| 4654 |
success = SetEndOfFile(file.m_file); |
| 4655 |
if (!success) { |
| 4656 |
os_file_handle_error_no_exit(pathname, "SetEndOfFile", false); |
| 4657 |
} |
| 4658 |
} |
| 4659 |
return (success); |
| 4660 |
} |
| 4661 |
|
| 4662 |
/** Truncates a file at its current position. |
| 4663 |
@param[in] file Handle to be truncated |
| 4664 |
@return true if success */ |
| 4665 |
bool os_file_set_eof(FILE *file) { |
| 4666 |
HANDLE h = (HANDLE)_get_osfhandle(fileno(file)); |
| 4667 |
|
| 4668 |
return (SetEndOfFile(h)); |
| 4669 |
} |
| 4670 |
|
| 4671 |
#ifdef UNIV_HOTBACKUP |
| 4672 |
/** Closes a file handle. |
| 4673 |
@param[in] file Handle to close |
| 4674 |
@return true if success */ |
| 4675 |
bool os_file_close_no_error_handling(os_file_t file) { |
| 4676 |
return (CloseHandle(file) ? true : false); |
| 4677 |
} |
| 4678 |
#endif /* UNIV_HOTBACKUP */ |
| 4679 |
|
| 4680 |
#ifndef UNIV_HOTBACKUP |
| 4681 |
/** This function can be called if one wants to post a batch of reads and |
| 4682 |
prefers an i/o-handler thread to handle them all at once later. You must |
| 4683 |
call os_aio_simulated_wake_handler_threads later to ensure the threads |
| 4684 |
are not left sleeping! */ |
| 4685 |
void os_aio_simulated_put_read_threads_to_sleep() { |
| 4686 |
AIO::simulated_put_read_threads_to_sleep(); |
| 4687 |
} |
| 4688 |
|
| 4689 |
/** This function can be called if one wants to post a batch of reads and |
| 4690 |
prefers an i/o-handler thread to handle them all at once later. You must |
| 4691 |
call os_aio_simulated_wake_handler_threads later to ensure the threads |
| 4692 |
are not left sleeping! */ |
| 4693 |
void AIO::simulated_put_read_threads_to_sleep() { |
| 4694 |
/* The idea of putting background IO threads to sleep is only for |
| 4695 |
Windows when using simulated AIO. Windows XP seems to schedule |
| 4696 |
background threads too eagerly to allow for coalescing during |
| 4697 |
readahead requests. */ |
| 4698 |
|
| 4699 |
if (srv_use_native_aio) { |
| 4700 |
/* We do not use simulated AIO: do nothing */ |
| 4701 |
|
| 4702 |
return; |
| 4703 |
} |
| 4704 |
|
| 4705 |
os_aio_recommend_sleep_for_read_threads = true; |
| 4706 |
|
| 4707 |
for (ulint i = 0; i < os_aio_n_segments; i++) { |
| 4708 |
AIO *array{}; |
| 4709 |
|
| 4710 |
(void)get_array_and_local_segment(array, i); |
| 4711 |
|
| 4712 |
if (array == s_reads) { |
| 4713 |
os_event_reset(os_aio_segment_wait_events[i]); |
| 4714 |
} |
| 4715 |
} |
| 4716 |
} |
| 4717 |
#endif /* !UNIV_HOTBACKUP */ |
| 4718 |
|
| 4719 |
/** Depth first traversal of the directory starting from basedir |
| 4720 |
@param[in] basedir Start scanning from this directory |
| 4721 |
@param[in] recursive `true` if scan should be recursive |
| 4722 |
@param[in] f Callback for each entry found */ |
| 4723 |
void Dir_Walker::walk_win32(const Path &basedir, bool recursive, Function &&f) { |
| 4724 |
using Stack = std::stack<Entry>; |
| 4725 |
|
| 4726 |
HRESULT res; |
| 4727 |
size_t length; |
| 4728 |
Stack directories; |
| 4729 |
TCHAR directory[MAX_PATH]; |
| 4730 |
|
| 4731 |
res = StringCchLength(basedir.c_str(), MAX_PATH, &length); |
| 4732 |
|
| 4733 |
/* Check if the name is too long. */ |
| 4734 |
if (!SUCCEEDED(res)) { |
| 4735 |
ib::warn(ER_IB_MSG_808) << "StringCchLength() call failed!"; |
| 4736 |
return; |
| 4737 |
|
| 4738 |
} else if (length > (MAX_PATH - 3)) { |
| 4739 |
ib::warn(ER_IB_MSG_809) << "Directory name too long: '" << basedir << "'"; |
| 4740 |
return; |
| 4741 |
} |
| 4742 |
|
| 4743 |
StringCchCopy(directory, MAX_PATH, basedir.c_str()); |
| 4744 |
|
| 4745 |
if (directory[_tcslen(directory) - 1] != TEXT('\\')) { |
| 4746 |
StringCchCat(directory, MAX_PATH, TEXT("\\*")); |
| 4747 |
} else { |
| 4748 |
StringCchCat(directory, MAX_PATH, TEXT("*")); |
| 4749 |
} |
| 4750 |
|
| 4751 |
directories.push(Entry(directory, 0)); |
| 4752 |
|
| 4753 |
using Type = std::codecvt_utf8<wchar_t>; |
| 4754 |
using Converter = std::wstring_convert<Type, wchar_t>; |
| 4755 |
|
| 4756 |
Converter converter; |
| 4757 |
|
| 4758 |
while (!directories.empty()) { |
| 4759 |
Entry current = directories.top(); |
| 4760 |
|
| 4761 |
directories.pop(); |
| 4762 |
|
| 4763 |
if (Fil_path::is_hidden(current.m_path)) { |
| 4764 |
ib::info(ER_IB_MSG_SKIP_HIDDEN_DIR, current.m_path.c_str()); |
| 4765 |
continue; |
| 4766 |
} |
| 4767 |
|
| 4768 |
HANDLE h; |
| 4769 |
WIN32_FIND_DATA dirent; |
| 4770 |
|
| 4771 |
h = FindFirstFile(current.m_path.c_str(), &dirent); |
| 4772 |
|
| 4773 |
if (h == INVALID_HANDLE_VALUE) { |
| 4774 |
ib::info(ER_IB_MSG_810) << "Directory read failed:" |
| 4775 |
<< " '" << current.m_path << "' during scan"; |
| 4776 |
|
| 4777 |
continue; |
| 4778 |
} |
| 4779 |
|
| 4780 |
do { |
| 4781 |
/* dirent.cFileName is a TCHAR. */ |
| 4782 |
if (_tcscmp(dirent.cFileName, _T(".")) == 0 || |
| 4783 |
_tcscmp(dirent.cFileName, _T("..")) == 0) { |
| 4784 |
continue; |
| 4785 |
} |
| 4786 |
|
| 4787 |
Path path(current.m_path); |
| 4788 |
|
| 4789 |
/* Shorten the path to remove the trailing '*'. */ |
| 4790 |
ut_ad(path.substr(path.size() - 2).compare("\\*") == 0); |
| 4791 |
|
| 4792 |
path.resize(path.size() - 1); |
| 4793 |
path.append(dirent.cFileName); |
| 4794 |
|
| 4795 |
/* Ignore hidden files and directories. */ |
| 4796 |
if (Fil_path::is_hidden(dirent) || Fil_path::is_hidden(path)) { |
| 4797 |
ib::info(ER_IB_MSG_SKIP_HIDDEN_DIR, path.c_str()); |
| 4798 |
continue; |
| 4799 |
} |
| 4800 |
|
| 4801 |
if ((dirent.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && recursive) { |
| 4802 |
path.append("\\*"); |
| 4803 |
|
| 4804 |
using Value = Stack::value_type; |
| 4805 |
|
| 4806 |
directories.push(Value{path, current.m_depth + 1}); |
| 4807 |
|
| 4808 |
} else { |
| 4809 |
f(path, current.m_depth + 1); |
| 4810 |
} |
| 4811 |
|
| 4812 |
} while (FindNextFile(h, &dirent) != 0); |
| 4813 |
|
| 4814 |
if (GetLastError() != ERROR_NO_MORE_FILES) { |
| 4815 |
ib::error(ER_IB_MSG_811) << "Scanning '" << directory << "'" |
| 4816 |
<< " - FindNextFile(): returned error"; |
| 4817 |
} |
| 4818 |
|
| 4819 |
FindClose(h); |
| 4820 |
} |
| 4821 |
} |
| 4822 |
#endif /* _WIN32 */ |
| 4823 |
|
| 4824 |
/** Does a synchronous read or write depending upon the type specified |
| 4825 |
In case of partial reads/writes the function tries |
| 4826 |
NUM_RETRIES_ON_PARTIAL_IO times to read/write the complete data. |
| 4827 |
@param[in] in_type IO flags |
| 4828 |
@param[in] file handle to an open file |
| 4829 |
@param[out] buf buffer where to read |
| 4830 |
@param[in] offset file offset from the start where to read |
| 4831 |
@param[in] n number of bytes to read, starting from offset |
| 4832 |
@param[out] err DB_SUCCESS or error code |
| 4833 |
@param[in] e_block encrypted block or nullptr. |
| 4834 |
@return number of bytes read/written, -1 if error */ |
| 4835 |
[[nodiscard]] static ssize_t os_file_io(const IORequest &in_type, |
| 4836 |
os_file_t file, void *buf, ulint n, |
| 4837 |
os_offset_t offset, dberr_t *err, |
| 4838 |
const file::Block *e_block) { |
| 4839 |
ulint original_n = n; |
| 4840 |
file::Block *block{}; |
| 4841 |
IORequest type = in_type; |
| 4842 |
ssize_t bytes_returned = 0; |
| 4843 |
byte *encrypt_log_buf = nullptr; |
| 4844 |
|
| 4845 |
if (type.is_compressed()) { |
| 4846 |
/* We don't compress the first page of any file. */ |
| 4847 |
ut_ad(offset > 0); |
| 4848 |
ut_ad(!type.is_log()); |
| 4849 |
if (e_block == nullptr) { |
| 4850 |
block = os_file_compress_page(type, buf, &n); |
| 4851 |
} else { |
| 4852 |
/* Since e_block is valid, encryption must have already happened. Since we |
| 4853 |
do compression before encryption, we assert here that there is no |
| 4854 |
encryption involved. */ |
| 4855 |
ut_ad(!type.is_encrypted()); |
| 4856 |
} |
| 4857 |
} |
| 4858 |
|
| 4859 |
/* We do encryption after compression, since if we do encryption |
| 4860 |
before compression, the encrypted data will cause compression fail |
| 4861 |
or low compression rate. */ |
| 4862 |
if ((type.is_encrypted() || e_block != nullptr) && type.is_write()) { |
| 4863 |
if (!type.is_log()) { |
| 4864 |
/* We don't encrypt the first page of any file. */ |
| 4865 |
auto compressed_block = block; |
| 4866 |
ut_ad(offset > 0); |
| 4867 |
|
| 4868 |
/* If dblwr is involved, we should not be reaching here, because we |
| 4869 |
encrypt the page at higher layer so that the same encrypted page can be |
| 4870 |
written to the dblwr file and the data file. During importing an |
| 4871 |
encrypted tablespace, we reach here. */ |
| 4872 |
if (e_block == nullptr) { |
| 4873 |
block = os_file_encrypt_page(type, buf, n); |
| 4874 |
} else { |
| 4875 |
block = const_cast<file::Block *>(e_block); |
| 4876 |
} |
| 4877 |
|
| 4878 |
if (compressed_block != nullptr) { |
| 4879 |
os_free_block(compressed_block); |
| 4880 |
} |
| 4881 |
} else { |
| 4882 |
ut_a(block == nullptr); |
| 4883 |
/* Skip encrypt log file header */ |
| 4884 |
if (offset >= LOG_FILE_HDR_SIZE) { |
| 4885 |
block = os_file_encrypt_log(type, buf, encrypt_log_buf, n); |
| 4886 |
} |
| 4887 |
} |
| 4888 |
} |
| 4889 |
|
| 4890 |
SyncFileIO sync_file_io(file, buf, n, offset); |
| 4891 |
|
| 4892 |
for (ulint i = 0; i < NUM_RETRIES_ON_PARTIAL_IO; ++i) { |
| 4893 |
ssize_t n_bytes = sync_file_io.execute(type); |
| 4894 |
|
| 4895 |
/* Check for a hard error. Not much we can do now. */ |
| 4896 |
if (n_bytes < 0) { |
| 4897 |
break; |
| 4898 |
|
| 4899 |
} else if ((ulint)n_bytes + bytes_returned == n) { |
| 4900 |
bytes_returned += n_bytes; |
| 4901 |
|
| 4902 |
if (offset > 0 && (type.is_compressed() || type.is_read())) { |
| 4903 |
*err = os_file_io_complete(type, file, reinterpret_cast<byte *>(buf), |
| 4904 |
original_n, offset, n); |
| 4905 |
} else { |
| 4906 |
*err = DB_SUCCESS; |
| 4907 |
} |
| 4908 |
|
| 4909 |
if (block != nullptr) { |
| 4910 |
os_free_block(block); |
| 4911 |
} |
| 4912 |
|
| 4913 |
if (encrypt_log_buf != nullptr) { |
| 4914 |
ut::aligned_free(encrypt_log_buf); |
| 4915 |
} |
| 4916 |
|
| 4917 |
return (original_n); |
| 4918 |
} |
| 4919 |
|
| 4920 |
/* Handle partial read/write. */ |
| 4921 |
|
| 4922 |
ut_ad((ulint)n_bytes + bytes_returned < n); |
| 4923 |
|
| 4924 |
bytes_returned += (ulint)n_bytes; |
| 4925 |
|
| 4926 |
if (!type.is_partial_io_warning_disabled()) { |
| 4927 |
const char *op = type.is_read() ? "read" : "written"; |
| 4928 |
|
| 4929 |
ib::warn(ER_IB_MSG_812) |
| 4930 |
<< n << " bytes should have been " << op << ". Only " |
| 4931 |
<< bytes_returned << " bytes " << op << ". Retrying" |
| 4932 |
<< " for the remaining bytes."; |
| 4933 |
} |
| 4934 |
|
| 4935 |
/* Advance the offset and buffer by n_bytes */ |
| 4936 |
sync_file_io.advance(n_bytes); |
| 4937 |
} |
| 4938 |
|
| 4939 |
if (block != nullptr) { |
| 4940 |
os_free_block(block); |
| 4941 |
} |
| 4942 |
|
| 4943 |
if (encrypt_log_buf != nullptr) { |
| 4944 |
ut::aligned_free(encrypt_log_buf); |
| 4945 |
} |
| 4946 |
|
| 4947 |
if (*err != DB_IO_DECRYPT_FAIL) { |
| 4948 |
*err = DB_IO_ERROR; |
| 4949 |
} |
| 4950 |
|
| 4951 |
if (!type.is_partial_io_warning_disabled()) { |
| 4952 |
ib::warn(ER_IB_MSG_813) |
| 4953 |
<< "Retry attempts for " << (type.is_read() ? "reading" : "writing") |
| 4954 |
<< " partial data failed."; |
| 4955 |
} |
| 4956 |
|
| 4957 |
return (bytes_returned); |
| 4958 |
} |
| 4959 |
|
| 4960 |
/** Does a synchronous write operation in Posix. |
| 4961 |
@param[in] type IO context |
| 4962 |
@param[in] file handle to an open file |
| 4963 |
@param[out] buf buffer from which to write |
| 4964 |
@param[in] n number of bytes to read, starting from offset |
| 4965 |
@param[in] offset file offset from the start where to read |
| 4966 |
@param[out] err DB_SUCCESS or error code |
| 4967 |
@param[in] e_block encrypted block or nullptr. |
| 4968 |
@return number of bytes written, -1 if error */ |
| 4969 |
[[nodiscard]] static ssize_t os_file_pwrite(IORequest &type, os_file_t file, |
| 4970 |
const byte *buf, ulint n, |
| 4971 |
os_offset_t offset, dberr_t *err, |
| 4972 |
const file::Block *e_block) { |
| 4973 |
#ifdef UNIV_HOTBACKUP |
| 4974 |
static meb::Mutex meb_mutex; |
| 4975 |
#endif /* UNIV_HOTBACKUP */ |
| 4976 |
|
| 4977 |
ut_ad(type.validate()); |
| 4978 |
|
| 4979 |
#ifdef UNIV_HOTBACKUP |
| 4980 |
meb_mutex.lock(); |
| 4981 |
#endif /* UNIV_HOTBACKUP */ |
| 4982 |
++os_n_file_writes; |
| 4983 |
#ifdef UNIV_HOTBACKUP |
| 4984 |
meb_mutex.unlock(); |
| 4985 |
#endif /* UNIV_HOTBACKUP */ |
| 4986 |
|
| 4987 |
os_n_pending_writes.fetch_add(1); |
| 4988 |
MONITOR_ATOMIC_INC(MONITOR_OS_PENDING_WRITES); |
| 4989 |
|
| 4990 |
ssize_t n_bytes = |
| 4991 |
os_file_io(type, file, (void *)buf, n, offset, err, e_block); |
| 4992 |
|
| 4993 |
os_n_pending_writes.fetch_sub(1); |
| 4994 |
MONITOR_ATOMIC_DEC(MONITOR_OS_PENDING_WRITES); |
| 4995 |
|
| 4996 |
return (n_bytes); |
| 4997 |
} |
| 4998 |
|
| 4999 |
/** Requests a synchronous write operation. |
| 5000 |
@param[in] type IO flags |
| 5001 |
@param[in] name name of the file or path as a null-terminated |
| 5002 |
string |
| 5003 |
@param[in] file handle to an open file |
| 5004 |
@param[out] buf buffer from which to write |
| 5005 |
@param[in] offset file offset from the start where to read |
| 5006 |
@param[in] n number of bytes to read, starting from offset |
| 5007 |
@param[in] e_block encrypted block or nullptr. |
| 5008 |
@return DB_SUCCESS if request was successful, false if fail */ |
| 5009 |
[[nodiscard]] static dberr_t os_file_write_page(IORequest &type, |
| 5010 |
const char *name, |
| 5011 |
os_file_t file, const byte *buf, |
| 5012 |
os_offset_t offset, ulint n, |
| 5013 |
const file::Block *e_block) { |
| 5014 |
dberr_t err(DB_ERROR_UNSET); |
| 5015 |
|
| 5016 |
ut_ad(type.validate()); |
| 5017 |
ut_ad(n > 0); |
| 5018 |
|
| 5019 |
ssize_t n_bytes = os_file_pwrite(type, file, buf, n, offset, &err, e_block); |
| 5020 |
|
| 5021 |
if ((ulint)n_bytes != n && !os_has_said_disk_full) { |
| 5022 |
ib::error(ER_IB_MSG_814) << "Write to file " << name << " failed at offset " |
| 5023 |
<< offset << ", " << n |
| 5024 |
<< " bytes should have been written," |
| 5025 |
" only " |
| 5026 |
<< n_bytes |
| 5027 |
<< " were written." |
| 5028 |
" Operating system error number " |
| 5029 |
<< errno |
| 5030 |
<< "." |
| 5031 |
" Check that your OS and file system" |
| 5032 |
" support files of this size." |
| 5033 |
" Check also that the disk is not full" |
| 5034 |
" or a disk quota exceeded."; |
| 5035 |
|
| 5036 |
if (strerror(errno) != nullptr) { |
| 5037 |
ib::error(ER_IB_MSG_815) |
| 5038 |
<< "Error number " << errno << " means '" << strerror(errno) << "'"; |
| 5039 |
} |
| 5040 |
|
| 5041 |
ib::info(ER_IB_MSG_816) << OPERATING_SYSTEM_ERROR_MSG; |
| 5042 |
|
| 5043 |
os_has_said_disk_full = true; |
| 5044 |
} |
| 5045 |
|
| 5046 |
return (err); |
| 5047 |
} |
| 5048 |
|
| 5049 |
/** Does a synchronous read operation in Posix. |
| 5050 |
@param[in] type IO flags |
| 5051 |
@param[in] file handle to an open file |
| 5052 |
@param[out] buf buffer where to read |
| 5053 |
@param[in] offset file offset from the start where to read |
| 5054 |
@param[in] n number of bytes to read, starting from offset |
| 5055 |
@param[out] err DB_SUCCESS or error code |
| 5056 |
@return number of bytes read, -1 if error */ |
| 5057 |
[[nodiscard]] static ssize_t os_file_pread(IORequest &type, os_file_t file, |
| 5058 |
void *buf, ulint n, |
| 5059 |
os_offset_t offset, dberr_t *err) { |
| 5060 |
#ifdef UNIV_HOTBACKUP |
| 5061 |
static meb::Mutex meb_mutex; |
| 5062 |
|
| 5063 |
meb_mutex.lock(); |
| 5064 |
#endif /* UNIV_HOTBACKUP */ |
| 5065 |
++os_n_file_reads; |
| 5066 |
#ifdef UNIV_HOTBACKUP |
| 5067 |
meb_mutex.unlock(); |
| 5068 |
#endif /* UNIV_HOTBACKUP */ |
| 5069 |
|
| 5070 |
os_n_pending_reads.fetch_add(1); |
| 5071 |
MONITOR_ATOMIC_INC(MONITOR_OS_PENDING_READS); |
| 5072 |
|
| 5073 |
ssize_t n_bytes = os_file_io(type, file, buf, n, offset, err, nullptr); |
| 5074 |
|
| 5075 |
os_n_pending_reads.fetch_sub(1); |
| 5076 |
MONITOR_ATOMIC_DEC(MONITOR_OS_PENDING_READS); |
| 5077 |
|
| 5078 |
return (n_bytes); |
| 5079 |
} |
| 5080 |
|
| 5081 |
/** Requests a synchronous positioned read operation. |
| 5082 |
@return DB_SUCCESS if request was successful, false if fail |
| 5083 |
@param[in] type IO flags |
| 5084 |
@param[in] file_name file name |
| 5085 |
@param[in] file handle to an open file |
| 5086 |
@param[out] buf buffer where to read |
| 5087 |
@param[in] offset file offset from the start where to read |
| 5088 |
@param[in] n number of bytes to read, starting from offset |
| 5089 |
@param[out] o number of bytes actually read |
| 5090 |
@param[in] exit_on_err if true then exit on error |
| 5091 |
@return DB_SUCCESS or error code */ |
| 5092 |
[[nodiscard]] static dberr_t os_file_read_page(IORequest &type, |
| 5093 |
const char *file_name, |
| 5094 |
os_file_t file, void *buf, |
| 5095 |
os_offset_t offset, ulint n, |
| 5096 |
ulint *o, bool exit_on_err) { |
| 5097 |
dberr_t err(DB_ERROR_UNSET); |
| 5098 |
|
| 5099 |
#ifdef UNIV_HOTBACKUP |
| 5100 |
static meb::Mutex meb_mutex; |
| 5101 |
|
| 5102 |
meb_mutex.lock(); |
| 5103 |
#endif /* UNIV_HOTBACKUP */ |
| 5104 |
os_bytes_read_since_printout += n; |
| 5105 |
#ifdef UNIV_HOTBACKUP |
| 5106 |
meb_mutex.unlock(); |
| 5107 |
#endif /* UNIV_HOTBACKUP */ |
| 5108 |
|
| 5109 |
ut_ad(type.validate()); |
| 5110 |
ut_ad(n > 0); |
| 5111 |
|
| 5112 |
for (;;) { |
| 5113 |
ssize_t n_bytes; |
| 5114 |
|
| 5115 |
n_bytes = os_file_pread(type, file, buf, n, offset, &err); |
| 5116 |
|
| 5117 |
if (o != nullptr) { |
| 5118 |
*o = n_bytes; |
| 5119 |
} |
| 5120 |
|
| 5121 |
if (err == DB_IO_DECRYPT_FAIL) { |
| 5122 |
return err; |
| 5123 |
|
| 5124 |
} else if (err != DB_SUCCESS && !exit_on_err) { |
| 5125 |
return err; |
| 5126 |
|
| 5127 |
} else if ((ulint)n_bytes == n) { |
| 5128 |
/** The read will succeed but decompress can fail |
| 5129 |
for various reasons. */ |
| 5130 |
|
| 5131 |
if (type.is_compression_enabled() && |
| 5132 |
!Compression::is_compressed_page(static_cast<byte *>(buf))) { |
| 5133 |
return (DB_SUCCESS); |
| 5134 |
|
| 5135 |
} else { |
| 5136 |
return (err); |
| 5137 |
} |
| 5138 |
} |
| 5139 |
|
| 5140 |
ib::error(ER_IB_MSG_817) |
| 5141 |
<< "Tried to read " << n << " bytes at offset " << offset |
| 5142 |
<< ", but was only able to read " << n_bytes; |
| 5143 |
|
| 5144 |
if (exit_on_err) { |
| 5145 |
if (!os_file_handle_error(file_name, "read")) { |
| 5146 |
/* Hard error */ |
| 5147 |
break; |
| 5148 |
} |
| 5149 |
|
| 5150 |
} else if (!os_file_handle_error_no_exit(file_name, "read", false)) { |
| 5151 |
/* Hard error */ |
| 5152 |
break; |
| 5153 |
} |
| 5154 |
|
| 5155 |
if (n_bytes > 0 && (ulint)n_bytes < n) { |
| 5156 |
n -= (ulint)n_bytes; |
| 5157 |
offset += (ulint)n_bytes; |
| 5158 |
buf = reinterpret_cast<uchar *>(buf) + (ulint)n_bytes; |
| 5159 |
} |
| 5160 |
} |
| 5161 |
|
| 5162 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_818) |
| 5163 |
<< "Cannot read from file. OS error number " << errno << "."; |
| 5164 |
|
| 5165 |
return (err); |
| 5166 |
} |
| 5167 |
|
| 5168 |
/** Retrieves the last error number if an error occurs in a file io function. |
| 5169 |
The number should be retrieved before any other OS calls (because they may |
| 5170 |
overwrite the error number). If the number is not known to this program, |
| 5171 |
the OS error number + 100 is returned. |
| 5172 |
@param[in] report_all_errors true if we want an error message printed |
| 5173 |
for all errors |
| 5174 |
@return error number, or OS error number + 100 */ |
| 5175 |
ulint os_file_get_last_error(bool report_all_errors) { |
| 5176 |
return (os_file_get_last_error_low(report_all_errors, false)); |
| 5177 |
} |
| 5178 |
|
| 5179 |
/** Does error handling when a file operation fails. |
| 5180 |
Conditionally exits (calling srv_fatal_error()) based on should_exit value |
| 5181 |
and the error type, if should_exit is true then on_error_silent is ignored. |
| 5182 |
@param[in] name name of a file or NULL |
| 5183 |
@param[in] operation operation |
| 5184 |
@param[in] should_exit call srv_fatal_error() on an unknown error, |
| 5185 |
if this parameter is true |
| 5186 |
@param[in] on_error_silent if true then don't print any message to the log |
| 5187 |
iff it is an unknown non-fatal error |
| 5188 |
@return true if we should retry the operation */ |
| 5189 |
[[nodiscard]] static bool os_file_handle_error_cond_exit(const char *name, |
| 5190 |
const char *operation, |
| 5191 |
bool should_exit, |
| 5192 |
bool on_error_silent) { |
| 5193 |
ulint err; |
| 5194 |
|
| 5195 |
err = os_file_get_last_error_low(false, on_error_silent); |
| 5196 |
|
| 5197 |
switch (err) { |
| 5198 |
case OS_FILE_DISK_FULL: |
| 5199 |
/* We only print a warning about disk full once */ |
| 5200 |
|
| 5201 |
if (os_has_said_disk_full) { |
| 5202 |
return (false); |
| 5203 |
} |
| 5204 |
|
| 5205 |
/* Disk full error is reported irrespective of the |
| 5206 |
on_error_silent setting. */ |
| 5207 |
|
| 5208 |
if (name) { |
| 5209 |
ib::error(ER_IB_MSG_819) |
| 5210 |
<< "Encountered a problem with file '" << name << "'"; |
| 5211 |
} |
| 5212 |
|
| 5213 |
ib::error(ER_IB_MSG_820) |
| 5214 |
<< "Disk is full. Try to clean the disk to free space."; |
| 5215 |
|
| 5216 |
os_has_said_disk_full = true; |
| 5217 |
|
| 5218 |
return (false); |
| 5219 |
|
| 5220 |
case OS_FILE_AIO_RESOURCES_RESERVED: |
| 5221 |
case OS_FILE_AIO_INTERRUPTED: |
| 5222 |
case OS_FILE_OPERATION_ABORTED: |
| 5223 |
|
| 5224 |
return (true); |
| 5225 |
|
| 5226 |
case OS_FILE_PATH_ERROR: |
| 5227 |
case OS_FILE_ALREADY_EXISTS: |
| 5228 |
case OS_FILE_ACCESS_VIOLATION: |
| 5229 |
|
| 5230 |
return (false); |
| 5231 |
|
| 5232 |
case OS_FILE_SHARING_VIOLATION: |
| 5233 |
|
| 5234 |
std::this_thread::sleep_for(std::chrono::seconds(10)); |
| 5235 |
return (true); |
| 5236 |
|
| 5237 |
case OS_FILE_INSUFFICIENT_RESOURCE: |
| 5238 |
|
| 5239 |
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 5240 |
return (true); |
| 5241 |
|
| 5242 |
case OS_FILE_NAME_TOO_LONG: |
| 5243 |
return (false); |
| 5244 |
|
| 5245 |
default: |
| 5246 |
|
| 5247 |
/* If it is an operation that can crash on error then it |
| 5248 |
is better to ignore on_error_silent and print an error message |
| 5249 |
to the log. */ |
| 5250 |
|
| 5251 |
if (should_exit || !on_error_silent) { |
| 5252 |
ib::error(ER_IB_MSG_821) |
| 5253 |
<< "File " << (name != nullptr ? name : "(unknown)") << ": '" |
| 5254 |
<< operation |
| 5255 |
<< "'" |
| 5256 |
" returned OS error " |
| 5257 |
<< err << "." << (should_exit ? " Cannot continue operation" : ""); |
| 5258 |
} |
| 5259 |
|
| 5260 |
if (should_exit) { |
| 5261 |
#ifndef UNIV_HOTBACKUP |
| 5262 |
srv_fatal_error(); |
| 5263 |
#else /* UNIV_HOTBACKUP */ |
| 5264 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_822) |
| 5265 |
<< "Internal error," |
| 5266 |
<< " cannot continue operation."; |
| 5267 |
#endif /* UNIV_HOTBACKUP */ |
| 5268 |
} |
| 5269 |
} |
| 5270 |
|
| 5271 |
return (false); |
| 5272 |
} |
| 5273 |
|
| 5274 |
/** Does error handling when a file operation fails. |
| 5275 |
@param[in] name File name or NULL |
| 5276 |
@param[in] operation Name of operation e.g., "read", "write" |
| 5277 |
@return true if we should retry the operation */ |
| 5278 |
static bool os_file_handle_error(const char *name, const char *operation) { |
| 5279 |
/* Exit in case of unknown error */ |
| 5280 |
return (os_file_handle_error_cond_exit(name, operation, true, false)); |
| 5281 |
} |
| 5282 |
|
| 5283 |
/** Does error handling when a file operation fails. |
| 5284 |
@param[in] name File name or NULL |
| 5285 |
@param[in] operation Name of operation e.g., "read", "write" |
| 5286 |
@param[in] on_error_silent if true then don't print any message to the log. |
| 5287 |
@return true if we should retry the operation */ |
| 5288 |
static bool os_file_handle_error_no_exit(const char *name, |
| 5289 |
const char *operation, |
| 5290 |
bool on_error_silent) { |
| 5291 |
/* Don't exit in case of unknown error */ |
| 5292 |
return ( |
| 5293 |
os_file_handle_error_cond_exit(name, operation, false, on_error_silent)); |
| 5294 |
} |
| 5295 |
|
| 5296 |
void os_file_set_nocache(int fd [[maybe_unused]], |
| 5297 |
const char *file_name [[maybe_unused]], |
| 5298 |
const char *operation_name [[maybe_unused]], |
| 5299 |
bool on_error_silent [[maybe_unused]]) { |
| 5300 |
/* some versions of Solaris may not have DIRECTIO_ON */ |
| 5301 |
#if defined(UNIV_SOLARIS) && defined(DIRECTIO_ON) |
| 5302 |
if (directio(fd, DIRECTIO_ON) == -1 && !on_error_silent) { |
| 5303 |
int errno_save = errno; |
| 5304 |
|
| 5305 |
ib::error(ER_IB_MSG_823) |
| 5306 |
<< "Failed to set DIRECTIO_ON on file " << file_name << "; " |
| 5307 |
<< operation_name << ": " << strerror(errno_save) |
| 5308 |
<< "," |
| 5309 |
" continuing anyway."; |
| 5310 |
} |
| 5311 |
#elif defined(O_DIRECT) |
| 5312 |
if (fcntl(fd, F_SETFL, O_DIRECT) == -1 && !on_error_silent) { |
| 5313 |
int errno_save = errno; |
| 5314 |
static bool warning_message_printed = false; |
| 5315 |
if (errno_save == EINVAL) { |
| 5316 |
if (!warning_message_printed) { |
| 5317 |
warning_message_printed = true; |
| 5318 |
#ifdef UNIV_LINUX |
| 5319 |
ib::warn(ER_IB_MSG_824) |
| 5320 |
<< "Failed to set O_DIRECT on file" << file_name << "; " |
| 5321 |
<< operation_name << ": " << strerror(errno_save) |
| 5322 |
<< ", " |
| 5323 |
"continuing anyway. O_DIRECT is " |
| 5324 |
"known to result in 'Invalid argument' " |
| 5325 |
"on Linux on tmpfs, " |
| 5326 |
"see MySQL Bug#26662."; |
| 5327 |
#else /* !UNIV_LINUX */ |
| 5328 |
goto short_warning; |
| 5329 |
#endif /* !UNIV_LINUX */ |
| 5330 |
} |
| 5331 |
} else { |
| 5332 |
#ifndef UNIV_LINUX |
| 5333 |
short_warning: |
| 5334 |
#endif /* !UNIV_LINUX */ |
| 5335 |
ib::warn(ER_IB_MSG_825) << "Failed to set O_DIRECT on file " << file_name |
| 5336 |
<< "; " << operation_name << " : " |
| 5337 |
<< strerror(errno_save) << ", continuing anyway."; |
| 5338 |
} |
| 5339 |
} |
| 5340 |
#endif /* !(UNIV_SOLARIS && DIRECTIO_ON) && O_DIRECT */ |
| 5341 |
} |
| 5342 |
|
| 5343 |
bool os_file_set_size_fast(const char *name, pfs_os_file_t pfs_file, |
| 5344 |
os_offset_t offset, os_offset_t size, bool flush) { |
| 5345 |
#if !defined(NO_FALLOCATE) && defined(UNIV_LINUX) && \ |
| 5346 |
defined(HAVE_FALLOC_FL_ZERO_RANGE) |
| 5347 |
ut_a(size >= offset); |
| 5348 |
|
| 5349 |
static bool print_message = true; |
| 5350 |
|
| 5351 |
int ret = |
| 5352 |
fallocate(pfs_file.m_file, FALLOC_FL_ZERO_RANGE, offset, size - offset); |
| 5353 |
|
| 5354 |
if (ret == 0) { |
| 5355 |
if (flush) { |
| 5356 |
return os_file_flush(pfs_file); |
| 5357 |
} |
| 5358 |
|
| 5359 |
return true; |
| 5360 |
} |
| 5361 |
|
| 5362 |
ut_a(ret == -1); |
| 5363 |
|
| 5364 |
/* Print the failure message only once for all the redo log files. */ |
| 5365 |
if (print_message) { |
| 5366 |
ib::info(ER_IB_MSG_1359) << "fallocate() failed with errno " << errno |
| 5367 |
<< " - falling back to writing NULLs."; |
| 5368 |
print_message = false; |
| 5369 |
} |
| 5370 |
#endif /* !NO_FALLOCATE && UNIV_LINUX && HAVE_FALLOC_FL_ZERO_RANGE */ |
| 5371 |
|
| 5372 |
return os_file_set_size(name, pfs_file, offset, size, flush); |
| 5373 |
} |
| 5374 |
|
| 5375 |
bool os_file_set_size(const char *name, pfs_os_file_t file, os_offset_t offset, |
| 5376 |
os_offset_t size, bool flush) { |
| 5377 |
/* Write up to FSP_EXTENT_SIZE bytes at a time. */ |
| 5378 |
ulint buf_size = 0; |
| 5379 |
|
| 5380 |
if (size <= UNIV_PAGE_SIZE) { |
| 5381 |
buf_size = 1; |
| 5382 |
} else { |
| 5383 |
buf_size = std::min(static_cast<ulint>(64), |
| 5384 |
static_cast<ulint>(size / UNIV_PAGE_SIZE)); |
| 5385 |
} |
| 5386 |
|
| 5387 |
ut_ad(buf_size != 0); |
| 5388 |
|
| 5389 |
buf_size *= UNIV_PAGE_SIZE; |
| 5390 |
|
| 5391 |
/* Align the buffer for possible raw i/o */ |
| 5392 |
byte *buf = static_cast<byte *>(ut::aligned_zalloc(buf_size, UNIV_PAGE_SIZE)); |
| 5393 |
|
| 5394 |
os_offset_t current_size = offset; |
| 5395 |
|
| 5396 |
/* Count to check and print progress of file write for file_size > 100 MB. */ |
| 5397 |
uint percentage_count = 10; |
| 5398 |
|
| 5399 |
while (current_size < size) { |
| 5400 |
ulint n_bytes; |
| 5401 |
|
| 5402 |
if (size - current_size < (os_offset_t)buf_size) { |
| 5403 |
n_bytes = (ulint)(size - current_size); |
| 5404 |
} else { |
| 5405 |
n_bytes = buf_size; |
| 5406 |
} |
| 5407 |
|
| 5408 |
dberr_t err; |
| 5409 |
IORequest request(IORequest::WRITE); |
| 5410 |
|
| 5411 |
err = os_file_write(request, name, file, buf, current_size, n_bytes); |
| 5412 |
|
| 5413 |
if (err != DB_SUCCESS) { |
| 5414 |
ut::aligned_free(buf); |
| 5415 |
return (false); |
| 5416 |
} |
| 5417 |
|
| 5418 |
/* Flush after each os_fsync_threhold bytes */ |
| 5419 |
if (flush && os_fsync_threshold != 0) { |
| 5420 |
if ((current_size + n_bytes) / os_fsync_threshold != |
| 5421 |
current_size / os_fsync_threshold) { |
| 5422 |
DBUG_EXECUTE_IF("flush_after_reaching_threshold", |
| 5423 |
std::cerr << os_fsync_threshold |
| 5424 |
<< " bytes being flushed at once" |
| 5425 |
<< std::endl;); |
| 5426 |
|
| 5427 |
bool ret = os_file_flush(file); |
| 5428 |
|
| 5429 |
if (!ret) { |
| 5430 |
ut::aligned_free(buf); |
| 5431 |
return (false); |
| 5432 |
} |
| 5433 |
} |
| 5434 |
} |
| 5435 |
|
| 5436 |
/* Print percentage of progress if the size is more than 100MB */ |
| 5437 |
if ((size >> 20) > 100) { |
| 5438 |
float progress_percentage = |
| 5439 |
((float)(current_size + n_bytes) / (float)size) * 100; |
| 5440 |
|
| 5441 |
if (progress_percentage >= percentage_count) { |
| 5442 |
ib::info(ER_IB_MSG_FILE_RESIZE, name, ulonglong{size >> 20}, |
| 5443 |
percentage_count); |
| 5444 |
percentage_count += 10; |
| 5445 |
} |
| 5446 |
} |
| 5447 |
|
| 5448 |
current_size += n_bytes; |
| 5449 |
} |
| 5450 |
|
| 5451 |
ut::aligned_free(buf); |
| 5452 |
|
| 5453 |
if (flush) { |
| 5454 |
return (os_file_flush(file)); |
| 5455 |
} |
| 5456 |
|
| 5457 |
return (true); |
| 5458 |
} |
| 5459 |
|
| 5460 |
/** Truncates a file to a specified size in bytes. |
| 5461 |
Do nothing if the size to preserve is greater or equal to the current |
| 5462 |
size of the file. |
| 5463 |
@param[in] pathname file path |
| 5464 |
@param[in] file file to be truncated |
| 5465 |
@param[in] size size to preserve in bytes |
| 5466 |
@return true if success */ |
| 5467 |
bool os_file_truncate(const char *pathname, pfs_os_file_t file, |
| 5468 |
os_offset_t size) { |
| 5469 |
/* Do nothing if the size preserved is larger than or equal to the |
| 5470 |
current size of file */ |
| 5471 |
os_offset_t size_bytes = os_file_get_size(file); |
| 5472 |
|
| 5473 |
if (size >= size_bytes) { |
| 5474 |
return (true); |
| 5475 |
} |
| 5476 |
|
| 5477 |
#ifdef _WIN32 |
| 5478 |
return (os_file_truncate_win32(pathname, file, size)); |
| 5479 |
#else /* !_WIN32 */ |
| 5480 |
return (os_file_truncate_posix(pathname, file, size)); |
| 5481 |
#endif /* !_WIN32 */ |
| 5482 |
} |
| 5483 |
|
| 5484 |
/** Set read/write position of a file handle to specific offset. |
| 5485 |
@param[in] pathname file path |
| 5486 |
@param[in] file file handle |
| 5487 |
@param[in] offset read/write offset |
| 5488 |
@return true if success */ |
| 5489 |
bool os_file_seek(const char *pathname, os_file_t file, os_offset_t offset) { |
| 5490 |
bool success = true; |
| 5491 |
|
| 5492 |
#ifdef _WIN32 |
| 5493 |
LARGE_INTEGER length; |
| 5494 |
|
| 5495 |
length.QuadPart = offset; |
| 5496 |
|
| 5497 |
success = SetFilePointerEx(file, length, NULL, FILE_BEGIN); |
| 5498 |
|
| 5499 |
#else /* !_WIN32 */ |
| 5500 |
off_t ret; |
| 5501 |
|
| 5502 |
ret = lseek(file, offset, SEEK_SET); |
| 5503 |
|
| 5504 |
if (ret == -1) { |
| 5505 |
success = false; |
| 5506 |
} |
| 5507 |
#endif /* !_WIN32 */ |
| 5508 |
|
| 5509 |
if (!success) { |
| 5510 |
os_file_handle_error_no_exit(pathname, "os_file_set", false); |
| 5511 |
} |
| 5512 |
|
| 5513 |
return (success); |
| 5514 |
} |
| 5515 |
|
| 5516 |
/** NOTE! Use the corresponding macro os_file_read_first_page(), not directly |
| 5517 |
this function! |
| 5518 |
Requests a synchronous read operation of page 0 of IBD file. |
| 5519 |
@param[in] type IO request context |
| 5520 |
@param[in] file_name file name |
| 5521 |
@param[in] file Open file handle |
| 5522 |
@param[out] buf buffer where to read |
| 5523 |
@param[in] offset file offset where to read |
| 5524 |
@param[in] n number of bytes to read |
| 5525 |
@return DB_SUCCESS if request was successful, DB_IO_ERROR on failure */ |
| 5526 |
dberr_t os_file_read_func(IORequest &type, const char *file_name, |
| 5527 |
os_file_t file, void *buf, os_offset_t offset, |
| 5528 |
ulint n) { |
| 5529 |
ut_ad(type.is_read()); |
| 5530 |
|
| 5531 |
return ( |
| 5532 |
os_file_read_page(type, file_name, file, buf, offset, n, nullptr, true)); |
| 5533 |
} |
| 5534 |
|
| 5535 |
/** NOTE! Use the corresponding macro os_file_read_first_page(), |
| 5536 |
not directly this function! |
| 5537 |
Requests a synchronous read operation of page 0 of IBD file |
| 5538 |
@param[in] type IO request context |
| 5539 |
@param[in] file_name file name |
| 5540 |
@param[in] file Open file handle |
| 5541 |
@param[out] buf buffer where to read |
| 5542 |
@param[in] n number of bytes to read |
| 5543 |
@return DB_SUCCESS if request was successful, DB_IO_ERROR on failure */ |
| 5544 |
dberr_t os_file_read_first_page_func(IORequest &type, const char *file_name, |
| 5545 |
os_file_t file, void *buf, ulint n) { |
| 5546 |
ut_ad(type.is_read()); |
| 5547 |
|
| 5548 |
dberr_t err = os_file_read_page(type, file_name, file, buf, 0, |
| 5549 |
UNIV_ZIP_SIZE_MIN, nullptr, true); |
| 5550 |
|
| 5551 |
if (err == DB_SUCCESS) { |
| 5552 |
uint32_t flags = fsp_header_get_flags(static_cast<byte *>(buf)); |
| 5553 |
const page_size_t page_size(flags); |
| 5554 |
/* TODO: Revert to single page access. |
| 5555 |
Temporally, accepting multiple pages for Fil_shard::get_file_size() during |
| 5556 |
recovery phase, until we can get consistent DD flag at the time. |
| 5557 |
Fil_shard::get_file_size() doesn't need multiple pages access for |
| 5558 |
estimation, if the consistent flag is got from recovered DD. */ |
| 5559 |
const size_t read_size = page_size.physical() * (n >> UNIV_PAGE_SIZE_SHIFT); |
| 5560 |
ut_ad(read_size > 0); |
| 5561 |
err = os_file_read_page(type, file_name, file, buf, 0, read_size, nullptr, |
| 5562 |
true); |
| 5563 |
} |
| 5564 |
return (err); |
| 5565 |
} |
| 5566 |
|
| 5567 |
/** copy data from one file to another file using read, write. |
| 5568 |
@param[in] src_file file handle to copy from |
| 5569 |
@param[in] src_offset offset to copy from |
| 5570 |
@param[in] dest_file file handle to copy to |
| 5571 |
@param[in] dest_offset offset to copy to |
| 5572 |
@param[in] size number of bytes to copy |
| 5573 |
@return DB_SUCCESS if successful */ |
| 5574 |
static dberr_t os_file_copy_read_write(os_file_t src_file, |
| 5575 |
os_offset_t src_offset, |
| 5576 |
os_file_t dest_file, |
| 5577 |
os_offset_t dest_offset, uint size) { |
| 5578 |
dberr_t err; |
| 5579 |
uint request_size; |
| 5580 |
const uint BUF_SIZE = 4 * UNIV_SECTOR_SIZE; |
| 5581 |
|
| 5582 |
alignas(UNIV_SECTOR_SIZE) char buf[BUF_SIZE]; |
| 5583 |
|
| 5584 |
IORequest read_request(IORequest::READ); |
| 5585 |
read_request.disable_compression(); |
| 5586 |
read_request.clear_encrypted(); |
| 5587 |
|
| 5588 |
IORequest write_request(IORequest::WRITE); |
| 5589 |
write_request.disable_compression(); |
| 5590 |
write_request.clear_encrypted(); |
| 5591 |
|
| 5592 |
while (size > 0) { |
| 5593 |
if (size > BUF_SIZE) { |
| 5594 |
request_size = BUF_SIZE; |
| 5595 |
} else { |
| 5596 |
request_size = size; |
| 5597 |
} |
| 5598 |
|
| 5599 |
err = os_file_read_func(read_request, nullptr, src_file, &buf, src_offset, |
| 5600 |
request_size); |
| 5601 |
|
| 5602 |
if (err != DB_SUCCESS) { |
| 5603 |
return (err); |
| 5604 |
} |
| 5605 |
src_offset += request_size; |
| 5606 |
|
| 5607 |
err = os_file_write_func(write_request, "file copy", dest_file, &buf, |
| 5608 |
dest_offset, request_size); |
| 5609 |
|
| 5610 |
if (err != DB_SUCCESS) { |
| 5611 |
return (err); |
| 5612 |
} |
| 5613 |
dest_offset += request_size; |
| 5614 |
size -= request_size; |
| 5615 |
} |
| 5616 |
|
| 5617 |
return (DB_SUCCESS); |
| 5618 |
} |
| 5619 |
|
| 5620 |
/** Copy data from one file to another file. Data is read/written |
| 5621 |
at current file offset. |
| 5622 |
@param[in] src_file file handle to copy from |
| 5623 |
@param[in] src_offset offset to copy from |
| 5624 |
@param[in] dest_file file handle to copy to |
| 5625 |
@param[in] dest_offset offset to copy to |
| 5626 |
@param[in] size number of bytes to copy |
| 5627 |
@return DB_SUCCESS if successful */ |
| 5628 |
#ifdef __linux__ |
| 5629 |
dberr_t os_file_copy_func(os_file_t src_file, os_offset_t src_offset, |
| 5630 |
os_file_t dest_file, os_offset_t dest_offset, |
| 5631 |
uint size) { |
| 5632 |
dberr_t err; |
| 5633 |
static bool use_sendfile = true; |
| 5634 |
|
| 5635 |
uint actual_size; |
| 5636 |
int ret_size; |
| 5637 |
|
| 5638 |
int src_fd; |
| 5639 |
int dest_fd; |
| 5640 |
|
| 5641 |
if (!os_file_seek(nullptr, src_file, src_offset)) { |
| 5642 |
return (DB_IO_ERROR); |
| 5643 |
} |
| 5644 |
|
| 5645 |
if (!os_file_seek(nullptr, dest_file, dest_offset)) { |
| 5646 |
return (DB_IO_ERROR); |
| 5647 |
} |
| 5648 |
|
| 5649 |
src_fd = OS_FD_FROM_FILE(src_file); |
| 5650 |
dest_fd = OS_FD_FROM_FILE(dest_file); |
| 5651 |
|
| 5652 |
while (use_sendfile && size > 0) { |
| 5653 |
ret_size = sendfile(dest_fd, src_fd, nullptr, size); |
| 5654 |
|
| 5655 |
if (ret_size == -1) { |
| 5656 |
/* Fall through read/write path. */ |
| 5657 |
ib::info(ER_IB_MSG_827) << "sendfile failed to copy data" |
| 5658 |
" : trying read/write "; |
| 5659 |
|
| 5660 |
use_sendfile = false; |
| 5661 |
break; |
| 5662 |
} |
| 5663 |
|
| 5664 |
actual_size = static_cast<uint>(ret_size); |
| 5665 |
|
| 5666 |
ut_ad(size >= actual_size); |
| 5667 |
size -= actual_size; |
| 5668 |
} |
| 5669 |
|
| 5670 |
if (size == 0) { |
| 5671 |
return (DB_SUCCESS); |
| 5672 |
} |
| 5673 |
|
| 5674 |
err = os_file_copy_read_write(src_file, src_offset, dest_file, dest_offset, |
| 5675 |
size); |
| 5676 |
|
| 5677 |
return (err); |
| 5678 |
} |
| 5679 |
#else /* !__linux__ */ |
| 5680 |
dberr_t os_file_copy_func(os_file_t src_file, os_offset_t src_offset, |
| 5681 |
os_file_t dest_file, os_offset_t dest_offset, |
| 5682 |
uint size) { |
| 5683 |
dberr_t err; |
| 5684 |
|
| 5685 |
err = os_file_copy_read_write(src_file, src_offset, dest_file, dest_offset, |
| 5686 |
size); |
| 5687 |
return (err); |
| 5688 |
} |
| 5689 |
#endif /* !__linux__ */ |
| 5690 |
|
| 5691 |
dberr_t os_file_read_no_error_handling_func(IORequest &type, |
| 5692 |
const char *file_name, |
| 5693 |
os_file_t file, void *buf, |
| 5694 |
os_offset_t offset, ulint n, |
| 5695 |
ulint *o) { |
| 5696 |
ut_ad(type.is_read()); |
| 5697 |
|
| 5698 |
return (os_file_read_page(type, file_name, file, buf, offset, n, o, false)); |
| 5699 |
} |
| 5700 |
|
| 5701 |
/** NOTE! Use the corresponding macro os_file_write(), not directly this |
| 5702 |
function! |
| 5703 |
Requests a synchronous write operation. |
| 5704 |
@param[in,out] type IO request context |
| 5705 |
@param[in] name name of the file or path as a null-terminated |
| 5706 |
string |
| 5707 |
@param[in] file Open file handle |
| 5708 |
@param[out] buf buffer where to read |
| 5709 |
@param[in] offset file offset where to read |
| 5710 |
@param[in] n number of bytes to read |
| 5711 |
@return DB_SUCCESS if request was successful */ |
| 5712 |
dberr_t os_file_write_func(IORequest &type, const char *name, os_file_t file, |
| 5713 |
const void *buf, os_offset_t offset, ulint n) { |
| 5714 |
ut_ad(type.validate()); |
| 5715 |
ut_ad(type.is_write()); |
| 5716 |
|
| 5717 |
/* We never compress the first page. |
| 5718 |
Note: This assumes we always do block IO. */ |
| 5719 |
if (offset == 0) { |
| 5720 |
type.clear_compressed(); |
| 5721 |
} |
| 5722 |
|
| 5723 |
const byte *ptr = reinterpret_cast<const byte *>(buf); |
| 5724 |
|
| 5725 |
return os_file_write_page(type, name, file, ptr, offset, n, |
| 5726 |
type.get_encrypted_block()); |
| 5727 |
} |
| 5728 |
|
| 5729 |
bool os_file_status(const char *path, bool *exists, os_file_type_t *type) { |
| 5730 |
#ifdef _WIN32 |
| 5731 |
return (os_file_status_win32(path, exists, type)); |
| 5732 |
#else /* !_WIN32 */ |
| 5733 |
return (os_file_status_posix(path, exists, type)); |
| 5734 |
#endif /* !_WIN32 */ |
| 5735 |
} |
| 5736 |
|
| 5737 |
bool os_file_exists(const char *path) { |
| 5738 |
#ifdef _WIN32 |
| 5739 |
return (os_file_exists_win32(path)); |
| 5740 |
#else /* !_WIN32 */ |
| 5741 |
return (os_file_exists_posix(path)); |
| 5742 |
#endif /* !_WIN32 */ |
| 5743 |
} |
| 5744 |
|
| 5745 |
/** Free storage space associated with a section of the file. |
| 5746 |
@param[in] fh Open file handle |
| 5747 |
@param[in] off Starting offset (SEEK_SET) |
| 5748 |
@param[in] len Size of the hole |
| 5749 |
@return DB_SUCCESS or error code */ |
| 5750 |
dberr_t os_file_punch_hole(os_file_t fh, os_offset_t off, os_offset_t len) { |
| 5751 |
/* In this debugging mode, we act as if punch hole is supported, |
| 5752 |
and then skip any calls to actually punch a hole here. |
| 5753 |
In this way, Transparent Page Compression is still being tested. */ |
| 5754 |
DBUG_EXECUTE_IF("ignore_punch_hole", return (DB_SUCCESS);); |
| 5755 |
|
| 5756 |
#ifdef _WIN32 |
| 5757 |
return (os_file_punch_hole_win32(fh, off, len)); |
| 5758 |
#else /* !_WIN32 */ |
| 5759 |
return (os_file_punch_hole_posix(fh, off, len)); |
| 5760 |
#endif /* !_WIN32 */ |
| 5761 |
} |
| 5762 |
|
| 5763 |
bool os_is_sparse_file_supported(pfs_os_file_t fh) { |
| 5764 |
/* In this debugging mode, we act as if punch hole is supported, |
| 5765 |
then we skip any calls to actually punch a hole. In this way, |
| 5766 |
Transparent Page Compression is still being tested. */ |
| 5767 |
DBUG_EXECUTE_IF("ignore_punch_hole", return (true);); |
| 5768 |
|
| 5769 |
dberr_t err; |
| 5770 |
|
| 5771 |
/* We don't know the FS block size, use the sector size. The FS |
| 5772 |
will do the magic. */ |
| 5773 |
err = os_file_punch_hole(fh.m_file, 0, UNIV_PAGE_SIZE); |
| 5774 |
|
| 5775 |
return (err == DB_SUCCESS); |
| 5776 |
} |
| 5777 |
|
| 5778 |
dberr_t os_get_free_space(const char *path, uint64_t &free_space) { |
| 5779 |
#ifdef _WIN32 |
| 5780 |
uint32_t block_size; |
| 5781 |
auto err = os_get_free_space_win32(path, block_size, free_space); |
| 5782 |
|
| 5783 |
#else /* !_WIN32 */ |
| 5784 |
auto err = os_get_free_space_posix(path, free_space); |
| 5785 |
|
| 5786 |
#endif /* !_WIN32 */ |
| 5787 |
return (err); |
| 5788 |
} |
| 5789 |
|
| 5790 |
/** This function returns information about the specified file |
| 5791 |
@param[in] path pathname of the file |
| 5792 |
@param[out] stat_info information of a file in a directory |
| 5793 |
@param[in] check_rw_perm for testing whether the file can be opened |
| 5794 |
in RW mode |
| 5795 |
@param[in] read_only true if file is opened in read-only mode |
| 5796 |
@return DB_SUCCESS if all OK */ |
| 5797 |
dberr_t os_file_get_status(const char *path, os_file_stat_t *stat_info, |
| 5798 |
bool check_rw_perm, bool read_only) { |
| 5799 |
dberr_t ret; |
| 5800 |
|
| 5801 |
#ifdef _WIN32 |
| 5802 |
struct _stat64 info; |
| 5803 |
|
| 5804 |
ret = os_file_get_status_win32(path, stat_info, &info, check_rw_perm, |
| 5805 |
read_only); |
| 5806 |
|
| 5807 |
#else /* !_WIN32 */ |
| 5808 |
struct stat info; |
| 5809 |
|
| 5810 |
ret = os_file_get_status_posix(path, stat_info, &info, check_rw_perm, |
| 5811 |
read_only); |
| 5812 |
|
| 5813 |
#endif /* !_WIN32 */ |
| 5814 |
|
| 5815 |
if (ret == DB_SUCCESS) { |
| 5816 |
stat_info->ctime = info.st_ctime; |
| 5817 |
stat_info->atime = info.st_atime; |
| 5818 |
stat_info->mtime = info.st_mtime; |
| 5819 |
stat_info->size = info.st_size; |
| 5820 |
} |
| 5821 |
|
| 5822 |
return (ret); |
| 5823 |
} |
| 5824 |
|
| 5825 |
dberr_t os_file_write_zeros(pfs_os_file_t file, const char *name, |
| 5826 |
ulint page_size, os_offset_t start, ulint len) { |
| 5827 |
ut_a(len > 0); |
| 5828 |
|
| 5829 |
/* Extend at most 1M at a time */ |
| 5830 |
ulint n_bytes = std::min(static_cast<ulint>(1024 * 1024), len); |
| 5831 |
|
| 5832 |
byte *buf = reinterpret_cast<byte *>(ut::aligned_zalloc(n_bytes, page_size)); |
| 5833 |
|
| 5834 |
os_offset_t offset = start; |
| 5835 |
dberr_t err = DB_SUCCESS; |
| 5836 |
const os_offset_t end = start + len; |
| 5837 |
IORequest request(IORequest::WRITE); |
| 5838 |
|
| 5839 |
while (offset < end) { |
| 5840 |
err = os_file_write(request, name, file, buf, offset, n_bytes); |
| 5841 |
|
| 5842 |
if (err != DB_SUCCESS) { |
| 5843 |
break; |
| 5844 |
} |
| 5845 |
|
| 5846 |
offset += n_bytes; |
| 5847 |
|
| 5848 |
n_bytes = std::min(n_bytes, static_cast<ulint>(end - offset)); |
| 5849 |
|
| 5850 |
DBUG_EXECUTE_IF("ib_crash_during_tablespace_extension", DBUG_SUICIDE();); |
| 5851 |
} |
| 5852 |
|
| 5853 |
ut::aligned_free(buf); |
| 5854 |
|
| 5855 |
return (err); |
| 5856 |
} |
| 5857 |
|
| 5858 |
bool os_file_check_mode(const char *name, bool read_only) { |
| 5859 |
os_file_stat_t stat; |
| 5860 |
|
| 5861 |
memset(&stat, 0x0, sizeof(stat)); |
| 5862 |
|
| 5863 |
dberr_t err = os_file_get_status(name, &stat, true, read_only); |
| 5864 |
|
| 5865 |
if (err == DB_FAIL) { |
| 5866 |
ib::error(ER_IB_MSG_1058, name); |
| 5867 |
return false; |
| 5868 |
|
| 5869 |
} else if (err == DB_SUCCESS) { |
| 5870 |
/* Note: stat.rw_perm is only valid on files */ |
| 5871 |
|
| 5872 |
if (stat.type == OS_FILE_TYPE_FILE) { |
| 5873 |
/* Note: stat.rw_perm is true if it can be opened in |
| 5874 |
mode specified by the "read_only" argument. */ |
| 5875 |
if (!stat.rw_perm) { |
| 5876 |
const char *mode = read_only ? "read" : "read-write"; |
| 5877 |
|
| 5878 |
ib::error(ER_IB_MSG_1059, name, mode); |
| 5879 |
return false; |
| 5880 |
} |
| 5881 |
return true; |
| 5882 |
|
| 5883 |
} else { |
| 5884 |
/* Not a regular file, bail out. */ |
| 5885 |
ib::error(ER_IB_MSG_1060, name); |
| 5886 |
|
| 5887 |
return false; |
| 5888 |
} |
| 5889 |
|
| 5890 |
} else { |
| 5891 |
/* This is OK. If the file create fails on RO media, there |
| 5892 |
is nothing we can do. */ |
| 5893 |
|
| 5894 |
ut_a(err == DB_NOT_FOUND); |
| 5895 |
|
| 5896 |
return true; |
| 5897 |
} |
| 5898 |
} |
| 5899 |
#ifndef UNIV_HOTBACKUP |
| 5900 |
dberr_t os_aio_handler(ulint segment, fil_node_t **m1, void **m2, |
| 5901 |
IORequest *request) { |
| 5902 |
dberr_t err; |
| 5903 |
|
| 5904 |
if (srv_use_native_aio) { |
| 5905 |
srv_set_io_thread_op_info(segment, "native aio handle"); |
| 5906 |
|
| 5907 |
#ifdef _WIN32 |
| 5908 |
|
| 5909 |
err = os_aio_windows_handler(segment, m1, m2, request); |
| 5910 |
|
| 5911 |
#elif defined(LINUX_NATIVE_AIO) |
| 5912 |
|
| 5913 |
err = os_aio_linux_handler(segment, m1, m2, request); |
| 5914 |
#else /* !_WIN32 && !LINUX_NATIVE_AIO */ |
| 5915 |
ut_error; |
| 5916 |
|
| 5917 |
err = DB_ERROR; /* Eliminate compiler warning */ |
| 5918 |
|
| 5919 |
#endif /* !_WIN32 && !LINUX_NATIVE_AIO */ |
| 5920 |
|
| 5921 |
} else { |
| 5922 |
srv_set_io_thread_op_info(segment, "simulated aio handle"); |
| 5923 |
|
| 5924 |
err = os_aio_simulated_handler(segment, m1, m2, request); |
| 5925 |
} |
| 5926 |
|
| 5927 |
return (err); |
| 5928 |
} |
| 5929 |
|
| 5930 |
/** Constructor |
| 5931 |
@param[in] id The latch ID |
| 5932 |
@param[in] n Number of AIO slots |
| 5933 |
@param[in] segments Number of segments */ |
| 5934 |
AIO::AIO(latch_id_t id, ulint n, ulint segments) |
| 5935 |
: m_slots(n), |
| 5936 |
m_n_segments(segments), |
| 5937 |
m_n_reserved(), |
| 5938 |
m_last_slot_used(0) |
| 5939 |
#ifdef LINUX_NATIVE_AIO |
| 5940 |
, |
| 5941 |
m_aio_ctx(), |
| 5942 |
m_events(m_slots.size()) |
| 5943 |
#elif defined(_WIN32) |
| 5944 |
, |
| 5945 |
m_handles() |
| 5946 |
#endif /* !LINUX_NATIVE_AIO && _WIN32 */ |
| 5947 |
{ |
| 5948 |
ut_a(n > 0); |
| 5949 |
ut_a(m_n_segments > 0); |
| 5950 |
|
| 5951 |
mutex_create(id, &m_mutex); |
| 5952 |
|
| 5953 |
m_not_full = os_event_create(); |
| 5954 |
m_is_empty = os_event_create(); |
| 5955 |
|
| 5956 |
#ifdef LINUX_NATIVE_AIO |
| 5957 |
memset(&m_events[0], 0x0, sizeof(m_events[0]) * m_events.size()); |
| 5958 |
#endif /* LINUX_NATIVE_AIO */ |
| 5959 |
|
| 5960 |
os_event_set(m_is_empty); |
| 5961 |
} |
| 5962 |
|
| 5963 |
size_t AIO::number_of_extra_threads() { return srv_read_only_mode ? 0 : 1; } |
| 5964 |
|
| 5965 |
/** Initialise the slots */ |
| 5966 |
dberr_t AIO::init_slots() { |
| 5967 |
for (ulint i = 0; i < m_slots.size(); ++i) { |
| 5968 |
Slot &slot = m_slots[i]; |
| 5969 |
|
| 5970 |
slot.pos = static_cast<uint16_t>(i); |
| 5971 |
|
| 5972 |
slot.is_reserved = false; |
| 5973 |
|
| 5974 |
#ifdef _WIN32 |
| 5975 |
|
| 5976 |
slot.handle = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 5977 |
|
| 5978 |
OVERLAPPED *over = &slot.control; |
| 5979 |
|
| 5980 |
over->hEvent = slot.handle; |
| 5981 |
|
| 5982 |
(*m_handles)[i] = over->hEvent; |
| 5983 |
|
| 5984 |
#elif defined(LINUX_NATIVE_AIO) |
| 5985 |
|
| 5986 |
slot.ret = 0; |
| 5987 |
|
| 5988 |
slot.n_bytes = 0; |
| 5989 |
|
| 5990 |
memset(&slot.control, 0x0, sizeof(slot.control)); |
| 5991 |
|
| 5992 |
#endif /* !_WIN32 && LINUX_NATIVE_AIO */ |
| 5993 |
} |
| 5994 |
|
| 5995 |
return (DB_SUCCESS); |
| 5996 |
} |
| 5997 |
|
| 5998 |
#ifdef LINUX_NATIVE_AIO |
| 5999 |
/** Initialise the Linux Native AIO interface */ |
| 6000 |
dberr_t AIO::init_linux_native_aio() { |
| 6001 |
/* Initialize the io_context array. One io_context |
| 6002 |
per segment in the array. */ |
| 6003 |
|
| 6004 |
ut_a(m_aio_ctx == nullptr); |
| 6005 |
|
| 6006 |
m_aio_ctx = static_cast<io_context **>(ut::zalloc_withkey( |
| 6007 |
UT_NEW_THIS_FILE_PSI_KEY, m_n_segments * sizeof(*m_aio_ctx))); |
| 6008 |
|
| 6009 |
if (m_aio_ctx == nullptr) { |
| 6010 |
return (DB_OUT_OF_MEMORY); |
| 6011 |
} |
| 6012 |
|
| 6013 |
io_context **ctx = m_aio_ctx; |
| 6014 |
ulint max_events = slots_per_segment(); |
| 6015 |
|
| 6016 |
for (ulint i = 0; i < m_n_segments; ++i, ++ctx) { |
| 6017 |
if (!linux_create_io_ctx(max_events, ctx)) { |
| 6018 |
/* If something bad happened during aio setup |
| 6019 |
we should call it a day and return right away. |
| 6020 |
We don't care about any leaks because a failure |
| 6021 |
to initialize the io subsystem means that the |
| 6022 |
server (or at least the innodb storage engine) |
| 6023 |
is not going to startup. */ |
| 6024 |
return (DB_IO_ERROR); |
| 6025 |
} |
| 6026 |
} |
| 6027 |
|
| 6028 |
return (DB_SUCCESS); |
| 6029 |
} |
| 6030 |
#endif /* LINUX_NATIVE_AIO */ |
| 6031 |
|
| 6032 |
/** Initialise the array */ |
| 6033 |
dberr_t AIO::init() { |
| 6034 |
ut_a(!m_slots.empty()); |
| 6035 |
|
| 6036 |
#ifdef _WIN32 |
| 6037 |
ut_a(m_handles == NULL); |
| 6038 |
|
| 6039 |
m_handles = |
| 6040 |
ut::new_withkey<Handles>(UT_NEW_THIS_FILE_PSI_KEY, m_slots.size()); |
| 6041 |
#endif /* _WIN32 */ |
| 6042 |
|
| 6043 |
if (srv_use_native_aio) { |
| 6044 |
#ifdef LINUX_NATIVE_AIO |
| 6045 |
dberr_t err = init_linux_native_aio(); |
| 6046 |
|
| 6047 |
if (err != DB_SUCCESS) { |
| 6048 |
return (err); |
| 6049 |
} |
| 6050 |
|
| 6051 |
#endif /* LINUX_NATIVE_AIO */ |
| 6052 |
} |
| 6053 |
|
| 6054 |
return (init_slots()); |
| 6055 |
} |
| 6056 |
|
| 6057 |
/** Creates an aio wait array. Note that we return NULL in case of failure. |
| 6058 |
We don't care about freeing memory here because we assume that a |
| 6059 |
failure will result in server refusing to start up. |
| 6060 |
@param[in] id Latch ID |
| 6061 |
@param[in] n maximum number of pending AIO operations |
| 6062 |
allowed; n must be divisible by m_n_segments |
| 6063 |
@param[in] n_segments number of segments in the AIO array |
| 6064 |
@return own: AIO array, NULL on failure */ |
| 6065 |
AIO *AIO::create(latch_id_t id, ulint n, ulint n_segments) { |
| 6066 |
ut_a(n_segments > 0); |
| 6067 |
|
| 6068 |
if ((n % n_segments)) { |
| 6069 |
ib::error(ER_IB_MSG_828) << "Maximum number of AIO operations must be " |
| 6070 |
<< "divisible by number of segments"; |
| 6071 |
|
| 6072 |
return (nullptr); |
| 6073 |
} |
| 6074 |
|
| 6075 |
AIO *array = |
| 6076 |
ut::new_withkey<AIO>(UT_NEW_THIS_FILE_PSI_KEY, id, n, n_segments); |
| 6077 |
|
| 6078 |
if (array != nullptr && array->init() != DB_SUCCESS) { |
| 6079 |
ut::delete_(array); |
| 6080 |
|
| 6081 |
array = nullptr; |
| 6082 |
} |
| 6083 |
|
| 6084 |
return (array); |
| 6085 |
} |
| 6086 |
|
| 6087 |
/** AIO destructor */ |
| 6088 |
AIO::~AIO() { |
| 6089 |
#ifdef _WIN32 |
| 6090 |
for (ulint i = 0; i < m_slots.size(); ++i) { |
| 6091 |
CloseHandle(m_slots[i].handle); |
| 6092 |
} |
| 6093 |
ut::delete_(m_handles); |
| 6094 |
#endif /* _WIN32 */ |
| 6095 |
|
| 6096 |
mutex_destroy(&m_mutex); |
| 6097 |
|
| 6098 |
os_event_destroy(m_not_full); |
| 6099 |
os_event_destroy(m_is_empty); |
| 6100 |
|
| 6101 |
#if defined(LINUX_NATIVE_AIO) |
| 6102 |
if (srv_use_native_aio) { |
| 6103 |
m_events.clear(); |
| 6104 |
ut::free(m_aio_ctx); |
| 6105 |
} |
| 6106 |
#endif /* LINUX_NATIVE_AIO */ |
| 6107 |
|
| 6108 |
m_slots.clear(); |
| 6109 |
} |
| 6110 |
|
| 6111 |
bool AIO::start(ulint n_per_seg, ulint n_readers, ulint n_writers) { |
| 6112 |
#if defined(LINUX_NATIVE_AIO) |
| 6113 |
/* Check if native aio is supported on this system and tmpfs */ |
| 6114 |
if (srv_use_native_aio && !is_linux_native_aio_supported()) { |
| 6115 |
ib::warn(ER_IB_MSG_829) << "Linux Native AIO disabled."; |
| 6116 |
|
| 6117 |
srv_use_native_aio = false; |
| 6118 |
} |
| 6119 |
#endif /* LINUX_NATIVE_AIO */ |
| 6120 |
|
| 6121 |
srv_reset_io_thread_op_info(); |
| 6122 |
|
| 6123 |
const auto n_extra = number_of_extra_threads(); |
| 6124 |
|
| 6125 |
size_t n_segments = 0; |
| 6126 |
|
| 6127 |
if (0 < n_extra) { |
| 6128 |
ut_ad(n_extra == 1); |
| 6129 |
s_ibuf = create(LATCH_ID_OS_AIO_IBUF_MUTEX, n_per_seg, 1); |
| 6130 |
|
| 6131 |
if (s_ibuf == nullptr) { |
| 6132 |
return false; |
| 6133 |
} |
| 6134 |
|
| 6135 |
srv_io_thread_function[n_segments++] = "insert buffer thread"; |
| 6136 |
|
| 6137 |
} else { |
| 6138 |
s_ibuf = nullptr; |
| 6139 |
} |
| 6140 |
ut_ad(n_extra == n_segments); |
| 6141 |
|
| 6142 |
s_reads = |
| 6143 |
create(LATCH_ID_OS_AIO_READ_MUTEX, n_readers * n_per_seg, n_readers); |
| 6144 |
if (s_reads == nullptr) { |
| 6145 |
return false; |
| 6146 |
} |
| 6147 |
|
| 6148 |
for (size_t i = 0; i < n_readers; ++i) { |
| 6149 |
ut_a(n_segments < SRV_MAX_N_IO_THREADS); |
| 6150 |
srv_io_thread_function[n_segments++] = "read thread"; |
| 6151 |
} |
| 6152 |
|
| 6153 |
s_writes = |
| 6154 |
create(LATCH_ID_OS_AIO_WRITE_MUTEX, n_writers * n_per_seg, n_writers); |
| 6155 |
|
| 6156 |
if (s_writes == nullptr) { |
| 6157 |
return false; |
| 6158 |
} |
| 6159 |
|
| 6160 |
for (size_t i = 0; i < n_writers; ++i) { |
| 6161 |
ut_a(n_segments < SRV_MAX_N_IO_THREADS); |
| 6162 |
srv_io_thread_function[n_segments++] = "write thread"; |
| 6163 |
} |
| 6164 |
|
| 6165 |
ut_ad(n_segments == n_extra + n_readers + n_writers); |
| 6166 |
|
| 6167 |
os_aio_n_segments = n_segments; |
| 6168 |
|
| 6169 |
os_aio_validate(); |
| 6170 |
|
| 6171 |
os_aio_segment_wait_events = static_cast<os_event_t *>( |
| 6172 |
ut::zalloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, |
| 6173 |
n_segments * sizeof *os_aio_segment_wait_events)); |
| 6174 |
|
| 6175 |
if (os_aio_segment_wait_events == nullptr) { |
| 6176 |
return false; |
| 6177 |
} |
| 6178 |
|
| 6179 |
for (size_t i = 0; i < n_segments; ++i) { |
| 6180 |
os_aio_segment_wait_events[i] = os_event_create(); |
| 6181 |
} |
| 6182 |
|
| 6183 |
os_last_printout = std::chrono::steady_clock::now(); |
| 6184 |
|
| 6185 |
return true; |
| 6186 |
} |
| 6187 |
|
| 6188 |
/** I/o-handler thread function. |
| 6189 |
@param[in] segment The AIO segment the thread will work on */ |
| 6190 |
static void io_handler_thread(ulint segment) { |
| 6191 |
while (srv_shutdown_state.load() != SRV_SHUTDOWN_EXIT_THREADS || |
| 6192 |
buf_flush_page_cleaner_is_active() || !os_aio_all_slots_free()) { |
| 6193 |
fil_aio_wait(segment); |
| 6194 |
} |
| 6195 |
} |
| 6196 |
|
| 6197 |
#ifdef UNIV_PFS_THREAD |
| 6198 |
mysql_pfs_key_t io_ibuf_thread_key; |
| 6199 |
mysql_pfs_key_t io_read_thread_key; |
| 6200 |
mysql_pfs_key_t io_write_thread_key; |
| 6201 |
#endif /* UNIV_PFS_THREAD */ |
| 6202 |
|
| 6203 |
void AIO::start_threads() { |
| 6204 |
ulint segment = 0; |
| 6205 |
const auto start = [&](mysql_pfs_key_t key, PSI_thread_seqnum seqnum) { |
| 6206 |
os_thread_create(key, seqnum, io_handler_thread, segment++).start(); |
| 6207 |
}; |
| 6208 |
/* For read only mode, we don't need ibuf I/O thread. */ |
| 6209 |
if (number_of_extra_threads()) { |
| 6210 |
ut_ad(s_ibuf != nullptr); |
| 6211 |
ut_ad(s_ibuf->get_n_segments() == 1); |
| 6212 |
start(io_ibuf_thread_key, 0); |
| 6213 |
} else { |
| 6214 |
ib::info(ER_IB_MSG_1128); |
| 6215 |
} |
| 6216 |
/* Numbering for ib_io_rd-NN starts with N=1. */ |
| 6217 |
for (PSI_thread_seqnum i = 1; i <= s_reads->get_n_segments(); ++i) { |
| 6218 |
start(io_read_thread_key, i); |
| 6219 |
} |
| 6220 |
/* Numbering for ib_io_wr-NN starts with N=1. */ |
| 6221 |
for (PSI_thread_seqnum i = 1; i <= s_writes->get_n_segments(); ++i) { |
| 6222 |
start(io_write_thread_key, i); |
| 6223 |
} |
| 6224 |
} |
| 6225 |
|
| 6226 |
/** Free the AIO arrays */ |
| 6227 |
void AIO::shutdown() { |
| 6228 |
ut::delete_(s_ibuf); |
| 6229 |
s_ibuf = nullptr; |
| 6230 |
|
| 6231 |
ut::delete_(s_writes); |
| 6232 |
s_writes = nullptr; |
| 6233 |
|
| 6234 |
ut::delete_(s_reads); |
| 6235 |
s_reads = nullptr; |
| 6236 |
} |
| 6237 |
#endif /* !UNIV_HOTBACKUP */ |
| 6238 |
#if !defined(NO_FALLOCATE) && defined(UNIV_LINUX) |
| 6239 |
|
| 6240 |
/** Max disk sector size */ |
| 6241 |
static const ulint MAX_SECTOR_SIZE = 4096; |
| 6242 |
|
| 6243 |
/** |
| 6244 |
Try and get the FusionIO sector size. */ |
| 6245 |
void os_fusionio_get_sector_size() { |
| 6246 |
if (srv_unix_file_flush_method == SRV_UNIX_O_DIRECT || |
| 6247 |
srv_unix_file_flush_method == SRV_UNIX_O_DIRECT_NO_FSYNC) { |
| 6248 |
ulint sector_size = UNIV_SECTOR_SIZE; |
| 6249 |
char *path = srv_data_home; |
| 6250 |
os_file_t check_file; |
| 6251 |
byte *block_ptr; |
| 6252 |
char current_dir[3]; |
| 6253 |
char *dir_end; |
| 6254 |
ulint dir_len; |
| 6255 |
ulint check_path_len; |
| 6256 |
char *check_file_name; |
| 6257 |
ssize_t ret; |
| 6258 |
|
| 6259 |
/* If the srv_data_home is empty, set the path to |
| 6260 |
current dir. */ |
| 6261 |
if (*path == 0) { |
| 6262 |
current_dir[0] = FN_CURLIB; |
| 6263 |
current_dir[1] = FN_LIBCHAR; |
| 6264 |
current_dir[2] = 0; |
| 6265 |
path = current_dir; |
| 6266 |
} |
| 6267 |
|
| 6268 |
/* Get the path of data file */ |
| 6269 |
dir_end = strrchr(path, OS_PATH_SEPARATOR); |
| 6270 |
dir_len = dir_end ? dir_end - path : strlen(path); |
| 6271 |
|
| 6272 |
/* allocate a new path and move the directory path to it. */ |
| 6273 |
check_path_len = dir_len + sizeof "/check_sector_size"; |
| 6274 |
check_file_name = static_cast<char *>( |
| 6275 |
ut::zalloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, check_path_len)); |
| 6276 |
memcpy(check_file_name, path, dir_len); |
| 6277 |
|
| 6278 |
/* Construct a check file name. */ |
| 6279 |
strcat(check_file_name + dir_len, "/check_sector_size"); |
| 6280 |
|
| 6281 |
/* Create a tmp file for checking sector size. */ |
| 6282 |
check_file = ::open(check_file_name, |
| 6283 |
O_CREAT | O_TRUNC | O_WRONLY | O_DIRECT, S_IRWXU); |
| 6284 |
|
| 6285 |
if (check_file == -1) { |
| 6286 |
ib::error(ER_IB_MSG_830) |
| 6287 |
<< "Failed to create check sector file, errno:" << errno |
| 6288 |
<< " Please confirm O_DIRECT is" |
| 6289 |
<< " supported and remove the file " << check_file_name |
| 6290 |
<< " if it exists."; |
| 6291 |
ut::free(check_file_name); |
| 6292 |
errno = 0; |
| 6293 |
return; |
| 6294 |
} |
| 6295 |
|
| 6296 |
/* Try to write the file with different sector size |
| 6297 |
alignment. */ |
| 6298 |
alignas(MAX_SECTOR_SIZE) byte data[MAX_SECTOR_SIZE] = {0}; |
| 6299 |
|
| 6300 |
while (sector_size <= MAX_SECTOR_SIZE) { |
| 6301 |
block_ptr = static_cast<byte *>(ut_align(&data, sector_size)); |
| 6302 |
ret = pwrite(check_file, block_ptr, sector_size, 0); |
| 6303 |
if (ret > 0 && (ulint)ret == sector_size) { |
| 6304 |
break; |
| 6305 |
} |
| 6306 |
sector_size *= 2; |
| 6307 |
} |
| 6308 |
|
| 6309 |
/* The sector size should <= MAX_SECTOR_SIZE. */ |
| 6310 |
ut_ad(sector_size <= MAX_SECTOR_SIZE); |
| 6311 |
|
| 6312 |
close(check_file); |
| 6313 |
unlink(check_file_name); |
| 6314 |
|
| 6315 |
ut::free(check_file_name); |
| 6316 |
errno = 0; |
| 6317 |
|
| 6318 |
os_io_ptr_align = sector_size; |
| 6319 |
} |
| 6320 |
} |
| 6321 |
#endif /* !NO_FALLOCATE && UNIV_LINUX */ |
| 6322 |
|
| 6323 |
/** Creates and initializes block_cache. Creates array of MAX_BLOCKS |
| 6324 |
and allocates the memory in each block to hold BUFFER_BLOCK_SIZE |
| 6325 |
of data. |
| 6326 |
|
| 6327 |
This function is called by InnoDB during srv_start(). |
| 6328 |
It is also called by MEB while applying the redo logs on TDE tablespaces, |
| 6329 |
the "Blocks" allocated in this block_cache are used to hold the decrypted |
| 6330 |
page data. */ |
| 6331 |
void os_create_block_cache() { |
| 6332 |
ut_a(block_cache == nullptr); |
| 6333 |
|
| 6334 |
block_cache = ut::new_withkey<Blocks>(UT_NEW_THIS_FILE_PSI_KEY, MAX_BLOCKS); |
| 6335 |
|
| 6336 |
for (Blocks::iterator it = block_cache->begin(); it != block_cache->end(); |
| 6337 |
++it) { |
| 6338 |
ut_a(!it->m_in_use); |
| 6339 |
ut_a(it->m_ptr == nullptr); |
| 6340 |
|
| 6341 |
/* Allocate double of max page size memory, since |
| 6342 |
compress could generate more bytes than original |
| 6343 |
data. */ |
| 6344 |
it->m_ptr = static_cast<byte *>( |
| 6345 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, BUFFER_BLOCK_SIZE)); |
| 6346 |
|
| 6347 |
ut_a(it->m_ptr != nullptr); |
| 6348 |
} |
| 6349 |
} |
| 6350 |
|
| 6351 |
#ifdef UNIV_HOTBACKUP |
| 6352 |
/** De-allocates block cache at InnoDB shutdown. */ |
| 6353 |
void meb_free_block_cache() { |
| 6354 |
if (block_cache == nullptr) { |
| 6355 |
return; |
| 6356 |
} |
| 6357 |
|
| 6358 |
for (Blocks::iterator it = block_cache->begin(); it != block_cache->end(); |
| 6359 |
++it) { |
| 6360 |
ut_a(!it->m_in_use); |
| 6361 |
ut::free(it->m_ptr); |
| 6362 |
} |
| 6363 |
|
| 6364 |
ut::delete_(block_cache); |
| 6365 |
|
| 6366 |
block_cache = nullptr; |
| 6367 |
} |
| 6368 |
|
| 6369 |
#else /* !UNIV_HOTBACKUP */ |
| 6370 |
|
| 6371 |
bool os_aio_init(ulint n_readers, ulint n_writers) { |
| 6372 |
/* Maximum number of pending aio operations allowed per segment */ |
| 6373 |
ulint limit = 8 * OS_AIO_N_PENDING_IOS_PER_THREAD; |
| 6374 |
|
| 6375 |
#ifdef _WIN32 |
| 6376 |
if (srv_use_native_aio) { |
| 6377 |
limit = SRV_N_PENDING_IOS_PER_THREAD; |
| 6378 |
} |
| 6379 |
#endif /* _WIN32 */ |
| 6380 |
|
| 6381 |
/* Get sector size for DIRECT_IO. In this case, we need to |
| 6382 |
know the sector size for aligning the write buffer. */ |
| 6383 |
#if !defined(NO_FALLOCATE) && defined(UNIV_LINUX) |
| 6384 |
os_fusionio_get_sector_size(); |
| 6385 |
#endif /* !NO_FALLOCATE && UNIV_LINUX */ |
| 6386 |
|
| 6387 |
return (AIO::start(limit, n_readers, n_writers)); |
| 6388 |
} |
| 6389 |
|
| 6390 |
void os_aio_start_threads() { AIO::start_threads(); } |
| 6391 |
|
| 6392 |
/** Frees the asynchronous io system. */ |
| 6393 |
void os_aio_free() { |
| 6394 |
AIO::shutdown(); |
| 6395 |
|
| 6396 |
for (ulint i = 0; i < os_aio_n_segments; i++) { |
| 6397 |
os_event_destroy(os_aio_segment_wait_events[i]); |
| 6398 |
} |
| 6399 |
|
| 6400 |
ut::free(os_aio_segment_wait_events); |
| 6401 |
os_aio_segment_wait_events = nullptr; |
| 6402 |
os_aio_n_segments = 0; |
| 6403 |
|
| 6404 |
for (Blocks::iterator it = block_cache->begin(); it != block_cache->end(); |
| 6405 |
++it) { |
| 6406 |
ut_a(!it->m_in_use); |
| 6407 |
ut::free(it->m_ptr); |
| 6408 |
} |
| 6409 |
|
| 6410 |
ut::delete_(block_cache); |
| 6411 |
|
| 6412 |
block_cache = nullptr; |
| 6413 |
} |
| 6414 |
|
| 6415 |
/** Wakes up all async i/o threads so that they know to exit themselves in |
| 6416 |
shutdown. */ |
| 6417 |
void os_aio_wake_all_threads_at_shutdown() { |
| 6418 |
#ifdef _WIN32 |
| 6419 |
|
| 6420 |
AIO::wake_at_shutdown(); |
| 6421 |
|
| 6422 |
#elif defined(LINUX_NATIVE_AIO) |
| 6423 |
|
| 6424 |
/* When using native AIO interface the io helper threads |
| 6425 |
wait on io_getevents with a timeout value of 500ms. At |
| 6426 |
each wake up these threads check the server status. |
| 6427 |
No need to do anything to wake them up. */ |
| 6428 |
|
| 6429 |
if (srv_use_native_aio) { |
| 6430 |
return; |
| 6431 |
} |
| 6432 |
|
| 6433 |
#endif /* !_WIN32 && LINUX_NATIVE_AIO */ |
| 6434 |
|
| 6435 |
/* Fall through to simulated AIO handler wakeup if we are |
| 6436 |
not using native AIO. */ |
| 6437 |
|
| 6438 |
/* This loop wakes up all simulated ai/o threads */ |
| 6439 |
|
| 6440 |
for (ulint i = 0; i < os_aio_n_segments; ++i) { |
| 6441 |
os_event_set(os_aio_segment_wait_events[i]); |
| 6442 |
} |
| 6443 |
} |
| 6444 |
|
| 6445 |
/** Waits until there are no pending writes in AIO::s_writes. There can |
| 6446 |
be other, synchronous, pending writes. */ |
| 6447 |
void os_aio_wait_until_no_pending_writes() { |
| 6448 |
AIO::wait_until_no_pending_writes(); |
| 6449 |
} |
| 6450 |
|
| 6451 |
/** Calculates segment number for a slot. |
| 6452 |
@param[in] array AIO wait array |
| 6453 |
@param[in] slot slot in this array |
| 6454 |
@return segment number (which is the number used by, for example, |
| 6455 |
I/O handler threads) */ |
| 6456 |
ulint AIO::get_segment_no_from_slot(const AIO *array, const Slot *slot) { |
| 6457 |
if (array == s_ibuf) { |
| 6458 |
ut_ad(s_ibuf->get_n_segments() == 1); |
| 6459 |
ut_ad(s_ibuf->get_n_segments() == number_of_extra_threads()); |
| 6460 |
return IO_IBUF_SEGMENT; |
| 6461 |
} |
| 6462 |
auto earlier_segments = number_of_extra_threads(); |
| 6463 |
|
| 6464 |
if (array == s_reads) { |
| 6465 |
return earlier_segments + slot->pos / s_reads->slots_per_segment(); |
| 6466 |
} |
| 6467 |
earlier_segments += s_reads->m_n_segments; |
| 6468 |
|
| 6469 |
ut_a(array == s_writes); |
| 6470 |
return earlier_segments + slot->pos / s_writes->slots_per_segment(); |
| 6471 |
} |
| 6472 |
|
| 6473 |
Slot *AIO::reserve_slot(IORequest &type, fil_node_t *m1, void *m2, |
| 6474 |
pfs_os_file_t file, const char *name, void *buf, |
| 6475 |
os_offset_t offset, ulint len, |
| 6476 |
const file::Block *e_block) { |
| 6477 |
ut_a(!type.is_log()); |
| 6478 |
#ifdef _WIN32 |
| 6479 |
ut_a((len & 0xFFFFFFFFUL) == len); |
| 6480 |
#endif /* _WIN32 */ |
| 6481 |
|
| 6482 |
/* No need of a mutex. Only reading constant fields */ |
| 6483 |
ut_ad(type.validate()); |
| 6484 |
|
| 6485 |
const auto slots_per_seg = slots_per_segment(); |
| 6486 |
|
| 6487 |
for (;;) { |
| 6488 |
acquire(); |
| 6489 |
|
| 6490 |
if (m_n_reserved != m_slots.size()) { |
| 6491 |
break; |
| 6492 |
} |
| 6493 |
|
| 6494 |
release(); |
| 6495 |
|
| 6496 |
if (!srv_use_native_aio) { |
| 6497 |
/* If the handler threads are suspended, |
| 6498 |
wake them so that we get more slots */ |
| 6499 |
|
| 6500 |
os_aio_simulated_wake_handler_threads(); |
| 6501 |
} |
| 6502 |
|
| 6503 |
os_event_wait(m_not_full); |
| 6504 |
} |
| 6505 |
|
| 6506 |
/* We will check first, next(first), next(next(first))... which should be a |
| 6507 |
permutation of values 0,..,m_slots.size()-1.*/ |
| 6508 |
auto find_slot = [this](size_t first, auto next) { |
| 6509 |
size_t i = first; |
| 6510 |
for (size_t counter = 0; counter < m_slots.size(); ++counter) { |
| 6511 |
if (!at(i)->is_reserved) { |
| 6512 |
return i; |
| 6513 |
} |
| 6514 |
i = next(i); |
| 6515 |
} |
| 6516 |
/* We know that there is a free slot, because m_n_reserved != m_slots.size() |
| 6517 |
was checked under the mutex protection, which we still hold. Additionally |
| 6518 |
the permutation generated by next() should visit all slots. If we checked |
| 6519 |
m_slots.size() elements of the sequence and not found a free slot, then it |
| 6520 |
was not a permutation, or there was no free slot.*/ |
| 6521 |
ut_error; |
| 6522 |
}; |
| 6523 |
size_t free_index; |
| 6524 |
if (srv_use_native_aio) { |
| 6525 |
/* We assume the m_slots.size() cannot be changed during runtime. */ |
| 6526 |
ut_a(m_last_slot_used < m_slots.size()); |
| 6527 |
/* We iterate through slots starting with the last used and then trying next |
| 6528 |
ones from consecutive segments to balance the incoming requests evenly |
| 6529 |
between the AIO threads. */ |
| 6530 |
free_index = find_slot(m_last_slot_used, [&](size_t i) { |
| 6531 |
i += slots_per_seg; |
| 6532 |
if (i >= m_slots.size()) { |
| 6533 |
/* Start again from the first segment, this time trying next slot in |
| 6534 |
each segment. If we checked the last slot in segment, start with |
| 6535 |
first slot. */ |
| 6536 |
i = (i + 1) % slots_per_seg; |
| 6537 |
} |
| 6538 |
return i; |
| 6539 |
}); |
| 6540 |
m_last_slot_used = free_index; |
| 6541 |
} else { |
| 6542 |
/* We attempt to keep adjacent blocks in the same local |
| 6543 |
segment. This can help in merging IO requests when we are |
| 6544 |
doing simulated AIO */ |
| 6545 |
const size_t local_seg = |
| 6546 |
(offset >> (UNIV_PAGE_SIZE_SHIFT + 6)) % m_n_segments; |
| 6547 |
/* We start our search for an available slot from our preferred |
| 6548 |
local segment and do a full scan of the array. */ |
| 6549 |
free_index = find_slot(local_seg * slots_per_seg, |
| 6550 |
[&](size_t i) { return (i + 1) % m_slots.size(); }); |
| 6551 |
} |
| 6552 |
Slot *const slot = at(free_index); |
| 6553 |
ut_a(slot->is_reserved == false); |
| 6554 |
|
| 6555 |
++m_n_reserved; |
| 6556 |
|
| 6557 |
if (m_n_reserved == 1) { |
| 6558 |
os_event_reset(m_is_empty); |
| 6559 |
} |
| 6560 |
|
| 6561 |
if (m_n_reserved == m_slots.size()) { |
| 6562 |
os_event_reset(m_not_full); |
| 6563 |
} |
| 6564 |
|
| 6565 |
slot->is_reserved = true; |
| 6566 |
slot->reservation_time = std::chrono::steady_clock::now(); |
| 6567 |
slot->m1 = m1; |
| 6568 |
slot->m2 = m2; |
| 6569 |
slot->file = file; |
| 6570 |
slot->name = name; |
| 6571 |
#ifdef _WIN32 |
| 6572 |
slot->len = static_cast<DWORD>(len); |
| 6573 |
#else /* !_WIN32 */ |
| 6574 |
slot->len = static_cast<ulint>(len); |
| 6575 |
#endif /* !_WIN32 */ |
| 6576 |
slot->type = type; |
| 6577 |
slot->buf = static_cast<byte *>(buf); |
| 6578 |
slot->ptr = slot->buf; |
| 6579 |
slot->offset = offset; |
| 6580 |
slot->err = DB_SUCCESS; |
| 6581 |
if (type.is_read()) { |
| 6582 |
/* The original size must not be specified for reads. */ |
| 6583 |
ut_ad(!slot->type.get_original_size()); |
| 6584 |
slot->type.set_original_size(static_cast<uint32_t>(len)); |
| 6585 |
} else if (type.is_write()) { |
| 6586 |
/* The original size may be supplied by user in case the punch hole is |
| 6587 |
requested, otherwise use the IO length specified. */ |
| 6588 |
if (slot->type.get_original_size() == 0) { |
| 6589 |
slot->type.set_original_size(static_cast<uint32_t>(len)); |
| 6590 |
} |
| 6591 |
} |
| 6592 |
slot->io_already_done = false; |
| 6593 |
slot->buf_block = nullptr; |
| 6594 |
|
| 6595 |
if (!srv_use_native_aio) { |
| 6596 |
slot->buf_block = const_cast<file::Block *>(e_block); |
| 6597 |
} |
| 6598 |
|
| 6599 |
if (srv_use_native_aio && offset > 0 && type.is_write() && |
| 6600 |
type.is_compressed()) { |
| 6601 |
ulint compressed_len = len; |
| 6602 |
|
| 6603 |
ut_ad(!type.is_log()); |
| 6604 |
|
| 6605 |
release(); |
| 6606 |
|
| 6607 |
void *src_buf = slot->buf; |
| 6608 |
|
| 6609 |
if (e_block == nullptr) { |
| 6610 |
slot->buf_block = os_file_compress_page(type, src_buf, &compressed_len); |
| 6611 |
} |
| 6612 |
|
| 6613 |
slot->buf = static_cast<byte *>(src_buf); |
| 6614 |
slot->ptr = slot->buf; |
| 6615 |
#ifdef _WIN32 |
| 6616 |
slot->len = static_cast<DWORD>(compressed_len); |
| 6617 |
#else /* !_WIN32 */ |
| 6618 |
slot->len = static_cast<ulint>(compressed_len); |
| 6619 |
#endif /* !_WIN32 */ |
| 6620 |
slot->skip_punch_hole = !type.punch_hole(); |
| 6621 |
|
| 6622 |
acquire(); |
| 6623 |
} |
| 6624 |
|
| 6625 |
/* We do encryption after compression, since if we do encryption |
| 6626 |
before compression, the encrypted data will cause compression fail |
| 6627 |
or low compression rate. */ |
| 6628 |
if (srv_use_native_aio && offset > 0 && type.is_write() && |
| 6629 |
(type.is_encrypted() || e_block != nullptr)) { |
| 6630 |
file::Block *encrypted_block = nullptr; |
| 6631 |
|
| 6632 |
release(); |
| 6633 |
|
| 6634 |
void *src_buf = slot->buf; |
| 6635 |
ut_a(!type.is_log()); |
| 6636 |
if (e_block == nullptr) { |
| 6637 |
encrypted_block = os_file_encrypt_page(type, src_buf, slot->len); |
| 6638 |
} else { |
| 6639 |
encrypted_block = const_cast<file::Block *>(e_block); |
| 6640 |
} |
| 6641 |
|
| 6642 |
if (slot->buf_block != nullptr) { |
| 6643 |
os_free_block(slot->buf_block); |
| 6644 |
} |
| 6645 |
|
| 6646 |
slot->buf_block = encrypted_block; |
| 6647 |
|
| 6648 |
slot->buf = static_cast<byte *>(src_buf); |
| 6649 |
|
| 6650 |
slot->ptr = slot->buf; |
| 6651 |
|
| 6652 |
if (encrypted_block != nullptr) { |
| 6653 |
#ifdef _WIN32 |
| 6654 |
slot->len = static_cast<DWORD>(encrypted_block->m_size); |
| 6655 |
#else /* !_WIN32 */ |
| 6656 |
slot->len = static_cast<ulint>(encrypted_block->m_size); |
| 6657 |
#endif /* !_WIN32 */ |
| 6658 |
} |
| 6659 |
|
| 6660 |
acquire(); |
| 6661 |
} |
| 6662 |
|
| 6663 |
#ifdef _WIN32 |
| 6664 |
{ |
| 6665 |
OVERLAPPED *control; |
| 6666 |
|
| 6667 |
control = &slot->control; |
| 6668 |
control->Offset = (DWORD)offset & 0xFFFFFFFF; |
| 6669 |
control->OffsetHigh = (DWORD)(offset >> 32); |
| 6670 |
|
| 6671 |
ResetEvent(slot->handle); |
| 6672 |
} |
| 6673 |
#elif defined(LINUX_NATIVE_AIO) |
| 6674 |
|
| 6675 |
/* If we are not using native AIO skip this part. */ |
| 6676 |
if (srv_use_native_aio) { |
| 6677 |
off_t aio_offset; |
| 6678 |
|
| 6679 |
/* Check if we are dealing with 64 bit arch. |
| 6680 |
If not then make sure that offset fits in 32 bits. */ |
| 6681 |
aio_offset = (off_t)offset; |
| 6682 |
|
| 6683 |
ut_a(sizeof(aio_offset) >= sizeof(offset) || |
| 6684 |
((os_offset_t)aio_offset) == offset); |
| 6685 |
|
| 6686 |
auto iocb = &slot->control; |
| 6687 |
|
| 6688 |
if (type.is_read()) { |
| 6689 |
io_prep_pread(iocb, file.m_file, slot->ptr, slot->len, aio_offset); |
| 6690 |
} else { |
| 6691 |
ut_ad(type.is_write()); |
| 6692 |
io_prep_pwrite(iocb, file.m_file, slot->ptr, slot->len, aio_offset); |
| 6693 |
} |
| 6694 |
|
| 6695 |
iocb->data = slot; |
| 6696 |
|
| 6697 |
slot->n_bytes = 0; |
| 6698 |
slot->ret = 0; |
| 6699 |
} |
| 6700 |
#endif /* !_WIN32 && LINUX_NATIVE_AIO */ |
| 6701 |
|
| 6702 |
release(); |
| 6703 |
|
| 6704 |
return (slot); |
| 6705 |
} |
| 6706 |
|
| 6707 |
/** Wakes up a simulated AIO I/O handler thread if it has something to do. |
| 6708 |
@param[in] global_segment The number of the segment in the AIO arrays */ |
| 6709 |
void AIO::wake_simulated_handler_thread(ulint global_segment) { |
| 6710 |
ut_ad(!srv_use_native_aio); |
| 6711 |
|
| 6712 |
AIO *array{}; |
| 6713 |
|
| 6714 |
auto segment = get_array_and_local_segment(array, global_segment); |
| 6715 |
|
| 6716 |
array->wake_simulated_handler_thread(global_segment, segment); |
| 6717 |
} |
| 6718 |
|
| 6719 |
/** Wakes up a simulated AIO I/O-handler thread if it has something to do |
| 6720 |
for a local segment in the AIO array. |
| 6721 |
@param[in] global_segment The number of the segment in the AIO arrays |
| 6722 |
@param[in] segment The local segment in the AIO array */ |
| 6723 |
void AIO::wake_simulated_handler_thread(ulint global_segment, ulint segment) { |
| 6724 |
ut_ad(!srv_use_native_aio); |
| 6725 |
|
| 6726 |
ulint n = slots_per_segment(); |
| 6727 |
ulint offset = segment * n; |
| 6728 |
|
| 6729 |
/* Look through n slots after the segment * n'th slot */ |
| 6730 |
|
| 6731 |
acquire(); |
| 6732 |
|
| 6733 |
const Slot *slot = at(offset); |
| 6734 |
|
| 6735 |
for (ulint i = 0; i < n; ++i, ++slot) { |
| 6736 |
if (slot->is_reserved) { |
| 6737 |
/* Found an i/o request */ |
| 6738 |
|
| 6739 |
release(); |
| 6740 |
|
| 6741 |
os_event_set(os_aio_segment_wait_events[global_segment]); |
| 6742 |
|
| 6743 |
return; |
| 6744 |
} |
| 6745 |
} |
| 6746 |
|
| 6747 |
release(); |
| 6748 |
} |
| 6749 |
|
| 6750 |
/** Wakes up simulated aio i/o-handler threads if they have something to do. */ |
| 6751 |
void os_aio_simulated_wake_handler_threads() { |
| 6752 |
if (srv_use_native_aio) { |
| 6753 |
/* We do not use simulated aio: do nothing */ |
| 6754 |
|
| 6755 |
return; |
| 6756 |
} |
| 6757 |
|
| 6758 |
os_aio_recommend_sleep_for_read_threads = false; |
| 6759 |
|
| 6760 |
for (ulint i = 0; i < os_aio_n_segments; ++i) { |
| 6761 |
AIO::wake_simulated_handler_thread(i); |
| 6762 |
} |
| 6763 |
} |
| 6764 |
|
| 6765 |
/** Select the IO slot array |
| 6766 |
@param[in,out] type Type of IO, READ or WRITE |
| 6767 |
@param[in] read_only true if running in read-only mode |
| 6768 |
@param[in] aio_mode IO mode |
| 6769 |
@return slot array or NULL if invalid mode specified */ |
| 6770 |
AIO *AIO::select_slot_array(IORequest &type, bool read_only, |
| 6771 |
AIO_mode aio_mode) { |
| 6772 |
AIO *array; |
| 6773 |
|
| 6774 |
ut_ad(type.validate()); |
| 6775 |
|
| 6776 |
switch (aio_mode) { |
| 6777 |
case AIO_mode::NORMAL: |
| 6778 |
array = type.is_read() ? AIO::s_reads : AIO::s_writes; |
| 6779 |
break; |
| 6780 |
|
| 6781 |
case AIO_mode::IBUF: |
| 6782 |
ut_ad(type.is_read()); |
| 6783 |
|
| 6784 |
/* Reduce probability of deadlock bugs in connection with ibuf: |
| 6785 |
do not let the ibuf i/o handler sleep */ |
| 6786 |
|
| 6787 |
type.clear_do_not_wake(); |
| 6788 |
|
| 6789 |
array = read_only ? AIO::s_reads : AIO::s_ibuf; |
| 6790 |
break; |
| 6791 |
|
| 6792 |
default: |
| 6793 |
ut_error; |
| 6794 |
} |
| 6795 |
|
| 6796 |
return (array); |
| 6797 |
} |
| 6798 |
|
| 6799 |
#ifdef _WIN32 |
| 6800 |
|
| 6801 |
static dberr_t os_aio_windows_handler(ulint segment, fil_node_t **m1, void **m2, |
| 6802 |
IORequest *type) { |
| 6803 |
Slot *slot = nullptr; |
| 6804 |
AIO *array{}; |
| 6805 |
|
| 6806 |
const auto segment_offset = AIO::get_array_and_local_segment(array, segment); |
| 6807 |
|
| 6808 |
dberr_t err = DB_ERROR_UNSET; |
| 6809 |
while (err == DB_ERROR_UNSET) { |
| 6810 |
/* NOTE! We only access constant fields in AIO's arrays - number of slots |
| 6811 |
and array of event handles, both initialized on startup. Therefore |
| 6812 |
we do not have to acquire the protecting mutex yet. */ |
| 6813 |
|
| 6814 |
ut_ad(os_aio_validate_skip()); |
| 6815 |
|
| 6816 |
srv_set_io_thread_op_info(segment, "wait Windows aio"); |
| 6817 |
|
| 6818 |
const auto pos = |
| 6819 |
WaitForMultipleObjects((DWORD)array->slots_per_segment(), |
| 6820 |
array->handles(segment_offset), FALSE, INFINITE); |
| 6821 |
|
| 6822 |
array->acquire(); |
| 6823 |
|
| 6824 |
if (srv_shutdown_state.load() == SRV_SHUTDOWN_EXIT_THREADS && |
| 6825 |
array->is_empty() && !buf_flush_page_cleaner_is_active()) { |
| 6826 |
*m1 = NULL; |
| 6827 |
*m2 = NULL; |
| 6828 |
|
| 6829 |
array->release(); |
| 6830 |
|
| 6831 |
return (DB_SUCCESS); |
| 6832 |
} |
| 6833 |
|
| 6834 |
ulint n = array->slots_per_segment(); |
| 6835 |
|
| 6836 |
ut_a(pos >= WAIT_OBJECT_0 && pos <= WAIT_OBJECT_0 + n); |
| 6837 |
|
| 6838 |
slot = array->at(pos + segment_offset * n); |
| 6839 |
|
| 6840 |
ut_a(slot->is_reserved); |
| 6841 |
|
| 6842 |
srv_set_io_thread_op_info(segment, "get windows aio return value"); |
| 6843 |
|
| 6844 |
BOOL ret = GetOverlappedResult(slot->file.m_file, &slot->control, |
| 6845 |
&slot->n_bytes, TRUE); |
| 6846 |
|
| 6847 |
*m1 = slot->m1; |
| 6848 |
*m2 = slot->m2; |
| 6849 |
|
| 6850 |
*type = slot->type; |
| 6851 |
|
| 6852 |
bool retry = false; |
| 6853 |
|
| 6854 |
/* We will finish the outer loop if the err is not reset to DB_ERROR_UNSET. |
| 6855 |
*/ |
| 6856 |
err = DB_IO_ERROR; |
| 6857 |
if (ret && slot->n_bytes == slot->len) { |
| 6858 |
err = DB_SUCCESS; |
| 6859 |
} else if (os_file_handle_error(slot->name, "Windows aio")) { |
| 6860 |
retry = true; |
| 6861 |
} |
| 6862 |
|
| 6863 |
array->release(); |
| 6864 |
|
| 6865 |
if (retry) { |
| 6866 |
/* Retry failed read/write async operation. |
| 6867 |
No need to hold array->m_mutex. */ |
| 6868 |
|
| 6869 |
#ifdef UNIV_PFS_IO |
| 6870 |
/* This read/write does not go through os_file_read |
| 6871 |
and os_file_write APIs, need to register with |
| 6872 |
performance schema explicitly here. */ |
| 6873 |
struct PSI_file_locker *locker = NULL; |
| 6874 |
PSI_file_locker_state state; |
| 6875 |
register_pfs_file_io_begin( |
| 6876 |
&state, locker, slot->file, slot->len, |
| 6877 |
slot->type.is_write() ? PSI_FILE_WRITE : PSI_FILE_READ, |
| 6878 |
UT_LOCATION_HERE); |
| 6879 |
#endif /* UNIV_PFS_IO */ |
| 6880 |
|
| 6881 |
if (slot->type.is_read()) { |
| 6882 |
ret = ReadFile(slot->file.m_file, slot->ptr, slot->len, &slot->n_bytes, |
| 6883 |
&slot->control); |
| 6884 |
} else { |
| 6885 |
ret = WriteFile(slot->file.m_file, slot->ptr, slot->len, &slot->n_bytes, |
| 6886 |
&slot->control); |
| 6887 |
} |
| 6888 |
|
| 6889 |
#ifdef UNIV_PFS_IO |
| 6890 |
register_pfs_file_io_end(locker, slot->len); |
| 6891 |
#endif /* UNIV_PFS_IO */ |
| 6892 |
|
| 6893 |
if ((ret && slot->len == slot->n_bytes) || |
| 6894 |
(!ret && GetLastError() == ERROR_IO_PENDING)) { |
| 6895 |
/* The overlapped operation was queued successfully. We will now retry |
| 6896 |
the wait to get any next AIO completion. */ |
| 6897 |
err = DB_ERROR_UNSET; |
| 6898 |
} |
| 6899 |
} |
| 6900 |
} |
| 6901 |
|
| 6902 |
if (err == DB_SUCCESS) { |
| 6903 |
ut_ad(!slot->type.is_log()); |
| 6904 |
/** If write of the page is compressed (compression is enabled, it is not |
| 6905 |
the first page, it is not a redolog, not a doublewrite buffer) and punch |
| 6906 |
holes are enabled, call AIOHandler::io_complete to check if hole punching is |
| 6907 |
needed. |
| 6908 |
Keep in sync with LinuxAIOHandler::collect(). */ |
| 6909 |
if (slot->offset > 0 && !slot->skip_punch_hole && |
| 6910 |
slot->type.is_compression_enabled() && !slot->type.is_log() && |
| 6911 |
slot->type.is_write() && slot->type.is_compressed() && |
| 6912 |
slot->type.punch_hole() && !slot->type.is_dblwr()) { |
| 6913 |
slot->err = AIOHandler::io_complete(slot); |
| 6914 |
} else { |
| 6915 |
slot->err = DB_SUCCESS; |
| 6916 |
} |
| 6917 |
err = AIOHandler::post_io_processing(slot); |
| 6918 |
} |
| 6919 |
|
| 6920 |
array->release_with_mutex(slot); |
| 6921 |
|
| 6922 |
return (err); |
| 6923 |
} |
| 6924 |
#endif /* _WIN32 */ |
| 6925 |
|
| 6926 |
dberr_t os_aio_func(IORequest &type, AIO_mode aio_mode, const char *name, |
| 6927 |
pfs_os_file_t file, void *buf, os_offset_t offset, ulint n, |
| 6928 |
bool read_only, fil_node_t *m1, void *m2) { |
| 6929 |
ut_a(!type.is_log()); |
| 6930 |
#ifdef _WIN32 |
| 6931 |
BOOL ret = TRUE; |
| 6932 |
#endif /* _WIN32 */ |
| 6933 |
|
| 6934 |
const file::Block *e_block = type.get_encrypted_block(); |
| 6935 |
|
| 6936 |
#ifdef UNIV_DEBUG |
| 6937 |
if (type.is_write() && e_block != nullptr) { |
| 6938 |
ut_ad(os_block_get_frame(e_block) == buf); |
| 6939 |
} |
| 6940 |
#endif /* UNIV_DEBUG */ |
| 6941 |
|
| 6942 |
ut_ad(n > 0); |
| 6943 |
ut_ad((n % OS_FILE_LOG_BLOCK_SIZE) == 0); |
| 6944 |
ut_ad((offset % OS_FILE_LOG_BLOCK_SIZE) == 0); |
| 6945 |
ut_ad(os_aio_validate_skip()); |
| 6946 |
|
| 6947 |
#ifdef _WIN32 |
| 6948 |
ut_ad((n & 0xFFFFFFFFUL) == n); |
| 6949 |
#endif /* _WIN32 */ |
| 6950 |
|
| 6951 |
if (aio_mode == AIO_mode::SYNC) { |
| 6952 |
/* This is actually an ordinary synchronous read or write: |
| 6953 |
no need to use an i/o-handler thread. NOTE that if we use |
| 6954 |
Windows "async" overlapped i/o, Windows does not allow us to use |
| 6955 |
ordinary synchronous operations etc. on the same file. The os_file_read() |
| 6956 |
and os_file_write() are handling this case correctly. |
| 6957 |
Also note that the Performance Schema instrumentation has |
| 6958 |
been performed by current os_aio_func()'s wrapper function |
| 6959 |
pfs_os_aio_func(). So we would no longer need to call |
| 6960 |
Performance Schema instrumented os_file_read() and |
| 6961 |
os_file_write(). Instead, we should use os_file_read_func() |
| 6962 |
and os_file_write_func() */ |
| 6963 |
|
| 6964 |
if (type.is_read()) { |
| 6965 |
return (os_file_read_func(type, name, file.m_file, buf, offset, n)); |
| 6966 |
} |
| 6967 |
|
| 6968 |
ut_ad(type.is_write()); |
| 6969 |
return (os_file_write_func(type, name, file.m_file, buf, offset, n)); |
| 6970 |
} |
| 6971 |
|
| 6972 |
try_again: |
| 6973 |
|
| 6974 |
auto array = AIO::select_slot_array(type, read_only, aio_mode); |
| 6975 |
|
| 6976 |
auto slot = |
| 6977 |
array->reserve_slot(type, m1, m2, file, name, buf, offset, n, e_block); |
| 6978 |
|
| 6979 |
if (type.is_read()) { |
| 6980 |
if (srv_use_native_aio) { |
| 6981 |
++os_n_file_reads; |
| 6982 |
|
| 6983 |
os_bytes_read_since_printout += n; |
| 6984 |
#ifdef _WIN32 |
| 6985 |
ret = ReadFile(file.m_file, slot->ptr, slot->len, &slot->n_bytes, |
| 6986 |
&slot->control); |
| 6987 |
#elif defined(LINUX_NATIVE_AIO) |
| 6988 |
if (!array->linux_dispatch(slot)) { |
| 6989 |
goto err_exit; |
| 6990 |
} |
| 6991 |
#endif /* !_WIN32 && LINUX_NATIVE_AIO */ |
| 6992 |
} else if (type.is_wake()) { |
| 6993 |
AIO::wake_simulated_handler_thread( |
| 6994 |
AIO::get_segment_no_from_slot(array, slot)); |
| 6995 |
} |
| 6996 |
} else if (type.is_write()) { |
| 6997 |
if (srv_use_native_aio) { |
| 6998 |
++os_n_file_writes; |
| 6999 |
|
| 7000 |
#ifdef _WIN32 |
| 7001 |
ret = WriteFile(file.m_file, slot->ptr, slot->len, &slot->n_bytes, |
| 7002 |
&slot->control); |
| 7003 |
#elif defined(LINUX_NATIVE_AIO) |
| 7004 |
if (!array->linux_dispatch(slot)) { |
| 7005 |
goto err_exit; |
| 7006 |
} |
| 7007 |
#endif /* !_WIN32 && LINUX_NATIVE_AIO */ |
| 7008 |
|
| 7009 |
} else if (type.is_wake()) { |
| 7010 |
AIO::wake_simulated_handler_thread( |
| 7011 |
AIO::get_segment_no_from_slot(array, slot)); |
| 7012 |
} |
| 7013 |
} else { |
| 7014 |
ut_error; |
| 7015 |
} |
| 7016 |
|
| 7017 |
#ifdef _WIN32 |
| 7018 |
if (srv_use_native_aio) { |
| 7019 |
if ((!ret && GetLastError() != ERROR_IO_PENDING) || |
| 7020 |
(ret && slot->len != slot->n_bytes)) { |
| 7021 |
goto err_exit; |
| 7022 |
} |
| 7023 |
} |
| 7024 |
#endif /* _WIN32 */ |
| 7025 |
|
| 7026 |
/* AIO request was queued successfully! */ |
| 7027 |
return (DB_SUCCESS); |
| 7028 |
|
| 7029 |
#if defined LINUX_NATIVE_AIO || defined _WIN32 |
| 7030 |
err_exit: |
| 7031 |
#endif /* LINUX_NATIVE_AIO || _WIN32 */ |
| 7032 |
|
| 7033 |
array->release_with_mutex(slot); |
| 7034 |
if (os_file_handle_error(name, type.is_read() ? "aio read" : "aio write")) { |
| 7035 |
goto try_again; |
| 7036 |
} |
| 7037 |
|
| 7038 |
return (DB_IO_ERROR); |
| 7039 |
} |
| 7040 |
|
| 7041 |
/** Simulated AIO handler for reaping IO requests */ |
| 7042 |
class SimulatedAIOHandler { |
| 7043 |
public: |
| 7044 |
/** Constructor |
| 7045 |
@param[in,out] array The AIO array |
| 7046 |
@param[in] segment Local segment in the array */ |
| 7047 |
SimulatedAIOHandler(AIO *array, ulint segment) |
| 7048 |
: m_oldest(), |
| 7049 |
m_n_elems(), |
| 7050 |
m_lowest_offset(std::numeric_limits<uint64_t>::max()), |
| 7051 |
m_array(array), |
| 7052 |
m_n_slots(), |
| 7053 |
m_segment(segment), |
| 7054 |
m_buf() { |
| 7055 |
ut_ad(m_segment < 100); |
| 7056 |
|
| 7057 |
m_slots.resize(OS_AIO_MERGE_N_CONSECUTIVE); |
| 7058 |
} |
| 7059 |
|
| 7060 |
/** Destructor */ |
| 7061 |
~SimulatedAIOHandler() { ut::aligned_free(m_buf); } |
| 7062 |
|
| 7063 |
/** Reset the state of the handler |
| 7064 |
@param[in] n_slots Number of pending AIO operations supported */ |
| 7065 |
void init(ulint n_slots) { |
| 7066 |
m_oldest = std::chrono::seconds::zero(); |
| 7067 |
m_n_elems = 0; |
| 7068 |
m_n_slots = n_slots; |
| 7069 |
m_lowest_offset = std::numeric_limits<uint64_t>::max(); |
| 7070 |
|
| 7071 |
ut::aligned_free(m_buf); |
| 7072 |
m_buf = nullptr; |
| 7073 |
|
| 7074 |
m_slots[0] = nullptr; |
| 7075 |
} |
| 7076 |
|
| 7077 |
/** Check if there is a slot for which the i/o has already been done |
| 7078 |
@param[out] n_reserved Number of reserved slots |
| 7079 |
@return the first completed slot that is found. */ |
| 7080 |
Slot *check_completed(ulint *n_reserved) { |
| 7081 |
ulint offset = m_segment * m_n_slots; |
| 7082 |
|
| 7083 |
*n_reserved = 0; |
| 7084 |
|
| 7085 |
Slot *slot; |
| 7086 |
|
| 7087 |
slot = m_array->at(offset); |
| 7088 |
|
| 7089 |
for (ulint i = 0; i < m_n_slots; ++i, ++slot) { |
| 7090 |
if (slot->is_reserved) { |
| 7091 |
if (slot->io_already_done) { |
| 7092 |
ut_a(slot->is_reserved); |
| 7093 |
|
| 7094 |
return (slot); |
| 7095 |
} |
| 7096 |
|
| 7097 |
++*n_reserved; |
| 7098 |
} |
| 7099 |
} |
| 7100 |
|
| 7101 |
return (nullptr); |
| 7102 |
} |
| 7103 |
|
| 7104 |
/** If there are at least 2 seconds old requests, then pick the |
| 7105 |
oldest one to prevent starvation. If several requests have the |
| 7106 |
same age, then pick the one at the lowest offset. |
| 7107 |
@return true if request was selected */ |
| 7108 |
bool select() { |
| 7109 |
if (!select_oldest()) { |
| 7110 |
return (select_lowest_offset()); |
| 7111 |
} |
| 7112 |
|
| 7113 |
return (true); |
| 7114 |
} |
| 7115 |
|
| 7116 |
/** Check if there are several consecutive blocks |
| 7117 |
to read or write. Merge them if found. */ |
| 7118 |
void merge() { |
| 7119 |
/* if m_n_elems != 0, then we have assigned |
| 7120 |
something valid to consecutive_ios[0] */ |
| 7121 |
ut_ad(m_n_elems != 0); |
| 7122 |
ut_ad(first_slot() != nullptr); |
| 7123 |
|
| 7124 |
Slot *slot = first_slot(); |
| 7125 |
|
| 7126 |
while (!merge_adjacent(slot)) { |
| 7127 |
/* No op */ |
| 7128 |
} |
| 7129 |
} |
| 7130 |
|
| 7131 |
/** We have now collected n_consecutive I/O requests |
| 7132 |
in the array; allocate a single buffer which can hold |
| 7133 |
all data, and perform the I/O |
| 7134 |
@return the length of the buffer */ |
| 7135 |
[[nodiscard]] ulint allocate_buffer() { |
| 7136 |
ulint len; |
| 7137 |
Slot *slot = first_slot(); |
| 7138 |
|
| 7139 |
ut_ad(m_buf == nullptr); |
| 7140 |
|
| 7141 |
if (slot->type.is_read() && m_n_elems > 1) { |
| 7142 |
len = 0; |
| 7143 |
|
| 7144 |
for (ulint i = 0; i < m_n_elems; ++i) { |
| 7145 |
len += m_slots[i]->len; |
| 7146 |
} |
| 7147 |
|
| 7148 |
m_buf = static_cast<byte *>(ut::aligned_alloc(len, UNIV_PAGE_SIZE)); |
| 7149 |
} else { |
| 7150 |
len = first_slot()->len; |
| 7151 |
m_buf = first_slot()->buf; |
| 7152 |
} |
| 7153 |
|
| 7154 |
return (len); |
| 7155 |
} |
| 7156 |
|
| 7157 |
/** We have to compress the individual pages and punch |
| 7158 |
holes in them on a page by page basis when writing to |
| 7159 |
tables that can be compressed at the IO level. |
| 7160 |
@param[in] len Value returned by allocate_buffer */ |
| 7161 |
void copy_to_buffer(ulint len) { |
| 7162 |
Slot *slot = first_slot(); |
| 7163 |
|
| 7164 |
if (len > slot->len && slot->type.is_write()) { |
| 7165 |
byte *ptr = m_buf; |
| 7166 |
|
| 7167 |
ut_ad(ptr != slot->buf); |
| 7168 |
|
| 7169 |
/* Copy the buffers to the combined buffer */ |
| 7170 |
for (ulint i = 0; i < m_n_elems; ++i) { |
| 7171 |
slot = m_slots[i]; |
| 7172 |
|
| 7173 |
memmove(ptr, slot->buf, slot->len); |
| 7174 |
|
| 7175 |
ptr += slot->len; |
| 7176 |
} |
| 7177 |
} |
| 7178 |
} |
| 7179 |
|
| 7180 |
/** Do the I/O with ordinary, synchronous i/o functions: */ |
| 7181 |
void io() { |
| 7182 |
if (first_slot()->type.is_write()) { |
| 7183 |
for (ulint i = 0; i < m_n_elems; ++i) { |
| 7184 |
write(m_slots[i]); |
| 7185 |
} |
| 7186 |
|
| 7187 |
} else { |
| 7188 |
for (ulint i = 0; i < m_n_elems; ++i) { |
| 7189 |
read(m_slots[i]); |
| 7190 |
} |
| 7191 |
} |
| 7192 |
} |
| 7193 |
|
| 7194 |
/** Do the decompression of the pages read in */ |
| 7195 |
void io_complete() { |
| 7196 |
// Note: For non-compressed tables. Not required |
| 7197 |
// for correctness. |
| 7198 |
} |
| 7199 |
|
| 7200 |
/** Mark the i/os done in slots */ |
| 7201 |
void done() { |
| 7202 |
for (ulint i = 0; i < m_n_elems; ++i) { |
| 7203 |
m_slots[i]->io_already_done = true; |
| 7204 |
} |
| 7205 |
} |
| 7206 |
|
| 7207 |
/** @return the first slot in the consecutive array */ |
| 7208 |
[[nodiscard]] Slot *first_slot() { |
| 7209 |
ut_a(m_n_elems > 0); |
| 7210 |
|
| 7211 |
return (m_slots[0]); |
| 7212 |
} |
| 7213 |
|
| 7214 |
/** Wait for I/O requests |
| 7215 |
@param[in] global_segment The global segment |
| 7216 |
@param[in,out] event Wait on event if no active requests |
| 7217 |
@return the number of slots */ |
| 7218 |
[[nodiscard]] ulint check_pending(ulint global_segment, os_event_t event); |
| 7219 |
|
| 7220 |
private: |
| 7221 |
/** Do the file read |
| 7222 |
@param[in,out] slot Slot that has the IO context */ |
| 7223 |
void read(Slot *slot) { |
| 7224 |
dberr_t err = os_file_read_func(slot->type, slot->name, slot->file.m_file, |
| 7225 |
slot->ptr, slot->offset, slot->len); |
| 7226 |
ut_a(err == DB_SUCCESS); |
| 7227 |
} |
| 7228 |
|
| 7229 |
/** Do the file write |
| 7230 |
@param[in,out] slot Slot that has the IO context */ |
| 7231 |
void write(Slot *slot) { |
| 7232 |
dberr_t err = os_file_write_func(slot->type, slot->name, slot->file.m_file, |
| 7233 |
slot->ptr, slot->offset, slot->len); |
| 7234 |
ut_a(err == DB_SUCCESS || err == DB_IO_NO_PUNCH_HOLE); |
| 7235 |
} |
| 7236 |
|
| 7237 |
/** @return true if the slots are adjacent and can be merged */ |
| 7238 |
bool adjacent(const Slot *s1, const Slot *s2) const { |
| 7239 |
return (s1 != s2 && s1->file.m_file == s2->file.m_file && |
| 7240 |
s2->offset == s1->offset + s1->len && s1->type == s2->type); |
| 7241 |
} |
| 7242 |
|
| 7243 |
/** @return true if merge limit reached or no adjacent slots found. */ |
| 7244 |
bool merge_adjacent(Slot *¤t) { |
| 7245 |
Slot *slot; |
| 7246 |
ulint offset = m_segment * m_n_slots; |
| 7247 |
|
| 7248 |
slot = m_array->at(offset); |
| 7249 |
|
| 7250 |
for (ulint i = 0; i < m_n_slots; ++i, ++slot) { |
| 7251 |
if (slot->is_reserved && adjacent(current, slot)) { |
| 7252 |
current = slot; |
| 7253 |
|
| 7254 |
/* Found a consecutive i/o request */ |
| 7255 |
|
| 7256 |
m_slots[m_n_elems] = slot; |
| 7257 |
|
| 7258 |
++m_n_elems; |
| 7259 |
|
| 7260 |
return (m_n_elems >= m_slots.capacity()); |
| 7261 |
} |
| 7262 |
} |
| 7263 |
|
| 7264 |
return (true); |
| 7265 |
} |
| 7266 |
|
| 7267 |
/** There were no old requests. Look for an I/O request at the lowest |
| 7268 |
offset in the array (we ignore the high 32 bits of the offset in these |
| 7269 |
heuristics) */ |
| 7270 |
bool select_lowest_offset() { |
| 7271 |
ut_ad(m_n_elems == 0); |
| 7272 |
|
| 7273 |
ulint offset = m_segment * m_n_slots; |
| 7274 |
|
| 7275 |
m_lowest_offset = std::numeric_limits<uint64_t>::max(); |
| 7276 |
|
| 7277 |
for (ulint i = 0; i < m_n_slots; ++i) { |
| 7278 |
Slot *slot; |
| 7279 |
|
| 7280 |
slot = m_array->at(i + offset); |
| 7281 |
|
| 7282 |
if (slot->is_reserved && slot->offset < m_lowest_offset) { |
| 7283 |
/* Found an i/o request */ |
| 7284 |
m_slots[0] = slot; |
| 7285 |
|
| 7286 |
m_n_elems = 1; |
| 7287 |
|
| 7288 |
m_lowest_offset = slot->offset; |
| 7289 |
} |
| 7290 |
} |
| 7291 |
|
| 7292 |
return (m_n_elems > 0); |
| 7293 |
} |
| 7294 |
|
| 7295 |
typedef std::vector<Slot *> slots_t; |
| 7296 |
|
| 7297 |
private: |
| 7298 |
/** Select the slot if it is older than the current oldest slot. |
| 7299 |
@param[in] slot The slot to check */ |
| 7300 |
void select_if_older(Slot *slot) { |
| 7301 |
const auto time_diff = |
| 7302 |
std::max(std::chrono::steady_clock::now() - slot->reservation_time, |
| 7303 |
std::chrono::steady_clock::duration{0}); |
| 7304 |
|
| 7305 |
if (time_diff >= std::chrono::seconds{2}) { |
| 7306 |
if ((time_diff > m_oldest) || |
| 7307 |
(time_diff == m_oldest && slot->offset < m_lowest_offset)) { |
| 7308 |
/* Found an i/o request */ |
| 7309 |
m_slots[0] = slot; |
| 7310 |
|
| 7311 |
m_n_elems = 1; |
| 7312 |
|
| 7313 |
m_oldest = time_diff; |
| 7314 |
|
| 7315 |
m_lowest_offset = slot->offset; |
| 7316 |
} |
| 7317 |
} |
| 7318 |
} |
| 7319 |
|
| 7320 |
/** Select th oldest slot in the array |
| 7321 |
@return true if oldest slot found */ |
| 7322 |
bool select_oldest() { |
| 7323 |
ut_ad(m_n_elems == 0); |
| 7324 |
|
| 7325 |
Slot *slot; |
| 7326 |
ulint offset = m_n_slots * m_segment; |
| 7327 |
|
| 7328 |
slot = m_array->at(offset); |
| 7329 |
|
| 7330 |
for (ulint i = 0; i < m_n_slots; ++i, ++slot) { |
| 7331 |
if (slot->is_reserved) { |
| 7332 |
select_if_older(slot); |
| 7333 |
} |
| 7334 |
} |
| 7335 |
|
| 7336 |
return (m_n_elems > 0); |
| 7337 |
} |
| 7338 |
|
| 7339 |
std::chrono::steady_clock::duration m_oldest; |
| 7340 |
ulint m_n_elems; |
| 7341 |
os_offset_t m_lowest_offset; |
| 7342 |
|
| 7343 |
AIO *m_array; |
| 7344 |
ulint m_n_slots; |
| 7345 |
ulint m_segment; |
| 7346 |
|
| 7347 |
slots_t m_slots; |
| 7348 |
|
| 7349 |
byte *m_buf; |
| 7350 |
}; |
| 7351 |
|
| 7352 |
/** Wait for I/O requests |
| 7353 |
@return the number of slots */ |
| 7354 |
ulint SimulatedAIOHandler::check_pending(ulint global_segment, |
| 7355 |
os_event_t event) { |
| 7356 |
/* NOTE! We only access constant fields in os_aio_array. |
| 7357 |
Therefore we do not have to acquire the protecting mutex yet */ |
| 7358 |
|
| 7359 |
ut_ad(os_aio_validate_skip()); |
| 7360 |
|
| 7361 |
ut_ad(m_segment < m_array->get_n_segments()); |
| 7362 |
|
| 7363 |
/* Look through n slots after the segment * n'th slot */ |
| 7364 |
|
| 7365 |
if (AIO::is_read(m_array) && os_aio_recommend_sleep_for_read_threads) { |
| 7366 |
/* Give other threads chance to add several |
| 7367 |
I/Os to the array at once. */ |
| 7368 |
|
| 7369 |
srv_set_io_thread_op_info(global_segment, "waiting for i/o request"); |
| 7370 |
|
| 7371 |
os_event_wait(event); |
| 7372 |
|
| 7373 |
return (0); |
| 7374 |
} |
| 7375 |
|
| 7376 |
return (m_array->slots_per_segment()); |
| 7377 |
} |
| 7378 |
|
| 7379 |
/** Does simulated AIO. This function should be called by an i/o-handler |
| 7380 |
thread. |
| 7381 |
|
| 7382 |
@param[in] global_segment The number of the segment in the aio arrays to |
| 7383 |
await for; segment 0 is the ibuf i/o thread, |
| 7384 |
then follow the non-ibuf read threads, |
| 7385 |
and as the last are the non-ibuf write threads |
| 7386 |
@param[out] m1 the messages passed with the AIO request; note |
| 7387 |
that also in the case where the AIO operation |
| 7388 |
failed, these output parameters are valid and |
| 7389 |
can be used to restart the operation, for |
| 7390 |
example |
| 7391 |
@param[out] m2 Callback argument |
| 7392 |
@param[in] type IO context |
| 7393 |
@return DB_SUCCESS or error code */ |
| 7394 |
static dberr_t os_aio_simulated_handler(ulint global_segment, fil_node_t **m1, |
| 7395 |
void **m2, IORequest *type) { |
| 7396 |
Slot *slot; |
| 7397 |
AIO *array{}; |
| 7398 |
os_event_t event = os_aio_segment_wait_events[global_segment]; |
| 7399 |
|
| 7400 |
auto segment = AIO::get_array_and_local_segment(array, global_segment); |
| 7401 |
|
| 7402 |
SimulatedAIOHandler handler(array, segment); |
| 7403 |
|
| 7404 |
for (;;) { |
| 7405 |
srv_set_io_thread_op_info(global_segment, "looking for i/o requests (a)"); |
| 7406 |
|
| 7407 |
ulint n_slots = handler.check_pending(global_segment, event); |
| 7408 |
|
| 7409 |
if (n_slots == 0) { |
| 7410 |
continue; |
| 7411 |
} |
| 7412 |
|
| 7413 |
handler.init(n_slots); |
| 7414 |
|
| 7415 |
srv_set_io_thread_op_info(global_segment, "looking for i/o requests (b)"); |
| 7416 |
|
| 7417 |
array->acquire(); |
| 7418 |
|
| 7419 |
ulint n_reserved; |
| 7420 |
|
| 7421 |
slot = handler.check_completed(&n_reserved); |
| 7422 |
|
| 7423 |
if (slot != nullptr) { |
| 7424 |
break; |
| 7425 |
|
| 7426 |
} else if (n_reserved == 0 && !buf_flush_page_cleaner_is_active() && |
| 7427 |
srv_shutdown_state.load() == SRV_SHUTDOWN_EXIT_THREADS) { |
| 7428 |
/* There is no completed request. If there |
| 7429 |
are no pending request at all, and the system |
| 7430 |
is being shut down, exit. */ |
| 7431 |
|
| 7432 |
array->release(); |
| 7433 |
|
| 7434 |
*m1 = nullptr; |
| 7435 |
|
| 7436 |
*m2 = nullptr; |
| 7437 |
|
| 7438 |
return (DB_SUCCESS); |
| 7439 |
|
| 7440 |
} else if (handler.select()) { |
| 7441 |
break; |
| 7442 |
} |
| 7443 |
|
| 7444 |
/* No I/O requested at the moment */ |
| 7445 |
|
| 7446 |
srv_set_io_thread_op_info(global_segment, "resetting wait event"); |
| 7447 |
|
| 7448 |
/* We wait here until tbere are more IO requests |
| 7449 |
for this segment. */ |
| 7450 |
|
| 7451 |
os_event_reset(event); |
| 7452 |
|
| 7453 |
array->release(); |
| 7454 |
|
| 7455 |
srv_set_io_thread_op_info(global_segment, "waiting for i/o request"); |
| 7456 |
|
| 7457 |
os_event_wait(event); |
| 7458 |
} |
| 7459 |
|
| 7460 |
/** Found a slot that has already completed its IO */ |
| 7461 |
|
| 7462 |
if (slot == nullptr) { |
| 7463 |
/* Merge adjacent requests */ |
| 7464 |
handler.merge(); |
| 7465 |
|
| 7466 |
/* Check if there are several consecutive blocks |
| 7467 |
to read or write */ |
| 7468 |
|
| 7469 |
srv_set_io_thread_op_info(global_segment, "consecutive i/o requests"); |
| 7470 |
|
| 7471 |
// Note: We don't support write combining for simulated AIO. |
| 7472 |
// ulint total_len = handler.allocate_buffer(); |
| 7473 |
|
| 7474 |
/* We release the array mutex for the time of the I/O: NOTE that |
| 7475 |
this assumes that there is just one i/o-handler thread serving |
| 7476 |
a single segment of slots! */ |
| 7477 |
|
| 7478 |
array->release(); |
| 7479 |
|
| 7480 |
// Note: We don't support write combining for simulated AIO. |
| 7481 |
// handler.copy_to_buffer(total_len); |
| 7482 |
|
| 7483 |
srv_set_io_thread_op_info(global_segment, "doing file i/o"); |
| 7484 |
|
| 7485 |
handler.io(); |
| 7486 |
|
| 7487 |
srv_set_io_thread_op_info(global_segment, "file i/o done"); |
| 7488 |
|
| 7489 |
handler.io_complete(); |
| 7490 |
|
| 7491 |
array->acquire(); |
| 7492 |
|
| 7493 |
handler.done(); |
| 7494 |
|
| 7495 |
/* We return the messages for the first slot now, and if there |
| 7496 |
were several slots, the messages will be returned with |
| 7497 |
subsequent calls of this function */ |
| 7498 |
|
| 7499 |
slot = handler.first_slot(); |
| 7500 |
} |
| 7501 |
|
| 7502 |
ut_ad(slot->is_reserved); |
| 7503 |
|
| 7504 |
*m1 = slot->m1; |
| 7505 |
*m2 = slot->m2; |
| 7506 |
|
| 7507 |
*type = slot->type; |
| 7508 |
|
| 7509 |
array->release(slot); |
| 7510 |
|
| 7511 |
array->release(); |
| 7512 |
|
| 7513 |
return (DB_SUCCESS); |
| 7514 |
} |
| 7515 |
|
| 7516 |
/** Get the total number of pending IOs |
| 7517 |
@return the total number of pending IOs */ |
| 7518 |
ulint AIO::total_pending_io_count() { |
| 7519 |
ulint count = s_reads->pending_io_count(); |
| 7520 |
|
| 7521 |
if (s_writes != nullptr) { |
| 7522 |
count += s_writes->pending_io_count(); |
| 7523 |
} |
| 7524 |
|
| 7525 |
if (s_ibuf != nullptr) { |
| 7526 |
count += s_ibuf->pending_io_count(); |
| 7527 |
} |
| 7528 |
|
| 7529 |
return (count); |
| 7530 |
} |
| 7531 |
|
| 7532 |
/** Validates the consistency the aio system. |
| 7533 |
@return true if ok */ |
| 7534 |
static bool os_aio_validate() { |
| 7535 |
/* The methods countds and validates, we ignore the count. */ |
| 7536 |
AIO::total_pending_io_count(); |
| 7537 |
|
| 7538 |
return (true); |
| 7539 |
} |
| 7540 |
|
| 7541 |
/** Prints pending IO requests per segment of an aio array. |
| 7542 |
We probably don't need per segment statistics but they can help us |
| 7543 |
during development phase to see if the IO requests are being |
| 7544 |
distributed as expected. |
| 7545 |
@param[in,out] file File where to print |
| 7546 |
@param[in] segments Pending IO array */ |
| 7547 |
void AIO::print_segment_info(FILE *file, const ulint *segments) { |
| 7548 |
ut_ad(m_n_segments > 0); |
| 7549 |
|
| 7550 |
if (m_n_segments > 1) { |
| 7551 |
fprintf(file, " ["); |
| 7552 |
|
| 7553 |
for (ulint i = 0; i < m_n_segments; ++i, ++segments) { |
| 7554 |
if (i != 0) { |
| 7555 |
fprintf(file, ", "); |
| 7556 |
} |
| 7557 |
|
| 7558 |
fprintf(file, ULINTPF, *segments); |
| 7559 |
} |
| 7560 |
|
| 7561 |
fprintf(file, "] "); |
| 7562 |
} |
| 7563 |
} |
| 7564 |
|
| 7565 |
/** Prints info about the aio array. |
| 7566 |
@param[in,out] file Where to print */ |
| 7567 |
void AIO::print(FILE *file) { |
| 7568 |
ulint count = 0; |
| 7569 |
ulint n_res_seg[SRV_MAX_N_IO_THREADS]; |
| 7570 |
|
| 7571 |
mutex_enter(&m_mutex); |
| 7572 |
|
| 7573 |
ut_a(!m_slots.empty()); |
| 7574 |
ut_a(m_n_segments > 0); |
| 7575 |
|
| 7576 |
memset(n_res_seg, 0x0, sizeof(n_res_seg)); |
| 7577 |
|
| 7578 |
for (ulint i = 0; i < m_slots.size(); ++i) { |
| 7579 |
Slot &slot = m_slots[i]; |
| 7580 |
ulint segment = (i * m_n_segments) / m_slots.size(); |
| 7581 |
|
| 7582 |
if (slot.is_reserved) { |
| 7583 |
++count; |
| 7584 |
|
| 7585 |
++n_res_seg[segment]; |
| 7586 |
|
| 7587 |
ut_a(slot.len > 0); |
| 7588 |
} |
| 7589 |
} |
| 7590 |
|
| 7591 |
ut_a(m_n_reserved == count); |
| 7592 |
|
| 7593 |
print_segment_info(file, n_res_seg); |
| 7594 |
|
| 7595 |
mutex_exit(&m_mutex); |
| 7596 |
} |
| 7597 |
|
| 7598 |
/** Print all the AIO segments |
| 7599 |
@param[in,out] file Where to print */ |
| 7600 |
void AIO::print_all(FILE *file) { |
| 7601 |
s_reads->print(file); |
| 7602 |
|
| 7603 |
if (s_writes != nullptr) { |
| 7604 |
fputs(", aio writes:", file); |
| 7605 |
s_writes->print(file); |
| 7606 |
} |
| 7607 |
|
| 7608 |
if (s_ibuf != nullptr) { |
| 7609 |
fputs(",\n ibuf aio reads:", file); |
| 7610 |
s_ibuf->print(file); |
| 7611 |
} |
| 7612 |
} |
| 7613 |
|
| 7614 |
/** Prints info of the aio arrays. |
| 7615 |
@param[in,out] file file where to print */ |
| 7616 |
void os_aio_print(FILE *file) { |
| 7617 |
double avg_bytes_read; |
| 7618 |
|
| 7619 |
for (ulint i = 0; i < os_aio_n_segments; ++i) { |
| 7620 |
fprintf(file, "I/O thread %lu state: %s (%s)", (ulong)i, |
| 7621 |
srv_io_thread_op_info[i], srv_io_thread_function[i]); |
| 7622 |
|
| 7623 |
#ifndef _WIN32 |
| 7624 |
if (os_event_is_set(os_aio_segment_wait_events[i])) { |
| 7625 |
fprintf(file, " ev set"); |
| 7626 |
} |
| 7627 |
#endif /* !_WIN32 */ |
| 7628 |
|
| 7629 |
fprintf(file, "\n"); |
| 7630 |
} |
| 7631 |
|
| 7632 |
fputs("Pending normal aio reads:", file); |
| 7633 |
|
| 7634 |
AIO::print_all(file); |
| 7635 |
|
| 7636 |
putc('\n', file); |
| 7637 |
const auto current_time = std::chrono::steady_clock::now(); |
| 7638 |
const auto time_elapsed_s = |
| 7639 |
0.001 + std::chrono::duration_cast<std::chrono::duration<double>>( |
| 7640 |
current_time - os_last_printout) |
| 7641 |
.count(); |
| 7642 |
|
| 7643 |
uint64_t n_log_pending_flushes; |
| 7644 |
#ifndef UNIV_HOTBACKUP |
| 7645 |
n_log_pending_flushes = log_pending_flushes(); |
| 7646 |
#else /* UNIV_HOTBACKUP */ |
| 7647 |
n_log_pending_flushes = 0; |
| 7648 |
#endif /* UNIV_HOTBACKUP */ |
| 7649 |
|
| 7650 |
fprintf(file, |
| 7651 |
"Pending flushes (fsync) log: " UINT64PF |
| 7652 |
"; " |
| 7653 |
"buffer pool: " UINT64PF "\n" ULINTPF " OS file reads, " ULINTPF |
| 7654 |
" OS file writes, " ULINTPF " OS fsyncs\n", |
| 7655 |
n_log_pending_flushes, fil_n_pending_tablespace_flushes.load(), |
| 7656 |
os_n_file_reads, os_n_file_writes, os_n_fsyncs); |
| 7657 |
|
| 7658 |
auto pending_writes = os_n_pending_writes.load(); |
| 7659 |
auto pending_reads = os_n_pending_reads.load(); |
| 7660 |
if (pending_writes != 0 || pending_reads != 0) { |
| 7661 |
fprintf(file, ULINTPF " pending preads, " ULINTPF " pending pwrites\n", |
| 7662 |
pending_reads, pending_writes); |
| 7663 |
} |
| 7664 |
|
| 7665 |
if (os_n_file_reads == os_n_file_reads_old) { |
| 7666 |
avg_bytes_read = 0.0; |
| 7667 |
} else { |
| 7668 |
avg_bytes_read = (double)os_bytes_read_since_printout / |
| 7669 |
(os_n_file_reads - os_n_file_reads_old); |
| 7670 |
} |
| 7671 |
|
| 7672 |
fprintf(file, |
| 7673 |
"%.2lf reads/s, %lu avg bytes/read," |
| 7674 |
" %.2lf writes/s, %.2lf fsyncs/s\n", |
| 7675 |
(os_n_file_reads - os_n_file_reads_old) / time_elapsed_s, |
| 7676 |
(ulong)avg_bytes_read, |
| 7677 |
(os_n_file_writes - os_n_file_writes_old) / time_elapsed_s, |
| 7678 |
(os_n_fsyncs - os_n_fsyncs_old) / time_elapsed_s); |
| 7679 |
|
| 7680 |
os_n_file_reads_old = os_n_file_reads; |
| 7681 |
os_n_file_writes_old = os_n_file_writes; |
| 7682 |
os_n_fsyncs_old = os_n_fsyncs; |
| 7683 |
os_bytes_read_since_printout = 0; |
| 7684 |
|
| 7685 |
os_last_printout = current_time; |
| 7686 |
} |
| 7687 |
|
| 7688 |
/** Refreshes the statistics used to print per-second averages. */ |
| 7689 |
void os_aio_refresh_stats() { |
| 7690 |
os_n_fsyncs_old = os_n_fsyncs; |
| 7691 |
|
| 7692 |
os_bytes_read_since_printout = 0; |
| 7693 |
|
| 7694 |
os_n_file_reads_old = os_n_file_reads; |
| 7695 |
|
| 7696 |
os_n_file_writes_old = os_n_file_writes; |
| 7697 |
|
| 7698 |
os_n_fsyncs_old = os_n_fsyncs; |
| 7699 |
|
| 7700 |
os_bytes_read_since_printout = 0; |
| 7701 |
|
| 7702 |
os_last_printout = std::chrono::steady_clock::now(); |
| 7703 |
} |
| 7704 |
|
| 7705 |
/** Checks that all slots in the system have been freed, that is, there are |
| 7706 |
no pending io operations. |
| 7707 |
@return true if all free */ |
| 7708 |
bool os_aio_all_slots_free() { return (AIO::total_pending_io_count() == 0); } |
| 7709 |
|
| 7710 |
#ifdef UNIV_DEBUG |
| 7711 |
/** Prints all pending IO for the array |
| 7712 |
@param[in,out] file file where to print */ |
| 7713 |
void AIO::to_file(FILE *file) const { |
| 7714 |
acquire(); |
| 7715 |
|
| 7716 |
fprintf(file, " %lu\n", static_cast<ulong>(m_n_reserved)); |
| 7717 |
|
| 7718 |
for (ulint i = 0; i < m_slots.size(); ++i) { |
| 7719 |
const Slot &slot = m_slots[i]; |
| 7720 |
|
| 7721 |
if (slot.is_reserved) { |
| 7722 |
fprintf(file, "%s IO for %s (offset=" UINT64PF ", size=%lu)\n", |
| 7723 |
slot.type.is_read() ? "read" : "write", slot.name, slot.offset, |
| 7724 |
slot.len); |
| 7725 |
} |
| 7726 |
} |
| 7727 |
|
| 7728 |
release(); |
| 7729 |
} |
| 7730 |
|
| 7731 |
/** Print pending IOs for all arrays */ |
| 7732 |
void AIO::print_to_file(FILE *file) { |
| 7733 |
fprintf(file, "Pending normal aio reads:"); |
| 7734 |
|
| 7735 |
s_reads->to_file(file); |
| 7736 |
|
| 7737 |
if (s_writes != nullptr) { |
| 7738 |
fprintf(file, "Pending normal aio writes:"); |
| 7739 |
s_writes->to_file(file); |
| 7740 |
} |
| 7741 |
|
| 7742 |
if (s_ibuf != nullptr) { |
| 7743 |
fprintf(file, "Pending ibuf aio reads:"); |
| 7744 |
s_ibuf->to_file(file); |
| 7745 |
} |
| 7746 |
} |
| 7747 |
|
| 7748 |
/** Prints all pending IO |
| 7749 |
@param[in] file File where to print */ |
| 7750 |
void os_aio_print_pending_io(FILE *file) { AIO::print_to_file(file); } |
| 7751 |
|
| 7752 |
#endif /* UNIV_DEBUG */ |
| 7753 |
#endif /* !UNIV_HOTBACKUP */ |
| 7754 |
|
| 7755 |
#ifndef _WIN32 |
| 7756 |
void os_file_set_umask(mode_t umask) { |
| 7757 |
static bool was_already_set{false}; |
| 7758 |
ut_a(!was_already_set); |
| 7759 |
was_already_set = true; |
| 7760 |
os_innodb_umask = umask; |
| 7761 |
} |
| 7762 |
#endif /* !_WIN32 */ |
| 7763 |
|
| 7764 |
/** Check if the path is a directory. The file/directory must exist. |
| 7765 |
@param[in] path The path to check |
| 7766 |
@return true if it is a directory */ |
| 7767 |
bool Dir_Walker::is_directory(const Path &path) { |
| 7768 |
os_file_type_t type; |
| 7769 |
bool exists; |
| 7770 |
|
| 7771 |
if (os_file_status(path.c_str(), &exists, &type)) { |
| 7772 |
ut_ad(exists); |
| 7773 |
ut_ad(type != OS_FILE_TYPE_MISSING); |
| 7774 |
|
| 7775 |
return (type == OS_FILE_TYPE_DIR); |
| 7776 |
} |
| 7777 |
|
| 7778 |
ut_ad(exists || type == OS_FILE_TYPE_FAILED); |
| 7779 |
ut_ad(type != OS_FILE_TYPE_MISSING); |
| 7780 |
|
| 7781 |
return (false); |
| 7782 |
} |
| 7783 |
|
| 7784 |
dberr_t os_file_write_retry(IORequest &type, const char *name, |
| 7785 |
pfs_os_file_t file, const void *buf, |
| 7786 |
os_offset_t offset, ulint n) { |
| 7787 |
dberr_t err; |
| 7788 |
for (;;) { |
| 7789 |
err = os_file_write(type, name, file, buf, offset, n); |
| 7790 |
|
| 7791 |
if (err == DB_SUCCESS || err == DB_TABLESPACE_DELETED) { |
| 7792 |
break; |
| 7793 |
} else if (err == DB_IO_ERROR) { |
| 7794 |
ib::error(ER_INNODB_IO_WRITE_ERROR_RETRYING, name); |
| 7795 |
std::chrono::seconds ten(10); |
| 7796 |
std::this_thread::sleep_for(ten); |
| 7797 |
continue; |
| 7798 |
} else { |
| 7799 |
ib::fatal(UT_LOCATION_HERE, ER_INNODB_IO_WRITE_FAILED, name); |
| 7800 |
} |
| 7801 |
} |
| 7802 |
return err; |
| 7803 |
} |