Skip to content

Commit

Permalink
MDEV-33379 innodb_log_file_buffering=OFF causes corruption on bcachefs
Browse files Browse the repository at this point in the history
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.

We will only implement 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_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
  • Loading branch information
dr-m committed Feb 20, 2024
1 parent d73baa4 commit f4d7545
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 234 deletions.
3 changes: 0 additions & 3 deletions cmake/os/AIX.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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")
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)
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)")
4 changes: 0 additions & 4 deletions cmake/os/SunOS.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ INCLUDE(CheckSymbolExists)
INCLUDE(CheckCSourceRuns)
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
SET(_FILE_OFFSET_BITS 64)

Expand Down
1 change: 0 additions & 1 deletion cmake/os/WindowsCache.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ SET(HAVE_EXECINFO_H CACHE INTERNAL "")
SET(HAVE_FCHMOD CACHE INTERNAL "")
SET(HAVE_FCNTL CACHE INTERNAL "")
SET(HAVE_FCNTL_H 1 CACHE INTERNAL "")
SET(HAVE_FCNTL_DIRECT 0 CACHE INTERNAL "")
SET(HAVE_FCNTL_NONBLOCK CACHE INTERNAL "")
SET(HAVE_FDATASYNC CACHE INTERNAL "")
SET(HAVE_DECL_FDATASYNC CACHE INTERNAL "")
Expand Down
1 change: 0 additions & 1 deletion config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#cmakedefine HAVE_DLFCN_H 1
#cmakedefine HAVE_EXECINFO_H 1
#cmakedefine HAVE_FCNTL_H 1
#cmakedefine HAVE_FCNTL_DIRECT 1
#cmakedefine HAVE_FENV_H 1
#cmakedefine HAVE_FLOAT_H 1
#cmakedefine HAVE_FNMATCH_H 1
Expand Down
1 change: 0 additions & 1 deletion configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,6 @@ CHECK_SYMBOL_EXISTS(O_NONBLOCK "unistd.h;fcntl.h" HAVE_FCNTL_NONBLOCK)
IF(NOT HAVE_FCNTL_NONBLOCK)
SET(NO_FCNTL_NONBLOCK 1)
ENDIF()
CHECK_SYMBOL_EXISTS(O_DIRECT "fcntl.h" HAVE_FCNTL_DIRECT)

#
# Test for how the C compiler does inline, if at all
Expand Down
4 changes: 2 additions & 2 deletions extra/mariabackup/xtrabackup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2419,12 +2419,12 @@ static bool innodb_init()
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_CREATE, OS_FILE_NORMAL,
#if defined _WIN32 || defined HAVE_FCNTL_DIRECT
#if defined _WIN32 || defined O_DIRECT
OS_DATA_FILE_NO_O_DIRECT,
#else
OS_DATA_FILE,
#endif
false, &ret);
false, &ret);
if (!ret)
{
invalid_log:
Expand Down
37 changes: 33 additions & 4 deletions mysys/mf_tempfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ File create_temp_file(char *to, const char *dir, const char *prefix,

DBUG_ENTER("create_temp_file");
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);
#endif

mode|= O_TRUNC | O_CREAT | O_RDWR; /* not O_EXCL, see Windows code below */

Expand Down Expand Up @@ -118,16 +122,41 @@ File create_temp_file(char *to, const char *dir, const char *prefix,

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,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)) >= 0)
const int flags= (mode & ~O_CREAT) | O_TMPFILE | O_CLOEXEC;
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);
file=my_register_filename(file, to, FILE_BY_O_TMPFILE,
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)
{
my_printf_error(EE_CANTCREATEFILE, "O_TMPFILE is not supported on %s "
Expand Down
6 changes: 4 additions & 2 deletions storage/innobase/fil/fil0fil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ static bool fil_node_open_file_low(fil_node_t *node)
ut_ad(node->space->is_closing());
mysql_mutex_assert_owner(&fil_system.mutex);
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;
switch (FSP_FLAGS_GET_ZIP_SSIZE(node->space->flags)) {
case 1:
Expand Down Expand Up @@ -1395,10 +1395,12 @@ ATTRIBUTE_COLD void fil_space_t::reopen_all()

ulint type= OS_DATA_FILE;

#if defined _WIN32 || defined O_DIRECT
switch (FSP_FLAGS_GET_ZIP_SSIZE(space.flags)) {
case 1: case 2:
type= OS_DATA_FILE_NO_O_DIRECT;
}
#endif

for (ulint count= 10000; count--;)
{
Expand Down Expand Up @@ -2019,7 +2021,7 @@ fil_ibd_create(

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;
switch (FSP_FLAGS_GET_ZIP_SSIZE(flags)) {
case 1:
Expand Down
2 changes: 1 addition & 1 deletion storage/innobase/handler/ha_innodb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3973,7 +3973,7 @@ static int innodb_init_params()
} else if (innodb_flush_method >= 4 /* O_DIRECT */
IF_WIN(&& innodb_flush_method < 8 /* normal */,)) {
/* O_DIRECT and similar settings do nothing */
#ifdef HAVE_FCNTL_DIRECT
#ifdef O_DIRECT
} else if (srv_use_atomic_writes && my_may_have_atomic_write) {
/* If atomic writes are enabled, do the same as with
innodb_flush_method=O_DIRECT: retain the default settings */
Expand Down
21 changes: 1 addition & 20 deletions storage/innobase/include/os0file.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,8 @@ enum os_file_create_t {
doesn't exist, error) */
OS_FILE_CREATE, /*!< to create new file (if
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
partition */
OS_FILE_CREATE_PATH, /*!< to create the directories */
OS_FILE_OPEN_RETRY, /*!< open with retry */

/** Flags that can be combined with the above values. Please ensure
Expand All @@ -144,7 +141,7 @@ static const ulint OS_FILE_NORMAL = 62;
/** Types for file create @{ */
static constexpr ulint OS_DATA_FILE = 100;
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;
#endif
/* @} */
Expand Down Expand Up @@ -375,22 +372,6 @@ os_file_create_simple_no_error_handling_func(
bool* success)
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 */
/** Obtain an exclusive lock on a file.
@param fd file descriptor
Expand Down
11 changes: 4 additions & 7 deletions storage/innobase/include/row0merge.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,11 @@ row_merge_drop_indexes(
prepare_inplace_alter_table_dict(). */
void row_merge_drop_temp_indexes();

/** Create temporary merge files in the given paramater path, and if
UNIV_PFS_IO defined, register the file descriptor with Performance Schema.
@param[in] path location for creating temporary merge files, or NULL
/** 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)
MY_ATTRIBUTE((warn_unused_result));
pfs_os_file_t row_merge_file_create_low(const char *path)
MY_ATTRIBUTE((warn_unused_result));
/*********************************************************************//**
Destroy a merge file. And de-register the file from Performance Schema
if UNIV_PFS_IO is defined. */
Expand Down
Loading

0 comments on commit f4d7545

Please sign in to comment.