mariadb/mysys/my_lock.c
Vladislav Vaintroub 716099e07c This is the downport of
Bug#24509 - 2048 file descriptor limit on windows needs increasing, also 
WL#3049 - improved Windows I/O
                        
The patch replaces the use of the POSIX I/O interfaces in mysys on Windows with 
the Win32 API calls (CreateFile, WriteFile, etc). The Windows HANDLE for the open 
file is stored in the my_file_info struct, along with a flag for append mode 
because the Windows API does not support opening files in append mode in all cases)
The default max open files has been increased to 16384 and can be increased further
by setting --max-open-files=<value> during the server start.
                              
Another major change in this patch that almost all Windows specific file IO code
has been moved to a new file my_winfile.c, greatly reducing the amount of code 
in #ifdef blocks within mysys, thus improving readability.
                               
                                    
Minor enhancements:
- my_(f)stat() is changed to use __stati64 structure with  64 file size
and timestamps. It will return correct file size now (C runtime implementation
used to report outdated information)
- my_lock on Windows is prepared to handle additional timeout parameter
- after review : changed __WIN__ to _WIN32 in the new and changed code.

client/mysqlbinlog.cc:
  fileno -> my_fileno
client/readline.cc:
  fileno -> my_fileno
include/config-win.h:
  Increase OS_FILE_LIMIT for Windows.
  Remove O_SHARE - Windows does not support it. Its definition conflicts with
  O_SHORT_LIVED, that has different semantics.
include/my_dir.h:
  Use stat64 for stat() family of functions on Windows, because of 64 bit file size.
include/my_global.h:
  Increased default value for open file limit to 16K
include/my_sys.h:
  - my_file_info got new structure members - file handle and open flags
  - 2 new Windows-only mysys functions : my_get_osfhandle and my_osmaperr,
    modelled after Windows C runtime functions _get_osfhandle and _dosmaperr
libmysql/CMakeLists.txt:
  new files my_winfile.c and my_winerr.c
mysql-test/suite/large_tests/r/lock_tables_big.result:
  test for more then 2048 open file descriptors on Windows
mysql-test/suite/large_tests/t/lock_tables_big.test:
  test for more then 2048 open file descriptors on Windows
mysys/CMakeLists.txt:
  new files my_winfile.c and my_winerr.c
mysys/Makefile.am:
  new files my_winfile.c and my_winerr.c
mysys/default_modify.c:
  fileno -> my_fileno
mysys/my_chsize.c:
  implementation of chsize on Windows now moved to my_winfile.c
mysys/my_create.c:
  - my_sopen->my_win_open
  - close open file before removing (won't generally work on Windows otherwise)
mysys/my_file.c:
  On Windows, files returned by my_open will not start with 0, but 2048 
  (making it simple to detect incompatible mix of CRT and mysys  io functions)
mysys/my_fopen.c:
  fileno->my_win_fileno , fclose->my_win_fclose, fdopen->my_win_fdopen
  Check for legal filename is done by my_win_[f]open functions
mysys/my_fstream.c:
  fileno->my_fileno
mysys/my_lib.c:
  Windows stat() functions are moved to my_winfile.c
mysys/my_lock.c:
  Move Windows code under #ifdef to a separate function win_lock().
  Add a parameter for lock wait timeout
mysys/my_mmap.c:
   _get_osfhandle->my_get_osfhandle
mysys/my_open.c:
  my_sopen->my_win_open (simpler interface)
mysys/my_pread.c:
  moved most windows specific code to my_win_file.c
  Use my_win_pread
mysys/my_quick.c:
  Use my_win_read/my_win_write
mysys/my_read.c:
  Moved most of windows specific code to my_win_file.c
  Use my_win_read()
mysys/my_seek.c:
  On Windows, use my_win_lseek() in my_seek()/my_tell()
  Removed dead code (synchronization of lseeks)
  Improved DBUG tracing (file position is ulonglong, not ulong)
mysys/my_static.c:
  Removed array initialization. my_file_info_default is global variable
  thus it is initialized with all zeros anyway
mysys/my_sync.c:
  _commit->my_win_fsync
mysys/my_winerr.c:
  New file my_winerr.c
  Exports my_osmaperr modelled after undocumented C runtime
  function _dosmaperr(). The problem with _dosmaperr() used previously is that 
  1) it is nowhere documented and thus code relying on it is not guaranteed to work
     in subsequent releases on the C runtime
  2) it is present only in static C runtime (mysqld does not link if compiled with
    /MD)
mysys/my_winfile.c:
  New file my_winfile.c
  Implements ANSI/Posix file IO routines, when possible using native Windows IO, without
  C runtime (C runtime dropped because of the 2048 file descriptor limit).
mysys/my_write.c:
  write->my_win_write
mysys/mysys_priv.h:
  Declaration of Windows Posix functions (private to mysys, shall not be visible
  outside)
storage/innobase/handler/ha_innodb.cc:
  mysys native Windows IO : correct innodb tmp file handling
  mysql_tmpfile does not return valid CRT file descriptor, thus
  it is not possible to dup() it. Instead, the native file handle has 
  to be duplicated and then converted to CRT descriptor.
storage/myisam/mi_locking.c:
  _commit->my_sync
2009-09-11 22:26:35 +02:00

276 lines
7.3 KiB
C

/* Copyright (C) 2000-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
the Free Software Foundation; version 2 of the License.
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>
#undef MY_HOW_OFTEN_TO_ALARM
#define MY_HOW_OFTEN_TO_ALARM ((int) my_time_to_wait_for_lock)
#ifdef NO_ALARM_LOOP
#undef NO_ALARM_LOOP
#endif
#include <my_alarm.h>
#ifdef __NETWARE__
#include <nks/fsio.h>
#endif
#ifdef _WIN32
#define WIN_LOCK_INFINITE -1
#define WIN_LOCK_SLEEP_MILLIS 100
static int win_lock(File fd, int locktype, my_off_t start, my_off_t length,
int timeout_sec)
{
LARGE_INTEGER liOffset,liLength;
DWORD dwFlags;
OVERLAPPED ov= {0};
HANDLE hFile= (HANDLE)my_get_osfhandle(fd);
DWORD lastError= 0;
int i;
int timeout_millis= timeout_sec * 1000;
DBUG_ENTER("win_lock");
liOffset.QuadPart= start;
liLength.QuadPart= length;
ov.Offset= liOffset.LowPart;
ov.OffsetHigh= liOffset.HighPart;
if (locktype == F_UNLCK)
{
if (UnlockFileEx(hFile, 0, liLength.LowPart, liLength.HighPart, &ov))
DBUG_RETURN(0);
/*
For compatibility with fcntl implementation, ignore error,
if region was not locked
*/
if (GetLastError() == ERROR_NOT_LOCKED)
{
SetLastError(0);
DBUG_RETURN(0);
}
goto error;
}
else if (locktype == F_RDLCK)
/* read lock is mapped to a shared lock. */
dwFlags= 0;
else
/* write lock is mapped to an exclusive lock. */
dwFlags= LOCKFILE_EXCLUSIVE_LOCK;
/*
Drop old lock first to avoid double locking.
During analyze of Bug#38133 (Myisamlog test fails on Windows)
I met the situation that the program myisamlog locked the file
exclusively, then additionally shared, then did one unlock, and
then blocked on an attempt to lock it exclusively again.
Unlocking before every lock fixed the problem.
Note that this introduces a race condition. When the application
wants to convert an exclusive lock into a shared one, it will now
first unlock the file and then lock it shared. A waiting exclusive
lock could step in here. For reasons described in Bug#38133 and
Bug#41124 (Server hangs on Windows with --external-locking after
INSERT...SELECT) and in the review thread at
http://lists.mysql.com/commits/60721 it seems to be the better
option than not to unlock here.
If one day someone notices a way how to do file lock type changes
on Windows without unlocking before taking the new lock, please
change this code accordingly to fix the race condition.
*/
if (!UnlockFileEx(hFile, 0, liLength.LowPart, liLength.HighPart, &ov) &&
(GetLastError() != ERROR_NOT_LOCKED))
goto error;
if (timeout_sec == WIN_LOCK_INFINITE)
{
if (LockFileEx(hFile, dwFlags, 0, liLength.LowPart, liLength.HighPart, &ov))
DBUG_RETURN(0);
goto error;
}
dwFlags|= LOCKFILE_FAIL_IMMEDIATELY;
timeout_millis= timeout_sec * 1000;
/* Try lock in a loop, until the lock is acquired or timeout happens */
for(i= 0; ;i+= WIN_LOCK_SLEEP_MILLIS)
{
if (LockFileEx(hFile, dwFlags, 0, liLength.LowPart, liLength.HighPart, &ov))
DBUG_RETURN(0);
if (GetLastError() != ERROR_LOCK_VIOLATION)
goto error;
if (i >= timeout_millis)
break;
Sleep(WIN_LOCK_SLEEP_MILLIS);
}
/* timeout */
errno= EAGAIN;
DBUG_RETURN(-1);
error:
my_osmaperr(GetLastError());
DBUG_RETURN(-1);
}
#endif
/*
Lock a part of a file
RETURN VALUE
0 Success
-1 An error has occured and 'my_errno' is set
to indicate the actual error code.
*/
int my_lock(File fd, int locktype, my_off_t start, my_off_t length,
myf MyFlags)
{
#ifdef HAVE_FCNTL
int value;
ALARM_VARIABLES;
#endif
#ifdef __NETWARE__
int nxErrno;
#endif
DBUG_ENTER("my_lock");
DBUG_PRINT("my",("fd: %d Op: %d start: %ld Length: %ld MyFlags: %d",
fd,locktype,(long) start,(long) length,MyFlags));
#ifdef VMS
DBUG_RETURN(0);
#else
if (my_disable_locking)
DBUG_RETURN(0);
#if defined(__NETWARE__)
{
NXSOffset_t nxLength = length;
unsigned long nxLockFlags = 0;
if (length == F_TO_EOF)
{
/* EOF is interpreted as a very large length. */
nxLength = 0x7FFFFFFFFFFFFFFF;
}
if (locktype == F_UNLCK)
{
/* The lock flags are currently ignored by NKS. */
if (!(nxErrno= NXFileRangeUnlock(fd, 0L, start, nxLength)))
DBUG_RETURN(0);
}
else
{
if (locktype == F_RDLCK)
{
/* A read lock is mapped to a shared lock. */
nxLockFlags = NX_RANGE_LOCK_SHARED;
}
else
{
/* A write lock is mapped to an exclusive lock. */
nxLockFlags = NX_RANGE_LOCK_EXCL;
}
if (MyFlags & MY_DONT_WAIT)
{
/* Don't block on the lock. */
nxLockFlags |= NX_RANGE_LOCK_TRYLOCK;
}
if (!(nxErrno= NXFileRangeLock(fd, nxLockFlags, start, nxLength)))
DBUG_RETURN(0);
}
}
#elif defined(_WIN32)
{
int timeout_sec;
if (MyFlags & MY_DONT_WAIT)
timeout_sec= 0;
else
timeout_sec= WIN_LOCK_INFINITE;
if(win_lock(fd, locktype, start, length, timeout_sec) == 0)
DBUG_RETURN(0);
}
#else
#if defined(HAVE_FCNTL)
{
struct flock lock;
lock.l_type= (short) locktype;
lock.l_whence= SEEK_SET;
lock.l_start= (off_t) start;
lock.l_len= (off_t) length;
if (MyFlags & MY_DONT_WAIT)
{
if (fcntl(fd,F_SETLK,&lock) != -1) /* Check if we can lock */
DBUG_RETURN(0); /* Ok, file locked */
DBUG_PRINT("info",("Was locked, trying with alarm"));
ALARM_INIT;
while ((value=fcntl(fd,F_SETLKW,&lock)) && ! ALARM_TEST &&
errno == EINTR)
{ /* Setup again so we don`t miss it */
ALARM_REINIT;
}
ALARM_END;
if (value != -1)
DBUG_RETURN(0);
if (errno == EINTR)
errno=EAGAIN;
}
else if (fcntl(fd,F_SETLKW,&lock) != -1) /* Wait until a lock */
DBUG_RETURN(0);
}
#else
if (MyFlags & MY_SEEK_NOT_DONE)
{
if (my_seek(fd,start,MY_SEEK_SET,MYF(MyFlags & ~MY_SEEK_NOT_DONE))
== MY_FILEPOS_ERROR)
{
/*
If an error has occured in my_seek then we will already
have an error code in my_errno; Just return error code.
*/
DBUG_RETURN(-1);
}
}
if (lockf(fd,locktype,length) != -1)
DBUG_RETURN(0);
#endif /* HAVE_FCNTL */
#endif /* HAVE_LOCKING */
#ifdef __NETWARE__
my_errno = nxErrno;
#else
/* We got an error. We don't want EACCES errors */
my_errno=(errno == EACCES) ? EAGAIN : errno ? errno : -1;
#endif
if (MyFlags & MY_WME)
{
if (locktype == F_UNLCK)
my_error(EE_CANTUNLOCK,MYF(ME_BELL+ME_WAITTANG),my_errno);
else
my_error(EE_CANTLOCK,MYF(ME_BELL+ME_WAITTANG),my_errno);
}
DBUG_PRINT("error",("my_errno: %d (%d)",my_errno,errno));
DBUG_RETURN(-1);
#endif /* ! VMS */
} /* my_lock */