MDEV-33379 innodb_log_file_buffering=OFF causes corruption on bcachefs

Apparently, invoking fcntl(fd, F_SETFL, O_DIRECT) will lead to
unexpected behaviour on Linux bcachefs and possibly other file systems,
depending on the operating system version. So, let us avoid doing that,
and instead just attempt to pass the O_DIRECT flag to open(). This should
make us compatible with NetBSD, IBM AIX, as well as Solaris and its
derivatives.

This fix does not change the fact that we had only implemented
innodb_log_file_buffering=OFF on systems where we can determine the
physical block size (typically 512 or 4096 bytes).
Currently, those operating systems are Linux and Microsoft Windows.

HAVE_FCNTL_DIRECT, os_file_set_nocache(): Remove.

OS_FILE_OVERWRITE, OS_FILE_CREATE_PATH: Remove (never used parameters).

os_file_log_buffered(), os_file_log_maybe_unbuffered(): Helper functions.

os_file_create_simple_func(): When applicable, initially attempt to
open files in O_DIRECT mode.

os_file_create_func(): When applicable, initially attempt to
open files in O_DIRECT mode.
For type==OS_LOG_FILE && create_mode != OS_FILE_CREATE
we will first invoke stat(2) on the file name to find out if the size
is compatible with O_DIRECT. If create_mode == OS_FILE_CREATE, we will
invoke fstat(2) on the created log file afterwards, and may close and
reopen the file in O_DIRECT mode if applicable.

create_temp_file(): Support O_DIRECT. This is only used if O_TMPFILE is
available and innodb_disable_sort_file_cache=ON (non-default value).
Notably, that setting never worked on Microsoft Windows.

row_merge_file_create_mode(): Split from row_merge_file_create_low().
Create a temporary file in the specified mode.

Reviewed by: Vladislav Vaintroub
This commit is contained in:
Marko Mäkelä 2024-02-20 11:22:45 +02:00
parent 4039d8605d
commit 7f7329f092
16 changed files with 227 additions and 304 deletions

View file

@ -34,8 +34,5 @@ ELSE()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGE_FILES -maix64 -pthread -mcmodel=large") SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGE_FILES -maix64 -pthread -mcmodel=large")
ENDIF() ENDIF()
# fcntl(fd, F_SETFL, O_DIRECT) is not supported; O_DIRECT is an open(2) flag
SET(HAVE_FCNTL_DIRECT 0 CACHE INTERNAL "")
# make it WARN by default, not AUTO (that implies -Werror) # make it WARN by default, not AUTO (that implies -Werror)
SET(MYSQL_MAINTAINER_MODE "WARN" CACHE STRING "Enable MariaDB maintainer-specific warnings. One of: NO (warnings are disabled) WARN (warnings are enabled) ERR (warnings are errors) AUTO (warnings are errors in Debug only)") SET(MYSQL_MAINTAINER_MODE "WARN" CACHE STRING "Enable MariaDB maintainer-specific warnings. One of: NO (warnings are disabled) WARN (warnings are enabled) ERR (warnings are errors) AUTO (warnings are errors in Debug only)")

View file

@ -17,10 +17,6 @@ INCLUDE(CheckSymbolExists)
INCLUDE(CheckCSourceRuns) INCLUDE(CheckCSourceRuns)
INCLUDE(CheckCSourceCompiles) INCLUDE(CheckCSourceCompiles)
# fcntl(fd, F_SETFL, O_DIRECT) is not supported,
# and directio(3C) would only work on UFS or NFS, not ZFS.
SET(HAVE_FCNTL_DIRECT 0 CACHE INTERNAL "")
# Enable 64 bit file offsets # Enable 64 bit file offsets
SET(_FILE_OFFSET_BITS 64) SET(_FILE_OFFSET_BITS 64)

View file

@ -44,7 +44,6 @@ SET(HAVE_EXECINFO_H CACHE INTERNAL "")
SET(HAVE_FCHMOD CACHE INTERNAL "") SET(HAVE_FCHMOD CACHE INTERNAL "")
SET(HAVE_FCNTL CACHE INTERNAL "") SET(HAVE_FCNTL CACHE INTERNAL "")
SET(HAVE_FCNTL_H 1 CACHE INTERNAL "") SET(HAVE_FCNTL_H 1 CACHE INTERNAL "")
SET(HAVE_FCNTL_DIRECT 0 CACHE INTERNAL "")
SET(HAVE_FCNTL_NONBLOCK CACHE INTERNAL "") SET(HAVE_FCNTL_NONBLOCK CACHE INTERNAL "")
SET(HAVE_FDATASYNC CACHE INTERNAL "") SET(HAVE_FDATASYNC CACHE INTERNAL "")
SET(HAVE_DECL_FDATASYNC CACHE INTERNAL "") SET(HAVE_DECL_FDATASYNC CACHE INTERNAL "")

View file

@ -30,7 +30,6 @@
#cmakedefine HAVE_DLFCN_H 1 #cmakedefine HAVE_DLFCN_H 1
#cmakedefine HAVE_EXECINFO_H 1 #cmakedefine HAVE_EXECINFO_H 1
#cmakedefine HAVE_FCNTL_H 1 #cmakedefine HAVE_FCNTL_H 1
#cmakedefine HAVE_FCNTL_DIRECT 1
#cmakedefine HAVE_FENV_H 1 #cmakedefine HAVE_FENV_H 1
#cmakedefine HAVE_FLOAT_H 1 #cmakedefine HAVE_FLOAT_H 1
#cmakedefine HAVE_FNMATCH_H 1 #cmakedefine HAVE_FNMATCH_H 1

View file

@ -706,7 +706,6 @@ CHECK_SYMBOL_EXISTS(O_NONBLOCK "unistd.h;fcntl.h" HAVE_FCNTL_NONBLOCK)
IF(NOT HAVE_FCNTL_NONBLOCK) IF(NOT HAVE_FCNTL_NONBLOCK)
SET(NO_FCNTL_NONBLOCK 1) SET(NO_FCNTL_NONBLOCK 1)
ENDIF() ENDIF()
CHECK_SYMBOL_EXISTS(O_DIRECT "fcntl.h" HAVE_FCNTL_DIRECT)
# #
# Test for how the C compiler does inline, if at all # Test for how the C compiler does inline, if at all

View file

@ -23,7 +23,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
#include <my_global.h> #include <my_global.h>
#include <mysql_version.h> #include <mysql_version.h>
#include <fcntl.h>
#include <stdarg.h> #include <stdarg.h>
#include <my_sys.h> #include <my_sys.h>

View file

@ -199,14 +199,6 @@ xb_fil_cur_open(
return(XB_FIL_CUR_SKIP); return(XB_FIL_CUR_SKIP);
} }
#ifdef HAVE_FCNTL_DIRECT
if (srv_file_flush_method == SRV_O_DIRECT
|| srv_file_flush_method == SRV_O_DIRECT_NO_FSYNC) {
os_file_set_nocache(cursor->file, node->name, "OPEN");
}
#endif
posix_fadvise(cursor->file, 0, 0, POSIX_FADV_SEQUENTIAL); posix_fadvise(cursor->file, 0, 0, POSIX_FADV_SEQUENTIAL);
cursor->page_size = node->space->physical_size(); cursor->page_size = node->space->physical_size();

View file

@ -54,7 +54,6 @@ Street, Fifth Floor, Boston, MA 02110-1335 USA
#include <scope.h> #include <scope.h>
#include <sql_class.h> #include <sql_class.h>
#include <fcntl.h>
#include <string.h> #include <string.h>
#ifdef __linux__ #ifdef __linux__
@ -2428,7 +2427,7 @@ static bool innodb_init()
os_file_delete_if_exists_func(ib_logfile0.c_str(), nullptr); os_file_delete_if_exists_func(ib_logfile0.c_str(), nullptr);
os_file_t file= os_file_create_func(ib_logfile0.c_str(), os_file_t file= os_file_create_func(ib_logfile0.c_str(),
OS_FILE_CREATE, OS_FILE_NORMAL, OS_FILE_CREATE, OS_FILE_NORMAL,
#if defined _WIN32 || defined HAVE_FCNTL_DIRECT #if defined _WIN32 || defined O_DIRECT
OS_DATA_FILE_NO_O_DIRECT, OS_DATA_FILE_NO_O_DIRECT,
#else #else
OS_DATA_FILE, OS_DATA_FILE,

View file

@ -66,7 +66,11 @@ File create_temp_file(char *to, const char *dir, const char *prefix,
DBUG_ENTER("create_temp_file"); DBUG_ENTER("create_temp_file");
DBUG_PRINT("enter", ("dir: %s, prefix: %s", dir ? dir : "(null)", prefix)); DBUG_PRINT("enter", ("dir: %s, prefix: %s", dir ? dir : "(null)", prefix));
#if !defined _WIN32 && defined O_DIRECT
DBUG_ASSERT((mode & (O_EXCL | O_TRUNC | O_CREAT | O_RDWR | O_DIRECT)) == 0);
#else
DBUG_ASSERT((mode & (O_EXCL | O_TRUNC | O_CREAT | O_RDWR)) == 0); DBUG_ASSERT((mode & (O_EXCL | O_TRUNC | O_CREAT | O_RDWR)) == 0);
#endif
mode|= O_TRUNC | O_CREAT | O_RDWR; /* not O_EXCL, see Windows code below */ mode|= O_TRUNC | O_CREAT | O_RDWR; /* not O_EXCL, see Windows code below */
@ -118,16 +122,41 @@ File create_temp_file(char *to, const char *dir, const char *prefix,
if ((MyFlags & MY_TEMPORARY) && O_TMPFILE_works) if ((MyFlags & MY_TEMPORARY) && O_TMPFILE_works)
{ {
/* explictly don't use O_EXCL here has it has a different /*
meaning with O_TMPFILE explicitly don't use O_EXCL here has it has a different
meaning with O_TMPFILE
*/ */
if ((file= open(dir, (mode & ~O_CREAT) | O_TMPFILE | O_CLOEXEC, const int flags= (mode & ~O_CREAT) | O_TMPFILE | O_CLOEXEC;
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)) >= 0) const mode_t open_mode= S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
# ifdef O_DIRECT
static int O_TMPFILE_works_with_O_DIRECT= O_DIRECT;
const int try_O_DIRECT= mode & O_TMPFILE_works_with_O_DIRECT;
if (try_O_DIRECT)
file= open(dir, flags | O_DIRECT, open_mode);
else
# endif
file= open(dir, flags, open_mode);
if (file >= 0)
{ {
# ifdef O_DIRECT
O_TMPFILE_works:
# endif
my_snprintf(to, FN_REFLEN, "%s/#sql/fd=%d", dir, file); my_snprintf(to, FN_REFLEN, "%s/#sql/fd=%d", dir, file);
file=my_register_filename(file, to, FILE_BY_O_TMPFILE, file=my_register_filename(file, to, FILE_BY_O_TMPFILE,
EE_CANTCREATEFILE, MyFlags); EE_CANTCREATEFILE, MyFlags);
} }
# ifdef O_DIRECT
else if (errno == EINVAL && try_O_DIRECT)
{
file= open(dir, flags, open_mode);
if (file >= 0)
{
O_TMPFILE_works_with_O_DIRECT= 0;
goto O_TMPFILE_works;
}
}
# endif
else if (errno == EOPNOTSUPP || errno == EINVAL) else if (errno == EOPNOTSUPP || errno == EINVAL)
{ {
my_printf_error(EE_CANTCREATEFILE, "O_TMPFILE is not supported on %s " my_printf_error(EE_CANTCREATEFILE, "O_TMPFILE is not supported on %s "

View file

@ -342,7 +342,7 @@ static bool fil_node_open_file_low(fil_node_t *node)
ut_ad(node->space->is_closing()); ut_ad(node->space->is_closing());
mysql_mutex_assert_owner(&fil_system.mutex); mysql_mutex_assert_owner(&fil_system.mutex);
static_assert(((UNIV_ZIP_SIZE_MIN >> 1) << 3) == 4096, "compatibility"); static_assert(((UNIV_ZIP_SIZE_MIN >> 1) << 3) == 4096, "compatibility");
#if defined _WIN32 || defined HAVE_FCNTL_DIRECT #if defined _WIN32 || defined O_DIRECT
ulint type; ulint type;
switch (FSP_FLAGS_GET_ZIP_SSIZE(node->space->flags)) { switch (FSP_FLAGS_GET_ZIP_SSIZE(node->space->flags)) {
case 1: case 1:
@ -1906,7 +1906,7 @@ fil_ibd_create(
static_assert(((UNIV_ZIP_SIZE_MIN >> 1) << 3) == 4096, static_assert(((UNIV_ZIP_SIZE_MIN >> 1) << 3) == 4096,
"compatibility"); "compatibility");
#if defined _WIN32 || defined HAVE_FCNTL_DIRECT #if defined _WIN32 || defined O_DIRECT
ulint type; ulint type;
switch (FSP_FLAGS_GET_ZIP_SSIZE(flags)) { switch (FSP_FLAGS_GET_ZIP_SSIZE(flags)) {
case 1: case 1:

View file

@ -4007,7 +4007,7 @@ static int innodb_init_params()
data_mysql_default_charset_coll = (ulint) default_charset_info->number; data_mysql_default_charset_coll = (ulint) default_charset_info->number;
#ifdef HAVE_FCNTL_DIRECT #if !defined _WIN32 && defined O_DIRECT
if (srv_use_atomic_writes && my_may_have_atomic_write) { if (srv_use_atomic_writes && my_may_have_atomic_write) {
/* /*
Force O_DIRECT on Unixes (on Windows writes are always Force O_DIRECT on Unixes (on Windows writes are always

View file

@ -63,7 +63,7 @@ enum srv_flush_t
SRV_LITTLESYNC, SRV_LITTLESYNC,
/** do not flush after writing */ /** do not flush after writing */
SRV_NOSYNC, SRV_NOSYNC,
/** invoke os_file_set_nocache() on data files. This implies using /** Open or create files with O_DIRECT. This implies using
unbuffered I/O but still fdatasync(), because some filesystems might unbuffered I/O but still fdatasync(), because some filesystems might
not flush meta-data on write completion */ not flush meta-data on write completion */
SRV_O_DIRECT, SRV_O_DIRECT,

View file

@ -113,11 +113,8 @@ enum os_file_create_t {
doesn't exist, error) */ doesn't exist, error) */
OS_FILE_CREATE, /*!< to create new file (if OS_FILE_CREATE, /*!< to create new file (if
exists, error) */ exists, error) */
OS_FILE_OVERWRITE, /*!< to create a new file, if exists
the overwrite old file */
OS_FILE_OPEN_RAW, /*!< to open a raw device or disk OS_FILE_OPEN_RAW, /*!< to open a raw device or disk
partition */ partition */
OS_FILE_CREATE_PATH, /*!< to create the directories */
OS_FILE_OPEN_RETRY, /*!< open with retry */ OS_FILE_OPEN_RETRY, /*!< open with retry */
/** Flags that can be combined with the above values. Please ensure /** Flags that can be combined with the above values. Please ensure
@ -144,7 +141,7 @@ static const ulint OS_FILE_NORMAL = 62;
/** Types for file create @{ */ /** Types for file create @{ */
static constexpr ulint OS_DATA_FILE = 100; static constexpr ulint OS_DATA_FILE = 100;
static constexpr ulint OS_LOG_FILE = 101; static constexpr ulint OS_LOG_FILE = 101;
#if defined _WIN32 || defined HAVE_FCNTL_DIRECT #if defined _WIN32 || defined O_DIRECT
static constexpr ulint OS_DATA_FILE_NO_O_DIRECT = 103; static constexpr ulint OS_DATA_FILE_NO_O_DIRECT = 103;
#endif #endif
/* @} */ /* @} */
@ -375,22 +372,6 @@ os_file_create_simple_no_error_handling_func(
bool* success) bool* success)
MY_ATTRIBUTE((warn_unused_result)); MY_ATTRIBUTE((warn_unused_result));
#ifndef HAVE_FCNTL_DIRECT
#define os_file_set_nocache(fd, file_name, operation_name) do{}while(0)
#else
/** Tries to disable OS caching on an opened file descriptor.
@param[in] fd file descriptor to alter
@param[in] file_name file name, used in the diagnostic message
@param[in] name "open" or "create"; used in the diagnostic
message */
void
os_file_set_nocache(
/*================*/
int fd, /*!< in: file descriptor to alter */
const char* file_name,
const char* operation_name);
#endif
#ifndef _WIN32 /* On Microsoft Windows, mandatory locking is used */ #ifndef _WIN32 /* On Microsoft Windows, mandatory locking is used */
/** Obtain an exclusive lock on a file. /** Obtain an exclusive lock on a file.
@param fd file descriptor @param fd file descriptor

View file

@ -165,14 +165,11 @@ row_merge_drop_indexes(
prepare_inplace_alter_table_dict(). */ prepare_inplace_alter_table_dict(). */
void row_merge_drop_temp_indexes(); void row_merge_drop_temp_indexes();
/** Create temporary merge files in the given paramater path, and if /** Create a temporary file at the specified path.
UNIV_PFS_IO defined, register the file descriptor with Performance Schema. @param path location for creating temporary merge files, or nullptr
@param[in] path location for creating temporary merge files, or NULL
@return File descriptor */ @return File descriptor */
pfs_os_file_t pfs_os_file_t row_merge_file_create_low(const char *path)
row_merge_file_create_low( MY_ATTRIBUTE((warn_unused_result));
const char* path)
MY_ATTRIBUTE((warn_unused_result));
/*********************************************************************//** /*********************************************************************//**
Destroy a merge file. And de-register the file from Performance Schema Destroy a merge file. And de-register the file from Performance Schema
if UNIV_PFS_IO is defined. */ if UNIV_PFS_IO is defined. */

View file

@ -974,57 +974,19 @@ os_file_create_simple_func(
*success = false; *success = false;
int create_flag; int create_flag = O_RDONLY | O_CLOEXEC;
const char* mode_str __attribute__((unused));
ut_a(!(create_mode & OS_FILE_ON_ERROR_SILENT)); ut_a(!(create_mode & OS_FILE_ON_ERROR_SILENT));
ut_a(!(create_mode & OS_FILE_ON_ERROR_NO_EXIT)); ut_a(!(create_mode & OS_FILE_ON_ERROR_NO_EXIT));
if (create_mode == OS_FILE_OPEN) { if (read_only) {
mode_str = "OPEN"; } else if (create_mode == OS_FILE_OPEN) {
if (access_type != OS_FILE_READ_ONLY) {
if (access_type == OS_FILE_READ_ONLY) { create_flag = O_RDWR | O_CLOEXEC;
create_flag = O_RDONLY;
} else if (read_only) {
create_flag = O_RDONLY;
} else {
create_flag = O_RDWR;
} }
} else if (read_only) {
mode_str = "OPEN";
create_flag = O_RDONLY;
} else if (create_mode == OS_FILE_CREATE) { } else if (create_mode == OS_FILE_CREATE) {
create_flag = O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC;
mode_str = "CREATE";
create_flag = O_RDWR | O_CREAT | O_EXCL;
} else if (create_mode == OS_FILE_CREATE_PATH) {
mode_str = "CREATE PATH";
/* Create subdirs along the path if needed. */
*success = os_file_create_subdirs_if_needed(name);
if (!*success) {
ib::error()
<< "Unable to create subdirectories '"
<< name << "'";
return(OS_FILE_CLOSED);
}
create_flag = O_RDWR | O_CREAT | O_EXCL;
create_mode = OS_FILE_CREATE;
} else { } else {
ib::error() ib::error()
<< "Unknown file create mode (" << "Unknown file create mode ("
<< create_mode << create_mode
@ -1035,10 +997,33 @@ os_file_create_simple_func(
bool retry; bool retry;
#ifdef O_DIRECT
int direct_flag = 0;
/* This function is always called for data files, we should disable
OS caching (O_DIRECT) here as we do in os_file_create_func(), so
we open the same file in the same mode, see man page of open(2). */
switch (srv_file_flush_method) {
case SRV_O_DSYNC:
case SRV_O_DIRECT:
case SRV_O_DIRECT_NO_FSYNC:
direct_flag = O_DIRECT;
break;
}
#else
constexpr int direct_flag = 0;
#endif
do { do {
file = open(name, create_flag | O_CLOEXEC, os_innodb_umask); file = open(name, create_flag | direct_flag, os_innodb_umask);
if (file == -1) { if (file == -1) {
#ifdef O_DIRECT
if (direct_flag && errno == EINVAL) {
direct_flag = 0;
retry = true;
continue;
}
#endif
*success = false; *success = false;
retry = os_file_handle_error( retry = os_file_handle_error(
name, name,
@ -1051,24 +1036,6 @@ os_file_create_simple_func(
} while (retry); } while (retry);
#ifdef HAVE_FCNTL_DIRECT
/* This function is always called for data files, we should disable
OS caching (O_DIRECT) here as we do in os_file_create_func(), so
we open the same file in the same mode, see man page of open(2). */
if (!srv_read_only_mode && *success) {
switch (srv_file_flush_method) {
case SRV_O_DSYNC:
case SRV_O_DIRECT:
case SRV_O_DIRECT_NO_FSYNC:
os_file_set_nocache(file, name, mode_str);
break;
default:
break;
}
}
#endif
#ifndef _WIN32
if (!read_only if (!read_only
&& *success && *success
&& access_type == OS_FILE_READ_WRITE && access_type == OS_FILE_READ_WRITE
@ -1079,7 +1046,6 @@ os_file_create_simple_func(
close(file); close(file);
file = -1; file = -1;
} }
#endif /* !_WIN32 */
return(file); return(file);
} }
@ -1113,6 +1079,61 @@ os_file_create_directory(
return(true); return(true);
} }
#ifdef O_DIRECT
# if defined __linux
/** Note that the log file uses buffered I/O. */
static ATTRIBUTE_COLD void os_file_log_buffered()
{
log_sys.log_maybe_unbuffered= false;
log_sys.log_buffered= true;
log_sys.set_block_size(512);
}
# endif
/** @return whether the log file may work with unbuffered I/O. */
static ATTRIBUTE_COLD bool os_file_log_maybe_unbuffered(const struct stat &st)
{
MSAN_STAT_WORKAROUND(&st);
# ifdef __linux__
char b[20 + sizeof "/sys/dev/block/" ":" "/../queue/physical_block_size"];
if (snprintf(b, sizeof b, "/sys/dev/block/%u:%u/queue/physical_block_size",
major(st.st_dev), minor(st.st_dev)) >=
static_cast<int>(sizeof b))
return false;
int f= open(b, O_RDONLY);
if (f == -1)
{
if (snprintf(b, sizeof b, "/sys/dev/block/%u:%u/../queue/"
"physical_block_size",
major(st.st_dev), minor(st.st_dev)) >=
static_cast<int>(sizeof b))
return false;
f= open(b, O_RDONLY);
}
unsigned long s= 0;
if (f != -1)
{
ssize_t l= read(f, b, sizeof b);
if (l > 0 && size_t(l) < sizeof b && b[l - 1] == '\n')
{
char *end= b;
s= strtoul(b, &end, 10);
if (b == end || *end != '\n')
s = 0;
}
close(f);
}
if (s > 4096 || s < 64 || !ut_is_2pow(s))
return false;
log_sys.set_block_size(uint32_t(s));
# else
constexpr unsigned long s= 4096;
# endif
return !(st.st_size & (s - 1));
}
#endif
/** NOTE! Use the corresponding macro os_file_create(), not directly /** NOTE! Use the corresponding macro os_file_create(), not directly
this function! this function!
Opens an existing file or creates a new. Opens an existing file or creates a new.
@ -1151,8 +1172,7 @@ os_file_create_func(
return(OS_FILE_CLOSED); return(OS_FILE_CLOSED);
); );
int create_flag; int create_flag;
const char* mode_str __attribute__((unused));
on_error_no_exit = create_mode & OS_FILE_ON_ERROR_NO_EXIT on_error_no_exit = create_mode & OS_FILE_ON_ERROR_NO_EXIT
? true : false; ? true : false;
@ -1162,30 +1182,14 @@ os_file_create_func(
create_mode &= ulint(~(OS_FILE_ON_ERROR_NO_EXIT create_mode &= ulint(~(OS_FILE_ON_ERROR_NO_EXIT
| OS_FILE_ON_ERROR_SILENT)); | OS_FILE_ON_ERROR_SILENT));
if (create_mode == OS_FILE_OPEN if (read_only) {
|| create_mode == OS_FILE_OPEN_RAW create_flag = O_RDONLY | O_CLOEXEC;
|| create_mode == OS_FILE_OPEN_RETRY) { } else if (create_mode == OS_FILE_OPEN
|| create_mode == OS_FILE_OPEN_RAW
mode_str = "OPEN"; || create_mode == OS_FILE_OPEN_RETRY) {
create_flag = O_RDWR | O_CLOEXEC;
create_flag = read_only ? O_RDONLY : O_RDWR;
} else if (read_only) {
mode_str = "OPEN";
create_flag = O_RDONLY;
} else if (create_mode == OS_FILE_CREATE) { } else if (create_mode == OS_FILE_CREATE) {
create_flag = O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC;
mode_str = "CREATE";
create_flag = O_RDWR | O_CREAT | O_EXCL;
} else if (create_mode == OS_FILE_OVERWRITE) {
mode_str = "OVERWRITE";
create_flag = O_RDWR | O_CREAT | O_TRUNC;
} else { } else {
ib::error() ib::error()
<< "Unknown file create mode (" << create_mode << ")" << "Unknown file create mode (" << create_mode << ")"
@ -1194,10 +1198,46 @@ os_file_create_func(
return(OS_FILE_CLOSED); return(OS_FILE_CLOSED);
} }
#ifdef HAVE_FCNTL_DIRECT #ifdef O_DIRECT
struct stat st;
ut_a(type == OS_LOG_FILE ut_a(type == OS_LOG_FILE
|| type == OS_DATA_FILE || type == OS_DATA_FILE || type == OS_DATA_FILE_NO_O_DIRECT);
|| type == OS_DATA_FILE_NO_O_DIRECT); int direct_flag = 0;
if (type == OS_DATA_FILE) {
switch (srv_file_flush_method) {
case SRV_O_DSYNC:
case SRV_O_DIRECT:
case SRV_O_DIRECT_NO_FSYNC:
direct_flag = O_DIRECT;
break;
default:
break;
}
# ifdef __linux__
} else if (type != OS_LOG_FILE) {
} else if (log_sys.log_buffered) {
skip_o_direct:
os_file_log_buffered();
} else if (create_mode != OS_FILE_CREATE && !log_sys.is_opened()) {
if (stat(name, &st)) {
if (errno == ENOENT) {
if (on_error_silent) goto not_found;
sql_print_error(
"InnoDB: File %s was not found", name);
goto not_found;
}
goto skip_o_direct;
}
if (!os_file_log_maybe_unbuffered(st)) {
goto skip_o_direct;
}
direct_flag = O_DIRECT;
log_sys.log_maybe_unbuffered= true;
# endif
}
#else #else
ut_a(type == OS_LOG_FILE || type == OS_DATA_FILE); ut_a(type == OS_LOG_FILE || type == OS_DATA_FILE);
#endif #endif
@ -1219,9 +1259,26 @@ os_file_create_func(
bool retry; bool retry;
do { do {
file = open(name, create_flag | O_CLOEXEC, os_innodb_umask); file = open(name, create_flag | direct_flag, os_innodb_umask);
if (file == -1) { if (file == -1) {
#ifdef O_DIRECT
if (direct_flag && errno == EINVAL) {
direct_flag = 0;
# ifdef __linux__
if (type == OS_LOG_FILE) {
os_file_log_buffered();
}
# endif
if (create_mode == OS_FILE_CREATE) {
/* Linux may create the file
before rejecting the O_DIRECT. */
unlink(name);
}
retry = true;
continue;
}
#endif
const char* operation; const char* operation;
operation = (create_mode == OS_FILE_CREATE operation = (create_mode == OS_FILE_CREATE
@ -1243,82 +1300,25 @@ os_file_create_func(
} while (retry); } while (retry);
if (!*success) { if (!*success) {
return file; #ifdef __linux__
not_found:
#endif
return OS_FILE_CLOSED;
} }
#ifdef HAVE_FCNTL_DIRECT #ifdef __linux__
if (type == OS_DATA_FILE) { if (!read_only && create_mode == OS_FILE_CREATE
switch (srv_file_flush_method) { && type == OS_LOG_FILE) {
case SRV_O_DSYNC: if (fstat(file, &st) || !os_file_log_maybe_unbuffered(st)) {
case SRV_O_DIRECT: os_file_log_buffered();
case SRV_O_DIRECT_NO_FSYNC:
# ifdef __linux__
use_o_direct:
# endif
os_file_set_nocache(file, name, mode_str);
break;
default:
break;
}
}
# ifdef __linux__
else if (type == OS_LOG_FILE && !log_sys.is_opened()) {
struct stat st;
char b[20 + sizeof "/sys/dev/block/" ":"
"/../queue/physical_block_size"];
int f;
if (fstat(file, &st)) {
goto skip_o_direct;
}
MSAN_STAT_WORKAROUND(&st);
if (snprintf(b, sizeof b,
"/sys/dev/block/%u:%u/queue/physical_block_size",
major(st.st_dev), minor(st.st_dev))
>= static_cast<int>(sizeof b)) {
goto skip_o_direct;
}
if ((f = open(b, O_RDONLY)) == -1) {
if (snprintf(b, sizeof b,
"/sys/dev/block/%u:%u/../queue/"
"physical_block_size",
major(st.st_dev), minor(st.st_dev))
>= static_cast<int>(sizeof b)) {
goto skip_o_direct;
}
f = open(b, O_RDONLY);
}
if (f != -1) {
ssize_t l = read(f, b, sizeof b);
unsigned long s = 0;
if (l > 0 && static_cast<size_t>(l) < sizeof b
&& b[l - 1] == '\n') {
char* end = b;
s = strtoul(b, &end, 10);
if (b == end || *end != '\n') {
s = 0;
}
}
close(f);
if (s > 4096 || s < 64 || !ut_is_2pow(s)) {
goto skip_o_direct;
}
log_sys.log_maybe_unbuffered= true;
log_sys.set_block_size(uint32_t(s));
if (!log_sys.log_buffered && !(st.st_size & (s - 1))) {
goto use_o_direct;
}
} else { } else {
skip_o_direct: close(file);
log_sys.log_maybe_unbuffered= false; return os_file_create_func(name, OS_FILE_OPEN, purpose,
log_sys.log_buffered= true; type, false, success);
log_sys.set_block_size(512);
} }
} }
# endif
#endif #endif
#ifndef _WIN32
if (!read_only if (!read_only
&& create_mode != OS_FILE_OPEN_RAW && create_mode != OS_FILE_OPEN_RAW
&& !my_disable_locking && !my_disable_locking
@ -1346,7 +1346,6 @@ skip_o_direct:
close(file); close(file);
file = -1; file = -1;
} }
#endif /* !_WIN32 */
return(file); return(file);
} }
@ -1373,38 +1372,24 @@ os_file_create_simple_no_error_handling_func(
bool* success) bool* success)
{ {
os_file_t file; os_file_t file;
int create_flag; int create_flag = O_RDONLY | O_CLOEXEC;
ut_a(!(create_mode & OS_FILE_ON_ERROR_SILENT)); ut_a(!(create_mode & OS_FILE_ON_ERROR_SILENT));
ut_a(!(create_mode & OS_FILE_ON_ERROR_NO_EXIT)); ut_a(!(create_mode & OS_FILE_ON_ERROR_NO_EXIT));
*success = false; *success = false;
if (create_mode == OS_FILE_OPEN) { if (read_only) {
} else if (create_mode == OS_FILE_OPEN) {
if (access_type == OS_FILE_READ_ONLY) { if (access_type != OS_FILE_READ_ONLY) {
create_flag = O_RDONLY;
} else if (read_only) {
create_flag = O_RDONLY;
} else {
ut_a(access_type == OS_FILE_READ_WRITE ut_a(access_type == OS_FILE_READ_WRITE
|| access_type == OS_FILE_READ_ALLOW_DELETE); || access_type == OS_FILE_READ_ALLOW_DELETE);
create_flag = O_RDWR; create_flag = O_RDWR;
} }
} else if (create_mode == OS_FILE_CREATE) {
} else if (read_only) { create_flag = O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC;
create_flag = O_RDONLY;
} else if (create_mode == OS_FILE_CREATE) {
create_flag = O_RDWR | O_CREAT | O_EXCL;
} else { } else {
@ -1415,11 +1400,10 @@ os_file_create_simple_no_error_handling_func(
return(OS_FILE_CLOSED); return(OS_FILE_CLOSED);
} }
file = open(name, create_flag | O_CLOEXEC, os_innodb_umask); file = open(name, create_flag, os_innodb_umask);
*success = (file != -1); *success = (file != -1);
#ifndef _WIN32
if (!read_only if (!read_only
&& *success && *success
&& access_type == OS_FILE_READ_WRITE && access_type == OS_FILE_READ_WRITE
@ -1431,7 +1415,6 @@ os_file_create_simple_no_error_handling_func(
file = -1; file = -1;
} }
#endif /* !_WIN32 */
return(file); return(file);
} }
@ -1953,23 +1936,6 @@ os_file_create_simple_func(
create_flag = CREATE_NEW; create_flag = CREATE_NEW;
} else if (create_mode == OS_FILE_CREATE_PATH) {
/* Create subdirs along the path if needed. */
*success = os_file_create_subdirs_if_needed(name);
if (!*success) {
ib::error()
<< "Unable to create subdirectories '"
<< name << "'";
return(OS_FILE_CLOSED);
}
create_flag = CREATE_NEW;
create_mode = OS_FILE_CREATE;
} else { } else {
ib::error() ib::error()
@ -2158,10 +2124,6 @@ os_file_create_func(
create_flag = CREATE_NEW; create_flag = CREATE_NEW;
} else if (create_mode == OS_FILE_OVERWRITE) {
create_flag = CREATE_ALWAYS;
} else { } else {
ib::error() ib::error()
<< "Unknown file create mode (" << create_mode << ") " << "Unknown file create mode (" << create_mode << ") "
@ -3045,36 +3007,6 @@ os_file_handle_error_cond_exit(
return(false); return(false);
} }
#ifdef HAVE_FCNTL_DIRECT
/** Tries to disable OS caching on an opened file descriptor.
@param[in] fd file descriptor to alter
@param[in] file_name file name, used in the diagnostic message
@param[in] name "open" or "create"; used in the diagnostic
message */
void
os_file_set_nocache(int fd, const char *file_name, const char *operation_name)
{
if (fcntl(fd, F_SETFL, O_DIRECT) == -1) {
int errno_save = errno;
static bool warning_message_printed = false;
if (errno_save == EINVAL) {
if (!warning_message_printed) {
warning_message_printed = true;
ib::info()
<< "Setting O_DIRECT on file "
<< file_name << " failed";
}
} else {
ib::warn()
<< "Failed to set O_DIRECT on file "
<< file_name << "; " << operation_name
<< " : " << strerror(errno_save)
<< ", continuing anyway.";
}
}
}
#endif /* HAVE_FCNTL_DIRECT */
/** Check if the file system supports sparse files. /** Check if the file system supports sparse files.
@param fh file handle @param fh file handle
@return true if the file system supports sparse files */ @return true if the file system supports sparse files */

View file

@ -4349,9 +4349,7 @@ void row_merge_drop_temp_indexes()
UNIV_PFS_IO defined, register the file descriptor with Performance Schema. UNIV_PFS_IO defined, register the file descriptor with Performance Schema.
@param[in] path location for creating temporary merge files, or NULL @param[in] path location for creating temporary merge files, or NULL
@return File descriptor */ @return File descriptor */
pfs_os_file_t static pfs_os_file_t row_merge_file_create_mode(const char *path, int mode)
row_merge_file_create_low(
const char* path)
{ {
if (!path) { if (!path) {
path = mysql_tmpdir; path = mysql_tmpdir;
@ -4392,6 +4390,13 @@ row_merge_file_create_low(
return(fd); return(fd);
} }
/** Create a temporary file at the specified path.
@param path location for creating temporary merge files, or nullptr
@return File descriptor */
pfs_os_file_t row_merge_file_create_low(const char *path)
{
return row_merge_file_create_mode(path, O_BINARY | O_SEQUENTIAL);
}
/** Create a merge file in the given location. /** Create a merge file in the given location.
@param[out] merge_file merge file structure @param[out] merge_file merge file structure
@ -4402,17 +4407,16 @@ row_merge_file_create(
merge_file_t* merge_file, merge_file_t* merge_file,
const char* path) const char* path)
{ {
merge_file->fd = row_merge_file_create_low(path);
merge_file->offset = 0; merge_file->offset = 0;
merge_file->n_rec = 0; merge_file->n_rec = 0;
#ifdef HAVE_FCNTL_DIRECT merge_file->fd =
if (merge_file->fd != OS_FILE_CLOSED) { row_merge_file_create_mode(path,
if (srv_disable_sort_file_cache) { #if !defined _WIN32 && defined O_DIRECT
os_file_set_nocache(merge_file->fd, srv_disable_sort_file_cache
"row0merge.cc", "sort"); ? O_DIRECT | O_BINARY | O_SEQUENTIAL
} :
}
#endif #endif
O_BINARY | O_SEQUENTIAL);
return(merge_file->fd); return(merge_file->fd);
} }