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