| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2010, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/os0file.ic |
| 29 |
The interface to the operating system file io |
| 30 |
|
| 31 |
Created 2/20/2010 Jimmy Yang |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include "univ.i" |
| 35 |
|
| 36 |
#ifdef UNIV_PFS_IO |
| 37 |
/** NOTE! Please use the corresponding macro |
| 38 |
os_file_create_simple_no_error_handling(), not directly this function! |
| 39 |
A performance schema instrumented wrapper function for |
| 40 |
os_file_create_simple_no_error_handling(). Add instrumentation to |
| 41 |
monitor file creation/open. |
| 42 |
@param[in] key Performance Schema Key |
| 43 |
@param[in] name name of the file or path as a null-terminated |
| 44 |
string |
| 45 |
@param[in] create_mode create mode |
| 46 |
@param[in] access_type OS_FILE_READ_ONLY, OS_FILE_READ_WRITE, or |
| 47 |
OS_FILE_READ_ALLOW_DELETE; the last option is |
| 48 |
used by a backup program reading the file |
| 49 |
@param[in] read_only if true read only mode checks are enforced |
| 50 |
@param[in] umask UNIX access permission to be set when creating a |
| 51 |
file. Use os_umask_default to use global default |
| 52 |
umask. |
| 53 |
@param[out] success true if succeeded |
| 54 |
@param[in] src_location location where func invoked |
| 55 |
@return own: handle to the file, not defined if error, error number |
| 56 |
can be retrieved with os_file_get_last_error */ |
| 57 |
static inline pfs_os_file_t pfs_os_file_create_simple_no_error_handling_func( |
| 58 |
mysql_pfs_key_t key, const char *name, ulint create_mode, ulint access_type, |
| 59 |
bool read_only, |
| 60 |
#ifndef _WIN32 |
| 61 |
mode_t umask, |
| 62 |
#endif |
| 63 |
bool *success, ut::Location src_location) { |
| 64 |
PSI_file_locker_state state; |
| 65 |
struct PSI_file_locker *locker = nullptr; |
| 66 |
pfs_os_file_t file; |
| 67 |
|
| 68 |
/* register a file open or creation depending on "create_mode" */ |
| 69 |
register_pfs_file_open_begin( |
| 70 |
&state, locker, key, |
| 71 |
create_mode == OS_FILE_CREATE ? PSI_FILE_CREATE : PSI_FILE_OPEN, name, |
| 72 |
src_location); |
| 73 |
|
| 74 |
file = os_file_create_simple_no_error_handling_func(name, create_mode, |
| 75 |
access_type, read_only, |
| 76 |
#ifndef _WIN32 |
| 77 |
umask, |
| 78 |
#endif |
| 79 |
success); |
| 80 |
|
| 81 |
file.m_psi = nullptr; |
| 82 |
register_pfs_file_open_end(locker, file, |
| 83 |
(*success == true ? success : nullptr)); |
| 84 |
|
| 85 |
return (file); |
| 86 |
} |
| 87 |
|
| 88 |
static inline pfs_os_file_t pfs_os_file_create_func( |
| 89 |
mysql_pfs_key_t key, const char *name, ulint create_mode, ulint purpose, |
| 90 |
bool read_only, bool *success, ut::Location src_location) { |
| 91 |
PSI_file_locker_state state; |
| 92 |
struct PSI_file_locker *locker = nullptr; |
| 93 |
pfs_os_file_t file; |
| 94 |
|
| 95 |
/* register a file open or creation depending on "create_mode" */ |
| 96 |
register_pfs_file_open_begin( |
| 97 |
&state, locker, key, |
| 98 |
create_mode == OS_FILE_CREATE ? PSI_FILE_CREATE : PSI_FILE_OPEN, name, |
| 99 |
src_location); |
| 100 |
|
| 101 |
file = os_file_create_func(name, create_mode, purpose, read_only, success); |
| 102 |
|
| 103 |
file.m_psi = nullptr; |
| 104 |
register_pfs_file_open_end(locker, file, |
| 105 |
(*success == true ? success : nullptr)); |
| 106 |
|
| 107 |
return (file); |
| 108 |
} |
| 109 |
/** |
| 110 |
NOTE! Please use the corresponding macro os_file_close(), not directly |
| 111 |
this function! |
| 112 |
A performance schema instrumented wrapper function for os_file_close(). |
| 113 |
@param[in] file handle to a file |
| 114 |
@param[in] src_location location where func invoked |
| 115 |
@return true if success */ |
| 116 |
static inline bool pfs_os_file_close_func(pfs_os_file_t file, |
| 117 |
ut::Location src_location) { |
| 118 |
PSI_file_locker_state state; |
| 119 |
struct PSI_file_locker *locker = nullptr; |
| 120 |
|
| 121 |
/* register the file close */ |
| 122 |
register_pfs_file_io_begin(&state, locker, file, 0, PSI_FILE_CLOSE, |
| 123 |
src_location); |
| 124 |
|
| 125 |
bool result = os_file_close_func(file.m_file); |
| 126 |
|
| 127 |
register_pfs_file_io_end(locker, 0); |
| 128 |
|
| 129 |
return (result); |
| 130 |
} |
| 131 |
|
| 132 |
#ifndef UNIV_HOTBACKUP |
| 133 |
static inline dberr_t pfs_os_aio_func(IORequest &type, AIO_mode aio_mode, |
| 134 |
const char *name, pfs_os_file_t file, |
| 135 |
void *buf, os_offset_t offset, ulint n, |
| 136 |
bool read_only, fil_node_t *m1, void *m2, |
| 137 |
ut::Location src_location) { |
| 138 |
PSI_file_locker_state state; |
| 139 |
struct PSI_file_locker *locker = nullptr; |
| 140 |
|
| 141 |
ut_ad(type.validate()); |
| 142 |
|
| 143 |
/* Register the read or write I/O depending on "type" */ |
| 144 |
register_pfs_file_io_begin(&state, locker, file, n, |
| 145 |
type.is_write() ? PSI_FILE_WRITE : PSI_FILE_READ, |
| 146 |
src_location); |
| 147 |
|
| 148 |
dberr_t result = os_aio_func(type, aio_mode, name, file, buf, offset, n, |
| 149 |
read_only, m1, m2); |
| 150 |
|
| 151 |
register_pfs_file_io_end(locker, n); |
| 152 |
|
| 153 |
return (result); |
| 154 |
} |
| 155 |
#endif /* UNIV_HOTBACKUP */ |
| 156 |
|
| 157 |
/** NOTE! Please use the corresponding macro os_file_read(), not directly |
| 158 |
this function! |
| 159 |
This is the performance schema instrumented wrapper function for |
| 160 |
os_file_read() which requests a synchronous read operation. |
| 161 |
@param[in, out] type IO request context |
| 162 |
@param[in] file_name file name |
| 163 |
@param[in] file Open file handle |
| 164 |
@param[out] buf buffer where to read |
| 165 |
@param[in] offset file offset where to read |
| 166 |
@param[in] n number of bytes to read |
| 167 |
@param[in] src_location location where func invoked |
| 168 |
@return DB_SUCCESS if request was successful */ |
| 169 |
static inline dberr_t pfs_os_file_read_func(IORequest &type, |
| 170 |
const char *file_name, |
| 171 |
pfs_os_file_t file, void *buf, |
| 172 |
os_offset_t offset, ulint n, |
| 173 |
ut::Location src_location) { |
| 174 |
PSI_file_locker_state state; |
| 175 |
struct PSI_file_locker *locker = nullptr; |
| 176 |
|
| 177 |
ut_ad(type.validate()); |
| 178 |
|
| 179 |
register_pfs_file_io_begin(&state, locker, file, n, PSI_FILE_READ, |
| 180 |
src_location); |
| 181 |
|
| 182 |
dberr_t result; |
| 183 |
|
| 184 |
result = os_file_read_func(type, file_name, file.m_file, buf, offset, n); |
| 185 |
|
| 186 |
register_pfs_file_io_end(locker, n); |
| 187 |
|
| 188 |
return (result); |
| 189 |
} |
| 190 |
|
| 191 |
/** NOTE! Please use the corresponding macro os_file_read_first_page(), |
| 192 |
not directly this function! |
| 193 |
This is the performance schema instrumented wrapper function for |
| 194 |
os_file_read() which requests a synchronous read operation. |
| 195 |
@param[in, out] type IO request context |
| 196 |
@param[in] file_name file name |
| 197 |
@param[in] file Open file handle |
| 198 |
@param[out] buf buffer where to read |
| 199 |
@param[in] n number of bytes to read |
| 200 |
@param[in] src_location location where func invoked |
| 201 |
@return DB_SUCCESS if request was successful */ |
| 202 |
static inline dberr_t pfs_os_file_read_first_page_func( |
| 203 |
IORequest &type, const char *file_name, pfs_os_file_t file, void *buf, |
| 204 |
ulint n, ut::Location src_location) { |
| 205 |
PSI_file_locker_state state; |
| 206 |
struct PSI_file_locker *locker = nullptr; |
| 207 |
|
| 208 |
ut_ad(type.validate()); |
| 209 |
|
| 210 |
register_pfs_file_io_begin(&state, locker, file, n, PSI_FILE_READ, |
| 211 |
src_location); |
| 212 |
|
| 213 |
dberr_t result; |
| 214 |
|
| 215 |
result = os_file_read_first_page_func(type, file_name, file.m_file, buf, n); |
| 216 |
|
| 217 |
register_pfs_file_io_end(locker, n); |
| 218 |
|
| 219 |
return (result); |
| 220 |
} |
| 221 |
|
| 222 |
/** Copy data from one file to another file. Data is read/written |
| 223 |
at current file offset. |
| 224 |
@param[in] src file handle to copy from |
| 225 |
@param[in] src_offset offset to copy from |
| 226 |
@param[in] dest file handle to copy to |
| 227 |
@param[in] dest_offset offset to copy to |
| 228 |
@param[in] size number of bytes to copy |
| 229 |
@param[in] src_location location where func invoked |
| 230 |
@return DB_SUCCESS if successful */ |
| 231 |
static inline dberr_t pfs_os_file_copy_func(pfs_os_file_t src, |
| 232 |
os_offset_t src_offset, |
| 233 |
pfs_os_file_t dest, |
| 234 |
os_offset_t dest_offset, uint size, |
| 235 |
ut::Location src_location) { |
| 236 |
dberr_t result; |
| 237 |
|
| 238 |
PSI_file_locker_state state_read; |
| 239 |
PSI_file_locker_state state_write; |
| 240 |
|
| 241 |
struct PSI_file_locker *locker_read = nullptr; |
| 242 |
struct PSI_file_locker *locker_write = nullptr; |
| 243 |
|
| 244 |
register_pfs_file_io_begin(&state_read, locker_read, src, size, PSI_FILE_READ, |
| 245 |
src_location); |
| 246 |
|
| 247 |
register_pfs_file_io_begin(&state_write, locker_write, dest, size, |
| 248 |
PSI_FILE_WRITE, src_location); |
| 249 |
|
| 250 |
result = |
| 251 |
os_file_copy_func(src.m_file, src_offset, dest.m_file, dest_offset, size); |
| 252 |
|
| 253 |
register_pfs_file_io_end(locker_write, size); |
| 254 |
register_pfs_file_io_end(locker_read, size); |
| 255 |
|
| 256 |
return (result); |
| 257 |
} |
| 258 |
|
| 259 |
static inline dberr_t pfs_os_file_read_no_error_handling_func( |
| 260 |
IORequest &type, const char *file_name, pfs_os_file_t file, void *buf, |
| 261 |
os_offset_t offset, ulint n, ulint *o, ut::Location src_location) { |
| 262 |
PSI_file_locker_state state; |
| 263 |
struct PSI_file_locker *locker = nullptr; |
| 264 |
|
| 265 |
register_pfs_file_io_begin(&state, locker, file, n, PSI_FILE_READ, |
| 266 |
src_location); |
| 267 |
|
| 268 |
dberr_t result = os_file_read_no_error_handling_func( |
| 269 |
type, file_name, file.m_file, buf, offset, n, o); |
| 270 |
|
| 271 |
register_pfs_file_io_end(locker, n); |
| 272 |
|
| 273 |
return (result); |
| 274 |
} |
| 275 |
|
| 276 |
/** NOTE! Please use the corresponding macro |
| 277 |
os_file_read_no_error_handling_int_fd(), not directly this function! |
| 278 |
This is the performance schema instrumented wrapper function for |
| 279 |
os_file_read_no_error_handling_int_fd_func() which requests a |
| 280 |
synchronous read operation. |
| 281 |
@param[in, out] type IO request context |
| 282 |
@param[in] file_name file name |
| 283 |
@param[in] file Open file handle |
| 284 |
@param[out] buf buffer where to read |
| 285 |
@param[in] offset file offset where to read |
| 286 |
@param[in] n number of bytes to read |
| 287 |
@param[out] o number of bytes actually read |
| 288 |
@param[in] src_location location where func invoked |
| 289 |
@return DB_SUCCESS if request was successful */ |
| 290 |
|
| 291 |
static inline dberr_t pfs_os_file_read_no_error_handling_int_fd_func( |
| 292 |
IORequest &type, const char *file_name, int file, void *buf, |
| 293 |
os_offset_t offset, ulint n, ulint *o, ut::Location src_location) { |
| 294 |
PSI_file_locker_state state; |
| 295 |
struct PSI_file_locker *locker = nullptr; |
| 296 |
|
| 297 |
locker = PSI_FILE_CALL(get_thread_file_descriptor_locker)(&state, file, |
| 298 |
PSI_FILE_READ); |
| 299 |
if (locker != nullptr) { |
| 300 |
PSI_FILE_CALL(start_file_wait) |
| 301 |
(locker, n, src_location.filename, src_location.line); |
| 302 |
} |
| 303 |
dberr_t result = os_file_read_no_error_handling_func( |
| 304 |
type, file_name, OS_FILE_FROM_FD(file), buf, offset, n, o); |
| 305 |
|
| 306 |
if (locker != nullptr) { |
| 307 |
PSI_FILE_CALL(end_file_wait)(locker, n); |
| 308 |
} |
| 309 |
|
| 310 |
return (result); |
| 311 |
} |
| 312 |
|
| 313 |
/** NOTE! Please use the corresponding macro os_file_write(), not directly |
| 314 |
this function! |
| 315 |
This is the performance schema instrumented wrapper function for |
| 316 |
os_file_write() which requests a synchronous write operation. |
| 317 |
@param[in, out] type IO request context |
| 318 |
@param[in] name Name of the file or path as NUL terminated |
| 319 |
string |
| 320 |
@param[in] file Open file handle |
| 321 |
@param[out] buf buffer where to read |
| 322 |
@param[in] offset file offset where to read |
| 323 |
@param[in] n number of bytes to read |
| 324 |
@param[in] src_location location where func invoked |
| 325 |
@return DB_SUCCESS if request was successful */ |
| 326 |
static inline dberr_t pfs_os_file_write_func(IORequest &type, const char *name, |
| 327 |
pfs_os_file_t file, |
| 328 |
const void *buf, |
| 329 |
os_offset_t offset, ulint n, |
| 330 |
ut::Location src_location) { |
| 331 |
PSI_file_locker_state state; |
| 332 |
struct PSI_file_locker *locker = nullptr; |
| 333 |
|
| 334 |
register_pfs_file_io_begin(&state, locker, file, n, PSI_FILE_WRITE, |
| 335 |
src_location); |
| 336 |
|
| 337 |
dberr_t result; |
| 338 |
|
| 339 |
result = os_file_write_func(type, name, file.m_file, buf, offset, n); |
| 340 |
|
| 341 |
register_pfs_file_io_end(locker, n); |
| 342 |
|
| 343 |
return (result); |
| 344 |
} |
| 345 |
|
| 346 |
/** NOTE! Please use the corresponding macro os_file_write(), not |
| 347 |
directly this function! |
| 348 |
This is the performance schema instrumented wrapper function for |
| 349 |
os_file_write() which requests a synchronous write operation. |
| 350 |
@param[in, out] type IO request context |
| 351 |
@param[in] name Name of the file or path as NUL terminated |
| 352 |
string |
| 353 |
@param[in] file Open file handle |
| 354 |
@param[out] buf buffer where to read |
| 355 |
@param[in] offset file offset where to read |
| 356 |
@param[in] n number of bytes to read |
| 357 |
@param[in] src_location location where func invoked |
| 358 |
@return DB_SUCCESS if request was successful */ |
| 359 |
static inline dberr_t pfs_os_file_write_int_fd_func(IORequest &type, |
| 360 |
const char *name, int file, |
| 361 |
const void *buf, |
| 362 |
os_offset_t offset, ulint n, |
| 363 |
ut::Location src_location) { |
| 364 |
PSI_file_locker_state state; |
| 365 |
struct PSI_file_locker *locker = nullptr; |
| 366 |
|
| 367 |
locker = PSI_FILE_CALL(get_thread_file_descriptor_locker)(&state, file, |
| 368 |
PSI_FILE_WRITE); |
| 369 |
if (locker != nullptr) { |
| 370 |
PSI_FILE_CALL(start_file_wait) |
| 371 |
(locker, n, src_location.filename, src_location.line); |
| 372 |
} |
| 373 |
dberr_t result = |
| 374 |
os_file_write_func(type, name, OS_FILE_FROM_FD(file), buf, offset, n); |
| 375 |
|
| 376 |
if (locker != nullptr) { |
| 377 |
PSI_FILE_CALL(end_file_wait)(locker, n); |
| 378 |
} |
| 379 |
|
| 380 |
return (result); |
| 381 |
} |
| 382 |
|
| 383 |
/** NOTE! Please use the corresponding macro os_file_flush(), |
| 384 |
not directly |
| 385 |
this function! |
| 386 |
This is the performance schema instrumented wrapper function for |
| 387 |
os_file_flush() which flushes the write buffers of a given file to the disk. |
| 388 |
Flushes the write buffers of a given file to the disk. |
| 389 |
@param[in] file Open file handle |
| 390 |
@param[in] src_location location where func invoked |
| 391 |
@return true if success */ |
| 392 |
static inline bool pfs_os_file_flush_func(pfs_os_file_t file, |
| 393 |
ut::Location src_location) { |
| 394 |
PSI_file_locker_state state; |
| 395 |
struct PSI_file_locker *locker = nullptr; |
| 396 |
|
| 397 |
register_pfs_file_io_begin(&state, locker, file, 0, PSI_FILE_SYNC, |
| 398 |
src_location); |
| 399 |
|
| 400 |
bool result = os_file_flush_func(file.m_file); |
| 401 |
|
| 402 |
register_pfs_file_io_end(locker, 0); |
| 403 |
|
| 404 |
return (result); |
| 405 |
} |
| 406 |
|
| 407 |
/** NOTE! Please use the corresponding macro os_file_rename(), not directly |
| 408 |
this function! |
| 409 |
This is the performance schema instrumented wrapper function for |
| 410 |
os_file_rename() |
| 411 |
@param[in] key Performance Schema Key |
| 412 |
@param[in] oldpath old file path as a null-terminated string |
| 413 |
@param[in] newpath new file path |
| 414 |
@param[in] src_location location where func invoked |
| 415 |
@return true if success */ |
| 416 |
static inline bool pfs_os_file_rename_func(mysql_pfs_key_t key, |
| 417 |
const char *oldpath, |
| 418 |
const char *newpath, |
| 419 |
ut::Location src_location) |
| 420 |
|
| 421 |
{ |
| 422 |
PSI_file_locker_state state; |
| 423 |
struct PSI_file_locker *locker = nullptr; |
| 424 |
|
| 425 |
register_pfs_file_rename_begin(&state, locker, key, PSI_FILE_RENAME, oldpath, |
| 426 |
newpath, src_location); |
| 427 |
|
| 428 |
bool result = os_file_rename_func(oldpath, newpath); |
| 429 |
|
| 430 |
register_pfs_file_rename_end(locker, oldpath, newpath, !result); |
| 431 |
|
| 432 |
return (result); |
| 433 |
} |
| 434 |
|
| 435 |
/** NOTE! Please use the corresponding macro os_file_delete(), not directly |
| 436 |
this function! |
| 437 |
This is the performance schema instrumented wrapper function for |
| 438 |
os_file_delete() |
| 439 |
@param[in] key Performance Schema Key |
| 440 |
@param[in] name old file path as a null-terminated string |
| 441 |
@param[in] src_location location where func invoked |
| 442 |
@return true if success */ |
| 443 |
static inline bool pfs_os_file_delete_func(mysql_pfs_key_t key, |
| 444 |
const char *name, |
| 445 |
ut::Location src_location) { |
| 446 |
PSI_file_locker_state state; |
| 447 |
struct PSI_file_locker *locker = nullptr; |
| 448 |
|
| 449 |
register_pfs_file_close_begin(&state, locker, key, PSI_FILE_DELETE, name, |
| 450 |
src_location); |
| 451 |
|
| 452 |
bool result = os_file_delete_func(name); |
| 453 |
|
| 454 |
register_pfs_file_close_end(locker, 0); |
| 455 |
|
| 456 |
return (result); |
| 457 |
} |
| 458 |
|
| 459 |
/** |
| 460 |
NOTE! Please use the corresponding macro os_file_delete_if_exists(), not |
| 461 |
directly this function! |
| 462 |
This is the performance schema instrumented wrapper function for |
| 463 |
os_file_delete_if_exists() |
| 464 |
@param[in] key Performance Schema Key |
| 465 |
@param[in] name old file path as a null-terminated string |
| 466 |
@param[in] exist indicate if file pre-exist |
| 467 |
@param[in] src_location location where func invoked |
| 468 |
@return true if success */ |
| 469 |
static inline bool pfs_os_file_delete_if_exists_func( |
| 470 |
mysql_pfs_key_t key, const char *name, bool *exist, |
| 471 |
ut::Location src_location) { |
| 472 |
PSI_file_locker_state state; |
| 473 |
struct PSI_file_locker *locker = nullptr; |
| 474 |
|
| 475 |
register_pfs_file_close_begin(&state, locker, key, PSI_FILE_DELETE, name, |
| 476 |
src_location); |
| 477 |
|
| 478 |
bool result = os_file_delete_if_exists_func(name, exist); |
| 479 |
|
| 480 |
register_pfs_file_close_end(locker, 0); |
| 481 |
|
| 482 |
return (result); |
| 483 |
} |
| 484 |
#endif /* UNIV_PFS_IO */ |