2003-11-02 14:55:02 +01:00
|
|
|
/* Copyright (C) 2003 MySQL AB
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2006-12-23 20:17:15 +01:00
|
|
|
the Free Software Foundation; version 2 of the License.
|
2003-11-02 14:55:02 +01:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
|
|
|
|
#include "mysys_priv.h"
|
|
|
|
#include "mysys_err.h"
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
Sync data in file to disk
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
my_sync()
|
|
|
|
fd File descritor to sync
|
|
|
|
my_flags Flags (now only MY_WME is supported)
|
|
|
|
|
|
|
|
NOTE
|
2005-04-05 13:17:49 +02:00
|
|
|
If file system supports its, only file data is synced, not inode data.
|
|
|
|
|
|
|
|
MY_IGNORE_BADFD is useful when fd is "volatile" - not protected by a
|
|
|
|
mutex. In this case by the time of fsync(), fd may be already closed by
|
|
|
|
another thread, or even reassigned to a different file. With this flag -
|
|
|
|
MY_IGNORE_BADFD - such a situation will not be considered an error.
|
|
|
|
(which is correct behaviour, if we know that the other thread synced the
|
|
|
|
file before closing)
|
2003-11-02 14:55:02 +01:00
|
|
|
|
|
|
|
RETURN
|
|
|
|
0 ok
|
|
|
|
-1 error
|
|
|
|
*/
|
|
|
|
|
|
|
|
int my_sync(File fd, myf my_flags)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
DBUG_ENTER("my_sync");
|
2007-12-18 02:21:32 +01:00
|
|
|
DBUG_PRINT("my",("fd: %d my_flags: %d", fd, my_flags));
|
2003-11-02 14:55:02 +01:00
|
|
|
|
2005-01-11 00:14:46 +01:00
|
|
|
do
|
|
|
|
{
|
2006-11-21 22:22:59 +01:00
|
|
|
#if defined(F_FULLFSYNC)
|
2006-11-22 23:38:10 +01:00
|
|
|
/*
|
|
|
|
In Mac OS X >= 10.3 this call is safer than fsync() (it forces the
|
WL#3072 Maria Recovery. Making DDLs durable in Maria:
Sync table files after CREATE (of non-temp table), DROP, RENAME,
TRUNCATE, sync directories and symlinks (for the 3 first commands).
Comments for future log records.
In ma_rename(), if rename of index works and then rename of data fails,
try to undo the rename of the index to leave a consistent state.
mysys/my_symlink.c:
sync directory after creation of a symbolic link in it, if asked
mysys/my_sync.c:
comment. Fix for when the file's name has no directory in it.
storage/maria/ma_create.c:
sync files and links and dirs when creating a non-temporary table.
Optimizations of the above to reduce syncs in the common cases:
* if index file and data file have the exact same paths (regular
and link), sync the directories (of regular and link) only once
after creating the last file (the data file).
* don't sync the data file if we didn't write to it (always true
in our builds).
storage/maria/ma_delete_all.c:
sync files after truncating a table
storage/maria/ma_delete_table.c:
sync files and symbolic links and dirs after dropping a table
storage/maria/ma_extra.c:
a function which wraps the sync of the index file and the sync of the
data file.
storage/maria/ma_locking.c:
using a wrapper function
storage/maria/ma_rename.c:
sync files and symbolic links and dirs after renaming a table.
If rename of index works and then rename of data fails, try to undo
the rename of the index to leave a consistent state. That is just a
try, it may fail...
storage/maria/ma_test3.c:
warning to not pay attention to this test.
storage/maria/maria_def.h:
declaration for the function added to ma_extra.c
2006-11-27 22:01:29 +01:00
|
|
|
disk's cache and guarantees ordered writes).
|
2006-11-22 23:38:10 +01:00
|
|
|
*/
|
2006-11-21 22:22:59 +01:00
|
|
|
if (!(res= fcntl(fd, F_FULLFSYNC, 0)))
|
|
|
|
break; /* ok */
|
2006-11-22 23:38:10 +01:00
|
|
|
/* Some file systems don't support F_FULLFSYNC and fail above: */
|
|
|
|
DBUG_PRINT("info",("fcntl(F_FULLFSYNC) failed, falling back"));
|
2006-11-21 22:22:59 +01:00
|
|
|
#endif
|
2003-11-02 14:55:02 +01:00
|
|
|
#if defined(HAVE_FDATASYNC)
|
2005-01-11 00:14:46 +01:00
|
|
|
res= fdatasync(fd);
|
2003-11-02 14:55:02 +01:00
|
|
|
#elif defined(HAVE_FSYNC)
|
2005-01-11 00:14:46 +01:00
|
|
|
res= fsync(fd);
|
2003-11-02 14:55:02 +01:00
|
|
|
#elif defined(__WIN__)
|
|
|
|
res= _commit(fd);
|
|
|
|
#else
|
2006-11-22 23:38:10 +01:00
|
|
|
#error Cannot find a way to sync a file, durability in danger
|
2005-01-11 00:14:46 +01:00
|
|
|
res= 0; /* No sync (strange OS) */
|
2003-11-02 14:55:02 +01:00
|
|
|
#endif
|
2005-01-11 00:14:46 +01:00
|
|
|
} while (res == -1 && errno == EINTR);
|
|
|
|
|
2003-11-02 14:55:02 +01:00
|
|
|
if (res)
|
|
|
|
{
|
2005-04-05 13:17:49 +02:00
|
|
|
int er= errno;
|
|
|
|
if (!(my_errno= er))
|
|
|
|
my_errno= -1; /* Unknown error */
|
2005-04-06 18:43:35 +02:00
|
|
|
if ((my_flags & MY_IGNORE_BADFD) &&
|
2005-04-05 13:17:49 +02:00
|
|
|
(er == EBADF || er == EINVAL || er == EROFS))
|
2006-11-22 23:38:10 +01:00
|
|
|
{
|
|
|
|
DBUG_PRINT("info", ("ignoring errno %d", er));
|
2005-04-05 13:17:49 +02:00
|
|
|
res= 0;
|
2006-11-22 23:38:10 +01:00
|
|
|
}
|
2005-04-05 13:17:49 +02:00
|
|
|
else if (my_flags & MY_WME)
|
2003-11-02 14:55:02 +01:00
|
|
|
my_error(EE_SYNC, MYF(ME_BELL+ME_WAITTANG), my_filename(fd), my_errno);
|
|
|
|
}
|
|
|
|
DBUG_RETURN(res);
|
2005-04-05 13:17:49 +02:00
|
|
|
} /* my_sync */
|
|
|
|
|
2006-11-21 22:22:59 +01:00
|
|
|
|
WL#3072 Maria Recovery. Making DDLs durable in Maria:
Sync table files after CREATE (of non-temp table), DROP, RENAME,
TRUNCATE, sync directories and symlinks (for the 3 first commands).
Comments for future log records.
In ma_rename(), if rename of index works and then rename of data fails,
try to undo the rename of the index to leave a consistent state.
mysys/my_symlink.c:
sync directory after creation of a symbolic link in it, if asked
mysys/my_sync.c:
comment. Fix for when the file's name has no directory in it.
storage/maria/ma_create.c:
sync files and links and dirs when creating a non-temporary table.
Optimizations of the above to reduce syncs in the common cases:
* if index file and data file have the exact same paths (regular
and link), sync the directories (of regular and link) only once
after creating the last file (the data file).
* don't sync the data file if we didn't write to it (always true
in our builds).
storage/maria/ma_delete_all.c:
sync files after truncating a table
storage/maria/ma_delete_table.c:
sync files and symbolic links and dirs after dropping a table
storage/maria/ma_extra.c:
a function which wraps the sync of the index file and the sync of the
data file.
storage/maria/ma_locking.c:
using a wrapper function
storage/maria/ma_rename.c:
sync files and symbolic links and dirs after renaming a table.
If rename of index works and then rename of data fails, try to undo
the rename of the index to leave a consistent state. That is just a
try, it may fail...
storage/maria/ma_test3.c:
warning to not pay attention to this test.
storage/maria/maria_def.h:
declaration for the function added to ma_extra.c
2006-11-27 22:01:29 +01:00
|
|
|
static const char cur_dir_name[]= {FN_CURLIB, 0};
|
2006-11-21 22:22:59 +01:00
|
|
|
/*
|
2006-11-22 23:38:10 +01:00
|
|
|
Force directory information to disk.
|
2006-11-21 22:22:59 +01:00
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
my_sync_dir()
|
|
|
|
dir_name the name of the directory
|
2006-11-22 23:38:10 +01:00
|
|
|
my_flags flags (MY_WME etc)
|
2006-11-21 22:22:59 +01:00
|
|
|
|
|
|
|
RETURN
|
2006-11-22 23:38:10 +01:00
|
|
|
0 if ok, !=0 if error
|
2006-11-21 22:22:59 +01:00
|
|
|
*/
|
2010-01-06 22:27:53 +01:00
|
|
|
int my_sync_dir(const char *dir_name __attribute__((unused)),
|
|
|
|
myf my_flags __attribute__((unused)))
|
2006-11-21 22:22:59 +01:00
|
|
|
{
|
2006-11-22 23:38:10 +01:00
|
|
|
#ifdef NEED_EXPLICIT_SYNC_DIR
|
2006-11-21 22:22:59 +01:00
|
|
|
File dir_fd;
|
2006-11-22 23:38:10 +01:00
|
|
|
int res= 0;
|
WL#3072 Maria Recovery. Making DDLs durable in Maria:
Sync table files after CREATE (of non-temp table), DROP, RENAME,
TRUNCATE, sync directories and symlinks (for the 3 first commands).
Comments for future log records.
In ma_rename(), if rename of index works and then rename of data fails,
try to undo the rename of the index to leave a consistent state.
mysys/my_symlink.c:
sync directory after creation of a symbolic link in it, if asked
mysys/my_sync.c:
comment. Fix for when the file's name has no directory in it.
storage/maria/ma_create.c:
sync files and links and dirs when creating a non-temporary table.
Optimizations of the above to reduce syncs in the common cases:
* if index file and data file have the exact same paths (regular
and link), sync the directories (of regular and link) only once
after creating the last file (the data file).
* don't sync the data file if we didn't write to it (always true
in our builds).
storage/maria/ma_delete_all.c:
sync files after truncating a table
storage/maria/ma_delete_table.c:
sync files and symbolic links and dirs after dropping a table
storage/maria/ma_extra.c:
a function which wraps the sync of the index file and the sync of the
data file.
storage/maria/ma_locking.c:
using a wrapper function
storage/maria/ma_rename.c:
sync files and symbolic links and dirs after renaming a table.
If rename of index works and then rename of data fails, try to undo
the rename of the index to leave a consistent state. That is just a
try, it may fail...
storage/maria/ma_test3.c:
warning to not pay attention to this test.
storage/maria/maria_def.h:
declaration for the function added to ma_extra.c
2006-11-27 22:01:29 +01:00
|
|
|
const char *correct_dir_name;
|
2009-11-25 16:48:29 +01:00
|
|
|
DBUG_ENTER("my_sync_dir");
|
|
|
|
DBUG_PRINT("my",("Dir: '%s' my_flags: %d", dir_name, my_flags));
|
WL#3072 Maria Recovery. Making DDLs durable in Maria:
Sync table files after CREATE (of non-temp table), DROP, RENAME,
TRUNCATE, sync directories and symlinks (for the 3 first commands).
Comments for future log records.
In ma_rename(), if rename of index works and then rename of data fails,
try to undo the rename of the index to leave a consistent state.
mysys/my_symlink.c:
sync directory after creation of a symbolic link in it, if asked
mysys/my_sync.c:
comment. Fix for when the file's name has no directory in it.
storage/maria/ma_create.c:
sync files and links and dirs when creating a non-temporary table.
Optimizations of the above to reduce syncs in the common cases:
* if index file and data file have the exact same paths (regular
and link), sync the directories (of regular and link) only once
after creating the last file (the data file).
* don't sync the data file if we didn't write to it (always true
in our builds).
storage/maria/ma_delete_all.c:
sync files after truncating a table
storage/maria/ma_delete_table.c:
sync files and symbolic links and dirs after dropping a table
storage/maria/ma_extra.c:
a function which wraps the sync of the index file and the sync of the
data file.
storage/maria/ma_locking.c:
using a wrapper function
storage/maria/ma_rename.c:
sync files and symbolic links and dirs after renaming a table.
If rename of index works and then rename of data fails, try to undo
the rename of the index to leave a consistent state. That is just a
try, it may fail...
storage/maria/ma_test3.c:
warning to not pay attention to this test.
storage/maria/maria_def.h:
declaration for the function added to ma_extra.c
2006-11-27 22:01:29 +01:00
|
|
|
/* Sometimes the path does not contain an explicit directory */
|
|
|
|
correct_dir_name= (dir_name[0] == 0) ? cur_dir_name : dir_name;
|
2006-11-21 22:22:59 +01:00
|
|
|
/*
|
2006-11-22 23:38:10 +01:00
|
|
|
Syncing a dir may give EINVAL on tmpfs on Linux, which is ok.
|
|
|
|
EIO on the other hand is very important. Hence MY_IGNORE_BADFD.
|
2006-11-21 22:22:59 +01:00
|
|
|
*/
|
WL#3072 Maria Recovery. Making DDLs durable in Maria:
Sync table files after CREATE (of non-temp table), DROP, RENAME,
TRUNCATE, sync directories and symlinks (for the 3 first commands).
Comments for future log records.
In ma_rename(), if rename of index works and then rename of data fails,
try to undo the rename of the index to leave a consistent state.
mysys/my_symlink.c:
sync directory after creation of a symbolic link in it, if asked
mysys/my_sync.c:
comment. Fix for when the file's name has no directory in it.
storage/maria/ma_create.c:
sync files and links and dirs when creating a non-temporary table.
Optimizations of the above to reduce syncs in the common cases:
* if index file and data file have the exact same paths (regular
and link), sync the directories (of regular and link) only once
after creating the last file (the data file).
* don't sync the data file if we didn't write to it (always true
in our builds).
storage/maria/ma_delete_all.c:
sync files after truncating a table
storage/maria/ma_delete_table.c:
sync files and symbolic links and dirs after dropping a table
storage/maria/ma_extra.c:
a function which wraps the sync of the index file and the sync of the
data file.
storage/maria/ma_locking.c:
using a wrapper function
storage/maria/ma_rename.c:
sync files and symbolic links and dirs after renaming a table.
If rename of index works and then rename of data fails, try to undo
the rename of the index to leave a consistent state. That is just a
try, it may fail...
storage/maria/ma_test3.c:
warning to not pay attention to this test.
storage/maria/maria_def.h:
declaration for the function added to ma_extra.c
2006-11-27 22:01:29 +01:00
|
|
|
if ((dir_fd= my_open(correct_dir_name, O_RDONLY, MYF(my_flags))) >= 0)
|
2006-11-21 22:22:59 +01:00
|
|
|
{
|
2006-11-22 23:38:10 +01:00
|
|
|
if (my_sync(dir_fd, MYF(my_flags | MY_IGNORE_BADFD)))
|
|
|
|
res= 2;
|
|
|
|
if (my_close(dir_fd, MYF(my_flags)))
|
|
|
|
res= 3;
|
2006-11-21 22:22:59 +01:00
|
|
|
}
|
|
|
|
else
|
2006-11-22 23:38:10 +01:00
|
|
|
res= 1;
|
|
|
|
DBUG_RETURN(res);
|
|
|
|
#else
|
|
|
|
return 0;
|
2006-11-21 22:22:59 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2006-11-22 23:38:10 +01:00
|
|
|
Force directory information to disk.
|
2006-11-21 22:22:59 +01:00
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
my_sync_dir_by_file()
|
|
|
|
file_name the name of a file in the directory
|
2006-11-22 23:38:10 +01:00
|
|
|
my_flags flags (MY_WME etc)
|
2006-11-21 22:22:59 +01:00
|
|
|
|
|
|
|
RETURN
|
2006-11-22 23:38:10 +01:00
|
|
|
0 if ok, !=0 if error
|
2006-11-21 22:22:59 +01:00
|
|
|
*/
|
2010-01-06 22:27:53 +01:00
|
|
|
int my_sync_dir_by_file(const char *file_name __attribute__((unused)),
|
|
|
|
myf my_flags __attribute__((unused)))
|
2006-11-21 22:22:59 +01:00
|
|
|
{
|
2006-11-22 23:38:10 +01:00
|
|
|
#ifdef NEED_EXPLICIT_SYNC_DIR
|
2006-11-21 22:22:59 +01:00
|
|
|
char dir_name[FN_REFLEN];
|
2007-07-02 19:45:15 +02:00
|
|
|
size_t dir_name_length;
|
|
|
|
dirname_part(dir_name, file_name, &dir_name_length);
|
2006-11-21 22:22:59 +01:00
|
|
|
return my_sync_dir(dir_name, my_flags);
|
2006-11-22 23:38:10 +01:00
|
|
|
#else
|
|
|
|
return 0;
|
2006-11-21 22:22:59 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|