diff options
Diffstat (limited to 'storage/innobase/os/os0file.cc')
-rw-r--r-- | storage/innobase/os/os0file.cc | 10808 |
1 files changed, 6462 insertions, 4346 deletions
diff --git a/storage/innobase/os/os0file.cc b/storage/innobase/os/os0file.cc index e21ffa2defa..0d63b6c091c 100644 --- a/storage/innobase/os/os0file.cc +++ b/storage/innobase/os/os0file.cc @@ -33,41 +33,61 @@ The interface to the operating system file i/o primitives Created 10/21/1995 Heikki Tuuri *******************************************************/ +#ifndef UNIV_INNOCHECKSUM + +#include "ha_prototypes.h" +#include "sql_const.h" + #include "os0file.h" #ifdef UNIV_NONINL #include "os0file.ic" #endif -#include "ut0mem.h" #include "srv0srv.h" #include "srv0start.h" #include "fil0fil.h" #include "fil0crypt.h" #include "fsp0fsp.h" #include "fil0pagecompress.h" -#include "buf0buf.h" -#include "srv0mon.h" #include "srv0srv.h" #ifdef HAVE_LINUX_UNISTD_H #include "unistd.h" #endif #ifndef UNIV_HOTBACKUP -# include "os0sync.h" +# include "os0event.h" # include "os0thread.h" #else /* !UNIV_HOTBACKUP */ -# ifdef __WIN__ +# ifdef _WIN32 /* Add includes for the _stat() call to compile on Windows */ # include <sys/types.h> # include <sys/stat.h> # include <errno.h> -# endif /* __WIN__ */ +# endif /* _WIN32 */ #endif /* !UNIV_HOTBACKUP */ -#if defined(LINUX_NATIVE_AIO) +#include <vector> + +#ifdef LINUX_NATIVE_AIO #include <libaio.h> +#endif /* LINUX_NATIVE_AIO */ + +#ifdef HAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE +# include <fcntl.h> +# include <linux/falloc.h> +#endif /* HAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE */ + +#ifdef HAVE_LZ4 +#include <lz4.h> #endif +#include <zlib.h> + +#ifdef UNIV_DEBUG +/** Set when InnoDB has invoked exit(). */ +bool innodb_calling_exit; +#endif /* UNIV_DEBUG */ + #if defined(UNIV_LINUX) && defined(HAVE_SYS_IOCTL_H) # include <sys/ioctl.h> # ifndef DFS_IOCTL_ATOMIC_WRITE_SET @@ -102,27 +122,37 @@ static const ulint IO_IBUF_SEGMENT = 0; /** Log segment id */ static const ulint IO_LOG_SEGMENT = 1; +/** Number of retries for partial I/O's */ +static const ulint NUM_RETRIES_ON_PARTIAL_IO = 10; + /* This specifies the file permissions InnoDB uses when it creates files in Unix; the value of os_innodb_umask is initialized in ha_innodb.cc to my_umask */ -#ifndef __WIN__ +#ifndef _WIN32 /** Umask for creating files */ -UNIV_INTERN ulint os_innodb_umask = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; +static ulint os_innodb_umask = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; #else /** Umask for creating files */ -UNIV_INTERN ulint os_innodb_umask = 0; +static ulint os_innodb_umask = 0; +#ifndef ECANCELED #define ECANCELED 125 -#endif /* __WIN__ */ +#endif + +/* On Windows when using native AIO the number of AIO requests +that a thread can handle at a given time is limited to 32 +i.e.: SRV_N_PENDING_IOS_PER_THREAD */ +#define SRV_N_PENDING_IOS_PER_THREAD OS_AIO_N_PENDING_IOS_PER_THREAD + +#endif /* _WIN32 */ #ifndef UNIV_HOTBACKUP -/* We use these mutexes to protect lseek + file i/o operation, if the -OS does not provide an atomic pread or pwrite, or similar */ -#define OS_FILE_N_SEEK_MUTEXES 16 -UNIV_INTERN os_ib_mutex_t os_file_seek_mutexes[OS_FILE_N_SEEK_MUTEXES]; -/* In simulated aio, merge at most this many consecutive i/os */ -#define OS_AIO_MERGE_N_CONSECUTIVE 64 +/** In simulated aio, merge at most this many consecutive i/os */ +static const ulint OS_AIO_MERGE_N_CONSECUTIVE = 64; + +/** Flag indicating if the page_cleaner is in active state. */ +extern bool buf_page_cleaner_is_active; #ifdef WITH_INNODB_DISALLOW_WRITES #define WAIT_ALLOW_WRITES() os_event_wait(srv_allow_writes_event) @@ -135,25 +165,25 @@ UNIV_INTERN os_ib_mutex_t os_file_seek_mutexes[OS_FILE_N_SEEK_MUTEXES]; InnoDB AIO Implementation: ========================= -We support native AIO for windows and linux. For rest of the platforms -we simulate AIO by special io-threads servicing the IO-requests. +We support native AIO for Windows and Linux. For rest of the platforms +we simulate AIO by special IO-threads servicing the IO-requests. Simulated AIO: ============== -In platforms where we 'simulate' AIO following is a rough explanation +On platforms where we 'simulate' AIO, the following is a rough explanation of the high level design. There are four io-threads (for ibuf, log, read, write). All synchronous IO requests are serviced by the calling thread using os_file_write/os_file_read. The Asynchronous requests are queued up in an array (there are four such arrays) by the calling thread. -Later these requests are picked up by the io-thread and are serviced +Later these requests are picked up by the IO-thread and are serviced synchronously. Windows native AIO: ================== -If srv_use_native_aio is not set then windows follow the same +If srv_use_native_aio is not set then Windows follow the same code as simulated AIO. If the flag is set then native AIO interface is used. On windows, one of the limitation is that if a file is opened for AIO no synchronous IO can be done on it. Therefore we have an @@ -172,7 +202,7 @@ Linux native AIO: ================= If we have libaio installed on the system and innodb_use_native_aio -is set to TRUE we follow the code path of native AIO, otherwise we +is set to true we follow the code path of native AIO, otherwise we do simulated AIO. There are innodb_file_io_threads helper threads. These threads work on the four arrays mentioned above in Simulated AIO. @@ -184,714 +214,1227 @@ the completed IO request and calls completion routine on it. **********************************************************************/ -/** Flag: enable debug printout for asynchronous i/o */ -UNIV_INTERN ibool os_aio_print_debug = FALSE; #ifdef UNIV_PFS_IO /* Keys to register InnoDB I/O with performance schema */ -UNIV_INTERN mysql_pfs_key_t innodb_file_data_key; -UNIV_INTERN mysql_pfs_key_t innodb_file_log_key; -UNIV_INTERN mysql_pfs_key_t innodb_file_temp_key; +mysql_pfs_key_t innodb_data_file_key; +mysql_pfs_key_t innodb_log_file_key; +mysql_pfs_key_t innodb_temp_file_key; #endif /* UNIV_PFS_IO */ -/** The asynchronous i/o array slot structure */ -struct os_aio_slot_t{ - ibool is_read; /*!< TRUE if a read operation */ - ulint pos; /*!< index of the slot in the aio - array */ - ibool reserved; /*!< TRUE if this slot is reserved */ - time_t reservation_time;/*!< time when reserved */ - ulint len; /*!< length of the block to read or - write */ - byte* buf; /*!< buffer used in i/o */ - ulint type; /*!< OS_FILE_READ or OS_FILE_WRITE */ +/** The asynchronous I/O context */ +struct Slot { + /** index of the slot in the aio array */ + uint16_t pos; + + /** true if this slot is reserved */ + bool is_reserved; + + /** time when reserved */ + time_t reservation_time; + + /** buffer used in i/o */ + byte* buf; ulint is_log; /*!< 1 if OS_FILE_LOG or 0 */ ulint page_size; /*!< UNIV_PAGE_SIZE or zip_size */ - os_offset_t offset; /*!< file offset in bytes */ - os_file_t file; /*!< file where to read or write */ - const char* name; /*!< file name or path */ - ibool io_already_done;/*!< used only in simulated aio: - TRUE if the physical i/o already - made and only the slot message - needs to be passed to the caller - of os_aio_simulated_handle */ - fil_node_t* message1; /*!< message which is given by the */ - void* message2; /*!< the requester of an aio operation - and which can be used to identify - which pending aio operation was - completed */ - ulint bitmap; - - ulint* write_size; /*!< Actual write size initialized - after fist successfull trim - operation for this page and if - initialized we do not trim again if - actual page size does not decrease. */ + /** Buffer pointer used for actual IO. We advance this + when partial IO is required and not buf */ + byte* ptr; + + /** OS_FILE_READ or OS_FILE_WRITE */ + IORequest type; + + /** file offset in bytes */ + os_offset_t offset; + + /** file where to read or write */ + os_file_t file; + + /** file name or path */ + const char* name; + + /** used only in simulated aio: true if the physical i/o + already made and only the slot message needs to be passed + to the caller of os_aio_simulated_handle */ + bool io_already_done; ulint file_block_size;/*!< file block size */ + /** The file node for which the IO is requested. */ + fil_node_t* m1; + + /** the requester of an aio operation and which can be used + to identify which pending aio operation was completed */ + void* m2; + + /** AIO completion status */ + dberr_t err; + #ifdef WIN_ASYNC_IO - HANDLE handle; /*!< handle object we need in the - OVERLAPPED struct */ - OVERLAPPED control; /*!< Windows control block for the - aio request */ + /** handle object we need in the OVERLAPPED struct */ + HANDLE handle; + + /** Windows control block for the aio request */ + OVERLAPPED control; + + /** bytes written/read */ + DWORD n_bytes; + + /** length of the block to read or write */ + DWORD len; + #elif defined(LINUX_NATIVE_AIO) - struct iocb control; /* Linux control block for aio */ - int n_bytes; /* bytes written/read. */ - int ret; /* AIO return code */ + /** Linux control block for aio */ + struct iocb control; + + /** AIO return code */ + int ret; + + /** bytes written/read. */ + ssize_t n_bytes; + + /** length of the block to read or write */ + ulint len; +#else + /** length of the block to read or write */ + ulint len; + + /** bytes written/read. */ + ulint n_bytes; #endif /* WIN_ASYNC_IO */ + + /** Length of the block before it was compressed */ + uint32 original_len; + + /** Unaligned buffer for compressed pages */ + byte* compressed_ptr; + + /** Compressed data page, aligned and derived from compressed_ptr */ + byte* compressed_page; + + /** true, if we shouldn't punch a hole after writing the page */ + bool skip_punch_hole; + + ulint* write_size; }; /** The asynchronous i/o array structure */ -struct os_aio_array_t{ - os_ib_mutex_t mutex; /*!< the mutex protecting the aio array */ - os_event_t not_full; - /*!< The event which is set to the - signaled state when there is space in - the aio outside the ibuf segment */ - os_event_t is_empty; - /*!< The event which is set to the - signaled state when there are no - pending i/os in this array */ - ulint n_slots;/*!< Total number of slots in the aio - array. This must be divisible by - n_threads. */ - ulint n_segments; - /*!< Number of segments in the aio - array of pending aio requests. A - thread can wait separately for any one - of the segments. */ - ulint cur_seg;/*!< We reserve IO requests in round - robin fashion to different segments. - This points to the segment that is to - be used to service next IO request. */ - ulint n_reserved; - /*!< Number of reserved slots in the - aio array outside the ibuf segment */ - os_aio_slot_t* slots; /*!< Pointer to the slots in the array */ -#ifdef __WIN__ - HANDLE* handles; - /*!< Pointer to an array of OS native - event handles where we copied the - handles from slots, in the same - order. This can be used in - WaitForMultipleObjects; used only in - Windows */ -#endif /* __WIN__ */ +class AIO { +public: + /** Constructor + @param[in] id Latch ID + @param[in] n_slots Number of slots to configure + @param[in] segments Number of segments to configure */ + AIO(latch_id_t id, ulint n_slots, ulint segments); + + /** Destructor */ + ~AIO(); + + /** Initialize the instance + @return DB_SUCCESS or error code */ + dberr_t init(); + + /** Requests for a slot in the aio array. If no slot is available, waits + until not_full-event becomes signaled. + + @param[in,out] type IO context + @param[in,out] m1 message to be passed along with the AIO + operation + @param[in,out] m2 message to be passed along with the AIO + operation + @param[in] file file handle + @param[in] name name of the file or path as a null-terminated + string + @param[in,out] buf buffer where to read or from which to write + @param[in] offset file offset, where to read from or start writing + @param[in] len length of the block to read or write + @return pointer to slot */ + Slot* reserve_slot( + IORequest& type, + fil_node_t* m1, + void* m2, + os_file_t file, + const char* name, + void* buf, + os_offset_t offset, + ulint len, + ulint* write_size) + __attribute__((warn_unused_result)); + + /** @return number of reserved slots */ + ulint pending_io_count() const; + + /** Returns a pointer to the nth slot in the aio array. + @param[in] index Index of the slot in the array + @return pointer to slot */ + const Slot* at(ulint i) const + __attribute__((warn_unused_result)) + { + ut_a(i < m_slots.size()); + + return(&m_slots[i]); + } + + /** Non const version */ + Slot* at(ulint i) + __attribute__((warn_unused_result)) + { + ut_a(i < m_slots.size()); + + return(&m_slots[i]); + } + + /** Frees a slot in the AIO array, assumes caller owns the mutex. + @param[in,out] slot Slot to release */ + void release(Slot* slot); + + /** Frees a slot in the AIO array, assumes caller doesn't own the mutex. + @param[in,out] slot Slot to release */ + void release_with_mutex(Slot* slot); + + /** Prints info about the aio array. + @param[in,out] file Where to print */ + void print(FILE* file); + + /** @return the number of slots per segment */ + ulint slots_per_segment() const + __attribute__((warn_unused_result)) + { + return(m_slots.size() / m_n_segments); + } + + /** @return accessor for n_segments */ + ulint get_n_segments() const + __attribute__((warn_unused_result)) + { + return(m_n_segments); + } + +#ifdef UNIV_DEBUG + /** @return true if the thread owns the mutex */ + bool is_mutex_owned() const + __attribute__((warn_unused_result)) + { + return(mutex_own(&m_mutex)); + } +#endif /* UNIV_DEBUG */ + + /** Acquire the mutex */ + void acquire() const + { + mutex_enter(&m_mutex); + } + + /** Release the mutex */ + void release() const + { + mutex_exit(&m_mutex); + } + + /** Write out the state to the file/stream + @param[in, out] file File to write to */ + void to_file(FILE* file) const; + +#ifdef LINUX_NATIVE_AIO + /** Dispatch an AIO request to the kernel. + @param[in,out] slot an already reserved slot + @return true on success. */ + bool linux_dispatch(Slot* slot) + __attribute__((warn_unused_result)); + + /** Accessor for an AIO event + @param[in] index Index into the array + @return the event at the index */ + io_event* io_events(ulint index) + __attribute__((warn_unused_result)) + { + ut_a(index < m_events.size()); + + return(&m_events[index]); + } + + /** Accessor for the AIO context + @param[in] segment Segment for which to get the context + @return the AIO context for the segment */ + io_context* io_ctx(ulint segment) + __attribute__((warn_unused_result)) + { + ut_ad(segment < get_n_segments()); + + return(m_aio_ctx[segment]); + } + + /** Creates an io_context for native linux AIO. + @param[in] max_events number of events + @param[out] io_ctx io_ctx to initialize. + @return true on success. */ + static bool linux_create_io_ctx(ulint max_events, io_context_t* io_ctx) + __attribute__((warn_unused_result)); + + /** Checks if the system supports native linux aio. On some kernel + versions where native aio is supported it won't work on tmpfs. In such + cases we can't use native aio as it is not possible to mix simulated + and native aio. + @return true if supported, false otherwise. */ + static bool is_linux_native_aio_supported() + __attribute__((warn_unused_result)); +#endif /* LINUX_NATIVE_AIO */ + +#ifdef WIN_ASYNC_IO + /** Wakes up all async i/o threads in the array in Windows async I/O at + shutdown. */ + void signal() + { + for (ulint i = 0; i < m_slots.size(); ++i) { + SetEvent(m_slots[i].handle); + } + } + + /** Wake up all AIO threads in Windows native aio */ + static void wake_at_shutdown() + { + s_reads->signal(); + + if (s_writes != NULL) { + s_writes->signal(); + } + + if (s_ibuf != NULL) { + s_ibuf->signal(); + } + + if (s_log != NULL) { + s_log->signal(); + } + } +#endif /* WIN_ASYNC_IO */ + +#ifdef _WIN32 + /** This function can be called if one wants to post a batch of reads + and prefers an I/O - handler thread to handle them all at once later.You + must call os_aio_simulated_wake_handler_threads later to ensure the + threads are not left sleeping! */ + static void simulated_put_read_threads_to_sleep(); + + /** The non asynchronous IO array. + @return the synchronous AIO array instance. */ + static AIO* sync_array() + __attribute__((warn_unused_result)) + { + return(s_sync); + } + + /** + Get the AIO handles for a segment. + @param[in] segment The local segment. + @return the handles for the segment. */ + HANDLE* handles(ulint segment) + __attribute__((warn_unused_result)) + { + ut_ad(segment < m_handles->size() / slots_per_segment()); + + return(&(*m_handles)[segment * slots_per_segment()]); + } + + /** @return true if no slots are reserved */ + bool is_empty() const + __attribute__((warn_unused_result)) + { + ut_ad(is_mutex_owned()); + return(m_n_reserved == 0); + } +#endif /* _WIN32 */ + + /** Create an instance using new(std::nothrow) + @param[in] id Latch ID + @param[in] n_slots The number of AIO request slots + @param[in] segments The number of segments + @return a new AIO instance */ + static AIO* create( + latch_id_t id, + ulint n_slots, + ulint segments) + __attribute__((warn_unused_result)); + + /** Initializes the asynchronous io system. Creates one array each + for ibuf and log I/O. Also creates one array each for read and write + where each array is divided logically into n_readers and n_writers + respectively. The caller must create an i/o handler thread for each + segment in these arrays. This function also creates the sync array. + No I/O handler thread needs to be created for that + @param[in] n_per_seg maximum number of pending aio + operations allowed per segment + @param[in] n_readers number of reader threads + @param[in] n_writers number of writer threads + @param[in] n_slots_sync number of slots in the sync aio array + @return true if AIO sub-system was started successfully */ + static bool start( + ulint n_per_seg, + ulint n_readers, + ulint n_writers, + ulint n_slots_sync) + __attribute__((warn_unused_result)); + + /** Free the AIO arrays */ + static void shutdown(); + + /** Print all the AIO segments + @param[in,out] file Where to print */ + static void print_all(FILE* file); + + /** Calculates local segment number and aio array from global + segment number. + @param[out] array AIO wait array + @param[in] segment global segment number + @return local segment number within the aio array */ + static ulint get_array_and_local_segment( + AIO** array, + ulint segment) + __attribute__((warn_unused_result)); + + /** Select the IO slot array + @param[in] type Type of IO, READ or WRITE + @param[in] read_only true if running in read-only mode + @param[in] mode IO mode + @return slot array or NULL if invalid mode specified */ + static AIO* select_slot_array( + IORequest& type, + bool read_only, + ulint mode) + __attribute__((warn_unused_result)); + + /** Calculates segment number for a slot. + @param[in] array AIO wait array + @param[in] slot slot in this array + @return segment number (which is the number used by, for example, + I/O handler threads) */ + static ulint get_segment_no_from_slot( + const AIO* array, + const Slot* slot) + __attribute__((warn_unused_result)); + + /** Wakes up a simulated AIO I/O-handler thread if it has something + to do. + @param[in] global_segment the number of the segment in the + AIO arrays */ + static void wake_simulated_handler_thread(ulint global_segment); + + /** Check if it is a read request + @param[in] aio The AIO instance to check + @return true if the AIO instance is for reading. */ + static bool is_read(const AIO* aio) + __attribute__((warn_unused_result)) + { + return(s_reads == aio); + } + + /** Wait on an event until no pending writes */ + static void wait_until_no_pending_writes() + { + os_event_wait(AIO::s_writes->m_is_empty); + } + + /** Print to file + @param[in] file File to write to */ + static void print_to_file(FILE* file); + + /** Check for pending IO. Gets the count and also validates the + data structures. + @return count of pending IO requests */ + static ulint total_pending_io_count(); + +private: + /** Initialise the slots + @return DB_SUCCESS or error code */ + dberr_t init_slots() + __attribute__((warn_unused_result)); + + /** Wakes up a simulated AIO I/O-handler thread if it has something + to do for a local segment in the AIO array. + @param[in] global_segment the number of the segment in the + AIO arrays + @param[in] segment the local segment in the AIO array */ + void wake_simulated_handler_thread(ulint global_segment, ulint segment); + + /** Prints pending IO requests per segment of an aio array. + We probably don't need per segment statistics but they can help us + during development phase to see if the IO requests are being + distributed as expected. + @param[in,out] file file where to print + @param[in] segments pending IO array */ + void print_segment_info( + FILE* file, + const ulint* segments); + +#ifdef LINUX_NATIVE_AIO + /** Initialise the Linux native AIO data structures + @return DB_SUCCESS or error code */ + dberr_t init_linux_native_aio() + __attribute__((warn_unused_result)); +#endif /* LINUX_NATIVE_AIO */ + +private: + typedef std::vector<Slot> Slots; + + /** the mutex protecting the aio array */ + mutable SysMutex m_mutex; + + /** Pointer to the slots in the array. + Number of elements must be divisible by n_threads. */ + Slots m_slots; + + /** Number of segments in the aio array of pending aio requests. + A thread can wait separately for any one of the segments. */ + ulint m_n_segments; + + /** The event which is set to the signaled state when + there is space in the aio outside the ibuf segment */ + os_event_t m_not_full; + + /** The event which is set to the signaled state when + there are no pending i/os in this array */ + os_event_t m_is_empty; + + /** Number of reserved slots in the AIO array outside + the ibuf segment */ + ulint m_n_reserved; + +#ifdef _WIN32 + typedef std::vector<HANDLE, ut_allocator<HANDLE> > Handles; + + /** Pointer to an array of OS native event handles where + we copied the handles from slots, in the same order. This + can be used in WaitForMultipleObjects; used only in Windows */ + Handles* m_handles; +#endif /* _WIN32 */ #if defined(LINUX_NATIVE_AIO) - io_context_t* aio_ctx; - /* completion queue for IO. There is - one such queue per segment. Each thread - will work on one ctx exclusively. */ - struct io_event* aio_events; - /* The array to collect completed IOs. - There is one such event for each - possible pending IO. The size of the - array is equal to n_slots. */ + typedef std::vector<io_event> IOEvents; + + /** completion queue for IO. There is one such queue per + segment. Each thread will work on one ctx exclusively. */ + io_context_t* m_aio_ctx; + + /** The array to collect completed IOs. There is one such + event for each possible pending IO. The size of the array + is equal to m_slots.size(). */ + IOEvents m_events; #endif /* LINUX_NATIV_AIO */ + + /** The aio arrays for non-ibuf i/o and ibuf i/o, as well as + sync AIO. These are NULL when the module has not yet been + initialized. */ + + /** Insert buffer */ + static AIO* s_ibuf; + + /** Redo log */ + static AIO* s_log; + + /** Reads */ + static AIO* s_reads; + + /** Writes */ + static AIO* s_writes; + + /** Synchronous I/O */ + static AIO* s_sync; }; +/** Static declarations */ +AIO* AIO::s_reads; +AIO* AIO::s_writes; +AIO* AIO::s_ibuf; +AIO* AIO::s_log; +AIO* AIO::s_sync; + #if defined(LINUX_NATIVE_AIO) /** timeout for each io_getevents() call = 500ms. */ -#define OS_AIO_REAP_TIMEOUT (500000000UL) +static const ulint OS_AIO_REAP_TIMEOUT = 500000000UL; /** time to sleep, in microseconds if io_setup() returns EAGAIN. */ -#define OS_AIO_IO_SETUP_RETRY_SLEEP (500000UL) +static const ulint OS_AIO_IO_SETUP_RETRY_SLEEP = 500000UL; /** number of attempts before giving up on io_setup(). */ -#define OS_AIO_IO_SETUP_RETRY_ATTEMPTS 5 -#endif +static const int OS_AIO_IO_SETUP_RETRY_ATTEMPTS = 5; +#endif /* LINUX_NATIVE_AIO */ -/** Array of events used in simulated aio */ +/** Array of events used in simulated AIO */ static os_event_t* os_aio_segment_wait_events = NULL; -/** The aio arrays for non-ibuf i/o and ibuf i/o, as well as sync aio. These -are NULL when the module has not yet been initialized. @{ */ -static os_aio_array_t* os_aio_read_array = NULL; /*!< Reads */ -static os_aio_array_t* os_aio_write_array = NULL; /*!< Writes */ -static os_aio_array_t* os_aio_ibuf_array = NULL; /*!< Insert buffer */ -static os_aio_array_t* os_aio_log_array = NULL; /*!< Redo log */ -static os_aio_array_t* os_aio_sync_array = NULL; /*!< Synchronous I/O */ -/* @} */ - /** Number of asynchronous I/O segments. Set by os_aio_init(). */ -static ulint os_aio_n_segments = ULINT_UNDEFINED; +static ulint os_aio_n_segments = ULINT_UNDEFINED; -/** If the following is TRUE, read i/o handler threads try to +/** If the following is true, read i/o handler threads try to wait until a batch of new read requests have been posted */ -static ibool os_aio_recommend_sleep_for_read_threads = FALSE; +static bool os_aio_recommend_sleep_for_read_threads = false; #endif /* !UNIV_HOTBACKUP */ -UNIV_INTERN ulint os_n_file_reads = 0; -UNIV_INTERN ulint os_bytes_read_since_printout = 0; -UNIV_INTERN ulint os_n_file_writes = 0; -UNIV_INTERN ulint os_n_fsyncs = 0; -UNIV_INTERN ulint os_n_file_reads_old = 0; -UNIV_INTERN ulint os_n_file_writes_old = 0; -UNIV_INTERN ulint os_n_fsyncs_old = 0; -UNIV_INTERN time_t os_last_printout; - -UNIV_INTERN ibool os_has_said_disk_full = FALSE; - -#if !defined(UNIV_HOTBACKUP) \ - && (!defined(HAVE_ATOMIC_BUILTINS) || UNIV_WORD_SIZE < 8) -/** The mutex protecting the following counts of pending I/O operations */ -static os_ib_mutex_t os_file_count_mutex; -#endif /* !UNIV_HOTBACKUP && (!HAVE_ATOMIC_BUILTINS || UNIV_WORD_SIZE < 8) */ - -/** Number of pending os_file_pread() operations */ -UNIV_INTERN ulint os_file_n_pending_preads = 0; -/** Number of pending os_file_pwrite() operations */ -UNIV_INTERN ulint os_file_n_pending_pwrites = 0; +ulint os_n_file_reads = 0; +ulint os_bytes_read_since_printout = 0; +ulint os_n_file_writes = 0; +ulint os_n_fsyncs = 0; +ulint os_n_file_reads_old = 0; +ulint os_n_file_writes_old = 0; +ulint os_n_fsyncs_old = 0; /** Number of pending write operations */ -UNIV_INTERN ulint os_n_pending_writes = 0; +ulint os_n_pending_writes = 0; /** Number of pending read operations */ -UNIV_INTERN ulint os_n_pending_reads = 0; - -/** After first fallocate failure we will disable os_file_trim */ -UNIV_INTERN ibool os_fallocate_failed = FALSE; - -/**********************************************************************//** -Directly manipulate the allocated disk space by deallocating for the file referred to -by fd for the byte range starting at offset and continuing for len bytes. -Within the specified range, partial file system blocks are zeroed, and whole -file system blocks are removed from the file. After a successful call, -subsequent reads from this range will return zeroes. -@return true if success, false if error */ -UNIV_INTERN -ibool -os_file_trim( -/*=========*/ - os_aio_slot_t* slot); /*!< in: slot structure */ - -/****************************************************************//** +ulint os_n_pending_reads = 0; + +time_t os_last_printout; +bool os_has_said_disk_full = false; + +/** Default Zip compression level */ +extern uint page_zip_level; + +#if DATA_TRX_ID_LEN > 6 +#error "COMPRESSION_ALGORITHM will not fit" +#endif /* DATA_TRX_ID_LEN */ + +/** Validates the consistency of the aio system. +@return true if ok */ +static +bool +os_aio_validate(); + +/** Does error handling when a file operation fails. +@param[in] name File name or NULL +@param[in] operation Name of operation e.g., "read", "write" +@return true if we should retry the operation */ +static +bool +os_file_handle_error( + const char* name, + const char* operation); + +/** Does error handling when a file operation fails. -@return TRUE if we should retry the operation */ -ibool +@param[in] name File name or NULL +@param[in] operation Name of operation e.g., "read", "write" +@param[in] silent if true then don't print any message to the log. +@return true if we should retry the operation */ +static +bool os_file_handle_error_no_exit( -/*=========================*/ - const char* name, /*!< in: name of a file or NULL */ - const char* operation, /*!< in: operation */ - ibool on_error_silent,/*!< in: if TRUE then don't print - any message to the log. */ - const char* file, /*!< in: file name */ - const ulint line); /*!< in: line */ - -/****************************************************************//** -Tries to enable the atomic write feature, if available, for the specified file -handle. -@return TRUE if success */ -static __attribute__((warn_unused_result)) -ibool -os_file_set_atomic_writes( -/*======================*/ - const char* name /*!< in: name of the file */ - __attribute__((unused)), - os_file_t file /*!< in: handle to the file */ - __attribute__((unused))) -{ -#ifdef DFS_IOCTL_ATOMIC_WRITE_SET - int atomic_option = 1; + const char* name, + const char* operation, + bool silent); + +/** Decompress after a read and punch a hole in the file if it was a write +@param[in] type IO context +@param[in] fh Open file handle +@param[in,out] buf Buffer to transform +@param[in,out] scratch Scratch area for read decompression +@param[in] src_len Length of the buffer before compression +@param[in] len Compressed buffer length for write and size + of buf len for read +@return DB_SUCCESS or error code */ +static +dberr_t +os_file_io_complete( + const IORequest&type, + os_file_t fh, + byte* buf, + byte* scratch, + ulint src_len, + ulint offset, + ulint len); + +/** Does simulated AIO. This function should be called by an i/o-handler +thread. + +@param[in] segment The number of the segment in the aio arrays to wait + for; segment 0 is the ibuf i/o thread, segment 1 the + log i/o thread, then follow the non-ibuf read threads, + and as the last are the non-ibuf write threads +@param[out] m1 the messages passed with the AIO request; note that + also in the case where the AIO operation failed, these + output parameters are valid and can be used to restart + the operation, for example +@param[out] m2 Callback argument +@param[in] type IO context +@return DB_SUCCESS or error code */ +static +dberr_t +os_aio_simulated_handler( + ulint global_segment, + fil_node_t** m1, + void** m2, + IORequest* type); + +#ifdef WIN_ASYNC_IO +/** This function is only used in Windows asynchronous i/o. +Waits for an aio operation to complete. This function is used to wait the +for completed requests. The aio array of pending requests is divided +into segments. The thread specifies which segment or slot it wants to wait +for. NOTE: this function will also take care of freeing the aio slot, +therefore no other thread is allowed to do the freeing! +@param[in] segment The number of the segment in the aio arrays to +wait for; segment 0 is the ibuf I/O thread, +segment 1 the log I/O thread, then follow the +non-ibuf read threads, and as the last are the +non-ibuf write threads; if this is +ULINT_UNDEFINED, then it means that sync AIO +is used, and this parameter is ignored +@param[in] pos this parameter is used only in sync AIO: +wait for the aio slot at this position +@param[out] m1 the messages passed with the AIO request; note +that also in the case where the AIO operation +failed, these output parameters are valid and +can be used to restart the operation, +for example +@param[out] m2 callback message +@param[out] type OS_FILE_WRITE or ..._READ +@return DB_SUCCESS or error code */ +static +dberr_t +os_aio_windows_handler( + ulint segment, + ulint pos, + fil_node_t** m1, + void** m2, + IORequest* type); +#endif /* WIN_ASYNC_IO */ + +/** Generic AIO Handler methods. Currently handles IO post processing. */ +class AIOHandler { +public: + /** Do any post processing after a read/write + @return DB_SUCCESS or error code. */ + static dberr_t post_io_processing(Slot* slot); - if (ioctl(file, DFS_IOCTL_ATOMIC_WRITE_SET, &atomic_option)) { + /** Decompress after a read and punch a hole in the file if + it was a write */ + static dberr_t io_complete(const Slot* slot) + { + ut_a(slot->offset > 0); + ut_a(slot->type.is_read() || !slot->skip_punch_hole); - fprintf(stderr, "InnoDB: Warning:Trying to enable atomic writes on " - "file %s on non-supported platform!\n", name); - os_file_handle_error_no_exit(name, "ioctl", FALSE, __FILE__, __LINE__); - return(FALSE); + return(os_file_io_complete( + slot->type, slot->file, slot->buf, + slot->compressed_page, slot->original_len, + static_cast<ulint>(slot->offset), + slot->len)); } - return(TRUE); -#else - fprintf(stderr, "InnoDB: Error: trying to enable atomic writes on " - "file %s on non-supported platform!\n", name); - return(FALSE); -#endif -} +private: + /** Check whether the page was compressed. + @param[in] slot The slot that contains the IO request + @return true if it was a compressed page */ + static bool is_compressed_page(const Slot* slot) + { + const byte* src = slot->buf; + ulint page_type = mach_read_from_2(src + FIL_PAGE_TYPE); -#ifdef UNIV_DEBUG -# ifndef UNIV_HOTBACKUP -/**********************************************************************//** -Validates the consistency the aio system some of the time. -@return TRUE if ok or the check was skipped */ -UNIV_INTERN -ibool -os_aio_validate_skip(void) -/*======================*/ -{ -/** Try os_aio_validate() every this many times */ -# define OS_AIO_VALIDATE_SKIP 13 + return(page_type == FIL_PAGE_COMPRESSED); + } - /** The os_aio_validate() call skip counter. - Use a signed type because of the race condition below. */ - static int os_aio_validate_count = OS_AIO_VALIDATE_SKIP; + /** Get the compressed page size. + @param[in] slot The slot that contains the IO request + @return number of bytes to read for a successful decompress */ + static ulint compressed_page_size(const Slot* slot) + { + ut_ad(slot->type.is_read()); + ut_ad(is_compressed_page(slot)); - /* There is a race condition below, but it does not matter, - because this call is only for heuristic purposes. We want to - reduce the call frequency of the costly os_aio_validate() - check in debug builds. */ - if (--os_aio_validate_count > 0) { - return(TRUE); + ulint size; + const byte* src = slot->buf; + + size = mach_read_from_2(src + FIL_PAGE_COMPRESS_SIZE_V1); + + return(size + FIL_PAGE_DATA); } - os_aio_validate_count = OS_AIO_VALIDATE_SKIP; - return(os_aio_validate()); -} -# endif /* !UNIV_HOTBACKUP */ -#endif /* UNIV_DEBUG */ + /** Check if the page contents can be decompressed. + @param[in] slot The slot that contains the IO request + @return true if the data read has all the compressed data */ + static bool can_decompress(const Slot* slot) + { + ut_ad(slot->type.is_read()); + ut_ad(is_compressed_page(slot)); -#ifdef __WIN__ -/***********************************************************************//** -Gets the operating system version. Currently works only on Windows. -@return OS_WIN95, OS_WIN31, OS_WINNT, OS_WIN2000, OS_WINXP, OS_WINVISTA, -OS_WIN7. */ -UNIV_INTERN -ulint -os_get_os_version(void) -/*===================*/ -{ - OSVERSIONINFO os_info; - - os_info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); - - ut_a(GetVersionEx(&os_info)); - - if (os_info.dwPlatformId == VER_PLATFORM_WIN32s) { - return(OS_WIN31); - } else if (os_info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) { - return(OS_WIN95); - } else if (os_info.dwPlatformId == VER_PLATFORM_WIN32_NT) { - switch (os_info.dwMajorVersion) { - case 3: - case 4: - return(OS_WINNT); - case 5: - return (os_info.dwMinorVersion == 0) - ? OS_WIN2000 : OS_WINXP; - case 6: - return (os_info.dwMinorVersion == 0) - ? OS_WINVISTA : OS_WIN7; - default: - return(OS_WIN7); + ulint version; + const byte* src = slot->buf; + + version = mach_read_from_1(src + FIL_PAGE_VERSION); + + ut_a(version == 1); + + /* Includes the page header size too */ + ulint size = compressed_page_size(slot); + + return(size <= (slot->ptr - slot->buf) + (ulint) slot->n_bytes); + } + + /** Check if we need to read some more data. + @param[in] slot The slot that contains the IO request + @param[in] n_bytes Total bytes read so far + @return DB_SUCCESS or error code */ + static dberr_t check_read(Slot* slot, ulint n_bytes); +}; + +/** Helper class for doing synchronous file IO. Currently, the objective +is to hide the OS specific code, so that the higher level functions aren't +peppered with #ifdef. Makes the code flow difficult to follow. */ +class SyncFileIO { +public: + /** Constructor + @param[in] fh File handle + @param[in,out] buf Buffer to read/write + @param[in] n Number of bytes to read/write + @param[in] offset Offset where to read or write */ + SyncFileIO(os_file_t fh, void* buf, ulint n, os_offset_t offset) + : + m_fh(fh), + m_buf(buf), + m_n(static_cast<ssize_t>(n)), + m_offset(offset) + { + ut_ad(m_n > 0); + + /* If off_t is > 4 bytes in size, then we assume we can pass a + 64-bit address */ + off_t offs = static_cast<off_t>(m_offset); + + if (sizeof(off_t) <= 4 && m_offset != (os_offset_t) offs) { + ib::error() << "file write at offset > 4 GB."; } - } else { - ut_error; - return(0); } + + /** Destructor */ + ~SyncFileIO() + { + /* No op */ + } + + /** Do the read/write + @param[in] request The IO context and type + @return the number of bytes read/written or negative value on error */ + ssize_t execute(const IORequest& request); + + /** Do the read/write + @param[in,out] slot The IO slot, it has the IO context + @return the number of bytes read/written or negative value on error */ + static ssize_t execute(Slot* slot); + + /** Move the read/write offset up to where the partial IO succeeded. + @param[in] n_bytes The number of bytes to advance */ + void advance(ssize_t n_bytes) + { + m_offset += n_bytes; + + ut_ad(m_n >= n_bytes); + + m_n -= n_bytes; + + m_buf = reinterpret_cast<uchar*>(m_buf) + n_bytes; + } + +private: + /** Open file handle */ + os_file_t m_fh; + + /** Buffer to read/write */ + void* m_buf; + + /** Number of bytes to read/write */ + ssize_t m_n; + + /** Offset from where to read/write */ + os_offset_t m_offset; +}; + +/** If it is a compressed page return the compressed page data + footer size +@param[in] buf Buffer to check, must include header + 10 bytes +@return ULINT_UNDEFINED if the page is not a compressed page or length + of the compressed data (including footer) if it is a compressed page */ +ulint +os_file_compressed_page_size(const byte* buf) +{ + ulint type = mach_read_from_2(buf + FIL_PAGE_TYPE); + + if (type == FIL_PAGE_COMPRESSED) { + ulint version = mach_read_from_1(buf + FIL_PAGE_VERSION); + ut_a(version == 1); + return(mach_read_from_2(buf + FIL_PAGE_COMPRESS_SIZE_V1)); + } + + return(ULINT_UNDEFINED); } -#endif /* __WIN__ */ -/***********************************************************************//** -Retrieves the last error number if an error occurs in a file io function. -The number should be retrieved before any other OS calls (because they may -overwrite the error number). If the number is not known to this program, -the OS error number + 100 is returned. -@return error number, or OS error number + 100 */ -static +/** If it is a compressed page return the original page data + footer size +@param[in] buf Buffer to check, must include header + 10 bytes +@return ULINT_UNDEFINED if the page is not a compressed page or length + of the original data + footer if it is a compressed page */ ulint -os_file_get_last_error_low( -/*=======================*/ - bool report_all_errors, /*!< in: TRUE if we want an error - message printed of all errors */ - bool on_error_silent) /*!< in: TRUE then don't print any - diagnostic to the log */ +os_file_original_page_size(const byte* buf) { -#ifdef __WIN__ + ulint type = mach_read_from_2(buf + FIL_PAGE_TYPE); - ulint err = (ulint) GetLastError(); - if (err == ERROR_SUCCESS) { - return(0); + if (type == FIL_PAGE_COMPRESSED) { + + ulint version = mach_read_from_1(buf + FIL_PAGE_VERSION); + ut_a(version == 1); + + return(mach_read_from_2(buf + FIL_PAGE_ORIGINAL_SIZE_V1)); } - if (report_all_errors - || (!on_error_silent - && err != ERROR_DISK_FULL - && err != ERROR_FILE_EXISTS)) { + return(ULINT_UNDEFINED); +} - ut_print_timestamp(stderr); - fprintf(stderr, - " InnoDB: Operating system error number %lu" - " in a file operation.\n", (ulong) err); +/** Check if we need to read some more data. +@param[in] slot The slot that contains the IO request +@param[in] n_bytes Total bytes read so far +@return DB_SUCCESS or error code */ +dberr_t +AIOHandler::check_read(Slot* slot, ulint n_bytes) +{ + dberr_t err; - if (err == ERROR_PATH_NOT_FOUND) { - fprintf(stderr, - "InnoDB: The error means the system" - " cannot find the path specified.\n"); + ut_ad(slot->type.is_read()); + ut_ad(slot->original_len > slot->len); - if (srv_is_being_started) { - fprintf(stderr, - "InnoDB: If you are installing InnoDB," - " remember that you must create\n" - "InnoDB: directories yourself, InnoDB" - " does not create them.\n"); - } - } else if (err == ERROR_ACCESS_DENIED) { - fprintf(stderr, - "InnoDB: The error means mysqld does not have" - " the access rights to\n" - "InnoDB: the directory. It may also be" - " you have created a subdirectory\n" - "InnoDB: of the same name as a data file.\n"); - } else if (err == ERROR_SHARING_VIOLATION - || err == ERROR_LOCK_VIOLATION) { - fprintf(stderr, - "InnoDB: The error means that another program" - " is using InnoDB's files.\n" - "InnoDB: This might be a backup or antivirus" - " software or another instance\n" - "InnoDB: of MySQL." - " Please close it to get rid of this error.\n"); - } else if (err == ERROR_WORKING_SET_QUOTA - || err == ERROR_NO_SYSTEM_RESOURCES) { - fprintf(stderr, - "InnoDB: The error means that there are no" - " sufficient system resources or quota to" - " complete the operation.\n"); - } else if (err == ERROR_OPERATION_ABORTED) { - fprintf(stderr, - "InnoDB: The error means that the I/O" - " operation has been aborted\n" - "InnoDB: because of either a thread exit" - " or an application request.\n" - "InnoDB: Retry attempt is made.\n"); - } else if (err == ECANCELED || err == ENOTTY) { - if (strerror(err) != NULL) { - fprintf(stderr, - "InnoDB: Error number %d" - " means '%s'.\n", - err, strerror(err)); - } + if (is_compressed_page(slot)) { + + if (can_decompress(slot)) { + + ut_a(slot->offset > 0); + + slot->len = slot->original_len; +#ifdef _WIN32 + slot->n_bytes = static_cast<DWORD>(n_bytes); +#else + slot->n_bytes = static_cast<ulint>(n_bytes); +#endif /* _WIN32 */ + + err = io_complete(slot); + ut_a(err == DB_SUCCESS); - if(srv_use_atomic_writes) { - fprintf(stderr, - "InnoDB: Error trying to enable atomic writes on " - "non-supported destination!\n"); - } } else { - fprintf(stderr, - "InnoDB: Some operating system error numbers" - " are described at\n" - "InnoDB: " - REFMAN - "operating-system-error-codes.html\n"); + /* Read the next block in */ + ut_ad(compressed_page_size(slot) >= n_bytes); + + err = DB_FAIL; } + } else { + err = DB_FAIL; } - fflush(stderr); + return(err); +} - if (err == ERROR_FILE_NOT_FOUND) { - return(OS_FILE_NOT_FOUND); - } else if (err == ERROR_DISK_FULL) { - return(OS_FILE_DISK_FULL); - } else if (err == ERROR_FILE_EXISTS) { - return(OS_FILE_ALREADY_EXISTS); - } else if (err == ERROR_SHARING_VIOLATION - || err == ERROR_LOCK_VIOLATION) { - return(OS_FILE_SHARING_VIOLATION); - } else if (err == ERROR_WORKING_SET_QUOTA - || err == ERROR_NO_SYSTEM_RESOURCES) { - return(OS_FILE_INSUFFICIENT_RESOURCE); - } else if (err == ERROR_OPERATION_ABORTED) { - return(OS_FILE_OPERATION_ABORTED); - } else if (err == ERROR_ACCESS_DENIED) { - return(OS_FILE_ACCESS_VIOLATION); - } else if (err == ERROR_BUFFER_OVERFLOW) { - return(OS_FILE_NAME_TOO_LONG); - } else { - return(OS_FILE_ERROR_MAX + err); - } -#else - int err = errno; - if (err == 0) { - return(0); - } +/** Do any post processing after a read/write +@return DB_SUCCESS or error code. */ +dberr_t +AIOHandler::post_io_processing(Slot* slot) +{ + dberr_t err; - if (report_all_errors - || (err != ENOSPC && err != EEXIST && !on_error_silent)) { + ut_ad(slot->is_reserved); - ut_print_timestamp(stderr); - fprintf(stderr, - " InnoDB: Operating system error number %d" - " in a file operation.\n", err); + /* Total bytes read so far */ + ulint n_bytes = (slot->ptr - slot->buf) + slot->n_bytes; - if (err == ENOENT) { - fprintf(stderr, - "InnoDB: The error means the system" - " cannot find the path specified.\n"); + /* Compressed writes can be smaller than the original length. + Therefore they can be processed without further IO. */ + if (n_bytes == slot->original_len + || (slot->type.is_write() + && slot->type.is_compressed() + && slot->len == static_cast<ulint>(slot->n_bytes))) { - if (srv_is_being_started) { - fprintf(stderr, - "InnoDB: If you are installing InnoDB," - " remember that you must create\n" - "InnoDB: directories yourself, InnoDB" - " does not create them.\n"); - } - } else if (err == EACCES) { - fprintf(stderr, - "InnoDB: The error means mysqld does not have" - " the access rights to\n" - "InnoDB: the directory.\n"); - } else if (err == ECANCELED || err == ENOTTY) { - if (strerror(err) != NULL) { - fprintf(stderr, - "InnoDB: Error number %d" - " means '%s'.\n", - err, strerror(err)); - } + if (!slot->type.is_log() && is_compressed_page(slot)) { - if(srv_use_atomic_writes) { - fprintf(stderr, - "InnoDB: Error trying to enable atomic writes on " - "non-supported destination!\n"); + ut_a(slot->offset > 0); + + if (slot->type.is_read()) { + slot->len = slot->original_len; } - } else { - if (strerror(err) != NULL) { - fprintf(stderr, - "InnoDB: Error number %d" - " means '%s'.\n", - err, strerror(err)); + + /* The punch hole has been done on collect() */ + + if (slot->type.is_read()) { + err = io_complete(slot); + } else { + err = DB_SUCCESS; } + ut_ad(err == DB_SUCCESS + || err == DB_UNSUPPORTED + || err == DB_CORRUPTION + || err == DB_IO_DECOMPRESS_FAIL); + } else { - fprintf(stderr, - "InnoDB: Some operating system" - " error numbers are described at\n" - "InnoDB: " - REFMAN - "operating-system-error-codes.html\n"); + err = DB_SUCCESS; } - } - fflush(stderr); + } else if ((ulint) slot->n_bytes == (ulint) slot->len) { - switch (err) { - case ENOSPC: - return(OS_FILE_DISK_FULL); - case ENOENT: - return(OS_FILE_NOT_FOUND); - case EEXIST: - return(OS_FILE_ALREADY_EXISTS); - case ENAMETOOLONG: - return(OS_FILE_NAME_TOO_LONG); - case EXDEV: - case ENOTDIR: - case EISDIR: - return(OS_FILE_PATH_ERROR); - case ECANCELED: - case ENOTTY: - return(OS_FILE_OPERATION_NOT_SUPPORTED); - case EAGAIN: - if (srv_use_native_aio) { - return(OS_FILE_AIO_RESOURCES_RESERVED); - } - break; - case EINTR: - if (srv_use_native_aio) { - return(OS_FILE_AIO_INTERRUPTED); - } - break; - case EACCES: - return(OS_FILE_ACCESS_VIOLATION); + /* It *must* be a partial read. */ + ut_ad(slot->len < slot->original_len); + + /* Has to be a read request, if it is less than + the original length. */ + ut_ad(slot->type.is_read()); + err = check_read(slot, n_bytes); + + } else { + err = DB_FAIL; } - return(OS_FILE_ERROR_MAX + err); -#endif + + return(err); } -/***********************************************************************//** -Retrieves the last error number if an error occurs in a file io function. -The number should be retrieved before any other OS calls (because they may -overwrite the error number). If the number is not known to this program, -the OS error number + 100 is returned. -@return error number, or OS error number + 100 */ -UNIV_INTERN +/** Count the number of free slots +@return number of reserved slots */ ulint -os_file_get_last_error( -/*===================*/ - bool report_all_errors) /*!< in: TRUE if we want an error - message printed of all errors */ +AIO::pending_io_count() const { - return(os_file_get_last_error_low(report_all_errors, false)); + acquire(); + +#ifdef UNIV_DEBUG + ut_a(m_n_segments > 0); + ut_a(!m_slots.empty()); + + ulint count = 0; + + for (ulint i = 0; i < m_slots.size(); ++i) { + + const Slot& slot = m_slots[i]; + + if (slot.is_reserved) { + ++count; + ut_a(slot.len > 0); + } + } + + ut_a(m_n_reserved == count); +#endif /* UNIV_DEBUG */ + + ulint reserved = m_n_reserved; + + release(); + + return(reserved); } -/****************************************************************//** -Does error handling when a file operation fails. -Conditionally exits (calling exit(3)) based on should_exit value and the -error type, if should_exit is TRUE then on_error_silent is ignored. -@return TRUE if we should retry the operation */ +/** Compress a data page +#param[in] block_size File system block size +@param[in] src Source contents to compress +@param[in] src_len Length in bytes of the source +@param[out] dst Compressed page contents +@param[out] dst_len Length in bytes of dst contents +@return buffer data, dst_len will have the length of the data */ static -ibool -os_file_handle_error_cond_exit( -/*===========================*/ - const char* name, /*!< in: name of a file or NULL */ - const char* operation, /*!< in: operation */ - ibool should_exit, /*!< in: call exit(3) if unknown error - and this parameter is TRUE */ - ibool on_error_silent,/*!< in: if TRUE then don't print - any message to the log iff it is - an unknown non-fatal error */ - const char* file, /*!< in: file name */ - const ulint line) /*!< in: line */ +byte* +os_file_compress_page( + Compression compression, + ulint block_size, + byte* src, + ulint src_len, + byte* dst, + ulint* dst_len) { - ulint err; + ulint len = 0; + ulint compression_level = page_zip_level; + ulint page_type = mach_read_from_2(src + FIL_PAGE_TYPE); - err = os_file_get_last_error_low(false, on_error_silent); + /* Must be divisible by the file system block size. */ + ut_ad(!(src_len % block_size)); - switch (err) { - case OS_FILE_DISK_FULL: - /* We only print a warning about disk full once */ + /* Shouldn't compress an already compressed page. */ + ut_ad(page_type != FIL_PAGE_COMPRESSED); - if (os_has_said_disk_full) { + /* The page must be at least twice as large as the file system + block size if we are to save any space. Ignore R-Tree pages for now, + they repurpose the same 8 bytes in the page header. No point in + compressing if the file system block size >= our page size. */ - return(FALSE); - } + if (page_type == FIL_PAGE_RTREE + || block_size == ULINT_UNDEFINED + || compression.m_type == Compression::NONE + || block_size >= src_len + || src_len < block_size * 2) { - /* Disk full error is reported irrespective of the - on_error_silent setting. */ + *dst_len = src_len; - if (name) { - ut_print_timestamp(stderr); - fprintf(stderr, - " InnoDB: Encountered a problem with" - " file %s\n", name); - } + return(src); + } + + /* Leave the header alone when compressing. */ + ut_ad(block_size >= FIL_PAGE_DATA * 2); + + ut_ad(src_len > FIL_PAGE_DATA + block_size); - ut_print_timestamp(stderr); - fprintf(stderr, - " InnoDB: Disk is full. Try to clean the disk" - " to free space.\n"); + /* Must compress to <= N-1 FS blocks. */ + ulint out_len = src_len - (FIL_PAGE_DATA + block_size); - fprintf(stderr, - " InnoDB: at file %s and at line %ld\n", file, line); + /* This is the original data page size - the page header. */ + ulint content_len = src_len - FIL_PAGE_DATA; - os_has_said_disk_full = TRUE; + ut_ad(out_len >= block_size - FIL_PAGE_DATA); + ut_ad(out_len <= src_len - (block_size + FIL_PAGE_DATA)); - fflush(stderr); + /* Only compress the data + trailer, leave the header alone */ + + switch (compression.m_type) { + case Compression::NONE: ut_error; - return(FALSE); - case OS_FILE_AIO_RESOURCES_RESERVED: - case OS_FILE_AIO_INTERRUPTED: + case Compression::ZLIB: { - return(TRUE); + uLongf zlen = static_cast<uLongf>(out_len); - case OS_FILE_PATH_ERROR: - case OS_FILE_ALREADY_EXISTS: - case OS_FILE_ACCESS_VIOLATION: + if (compress2( + dst + FIL_PAGE_DATA, + &zlen, + src + FIL_PAGE_DATA, + static_cast<uLong>(content_len), + static_cast<int>(compression_level)) != Z_OK) { - return(FALSE); + *dst_len = src_len; - case OS_FILE_SHARING_VIOLATION: + return(src); + } - os_thread_sleep(10000000); /* 10 sec */ - return(TRUE); + len = static_cast<ulint>(zlen); - case OS_FILE_OPERATION_ABORTED: - case OS_FILE_INSUFFICIENT_RESOURCE: + break; + } - os_thread_sleep(100000); /* 100 ms */ - return(TRUE); +#ifdef HAVE_LZ4 + case Compression::LZ4: - default: + len = LZ4_compress_limitedOutput( + reinterpret_cast<char*>(src) + FIL_PAGE_DATA, + reinterpret_cast<char*>(dst) + FIL_PAGE_DATA, + static_cast<int>(content_len), + static_cast<int>(out_len)); - /* If it is an operation that can crash on error then it - is better to ignore on_error_silent and print an error message - to the log. */ + ut_a(len <= src_len - FIL_PAGE_DATA); - if (should_exit || !on_error_silent) { - fprintf(stderr, - " InnoDB: Operation %s to file %s and at line %ld\n", - operation, file, line); - } + if (len == 0 || len >= out_len) { - if (should_exit || !on_error_silent) { - ib_logf(IB_LOG_LEVEL_ERROR, "File %s: '%s' returned OS " - "error " ULINTPF ".%s", name ? name : "(unknown)", - operation, err, should_exit - ? " Cannot continue operation" : ""); - } + *dst_len = src_len; - if (should_exit) { - exit(1); + return(src); } + + break; +#endif + + default: + *dst_len = src_len; + return(src); + } + + ut_a(len <= out_len); + + ut_ad(memcmp(src + FIL_PAGE_LSN + 4, + src + src_len - FIL_PAGE_END_LSN_OLD_CHKSUM + 4, 4) + == 0); + + /* Copy the header as is. */ + memmove(dst, src, FIL_PAGE_DATA); + + /* Add compression control information. Required for decompressing. */ + mach_write_to_2(dst + FIL_PAGE_TYPE, FIL_PAGE_COMPRESSED); + + mach_write_to_1(dst + FIL_PAGE_VERSION, 1); + + mach_write_to_1(dst + FIL_PAGE_ALGORITHM_V1, compression.m_type); + + mach_write_to_2(dst + FIL_PAGE_ORIGINAL_TYPE_V1, page_type); + + mach_write_to_2(dst + FIL_PAGE_ORIGINAL_SIZE_V1, content_len); + + mach_write_to_2(dst + FIL_PAGE_COMPRESS_SIZE_V1, len); + + /* Round to the next full block size */ + + len += FIL_PAGE_DATA; + + *dst_len = ut_calc_align(len, block_size); + + ut_ad(*dst_len >= len && *dst_len <= out_len + FIL_PAGE_DATA); + + /* Clear out the unused portion of the page. */ + if (len % block_size) { + memset(dst + len, 0x0, block_size - (len % block_size)); } - return(FALSE); + return(dst); } -/****************************************************************//** -Does error handling when a file operation fails. -@return TRUE if we should retry the operation */ -static -ibool -os_file_handle_error( -/*=================*/ - const char* name, /*!< in: name of a file or NULL */ - const char* operation, /*!< in: operation */ - const char* file, /*!< in: file name */ - const ulint line) /*!< in: line */ +#ifdef UNIV_DEBUG +# ifndef UNIV_HOTBACKUP +/** Validates the consistency the aio system some of the time. +@return true if ok or the check was skipped */ +bool +os_aio_validate_skip() { - /* exit in case of unknown error */ - return(os_file_handle_error_cond_exit(name, operation, TRUE, FALSE, file, line)); -} +/** Try os_aio_validate() every this many times */ +# define OS_AIO_VALIDATE_SKIP 13 -/****************************************************************//** -Does error handling when a file operation fails. -@return TRUE if we should retry the operation */ -ibool -os_file_handle_error_no_exit( -/*=========================*/ - const char* name, /*!< in: name of a file or NULL */ - const char* operation, /*!< in: operation */ - ibool on_error_silent,/*!< in: if TRUE then don't print - any message to the log. */ - const char* file, /*!< in: file name */ - const ulint line) /*!< in: line */ -{ - /* don't exit in case of unknown error */ - return(os_file_handle_error_cond_exit( - name, operation, FALSE, on_error_silent, file, line)); + /** The os_aio_validate() call skip counter. + Use a signed type because of the race condition below. */ + static int os_aio_validate_count = OS_AIO_VALIDATE_SKIP; + + /* There is a race condition below, but it does not matter, + because this call is only for heuristic purposes. We want to + reduce the call frequency of the costly os_aio_validate() + check in debug builds. */ + --os_aio_validate_count; + + if (os_aio_validate_count > 0) { + return(true); + } + + os_aio_validate_count = OS_AIO_VALIDATE_SKIP; + return(os_aio_validate()); } +# endif /* !UNIV_HOTBACKUP */ +#endif /* UNIV_DEBUG */ #undef USE_FILE_LOCK #define USE_FILE_LOCK -#if defined(UNIV_HOTBACKUP) || defined(__WIN__) +#if defined(UNIV_HOTBACKUP) || defined(_WIN32) /* InnoDB Hot Backup does not lock the data files. * On Windows, mandatory locking is used. */ # undef USE_FILE_LOCK #endif #ifdef USE_FILE_LOCK -/****************************************************************//** -Obtain an exclusive lock on a file. -@return 0 on success */ +/** Obtain an exclusive lock on a file. +@param[in] fd file descriptor +@param[in] name file name +@return 0 on success */ static int os_file_lock( -/*=========*/ - int fd, /*!< in: file descriptor */ - const char* name) /*!< in: file name */ + int fd, + const char* name) { struct flock lk; - ut_ad(!srv_read_only_mode); - lk.l_type = F_WRLCK; lk.l_whence = SEEK_SET; lk.l_start = lk.l_len = 0; if (fcntl(fd, F_SETLK, &lk) == -1) { - ib_logf(IB_LOG_LEVEL_ERROR, - "Unable to lock %s, error: %d", name, errno); + ib::error() + << "Unable to lock " << name + << " error: " << errno; if (errno == EAGAIN || errno == EACCES) { - ib_logf(IB_LOG_LEVEL_INFO, - "Check that you do not already have " - "another mysqld process using the " - "same InnoDB data or log files."); + + ib::info() + << "Check that you do not already have" + " another mysqld process using the" + " same InnoDB data or log files."; } return(-1); @@ -902,48 +1445,125 @@ os_file_lock( #endif /* USE_FILE_LOCK */ #ifndef UNIV_HOTBACKUP -/****************************************************************//** -Creates the seek mutexes used in positioned reads and writes. */ -UNIV_INTERN + +/** Calculates local segment number and aio array from global segment number. +@param[out] array aio wait array +@param[in] segment global segment number +@return local segment number within the aio array */ +ulint +AIO::get_array_and_local_segment( + AIO** array, + ulint segment) +{ + ulint local_segment; + ulint n_extra_segs = (srv_read_only_mode) ? 0 : 2; + + ut_a(segment < os_aio_n_segments); + + if (!srv_read_only_mode && segment < n_extra_segs) { + + /* We don't support ibuf/log IO during read only mode. */ + + if (segment == IO_IBUF_SEGMENT) { + + *array = s_ibuf; + + } else if (segment == IO_LOG_SEGMENT) { + + *array = s_log; + + } else { + *array = NULL; + } + + local_segment = 0; + + } else if (segment < s_reads->m_n_segments + n_extra_segs) { + + *array = s_reads; + local_segment = segment - n_extra_segs; + + } else { + *array = s_writes; + + local_segment = segment + - (s_reads->m_n_segments + n_extra_segs); + } + + return(local_segment); +} + +/** Frees a slot in the aio array. Assumes caller owns the mutex. +@param[in,out] slot Slot to release */ void -os_io_init_simple(void) -/*===================*/ +AIO::release(Slot* slot) { -#if !defined(HAVE_ATOMIC_BUILTINS) || UNIV_WORD_SIZE < 8 - os_file_count_mutex = os_mutex_create(); -#endif /* !HAVE_ATOMIC_BUILTINS || UNIV_WORD_SIZE < 8 */ + ut_ad(is_mutex_owned()); + + ut_ad(slot->is_reserved); + + slot->is_reserved = false; + + --m_n_reserved; - for (ulint i = 0; i < OS_FILE_N_SEEK_MUTEXES; i++) { - os_file_seek_mutexes[i] = os_mutex_create(); + if (m_n_reserved == m_slots.size() - 1) { + os_event_set(m_not_full); } + + if (m_n_reserved == 0) { + os_event_set(m_is_empty); + } + +#ifdef WIN_ASYNC_IO + + ResetEvent(slot->handle); + +#elif defined(LINUX_NATIVE_AIO) + + if (srv_use_native_aio) { + memset(&slot->control, 0x0, sizeof(slot->control)); + slot->ret = 0; + slot->n_bytes = 0; + } else { + /* These fields should not be used if we are not + using native AIO. */ + ut_ad(slot->n_bytes == 0); + ut_ad(slot->ret == 0); + } + +#endif /* WIN_ASYNC_IO */ } -/** Create a temporary file. This function is like tmpfile(3), but -the temporary file is created in the given parameter path. If the path -is null then it will create the file in the mysql server configuration -parameter (--tmpdir). -@param[in] path location for creating temporary file +/** Frees a slot in the AIO array. Assumes caller doesn't own the mutex. +@param[in,out] slot Slot to release */ +void +AIO::release_with_mutex(Slot* slot) +{ + acquire(); + + release(slot); + + release(); +} + +/** Creates a temporary file. This function is like tmpfile(3), but @return temporary file handle, or NULL on error */ -UNIV_INTERN FILE* -os_file_create_tmpfile( - const char* path) +os_file_create_tmpfile() { FILE* file = NULL; - WAIT_ALLOW_WRITES(); - int fd = innobase_mysql_tmpfile(path); - - ut_ad(!srv_read_only_mode); + int fd = innobase_mysql_tmpfile(); if (fd >= 0) { file = fdopen(fd, "w+b"); } - if (!file) { - ut_print_timestamp(stderr); - fprintf(stderr, - " InnoDB: Error: unable to create temporary file;" - " errno: %d\n", errno); + if (file == NULL) { + + ib::error() + << "Unable to create temporary file; errno: " + << errno; + if (fd >= 0) { close(fd); } @@ -951,678 +1571,1600 @@ os_file_create_tmpfile( return(file); } -#endif /* !UNIV_HOTBACKUP */ -/***********************************************************************//** -The os_file_opendir() function opens a directory stream corresponding to the -directory named by the dirname argument. The directory stream is positioned -at the first entry. In both Unix and Windows we automatically skip the '.' -and '..' items at the start of the directory listing. -@return directory stream, NULL if error */ -UNIV_INTERN -os_file_dir_t -os_file_opendir( -/*============*/ - const char* dirname, /*!< in: directory name; it must not - contain a trailing '\' or '/' */ - ibool error_is_fatal) /*!< in: TRUE if we should treat an - error as a fatal error; if we try to - open symlinks then we do not wish a - fatal error if it happens not to be - a directory */ +/** Rewind file to its start, read at most size - 1 bytes from it to str, and +NUL-terminate str. All errors are silently ignored. This function is +mostly meant to be used with temporary files. +@param[in,out] file File to read from +@param[in,out] str Buffer where to read +@param[in] size Size of buffer */ +void +os_file_read_string( + FILE* file, + char* str, + ulint size) { - os_file_dir_t dir; -#ifdef __WIN__ - LPWIN32_FIND_DATA lpFindFileData; - char path[OS_FILE_MAX_PATH + 3]; + if (size != 0) { + rewind(file); - ut_a(strlen(dirname) < OS_FILE_MAX_PATH); + size_t flen = fread(str, 1, size - 1, file); - strcpy(path, dirname); - strcpy(path + strlen(path), "\\*"); + str[flen] = '\0'; + } +} - /* Note that in Windows opening the 'directory stream' also retrieves - the first entry in the directory. Since it is '.', that is no problem, - as we will skip over the '.' and '..' entries anyway. */ +/** Decompress after a read and punch a hole in the file if it was a write +@param[in] type IO context +@param[in] fh Open file handle +@param[in,out] buf Buffer to transform +@param[in,out] scratch Scratch area for read decompression +@param[in] src_len Length of the buffer before compression +@param[in] len Used buffer length for write and output + buf len for read +@return DB_SUCCESS or error code */ +static +dberr_t +os_file_io_complete( + const IORequest&type, + os_file_t fh, + byte* buf, + byte* scratch, + ulint src_len, + ulint offset, + ulint len) +{ + /* We never compress/decompress the first page */ + ut_a(offset > 0); + ut_ad(type.validate()); - lpFindFileData = static_cast<LPWIN32_FIND_DATA>( - ut_malloc(sizeof(WIN32_FIND_DATA))); + if (!type.is_compression_enabled()) { - dir = FindFirstFile((LPCTSTR) path, lpFindFileData); + return(DB_SUCCESS); - ut_free(lpFindFileData); + } else if (type.is_read()) { - if (dir == INVALID_HANDLE_VALUE) { + ut_ad(!type.is_log()); - if (error_is_fatal) { - os_file_handle_error(dirname, "opendir", __FILE__, __LINE__); + return(os_file_decompress_page( + type.is_dblwr_recover(), buf, scratch, len)); + + } else if (type.punch_hole()) { + + ut_ad(len <= src_len); + ut_ad(!type.is_log()); + ut_ad(type.is_write()); + ut_ad(type.is_compressed()); + + /* Nothing to do. */ + if (len == src_len) { + return(DB_SUCCESS); } - return(NULL); - } +#ifdef UNIV_DEBUG + const ulint block_size = type.block_size(); +#endif /* UNIV_DEBUG */ - return(dir); -#else - dir = opendir(dirname); + /* We don't support multiple page sizes in the server + at the moment. */ + ut_ad(src_len == srv_page_size); - if (dir == NULL && error_is_fatal) { - os_file_handle_error(dirname, "opendir", __FILE__, __LINE__); + /* Must be a multiple of the compression unit size. */ + ut_ad((len % block_size) == 0); + ut_ad((offset % block_size) == 0); + + ut_ad(len + block_size <= src_len); + + offset += len; + + return(os_file_punch_hole(fh, offset, src_len - len)); } - return(dir); -#endif /* __WIN__ */ + ut_ad(!type.is_log()); + + return(DB_SUCCESS); } -/***********************************************************************//** -Closes a directory stream. -@return 0 if success, -1 if failure */ -UNIV_INTERN -int -os_file_closedir( -/*=============*/ - os_file_dir_t dir) /*!< in: directory stream */ +#endif /* !UNIV_HOTBACKUP */ + +/** This function returns a new path name after replacing the basename +in an old path with a new basename. The old_path is a full path +name including the extension. The tablename is in the normal +form "databasename/tablename". The new base name is found after +the forward slash. Both input strings are null terminated. + +This function allocates memory to be returned. It is the callers +responsibility to free the return value after it is no longer needed. + +@param[in] old_path Pathname +@param[in] tablename Contains new base name +@return own: new full pathname */ +char* +os_file_make_new_pathname( + const char* old_path, + const char* tablename) { -#ifdef __WIN__ - BOOL ret; + ulint dir_len; + char* last_slash; + char* base_name; + char* new_path; + ulint new_path_len; - ret = FindClose(dir); + /* Split the tablename into its database and table name components. + They are separated by a '/'. */ + last_slash = strrchr((char*) tablename, '/'); + base_name = last_slash ? last_slash + 1 : (char*) tablename; - if (!ret) { - os_file_handle_error_no_exit(NULL, "closedir", FALSE, __FILE__, __LINE__); + /* Find the offset of the last slash. We will strip off the + old basename.ibd which starts after that slash. */ + last_slash = strrchr((char*) old_path, OS_PATH_SEPARATOR); + dir_len = last_slash ? last_slash - old_path : strlen(old_path); - return(-1); + /* allocate a new path and move the old directory path to it. */ + new_path_len = dir_len + strlen(base_name) + sizeof "/.ibd"; + new_path = static_cast<char*>(ut_malloc_nokey(new_path_len)); + memcpy(new_path, old_path, dir_len); + + ut_snprintf(new_path + dir_len, + new_path_len - dir_len, + "%c%s.ibd", + OS_PATH_SEPARATOR, + base_name); + + return(new_path); +} + +/** This function reduces a null-terminated full remote path name into +the path that is sent by MySQL for DATA DIRECTORY clause. It replaces +the 'databasename/tablename.ibd' found at the end of the path with just +'tablename'. + +Since the result is always smaller than the path sent in, no new memory +is allocated. The caller should allocate memory for the path sent in. +This function manipulates that path in place. + +If the path format is not as expected, just return. The result is used +to inform a SHOW CREATE TABLE command. +@param[in,out] data_dir_path Full path/data_dir_path */ +void +os_file_make_data_dir_path( + char* data_dir_path) +{ + /* Replace the period before the extension with a null byte. */ + char* ptr = strrchr((char*) data_dir_path, '.'); + + if (ptr == NULL) { + return; } - return(0); -#else - int ret; + ptr[0] = '\0'; - ret = closedir(dir); + /* The tablename starts after the last slash. */ + ptr = strrchr((char*) data_dir_path, OS_PATH_SEPARATOR); - if (ret) { - os_file_handle_error_no_exit(NULL, "closedir", FALSE, __FILE__, __LINE__); + if (ptr == NULL) { + return; } - return(ret); -#endif /* __WIN__ */ + ptr[0] = '\0'; + + char* tablename = ptr + 1; + + /* The databasename starts after the next to last slash. */ + ptr = strrchr((char*) data_dir_path, OS_PATH_SEPARATOR); + + if (ptr == NULL) { + return; + } + + ulint tablename_len = ut_strlen(tablename); + + ut_memmove(++ptr, tablename, tablename_len); + + ptr[tablename_len] = '\0'; } -/***********************************************************************//** -This function returns information of the next file in the directory. We jump -over the '.' and '..' entries in the directory. -@return 0 if ok, -1 if error, 1 if at the end of the directory */ -UNIV_INTERN -int -os_file_readdir_next_file( -/*======================*/ - const char* dirname,/*!< in: directory name or path */ - os_file_dir_t dir, /*!< in: directory stream */ - os_file_stat_t* info) /*!< in/out: buffer where the info is returned */ +/** Check if the path refers to the root of a drive using a pointer +to the last directory separator that the caller has fixed. +@param[in] path path name +@param[in] path last directory separator in the path +@return true if this path is a drive root, false if not */ +UNIV_INLINE +bool +os_file_is_root( + const char* path, + const char* last_slash) { -#ifdef __WIN__ - LPWIN32_FIND_DATA lpFindFileData; - BOOL ret; + return( +#ifdef _WIN32 + (last_slash == path + 2 && path[1] == ':') || +#endif /* _WIN32 */ + last_slash == path); +} - lpFindFileData = static_cast<LPWIN32_FIND_DATA>( - ut_malloc(sizeof(WIN32_FIND_DATA))); -next_file: - ret = FindNextFile(dir, lpFindFileData); +/** Return the parent directory component of a null-terminated path. +Return a new buffer containing the string up to, but not including, +the final component of the path. +The path returned will not contain a trailing separator. +Do not return a root path, return NULL instead. +The final component trimmed off may be a filename or a directory name. +If the final component is the only component of the path, return NULL. +It is the caller's responsibility to free the returned string after it +is no longer needed. +@param[in] path Path name +@return own: parent directory of the path */ +static +char* +os_file_get_parent_dir( + const char* path) +{ + bool has_trailing_slash = false; - if (ret) { - ut_a(strlen((char*) lpFindFileData->cFileName) - < OS_FILE_MAX_PATH); + /* Find the offset of the last slash */ + const char* last_slash = strrchr(path, OS_PATH_SEPARATOR); - if (strcmp((char*) lpFindFileData->cFileName, ".") == 0 - || strcmp((char*) lpFindFileData->cFileName, "..") == 0) { + if (!last_slash) { + /* No slash in the path, return NULL */ + return(NULL); + } - goto next_file; - } + /* Ok, there is a slash. Is there anything after it? */ + if (static_cast<size_t>(last_slash - path + 1) == strlen(path)) { + has_trailing_slash = true; + } - strcpy(info->name, (char*) lpFindFileData->cFileName); + /* Reduce repetative slashes. */ + while (last_slash > path + && last_slash[-1] == OS_PATH_SEPARATOR) { + last_slash--; + } - info->size = (ib_int64_t)(lpFindFileData->nFileSizeLow) - + (((ib_int64_t)(lpFindFileData->nFileSizeHigh)) - << 32); + /* Check for the root of a drive. */ + if (os_file_is_root(path, last_slash)) { + return(NULL); + } - if (lpFindFileData->dwFileAttributes - & FILE_ATTRIBUTE_REPARSE_POINT) { - /* TODO: test Windows symlinks */ - /* TODO: MySQL has apparently its own symlink - implementation in Windows, dbname.sym can - redirect a database directory: - REFMAN "windows-symbolic-links.html" */ - info->type = OS_FILE_TYPE_LINK; - } else if (lpFindFileData->dwFileAttributes - & FILE_ATTRIBUTE_DIRECTORY) { - info->type = OS_FILE_TYPE_DIR; - } else { - /* It is probably safest to assume that all other - file types are normal. Better to check them rather - than blindly skip them. */ + /* If a trailing slash prevented the first strrchr() from trimming + the last component of the path, trim that component now. */ + if (has_trailing_slash) { + /* Back up to the previous slash. */ + last_slash--; + while (last_slash > path + && last_slash[0] != OS_PATH_SEPARATOR) { + last_slash--; + } - info->type = OS_FILE_TYPE_FILE; + /* Reduce repetative slashes. */ + while (last_slash > path + && last_slash[-1] == OS_PATH_SEPARATOR) { + last_slash--; } } - ut_free(lpFindFileData); + /* Check for the root of a drive. */ + if (os_file_is_root(path, last_slash)) { + return(NULL); + } - if (ret) { - return(0); - } else if (GetLastError() == ERROR_NO_MORE_FILES) { + /* Non-trivial directory component */ - return(1); - } else { - os_file_handle_error_no_exit(NULL, "readdir_next_file", FALSE, __FILE__, __LINE__); - return(-1); - } -#else - struct dirent* ent; - char* full_path; - int ret; - struct stat statinfo; -#ifdef HAVE_READDIR_R - char dirent_buf[sizeof(struct dirent) - + _POSIX_PATH_MAX + 100]; - /* In /mysys/my_lib.c, _POSIX_PATH_MAX + 1 is used as - the max file name len; but in most standards, the - length is NAME_MAX; we add 100 to be even safer */ -#endif + return(mem_strdupl(path, last_slash - path)); +} +#ifdef UNIV_ENABLE_UNIT_TEST_GET_PARENT_DIR -next_file: +/* Test the function os_file_get_parent_dir. */ +void +test_os_file_get_parent_dir( + const char* child_dir, + const char* expected_dir) +{ + char* child = mem_strdup(child_dir); + char* expected = expected_dir == NULL ? NULL + : mem_strdup(expected_dir); + + /* os_file_get_parent_dir() assumes that separators are + converted to OS_PATH_SEPARATOR. */ + os_normalize_path(child); + os_normalize_path(expected); + + char* parent = os_file_get_parent_dir(child); + + bool unexpected = (expected == NULL + ? (parent != NULL) + : (0 != strcmp(parent, expected))); + if (unexpected) { + ib::fatal() << "os_file_get_parent_dir('" << child + << "') returned '" << parent + << "', instead of '" << expected << "'."; + } + ut_free(parent); + ut_free(child); + ut_free(expected); +} -#ifdef HAVE_READDIR_R - ret = readdir_r(dir, (struct dirent*) dirent_buf, &ent); +/* Test the function os_file_get_parent_dir. */ +void +unit_test_os_file_get_parent_dir() +{ + test_os_file_get_parent_dir("/usr/lib/a", "/usr/lib"); + test_os_file_get_parent_dir("/usr/", NULL); + test_os_file_get_parent_dir("//usr//", NULL); + test_os_file_get_parent_dir("usr", NULL); + test_os_file_get_parent_dir("usr//", NULL); + test_os_file_get_parent_dir("/", NULL); + test_os_file_get_parent_dir("//", NULL); + test_os_file_get_parent_dir(".", NULL); + test_os_file_get_parent_dir("..", NULL); +# ifdef _WIN32 + test_os_file_get_parent_dir("D:", NULL); + test_os_file_get_parent_dir("D:/", NULL); + test_os_file_get_parent_dir("D:\\", NULL); + test_os_file_get_parent_dir("D:/data", NULL); + test_os_file_get_parent_dir("D:/data/", NULL); + test_os_file_get_parent_dir("D:\\data\\", NULL); + test_os_file_get_parent_dir("D:///data/////", NULL); + test_os_file_get_parent_dir("D:\\\\\\data\\\\\\\\", NULL); + test_os_file_get_parent_dir("D:/data//a", "D:/data"); + test_os_file_get_parent_dir("D:\\data\\\\a", "D:\\data"); + test_os_file_get_parent_dir("D:///data//a///b/", "D:///data//a"); + test_os_file_get_parent_dir("D:\\\\\\data\\\\a\\\\\\b\\", "D:\\\\\\data\\\\a"); +#endif /* _WIN32 */ +} +#endif /* UNIV_ENABLE_UNIT_TEST_GET_PARENT_DIR */ - if (ret != 0 -#ifdef UNIV_AIX - /* On AIX, only if we got non-NULL 'ent' (result) value and - a non-zero 'ret' (return) value, it indicates a failed - readdir_r() call. An NULL 'ent' with an non-zero 'ret' - would indicate the "end of the directory" is reached. */ - && ent != NULL -#endif - ) { - fprintf(stderr, - "InnoDB: cannot read directory %s, error %lu\n", - dirname, (ulong) ret); - return(-1); +/** Creates all missing subdirectories along the given path. +@param[in] path Path name +@return DB_SUCCESS if OK, otherwise error code. */ +dberr_t +os_file_create_subdirs_if_needed( + const char* path) +{ + if (srv_read_only_mode) { + + ib::error() + << "read only mode set. Can't create " + << "subdirectories '" << path << "'"; + + return(DB_READ_ONLY); + } - if (ent == NULL) { - /* End of directory */ + char* subdir = os_file_get_parent_dir(path); - return(1); + if (subdir == NULL) { + /* subdir is root or cwd, nothing to do */ + return(DB_SUCCESS); } - ut_a(strlen(ent->d_name) < _POSIX_PATH_MAX + 100 - 1); -#else - ent = readdir(dir); + /* Test if subdir exists */ + os_file_type_t type; + bool subdir_exists; + bool success = os_file_status(subdir, &subdir_exists, &type); - if (ent == NULL) { + if (success && !subdir_exists) { - return(1); + /* Subdir does not exist, create it */ + dberr_t err = os_file_create_subdirs_if_needed(subdir); + + if (err != DB_SUCCESS) { + + ut_free(subdir); + + return(err); + } + + success = os_file_create_directory(subdir, false); } + + ut_free(subdir); + + return(success ? DB_SUCCESS : DB_ERROR); +} + +/** Allocate the buffer for IO on a transparently compressed table. +@param[in] type IO flags +@param[out] buf buffer to read or write +@param[in,out] n number of bytes to read/write, starting from + offset +@return pointer to allocated page, compressed data is written to the offset + that is aligned on UNIV_SECTOR_SIZE of Block.m_ptr */ +static +byte* +os_file_compress_page( + IORequest& type, + void*& buf, + ulint* n) +{ + ut_ad(!type.is_log()); + ut_ad(type.is_write()); + ut_ad(type.is_compressed()); + + ulint n_alloc = *n * 2; + + ut_a(n_alloc < UNIV_PAGE_SIZE_MAX * 2); +#ifdef HAVE_LZ4 + ut_a(type.compression_algorithm().m_type != Compression::LZ4 + || static_cast<ulint>(LZ4_COMPRESSBOUND(*n)) < n_alloc); #endif - ut_a(strlen(ent->d_name) < OS_FILE_MAX_PATH); - if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) { + byte* ptr = reinterpret_cast<byte*>(ut_malloc_nokey(n_alloc)); - goto next_file; + if (ptr == NULL) { + return(NULL); } - strcpy(info->name, ent->d_name); + ulint old_compressed_len; + ulint compressed_len = *n; - full_path = static_cast<char*>( - ut_malloc(strlen(dirname) + strlen(ent->d_name) + 10)); + old_compressed_len = mach_read_from_2( + reinterpret_cast<byte*>(buf) + + FIL_PAGE_COMPRESS_SIZE_V1); - sprintf(full_path, "%s/%s", dirname, ent->d_name); + if (old_compressed_len > 0) { + old_compressed_len = ut_calc_align( + old_compressed_len + FIL_PAGE_DATA, + type.block_size()); + } - ret = stat(full_path, &statinfo); + byte* compressed_page; - if (ret) { + compressed_page = static_cast<byte*>( + ut_align(ptr, UNIV_SECTOR_SIZE)); - if (errno == ENOENT) { - /* readdir() returned a file that does not exist, - it must have been deleted in the meantime. Do what - would have happened if the file was deleted before - readdir() - ignore and go to the next entry. - If this is the last entry then info->name will still - contain the name of the deleted file when this - function returns, but this is not an issue since the - caller shouldn't be looking at info when end of - directory is returned. */ + byte* buf_ptr; - ut_free(full_path); + buf_ptr = os_file_compress_page( + type.compression_algorithm(), + type.block_size(), + reinterpret_cast<byte*>(buf), + *n, + compressed_page, + &compressed_len); - goto next_file; + if (buf_ptr != buf) { + /* Set new compressed size to uncompressed page. */ + memcpy(reinterpret_cast<byte*>(buf) + FIL_PAGE_COMPRESS_SIZE_V1, + buf_ptr + FIL_PAGE_COMPRESS_SIZE_V1, 2); + + buf = buf_ptr; + *n = compressed_len; + + if (compressed_len >= old_compressed_len) { + + ut_ad(old_compressed_len <= UNIV_PAGE_SIZE); + + type.clear_punch_hole(); } + } - os_file_handle_error_no_exit(full_path, "stat", FALSE, __FILE__, __LINE__); + return(ptr); +} - ut_free(full_path); +#ifndef _WIN32 - return(-1); +/** Do the read/write +@param[in] request The IO context and type +@return the number of bytes read/written or negative value on error */ +ssize_t +SyncFileIO::execute(const IORequest& request) +{ + ssize_t n_bytes; + + if (request.is_read()) { + n_bytes = pread(m_fh, m_buf, m_n, m_offset); + } else { + ut_ad(request.is_write()); + n_bytes = pwrite(m_fh, m_buf, m_n, m_offset); } - info->size = (ib_int64_t) statinfo.st_size; + return(n_bytes); +} - if (S_ISDIR(statinfo.st_mode)) { - info->type = OS_FILE_TYPE_DIR; - } else if (S_ISLNK(statinfo.st_mode)) { - info->type = OS_FILE_TYPE_LINK; - } else if (S_ISREG(statinfo.st_mode)) { - info->type = OS_FILE_TYPE_FILE; +/** Free storage space associated with a section of the file. +@param[in] fh Open file handle +@param[in] off Starting offset (SEEK_SET) +@param[in] len Size of the hole +@return DB_SUCCESS or error code */ +static +dberr_t +os_file_punch_hole_posix( + os_file_t fh, + os_offset_t off, + os_offset_t len) +{ + +#ifdef HAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE + const int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; + + int ret = fallocate(fh, mode, off, len); + + if (ret == 0) { + return(DB_SUCCESS); + } + + ut_a(ret == -1); + + if (errno == ENOTSUP) { + return(DB_IO_NO_PUNCH_HOLE); + } + + ib::warn() + << "fallocate(" << fh + <<", FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, " + << off << ", " << len << ") returned errno: " + << errno; + + return(DB_IO_ERROR); + +#elif defined(UNIV_SOLARIS) + + // Use F_FREESP + +#endif /* HAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE */ + + return(DB_IO_NO_PUNCH_HOLE); +} + +#if defined(LINUX_NATIVE_AIO) + +/** Linux native AIO handler */ +class LinuxAIOHandler { +public: + /** + @param[in] global_segment The global segment*/ + LinuxAIOHandler(ulint global_segment) + : + m_global_segment(global_segment) + { + /* Should never be doing Sync IO here. */ + ut_a(m_global_segment != ULINT_UNDEFINED); + + /* Find the array and the local segment. */ + + m_segment = AIO::get_array_and_local_segment( + &m_array, m_global_segment); + + m_n_slots = m_array->slots_per_segment(); + } + + /** Destructor */ + ~LinuxAIOHandler() + { + // No op + } + + /** + Process a Linux AIO request + @param[out] m1 the messages passed with the + @param[out] m2 AIO request; note that in case the + AIO operation failed, these output + parameters are valid and can be used to + restart the operation. + @param[out] request IO context + @return DB_SUCCESS or error code */ + dberr_t poll(fil_node_t** m1, void** m2, IORequest* request); + +private: + /** Resubmit an IO request that was only partially successful + @param[in,out] slot Request to resubmit + @return DB_SUCCESS or DB_FAIL if the IO resubmit request failed */ + dberr_t resubmit(Slot* slot); + + /** Check if the AIO succeeded + @param[in,out] slot The slot to check + @return DB_SUCCESS, DB_FAIL if the operation should be retried or + DB_IO_ERROR on all other errors */ + dberr_t check_state(Slot* slot); + + /** @return true if a shutdown was detected */ + bool is_shutdown() const + { + return(srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS + && !buf_page_cleaner_is_active); + } + + /** If no slot was found then the m_array->m_mutex will be released. + @param[out] n_pending The number of pending IOs + @return NULL or a slot that has completed IO */ + Slot* find_completed_slot(ulint* n_pending); + + /** This is called from within the IO-thread. If there are no completed + IO requests in the slot array, the thread calls this function to + collect more requests from the Linux kernel. + The IO-thread waits on io_getevents(), which is a blocking call, with + a timeout value. Unless the system is very heavy loaded, keeping the + IO-thread very busy, the io-thread will spend most of its time waiting + in this function. + The IO-thread also exits in this function. It checks server status at + each wakeup and that is why we use timed wait in io_getevents(). */ + void collect(); + +private: + /** Slot array */ + AIO* m_array; + + /** Number of slots inthe local segment */ + ulint m_n_slots; + + /** The local segment to check */ + ulint m_segment; + + /** The global segment */ + ulint m_global_segment; +}; + +/** Resubmit an IO request that was only partially successful +@param[in,out] slot Request to resubmit +@return DB_SUCCESS or DB_FAIL if the IO resubmit request failed */ +dberr_t +LinuxAIOHandler::resubmit(Slot* slot) +{ +#ifdef UNIV_DEBUG + /* Bytes already read/written out */ + ulint n_bytes = slot->ptr - slot->buf; + + ut_ad(m_array->is_mutex_owned()); + + ut_ad(n_bytes < slot->original_len); + ut_ad(static_cast<ulint>(slot->n_bytes) < slot->original_len - n_bytes); + /* Partial read or write scenario */ + ut_ad(slot->len >= static_cast<ulint>(slot->n_bytes)); +#endif /* UNIV_DEBUG */ + + slot->len -= slot->n_bytes; + slot->ptr += slot->n_bytes; + slot->offset += slot->n_bytes; + + /* Resetting the bytes read/written */ + slot->n_bytes = 0; + slot->io_already_done = false; + + struct iocb* iocb = &slot->control; + + if (slot->type.is_read()) { + + io_prep_pread( + iocb, + slot->file, + slot->ptr, + slot->len, + static_cast<off_t>(slot->offset)); } else { - info->type = OS_FILE_TYPE_UNKNOWN; + + ut_a(slot->type.is_write()); + + io_prep_pwrite( + iocb, + slot->file, + slot->ptr, + slot->len, + static_cast<off_t>(slot->offset)); } - ut_free(full_path); + iocb->data = slot; - return(0); -#endif + /* Resubmit an I/O request */ + int ret = io_submit(m_array->io_ctx(m_segment), 1, &iocb); + + if (ret < -1) { + errno = -ret; + } + + return(ret < 0 ? DB_IO_PARTIAL_FAILED : DB_SUCCESS); } -/*****************************************************************//** -This function attempts to create a directory named pathname. The new -directory gets default permissions. On Unix the permissions are -(0770 & ~umask). If the directory exists already, nothing is done and -the call succeeds, unless the fail_if_exists arguments is true. -If another error occurs, such as a permission error, this does not crash, -but reports the error and returns FALSE. -@return TRUE if call succeeds, FALSE on error */ -UNIV_INTERN -ibool -os_file_create_directory( -/*=====================*/ - const char* pathname, /*!< in: directory name as - null-terminated string */ - ibool fail_if_exists) /*!< in: if TRUE, pre-existing directory - is treated as an error. */ +/** Check if the AIO succeeded +@param[in,out] slot The slot to check +@return DB_SUCCESS, DB_FAIL if the operation should be retried or + DB_IO_ERROR on all other errors */ +dberr_t +LinuxAIOHandler::check_state(Slot* slot) { -#ifdef __WIN__ - BOOL rcode; + ut_ad(m_array->is_mutex_owned()); - rcode = CreateDirectory((LPCTSTR) pathname, NULL); - if (!(rcode != 0 - || (GetLastError() == ERROR_ALREADY_EXISTS - && !fail_if_exists))) { + /* Note that it may be that there is more then one completed + IO requests. We process them one at a time. We may have a case + here to improve the performance slightly by dealing with all + requests in one sweep. */ - os_file_handle_error_no_exit( - pathname, "CreateDirectory", FALSE, __FILE__, __LINE__); + srv_set_io_thread_op_info( + m_global_segment, "processing completed aio requests"); + + ut_ad(slot->io_already_done); + + dberr_t err; + + if (slot->ret == 0) { + + err = AIOHandler::post_io_processing(slot); + + } else { + errno = -slot->ret; - return(FALSE); + /* os_file_handle_error does tell us if we should retry + this IO. As it stands now, we don't do this retry when + reaping requests from a different context than + the dispatcher. This non-retry logic is the same for + Windows and Linux native AIO. + We should probably look into this to transparently + re-submit the IO. */ + os_file_handle_error(slot->name, "Linux aio"); + + err = DB_IO_ERROR; } - return(TRUE); -#else - int rcode; - WAIT_ALLOW_WRITES(); + return(err); +} - rcode = mkdir(pathname, 0770); +/** If no slot was found then the m_array->m_mutex will be released. +@param[out] n_pending The number of pending IOs +@return NULL or a slot that has completed IO */ +Slot* +LinuxAIOHandler::find_completed_slot(ulint* n_pending) +{ + ulint offset = m_n_slots * m_segment; - if (!(rcode == 0 || (errno == EEXIST && !fail_if_exists))) { - /* failure */ - os_file_handle_error_no_exit(pathname, "mkdir", FALSE, __FILE__, __LINE__); + *n_pending = 0; + + m_array->acquire(); + + Slot* slot = m_array->at(offset); + + for (ulint i = 0; i < m_n_slots; ++i, ++slot) { - return(FALSE); + if (slot->is_reserved) { + + ++*n_pending; + + if (slot->io_already_done) { + + /* Something for us to work on. + Note: We don't release the mutex. */ + return(slot); + } + } } - return (TRUE); -#endif /* __WIN__ */ + m_array->release(); + + return(NULL); } -/****************************************************************//** -NOTE! Use the corresponding macro os_file_create_simple(), not directly -this function! -A simple function to open or create a file. -@return own: handle to the file, not defined if error, error number -can be retrieved with os_file_get_last_error */ -UNIV_INTERN -os_file_t -os_file_create_simple_func( -/*=======================*/ - const char* name, /*!< in: name of the file or path as a - null-terminated string */ - ulint create_mode,/*!< in: create mode */ - ulint access_type,/*!< in: OS_FILE_READ_ONLY or - OS_FILE_READ_WRITE */ - ibool* success)/*!< out: TRUE if succeed, FALSE if error */ +/** This function is only used in Linux native asynchronous i/o. This is +called from within the io-thread. If there are no completed IO requests +in the slot array, the thread calls this function to collect more +requests from the kernel. +The io-thread waits on io_getevents(), which is a blocking call, with +a timeout value. Unless the system is very heavy loaded, keeping the +io-thread very busy, the io-thread will spend most of its time waiting +in this function. +The io-thread also exits in this function. It checks server status at +each wakeup and that is why we use timed wait in io_getevents(). */ +void +LinuxAIOHandler::collect() { - os_file_t file; - ibool retry; + ut_ad(m_n_slots > 0); + ut_ad(m_array != NULL); + ut_ad(m_segment < m_array->get_n_segments()); - *success = FALSE; -#ifdef __WIN__ - DWORD access; - DWORD create_flag; - DWORD attributes = 0; + /* Which io_context we are going to use. */ + io_context* io_ctx = m_array->io_ctx(m_segment); - ut_a(!(create_mode & OS_FILE_ON_ERROR_SILENT)); - ut_a(!(create_mode & OS_FILE_ON_ERROR_NO_EXIT)); + /* Starting point of the m_segment we will be working on. */ + ulint start_pos = m_segment * m_n_slots; - if (create_mode == OS_FILE_OPEN) { + /* End point. */ + ulint end_pos = start_pos + m_n_slots; - create_flag = OPEN_EXISTING; + for (;;) { + struct io_event* events; - } else if (srv_read_only_mode) { + /* Which part of event array we are going to work on. */ + events = m_array->io_events(m_segment * m_n_slots); - create_flag = OPEN_EXISTING; + /* Initialize the events. */ + memset(events, 0, sizeof(*events) * m_n_slots); - } else if (create_mode == OS_FILE_CREATE) { + /* The timeout value is arbitrary. We probably need + to experiment with it a little. */ + struct timespec timeout; - create_flag = CREATE_NEW; + timeout.tv_sec = 0; + timeout.tv_nsec = OS_AIO_REAP_TIMEOUT; - } else if (create_mode == OS_FILE_CREATE_PATH) { + int ret; - ut_a(!srv_read_only_mode); + ret = io_getevents(io_ctx, 1, m_n_slots, events, &timeout); - /* Create subdirs along the path if needed */ - *success = os_file_create_subdirs_if_needed(name); + for (int i = 0; i < ret; ++i) { - if (!*success) { + struct iocb* iocb; + + iocb = reinterpret_cast<struct iocb*>(events[i].obj); + ut_a(iocb != NULL); + + Slot* slot = reinterpret_cast<Slot*>(iocb->data); + + /* Some sanity checks. */ + ut_a(slot != NULL); + ut_a(slot->is_reserved); + + /* We are not scribbling previous segment. */ + ut_a(slot->pos >= start_pos); + + /* We have not overstepped to next segment. */ + ut_a(slot->pos < end_pos); - ib_logf(IB_LOG_LEVEL_ERROR, - "Unable to create subdirectories '%s'", - name); + /* We never compress/decompress the first page */ + + if (slot->offset > 0 + && !slot->skip_punch_hole + && slot->type.is_compression_enabled() + && !slot->type.is_log() + && slot->type.is_write() + && slot->type.is_compressed() + && slot->type.punch_hole()) { + + slot->err = AIOHandler::io_complete(slot); + } else { + slot->err = DB_SUCCESS; + } - return((os_file_t) -1); + /* Mark this request as completed. The error handling + will be done in the calling function. */ + m_array->acquire(); + + slot->ret = events[i].res2; + slot->io_already_done = true; + slot->n_bytes = events[i].res; + + m_array->release(); } - create_flag = CREATE_NEW; - create_mode = OS_FILE_CREATE; + if (srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS + || !buf_page_cleaner_is_active + || ret > 0) { - } else { - ib_logf(IB_LOG_LEVEL_ERROR, - "Unknown file create mode (%lu) for file '%s'", - create_mode, name); + break; + } - return((os_file_t) -1); - } + /* This error handling is for any error in collecting the + IO requests. The errors, if any, for any particular IO + request are simply passed on to the calling routine. */ - if (access_type == OS_FILE_READ_ONLY) { - access = GENERIC_READ; - } else if (srv_read_only_mode) { + switch (ret) { + case -EAGAIN: + /* Not enough resources! Try again. */ - ib_logf(IB_LOG_LEVEL_INFO, - "read only mode set. Unable to " - "open file '%s' in RW mode, trying RO mode", name); + case -EINTR: + /* Interrupted! The behaviour in case of an interrupt. + If we have some completed IOs available then the + return code will be the number of IOs. We get EINTR + only if there are no completed IOs and we have been + interrupted. */ - access = GENERIC_READ; + case 0: + /* No pending request! Go back and check again. */ - } else if (access_type == OS_FILE_READ_WRITE) { - access = GENERIC_READ | GENERIC_WRITE; - } else { - ib_logf(IB_LOG_LEVEL_ERROR, - "Unknown file access type (%lu) for file '%s'", - access_type, name); + continue; + } + + /* All other errors should cause a trap for now. */ + ib::fatal() + << "Unexpected ret_code[" << ret + << "] from io_getevents()!"; - return((os_file_t) -1); + break; } +} - do { - /* Use default security attributes and no template file. */ +/** Process a Linux AIO request +@param[out] m1 the messages passed with the +@param[out] m2 AIO request; note that in case the + AIO operation failed, these output + parameters are valid and can be used to + restart the operation. +@param[out] request IO context +@return DB_SUCCESS or error code */ +dberr_t +LinuxAIOHandler::poll(fil_node_t** m1, void** m2, IORequest* request) +{ + dberr_t err; + Slot* slot; - file = CreateFile( - (LPCTSTR) name, access, FILE_SHARE_READ, NULL, - create_flag, attributes, NULL); + /* Loop until we have found a completed request. */ + for (;;) { - if (file == INVALID_HANDLE_VALUE) { + ulint n_pending; - *success = FALSE; + slot = find_completed_slot(&n_pending); - retry = os_file_handle_error( - name, create_mode == OS_FILE_OPEN ? - "open" : "create", __FILE__, __LINE__); + if (slot != NULL) { + + ut_ad(m_array->is_mutex_owned()); + + err = check_state(slot); + + /* DB_FAIL is not a hard error, we should retry */ + if (err != DB_FAIL) { + break; + } + + /* Partial IO, resubmit request for + remaining bytes to read/write */ + err = resubmit(slot); + + if (err != DB_SUCCESS) { + break; + } + + m_array->release(); + + } else if (is_shutdown() && n_pending == 0) { + + /* There is no completed request. If there is + no pending request at all, and the system is + being shut down, exit. */ + + *m1 = NULL; + *m2 = NULL; + + return(DB_SUCCESS); } else { - *success = TRUE; - retry = false; + + /* Wait for some request. Note that we return + from wait if we have found a request. */ + + srv_set_io_thread_op_info( + m_global_segment, + "waiting for completed aio requests"); + + collect(); } + } - } while (retry); + if (err == DB_IO_PARTIAL_FAILED) { + /* Aborting in case of submit failure */ + ib::fatal() + << "Native Linux AIO interface. " + "io_submit() call failed when " + "resubmitting a partial I/O " + "request on the file " << slot->name + << "."; + } -#else /* __WIN__ */ - int create_flag; - if (create_mode != OS_FILE_OPEN && create_mode != OS_FILE_OPEN_RAW) - WAIT_ALLOW_WRITES(); + *m1 = slot->m1; + *m2 = slot->m2; - ut_a(!(create_mode & OS_FILE_ON_ERROR_SILENT)); - ut_a(!(create_mode & OS_FILE_ON_ERROR_NO_EXIT)); + *request = slot->type; - if (create_mode == OS_FILE_OPEN) { + m_array->release(slot); - if (access_type == OS_FILE_READ_ONLY) { - create_flag = O_RDONLY; - } else if (srv_read_only_mode) { - create_flag = O_RDONLY; - } else { - create_flag = O_RDWR; + m_array->release(); + + return(err); +} + +/** This function is only used in Linux native asynchronous i/o. +Waits for an aio operation to complete. This function is used to wait for +the completed requests. The aio array of pending requests is divided +into segments. The thread specifies which segment or slot it wants to wait +for. NOTE: this function will also take care of freeing the aio slot, +therefore no other thread is allowed to do the freeing! + +@param[in] global_seg segment number in the aio array + to wait for; segment 0 is the ibuf + i/o thread, segment 1 is log i/o thread, + then follow the non-ibuf read threads, + and the last are the non-ibuf write + threads. +@param[out] m1 the messages passed with the +@param[out] m2 AIO request; note that in case the + AIO operation failed, these output + parameters are valid and can be used to + restart the operation. +@param[out]xi request IO context +@return DB_SUCCESS if the IO was successful */ +static +dberr_t +os_aio_linux_handler( + ulint global_segment, + fil_node_t** m1, + void** m2, + IORequest* request) +{ + LinuxAIOHandler handler(global_segment); + + dberr_t err = handler.poll(m1, m2, request); + + if (err == DB_IO_NO_PUNCH_HOLE) { + fil_no_punch_hole(*m1); + err = DB_SUCCESS; + } + + return(err); +} + +/** Dispatch an AIO request to the kernel. +@param[in,out] slot an already reserved slot +@return true on success. */ +bool +AIO::linux_dispatch(Slot* slot) +{ + ut_a(slot->is_reserved); + ut_ad(slot->type.validate()); + + /* Find out what we are going to work with. + The iocb struct is directly in the slot. + The io_context is one per segment. */ + + ulint io_ctx_index; + struct iocb* iocb = &slot->control; + + io_ctx_index = (slot->pos * m_n_segments) / m_slots.size(); + + int ret = io_submit(m_aio_ctx[io_ctx_index], 1, &iocb); + + /* io_submit() returns number of successfully queued requests + or -errno. */ + + if (ret != 1) { + errno = -ret; + } + + return(ret == 1); +} + +/** Creates an io_context for native linux AIO. +@param[in] max_events number of events +@param[out] io_ctx io_ctx to initialize. +@return true on success. */ +bool +AIO::linux_create_io_ctx( + ulint max_events, + io_context_t* io_ctx) +{ + ssize_t n_retries = 0; + + for (;;) { + + memset(io_ctx, 0x0, sizeof(*io_ctx)); + + /* Initialize the io_ctx. Tell it how many pending + IO requests this context will handle. */ + + int ret = io_setup(max_events, io_ctx); + + if (ret == 0) { + /* Success. Return now. */ + return(true); } - } else if (srv_read_only_mode) { + /* If we hit EAGAIN we'll make a few attempts before failing. */ + + switch (ret) { + case -EAGAIN: + if (n_retries == 0) { + /* First time around. */ + ib::warn() + << "io_setup() failed with EAGAIN." + " Will make " + << OS_AIO_IO_SETUP_RETRY_ATTEMPTS + << " attempts before giving up."; + } - create_flag = O_RDONLY; + if (n_retries < OS_AIO_IO_SETUP_RETRY_ATTEMPTS) { - } else if (create_mode == OS_FILE_CREATE) { + ++n_retries; - create_flag = O_RDWR | O_CREAT | O_EXCL; + ib::warn() + << "io_setup() attempt " + << n_retries << "."; - } else if (create_mode == OS_FILE_CREATE_PATH) { + os_thread_sleep(OS_AIO_IO_SETUP_RETRY_SLEEP); - /* Create subdirs along the path if needed */ + continue; + } - *success = os_file_create_subdirs_if_needed(name); + /* Have tried enough. Better call it a day. */ + ib::error() + << "io_setup() failed with EAGAIN after " + << OS_AIO_IO_SETUP_RETRY_ATTEMPTS + << " attempts."; + break; - if (!*success) { + case -ENOSYS: + ib::error() + << "Linux Native AIO interface" + " is not supported on this platform. Please" + " check your OS documentation and install" + " appropriate binary of InnoDB."; - ib_logf(IB_LOG_LEVEL_ERROR, - "Unable to create subdirectories '%s'", - name); + break; - return((os_file_t) -1); + default: + ib::error() + << "Linux Native AIO setup" + << " returned following error[" + << ret << "]"; + break; } - create_flag = O_RDWR | O_CREAT | O_EXCL; - create_mode = OS_FILE_CREATE; + ib::info() + << "You can disable Linux Native AIO by" + " setting innodb_use_native_aio = 0 in my.cnf"; + + break; + } + + return(false); +} + +/** Checks if the system supports native linux aio. On some kernel +versions where native aio is supported it won't work on tmpfs. In such +cases we can't use native aio as it is not possible to mix simulated +and native aio. +@return: true if supported, false otherwise. */ +bool +AIO::is_linux_native_aio_supported() +{ + int fd; + io_context_t io_ctx; + char name[1000]; + + if (!linux_create_io_ctx(1, &io_ctx)) { + + /* The platform does not support native aio. */ + + return(false); + + } else if (!srv_read_only_mode) { + + /* Now check if tmpdir supports native aio ops. */ + fd = innobase_mysql_tmpfile(); + + if (fd < 0) { + ib::warn() + << "Unable to create temp file to check" + " native AIO support."; + + return(false); + } } else { - ib_logf(IB_LOG_LEVEL_ERROR, - "Unknown file create mode (%lu) for file '%s'", - create_mode, name); + os_normalize_path_for_win(srv_log_group_home_dir); + + ulint dirnamelen = strlen(srv_log_group_home_dir); + + ut_a(dirnamelen < (sizeof name) - 10 - sizeof "ib_logfile"); - return((os_file_t) -1); + memcpy(name, srv_log_group_home_dir, dirnamelen); + + /* Add a path separator if needed. */ + if (dirnamelen && name[dirnamelen - 1] != OS_PATH_SEPARATOR) { + + name[dirnamelen++] = OS_PATH_SEPARATOR; + } + + strcpy(name + dirnamelen, "ib_logfile0"); + + fd = ::open(name, O_RDONLY); + + if (fd == -1) { + + ib::warn() + << "Unable to open" + << " \"" << name << "\" to check native" + << " AIO read support."; + + return(false); + } } - do { - file = ::open(name, create_flag, os_innodb_umask); + struct io_event io_event; - if (file == -1) { - *success = FALSE; + memset(&io_event, 0x0, sizeof(io_event)); + + byte* buf = static_cast<byte*>(ut_malloc_nokey(UNIV_PAGE_SIZE * 2)); + byte* ptr = static_cast<byte*>(ut_align(buf, UNIV_PAGE_SIZE)); + + struct iocb iocb; + + /* Suppress valgrind warning. */ + memset(buf, 0x00, UNIV_PAGE_SIZE * 2); + memset(&iocb, 0x0, sizeof(iocb)); + + struct iocb* p_iocb = &iocb; + + if (!srv_read_only_mode) { + + io_prep_pwrite(p_iocb, fd, ptr, UNIV_PAGE_SIZE, 0); + + } else { + ut_a(UNIV_PAGE_SIZE >= 512); + io_prep_pread(p_iocb, fd, ptr, 512, 0); + } + + int err = io_submit(io_ctx, 1, &p_iocb); + + if (err >= 1) { + /* Now collect the submitted IO request. */ + err = io_getevents(io_ctx, 1, 1, &io_event, NULL); + } + + ut_free(buf); + close(fd); + + switch (err) { + case 1: + return(true); + + case -EINVAL: + case -ENOSYS: + ib::error() + << "Linux Native AIO not supported. You can either" + " move " + << (srv_read_only_mode ? name : "tmpdir") + << " to a file system that supports native" + " AIO or you can set innodb_use_native_aio to" + " FALSE to avoid this message."; + + /* fall through. */ + default: + ib::error() + << "Linux Native AIO check on " + << (srv_read_only_mode ? name : "tmpdir") + << "returned error[" << -err << "]"; + } + + return(false); +} + +#endif /* LINUX_NATIVE_AIO */ + +/** Retrieves the last error number if an error occurs in a file io function. +The number should be retrieved before any other OS calls (because they may +overwrite the error number). If the number is not known to this program, +the OS error number + 100 is returned. +@param[in] report_all_errors true if we want an error message + printed of all errors +@param[in] on_error_silent true then don't print any diagnostic + to the log +@return error number, or OS error number + 100 */ +static +ulint +os_file_get_last_error_low( + bool report_all_errors, + bool on_error_silent) +{ + int err = errno; + + if (err == 0) { + return(0); + } + + if (report_all_errors + || (err != ENOSPC && err != EEXIST && !on_error_silent)) { + + ib::error() + << "Operating system error number " + << err + << " in a file operation."; + + if (err == ENOENT) { + + ib::error() + << "The error means the system" + " cannot find the path specified."; + + if (srv_is_being_started) { + + ib::error() + << "If you are installing InnoDB," + " remember that you must create" + " directories yourself, InnoDB" + " does not create them."; + } + } else if (err == EACCES) { + + ib::error() + << "The error means mysqld does not have" + " the access rights to the directory."; - retry = os_file_handle_error( - name, - create_mode == OS_FILE_OPEN - ? "open" : "create", __FILE__, __LINE__); } else { - *success = TRUE; - retry = false; + if (strerror(err) != NULL) { + + ib::error() + << "Error number " << err << " means '" + << strerror(err) << "'"; + } + + ib::info() << OPERATING_SYSTEM_ERROR_MSG; } + } - } while (retry); + switch (err) { + case ENOSPC: + return(OS_FILE_DISK_FULL); + case ENOENT: + return(OS_FILE_NOT_FOUND); + case EEXIST: + return(OS_FILE_ALREADY_EXISTS); + case EXDEV: + case ENOTDIR: + case EISDIR: + return(OS_FILE_PATH_ERROR); + case EAGAIN: + if (srv_use_native_aio) { + return(OS_FILE_AIO_RESOURCES_RESERVED); + } + break; + case EINTR: + if (srv_use_native_aio) { + return(OS_FILE_AIO_INTERRUPTED); + } + break; + case EACCES: + return(OS_FILE_ACCESS_VIOLATION); + } + return(OS_FILE_ERROR_MAX + err); +} -#ifdef USE_FILE_LOCK - if (!srv_read_only_mode - && *success - && (access_type == OS_FILE_READ_WRITE) - && os_file_lock(file, name)) { +/** Wrapper to fsync(2) that retries the call on some errors. +Returns the value 0 if successful; otherwise the value -1 is returned and +the global variable errno is set to indicate the error. +@param[in] file open file handle +@return 0 if success, -1 otherwise */ +static +int +os_file_fsync_posix( + os_file_t file) +{ + ulint failures = 0; - *success = FALSE; - close(file); - file = -1; + for (;;) { + + ++os_n_fsyncs; + + int ret = fsync(file); + + if (ret == 0) { + return(ret); + } + + switch(errno) { + case ENOLCK: + + ++failures; + ut_a(failures < 1000); + + if (!(failures % 100)) { + + ib::warn() + << "fsync(): " + << "No locks available; retrying"; + } + + /* 0.2 sec */ + os_thread_sleep(200000); + break; + + case EIO: + + ++failures; + ut_a(failures < 1000); + + if (!(failures % 100)) { + + ib::warn() + << "fsync(): " + << "An error occurred during " + << "synchronization," + << " retrying"; + } + + /* 0.2 sec */ + os_thread_sleep(200000); + break; + + case EINTR: + + ++failures; + ut_a(failures < 2000); + break; + + default: + ut_error; + break; + } } -#endif /* USE_FILE_LOCK */ -#endif /* __WIN__ */ + ut_error; - return(file); + return(-1); } -/****************************************************************//** -NOTE! Use the corresponding macro -os_file_create_simple_no_error_handling(), not directly this function! -A simple function to open or create a file. -@return own: handle to the file, not defined if error, error number -can be retrieved with os_file_get_last_error */ -UNIV_INTERN -os_file_t -os_file_create_simple_no_error_handling_func( -/*=========================================*/ - const char* name, /*!< in: name of the file or path as a - null-terminated string */ - ulint create_mode,/*!< in: create mode */ - ulint access_type,/*!< in: OS_FILE_READ_ONLY, - OS_FILE_READ_WRITE, or - OS_FILE_READ_ALLOW_DELETE; the last option is - used by a backup program reading the file */ - ibool* success,/*!< out: TRUE if succeed, FALSE if error */ - ulint atomic_writes) /*! in: atomic writes table option - value */ +/** Check the existence and type of the given file. +@param[in] path path name of file +@param[out] exists true if the file exists +@param[out] type Type of the file, if it exists +@return true if call succeeded */ +bool +os_file_status_posix( + const char* path, + bool* exists, + os_file_type_t* type) { - os_file_t file; - atomic_writes_t awrites = (atomic_writes_t) atomic_writes; + struct stat statinfo; - *success = FALSE; -#ifdef __WIN__ - DWORD access; - DWORD create_flag; - DWORD attributes = 0; - DWORD share_mode = FILE_SHARE_READ; + int ret = stat(path, &statinfo); - ut_a(name); + *exists = !ret; - ut_a(!(create_mode & OS_FILE_ON_ERROR_SILENT)); - ut_a(!(create_mode & OS_FILE_ON_ERROR_NO_EXIT)); + if (!ret) { + /* file exists, everything OK */ + + } else if (errno == ENOENT || errno == ENOTDIR || errno == ENAMETOOLONG) { + /* file does not exist */ + return(true); - if (create_mode == OS_FILE_OPEN) { - create_flag = OPEN_EXISTING; - } else if (srv_read_only_mode) { - create_flag = OPEN_EXISTING; - } else if (create_mode == OS_FILE_CREATE) { - create_flag = CREATE_NEW; } else { + /* file exists, but stat call failed */ + os_file_handle_error_no_exit(path, "stat", false); + return(false); + } + + if (S_ISDIR(statinfo.st_mode)) { + *type = OS_FILE_TYPE_DIR; - ib_logf(IB_LOG_LEVEL_ERROR, - "Unknown file create mode (%lu) for file '%s'", - create_mode, name); + } else if (S_ISLNK(statinfo.st_mode)) { + *type = OS_FILE_TYPE_LINK; - return((os_file_t) -1); + } else if (S_ISREG(statinfo.st_mode)) { + *type = OS_FILE_TYPE_FILE; + } else { + *type = OS_FILE_TYPE_UNKNOWN; } - if (access_type == OS_FILE_READ_ONLY) { - access = GENERIC_READ; - } else if (srv_read_only_mode) { - access = GENERIC_READ; - } else if (access_type == OS_FILE_READ_WRITE) { - access = GENERIC_READ | GENERIC_WRITE; - } else if (access_type == OS_FILE_READ_ALLOW_DELETE) { + return(true); +} - ut_a(!srv_read_only_mode); +/** NOTE! Use the corresponding macro os_file_flush(), not directly this +function! +Flushes the write buffers of a given file to the disk. +@param[in] file handle to a file +@return true if success */ +bool +os_file_flush_func( + os_file_t file) +{ + int ret; - access = GENERIC_READ; + ret = os_file_fsync_posix(file); - /*!< A backup program has to give mysqld the maximum - freedom to do what it likes with the file */ + if (ret == 0) { + return(true); + } - share_mode |= FILE_SHARE_DELETE | FILE_SHARE_WRITE; - } else { - ib_logf(IB_LOG_LEVEL_ERROR, - "Unknown file access type (%lu) for file '%s'", - access_type, name); + /* Since Linux returns EINVAL if the 'file' is actually a raw device, + we choose to ignore that error if we are using raw disks */ + + if (srv_start_raw_disk_in_use && errno == EINVAL) { - return((os_file_t) -1); + return(true); } - file = CreateFile((LPCTSTR) name, - access, - share_mode, - NULL, // Security attributes - create_flag, - attributes, - NULL); // No template file + ib::error() << "The OS said file flush did not succeed"; - /* If we have proper file handle and atomic writes should be used, - try to set atomic writes and if that fails when creating a new - table, produce a error. If atomic writes are used on existing - file, ignore error and use traditional writes for that file */ - if (file != INVALID_HANDLE_VALUE - && (awrites == ATOMIC_WRITES_ON || - (srv_use_atomic_writes && awrites == ATOMIC_WRITES_DEFAULT)) - && !os_file_set_atomic_writes(name, file)) { - if (create_mode == OS_FILE_CREATE) { - fprintf(stderr, "InnoDB: Error: Can't create file using atomic writes\n"); - CloseHandle(file); - os_file_delete_if_exists_func(name); - *success = FALSE; - file = INVALID_HANDLE_VALUE; - } - } + os_file_handle_error(NULL, "flush"); + + /* It is a fatal error if a file flush does not succeed, because then + the database can get corrupt on disk */ + ut_error; + + return(false); +} + +/** NOTE! Use the corresponding macro os_file_create_simple(), not directly +this function! +A simple function to open or create a file. +@param[in] name name of the file or path as a null-terminated + string +@param[in] create_mode create mode +@param[in] access_type OS_FILE_READ_ONLY or OS_FILE_READ_WRITE +@param[in] read_only if true, read only checks are enforced +@param[out] success true if succeed, false if error +@return handle to the file, not defined if error, error number + can be retrieved with os_file_get_last_error */ +os_file_t +os_file_create_simple_func( + const char* name, + ulint create_mode, + ulint access_type, + bool read_only, + bool* success) +{ + os_file_t file; + + *success = false; - *success = (file != INVALID_HANDLE_VALUE); -#else /* __WIN__ */ int create_flag; const char* mode_str = NULL; - ut_a(name); - if (create_mode != OS_FILE_OPEN && create_mode != OS_FILE_OPEN_RAW) - WAIT_ALLOW_WRITES(); - ut_a(!(create_mode & OS_FILE_ON_ERROR_SILENT)); ut_a(!(create_mode & OS_FILE_ON_ERROR_NO_EXIT)); if (create_mode == OS_FILE_OPEN) { - mode_str = "OPEN"; if (access_type == OS_FILE_READ_ONLY) { create_flag = O_RDONLY; - } else if (srv_read_only_mode) { + } else if (read_only) { create_flag = O_RDONLY; } else { - - ut_a(access_type == OS_FILE_READ_WRITE - || access_type == OS_FILE_READ_ALLOW_DELETE); - create_flag = O_RDWR; } - } else if (srv_read_only_mode) { + } else if (read_only) { mode_str = "OPEN"; - create_flag = O_RDONLY; } else if (create_mode == OS_FILE_CREATE) { 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 { - ib_logf(IB_LOG_LEVEL_ERROR, - "Unknown file create mode (%lu) for file '%s'", - create_mode, name); - return((os_file_t) -1); + ib::error() + << "Unknown file create mode (" + << create_mode + << " for file '" << name << "'"; + + return(OS_FILE_CLOSED); } - file = ::open(name, create_flag, os_innodb_umask); + bool retry; - *success = file == -1 ? FALSE : TRUE; + do { + file = ::open(name, create_flag, os_innodb_umask); + + if (file == -1) { + *success = false; + retry = os_file_handle_error( + name, + create_mode == OS_FILE_OPEN + ? "open" : "create"); + } else { + *success = true; + retry = false; + } + + } while (retry); /* 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 @@ -1636,15 +3178,14 @@ os_file_create_simple_no_error_handling_func( } #ifdef USE_FILE_LOCK - if (!srv_read_only_mode + if (!read_only && *success && (access_type == OS_FILE_READ_WRITE) && os_file_lock(file, name)) { - *success = FALSE; + *success = false; close(file); file = -1; - } #endif /* USE_FILE_LOCK */ @@ -1652,6 +3193,7 @@ os_file_create_simple_no_error_handling_func( try to set atomic writes and if that fails when creating a new table, produce a error. If atomic writes are used on existing file, ignore error and use traditional writes for that file */ + /* JAN: TODO: ATOMIC WRITES if (file != -1 && (awrites == ATOMIC_WRITES_ON || (srv_use_atomic_writes && awrites == ATOMIC_WRITES_DEFAULT)) @@ -1664,270 +3206,239 @@ os_file_create_simple_no_error_handling_func( file = -1; } } + */ -#endif /* __WIN__ */ - return(file); } -/****************************************************************//** -Tries to disable OS caching on an opened file descriptor. */ -UNIV_INTERN -void -os_file_set_nocache( -/*================*/ - os_file_t fd /*!< in: file descriptor to alter */ - __attribute__((unused)), - const char* file_name /*!< in: used in the diagnostic - message */ - MY_ATTRIBUTE((unused)), - const char* operation_name MY_ATTRIBUTE((unused))) - /*!< in: "open" or "create"; used - in the diagnostic message */ +/** This function attempts to create a directory named pathname. The new +directory gets default permissions. On Unix the permissions are +(0770 & ~umask). If the directory exists already, nothing is done and +the call succeeds, unless the fail_if_exists arguments is true. +If another error occurs, such as a permission error, this does not crash, +but reports the error and returns false. +@param[in] pathname directory name as null-terminated string +@param[in] fail_if_exists if true, pre-existing directory is treated as + an error. +@return true if call succeeds, false on error */ +bool +os_file_create_directory( + const char* pathname, + bool fail_if_exists) { - /* some versions of Solaris may not have DIRECTIO_ON */ -#if defined(UNIV_SOLARIS) && defined(DIRECTIO_ON) - if (directio(fd, DIRECTIO_ON) == -1) { - int errno_save = errno; + int rcode = mkdir(pathname, 0770); - ib_logf(IB_LOG_LEVEL_ERROR, - "Failed to set DIRECTIO_ON on file %s: %s: %s, " - "continuing anyway.", - file_name, operation_name, strerror(errno_save)); - } -#elif defined(O_DIRECT) - 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; -# ifdef UNIV_LINUX - ib_logf(IB_LOG_LEVEL_WARN, - "Failed to set O_DIRECT on file " - "%s: %s: %s, continuing anyway. " - "O_DIRECT is known to result " - "in 'Invalid argument' on Linux on " - "tmpfs, see MySQL Bug#26662.", - file_name, operation_name, - strerror(errno_save)); -# else /* UNIV_LINUX */ - goto short_warning; -# endif /* UNIV_LINUX */ - } - } else { -# ifndef UNIV_LINUX -short_warning: -# endif - ib_logf(IB_LOG_LEVEL_WARN, - "Failed to set O_DIRECT on file %s: %s: %s, " - "continuing anyway.", - file_name, operation_name, strerror(errno_save)); - } + if (!(rcode == 0 || (errno == EEXIST && !fail_if_exists))) { + /* failure */ + os_file_handle_error_no_exit(pathname, "mkdir", false); + + return(false); } -#endif /* defined(UNIV_SOLARIS) && defined(DIRECTIO_ON) */ + + return(true); } -/****************************************************************//** -NOTE! Use the corresponding macro os_file_create(), not directly -this function! -Opens an existing file or creates a new. -@return own: handle to the file, not defined if error, error number -can be retrieved with os_file_get_last_error */ -UNIV_INTERN -os_file_t -os_file_create_func( -/*================*/ - const char* name, /*!< in: name of the file or path as a - null-terminated string */ - ulint create_mode,/*!< in: create mode */ - ulint purpose,/*!< in: OS_FILE_AIO, if asynchronous, - non-buffered i/o is desired, - OS_FILE_NORMAL, if any normal file; - NOTE that it also depends on type, os_aio_.. - and srv_.. variables whether we really use - async i/o or unbuffered i/o: look in the - function source code for the exact rules */ - ulint type, /*!< in: OS_DATA_FILE or OS_LOG_FILE */ - ibool* success,/*!< out: TRUE if succeed, FALSE if error */ - ulint atomic_writes) /*! in: atomic writes table option - value */ +/** +The os_file_opendir() function opens a directory stream corresponding to the +directory named by the dirname argument. The directory stream is positioned +at the first entry. In both Unix and Windows we automatically skip the '.' +and '..' items at the start of the directory listing. +@param[in] dirname directory name; it must not contain a trailing + '\' or '/' +@param[in] is_fatal true if we should treat an error as a fatal + error; if we try to open symlinks then we do + not wish a fatal error if it happens not to be + a directory +@return directory stream, NULL if error */ +os_file_dir_t +os_file_opendir( + const char* dirname, + bool error_is_fatal) { - os_file_t file; - ibool retry; - ibool on_error_no_exit; - ibool on_error_silent; - atomic_writes_t awrites = (atomic_writes_t) atomic_writes; - -#ifdef __WIN__ - DBUG_EXECUTE_IF( - "ib_create_table_fail_disk_full", - *success = FALSE; - SetLastError(ERROR_DISK_FULL); - return((os_file_t) -1); - ); -#else /* __WIN__ */ - DBUG_EXECUTE_IF( - "ib_create_table_fail_disk_full", - *success = FALSE; - errno = ENOSPC; - return((os_file_t) -1); - ); -#endif /* __WIN__ */ + os_file_dir_t dir; + dir = opendir(dirname); -#ifdef __WIN__ - DWORD create_flag; - DWORD share_mode = FILE_SHARE_READ; + if (dir == NULL && error_is_fatal) { + os_file_handle_error(dirname, "opendir"); + } - on_error_no_exit = create_mode & OS_FILE_ON_ERROR_NO_EXIT - ? TRUE : FALSE; + return(dir); +} - on_error_silent = create_mode & OS_FILE_ON_ERROR_SILENT - ? TRUE : FALSE; +/** Closes a directory stream. +@param[in] dir directory stream +@return 0 if success, -1 if failure */ +int +os_file_closedir( + os_file_dir_t dir) +{ + int ret = closedir(dir); - create_mode &= ~OS_FILE_ON_ERROR_NO_EXIT; - create_mode &= ~OS_FILE_ON_ERROR_SILENT; + if (ret != 0) { + os_file_handle_error_no_exit(NULL, "closedir", false); + } - if (create_mode == OS_FILE_OPEN_RAW) { + return(ret); +} - ut_a(!srv_read_only_mode); +/** This function returns information of the next file in the directory. We jump +over the '.' and '..' entries in the directory. +@param[in] dirname directory name or path +@param[in] dir directory stream +@param[out] info buffer where the info is returned +@return 0 if ok, -1 if error, 1 if at the end of the directory */ +int +os_file_readdir_next_file( + const char* dirname, + os_file_dir_t dir, + os_file_stat_t* info) +{ + struct dirent* ent; + char* full_path; + int ret; + struct stat statinfo; - create_flag = OPEN_EXISTING; +#ifdef HAVE_READDIR_R + char dirent_buf[sizeof(struct dirent) + + _POSIX_PATH_MAX + 100]; + /* In /mysys/my_lib.c, _POSIX_PATH_MAX + 1 is used as + the max file name len; but in most standards, the + length is NAME_MAX; we add 100 to be even safer */ +#endif /* HAVE_READDIR_R */ - /* On Windows Physical devices require admin privileges and - have to have the write-share mode set. See the remarks - section for the CreateFile() function documentation in MSDN. */ +next_file: - share_mode |= FILE_SHARE_WRITE; +#ifdef HAVE_READDIR_R + ret = readdir_r(dir, (struct dirent*) dirent_buf, &ent); - } else if (create_mode == OS_FILE_OPEN - || create_mode == OS_FILE_OPEN_RETRY) { + if (ret != 0) { - create_flag = OPEN_EXISTING; + ib::error() + << "Cannot read directory " << dirname + << " error: " << ret; - } else if (srv_read_only_mode) { + return(-1); + } - create_flag = OPEN_EXISTING; + if (ent == NULL) { + /* End of directory */ - } else if (create_mode == OS_FILE_CREATE) { + return(1); + } - create_flag = CREATE_NEW; + ut_a(strlen(ent->d_name) < _POSIX_PATH_MAX + 100 - 1); +#else + ent = readdir(dir); - } else if (create_mode == OS_FILE_OVERWRITE) { + if (ent == NULL) { - create_flag = CREATE_ALWAYS; + return(1); + } +#endif /* HAVE_READDIR_R */ + ut_a(strlen(ent->d_name) < OS_FILE_MAX_PATH); - } else { - ib_logf(IB_LOG_LEVEL_ERROR, - "Unknown file create mode (%lu) for file '%s'", - create_mode, name); + if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) { - return((os_file_t) -1); + goto next_file; } - DWORD attributes = 0; + strcpy(info->name, ent->d_name); -#ifdef UNIV_HOTBACKUP - attributes |= FILE_FLAG_NO_BUFFERING; -#else - if (purpose == OS_FILE_AIO) { + full_path = static_cast<char*>( + ut_malloc_nokey(strlen(dirname) + strlen(ent->d_name) + 10)); -#ifdef WIN_ASYNC_IO - /* If specified, use asynchronous (overlapped) io and no - buffering of writes in the OS */ + sprintf(full_path, "%s/%s", dirname, ent->d_name); - if (srv_use_native_aio) { - attributes |= FILE_FLAG_OVERLAPPED; - } -#endif /* WIN_ASYNC_IO */ + ret = stat(full_path, &statinfo); - } else if (purpose == OS_FILE_NORMAL) { - /* Use default setting. */ - } else { - ib_logf(IB_LOG_LEVEL_ERROR, - "Unknown purpose flag (%lu) while opening file '%s'", - purpose, name); + if (ret) { - return((os_file_t)(-1)); - } + if (errno == ENOENT) { + /* readdir() returned a file that does not exist, + it must have been deleted in the meantime. Do what + would have happened if the file was deleted before + readdir() - ignore and go to the next entry. + If this is the last entry then info->name will still + contain the name of the deleted file when this + function returns, but this is not an issue since the + caller shouldn't be looking at info when end of + directory is returned. */ -#ifdef UNIV_NON_BUFFERED_IO - // TODO: Create a bug, this looks wrong. The flush log - // parameter is dynamic. - if (type == OS_LOG_FILE && srv_flush_log_at_trx_commit == 2) { + ut_free(full_path); - /* Do not use unbuffered i/o for the log files because - value 2 denotes that we do not flush the log at every - commit, but only once per second */ + goto next_file; + } - } else if (srv_win_file_flush_method == SRV_WIN_IO_UNBUFFERED) { + os_file_handle_error_no_exit(full_path, "stat", false); - attributes |= FILE_FLAG_NO_BUFFERING; + ut_free(full_path); + + return(-1); } -#endif /* UNIV_NON_BUFFERED_IO */ -#endif /* UNIV_HOTBACKUP */ - DWORD access = GENERIC_READ; + info->size = statinfo.st_size; - if (!srv_read_only_mode) { - access |= GENERIC_WRITE; + if (S_ISDIR(statinfo.st_mode)) { + info->type = OS_FILE_TYPE_DIR; + } else if (S_ISLNK(statinfo.st_mode)) { + info->type = OS_FILE_TYPE_LINK; + } else if (S_ISREG(statinfo.st_mode)) { + info->type = OS_FILE_TYPE_FILE; + } else { + info->type = OS_FILE_TYPE_UNKNOWN; } - do { - /* Use default security attributes and no template file. */ - file = CreateFile( - (LPCTSTR) name, access, share_mode, NULL, - create_flag, attributes, NULL); - - if (file == INVALID_HANDLE_VALUE) { - const char* operation; + ut_free(full_path); - operation = (create_mode == OS_FILE_CREATE - && !srv_read_only_mode) - ? "create" : "open"; + return(0); +} - *success = FALSE; +/** NOTE! Use the corresponding macro os_file_create(), not directly +this function! +Opens an existing file or creates a new. +@param[in] name name of the file or path as a null-terminated + string +@param[in] create_mode create mode +@param[in] purpose OS_FILE_AIO, if asynchronous, non-buffered I/O + is desired, OS_FILE_NORMAL, if any normal file; + NOTE that it also depends on type, os_aio_.. + and srv_.. variables whether we really use async + I/O or unbuffered I/O: look in the function + source code for the exact rules +@param[in] type OS_DATA_FILE or OS_LOG_FILE +@param[in] read_only true, if read only checks should be enforcedm +@param[in] success true if succeeded +@return handle to the file, not defined if error, error number + can be retrieved with os_file_get_last_error */ +os_file_t +os_file_create_func( + const char* name, + ulint create_mode, + ulint purpose, + ulint type, + bool read_only, + bool* success) +{ + bool on_error_no_exit; + bool on_error_silent; - if (on_error_no_exit) { - retry = os_file_handle_error_no_exit( - name, operation, on_error_silent, __FILE__, __LINE__); - } else { - retry = os_file_handle_error(name, operation, __FILE__, __LINE__); - } - } else { - *success = TRUE; - retry = FALSE; - } + *success = false; - } while (retry); + DBUG_EXECUTE_IF( + "ib_create_table_fail_disk_full", + *success = false; + errno = ENOSPC; + return(OS_FILE_CLOSED); + ); - /* If we have proper file handle and atomic writes should be used, - try to set atomic writes and if that fails when creating a new - table, produce a error. If atomic writes are used on existing - file, ignore error and use traditional writes for that file */ - if (file != INVALID_HANDLE_VALUE && type == OS_DATA_FILE - && (awrites == ATOMIC_WRITES_ON || - (srv_use_atomic_writes && awrites == ATOMIC_WRITES_DEFAULT)) - && !os_file_set_atomic_writes(name, file)) { - if (create_mode == OS_FILE_CREATE) { - fprintf(stderr, "InnoDB: Error: Can't create file using atomic writes\n"); - CloseHandle(file); - os_file_delete_if_exists_func(name); - *success = FALSE; - file = INVALID_HANDLE_VALUE; - } - } -#else /* __WIN__ */ int create_flag; const char* mode_str = NULL; - if (create_mode != OS_FILE_OPEN && create_mode != OS_FILE_OPEN_RAW) - WAIT_ALLOW_WRITES(); on_error_no_exit = create_mode & OS_FILE_ON_ERROR_NO_EXIT - ? TRUE : FALSE; + ? true : false; on_error_silent = create_mode & OS_FILE_ON_ERROR_SILENT - ? TRUE : FALSE; + ? true : false; create_mode &= ~OS_FILE_ON_ERROR_NO_EXIT; create_mode &= ~OS_FILE_ON_ERROR_SILENT; @@ -1938,9 +3449,9 @@ os_file_create_func( mode_str = "OPEN"; - create_flag = srv_read_only_mode ? O_RDONLY : O_RDWR; + create_flag = read_only ? O_RDONLY : O_RDWR; - } else if (srv_read_only_mode) { + } else if (read_only) { mode_str = "OPEN"; @@ -1957,14 +3468,17 @@ os_file_create_func( create_flag = O_RDWR | O_CREAT | O_TRUNC; } else { - ib_logf(IB_LOG_LEVEL_ERROR, - "Unknown file create mode (%lu) for file '%s'", - create_mode, name); + ib::error() + << "Unknown file create mode (" << create_mode << ")" + << " for file '" << name << "'"; - return((os_file_t) -1); + return(OS_FILE_CLOSED); } - ut_a(type == OS_LOG_FILE || type == OS_DATA_FILE); + ut_a(type == OS_LOG_FILE + || type == OS_DATA_FILE + || type == OS_DATA_TEMP_FILE); + ut_a(purpose == OS_FILE_AIO || purpose == OS_FILE_NORMAL); #ifdef O_SYNC @@ -1972,7 +3486,7 @@ os_file_create_func( O_SYNC because the datasync options seemed to corrupt files in 2001 in both Linux and Solaris */ - if (!srv_read_only_mode + if (!read_only && type == OS_LOG_FILE && srv_unix_file_flush_method == SRV_UNIX_O_DSYNC) { @@ -1980,6 +3494,9 @@ os_file_create_func( } #endif /* O_SYNC */ + os_file_t file; + bool retry; + do { file = ::open(name, create_flag, os_innodb_umask); @@ -1987,19 +3504,18 @@ os_file_create_func( const char* operation; operation = (create_mode == OS_FILE_CREATE - && !srv_read_only_mode) - ? "create" : "open"; + && !read_only) ? "create" : "open"; - *success = FALSE; + *success = false; if (on_error_no_exit) { retry = os_file_handle_error_no_exit( - name, operation, on_error_silent, __FILE__, __LINE__); + name, operation, on_error_silent); } else { - retry = os_file_handle_error(name, operation, __FILE__, __LINE__); + retry = os_file_handle_error(name, operation); } } else { - *success = TRUE; + *success = true; retry = false; } @@ -2016,32 +3532,30 @@ os_file_create_func( } #ifdef USE_FILE_LOCK - if (!srv_read_only_mode + if (!read_only && *success && create_mode != OS_FILE_OPEN_RAW && os_file_lock(file, name)) { if (create_mode == OS_FILE_OPEN_RETRY) { - ut_a(!srv_read_only_mode); - - ib_logf(IB_LOG_LEVEL_INFO, - "Retrying to lock the first data file"); + ib::info() + << "Retrying to lock the first data file"; for (int i = 0; i < 100; i++) { os_thread_sleep(1000000); if (!os_file_lock(file, name)) { - *success = TRUE; + *success = true; return(file); } } - ib_logf(IB_LOG_LEVEL_INFO, - "Unable to open the first data file"); + ib::info() + << "Unable to open the first data file"; } - *success = FALSE; + *success = false; close(file); file = -1; } @@ -2051,6 +3565,7 @@ os_file_create_func( try to set atomic writes and if that fails when creating a new table, produce a error. If atomic writes are used on existing file, ignore error and use traditional writes for that file */ + /* JAN: TODO: ATOMIC WRITES if (file != -1 && type == OS_DATA_FILE && (awrites == ATOMIC_WRITES_ON || (srv_use_atomic_writes && awrites == ATOMIC_WRITES_DEFAULT)) @@ -2063,155 +3578,155 @@ os_file_create_func( file = -1; } } -#endif /* __WIN__ */ - + */ return(file); } -/***********************************************************************//** -Deletes a file if it exists. The file has to be closed before calling this. -@return TRUE if success */ -UNIV_INTERN -bool -os_file_delete_if_exists_func( -/*==========================*/ - const char* name) /*!< in: file path as a null-terminated - string */ +/** NOTE! Use the corresponding macro +os_file_create_simple_no_error_handling(), not directly this function! +A simple function to open or create a file. +@param[in] name name of the file or path as a null-terminated + string +@param[in] create_mode create mode +@param[in] access_type OS_FILE_READ_ONLY, OS_FILE_READ_WRITE, or + OS_FILE_READ_ALLOW_DELETE; the last option + is used by a backup program reading the file +@param[in] read_only if true read only mode checks are enforced +@param[out] success true if succeeded +@return own: handle to the file, not defined if error, error number + can be retrieved with os_file_get_last_error */ +os_file_t +os_file_create_simple_no_error_handling_func( + const char* name, + ulint create_mode, + ulint access_type, + bool read_only, + bool* success) { -#ifdef __WIN__ - bool ret; - ulint count = 0; -loop: - /* In Windows, deleting an .ibd file may fail if mysqlbackup is copying - it */ - - ret = DeleteFile((LPCTSTR) name); + os_file_t file; + int create_flag; - if (ret) { - return(true); - } + ut_a(!(create_mode & OS_FILE_ON_ERROR_SILENT)); + ut_a(!(create_mode & OS_FILE_ON_ERROR_NO_EXIT)); - DWORD lasterr = GetLastError(); - if (lasterr == ERROR_FILE_NOT_FOUND - || lasterr == ERROR_PATH_NOT_FOUND) { - /* the file does not exist, this not an error */ + *success = false; - return(true); - } + if (create_mode == OS_FILE_OPEN) { - count++; + if (access_type == OS_FILE_READ_ONLY) { - if (count > 100 && 0 == (count % 10)) { - os_file_get_last_error(true); /* print error information */ + create_flag = O_RDONLY; - ib_logf(IB_LOG_LEVEL_WARN, "Delete of file %s failed.", name); - } + } else if (read_only) { - os_thread_sleep(500000); /* sleep for 0.5 second */ + create_flag = O_RDONLY; - if (count > 2000) { + } else { - return(false); - } + ut_a(access_type == OS_FILE_READ_WRITE + || access_type == OS_FILE_READ_ALLOW_DELETE); - goto loop; -#else - int ret; - WAIT_ALLOW_WRITES(); + create_flag = O_RDWR; + } - ret = unlink(name); + } else if (read_only) { - if (ret != 0 && errno != ENOENT) { - os_file_handle_error_no_exit(name, "delete", FALSE, __FILE__, __LINE__); + create_flag = O_RDONLY; - return(false); - } + } else if (create_mode == OS_FILE_CREATE) { - return(true); -#endif /* __WIN__ */ -} + create_flag = O_RDWR | O_CREAT | O_EXCL; -/***********************************************************************//** -Deletes a file. The file has to be closed before calling this. -@return TRUE if success */ -UNIV_INTERN -bool -os_file_delete_func( -/*================*/ - const char* name) /*!< in: file path as a null-terminated - string */ -{ -#ifdef __WIN__ - BOOL ret; - ulint count = 0; -loop: - /* In Windows, deleting an .ibd file may fail if mysqlbackup is copying - it */ + } else { - ret = DeleteFile((LPCTSTR) name); + ib::error() + << "Unknown file create mode " + << create_mode << " for file '" << name << "'"; - if (ret) { - return(true); + return(OS_FILE_CLOSED); } - if (GetLastError() == ERROR_FILE_NOT_FOUND) { - /* If the file does not exist, we classify this as a 'mild' - error and return */ + file = ::open(name, create_flag, os_innodb_umask); - return(false); - } + *success = (file != -1); + +#ifdef USE_FILE_LOCK + if (!read_only + && *success + && access_type == OS_FILE_READ_WRITE + && os_file_lock(file, name)) { - count++; + *success = false; + close(file); + file = -1; - if (count > 100 && 0 == (count % 10)) { - os_file_get_last_error(true); /* print error information */ + } +#endif /* USE_FILE_LOCK */ - fprintf(stderr, - "InnoDB: Warning: cannot delete file %s\n" - "InnoDB: Are you running mysqlbackup" - " to back up the file?\n", name); + return(file); +} + +/** Deletes a file if it exists. The file has to be closed before calling this. +@param[in] name file path as a null-terminated string +@param[out] exist indicate if file pre-exist +@return true if success */ +bool +os_file_delete_if_exists_func( + const char* name, + bool* exist) +{ + if (exist != NULL) { + *exist = true; } - os_thread_sleep(1000000); /* sleep for a second */ + int ret = unlink(name); - if (count > 2000) { + if (ret != 0 && errno == ENOENT) { + if (exist != NULL) { + *exist = false; + } + } else if (ret != 0 && errno != ENOENT) { + os_file_handle_error_no_exit(name, "delete", false); return(false); } - goto loop; -#else - int ret; - WAIT_ALLOW_WRITES(); + return(true); +} - ret = unlink(name); +/** Deletes a file. The file has to be closed before calling this. +@param[in] name file path as a null-terminated string +@return true if success */ +bool +os_file_delete_func( + const char* name) +{ + int ret = unlink(name); if (ret != 0) { - os_file_handle_error_no_exit(name, "delete", FALSE, __FILE__, __LINE__); + os_file_handle_error_no_exit(name, "delete", FALSE); return(false); } return(true); -#endif } -/***********************************************************************//** -NOTE! Use the corresponding macro os_file_rename(), not directly this function! +/** NOTE! Use the corresponding macro os_file_rename(), not directly this +function! Renames a file (can also move it to another directory). It is safest that the file is closed before calling this function. -@return TRUE if success */ -UNIV_INTERN -ibool +@param[in] oldpath old file path as a null-terminated string +@param[in] newpath new file path +@return true if success */ +bool os_file_rename_func( -/*================*/ - const char* oldpath,/*!< in: old file path as a null-terminated - string */ - const char* newpath)/*!< in: new file path */ + const char* oldpath, + const char* newpath) { #ifdef UNIV_DEBUG os_file_type_t type; - ibool exists; + bool exists; /* New path must not exist. */ ut_ad(os_file_status(newpath, &exists, &type)); @@ -2222,319 +3737,394 @@ os_file_rename_func( ut_ad(exists); #endif /* UNIV_DEBUG */ -#ifdef __WIN__ - BOOL ret; - - ret = MoveFile((LPCTSTR) oldpath, (LPCTSTR) newpath); - - if (ret) { - return(TRUE); - } - - os_file_handle_error_no_exit(oldpath, "rename", FALSE, __FILE__, __LINE__); - - return(FALSE); -#else - int ret; - WAIT_ALLOW_WRITES(); - - ret = rename(oldpath, newpath); + int ret = rename(oldpath, newpath); if (ret != 0) { - os_file_handle_error_no_exit(oldpath, "rename", FALSE, __FILE__, __LINE__); + os_file_handle_error_no_exit(oldpath, "rename", FALSE); - return(FALSE); + return(false); } - return(TRUE); -#endif /* __WIN__ */ + return(true); } -/***********************************************************************//** -NOTE! Use the corresponding macro os_file_close(), not directly this function! +/** NOTE! Use the corresponding macro os_file_close(), not directly this +function! Closes a file handle. In case of error, error number can be retrieved with os_file_get_last_error. -@return TRUE if success */ -UNIV_INTERN -ibool +@param[in] file Handle to close +@return true if success */ +bool os_file_close_func( -/*===============*/ - os_file_t file) /*!< in, own: handle to a file */ + os_file_t file) { -#ifdef __WIN__ - BOOL ret; + int ret = close(file); - ret = CloseHandle(file); + if (ret == -1) { + os_file_handle_error(NULL, "close"); - if (ret) { - return(TRUE); + return(false); } - os_file_handle_error(NULL, "close", __FILE__, __LINE__); + return(true); +} - return(FALSE); -#else - int ret; +/** Gets a file size. +@param[in] file handle to an open file +@return file size, or (os_offset_t) -1 on failure */ +os_offset_t +os_file_get_size( + os_file_t file) +{ + /* Store current position */ + os_offset_t pos = lseek(file, 0, SEEK_CUR); + os_offset_t file_size = lseek(file, 0, SEEK_END); - ret = close(file); + /* Restore current position as the function should not change it */ + lseek(file, pos, SEEK_SET); - if (ret == -1) { - os_file_handle_error(NULL, "close", __FILE__, __LINE__); + return(file_size); +} - return(FALSE); +/** Gets a file size. +@param[in] filename Full path to the filename to check +@return file size if OK, else set m_total_size to ~0 and m_alloc_size to + errno */ +os_file_size_t +os_file_get_size( + const char* filename) +{ + struct stat s; + os_file_size_t file_size; + + int ret = stat(filename, &s); + + if (ret == 0) { + file_size.m_total_size = s.st_size; + /* st_blocks is in 512 byte sized blocks */ + file_size.m_alloc_size = s.st_blocks * 512; + } else { + file_size.m_total_size = ~0; + file_size.m_alloc_size = (os_offset_t) errno; } - return(TRUE); -#endif /* __WIN__ */ + return(file_size); } -#ifdef UNIV_HOTBACKUP -/***********************************************************************//** -Closes a file handle. -@return TRUE if success */ -UNIV_INTERN -ibool -os_file_close_no_error_handling( -/*============================*/ - os_file_t file) /*!< in, own: handle to a file */ +/** This function returns information about the specified file +@param[in] path pathname of the file +@param[out] stat_info information of a file in a directory +@param[in,out] statinfo information of a file in a directory +@param[in] check_rw_perm for testing whether the file can be opened + in RW mode +@param[in] read_only if true read only mode checks are enforced +@return DB_SUCCESS if all OK */ +static +dberr_t +os_file_get_status_posix( + const char* path, + os_file_stat_t* stat_info, + struct stat* statinfo, + bool check_rw_perm, + bool read_only) { -#ifdef __WIN__ - BOOL ret; + int ret = stat(path, statinfo); + + if (ret && (errno == ENOENT || errno == ENOTDIR)) { + /* file does not exist */ - ret = CloseHandle(file); + return(DB_NOT_FOUND); - if (ret) { - return(TRUE); + } else if (ret) { + /* file exists, but stat call failed */ + + os_file_handle_error_no_exit(path, "stat", false); + + return(DB_FAIL); } - return(FALSE); -#else - int ret; + switch (statinfo->st_mode & S_IFMT) { + case S_IFDIR: + stat_info->type = OS_FILE_TYPE_DIR; + break; + case S_IFLNK: + stat_info->type = OS_FILE_TYPE_LINK; + break; + case S_IFBLK: + /* Handle block device as regular file. */ + case S_IFCHR: + /* Handle character device as regular file. */ + case S_IFREG: + stat_info->type = OS_FILE_TYPE_FILE; + break; + default: + stat_info->type = OS_FILE_TYPE_UNKNOWN; + } - ret = close(file); + stat_info->size = statinfo->st_size; + stat_info->block_size = statinfo->st_blksize; + stat_info->alloc_size = statinfo->st_blocks * 512; - if (ret == -1) { + if (check_rw_perm + && (stat_info->type == OS_FILE_TYPE_FILE + || stat_info->type == OS_FILE_TYPE_BLOCK)) { + + int access = !read_only ? O_RDWR : O_RDONLY; + int fh = ::open(path, access, os_innodb_umask); - return(FALSE); + if (fh == -1) { + stat_info->rw_perm = false; + } else { + stat_info->rw_perm = true; + close(fh); + } } - return(TRUE); -#endif /* __WIN__ */ + return(DB_SUCCESS); } -#endif /* UNIV_HOTBACKUP */ -/***********************************************************************//** -Gets a file size. -@return file size, or (os_offset_t) -1 on failure */ -UNIV_INTERN -os_offset_t -os_file_get_size( -/*=============*/ - os_file_t file) /*!< in: handle to a file */ +/** Truncates a file to a specified size in bytes. +Do nothing if the size to preserve is greater or equal to the current +size of the file. +@param[in] pathname file path +@param[in] file file to be truncated +@param[in] size size to preserve in bytes +@return true if success */ +static +bool +os_file_truncate_posix( + const char* pathname, + os_file_t file, + os_offset_t size) { -#ifdef __WIN__ - os_offset_t offset; - DWORD high; - DWORD low; + int res = ftruncate(file, size); - low = GetFileSize(file, &high); + if (res == -1) { - if ((low == 0xFFFFFFFF) && (GetLastError() != NO_ERROR)) { - return((os_offset_t) -1); - } + bool retry; - offset = (os_offset_t) low | ((os_offset_t) high << 32); + retry = os_file_handle_error_no_exit( + pathname, "truncate", false); - return(offset); -#else - return((os_offset_t) lseek(file, 0, SEEK_END)); -#endif /* __WIN__ */ + if (retry) { + ib::warn() + << "Truncate failed for '" + << pathname << "'"; + } + } + + return(res == 0); } -/***********************************************************************//** -Write the specified number of zeros to a newly created file. -@return TRUE if success */ -UNIV_INTERN -ibool -os_file_set_size( -/*=============*/ - const char* name, /*!< in: name of the file or path as a - null-terminated string */ - os_file_t file, /*!< in: handle to a file */ - os_offset_t size) /*!< in: file size */ +/** Truncates a file at its current position. +@return true if success */ +bool +os_file_set_eof( + FILE* file) /*!< in: file to be truncated */ { - os_offset_t current_size; - ibool ret; - byte* buf; - byte* buf2; - ulint buf_size; + return(!ftruncate(fileno(file), ftell(file))); +} - current_size = 0; +#ifdef UNIV_HOTBACKUP +/** Closes a file handle. +@param[in] file Handle to a file +@return true if success */ +bool +os_file_close_no_error_handling( + os_file_t file) +{ + return(close(file) != -1); +} +#endif /* UNIV_HOTBACKUP */ -#ifdef HAVE_POSIX_FALLOCATE - if (srv_use_posix_fallocate) { +/** This function can be called if one wants to post a batch of reads and +prefers an i/o-handler thread to handle them all at once later. You must +call os_aio_simulated_wake_handler_threads later to ensure the threads +are not left sleeping! */ +void +os_aio_simulated_put_read_threads_to_sleep() +{ + /* No op on non Windows */ +} - if (posix_fallocate(file, current_size, size) == -1) { +#else /* !_WIN32 */ - fprintf(stderr, "InnoDB: Error: preallocating file " - "space for file \'%s\' failed. Current size " - "%lu, desired size %lu\n", - name, (ulong) current_size, (ulong) size); - os_file_handle_error_no_exit(name, "posix_fallocate", FALSE, __FILE__, __LINE__); +#include <WinIoCtl.h> - return(FALSE); - } - return(TRUE); - } -#endif +/** Do the read/write +@param[in] request The IO context and type +@return the number of bytes read/written or negative value on error */ +ssize_t +SyncFileIO::execute(const IORequest& request) +{ + OVERLAPPED seek; - /* Write up to 1 megabyte at a time. */ - buf_size = ut_min(64, (ulint) (size / UNIV_PAGE_SIZE)) - * UNIV_PAGE_SIZE; - buf2 = static_cast<byte*>(ut_malloc(buf_size + UNIV_PAGE_SIZE)); + memset(&seek, 0x0, sizeof(seek)); - /* Align the buffer for possible raw i/o */ - buf = static_cast<byte*>(ut_align(buf2, UNIV_PAGE_SIZE)); + seek.Offset = (DWORD) m_offset & 0xFFFFFFFF; + seek.OffsetHigh = (DWORD) (m_offset >> 32); - /* Write buffer full of zeros */ - memset(buf, 0, buf_size); + BOOL ret; + DWORD n_bytes; - if (size >= (os_offset_t) 100 << 20) { + if (request.is_read()) { + ret = ReadFile(m_fh, m_buf, + static_cast<DWORD>(m_n), &n_bytes, &seek); - fprintf(stderr, "InnoDB: Progress in MB:"); + } else { + ut_ad(request.is_write()); + ret = WriteFile(m_fh, m_buf, + static_cast<DWORD>(m_n), &n_bytes, &seek); } - while (current_size < size) { - ulint n_bytes; + return(ret ? static_cast<ssize_t>(n_bytes) : -1); +} - if (size - current_size < (os_offset_t) buf_size) { - n_bytes = (ulint) (size - current_size); - } else { - n_bytes = buf_size; - } +/** Do the read/write +@param[in,out] slot The IO slot, it has the IO context +@return the number of bytes read/written or negative value on error */ +ssize_t +SyncFileIO::execute(Slot* slot) +{ + BOOL ret; - ret = os_file_write(name, file, buf, current_size, n_bytes); + if (slot->type.is_read()) { - if (!ret) { - ut_free(buf2); - goto error_handling; - } + ret = ReadFile( + slot->file, slot->ptr, slot->len, + &slot->n_bytes, &slot->control); - /* Print about progress for each 100 MB written */ - if ((current_size + n_bytes) / (100 << 20) - != current_size / (100 << 20)) { + } else { + ut_ad(slot->type.is_write()); - fprintf(stderr, " %lu00", - (ulong) ((current_size + n_bytes) - / (100 << 20))); - } + ret = WriteFile( + slot->file, slot->ptr, slot->len, + &slot->n_bytes, &slot->control); - current_size += n_bytes; } - if (size >= (os_offset_t) 100 << 20) { + return(ret ? slot->n_bytes : -1); +} - fprintf(stderr, "\n"); - } +/** Check if the file system supports sparse files. +@param[in] name File name +@return true if the file system supports sparse files */ +static +bool +os_is_sparse_file_supported_win32(const char* filename) +{ + char volname[MAX_PATH]; + BOOL result = GetVolumePathName(filename, volname, MAX_PATH); - ut_free(buf2); + if (!result) { - ret = os_file_flush(file); + ib::error() + << "os_is_sparse_file_supported: " + << "Failed to get the volume path name for: " + << filename + << "- OS error number " << GetLastError(); - if (ret) { - return(TRUE); + return(false); } -error_handling: - return(FALSE); -} + DWORD flags; -/***********************************************************************//** -Truncates a file at its current position. -@return TRUE if success */ -UNIV_INTERN -ibool -os_file_set_eof( -/*============*/ - FILE* file) /*!< in: file to be truncated */ -{ -#ifdef __WIN__ - HANDLE h = (HANDLE) _get_osfhandle(fileno(file)); - return(SetEndOfFile(h)); -#else /* __WIN__ */ - WAIT_ALLOW_WRITES(); - return(!ftruncate(fileno(file), ftell(file))); -#endif /* __WIN__ */ -} + GetVolumeInformation( + volname, NULL, MAX_PATH, NULL, NULL, + &flags, NULL, MAX_PATH); -#ifndef __WIN__ -/***********************************************************************//** -Wrapper to fsync(2) that retries the call on some errors. -Returns the value 0 if successful; otherwise the value -1 is returned and -the global variable errno is set to indicate the error. -@return 0 if success, -1 otherwise */ + return(flags & FILE_SUPPORTS_SPARSE_FILES) ? true : false; +} +/** Free storage space associated with a section of the file. +@param[in] fh Open file handle +@param[in] page_size Tablespace page size +@param[in] block_size File system block size +@param[in] off Starting offset (SEEK_SET) +@param[in] len Size of the hole +@return 0 on success or errno */ static -int -os_file_fsync( -/*==========*/ - os_file_t file) /*!< in: handle to a file */ +dberr_t +os_file_punch_hole_win32( + os_file_t fh, + os_offset_t off, + os_offset_t len) { - int ret; - int failures; - ibool retry; + FILE_ZERO_DATA_INFORMATION punch; - failures = 0; + punch.FileOffset.QuadPart = off; + punch.BeyondFinalZero.QuadPart = off + len; - do { - ret = fsync(file); + /* If lpOverlapped is NULL, lpBytesReturned cannot be NULL, + therefore we pass a dummy parameter. */ + DWORD temp; - os_n_fsyncs++; + BOOL result = DeviceIoControl( + fh, FSCTL_SET_ZERO_DATA, &punch, sizeof(punch), + NULL, 0, &temp, NULL); - if (ret == -1 && errno == ENOLCK) { + return(!result ? DB_IO_NO_PUNCH_HOLE : DB_SUCCESS); +} - if (failures % 100 == 0) { +/** Check the existence and type of the given file. +@param[in] path path name of file +@param[out] exists true if the file exists +@param[out] type Type of the file, if it exists +@return true if call succeeded */ +bool +os_file_status_win32( + const char* path, + bool* exists, + os_file_type_t* type) +{ + int ret; + struct _stat64 statinfo; - ut_print_timestamp(stderr); - fprintf(stderr, - " InnoDB: fsync(): " - "No locks available; retrying\n"); - } + ret = _stat64(path, &statinfo); + + *exists = !ret; - os_thread_sleep(200000 /* 0.2 sec */); + if (!ret) { + /* file exists, everything OK */ - failures++; + } else if (errno == ENOENT || errno == ENOTDIR || errno == ENAMETOOLONG) { + /* file does not exist */ + return(true); - retry = TRUE; - } else { + } else { + /* file exists, but stat call failed */ + os_file_handle_error_no_exit(path, "stat", false); + return(false); + } - retry = FALSE; - } - } while (retry); + if (_S_IFDIR & statinfo.st_mode) { + *type = OS_FILE_TYPE_DIR; - return(ret); + } else if (_S_IFREG & statinfo.st_mode) { + *type = OS_FILE_TYPE_FILE; + + } else { + *type = OS_FILE_TYPE_UNKNOWN; + } + + return(true); } -#endif /* !__WIN__ */ -/***********************************************************************//** -NOTE! Use the corresponding macro os_file_flush(), not directly this function! +/** NOTE! Use the corresponding macro os_file_flush(), not directly this +function! Flushes the write buffers of a given file to the disk. -@return TRUE if success */ -UNIV_INTERN -ibool +@param[in] file handle to a file +@return true if success */ +bool os_file_flush_func( -/*===============*/ - os_file_t file) /*!< in, own: handle to a file */ + os_file_t file) { -#ifdef __WIN__ - BOOL ret; - - os_n_fsyncs++; + ++os_n_fsyncs; - ret = FlushFileBuffers(file); + BOOL ret = FlushFileBuffers(file); if (ret) { - return(TRUE); + return(true); } /* Since Windows returns ERROR_INVALID_FUNCTION if the 'file' is @@ -2543,874 +4133,957 @@ os_file_flush_func( if (srv_start_raw_disk_in_use && GetLastError() == ERROR_INVALID_FUNCTION) { - return(TRUE); + return(true); } - os_file_handle_error(NULL, "flush", __FILE__, __LINE__); + os_file_handle_error(NULL, "flush"); /* It is a fatal error if a file flush does not succeed, because then the database can get corrupt on disk */ ut_error; - return(FALSE); -#else - int ret; - WAIT_ALLOW_WRITES(); - -#if defined(HAVE_DARWIN_THREADS) -# ifndef F_FULLFSYNC - /* The following definition is from the Mac OS X 10.3 <sys/fcntl.h> */ -# define F_FULLFSYNC 51 /* fsync + ask the drive to flush to the media */ -# elif F_FULLFSYNC != 51 -# error "F_FULLFSYNC != 51: ABI incompatibility with Mac OS X 10.3" -# endif - /* Apple has disabled fsync() for internal disk drives in OS X. That - caused corruption for a user when he tested a power outage. Let us in - OS X use a nonstandard flush method recommended by an Apple - engineer. */ + return(false); +} - if (!srv_have_fullfsync) { - /* If we are not on an operating system that supports this, - then fall back to a plain fsync. */ +/** Retrieves the last error number if an error occurs in a file io function. +The number should be retrieved before any other OS calls (because they may +overwrite the error number). If the number is not known to this program, +the OS error number + 100 is returned. +@param[in] report_all_errors true if we want an error message printed + of all errors +@param[in] on_error_silent true then don't print any diagnostic + to the log +@return error number, or OS error number + 100 */ +static +ulint +os_file_get_last_error_low( + bool report_all_errors, + bool on_error_silent) +{ + ulint err = (ulint) GetLastError(); - ret = os_file_fsync(file); - } else { - ret = fcntl(file, F_FULLFSYNC, NULL); + if (err == ERROR_SUCCESS) { + return(0); + } - if (ret) { - /* If we are not on a file system that supports this, - then fall back to a plain fsync. */ - ret = os_file_fsync(file); + if (report_all_errors + || (!on_error_silent + && err != ERROR_DISK_FULL + && err != ERROR_FILE_EXISTS)) { + + ib::error() + << "Operating system error number " << err + << " in a file operation."; + + if (err == ERROR_PATH_NOT_FOUND) { + ib::error() + << "The error means the system" + " cannot find the path specified."; + + if (srv_is_being_started) { + ib::error() + << "If you are installing InnoDB," + " remember that you must create" + " directories yourself, InnoDB" + " does not create them."; + } + + } else if (err == ERROR_ACCESS_DENIED) { + + ib::error() + << "The error means mysqld does not have" + " the access rights to" + " the directory. It may also be" + " you have created a subdirectory" + " of the same name as a data file."; + + } else if (err == ERROR_SHARING_VIOLATION + || err == ERROR_LOCK_VIOLATION) { + + ib::error() + << "The error means that another program" + " is using InnoDB's files." + " This might be a backup or antivirus" + " software or another instance" + " of MySQL." + " Please close it to get rid of this error."; + + } else if (err == ERROR_WORKING_SET_QUOTA + || err == ERROR_NO_SYSTEM_RESOURCES) { + + ib::error() + << "The error means that there are no" + " sufficient system resources or quota to" + " complete the operation."; + + } else if (err == ERROR_OPERATION_ABORTED) { + + ib::error() + << "The error means that the I/O" + " operation has been aborted" + " because of either a thread exit" + " or an application request." + " Retry attempt is made."; + } else { + + ib::info() << OPERATING_SYSTEM_ERROR_MSG; } } -#else - ret = os_file_fsync(file); -#endif - if (ret == 0) { - return(TRUE); + if (err == ERROR_FILE_NOT_FOUND) { + return(OS_FILE_NOT_FOUND); + } else if (err == ERROR_DISK_FULL) { + return(OS_FILE_DISK_FULL); + } else if (err == ERROR_FILE_EXISTS) { + return(OS_FILE_ALREADY_EXISTS); + } else if (err == ERROR_SHARING_VIOLATION + || err == ERROR_LOCK_VIOLATION) { + return(OS_FILE_SHARING_VIOLATION); + } else if (err == ERROR_WORKING_SET_QUOTA + || err == ERROR_NO_SYSTEM_RESOURCES) { + return(OS_FILE_INSUFFICIENT_RESOURCE); + } else if (err == ERROR_OPERATION_ABORTED) { + return(OS_FILE_OPERATION_ABORTED); + } else if (err == ERROR_ACCESS_DENIED) { + return(OS_FILE_ACCESS_VIOLATION); } - /* Since Linux returns EINVAL if the 'file' is actually a raw device, - we choose to ignore that error if we are using raw disks */ + return(OS_FILE_ERROR_MAX + err); +} - if (srv_start_raw_disk_in_use && errno == EINVAL) { +/** NOTE! Use the corresponding macro os_file_create_simple(), not directly +this function! +A simple function to open or create a file. +@param[in] name name of the file or path as a null-terminated + string +@param[in] create_mode create mode +@param[in] access_type OS_FILE_READ_ONLY or OS_FILE_READ_WRITE +@param[in] read_only if true read only mode checks are enforced +@param[out] success true if succeed, false if error +@return handle to the file, not defined if error, error number + can be retrieved with os_file_get_last_error */ +os_file_t +os_file_create_simple_func( + const char* name, + ulint create_mode, + ulint access_type, + bool read_only, + bool* success) +{ + os_file_t file; - return(TRUE); - } + *success = false; - ib_logf(IB_LOG_LEVEL_ERROR, "The OS said file flush did not succeed"); + DWORD access; + DWORD create_flag; + DWORD attributes = 0; - os_file_handle_error(NULL, "flush", __FILE__, __LINE__); + ut_a(!(create_mode & OS_FILE_ON_ERROR_SILENT)); + ut_a(!(create_mode & OS_FILE_ON_ERROR_NO_EXIT)); - /* It is a fatal error if a file flush does not succeed, because then - the database can get corrupt on disk */ - ut_error; + if (create_mode == OS_FILE_OPEN) { - return(FALSE); -#endif -} + create_flag = OPEN_EXISTING; -#ifndef __WIN__ -/*******************************************************************//** -Does a synchronous read operation in Posix. -@return number of bytes read, -1 if error */ -static MY_ATTRIBUTE((nonnull, warn_unused_result)) -ssize_t -os_file_pread( -/*==========*/ - os_file_t file, /*!< in: handle to a file */ - void* buf, /*!< in: buffer where to read */ - ulint n, /*!< in: number of bytes to read */ - os_offset_t offset) /*!< in: file offset from where to read */ -{ - off_t offs; -#if defined(HAVE_PREAD) && !defined(HAVE_BROKEN_PREAD) - ssize_t n_bytes; -#endif /* HAVE_PREAD && !HAVE_BROKEN_PREAD */ + } else if (read_only) { - ut_ad(n); + create_flag = OPEN_EXISTING; - /* If off_t is > 4 bytes in size, then we assume we can pass a - 64-bit address */ - offs = (off_t) offset; + } else if (create_mode == OS_FILE_CREATE) { - if (sizeof(off_t) <= 4) { - if (offset != (os_offset_t) offs) { - ib_logf(IB_LOG_LEVEL_ERROR, - "File read at offset > 4 GB"); + 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 { + + ib::error() + << "Unknown file create mode (" + << create_mode << ") for file '" + << name << "'"; + + return(OS_FILE_CLOSED); } - os_n_file_reads++; + if (access_type == OS_FILE_READ_ONLY) { -#if defined(HAVE_PREAD) && !defined(HAVE_BROKEN_PREAD) -#if defined(HAVE_ATOMIC_BUILTINS) && UNIV_WORD_SIZE == 8 - (void) os_atomic_increment_ulint(&os_n_pending_reads, 1); - (void) os_atomic_increment_ulint(&os_file_n_pending_preads, 1); - MONITOR_ATOMIC_INC(MONITOR_OS_PENDING_READS); -#else - os_mutex_enter(os_file_count_mutex); - os_file_n_pending_preads++; - os_n_pending_reads++; - MONITOR_INC(MONITOR_OS_PENDING_READS); - os_mutex_exit(os_file_count_mutex); -#endif /* HAVE_ATOMIC_BUILTINS && UNIV_WORD == 8 */ + access = GENERIC_READ; - n_bytes = pread(file, buf, n, offs); + } else if (read_only) { -#if defined(HAVE_ATOMIC_BUILTINS) && UNIV_WORD_SIZE == 8 - (void) os_atomic_decrement_ulint(&os_n_pending_reads, 1); - (void) os_atomic_decrement_ulint(&os_file_n_pending_preads, 1); - MONITOR_ATOMIC_DEC(MONITOR_OS_PENDING_READS); -#else - os_mutex_enter(os_file_count_mutex); - os_file_n_pending_preads--; - os_n_pending_reads--; - MONITOR_DEC(MONITOR_OS_PENDING_READS); - os_mutex_exit(os_file_count_mutex); -#endif /* !HAVE_ATOMIC_BUILTINS || UNIV_WORD == 8 */ + ib::info() + << "Read only mode set. Unable to" + " open file '" << name << "' in RW mode, " + << "trying RO mode", name; - return(n_bytes); -#else - { - off_t ret_offset; - ssize_t ret; -#ifndef UNIV_HOTBACKUP - ulint i; -#endif /* !UNIV_HOTBACKUP */ + access = GENERIC_READ; -#if defined(HAVE_ATOMIC_BUILTINS) && UNIV_WORD_SIZE == 8 - (void) os_atomic_increment_ulint(&os_n_pending_reads, 1); - MONITOR_ATOMIC_INC(MONITOR_OS_PENDING_READS); -#else - os_mutex_enter(os_file_count_mutex); - os_n_pending_reads++; - MONITOR_INC(MONITOR_OS_PENDING_READS); - os_mutex_exit(os_file_count_mutex); -#endif /* HAVE_ATOMIC_BUILTINS && UNIV_WORD == 8 */ -#ifndef UNIV_HOTBACKUP - /* Protect the seek / read operation with a mutex */ - i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES; + } else if (access_type == OS_FILE_READ_WRITE) { - os_mutex_enter(os_file_seek_mutexes[i]); -#endif /* !UNIV_HOTBACKUP */ + access = GENERIC_READ | GENERIC_WRITE; + + } else { + + ib::error() + << "Unknown file access type (" << access_type << ") " + "for file '" << name << "'"; + + return(OS_FILE_CLOSED); + } + + bool retry; + + do { + /* Use default security attributes and no template file. */ + + file = CreateFile( + (LPCTSTR) name, access, FILE_SHARE_READ, NULL, + create_flag, attributes, NULL); + + if (file == INVALID_HANDLE_VALUE) { + + *success = false; - ret_offset = lseek(file, offs, SEEK_SET); + retry = os_file_handle_error( + name, create_mode == OS_FILE_OPEN ? + "open" : "create"); - if (ret_offset < 0) { - ret = -1; } else { - ret = read(file, buf, (ssize_t) n); - } -#ifndef UNIV_HOTBACKUP - os_mutex_exit(os_file_seek_mutexes[i]); -#endif /* !UNIV_HOTBACKUP */ + retry = false; -#if defined(HAVE_ATOMIC_BUILTINS) && UNIV_WORD_SIZE == 8 - (void) os_atomic_decrement_ulint(&os_n_pending_reads, 1); - MONITOR_ATOIC_DEC(MONITOR_OS_PENDING_READS); -#else - os_mutex_enter(os_file_count_mutex); - os_n_pending_reads--; - MONITOR_DEC(MONITOR_OS_PENDING_READS); - os_mutex_exit(os_file_count_mutex); -#endif /* HAVE_ATOMIC_BUILTINS && UNIV_WORD_SIZE == 8 */ + *success = true; - return(ret); - } -#endif + DWORD temp; + + /* This is a best effort use case, if it fails then + we will find out when we try and punch the hole. */ + + DeviceIoControl( + file, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, + &temp, NULL); + } + + } while (retry); + + return(file); } -/*******************************************************************//** -Does a synchronous write operation in Posix. -@return number of bytes written, -1 if error */ -static MY_ATTRIBUTE((nonnull, warn_unused_result)) -ssize_t -os_file_pwrite( -/*===========*/ - os_file_t file, /*!< in: handle to a file */ - const void* buf, /*!< in: buffer from where to write */ - ulint n, /*!< in: number of bytes to write */ - os_offset_t offset) /*!< in: file offset where to write */ +/** This function attempts to create a directory named pathname. The new +directory gets default permissions. On Unix the permissions are +(0770 & ~umask). If the directory exists already, nothing is done and +the call succeeds, unless the fail_if_exists arguments is true. +If another error occurs, such as a permission error, this does not crash, +but reports the error and returns false. +@param[in] pathname directory name as null-terminated string +@param[in] fail_if_exists if true, pre-existing directory is treated + as an error. +@return true if call succeeds, false on error */ +bool +os_file_create_directory( + const char* pathname, + bool fail_if_exists) { - ssize_t ret; - off_t offs; + BOOL rcode; - ut_ad(n); - ut_ad(!srv_read_only_mode); + rcode = CreateDirectory((LPCTSTR) pathname, NULL); + if (!(rcode != 0 + || (GetLastError() == ERROR_ALREADY_EXISTS + && !fail_if_exists))) { - /* If off_t is > 4 bytes in size, then we assume we can pass a - 64-bit address */ - offs = (off_t) offset; + os_file_handle_error_no_exit( + pathname, "CreateDirectory", false); - if (sizeof(off_t) <= 4) { - if (offset != (os_offset_t) offs) { - ib_logf(IB_LOG_LEVEL_ERROR, - "File write at offset > 4 GB."); - } + return(false); } - os_n_file_writes++; - -#if defined(HAVE_PWRITE) && !defined(HAVE_BROKEN_PREAD) -#if !defined(HAVE_ATOMIC_BUILTINS) || UNIV_WORD_SIZE < 8 - os_mutex_enter(os_file_count_mutex); - os_file_n_pending_pwrites++; - os_n_pending_writes++; - MONITOR_INC(MONITOR_OS_PENDING_WRITES); - os_mutex_exit(os_file_count_mutex); -#else - (void) os_atomic_increment_ulint(&os_n_pending_writes, 1); - (void) os_atomic_increment_ulint(&os_file_n_pending_pwrites, 1); - MONITOR_ATOMIC_INC(MONITOR_OS_PENDING_WRITES); -#endif /* !HAVE_ATOMIC_BUILTINS || UNIV_WORD < 8 */ + return(true); +} - ret = pwrite(file, buf, (ssize_t) n, offs); +/** The os_file_opendir() function opens a directory stream corresponding to the +directory named by the dirname argument. The directory stream is positioned +at the first entry. In both Unix and Windows we automatically skip the '.' +and '..' items at the start of the directory listing. +@param[in] dirname directory name; it must not contain a trailing + '\' or '/' +@param[in] is_fatal true if we should treat an error as a fatal + error; if we try to open symlinks then we do + not wish a fatal error if it happens not to + be a directory +@return directory stream, NULL if error */ +os_file_dir_t +os_file_opendir( + const char* dirname, + bool error_is_fatal) +{ + os_file_dir_t dir; + LPWIN32_FIND_DATA lpFindFileData; + char path[OS_FILE_MAX_PATH + 3]; -#if !defined(HAVE_ATOMIC_BUILTINS) || UNIV_WORD_SIZE < 8 - os_mutex_enter(os_file_count_mutex); - os_file_n_pending_pwrites--; - os_n_pending_writes--; - MONITOR_DEC(MONITOR_OS_PENDING_WRITES); - os_mutex_exit(os_file_count_mutex); -#else - (void) os_atomic_decrement_ulint(&os_n_pending_writes, 1); - (void) os_atomic_decrement_ulint(&os_file_n_pending_pwrites, 1); - MONITOR_ATOMIC_DEC(MONITOR_OS_PENDING_WRITES); -#endif /* !HAVE_ATOMIC_BUILTINS || UNIV_WORD < 8 */ + ut_a(strlen(dirname) < OS_FILE_MAX_PATH); - return(ret); -#else - { - off_t ret_offset; -# ifndef UNIV_HOTBACKUP - ulint i; -# endif /* !UNIV_HOTBACKUP */ + strcpy(path, dirname); + strcpy(path + strlen(path), "\\*"); - os_mutex_enter(os_file_count_mutex); - os_n_pending_writes++; - MONITOR_INC(MONITOR_OS_PENDING_WRITES); - os_mutex_exit(os_file_count_mutex); + /* Note that in Windows opening the 'directory stream' also retrieves + the first entry in the directory. Since it is '.', that is no problem, + as we will skip over the '.' and '..' entries anyway. */ -# ifndef UNIV_HOTBACKUP - /* Protect the seek / write operation with a mutex */ - i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES; + lpFindFileData = static_cast<LPWIN32_FIND_DATA>( + ut_malloc_nokey(sizeof(WIN32_FIND_DATA))); - os_mutex_enter(os_file_seek_mutexes[i]); -# endif /* UNIV_HOTBACKUP */ + dir = FindFirstFile((LPCTSTR) path, lpFindFileData); - ret_offset = lseek(file, offs, SEEK_SET); + ut_free(lpFindFileData); - if (ret_offset < 0) { - ret = -1; + if (dir == INVALID_HANDLE_VALUE) { - goto func_exit; + if (error_is_fatal) { + os_file_handle_error(dirname, "opendir"); } - ret = write(file, buf, (ssize_t) n); + return(NULL); + } -func_exit: -# ifndef UNIV_HOTBACKUP - os_mutex_exit(os_file_seek_mutexes[i]); -# endif /* !UNIV_HOTBACKUP */ + return(dir); +} - os_mutex_enter(os_file_count_mutex); - os_n_pending_writes--; - MONITOR_DEC(MONITOR_OS_PENDING_WRITES); - os_mutex_exit(os_file_count_mutex); +/** Closes a directory stream. +@param[in] dir directory stream +@return 0 if success, -1 if failure */ +int +os_file_closedir( + os_file_dir_t dir) +{ + BOOL ret; + + ret = FindClose(dir); - return(ret); + if (!ret) { + os_file_handle_error_no_exit(NULL, "closedir", false); + + return(-1); } -#endif /* !UNIV_HOTBACKUP */ + + return(0); } -#endif -/*******************************************************************//** -NOTE! Use the corresponding macro os_file_read(), not directly this -function! -Requests a synchronous positioned read operation. -@return TRUE if request was successful, FALSE if fail */ -UNIV_INTERN -ibool -os_file_read_func( -/*==============*/ - os_file_t file, /*!< in: handle to a file */ - void* buf, /*!< in: buffer where to read */ - os_offset_t offset, /*!< in: file offset where to read */ - ulint n) /*!< in: number of bytes to read */ +/** This function returns information of the next file in the directory. We +jump over the '.' and '..' entries in the directory. +@param[in] dirname directory name or path +@param[in] dir directory stream +@param[out] info buffer where the info is returned +@return 0 if ok, -1 if error, 1 if at the end of the directory */ +int +os_file_readdir_next_file( + const char* dirname, + os_file_dir_t dir, + os_file_stat_t* info) { -#ifdef __WIN__ BOOL ret; - DWORD len; - DWORD ret2; - DWORD low; - DWORD high; - ibool retry; -#ifndef UNIV_HOTBACKUP - ulint i; -#endif /* !UNIV_HOTBACKUP */ + int status; + WIN32_FIND_DATA find_data; - /* On 64-bit Windows, ulint is 64 bits. But offset and n should be - no more than 32 bits. */ - ut_a((n & 0xFFFFFFFFUL) == n); +next_file: - os_n_file_reads++; - os_bytes_read_since_printout += n; + ret = FindNextFile(dir, &find_data); -try_again: - ut_ad(buf); - ut_ad(n > 0); + if (ret > 0) { - low = (DWORD) offset & 0xFFFFFFFF; - high = (DWORD) (offset >> 32); + const char* name; - os_mutex_enter(os_file_count_mutex); - os_n_pending_reads++; - MONITOR_INC(MONITOR_OS_PENDING_READS); - os_mutex_exit(os_file_count_mutex); + name = static_cast<const char*>(find_data.cFileName); -#ifndef UNIV_HOTBACKUP - /* Protect the seek / read operation with a mutex */ - i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES; + ut_a(strlen(name) < OS_FILE_MAX_PATH); - os_mutex_enter(os_file_seek_mutexes[i]); -#endif /* !UNIV_HOTBACKUP */ + if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) { - ret2 = SetFilePointer( - file, low, reinterpret_cast<PLONG>(&high), FILE_BEGIN); + goto next_file; + } - if (ret2 == 0xFFFFFFFF && GetLastError() != NO_ERROR) { + strcpy(info->name, name); -#ifndef UNIV_HOTBACKUP - os_mutex_exit(os_file_seek_mutexes[i]); -#endif /* !UNIV_HOTBACKUP */ + info->size = find_data.nFileSizeHigh; + info->size <<= 32; + info->size |= find_data.nFileSizeLow; - os_mutex_enter(os_file_count_mutex); - os_n_pending_reads--; - MONITOR_DEC(MONITOR_OS_PENDING_READS); - os_mutex_exit(os_file_count_mutex); + if (find_data.dwFileAttributes + & FILE_ATTRIBUTE_REPARSE_POINT) { - goto error_handling; - } + /* TODO: test Windows symlinks */ + /* TODO: MySQL has apparently its own symlink + implementation in Windows, dbname.sym can + redirect a database directory: + REFMAN "windows-symbolic-links.html" */ - ret = ReadFile(file, buf, (DWORD) n, &len, NULL); + info->type = OS_FILE_TYPE_LINK; -#ifndef UNIV_HOTBACKUP - os_mutex_exit(os_file_seek_mutexes[i]); -#endif /* !UNIV_HOTBACKUP */ + } else if (find_data.dwFileAttributes + & FILE_ATTRIBUTE_DIRECTORY) { - os_mutex_enter(os_file_count_mutex); - os_n_pending_reads--; - MONITOR_DEC(MONITOR_OS_PENDING_READS); - os_mutex_exit(os_file_count_mutex); + info->type = OS_FILE_TYPE_DIR; - if (ret && len == n) { - return(TRUE); - } -#else /* __WIN__ */ - ibool retry; - ssize_t ret; + } else { - os_bytes_read_since_printout += n; + /* It is probably safest to assume that all other + file types are normal. Better to check them rather + than blindly skip them. */ -try_again: - ret = os_file_pread(file, buf, n, offset); - - if ((ulint) ret == n) { - return(TRUE); - } else if (ret == -1) { - ib_logf(IB_LOG_LEVEL_ERROR, - "Error in system call pread(). The operating" - " system error number is %lu.",(ulint) errno); - } else { - /* Partial read occurred */ - ib_logf(IB_LOG_LEVEL_ERROR, - "Tried to read " ULINTPF " bytes at offset " - UINT64PF ". Was only able to read %ld.", - n, offset, (lint) ret); - } -#endif /* __WIN__ */ -#ifdef __WIN__ -error_handling: -#endif - retry = os_file_handle_error(NULL, "read", __FILE__, __LINE__); + info->type = OS_FILE_TYPE_FILE; + } - if (retry) { - goto try_again; - } + status = 0; - fprintf(stderr, - "InnoDB: Fatal error: cannot read from file." - " OS error number %lu.\n", -#ifdef __WIN__ - (ulong) GetLastError() -#else - (ulong) errno -#endif /* __WIN__ */ - ); - fflush(stderr); + } else if (GetLastError() == ERROR_NO_MORE_FILES) { - ut_error; + status = 1; + + } else { - return(FALSE); + os_file_handle_error_no_exit(NULL, "readdir_next_file", false); + + status = -1; + } + + return(status); } -/*******************************************************************//** -NOTE! Use the corresponding macro os_file_read_no_error_handling(), -not directly this function! -Requests a synchronous positioned read operation. This function does not do -any error handling. In case of error it returns FALSE. -@return TRUE if request was successful, FALSE if fail */ -UNIV_INTERN -ibool -os_file_read_no_error_handling_func( -/*================================*/ - os_file_t file, /*!< in: handle to a file */ - void* buf, /*!< in: buffer where to read */ - os_offset_t offset, /*!< in: file offset where to read */ - ulint n) /*!< in: number of bytes to read */ +/** NOTE! Use the corresponding macro os_file_create(), not directly +this function! +Opens an existing file or creates a new. +@param[in] name name of the file or path as a null-terminated + string +@param[in] create_mode create mode +@param[in] purpose OS_FILE_AIO, if asynchronous, non-buffered I/O + is desired, OS_FILE_NORMAL, if any normal file; + NOTE that it also depends on type, os_aio_.. + and srv_.. variables whether we really use async + I/O or unbuffered I/O: look in the function + source code for the exact rules +@param[in] type OS_DATA_FILE or OS_LOG_FILE +@param[in] success true if succeeded +@return handle to the file, not defined if error, error number + can be retrieved with os_file_get_last_error */ +os_file_t +os_file_create_func( + const char* name, + ulint create_mode, + ulint purpose, + ulint type, + bool read_only, + bool* success) { -#ifdef __WIN__ - BOOL ret; - DWORD len; - DWORD ret2; - DWORD low; - DWORD high; - ibool retry; -#ifndef UNIV_HOTBACKUP - ulint i; -#endif /* !UNIV_HOTBACKUP */ + os_file_t file; + bool retry; + bool on_error_no_exit; + bool on_error_silent; - /* On 64-bit Windows, ulint is 64 bits. But offset and n should be - no more than 32 bits. */ - ut_a((n & 0xFFFFFFFFUL) == n); + *success = false; - os_n_file_reads++; - os_bytes_read_since_printout += n; + DBUG_EXECUTE_IF( + "ib_create_table_fail_disk_full", + *success = false; + SetLastError(ERROR_DISK_FULL); + return(OS_FILE_CLOSED); + ); -try_again: - ut_ad(buf); - ut_ad(n > 0); + DWORD create_flag; + DWORD share_mode = FILE_SHARE_READ; - low = (DWORD) offset & 0xFFFFFFFF; - high = (DWORD) (offset >> 32); + on_error_no_exit = create_mode & OS_FILE_ON_ERROR_NO_EXIT + ? true : false; - os_mutex_enter(os_file_count_mutex); - os_n_pending_reads++; - MONITOR_INC(MONITOR_OS_PENDING_READS); - os_mutex_exit(os_file_count_mutex); + on_error_silent = create_mode & OS_FILE_ON_ERROR_SILENT + ? true : false; -#ifndef UNIV_HOTBACKUP - /* Protect the seek / read operation with a mutex */ - i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES; + create_mode &= ~OS_FILE_ON_ERROR_NO_EXIT; + create_mode &= ~OS_FILE_ON_ERROR_SILENT; - os_mutex_enter(os_file_seek_mutexes[i]); -#endif /* !UNIV_HOTBACKUP */ + if (create_mode == OS_FILE_OPEN_RAW) { - ret2 = SetFilePointer( - file, low, reinterpret_cast<PLONG>(&high), FILE_BEGIN); + ut_a(!read_only); - if (ret2 == 0xFFFFFFFF && GetLastError() != NO_ERROR) { + create_flag = OPEN_EXISTING; -#ifndef UNIV_HOTBACKUP - os_mutex_exit(os_file_seek_mutexes[i]); -#endif /* !UNIV_HOTBACKUP */ + /* On Windows Physical devices require admin privileges and + have to have the write-share mode set. See the remarks + section for the CreateFile() function documentation in MSDN. */ - os_mutex_enter(os_file_count_mutex); - os_n_pending_reads--; - MONITOR_DEC(MONITOR_OS_PENDING_READS); - os_mutex_exit(os_file_count_mutex); + share_mode |= FILE_SHARE_WRITE; - goto error_handling; - } + } else if (create_mode == OS_FILE_OPEN + || create_mode == OS_FILE_OPEN_RETRY) { - ret = ReadFile(file, buf, (DWORD) n, &len, NULL); + create_flag = OPEN_EXISTING; -#ifndef UNIV_HOTBACKUP - os_mutex_exit(os_file_seek_mutexes[i]); -#endif /* !UNIV_HOTBACKUP */ + } else if (read_only) { + + create_flag = OPEN_EXISTING; - os_mutex_enter(os_file_count_mutex); - os_n_pending_reads--; - MONITOR_DEC(MONITOR_OS_PENDING_READS); - os_mutex_exit(os_file_count_mutex); + } else if (create_mode == OS_FILE_CREATE) { - if (ret && len == n) { - return(TRUE); + create_flag = CREATE_NEW; + + } else if (create_mode == OS_FILE_OVERWRITE) { + + create_flag = CREATE_ALWAYS; + + } else { + ib::error() + << "Unknown file create mode (" << create_mode << ") " + << " for file '" << name << "'"; + + return(OS_FILE_CLOSED); } -#else /* __WIN__ */ - ibool retry; - ssize_t ret; - os_bytes_read_since_printout += n; + DWORD attributes = 0; -try_again: - ret = os_file_pread(file, buf, n, offset); - - if ((ulint) ret == n) { - return(TRUE); - } else if (ret == -1) { - ib_logf(IB_LOG_LEVEL_ERROR, - "Error in system call pread(). The operating" - " system error number is %lu.",(ulint) errno); - } else { - /* Partial read occurred */ - ib_logf(IB_LOG_LEVEL_ERROR, - "Tried to read " ULINTPF " bytes at offset " - UINT64PF ". Was only able to read %ld.", - n, offset, (lint) ret); - } -#endif /* __WIN__ */ -#ifdef __WIN__ -error_handling: -#endif - retry = os_file_handle_error_no_exit(NULL, "read", FALSE, __FILE__, __LINE__); +#ifdef UNIV_HOTBACKUP + attributes |= FILE_FLAG_NO_BUFFERING; +#else + if (purpose == OS_FILE_AIO) { - if (retry) { - goto try_again; +#ifdef WIN_ASYNC_IO + /* If specified, use asynchronous (overlapped) io and no + buffering of writes in the OS */ + + if (srv_use_native_aio) { + attributes |= FILE_FLAG_OVERLAPPED; + } +#endif /* WIN_ASYNC_IO */ + + } else if (purpose == OS_FILE_NORMAL) { + + /* Use default setting. */ + + } else { + + ib::error() + << "Unknown purpose flag (" << purpose << ") " + << "while opening file '" << name << "'"; + + return(OS_FILE_CLOSED); } - return(FALSE); -} +#ifdef UNIV_NON_BUFFERED_IO + // TODO: Create a bug, this looks wrong. The flush log + // parameter is dynamic. + if (type == OS_LOG_FILE && srv_flush_log_at_trx_commit == 2) { -/*******************************************************************//** -Rewind file to its start, read at most size - 1 bytes from it to str, and -NUL-terminate str. All errors are silently ignored. This function is -mostly meant to be used with temporary files. */ -UNIV_INTERN -void -os_file_read_string( -/*================*/ - FILE* file, /*!< in: file to read from */ - char* str, /*!< in: buffer where to read */ - ulint size) /*!< in: size of buffer */ -{ - size_t flen; + /* Do not use unbuffered i/o for the log files because + value 2 denotes that we do not flush the log at every + commit, but only once per second */ - if (size == 0) { - return; + } else if (srv_win_file_flush_method == SRV_WIN_IO_UNBUFFERED) { + + attributes |= FILE_FLAG_NO_BUFFERING; + } +#endif /* UNIV_NON_BUFFERED_IO */ + +#endif /* UNIV_HOTBACKUP */ + DWORD access = GENERIC_READ; + + if (!read_only) { + access |= GENERIC_WRITE; } - rewind(file); - flen = fread(str, 1, size - 1, file); - str[flen] = '\0'; + do { + /* Use default security attributes and no template file. */ + file = CreateFile( + (LPCTSTR) name, access, share_mode, NULL, + create_flag, attributes, NULL); + + if (file == INVALID_HANDLE_VALUE) { + const char* operation; + + operation = (create_mode == OS_FILE_CREATE + && !read_only) + ? "create" : "open"; + + *success = false; + + if (on_error_no_exit) { + retry = os_file_handle_error_no_exit( + name, operation, on_error_silent); + } else { + retry = os_file_handle_error(name, operation); + } + } else { + + retry = false; + + *success = true; + + DWORD temp; + + /* This is a best effort use case, if it fails then + we will find out when we try and punch the hole. */ + DeviceIoControl( + file, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, + &temp, NULL); + } + + } while (retry); + + return(file); } -/*******************************************************************//** -NOTE! Use the corresponding macro os_file_write(), not directly -this function! -Requests a synchronous write operation. -@return TRUE if request was successful, FALSE if fail */ -UNIV_INTERN -ibool -os_file_write_func( -/*===============*/ - const char* name, /*!< in: name of the file or path as a - null-terminated string */ - os_file_t file, /*!< in: handle to a file */ - const void* buf, /*!< in: buffer from which to write */ - os_offset_t offset, /*!< in: file offset where to write */ - ulint n) /*!< in: number of bytes to write */ +/** NOTE! Use the corresponding macro os_file_create_simple_no_error_handling(), +not directly this function! +A simple function to open or create a file. +@param[in] name name of the file or path as a null-terminated + string +@param[in] create_mode create mode +@param[in] access_type OS_FILE_READ_ONLY, OS_FILE_READ_WRITE, or + OS_FILE_READ_ALLOW_DELETE; the last option is + used by a backup program reading the file +@param[out] success true if succeeded +@return own: handle to the file, not defined if error, error number + can be retrieved with os_file_get_last_error */ +os_file_t +os_file_create_simple_no_error_handling_func( + const char* name, + ulint create_mode, + ulint access_type, + bool read_only, + bool* success) { - ut_ad(!srv_read_only_mode); + os_file_t file; -#ifdef __WIN__ - BOOL ret; - DWORD len; - DWORD ret2; - DWORD low; - DWORD high; - ulint n_retries = 0; - ulint err; - DWORD saved_error = 0; -#ifndef UNIV_HOTBACKUP - ulint i; -#endif /* !UNIV_HOTBACKUP */ + *success = false; - /* On 64-bit Windows, ulint is 64 bits. But offset and n should be - no more than 32 bits. */ - ut_a((n & 0xFFFFFFFFUL) == n); + DWORD access; + DWORD create_flag; + DWORD attributes = 0; + DWORD share_mode = FILE_SHARE_READ; - os_n_file_writes++; + ut_a(name); - ut_ad(buf); - ut_ad(n > 0); + ut_a(!(create_mode & OS_FILE_ON_ERROR_SILENT)); + ut_a(!(create_mode & OS_FILE_ON_ERROR_NO_EXIT)); -retry: - low = (DWORD) offset & 0xFFFFFFFF; - high = (DWORD) (offset >> 32); + if (create_mode == OS_FILE_OPEN) { - os_mutex_enter(os_file_count_mutex); - os_n_pending_writes++; - MONITOR_INC(MONITOR_OS_PENDING_WRITES); - os_mutex_exit(os_file_count_mutex); + create_flag = OPEN_EXISTING; -#ifndef UNIV_HOTBACKUP - /* Protect the seek / write operation with a mutex */ - i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES; + } else if (read_only) { - os_mutex_enter(os_file_seek_mutexes[i]); -#endif /* !UNIV_HOTBACKUP */ + create_flag = OPEN_EXISTING; - ret2 = SetFilePointer( - file, low, reinterpret_cast<PLONG>(&high), FILE_BEGIN); + } else if (create_mode == OS_FILE_CREATE) { - if (ret2 == 0xFFFFFFFF && GetLastError() != NO_ERROR) { + create_flag = CREATE_NEW; -#ifndef UNIV_HOTBACKUP - os_mutex_exit(os_file_seek_mutexes[i]); -#endif /* !UNIV_HOTBACKUP */ + } else { - os_mutex_enter(os_file_count_mutex); - os_n_pending_writes--; - MONITOR_DEC(MONITOR_OS_PENDING_WRITES); - os_mutex_exit(os_file_count_mutex); + ib::error() + << "Unknown file create mode (" << create_mode << ") " + << " for file '" << name << "'"; - ut_print_timestamp(stderr); + return(OS_FILE_CLOSED); + } - fprintf(stderr, - " InnoDB: Error: File pointer positioning to" - " file %s failed at\n" - "InnoDB: offset %llu. Operating system" - " error number %lu.\n" - "InnoDB: Some operating system error numbers" - " are described at\n" - "InnoDB: " - REFMAN "operating-system-error-codes.html\n", - name, offset, (ulong) GetLastError()); + if (access_type == OS_FILE_READ_ONLY) { - return(FALSE); - } + access = GENERIC_READ; - ret = WriteFile(file, buf, (DWORD) n, &len, NULL); + } else if (read_only) { -#ifndef UNIV_HOTBACKUP - os_mutex_exit(os_file_seek_mutexes[i]); -#endif /* !UNIV_HOTBACKUP */ + access = GENERIC_READ; + + } else if (access_type == OS_FILE_READ_WRITE) { + + access = GENERIC_READ | GENERIC_WRITE; + + } else if (access_type == OS_FILE_READ_ALLOW_DELETE) { + + ut_a(!read_only); - os_mutex_enter(os_file_count_mutex); - os_n_pending_writes--; - MONITOR_DEC(MONITOR_OS_PENDING_WRITES); - os_mutex_exit(os_file_count_mutex); + access = GENERIC_READ; - if (ret && len == n) { + /*!< A backup program has to give mysqld the maximum + freedom to do what it likes with the file */ - return(TRUE); + share_mode |= FILE_SHARE_DELETE | FILE_SHARE_WRITE; + } else { + + ib::error() + << "Unknown file access type (" << access_type << ") " + << "for file '" << name << "'"; + + return(OS_FILE_CLOSED); } - /* If some background file system backup tool is running, then, at - least in Windows 2000, we may get here a specific error. Let us - retry the operation 100 times, with 1 second waits. */ + file = CreateFile((LPCTSTR) name, + access, + share_mode, + NULL, // Security attributes + create_flag, + attributes, + NULL); // No template file - if (GetLastError() == ERROR_LOCK_VIOLATION && n_retries < 100) { + *success = (file != INVALID_HANDLE_VALUE); - os_thread_sleep(1000000); + return(file); +} - n_retries++; +/** Deletes a file if it exists. The file has to be closed before calling this. +@param[in] name file path as a null-terminated string +@param[out] exist indicate if file pre-exist +@return true if success */ +bool +os_file_delete_if_exists_func( + const char* name, + bool* exist) +{ + ulint count = 0; - goto retry; + if (exist != NULL) { + *exist = true; } - if (!os_has_said_disk_full) { - char *winmsg = NULL; + for (;;) { + /* In Windows, deleting an .ibd file may fail if ibbackup + is copying it */ - saved_error = GetLastError(); - err = (ulint) saved_error; + bool ret = DeleteFile((LPCTSTR) name); - ut_print_timestamp(stderr); + if (ret) { + return(true); + } - fprintf(stderr, - " InnoDB: Error: Write to file %s failed" - " at offset %llu.\n" - "InnoDB: %lu bytes should have been written," - " only %lu were written.\n" - "InnoDB: Operating system error number %lu.\n" - "InnoDB: Check that your OS and file system" - " support files of this size.\n" - "InnoDB: Check also that the disk is not full" - " or a disk quota exceeded.\n", - name, offset, - (ulong) n, (ulong) len, (ulong) err); + DWORD lasterr = GetLastError(); - /* Ask Windows to prepare a standard message for a - GetLastError() */ + if (lasterr == ERROR_FILE_NOT_FOUND + || lasterr == ERROR_PATH_NOT_FOUND) { - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, saved_error, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPSTR)&winmsg, 0, NULL); + /* the file does not exist, this not an error */ + if (exist != NULL) { + *exist = false; + } - if (winmsg) { - fprintf(stderr, - "InnoDB: FormatMessage: Error number %lu means '%s'.\n", - (ulong) saved_error, winmsg); - LocalFree(winmsg); + return(true); } - if (strerror((int) err) != NULL) { - fprintf(stderr, - "InnoDB: Error number %lu means '%s'.\n", - (ulong) err, strerror((int) err)); + ++count; + + if (count > 100 && 0 == (count % 10)) { + + /* Print error information */ + os_file_get_last_error(true); + + ib::warn() << "Delete of file '" << name << "' failed."; } - fprintf(stderr, - "InnoDB: Some operating system error numbers" - " are described at\n" - "InnoDB: " - REFMAN "operating-system-error-codes.html\n"); + /* Sleep for a second */ + os_thread_sleep(1000000); + + if (count > 2000) { - os_has_said_disk_full = TRUE; + return(false); + } } +} - return(FALSE); -#else - ssize_t ret; - WAIT_ALLOW_WRITES(); +/** Deletes a file. The file has to be closed before calling this. +@param[in] name File path as NUL terminated string +@return true if success */ +bool +os_file_delete_func( + const char* name) +{ + ulint count = 0; - ret = os_file_pwrite(file, buf, n, offset); + for (;;) { + /* In Windows, deleting an .ibd file may fail if ibbackup + is copying it */ - if ((ulint) ret == n) { + BOOL ret = DeleteFile((LPCTSTR) name); - return(TRUE); - } + if (ret) { + return(true); + } - if (!os_has_said_disk_full) { - ut_print_timestamp(stderr); + if (GetLastError() == ERROR_FILE_NOT_FOUND) { + /* If the file does not exist, we classify this as + a 'mild' error and return */ - if(ret == -1) { - ib_logf(IB_LOG_LEVEL_ERROR, - "Failure of system call pwrite(). Operating" - " system error number is %lu.", - (ulint) errno); - } else { - fprintf(stderr, - " InnoDB: Error: Write to file %s failed" - " at offset " UINT64PF ".\n" - "InnoDB: %lu bytes should have been written," - " only %ld were written.\n" - "InnoDB: Operating system error number %lu.\n" - "InnoDB: Check that your OS and file system" - " support files of this size.\n" - "InnoDB: Check also that the disk is not full" - " or a disk quota exceeded.\n", - name, offset, n, (lint) ret, - (ulint) errno); + return(false); } - if (strerror(errno) != NULL) { - fprintf(stderr, - "InnoDB: Error number %d means '%s'.\n", - errno, strerror(errno)); + ++count; + + if (count > 100 && 0 == (count % 10)) { + + /* print error information */ + os_file_get_last_error(true); + + ib::warn() + << "Cannot delete file '" << name << "'. Are " + << "you running ibbackup to back up the file?"; } - fprintf(stderr, - "InnoDB: Some operating system error numbers" - " are described at\n" - "InnoDB: " - REFMAN "operating-system-error-codes.html\n"); + /* sleep for a second */ + os_thread_sleep(1000000); + + if (count > 2000) { - os_has_said_disk_full = TRUE; + return(false); + } } - return(FALSE); -#endif + ut_error; + return(false); } -/*******************************************************************//** -Check the existence and type of the given file. -@return TRUE if call succeeded */ -UNIV_INTERN -ibool -os_file_status( -/*===========*/ - const char* path, /*!< in: pathname of the file */ - ibool* exists, /*!< out: TRUE if file exists */ - os_file_type_t* type) /*!< out: type of the file (if it exists) */ +/** NOTE! Use the corresponding macro os_file_rename(), not directly this +function! +Renames a file (can also move it to another directory). It is safest that the +file is closed before calling this function. +@param[in] oldpath old file path as a null-terminated string +@param[in] newpath new file path +@return true if success */ +bool +os_file_rename_func( + const char* oldpath, + const char* newpath) { -#ifdef __WIN__ - int ret; - struct _stat64 statinfo; +#ifdef UNIV_DEBUG + os_file_type_t type; + bool exists; - ret = _stat64(path, &statinfo); - if (ret && (errno == ENOENT || errno == ENOTDIR || errno == ENAMETOOLONG)) { - /* file does not exist */ - *exists = FALSE; - return(TRUE); - } else if (ret) { - /* file exists, but stat call failed */ + /* New path must not exist. */ + ut_ad(os_file_status(newpath, &exists, &type)); + ut_ad(!exists); - os_file_handle_error_no_exit(path, "stat", FALSE, __FILE__, __LINE__); + /* Old path must exist. */ + ut_ad(os_file_status(oldpath, &exists, &type)); + ut_ad(exists); +#endif /* UNIV_DEBUG */ - return(FALSE); + if (MoveFile((LPCTSTR) oldpath, (LPCTSTR) newpath)) { + return(true); } - if (_S_IFDIR & statinfo.st_mode) { - *type = OS_FILE_TYPE_DIR; - } else if (_S_IFREG & statinfo.st_mode) { - *type = OS_FILE_TYPE_FILE; - } else { - *type = OS_FILE_TYPE_UNKNOWN; - } + os_file_handle_error_no_exit(oldpath, "rename", false); - *exists = TRUE; + return(false); +} - return(TRUE); -#else - int ret; - struct stat statinfo; +/** NOTE! Use the corresponding macro os_file_close(), not directly +this function! +Closes a file handle. In case of error, error number can be retrieved with +os_file_get_last_error. +@param[in,own] file Handle to a file +@return true if success */ +bool +os_file_close_func( + os_file_t file) +{ + ut_a(file > 0); - ret = stat(path, &statinfo); - if (ret && (errno == ENOENT || errno == ENOTDIR || errno == ENAMETOOLONG)) { - /* file does not exist */ - *exists = FALSE; - return(TRUE); - } else if (ret) { - /* file exists, but stat call failed */ + if (CloseHandle(file)) { + return(true); + } + + os_file_handle_error(NULL, "close"); + + return(false); +} - os_file_handle_error_no_exit(path, "stat", FALSE, __FILE__, __LINE__); +/** Gets a file size. +@param[in] file Handle to a file +@return file size, or (os_offset_t) -1 on failure */ +os_offset_t +os_file_get_size( + os_file_t file) +{ + DWORD high; + DWORD low = GetFileSize(file, &high); - return(FALSE); + if (low == 0xFFFFFFFF && GetLastError() != NO_ERROR) { + return((os_offset_t) -1); } - if (S_ISDIR(statinfo.st_mode)) { - *type = OS_FILE_TYPE_DIR; - } else if (S_ISLNK(statinfo.st_mode)) { - *type = OS_FILE_TYPE_LINK; - } else if (S_ISREG(statinfo.st_mode)) { - *type = OS_FILE_TYPE_FILE; + return(os_offset_t(low | (os_offset_t(high) << 32))); +} + +/** Gets a file size. +@param[in] filename Full path to the filename to check +@return file size if OK, else set m_total_size to ~0 and m_alloc_size to + errno */ +os_file_size_t +os_file_get_size( + const char* filename) +{ + struct __stat64 s; + os_file_size_t file_size; + + int ret = _stat64(filename, &s); + + if (ret == 0) { + + file_size.m_total_size = s.st_size; + + DWORD low_size; + DWORD high_size; + + low_size = GetCompressedFileSize(filename, &high_size); + + if (low_size != INVALID_FILE_SIZE) { + + file_size.m_alloc_size = high_size; + file_size.m_alloc_size <<= 32; + file_size.m_alloc_size |= low_size; + + } else { + ib::error() + << "GetCompressedFileSize(" + << filename << ", ..) failed."; + + file_size.m_alloc_size = (os_offset_t) -1; + } } else { - *type = OS_FILE_TYPE_UNKNOWN; + file_size.m_total_size = ~0; + file_size.m_alloc_size = (os_offset_t) ret; } - *exists = TRUE; - - return(TRUE); -#endif + return(file_size); } -/*******************************************************************//** -This function returns information about the specified file -@return DB_SUCCESS if all OK */ -UNIV_INTERN +/** This function returns information about the specified file +@param[in] path pathname of the file +@param[out] stat_info information of a file in a directory +@param[in,out] statinfo information of a file in a directory +@param[in] check_rw_perm for testing whether the file can be opened + in RW mode +@param[in] read_only true if the file is opened in read-only mode +@return DB_SUCCESS if all OK */ +static dberr_t -os_file_get_status( -/*===============*/ - const char* path, /*!< in: pathname of the file */ - os_file_stat_t* stat_info, /*!< information of a file in a - directory */ - bool check_rw_perm) /*!< in: for testing whether the - file can be opened in RW mode */ +os_file_get_status_win32( + const char* path, + os_file_stat_t* stat_info, + struct _stat64* statinfo, + bool check_rw_perm, + bool read_only) { - int ret; - -#ifdef __WIN__ - struct _stat64 statinfo; - - ret = _stat64(path, &statinfo); + int ret = _stat64(path, statinfo); if (ret && (errno == ENOENT || errno == ENOTDIR)) { /* file does not exist */ @@ -3420,17 +5093,19 @@ os_file_get_status( } else if (ret) { /* file exists, but stat call failed */ - os_file_handle_error_no_exit(path, "stat", FALSE, __FILE__, __LINE__); + os_file_handle_error_no_exit(path, "STAT", false); return(DB_FAIL); - } else if (_S_IFDIR & statinfo.st_mode) { + } else if (_S_IFDIR & statinfo->st_mode) { + stat_info->type = OS_FILE_TYPE_DIR; - } else if (_S_IFREG & statinfo.st_mode) { + + } else if (_S_IFREG & statinfo->st_mode) { DWORD access = GENERIC_READ; - if (!srv_read_only_mode) { + if (!read_only) { access |= GENERIC_WRITE; } @@ -3457,731 +5132,1309 @@ os_file_get_status( CloseHandle(fh); } } - } else { - stat_info->type = OS_FILE_TYPE_UNKNOWN; - } -#else - struct stat statinfo; - ret = stat(path, &statinfo); + char volname[MAX_PATH]; + BOOL result = GetVolumePathName(path, volname, MAX_PATH); - if (ret && (errno == ENOENT || errno == ENOTDIR)) { - /* file does not exist */ + if (!result) { - return(DB_NOT_FOUND); + ib::error() + << "os_file_get_status_win32: " + << "Failed to get the volume path name for: " + << path + << "- OS error number " << GetLastError(); - } else if (ret) { - /* file exists, but stat call failed */ + return(DB_FAIL); + } - os_file_handle_error_no_exit(path, "stat", FALSE, __FILE__, __LINE__); + DWORD sectorsPerCluster; + DWORD bytesPerSector; + DWORD numberOfFreeClusters; + DWORD totalNumberOfClusters; - return(DB_FAIL); + result = GetDiskFreeSpace( + (LPCSTR) volname, + §orsPerCluster, + &bytesPerSector, + &numberOfFreeClusters, + &totalNumberOfClusters); - } + if (!result) { - switch (statinfo.st_mode & S_IFMT) { - case S_IFDIR: - stat_info->type = OS_FILE_TYPE_DIR; - break; - case S_IFLNK: - stat_info->type = OS_FILE_TYPE_LINK; - break; - case S_IFBLK: - /* Handle block device as regular file. */ - case S_IFCHR: - /* Handle character device as regular file. */ - case S_IFREG: - stat_info->type = OS_FILE_TYPE_FILE; - break; - default: + ib::error() + << "GetDiskFreeSpace(" << volname << ",...) " + << "failed " + << "- OS error number " << GetLastError(); + + return(DB_FAIL); + } + + stat_info->block_size = bytesPerSector * sectorsPerCluster; + + /* On Windows the block size is not used as the allocation + unit for sparse files. The underlying infra-structure for + sparse files is based on NTFS compression. The punch hole + is done on a "compression unit". This compression unit + is based on the cluster size. You cannot punch a hole if + the cluster size >= 8K. For smaller sizes the table is + as follows: + + Cluster Size Compression Unit + 512 Bytes 8 KB + 1 KB 16 KB + 2 KB 32 KB + 4 KB 64 KB + + Default NTFS cluster size is 4K, compression unit size of 64K. + Therefore unless the user has created the file system with + a smaller cluster size and used larger page sizes there is + little benefit from compression out of the box. */ + + stat_info->block_size = (stat_info->block_size <= 4096) + ? stat_info->block_size * 16 : ULINT_UNDEFINED; + } else { stat_info->type = OS_FILE_TYPE_UNKNOWN; } + return(DB_SUCCESS); +} - if (check_rw_perm && stat_info->type == OS_FILE_TYPE_FILE) { - - int fh; - int access; +/** Truncates a file to a specified size in bytes. +Do nothing if the size to preserve is greater or equal to the current +size of the file. +@param[in] pathname file path +@param[in] file file to be truncated +@param[in] size size to preserve in bytes +@return true if success */ +static +bool +os_file_truncate_win32( + const char* pathname, + os_file_t file, + os_offset_t size) +{ + LARGE_INTEGER length; - access = !srv_read_only_mode ? O_RDWR : O_RDONLY; + length.QuadPart = size; - fh = ::open(path, access, os_innodb_umask); + BOOL success = SetFilePointerEx(file, length, NULL, FILE_BEGIN); - if (fh == -1) { - stat_info->rw_perm = false; - } else { - stat_info->rw_perm = true; - close(fh); + if (!success) { + os_file_handle_error_no_exit( + pathname, "SetFilePointerEx", false); + } else { + success = SetEndOfFile(file); + if (!success) { + os_file_handle_error_no_exit( + pathname, "SetEndOfFile", false); } } + return(success); +} -#endif /* _WIN_ */ +/** Truncates a file at its current position. +@param[in] file Handle to be truncated +@return true if success */ +bool +os_file_set_eof( + FILE* file) +{ + HANDLE h = (HANDLE) _get_osfhandle(fileno(file)); - stat_info->ctime = statinfo.st_ctime; - stat_info->atime = statinfo.st_atime; - stat_info->mtime = statinfo.st_mtime; - stat_info->size = statinfo.st_size; + return(SetEndOfFile(h)); +} - return(DB_SUCCESS); +#ifdef UNIV_HOTBACKUP +/** Closes a file handle. +@param[in] file Handle to close +@return true if success */ +bool +os_file_close_no_error_handling( + os_file_t file) +{ + return(CloseHandle(file) ? true : false); } +#endif /* UNIV_HOTBACKUP */ -/* path name separator character */ -#ifdef __WIN__ -# define OS_FILE_PATH_SEPARATOR '\\' -#else -# define OS_FILE_PATH_SEPARATOR '/' -#endif +/** This function can be called if one wants to post a batch of reads and +prefers an i/o-handler thread to handle them all at once later. You must +call os_aio_simulated_wake_handler_threads later to ensure the threads +are not left sleeping! */ +void +os_aio_simulated_put_read_threads_to_sleep() +{ + AIO::simulated_put_read_threads_to_sleep(); +} -/****************************************************************//** -This function returns a new path name after replacing the basename -in an old path with a new basename. The old_path is a full path -name including the extension. The tablename is in the normal -form "databasename/tablename". The new base name is found after -the forward slash. Both input strings are null terminated. +/** This function can be called if one wants to post a batch of reads and +prefers an i/o-handler thread to handle them all at once later. You must +call os_aio_simulated_wake_handler_threads later to ensure the threads +are not left sleeping! */ +void +AIO::simulated_put_read_threads_to_sleep() +{ + /* The idea of putting background IO threads to sleep is only for + Windows when using simulated AIO. Windows XP seems to schedule + background threads too eagerly to allow for coalescing during + readahead requests. */ -This function allocates memory to be returned. It is the callers -responsibility to free the return value after it is no longer needed. + if (srv_use_native_aio) { + /* We do not use simulated AIO: do nothing */ -@return own: new full pathname */ -UNIV_INTERN -char* -os_file_make_new_pathname( -/*======================*/ - const char* old_path, /*!< in: pathname */ - const char* tablename) /*!< in: contains new base name */ -{ - ulint dir_len; - char* last_slash; - char* base_name; - char* new_path; - ulint new_path_len; + return; + } - /* Split the tablename into its database and table name components. - They are separated by a '/'. */ - last_slash = strrchr((char*) tablename, '/'); - base_name = last_slash ? last_slash + 1 : (char*) tablename; + os_aio_recommend_sleep_for_read_threads = true; - /* Find the offset of the last slash. We will strip off the - old basename.ibd which starts after that slash. */ - last_slash = strrchr((char*) old_path, OS_FILE_PATH_SEPARATOR); - dir_len = last_slash ? last_slash - old_path : strlen(old_path); + for (ulint i = 0; i < os_aio_n_segments; i++) { + AIO* array; - /* allocate a new path and move the old directory path to it. */ - new_path_len = dir_len + strlen(base_name) + sizeof "/.ibd"; - new_path = static_cast<char*>(mem_alloc(new_path_len)); - memcpy(new_path, old_path, dir_len); + get_array_and_local_segment(&array, i); - ut_snprintf(new_path + dir_len, - new_path_len - dir_len, - "%c%s.ibd", - OS_FILE_PATH_SEPARATOR, - base_name); + if (array == s_reads) { - return(new_path); + os_event_reset(os_aio_segment_wait_events[i]); + } + } } -/****************************************************************//** -This function returns a remote path name by combining a data directory -path provided in a DATA DIRECTORY clause with the tablename which is -in the form 'database/tablename'. It strips the file basename (which -is the tablename) found after the last directory in the path provided. -The full filepath created will include the database name as a directory -under the path provided. The filename is the tablename with the '.ibd' -extension. All input and output strings are null-terminated. - -This function allocates memory to be returned. It is the callers -responsibility to free the return value after it is no longer needed. +#endif /* !_WIN32*/ -@return own: A full pathname; data_dir_path/databasename/tablename.ibd */ -UNIV_INTERN -char* -os_file_make_remote_pathname( -/*=========================*/ - const char* data_dir_path, /*!< in: pathname */ - const char* tablename, /*!< in: tablename */ - const char* extention) /*!< in: file extention; ibd,cfg */ +/** Validate the type, offset and number of bytes to read * +@param[in] type IO flags +@param[in] offset Offset from start of the file +@param[in] n Number of bytes to read from offset */ +static +void +os_file_check_args(const IORequest& type, os_offset_t offset, ulint n) { - ulint data_dir_len; - char* last_slash; - char* new_path; - ulint new_path_len; + ut_ad(type.validate()); - ut_ad(extention && strlen(extention) == 3); + ut_ad(n > 0); - /* Find the offset of the last slash. We will strip off the - old basename or tablename which starts after that slash. */ - last_slash = strrchr((char*) data_dir_path, OS_FILE_PATH_SEPARATOR); - data_dir_len = last_slash ? last_slash - data_dir_path : strlen(data_dir_path); + /* If off_t is > 4 bytes in size, then we assume we can pass a + 64-bit address */ + off_t offs = static_cast<off_t>(offset); - /* allocate a new path and move the old directory path to it. */ - new_path_len = data_dir_len + strlen(tablename) - + sizeof "/." + strlen(extention); - new_path = static_cast<char*>(mem_alloc(new_path_len)); - memcpy(new_path, data_dir_path, data_dir_len); - ut_snprintf(new_path + data_dir_len, - new_path_len - data_dir_len, - "%c%s.%s", - OS_FILE_PATH_SEPARATOR, - tablename, - extention); - - srv_normalize_path_for_win(new_path); + if (sizeof(off_t) <= 4 && offset != (os_offset_t) offs) { - return(new_path); + ib::error() << "file write at offset > 4 GB."; + } } -/****************************************************************//** -This function reduces a null-terminated full remote path name into -the path that is sent by MySQL for DATA DIRECTORY clause. It replaces -the 'databasename/tablename.ibd' found at the end of the path with just -'tablename'. +/** Does a syncronous read or write depending upon the type specified +In case of partial reads/writes the function tries +NUM_RETRIES_ON_PARTIAL_IO times to read/write the complete data. +@param[in] type, IO flags +@param[in] file handle to an open file +@param[out] buf buffer where to read +@param[in] offset file offset from the start where to read +@param[in] n number of bytes to read, starting from offset +@param[out] err DB_SUCCESS or error code +@return number of bytes read/written, -1 if error */ +static __attribute__((warn_unused_result)) +ssize_t +os_file_io( + const IORequest&in_type, + os_file_t file, + void* buf, + ulint n, + os_offset_t offset, + dberr_t* err) +{ + byte* ptr; + ulint original_n = n; + IORequest type = in_type; + byte* compressed_page; + ssize_t bytes_returned = 0; -Since the result is always smaller than the path sent in, no new memory -is allocated. The caller should allocate memory for the path sent in. -This function manipulates that path in place. + if (type.is_compressed()) { -If the path format is not as expected, just return. The result is used -to inform a SHOW CREATE TABLE command. */ -UNIV_INTERN -void -os_file_make_data_dir_path( -/*========================*/ - char* data_dir_path) /*!< in/out: full path/data_dir_path */ -{ - char* ptr; - char* tablename; - ulint tablename_len; + /* We don't compress the first page of any file. */ + ut_ad(offset > 0); - /* Replace the period before the extension with a null byte. */ - ptr = strrchr((char*) data_dir_path, '.'); - if (!ptr) { - return; + ptr = os_file_compress_page(type, buf, &n); + + compressed_page = static_cast<byte*>( + ut_align(ptr, UNIV_SECTOR_SIZE)); + + } else { + ptr = NULL; + compressed_page = NULL; } - ptr[0] = '\0'; - /* The tablename starts after the last slash. */ - ptr = strrchr((char*) data_dir_path, OS_FILE_PATH_SEPARATOR); - if (!ptr) { - return; + SyncFileIO sync_file_io(file, buf, n, offset); + + for (ulint i = 0; i < NUM_RETRIES_ON_PARTIAL_IO; ++i) { + + ssize_t n_bytes = sync_file_io.execute(type); + + /* Check for a hard error. Not much we can do now. */ + if (n_bytes < 0) { + + break; + + } else if ((ulint) n_bytes + bytes_returned == n) { + + bytes_returned += n_bytes; + + if (offset > 0 + && (type.is_compressed() || type.is_read())) { + + *err = os_file_io_complete( + type, file, + reinterpret_cast<byte*>(buf), + compressed_page, original_n, + static_cast<ulint>(offset), n); + + if (ptr != NULL) { + ut_free(ptr); + } + + } else { + + *err = DB_SUCCESS; + } + + return(original_n); + } + + /* Handle partial read/write. */ + + ut_ad((ulint) n_bytes + bytes_returned < n); + + bytes_returned += (ulint) n_bytes; + + if (!type.is_partial_io_warning_disabled()) { + + const char* op = type.is_read() + ? "read" : "written"; + + ib::warn() + << n + << " bytes should have been " << op << ". Only " + << bytes_returned + << " bytes " << op << ". Retrying" + << " for the remaining bytes."; + } + + /* Advance the offset and buffer by n_bytes */ + sync_file_io.advance(n_bytes); } - ptr[0] = '\0'; - tablename = ptr + 1; - /* The databasename starts after the next to last slash. */ - ptr = strrchr((char*) data_dir_path, OS_FILE_PATH_SEPARATOR); - if (!ptr) { - return; + if (ptr != NULL) { + ut_free(ptr); } - tablename_len = ut_strlen(tablename); - ut_memmove(++ptr, tablename, tablename_len); + *err = DB_IO_ERROR; - ptr[tablename_len] = '\0'; + if (!type.is_partial_io_warning_disabled()) { + ib::warn() + << "Retry attempts for " + << (type.is_read() ? "reading" : "writing") + << " partial data failed."; + } + + return(bytes_returned); } -/****************************************************************//** -The function os_file_dirname returns a directory component of a -null-terminated pathname string. In the usual case, dirname returns -the string up to, but not including, the final '/', and basename -is the component following the final '/'. Trailing '/' characters -are not counted as part of the pathname. +/** Does a synchronous write operation in Posix. +@param[in] type IO context +@param[in] file handle to an open file +@param[out] buf buffer from which to write +@param[in] n number of bytes to read, starting from offset +@param[in] offset file offset from the start where to read +@param[out] err DB_SUCCESS or error code +@return number of bytes written, -1 if error */ +static __attribute__((warn_unused_result)) +ssize_t +os_file_pwrite( + IORequest& type, + os_file_t file, + const byte* buf, + ulint n, + os_offset_t offset, + dberr_t* err) +{ + ut_ad(type.validate()); -If path does not contain a slash, dirname returns the string ".". + ++os_n_file_writes; -Concatenating the string returned by dirname, a "/", and the basename -yields a complete pathname. + (void) os_atomic_increment_ulint(&os_n_pending_writes, 1); + MONITOR_ATOMIC_INC(MONITOR_OS_PENDING_WRITES); -The return value is a copy of the directory component of the pathname. -The copy is allocated from heap. It is the caller responsibility -to free it after it is no longer needed. + ssize_t n_bytes = os_file_io(type, file, (void*) buf, n, offset, err); -The following list of examples (taken from SUSv2) shows the strings -returned by dirname and basename for different paths: + (void) os_atomic_decrement_ulint(&os_n_pending_writes, 1); + MONITOR_ATOMIC_DEC(MONITOR_OS_PENDING_WRITES); - path dirname basename - "/usr/lib" "/usr" "lib" - "/usr/" "/" "usr" - "usr" "." "usr" - "/" "/" "/" - "." "." "." - ".." "." ".." + return(n_bytes); +} -@return own: directory component of the pathname */ -UNIV_INTERN -char* -os_file_dirname( -/*============*/ - const char* path) /*!< in: pathname */ +/** Requests a synchronous write operation. +@param[in] type IO flags +@param[in] file handle to an open file +@param[out] buf buffer from which to write +@param[in] offset file offset from the start where to read +@param[in] n number of bytes to read, starting from offset +@return DB_SUCCESS if request was successful, false if fail */ +static __attribute__((warn_unused_result)) +dberr_t +os_file_write_page( + IORequest& type, + const char* name, + os_file_t file, + const byte* buf, + os_offset_t offset, + ulint n) { - /* Find the offset of the last slash */ - const char* last_slash = strrchr(path, OS_FILE_PATH_SEPARATOR); - if (!last_slash) { - /* No slash in the path, return "." */ + dberr_t err; - return(mem_strdup(".")); - } + os_file_check_args(type, offset, n); - /* Ok, there is a slash */ + ssize_t n_bytes = os_file_pwrite(type, file, buf, n, offset, &err); - if (last_slash == path) { - /* last slash is the first char of the path */ + if ((ulint) n_bytes != n && !os_has_said_disk_full) { - return(mem_strdup("/")); + ib::error() + << "Write to file " << name << "failed at offset " + << offset << ", " << n + << " bytes should have been written," + " only " << n_bytes << " were written." + " Operating system error number " << errno << "." + " Check that your OS and file system" + " support files of this size." + " Check also that the disk is not full" + " or a disk quota exceeded."; + + if (strerror(errno) != NULL) { + + ib::error() + << "Error number " << errno + << " means '" << strerror(errno) << "'"; + } + + ib::info() << OPERATING_SYSTEM_ERROR_MSG; + + os_has_said_disk_full = true; } - /* Non-trivial directory component */ + return(err); +} - return(mem_strdupl(path, last_slash - path)); +/** Does a synchronous read operation in Posix. +@param[in] type IO flags +@param[in] file handle to an open file +@param[out] buf buffer where to read +@param[in] offset file offset from the start where to read +@param[in] n number of bytes to read, starting from offset +@param[out] err DB_SUCCESS or error code +@return number of bytes read, -1 if error */ +static __attribute__((warn_unused_result)) +ssize_t +os_file_pread( + IORequest& type, + os_file_t file, + void* buf, + ulint n, + os_offset_t offset, + dberr_t* err) +{ + ++os_n_file_reads; + + (void) os_atomic_increment_ulint(&os_n_pending_reads, 1); + MONITOR_ATOMIC_INC(MONITOR_OS_PENDING_READS); + + ssize_t n_bytes = os_file_io(type, file, buf, n, offset, err); + + (void) os_atomic_decrement_ulint(&os_n_pending_reads, 1); + MONITOR_ATOMIC_DEC(MONITOR_OS_PENDING_READS); + + return(n_bytes); } -/****************************************************************//** -Creates all missing subdirectories along the given path. -@return TRUE if call succeeded FALSE otherwise */ -UNIV_INTERN -ibool -os_file_create_subdirs_if_needed( -/*=============================*/ - const char* path) /*!< in: path name */ +/** Requests a synchronous positioned read operation. +@return DB_SUCCESS if request was successful, false if fail +@param[in] type IO flags +@param[in] file handle to an open file +@param[out] buf buffer where to read +@param[in] offset file offset from the start where to read +@param[in] n number of bytes to read, starting from offset +@param[out] o number of bytes actually read +@param[in] exit_on_err if true then exit on error +@return DB_SUCCESS or error code */ +static __attribute__((warn_unused_result)) +dberr_t +os_file_read_page( + IORequest& type, + os_file_t file, + void* buf, + os_offset_t offset, + ulint n, + ulint* o, + bool exit_on_err) { - if (srv_read_only_mode) { + dberr_t err; - ib_logf(IB_LOG_LEVEL_ERROR, - "read only mode set. Can't create subdirectories '%s'", - path); + os_bytes_read_since_printout += n; - return(FALSE); + os_file_check_args(type, offset, n); - } + for (;;) { + ssize_t n_bytes; - char* subdir = os_file_dirname(path); + n_bytes = os_file_pread(type, file, buf, n, offset, &err); - if (strlen(subdir) == 1 - && (*subdir == OS_FILE_PATH_SEPARATOR || *subdir == '.')) { - /* subdir is root or cwd, nothing to do */ - mem_free(subdir); + if (o != NULL) { + *o = n_bytes; + } - return(TRUE); - } + if (err != DB_SUCCESS && !exit_on_err) { - /* Test if subdir exists */ - os_file_type_t type; - ibool subdir_exists; - ibool success = os_file_status(subdir, &subdir_exists, &type); + return(err); - if (success && !subdir_exists) { + } else if ((ulint) n_bytes == n) { - /* subdir does not exist, create it */ - success = os_file_create_subdirs_if_needed(subdir); + /** The read will succeed but decompress can fail + for various reasons. */ - if (!success) { - mem_free(subdir); + if (type.is_compression_enabled() + && !Compression::is_compressed_page( + static_cast<byte*>(buf))) { + + return(DB_SUCCESS); + + } else { + return(err); + } + } - return(FALSE); + ib::error() + << "Tried to read " << n << " bytes at offset " + << offset << " was only able to read" << n_bytes; + + if (exit_on_err) { + + if (!os_file_handle_error(NULL, "read")) { + /* Hard error */ + break; + } + + } else if (!os_file_handle_error_no_exit(NULL, "read", false)) { + + /* Hard error */ + break; } - success = os_file_create_directory(subdir, FALSE); + if (n_bytes > 0 && (ulint) n_bytes < n) { + n -= (ulint) n_bytes; + offset += (ulint) n_bytes; + buf = reinterpret_cast<uchar*>(buf) + (ulint) n_bytes; + } } - mem_free(subdir); + ib::fatal() + << "Cannot read from file. OS error number " + << errno << "."; - return(success); + return(err); } -#ifndef UNIV_HOTBACKUP -/****************************************************************//** -Returns a pointer to the nth slot in the aio array. -@return pointer to slot */ -static -os_aio_slot_t* -os_aio_array_get_nth_slot( -/*======================*/ - os_aio_array_t* array, /*!< in: aio array */ - ulint index) /*!< in: index of the slot */ +/** Retrieves the last error number if an error occurs in a file io function. +The number should be retrieved before any other OS calls (because they may +overwrite the error number). If the number is not known to this program, +the OS error number + 100 is returned. +@param[in] report_all_errors true if we want an error printed + for all errors +@return error number, or OS error number + 100 */ +ulint +os_file_get_last_error( + bool report_all_errors) { - ut_a(index < array->n_slots); - - return(&array->slots[index]); + return(os_file_get_last_error_low(report_all_errors, false)); } -#if defined(LINUX_NATIVE_AIO) -/******************************************************************//** -Creates an io_context for native linux AIO. -@return TRUE on success. */ -static -ibool -os_aio_linux_create_io_ctx( -/*=======================*/ - ulint max_events, /*!< in: number of events. */ - io_context_t* io_ctx) /*!< out: io_ctx to initialize. */ +/** Does error handling when a file operation fails. +Conditionally exits (calling srv_fatal_error()) based on should_exit value +and the error type, if should_exit is true then on_error_silent is ignored. +@param[in] name name of a file or NULL +@param[in] operation operation +@param[in] should_exit call srv_fatal_error() on an unknown error, + if this parameter is true +@param[in] on_error_silent if true then don't print any message to the log + iff it is an unknown non-fatal error +@return true if we should retry the operation */ +static __attribute__((warn_unused_result)) +bool +os_file_handle_error_cond_exit( + const char* name, + const char* operation, + bool should_exit, + bool on_error_silent) { - int ret; - ulint retries = 0; + ulint err; -retry: - memset(io_ctx, 0x0, sizeof(*io_ctx)); + err = os_file_get_last_error_low(false, on_error_silent); - /* Initialize the io_ctx. Tell it how many pending - IO requests this context will handle. */ + switch (err) { + case OS_FILE_DISK_FULL: + /* We only print a warning about disk full once */ - ret = io_setup(max_events, io_ctx); - if (ret == 0) { -#if defined(UNIV_AIO_DEBUG) - fprintf(stderr, - "InnoDB: Linux native AIO:" - " initialized io_ctx for segment\n"); -#endif - /* Success. Return now. */ - return(TRUE); - } - - /* If we hit EAGAIN we'll make a few attempts before failing. */ - - switch (ret) { - case -EAGAIN: - if (retries == 0) { - /* First time around. */ - ut_print_timestamp(stderr); - fprintf(stderr, - " InnoDB: Warning: io_setup() failed" - " with EAGAIN. Will make %d attempts" - " before giving up.\n", - OS_AIO_IO_SETUP_RETRY_ATTEMPTS); + if (os_has_said_disk_full) { + + return(false); } - if (retries < OS_AIO_IO_SETUP_RETRY_ATTEMPTS) { - ++retries; - fprintf(stderr, - "InnoDB: Warning: io_setup() attempt" - " %lu failed.\n", - retries); - os_thread_sleep(OS_AIO_IO_SETUP_RETRY_SLEEP); - goto retry; + /* Disk full error is reported irrespective of the + on_error_silent setting. */ + + if (name) { + + ib::error() + << "Encountered a problem with file '" + << name << "'"; } - /* Have tried enough. Better call it a day. */ - ut_print_timestamp(stderr); - fprintf(stderr, - " InnoDB: Error: io_setup() failed" - " with EAGAIN after %d attempts.\n", - OS_AIO_IO_SETUP_RETRY_ATTEMPTS); - break; + ib::error() + << "Disk is full. Try to clean the disk to free space."; - case -ENOSYS: - ut_print_timestamp(stderr); - fprintf(stderr, - " InnoDB: Error: Linux Native AIO interface" - " is not supported on this platform. Please" - " check your OS documentation and install" - " appropriate binary of InnoDB.\n"); + os_has_said_disk_full = true; - break; + return(false); + + case OS_FILE_AIO_RESOURCES_RESERVED: + case OS_FILE_AIO_INTERRUPTED: + + return(true); + + case OS_FILE_PATH_ERROR: + case OS_FILE_ALREADY_EXISTS: + case OS_FILE_ACCESS_VIOLATION: + + return(false); + + case OS_FILE_SHARING_VIOLATION: + + os_thread_sleep(10000000); /* 10 sec */ + return(true); + + case OS_FILE_OPERATION_ABORTED: + case OS_FILE_INSUFFICIENT_RESOURCE: + + os_thread_sleep(100000); /* 100 ms */ + return(true); default: - ut_print_timestamp(stderr); - fprintf(stderr, - " InnoDB: Error: Linux Native AIO setup" - " returned following error[%d]\n", -ret); - break; + + /* If it is an operation that can crash on error then it + is better to ignore on_error_silent and print an error message + to the log. */ + + if (should_exit || !on_error_silent) { + ib::error() << "File " + << (name != NULL ? name : "(unknown)") + << ": '" << operation << "'" + " returned OS error " << err << "." + << (should_exit + ? " Cannot continue operation" : ""); + } + + if (should_exit) { + srv_fatal_error(); + } } - fprintf(stderr, - "InnoDB: You can disable Linux Native AIO by" - " setting innodb_use_native_aio = 0 in my.cnf\n"); - return(FALSE); + return(false); } -/******************************************************************//** -Checks if the system supports native linux aio. On some kernel -versions where native aio is supported it won't work on tmpfs. In such -cases we can't use native aio as it is not possible to mix simulated -and native aio. -@return: TRUE if supported, FALSE otherwise. */ +/** Does error handling when a file operation fails. +@param[in] name name of a file or NULL +@param[in] operation operation name that failed +@return true if we should retry the operation */ static -ibool -os_aio_native_aio_supported(void) -/*=============================*/ +bool +os_file_handle_error( + const char* name, + const char* operation) { - int fd; - io_context_t io_ctx; - char name[1000]; + /* Exit in case of unknown error */ + return(os_file_handle_error_cond_exit(name, operation, true, false)); +} - if (!os_aio_linux_create_io_ctx(1, &io_ctx)) { - /* The platform does not support native aio. */ - return(FALSE); - } else if (!srv_read_only_mode) { - /* Now check if tmpdir supports native aio ops. */ - fd = innobase_mysql_tmpfile(NULL); +/** Does error handling when a file operation fails. +@param[in] name name of a file or NULL +@param[in] operation operation name that failed +@param[in] on_error_silent if true then don't print any message to the log. +@return true if we should retry the operation */ +static +bool +os_file_handle_error_no_exit( + const char* name, + const char* operation, + bool on_error_silent) +{ + /* Don't exit in case of unknown error */ + return(os_file_handle_error_cond_exit( + name, operation, false, on_error_silent)); +} - if (fd < 0) { - ib_logf(IB_LOG_LEVEL_WARN, - "Unable to create temp file to check " - "native AIO support."); +/** 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( + os_file_t fd __attribute__((unused)), + const char* file_name __attribute__((unused)), + const char* operation_name __attribute__((unused))) +{ + /* some versions of Solaris may not have DIRECTIO_ON */ +#if defined(UNIV_SOLARIS) && defined(DIRECTIO_ON) + if (directio(fd, DIRECTIO_ON) == -1) { + int errno_save = errno; - return(FALSE); + ib::error() + << "Failed to set DIRECTIO_ON on file " + << file_name << ": " << operation_name + << strerror(errno_save) << "," + " continuing anyway."; + } +#elif defined(O_DIRECT) + 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; +# ifdef UNIV_LINUX + ib::warn() + << "Failed to set O_DIRECT on file" + << file_name << ";" << operation_name + << ": " << strerror(errno_save) << ", " + << "ccontinuing anyway. O_DIRECT is " + "known to result in 'Invalid argument' " + "on Linux on tmpfs, " + "see MySQL Bug#26662."; +# else /* UNIV_LINUX */ + goto short_warning; +# endif /* UNIV_LINUX */ + } + } else { +# ifndef UNIV_LINUX +short_warning: +# endif + ib::warn() + << "Failed to set O_DIRECT on file " + << file_name << "; " << operation_name + << " : " << strerror(errno_save) + << " continuing anyway."; } - } else { + } +#endif /* defined(UNIV_SOLARIS) && defined(DIRECTIO_ON) */ +} + +/** Write the specified number of zeros to a newly created file. +@param[in] name name of the file or path as a null-terminated + string +@param[in] file handle to a file +@param[in] size file size +@param[in] read_only Enable read-only checks if true +@return true if success */ +bool +os_file_set_size( + const char* name, + os_file_t file, + os_offset_t size, + bool read_only) +{ + /* Write up to 1 megabyte at a time. */ + ulint buf_size = ut_min( + static_cast<ulint>(64), + static_cast<ulint>(size / UNIV_PAGE_SIZE)); - srv_normalize_path_for_win(srv_log_group_home_dir); + buf_size *= UNIV_PAGE_SIZE; - ulint dirnamelen = strlen(srv_log_group_home_dir); - ut_a(dirnamelen < (sizeof name) - 10 - sizeof "ib_logfile"); - memcpy(name, srv_log_group_home_dir, dirnamelen); + /* Align the buffer for possible raw i/o */ + byte* buf2; - /* Add a path separator if needed. */ - if (dirnamelen && name[dirnamelen - 1] != SRV_PATH_SEPARATOR) { - name[dirnamelen++] = SRV_PATH_SEPARATOR; + buf2 = static_cast<byte*>(ut_malloc_nokey(buf_size + UNIV_PAGE_SIZE)); + + byte* buf = static_cast<byte*>(ut_align(buf2, UNIV_PAGE_SIZE)); + + /* Write buffer full of zeros */ + memset(buf, 0, buf_size); + + if (size >= (os_offset_t) 100 << 20) { + + ib::info() << "Progress in MB:"; + } + + os_offset_t current_size = 0; + + while (current_size < size) { + ulint n_bytes; + + if (size - current_size < (os_offset_t) buf_size) { + n_bytes = (ulint) (size - current_size); + } else { + n_bytes = buf_size; } - strcpy(name + dirnamelen, "ib_logfile0"); + dberr_t err; + IORequest request(IORequest::WRITE); - fd = ::open(name, O_RDONLY); +#ifdef UNIV_HOTBACKUP - if (fd == -1) { + err = os_file_write( + request, name, file, buf, current_size, n_bytes); +#else + /* Using OS_AIO_SYNC mode on POSIX systems will result in + fall back to os_file_write/read. On Windows it will use + special mechanism to wait before it returns back. */ + + err = os_aio( + request, + OS_AIO_SYNC, name, + file, buf, current_size, n_bytes, + read_only, NULL, NULL, NULL); +#endif /* UNIV_HOTBACKUP */ - ib_logf(IB_LOG_LEVEL_WARN, - "Unable to open \"%s\" to check " - "native AIO read support.", name); + if (err != DB_SUCCESS) { - return(FALSE); + ut_free(buf2); + return(false); } - } - struct io_event io_event; + /* Print about progress for each 100 MB written */ + if ((current_size + n_bytes) / (100 << 20) + != current_size / (100 << 20)) { - memset(&io_event, 0x0, sizeof(io_event)); + fprintf(stderr, " %lu00", + (ulong) ((current_size + n_bytes) + / (100 << 20))); + } - byte* buf = static_cast<byte*>(ut_malloc(UNIV_PAGE_SIZE * 2)); - byte* ptr = static_cast<byte*>(ut_align(buf, UNIV_PAGE_SIZE)); + current_size += n_bytes; + } - struct iocb iocb; + if (size >= (os_offset_t) 100 << 20) { - /* Suppress valgrind warning. */ - memset(buf, 0x00, UNIV_PAGE_SIZE * 2); - memset(&iocb, 0x0, sizeof(iocb)); + fprintf(stderr, "\n"); + } - struct iocb* p_iocb = &iocb; + ut_free(buf2); - if (!srv_read_only_mode) { - io_prep_pwrite(p_iocb, fd, ptr, UNIV_PAGE_SIZE, 0); - } else { - ut_a(UNIV_PAGE_SIZE >= 512); - io_prep_pread(p_iocb, fd, ptr, 512, 0); + return(os_file_flush(file)); +} + +/** Truncates a file to a specified size in bytes. +Do nothing if the size to preserve is greater or equal to the current +size of the file. +@param[in] pathname file path +@param[in] file file to be truncated +@param[in] size size to preserve in bytes +@return true if success */ +bool +os_file_truncate( + const char* pathname, + os_file_t file, + os_offset_t size) +{ + /* Do nothing if the size preserved is larger than or equal to the + current size of file */ + os_offset_t size_bytes = os_file_get_size(file); + + if (size >= size_bytes) { + return(true); } - int err = io_submit(io_ctx, 1, &p_iocb); +#ifdef _WIN32 + return(os_file_truncate_win32(pathname, file, size)); +#else /* _WIN32 */ + return(os_file_truncate_posix(pathname, file, size)); +#endif /* _WIN32 */ +} - if (err >= 1) { - /* Now collect the submitted IO request. */ - err = io_getevents(io_ctx, 1, 1, &io_event, NULL); +/** NOTE! Use the corresponding macro os_file_read(), not directly this +function! +Requests a synchronous positioned read operation. +@return DB_SUCCESS if request was successful, DB_IO_ERROR on failure +@param[in] type IO flags +@param[in] file handle to an open file +@param[out] buf buffer where to read +@param[in] offset file offset from the start where to read +@param[in] n number of bytes to read, starting from offset +@return DB_SUCCESS or error code */ +dberr_t +os_file_read_func( + IORequest& type, + os_file_t file, + void* buf, + os_offset_t offset, + ulint n) +{ + ut_ad(type.is_read()); + + return(os_file_read_page(type, file, buf, offset, n, NULL, true)); +} + +/** NOTE! Use the corresponding macro os_file_read_no_error_handling(), +not directly this function! +Requests a synchronous positioned read operation. +@return DB_SUCCESS if request was successful, DB_IO_ERROR on failure +@param[in] type IO flags +@param[in] file handle to an open file +@param[out] buf buffer where to read +@param[in] offset file offset from the start where to read +@param[in] n number of bytes to read, starting from offset +@param[out] o number of bytes actually read +@return DB_SUCCESS or error code */ +dberr_t +os_file_read_no_error_handling_func( + IORequest& type, + os_file_t file, + void* buf, + os_offset_t offset, + ulint n, + ulint* o) +{ + ut_ad(type.is_read()); + + return(os_file_read_page(type, file, buf, offset, n, o, false)); +} + +/** NOTE! Use the corresponding macro os_file_write(), not directly +Requests a synchronous write operation. +@param[in] type IO flags +@param[in] file handle to an open file +@param[out] buf buffer from which to write +@param[in] offset file offset from the start where to read +@param[in] n number of bytes to read, starting from offset +@return DB_SUCCESS if request was successful, false if fail */ +dberr_t +os_file_write_func( + IORequest& type, + const char* name, + os_file_t file, + const void* buf, + os_offset_t offset, + ulint n) +{ + ut_ad(type.validate()); + ut_ad(type.is_write()); + + /* We never compress the first page. + Note: This assumes we always do block IO. */ + if (offset == 0) { + type.clear_compressed(); } - ut_free(buf); - close(fd); + const byte* ptr = reinterpret_cast<const byte*>(buf); - switch (err) { - case 1: - return(TRUE); + return(os_file_write_page(type, name, file, ptr, offset, n)); +} - case -EINVAL: - case -ENOSYS: - ib_logf(IB_LOG_LEVEL_ERROR, - "Linux Native AIO not supported. You can either " - "move %s to a file system that supports native " - "AIO or you can set innodb_use_native_aio to " - "FALSE to avoid this message.", - srv_read_only_mode ? name : "tmpdir"); +/** Check the existence and type of the given file. +@param[in] path path name of file +@param[out] exists true if the file exists +@param[out] type Type of the file, if it exists +@return true if call succeeded */ +bool +os_file_status( + const char* path, + bool* exists, + os_file_type_t* type) +{ +#ifdef _WIN32 + return(os_file_status_win32(path, exists, type)); +#else + return(os_file_status_posix(path, exists, type)); +#endif /* _WIN32 */ +} - /* fall through. */ - default: - ib_logf(IB_LOG_LEVEL_ERROR, - "Linux Native AIO check on %s returned error[%d]", - srv_read_only_mode ? name : "tmpdir", -err); +/** Free storage space associated with a section of the file. +@param[in] fh Open file handle +@param[in] off Starting offset (SEEK_SET) +@param[in] len Size of the hole +@return DB_SUCCESS or error code */ +dberr_t +os_file_punch_hole( + os_file_t fh, + os_offset_t off, + os_offset_t len) +{ +#ifdef _WIN32 + return(os_file_punch_hole_win32(fh, off, len)); +#else + return(os_file_punch_hole_posix(fh, off, len)); +#endif /* _WIN32 */ +} + +/** Check if the file system supports sparse files. + +Warning: On POSIX systems we try and punch a hole from offset 0 to +the system configured page size. This should only be called on an empty +file. + +Note: On Windows we use the name and on Unices we use the file handle. + +@param[in] name File name +@param[in] fh File handle for the file - if opened +@return true if the file system supports sparse files */ +bool +os_is_sparse_file_supported(const char* path, os_file_t fh) +{ +#ifdef _WIN32 + return(os_is_sparse_file_supported_win32(path)); +#else + dberr_t err; + + /* We don't know the FS block size, use the sector size. The FS + will do the magic. */ + err = os_file_punch_hole(fh, 0, UNIV_PAGE_SIZE); + + return(err == DB_SUCCESS); +#endif /* _WIN32 */ +} + +/** This function returns information about the specified file +@param[in] path pathname of the file +@param[out] stat_info information of a file in a directory +@param[in] check_rw_perm for testing whether the file can be opened + in RW mode +@param[in] read_only true if file is opened in read-only mode +@return DB_SUCCESS if all OK */ +dberr_t +os_file_get_status( + const char* path, + os_file_stat_t* stat_info, + bool check_rw_perm, + bool read_only) +{ + dberr_t ret; + +#ifdef _WIN32 + struct _stat64 info; + + ret = os_file_get_status_win32( + path, stat_info, &info, check_rw_perm, read_only); + +#else + struct stat info; + + ret = os_file_get_status_posix( + path, stat_info, &info, check_rw_perm, read_only); + +#endif /* _WIN32 */ + + if (ret == DB_SUCCESS) { + stat_info->ctime = info.st_ctime; + stat_info->atime = info.st_atime; + stat_info->mtime = info.st_mtime; + stat_info->size = info.st_size; } - return(FALSE); + return(ret); } -#endif /* LINUX_NATIVE_AIO */ -/******************************************************************//** -Creates an aio wait array. Note that we return NULL in case of failure. -We don't care about freeing memory here because we assume that a -failure will result in server refusing to start up. -@return own: aio array, NULL on failure */ -static -os_aio_array_t* -os_aio_array_create( -/*================*/ - ulint n, /*!< in: maximum number of pending aio - operations allowed; n must be - divisible by n_segments */ - ulint n_segments) /*!< in: number of segments in the aio array */ -{ - os_aio_array_t* array; +/** +Waits for an AIO operation to complete. This function is used to wait the +for completed requests. The aio array of pending requests is divided +into segments. The thread specifies which segment or slot it wants to wait +for. NOTE: this function will also take care of freeing the aio slot, +therefore no other thread is allowed to do the freeing! +@param[in] segment The number of the segment in the aio arrays to + wait for; segment 0 is the ibuf I/O thread, + segment 1 the log I/O thread, then follow the + non-ibuf read threads, and as the last are the + non-ibuf write threads; if this is + ULINT_UNDEFINED, then it means that sync AIO + is used, and this parameter is ignored +@param[out] m1 the messages passed with the AIO request; note + that also in the case where the AIO operation + failed, these output parameters are valid and + can be used to restart the operation, + for example +@param[out] m2 callback message +@param[out] type OS_FILE_WRITE or ..._READ +@return DB_SUCCESS or error code */ +dberr_t +os_aio_handler( + ulint segment, + fil_node_t** m1, + void** m2, + IORequest* request) +{ + dberr_t err; + + if (srv_use_native_aio) { + srv_set_io_thread_op_info(segment, "native aio handle"); + #ifdef WIN_ASYNC_IO - OVERLAPPED* over; + + err = os_aio_windows_handler(segment, 0, m1, m2, request); + #elif defined(LINUX_NATIVE_AIO) - struct io_event* io_event = NULL; + + err = os_aio_linux_handler(segment, m1, m2, request); + +#else + ut_error; + + err = DB_ERROR; /* Eliminate compiler warning */ + #endif /* WIN_ASYNC_IO */ + + } else { + srv_set_io_thread_op_info(segment, "simulated aio handle"); + + err = os_aio_simulated_handler(segment, m1, m2, request); + } + + return(err); +} + +/** Constructor +@param[in] id The latch ID +@param[in] n Number of AIO slots +@param[in] segments Number of segments */ +AIO::AIO( + latch_id_t id, + ulint n, + ulint segments) + : + m_slots(n), + m_n_segments(segments), + m_n_reserved() +# ifdef LINUX_NATIVE_AIO + ,m_aio_ctx(), + m_events(m_slots.size()) +# elif defined(_WIN32) + ,m_handles() +# endif /* LINUX_NATIVE_AIO */ +{ ut_a(n > 0); - ut_a(n_segments > 0); + ut_a(m_n_segments > 0); - array = static_cast<os_aio_array_t*>(ut_malloc(sizeof(*array))); - memset(array, 0x0, sizeof(*array)); + mutex_create(id, &m_mutex); - array->mutex = os_mutex_create(); - array->not_full = os_event_create(); - array->is_empty = os_event_create(); + m_not_full = os_event_create("aio_not_full"); + m_is_empty = os_event_create("aio_is_empty"); - os_event_set(array->is_empty); + memset(&m_slots[0], 0x0, sizeof(m_slots[0]) * m_slots.size()); +#ifdef LINUX_NATIVE_AIO + memset(&m_events[0], 0x0, sizeof(m_events[0]) * m_events.size()); +#endif /* LINUX_NATIVE_AIO */ - array->n_slots = n; - array->n_segments = n_segments; + os_event_set(m_is_empty); +} - array->slots = static_cast<os_aio_slot_t*>( - ut_malloc(n * sizeof(*array->slots))); +/** Initialise the slots */ +dberr_t +AIO::init_slots() +{ + for (ulint i = 0; i < m_slots.size(); ++i) { + Slot& slot = m_slots[i]; - memset(array->slots, 0x0, n * sizeof(*array->slots)); + slot.pos = static_cast<uint16_t>(i); -#ifdef __WIN__ - array->handles = static_cast<HANDLE*>(ut_malloc(n * sizeof(HANDLE))); -#endif /* __WIN__ */ + slot.is_reserved = false; -#if defined(LINUX_NATIVE_AIO) - array->aio_ctx = NULL; - array->aio_events = NULL; +#ifdef WIN_ASYNC_IO + + slot.handle = CreateEvent(NULL, TRUE, FALSE, NULL); + + OVERLAPPED* over = &slot.control; + + over->hEvent = slot.handle; + + (*m_handles)[i] = over->hEvent; + +#elif defined(LINUX_NATIVE_AIO) + + slot.ret = 0; + + slot.n_bytes = 0; + + memset(&slot.control, 0x0, sizeof(slot.control)); + +#endif /* WIN_ASYNC_IO */ + + slot.compressed_ptr = reinterpret_cast<byte*>( + ut_zalloc_nokey(UNIV_PAGE_SIZE_MAX * 2)); + + if (slot.compressed_ptr == NULL) { + return(DB_OUT_OF_MEMORY); + } - /* If we are not using native aio interface then skip this - part of initialization. */ - if (!srv_use_native_aio) { - goto skip_native_aio; + slot.compressed_page = static_cast<byte *>( + ut_align(slot.compressed_ptr, UNIV_PAGE_SIZE)); } + return(DB_SUCCESS); +} + +#ifdef LINUX_NATIVE_AIO +/** Initialise the Linux Native AIO interface */ +dberr_t +AIO::init_linux_native_aio() +{ /* Initialize the io_context array. One io_context per segment in the array. */ - array->aio_ctx = static_cast<io_context**>( - ut_malloc(n_segments * sizeof(*array->aio_ctx))); + ut_a(m_aio_ctx == NULL); - for (ulint i = 0; i < n_segments; ++i) { - if (!os_aio_linux_create_io_ctx(n/n_segments, - &array->aio_ctx[i])) { + m_aio_ctx = static_cast<io_context**>( + ut_zalloc_nokey(m_n_segments * sizeof(*m_aio_ctx))); + + if (m_aio_ctx == NULL) { + return(DB_OUT_OF_MEMORY); + } + + io_context** ctx = m_aio_ctx; + ulint max_events = slots_per_segment(); + + for (ulint i = 0; i < m_n_segments; ++i, ++ctx) { + + if (!linux_create_io_ctx(max_events, ctx)) { /* If something bad happened during aio setup - we disable linux native aio. - The disadvantage will be a small memory leak - at shutdown but that's ok compared to a crash - or a not working server. - This frequently happens when running the test suite - with many threads on a system with low fs.aio-max-nr! - */ - - fprintf(stderr, - " InnoDB: Warning: Linux Native AIO disabled " - "because os_aio_linux_create_io_ctx() " - "failed. To get rid of this warning you can " - "try increasing system " - "fs.aio-max-nr to 1048576 or larger or " - "setting innodb_use_native_aio = 0 in my.cnf\n"); - srv_use_native_aio = FALSE; - goto skip_native_aio; + we should call it a day and return right away. + We don't care about any leaks because a failure + to initialize the io subsystem means that the + server (or atleast the innodb storage engine) + is not going to startup. */ + return(DB_IO_ERROR); } } - /* Initialize the event array. One event per slot. */ - io_event = static_cast<struct io_event*>( - ut_malloc(n * sizeof(*io_event))); + return(DB_SUCCESS); +} +#endif /* LINUX_NATIVE_AIO */ + +/** Initialise the array */ +dberr_t +AIO::init() +{ + ut_a(!m_slots.empty()); + +#ifdef _WIN32 + ut_a(m_handles == NULL); - memset(io_event, 0x0, sizeof(*io_event) * n); - array->aio_events = io_event; + m_handles = UT_NEW_NOKEY(Handles(m_slots.size())); +#endif /* _WIN32 */ + + if (srv_use_native_aio) { +#ifdef LINUX_NATIVE_AIO + dberr_t err = init_linux_native_aio(); + + if (err != DB_SUCCESS) { + return(err); + } -skip_native_aio: #endif /* LINUX_NATIVE_AIO */ - for (ulint i = 0; i < n; i++) { - os_aio_slot_t* slot; + } - slot = os_aio_array_get_nth_slot(array, i); + return(init_slots()); +} - slot->pos = i; - slot->reserved = FALSE; -#ifdef WIN_ASYNC_IO - slot->handle = CreateEvent(NULL,TRUE, FALSE, NULL); +/** Creates an aio wait array. Note that we return NULL in case of failure. +We don't care about freeing memory here because we assume that a +failure will result in server refusing to start up. +@param[in] id Latch ID +@param[in] n maximum number of pending AIO operations + allowed; n must be divisible by m_n_segments +@param[in] n_segments number of segments in the AIO array +@return own: AIO array, NULL on failure */ +AIO* +AIO::create( + latch_id_t id, + ulint n, + ulint n_segments) +{ + if ((n % n_segments)) { - over = &slot->control; + ib::error() + << "Maximum number of AIO operations must be " + << "divisible by number of segments"; - over->hEvent = slot->handle; + return(NULL); + } - array->handles[i] = over->hEvent; + AIO* array = UT_NEW_NOKEY(AIO(id, n, n_segments)); -#elif defined(LINUX_NATIVE_AIO) - memset(&slot->control, 0x0, sizeof(slot->control)); - slot->n_bytes = 0; - slot->ret = 0; -#endif /* WIN_ASYNC_IO */ + if (array != NULL && array->init() != DB_SUCCESS) { + + UT_DELETE(array); + + array = NULL; } return(array); } -/************************************************************************//** -Frees an aio wait array. */ -static -void -os_aio_array_free( -/*==============*/ - os_aio_array_t*& array) /*!< in, own: array to free */ +/** AIO destructor */ +AIO::~AIO() { #ifdef WIN_ASYNC_IO - ulint i; - for (i = 0; i < array->n_slots; i++) { - os_aio_slot_t* slot = os_aio_array_get_nth_slot(array, i); - CloseHandle(slot->handle); + for (ulint i = 0; i < m_slots.size(); ++i) { + CloseHandle(m_slots[i].handle); } #endif /* WIN_ASYNC_IO */ -#ifdef __WIN__ - ut_free(array->handles); -#endif /* __WIN__ */ - os_mutex_free(array->mutex); - os_event_free(array->not_full); - os_event_free(array->is_empty); +#ifdef _WIN32 + UT_DELETE(m_handles); +#endif /* _WIN32 */ + + mutex_destroy(&m_mutex); + + os_event_destroy(m_not_full); + os_event_destroy(m_is_empty); #if defined(LINUX_NATIVE_AIO) if (srv_use_native_aio) { - ut_free(array->aio_events); - ut_free(array->aio_ctx); + m_events.clear(); + ut_free(m_aio_ctx); } #endif /* LINUX_NATIVE_AIO */ - ut_free(array->slots); - ut_free(array); + for (ulint i = 0; i < m_slots.size(); ++i) { + Slot& slot = m_slots[i]; + + if (slot.compressed_ptr != NULL) { + ut_free(slot.compressed_ptr); + slot.compressed_ptr = NULL; + slot.compressed_page = NULL; + } + } - array = 0; + m_slots.clear(); } -/*********************************************************************** -Initializes the asynchronous io system. Creates one array each for ibuf +/** Initializes the asynchronous io system. Creates one array each for ibuf and log i/o. Also creates one array each for read and write where each -array is divided logically into n_read_segs and n_write_segs +array is divided logically into n_readers and n_writers respectively. The caller must create an i/o handler thread for each segment in these arrays. This function also creates the sync array. -No i/o handler thread needs to be created for that */ -UNIV_INTERN -ibool -os_aio_init( -/*========*/ - ulint n_per_seg, /*<! in: maximum number of pending aio - operations allowed per segment */ - ulint n_read_segs, /*<! in: number of reader threads */ - ulint n_write_segs, /*<! in: number of writer threads */ - ulint n_slots_sync) /*<! in: number of slots in the sync aio - array */ +No i/o handler thread needs to be created for that +@param[in] n_per_seg maximum number of pending aio + operations allowed per segment +@param[in] n_readers number of reader threads +@param[in] n_writers number of writer threads +@param[in] n_slots_sync number of slots in the sync aio array +@return true if the AIO sub-system was started successfully */ +bool +AIO::start( + ulint n_per_seg, + ulint n_readers, + ulint n_writers, + ulint n_slots_sync) { - os_io_init_simple(); - #if defined(LINUX_NATIVE_AIO) /* Check if native aio is supported on this system and tmpfs */ - if (srv_use_native_aio && !os_aio_native_aio_supported()) { + if (srv_use_native_aio && !is_linux_native_aio_supported()) { - ib_logf(IB_LOG_LEVEL_WARN, "Linux Native AIO disabled."); + ib::warn() << "Linux Native AIO disabled."; srv_use_native_aio = FALSE; } @@ -4189,69 +6442,71 @@ os_aio_init( srv_reset_io_thread_op_info(); - os_aio_read_array = os_aio_array_create( - n_read_segs * n_per_seg, n_read_segs); + s_reads = create( + LATCH_ID_OS_AIO_READ_MUTEX, n_readers * n_per_seg, n_readers); - if (os_aio_read_array == NULL) { - return(FALSE); + if (s_reads == NULL) { + return(false); } - ulint start = (srv_read_only_mode) ? 0 : 2; - ulint n_segs = n_read_segs + start; + ulint start = srv_read_only_mode ? 0 : 2; + ulint n_segs = n_readers + start; - /* 0 is the ibuf segment and 1 is the insert buffer segment. */ + /* 0 is the ibuf segment and 1 is the redo log segment. */ for (ulint i = start; i < n_segs; ++i) { ut_a(i < SRV_MAX_N_IO_THREADS); srv_io_thread_function[i] = "read thread"; } - ulint n_segments = n_read_segs; + ulint n_segments = n_readers; if (!srv_read_only_mode) { - os_aio_log_array = os_aio_array_create(n_per_seg, 1); + s_ibuf = create(LATCH_ID_OS_AIO_IBUF_MUTEX, n_per_seg, 1); - if (os_aio_log_array == NULL) { - return(FALSE); + if (s_ibuf == NULL) { + return(false); } ++n_segments; - srv_io_thread_function[1] = "log thread"; + srv_io_thread_function[0] = "insert buffer thread"; - os_aio_ibuf_array = os_aio_array_create(n_per_seg, 1); + s_log = create(LATCH_ID_OS_AIO_LOG_MUTEX, n_per_seg, 1); - if (os_aio_ibuf_array == NULL) { - return(FALSE); + if (s_log == NULL) { + return(false); } ++n_segments; - srv_io_thread_function[0] = "insert buffer thread"; + srv_io_thread_function[1] = "log thread"; - os_aio_write_array = os_aio_array_create( - n_write_segs * n_per_seg, n_write_segs); + } else { + s_ibuf = s_log = NULL; + } - if (os_aio_write_array == NULL) { - return(FALSE); - } + s_writes = create( + LATCH_ID_OS_AIO_WRITE_MUTEX, n_writers * n_per_seg, n_writers); - n_segments += n_write_segs; + if (s_writes == NULL) { + return(false); + } - for (ulint i = start + n_read_segs; i < n_segments; ++i) { - ut_a(i < SRV_MAX_N_IO_THREADS); - srv_io_thread_function[i] = "write thread"; - } + n_segments += n_writers; - ut_ad(n_segments >= 4); - } else { - ut_ad(n_segments > 0); + for (ulint i = start + n_readers; i < n_segments; ++i) { + ut_a(i < SRV_MAX_N_IO_THREADS); + srv_io_thread_function[i] = "write thread"; } - os_aio_sync_array = os_aio_array_create(n_slots_sync, 1); + ut_ad(n_segments >= static_cast<ulint>(srv_read_only_mode ? 2 : 4)); + + s_sync = create(LATCH_ID_OS_AIO_SYNC_MUTEX, n_slots_sync, 1); - if (os_aio_sync_array == NULL) { - return(FALSE); + if (s_sync == NULL) { + + return(false); } os_aio_n_segments = n_segments; @@ -4259,93 +6514,93 @@ os_aio_init( os_aio_validate(); os_aio_segment_wait_events = static_cast<os_event_t*>( - ut_malloc(n_segments * sizeof *os_aio_segment_wait_events)); + ut_zalloc_nokey( + n_segments * sizeof *os_aio_segment_wait_events)); + + if (os_aio_segment_wait_events == NULL) { + + return(false); + } for (ulint i = 0; i < n_segments; ++i) { - os_aio_segment_wait_events[i] = os_event_create(); + os_aio_segment_wait_events[i] = os_event_create(0); } os_last_printout = ut_time(); - return(TRUE); - + return(true); } -/*********************************************************************** -Frees the asynchronous io system. */ -UNIV_INTERN +/** Free the AIO arrays */ void -os_aio_free(void) -/*=============*/ +AIO::shutdown() { - if (os_aio_ibuf_array != 0) { - os_aio_array_free(os_aio_ibuf_array); - } + UT_DELETE(s_ibuf); + s_ibuf = NULL; - if (os_aio_log_array != 0) { - os_aio_array_free(os_aio_log_array); - } + UT_DELETE(s_log); + s_log = NULL; - if (os_aio_write_array != 0) { - os_aio_array_free(os_aio_write_array); - } + UT_DELETE(s_writes); + s_writes = NULL; - if (os_aio_sync_array != 0) { - os_aio_array_free(os_aio_sync_array); - } + UT_DELETE(s_sync); + s_sync = NULL; - os_aio_array_free(os_aio_read_array); + UT_DELETE(s_reads); + s_reads = NULL; +} - for (ulint i = 0; i < os_aio_n_segments; i++) { - os_event_free(os_aio_segment_wait_events[i]); +/** Initializes the asynchronous io system. Creates one array each for ibuf +and log i/o. Also creates one array each for read and write where each +array is divided logically into n_readers and n_writers +respectively. The caller must create an i/o handler thread for each +segment in these arrays. This function also creates the sync array. +No i/o handler thread needs to be created for that +@param[in] n_readers number of reader threads +@param[in] n_writers number of writer threads +@param[in] n_slots_sync number of slots in the sync aio array */ +bool +os_aio_init( + ulint n_readers, + ulint n_writers, + ulint n_slots_sync) +{ + /* Maximum number of pending aio operations allowed per segment */ + ulint limit = 8 * OS_AIO_N_PENDING_IOS_PER_THREAD; + +#ifdef _WIN32 + if (srv_use_native_aio) { + limit = SRV_N_PENDING_IOS_PER_THREAD; } +#endif /* _WIN32 */ - ut_free(os_aio_segment_wait_events); - os_aio_segment_wait_events = 0; - os_aio_n_segments = 0; + return(AIO::start(limit, n_readers, n_writers, n_slots_sync)); } -#ifdef WIN_ASYNC_IO -/************************************************************************//** -Wakes up all async i/o threads in the array in Windows async i/o at -shutdown. */ -static +/** Frees the asynchronous io system. */ void -os_aio_array_wake_win_aio_at_shutdown( -/*==================================*/ - os_aio_array_t* array) /*!< in: aio array */ +os_aio_free() { - ulint i; + AIO::shutdown(); - for (i = 0; i < array->n_slots; i++) { - - SetEvent((array->slots + i)->handle); + for (ulint i = 0; i < os_aio_n_segments; i++) { + os_event_destroy(os_aio_segment_wait_events[i]); } + + ut_free(os_aio_segment_wait_events); + os_aio_segment_wait_events = 0; + os_aio_n_segments = 0; } -#endif -/************************************************************************//** -Wakes up all async i/o threads so that they know to exit themselves in +/** Wakes up all async i/o threads so that they know to exit themselves in shutdown. */ -UNIV_INTERN void -os_aio_wake_all_threads_at_shutdown(void) -/*=====================================*/ +os_aio_wake_all_threads_at_shutdown() { #ifdef WIN_ASYNC_IO - /* This code wakes up all ai/o threads in Windows native aio */ - os_aio_array_wake_win_aio_at_shutdown(os_aio_read_array); - if (os_aio_write_array != 0) { - os_aio_array_wake_win_aio_at_shutdown(os_aio_write_array); - } - - if (os_aio_ibuf_array != 0) { - os_aio_array_wake_win_aio_at_shutdown(os_aio_ibuf_array); - } - if (os_aio_log_array != 0) { - os_aio_array_wake_win_aio_at_shutdown(os_aio_log_array); - } + AIO::wake_at_shutdown(); #elif defined(LINUX_NATIVE_AIO) @@ -4358,359 +6613,341 @@ os_aio_wake_all_threads_at_shutdown(void) return; } +#endif /* !WIN_ASYNC_AIO */ + /* Fall through to simulated AIO handler wakeup if we are not using native AIO. */ -#endif /* !WIN_ASYNC_AIO */ /* This loop wakes up all simulated ai/o threads */ - for (ulint i = 0; i < os_aio_n_segments; i++) { + for (ulint i = 0; i < os_aio_n_segments; ++i) { os_event_set(os_aio_segment_wait_events[i]); } } -/************************************************************************//** -Waits until there are no pending writes in os_aio_write_array. There can +/** Waits until there are no pending writes in AIO::s_writes. There can be other, synchronous, pending writes. */ -UNIV_INTERN void -os_aio_wait_until_no_pending_writes(void) -/*=====================================*/ +os_aio_wait_until_no_pending_writes() { - ut_ad(!srv_read_only_mode); - os_event_wait(os_aio_write_array->is_empty); + AIO::wait_until_no_pending_writes(); } -/**********************************************************************//** -Calculates segment number for a slot. +/** Calculates segment number for a slot. +@param[in] array AIO wait array +@param[in] slot slot in this array @return segment number (which is the number used by, for example, -i/o-handler threads) */ -static + I/O-handler threads) */ ulint -os_aio_get_segment_no_from_slot( -/*============================*/ - os_aio_array_t* array, /*!< in: aio wait array */ - os_aio_slot_t* slot) /*!< in: slot in this array */ +AIO::get_segment_no_from_slot( + const AIO* array, + const Slot* slot) { ulint segment; ulint seg_len; - if (array == os_aio_ibuf_array) { + if (array == s_ibuf) { ut_ad(!srv_read_only_mode); segment = IO_IBUF_SEGMENT; - } else if (array == os_aio_log_array) { + } else if (array == s_log) { ut_ad(!srv_read_only_mode); segment = IO_LOG_SEGMENT; - } else if (array == os_aio_read_array) { - seg_len = os_aio_read_array->n_slots - / os_aio_read_array->n_segments; + } else if (array == s_reads) { + seg_len = s_reads->slots_per_segment(); segment = (srv_read_only_mode ? 0 : 2) + slot->pos / seg_len; } else { - ut_ad(!srv_read_only_mode); - ut_a(array == os_aio_write_array); + ut_a(array == s_writes); - seg_len = os_aio_write_array->n_slots - / os_aio_write_array->n_segments; + seg_len = s_writes->slots_per_segment(); - segment = os_aio_read_array->n_segments + 2 - + slot->pos / seg_len; + segment = s_reads->m_n_segments + + (srv_read_only_mode ? 0 : 2) + slot->pos / seg_len; } return(segment); } -/**********************************************************************//** -Calculates local segment number and aio array from global segment number. -@return local segment number within the aio array */ -static -ulint -os_aio_get_array_and_local_segment( -/*===============================*/ - os_aio_array_t** array, /*!< out: aio wait array */ - ulint global_segment)/*!< in: global segment number */ -{ - ulint segment; - - ut_a(global_segment < os_aio_n_segments); - - if (srv_read_only_mode) { - *array = os_aio_read_array; - - return(global_segment); - } else if (global_segment == IO_IBUF_SEGMENT) { - *array = os_aio_ibuf_array; - segment = 0; - - } else if (global_segment == IO_LOG_SEGMENT) { - *array = os_aio_log_array; - segment = 0; - - } else if (global_segment < os_aio_read_array->n_segments + 2) { - *array = os_aio_read_array; - - segment = global_segment - 2; - } else { - *array = os_aio_write_array; - - segment = global_segment - (os_aio_read_array->n_segments + 2); - } - - return(segment); -} - -/*******************************************************************//** -Requests for a slot in the aio array. If no slot is available, waits until +/** Requests for a slot in the aio array. If no slot is available, waits until not_full-event becomes signaled. -@return pointer to slot */ -static -os_aio_slot_t* -os_aio_array_reserve_slot( -/*======================*/ - ulint type, /*!< in: OS_FILE_READ or OS_FILE_WRITE */ - ulint is_log, /*!< in: 1 is OS_FILE_LOG or 0 */ - os_aio_array_t* array, /*!< in: aio array */ - fil_node_t* message1,/*!< in: message to be passed along with - the aio operation */ - void* message2,/*!< in: message to be passed along with - the aio operation */ - os_file_t file, /*!< in: file handle */ - const char* name, /*!< in: name of the file or path as a - null-terminated string */ - void* buf, /*!< in: buffer where to read or from which - to write */ - os_offset_t offset, /*!< in: file offset */ - ulint len, /*!< in: length of the block to read or write */ - ulint page_size, /*!< in: page size in bytes */ + +@param[in,out] type IO context +@param[in,out] m1 message to be passed along with the AIO + operation +@param[in,out] m2 message to be passed along with the AIO + operation +@param[in] file file handle +@param[in] name name of the file or path as a NUL-terminated + string +@param[in,out] buf buffer where to read or from which to write +@param[in] offset file offset, where to read from or start writing +@param[in] len length of the block to read or write +@return pointer to slot */ +Slot* +AIO::reserve_slot( + IORequest& type, + fil_node_t* m1, + void* m2, + os_file_t file, + const char* name, + void* buf, + os_offset_t offset, + ulint len, ulint* write_size)/*!< in/out: Actual write size initialized after fist successfull trim operation for this page and if initialized we do not trim again if actual page size does not decrease. */ { - os_aio_slot_t* slot = NULL; -#ifdef WIN_ASYNC_IO - OVERLAPPED* control; - -#elif defined(LINUX_NATIVE_AIO) - - struct iocb* iocb; - off_t aio_offset; - -#endif /* WIN_ASYNC_IO */ - ulint i; - ulint counter; - ulint slots_per_seg; - ulint local_seg; - #ifdef WIN_ASYNC_IO ut_a((len & 0xFFFFFFFFUL) == len); #endif /* WIN_ASYNC_IO */ /* No need of a mutex. Only reading constant fields */ - slots_per_seg = array->n_slots / array->n_segments; + ulint slots_per_seg; + + ut_ad(type.validate()); + + slots_per_seg = slots_per_segment(); /* We attempt to keep adjacent blocks in the same local segment. This can help in merging IO requests when we are doing simulated AIO */ - local_seg = (offset >> (UNIV_PAGE_SIZE_SHIFT + 6)) - % array->n_segments; + ulint local_seg; + + local_seg = (offset >> (UNIV_PAGE_SIZE_SHIFT + 6)) % m_n_segments; -loop: - os_mutex_enter(array->mutex); + for (;;) { + + acquire(); + + if (m_n_reserved != m_slots.size()) { + break; + } - if (array->n_reserved == array->n_slots) { - os_mutex_exit(array->mutex); + release(); if (!srv_use_native_aio) { - /* If the handler threads are suspended, wake them - so that we get more slots */ + /* If the handler threads are suspended, + wake them so that we get more slots */ os_aio_simulated_wake_handler_threads(); } - os_event_wait(array->not_full); - - goto loop; + os_event_wait(m_not_full); } + ulint counter = 0; + Slot* slot = NULL; + /* We start our search for an available slot from our preferred local segment and do a full scan of the array. We are guaranteed to find a slot in full scan. */ - for (i = local_seg * slots_per_seg, counter = 0; - counter < array->n_slots; - i++, counter++) { + for (ulint i = local_seg * slots_per_seg; + counter < m_slots.size(); + ++i, ++counter) { - i %= array->n_slots; + i %= m_slots.size(); - slot = os_aio_array_get_nth_slot(array, i); + slot = at(i); - if (slot->reserved == FALSE) { - goto found; + if (slot->is_reserved == false) { + break; } } /* We MUST always be able to get hold of a reserved slot. */ - ut_error; + ut_a(counter < m_slots.size()); + + ut_a(slot->is_reserved == false); -found: - ut_a(slot->reserved == FALSE); - array->n_reserved++; + ++m_n_reserved; - if (array->n_reserved == 1) { - os_event_reset(array->is_empty); + if (m_n_reserved == 1) { + os_event_reset(m_is_empty); } - if (array->n_reserved == array->n_slots) { - os_event_reset(array->not_full); + if (m_n_reserved == m_slots.size()) { + os_event_reset(m_not_full); } - slot->reserved = TRUE; + slot->is_reserved = true; slot->reservation_time = ut_time(); - slot->message1 = message1; - slot->message2 = message2; + slot->m1 = m1; + slot->m2 = m2; slot->file = file; slot->name = name; - slot->len = len; +#ifdef _WIN32 + slot->len = static_cast<DWORD>(len); +#else + slot->len = static_cast<ulint>(len); +#endif /* _WIN32 */ slot->type = type; + slot->buf = static_cast<byte*>(buf); + slot->ptr = slot->buf; slot->offset = offset; - slot->io_already_done = FALSE; + slot->err = DB_SUCCESS; slot->write_size = write_size; - slot->is_log = is_log; - slot->page_size = page_size; - - if (message1) { - slot->file_block_size = fil_node_get_block_size(message1); - } - + slot->is_log = type.is_log(); + slot->original_len = static_cast<uint32>(len); + slot->io_already_done = false; slot->buf = static_cast<byte*>(buf); -#ifdef WIN_ASYNC_IO - control = &slot->control; - control->Offset = (DWORD) offset & 0xFFFFFFFF; - control->OffsetHigh = (DWORD) (offset >> 32); - ResetEvent(slot->handle); + if (srv_use_native_aio + && offset > 0 + && type.is_write() + && type.is_compressed()) { -#elif defined(LINUX_NATIVE_AIO) + ut_ad(!type.is_log()); - /* If we are not using native AIO skip this part. */ - if (!srv_use_native_aio) { - goto skip_native_aio; - } + release(); - /* Check if we are dealing with 64 bit arch. - If not then make sure that offset fits in 32 bits. */ - aio_offset = (off_t) offset; + ulint compressed_len = len; - ut_a(sizeof(aio_offset) >= sizeof(offset) - || ((os_offset_t) aio_offset) == offset); + ulint old_compressed_len; - iocb = &slot->control; + old_compressed_len = mach_read_from_2( + slot->buf + FIL_PAGE_COMPRESS_SIZE_V1); - if (type == OS_FILE_READ) { - io_prep_pread(iocb, file, buf, len, aio_offset); - } else { - ut_a(type == OS_FILE_WRITE); - io_prep_pwrite(iocb, file, buf, len, aio_offset); - } - - iocb->data = (void*) slot; - slot->n_bytes = 0; - slot->ret = 0; - -skip_native_aio: -#endif /* LINUX_NATIVE_AIO */ - os_mutex_exit(array->mutex); + if (old_compressed_len > 0) { + old_compressed_len = ut_calc_align( + old_compressed_len + FIL_PAGE_DATA, + slot->type.block_size()); + } - return(slot); -} + byte* ptr; + + ptr = os_file_compress_page( + slot->type.compression_algorithm(), + slot->type.block_size(), + slot->buf, + slot->len, + slot->compressed_page, + &compressed_len); + + if (ptr != buf) { + /* Set new compressed size to uncompressed page. */ + memcpy(slot->buf + FIL_PAGE_COMPRESS_SIZE_V1, + slot->compressed_page + + FIL_PAGE_COMPRESS_SIZE_V1, 2); +#ifdef _WIN32 + slot->len = static_cast<DWORD>(compressed_len); +#else + slot->len = static_cast<ulint>(compressed_len); +#endif /* _WIN32 */ + slot->buf = slot->compressed_page; + slot->ptr = slot->buf; -/*******************************************************************//** -Frees a slot in the aio array. */ -static -void -os_aio_array_free_slot( -/*===================*/ - os_aio_array_t* array, /*!< in: aio array */ - os_aio_slot_t* slot) /*!< in: pointer to slot */ -{ - os_mutex_enter(array->mutex); + if (old_compressed_len > 0 + && compressed_len >= old_compressed_len) { - ut_ad(slot->reserved); + ut_ad(old_compressed_len <= UNIV_PAGE_SIZE); - slot->reserved = FALSE; + slot->skip_punch_hole = true; - array->n_reserved--; + } else { + slot->skip_punch_hole = false; + } - if (array->n_reserved == array->n_slots - 1) { - os_event_set(array->not_full); - } + } else { + slot->skip_punch_hole = false; + } - if (array->n_reserved == 0) { - os_event_set(array->is_empty); + acquire(); } #ifdef WIN_ASYNC_IO + { + OVERLAPPED* control; - ResetEvent(slot->handle); + control = &slot->control; + control->Offset = (DWORD) offset & 0xFFFFFFFF; + control->OffsetHigh = (DWORD) (offset >> 32); + ResetEvent(slot->handle); + } #elif defined(LINUX_NATIVE_AIO) + /* If we are not using native AIO skip this part. */ if (srv_use_native_aio) { - memset(&slot->control, 0x0, sizeof(slot->control)); + + off_t aio_offset; + + /* Check if we are dealing with 64 bit arch. + If not then make sure that offset fits in 32 bits. */ + aio_offset = (off_t) offset; + + ut_a(sizeof(aio_offset) >= sizeof(offset) + || ((os_offset_t) aio_offset) == offset); + + struct iocb* iocb = &slot->control; + + if (type.is_read()) { + + io_prep_pread( + iocb, file, slot->ptr, slot->len, aio_offset); + } else { + ut_ad(type.is_write()); + + io_prep_pwrite( + iocb, file, slot->ptr, slot->len, aio_offset); + } + + iocb->data = slot; + slot->n_bytes = 0; slot->ret = 0; - /*fprintf(stderr, "Freed up Linux native slot.\n");*/ - } else { - /* These fields should not be used if we are not - using native AIO. */ - ut_ad(slot->n_bytes == 0); - ut_ad(slot->ret == 0); } +#endif /* LINUX_NATIVE_AIO */ -#endif - os_mutex_exit(array->mutex); + release(); + + return(slot); } -/**********************************************************************//** -Wakes up a simulated aio i/o-handler thread if it has something to do. */ -static +/** Wakes up a simulated aio i/o-handler thread if it has something to do. +@param[in] global_segment The number of the segment in the AIO arrays */ void -os_aio_simulated_wake_handler_thread( -/*=================================*/ - ulint global_segment) /*!< in: the number of the segment in the aio - arrays */ +AIO::wake_simulated_handler_thread(ulint global_segment) { - os_aio_array_t* array; - ulint segment; - ut_ad(!srv_use_native_aio); - segment = os_aio_get_array_and_local_segment(&array, global_segment); + AIO* array; + ulint segment = get_array_and_local_segment(&array, global_segment); - ulint n = array->n_slots / array->n_segments; + array->wake_simulated_handler_thread(global_segment, segment); +} - segment *= n; +/** Wakes up a simulated AIO I/O-handler thread if it has something to do +for a local segment in the AIO array. +@param[in] global_segment The number of the segment in the AIO arrays +@param[in] segment The local segment in the AIO array */ +void +AIO::wake_simulated_handler_thread(ulint global_segment, ulint segment) +{ + ut_ad(!srv_use_native_aio); + + ulint n = slots_per_segment(); + ulint offset = segment * n; /* Look through n slots after the segment * n'th slot */ - os_mutex_enter(array->mutex); + acquire(); - for (ulint i = 0; i < n; ++i) { - const os_aio_slot_t* slot; + const Slot* slot = at(offset); - slot = os_aio_array_get_nth_slot(array, segment + i); + for (ulint i = 0; i < n; ++i, ++slot) { - if (slot->reserved) { + if (slot->is_reserved) { /* Found an i/o request */ - os_mutex_exit(array->mutex); + release(); os_event_t event; @@ -4722,15 +6959,12 @@ os_aio_simulated_wake_handler_thread( } } - os_mutex_exit(array->mutex); + release(); } -/**********************************************************************//** -Wakes up simulated aio i/o-handler threads if they have something to do. */ -UNIV_INTERN +/** Wakes up simulated aio i/o-handler threads if they have something to do. */ void -os_aio_simulated_wake_handler_threads(void) -/*=======================================*/ +os_aio_simulated_wake_handler_threads() { if (srv_use_native_aio) { /* We do not use simulated aio: do nothing */ @@ -4738,168 +6972,286 @@ os_aio_simulated_wake_handler_threads(void) return; } - os_aio_recommend_sleep_for_read_threads = FALSE; + os_aio_recommend_sleep_for_read_threads = false; for (ulint i = 0; i < os_aio_n_segments; i++) { - os_aio_simulated_wake_handler_thread(i); + AIO::wake_simulated_handler_thread(i); } } -/**********************************************************************//** -This function can be called if one wants to post a batch of reads and -prefers an i/o-handler thread to handle them all at once later. You must -call os_aio_simulated_wake_handler_threads later to ensure the threads -are not left sleeping! */ -UNIV_INTERN -void -os_aio_simulated_put_read_threads_to_sleep(void) -/*============================================*/ +/** Select the IO slot array +@param[in] type Type of IO, READ or WRITE +@param[in] read_only true if running in read-only mode +@param[in] mode IO mode +@return slot array or NULL if invalid mode specified */ +AIO* +AIO::select_slot_array(IORequest& type, bool read_only, ulint mode) { + AIO* array; -/* The idea of putting background IO threads to sleep is only for -Windows when using simulated AIO. Windows XP seems to schedule -background threads too eagerly to allow for coalescing during -readahead requests. */ -#ifdef __WIN__ - os_aio_array_t* array; + ut_ad(type.validate()); - if (srv_use_native_aio) { - /* We do not use simulated aio: do nothing */ + switch (mode) { + case OS_AIO_NORMAL: - return; - } + array = type.is_read() ? AIO::s_reads : AIO::s_writes; + break; - os_aio_recommend_sleep_for_read_threads = TRUE; + case OS_AIO_IBUF: + ut_ad(type.is_read()); - for (ulint i = 0; i < os_aio_n_segments; i++) { - os_aio_get_array_and_local_segment(&array, i); + /* Reduce probability of deadlock bugs in connection with ibuf: + do not let the ibuf i/o handler sleep */ - if (array == os_aio_read_array) { + type.clear_do_not_wake(); - os_event_reset(os_aio_segment_wait_events[i]); - } + array = read_only ? AIO::s_reads : AIO::s_ibuf; + break; + + case OS_AIO_LOG: + + array = read_only ? AIO::s_reads : AIO::s_log; + break; + + case OS_AIO_SYNC: + + array = AIO::s_sync; +#if defined(LINUX_NATIVE_AIO) + /* In Linux native AIO we don't use sync IO array. */ + ut_a(!srv_use_native_aio); +#endif /* LINUX_NATIVE_AIO */ + break; + + default: + ut_error; + array = NULL; /* Eliminate compiler warning */ } -#endif /* __WIN__ */ + + return(array); } -#if defined(LINUX_NATIVE_AIO) -/*******************************************************************//** -Dispatch an AIO request to the kernel. -@return TRUE on success. */ +#ifdef WIN_ASYNC_IO +/** This function is only used in Windows asynchronous i/o. +Waits for an aio operation to complete. This function is used to wait the +for completed requests. The aio array of pending requests is divided +into segments. The thread specifies which segment or slot it wants to wait +for. NOTE: this function will also take care of freeing the aio slot, +therefore no other thread is allowed to do the freeing! +@param[in] segment The number of the segment in the aio arrays to + wait for; segment 0 is the ibuf I/O thread, + segment 1 the log I/O thread, then follow the + non-ibuf read threads, and as the last are the + non-ibuf write threads; if this is + ULINT_UNDEFINED, then it means that sync AIO + is used, and this parameter is ignored +@param[in] pos this parameter is used only in sync AIO: + wait for the aio slot at this position +@param[out] m1 the messages passed with the AIO request; note + that also in the case where the AIO operation + failed, these output parameters are valid and + can be used to restart the operation, + for example +@param[out] m2 callback message +@param[out] type OS_FILE_WRITE or ..._READ +@return DB_SUCCESS or error code */ static -ibool -os_aio_linux_dispatch( -/*==================*/ - os_aio_array_t* array, /*!< in: io request array. */ - os_aio_slot_t* slot) /*!< in: an already reserved slot. */ +dberr_t +os_aio_windows_handler( + ulint segment, + ulint pos, + fil_node_t** m1, + void** m2, + IORequest* type) { - int ret; - ulint io_ctx_index; - struct iocb* iocb; + Slot* slot; + dberr_t err; + AIO* array; + ulint orig_seg = segment; - ut_ad(slot != NULL); - ut_ad(array); + if (segment == ULINT_UNDEFINED) { + segment = 0; + array = AIO::sync_array(); + } else { + segment = AIO::get_array_and_local_segment(&array, segment); + } - ut_a(slot->reserved); + /* NOTE! We only access constant fields in os_aio_array. Therefore + we do not have to acquire the protecting mutex yet */ - /* Find out what we are going to work with. - The iocb struct is directly in the slot. - The io_context is one per segment. */ + ut_ad(os_aio_validate_skip()); - iocb = &slot->control; - io_ctx_index = (slot->pos * array->n_segments) / array->n_slots; + if (array == AIO::sync_array()) { - ret = io_submit(array->aio_ctx[io_ctx_index], 1, &iocb); + WaitForSingleObject(array->at(pos)->handle, INFINITE); -#if defined(UNIV_AIO_DEBUG) - fprintf(stderr, - "io_submit[%c] ret[%d]: slot[%p] ctx[%p] seg[%lu]\n", - (slot->type == OS_FILE_WRITE) ? 'w' : 'r', ret, slot, - array->aio_ctx[io_ctx_index], (ulong) io_ctx_index); -#endif + } else { + if (orig_seg != ULINT_UNDEFINED) { + srv_set_io_thread_op_info(orig_seg, "wait Windows aio"); + } - /* io_submit returns number of successfully - queued requests or -errno. */ - if (UNIV_UNLIKELY(ret != 1)) { - errno = -ret; - return(FALSE); + pos = WaitForMultipleObjects( + (DWORD) array->slots_per_segment(), + array->handles(segment), + FALSE, INFINITE); } - return(TRUE); -} -#endif /* LINUX_NATIVE_AIO */ + array->acquire(); + + if (srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS + && array->is_empty() + && !buf_page_cleaner_is_active) { + + *m1 = NULL; + *m2 = NULL; + + array->release(); + + return(DB_SUCCESS); + } + + ulint n = array->slots_per_segment(); + + ut_a(pos >= WAIT_OBJECT_0 && pos <= WAIT_OBJECT_0 + n); + + slot = array->at(pos + segment * n); + + ut_a(slot->is_reserved); + + if (orig_seg != ULINT_UNDEFINED) { + srv_set_io_thread_op_info( + orig_seg, "get windows aio return value"); + } + + BOOL ret; + + ret = GetOverlappedResult( + slot->file, &slot->control, &slot->n_bytes, TRUE); + + *m1 = slot->m1; + *m2 = slot->m2; + + *type = slot->type; + + BOOL retry = FALSE; + + if (ret && slot->n_bytes == slot->len) { + + err = DB_SUCCESS; + + } else if (os_file_handle_error(slot->name, "Windows aio")) { + + retry = true; + + } else { + + err = DB_IO_ERROR; + } + + array->release(); + + if (retry) { + /* Retry failed read/write operation synchronously. + No need to hold array->m_mutex. */ + +#ifdef UNIV_PFS_IO + /* This read/write does not go through os_file_read + and os_file_write APIs, need to register with + performance schema explicitly here. */ + struct PSI_file_locker* locker = NULL; + + register_pfs_file_io_begin( + locker, slot->file, slot->len, + slot->type.is_write() + ? PSI_FILE_WRITE : PSI_FILE_READ, __FILE__, __LINE__); +#endif /* UNIV_PFS_IO */ + + ut_a((slot->len & 0xFFFFFFFFUL) == slot->len); + + ssize_t n_bytes = SyncFileIO::execute(slot); + +#ifdef UNIV_PFS_IO + register_pfs_file_io_end(locker, slot->len); +#endif /* UNIV_PFS_IO */ + + if (n_bytes < 0 && GetLastError() == ERROR_IO_PENDING) { + /* AIO was queued successfully! + We want a synchronous I/O operation on a + file where we also use async I/O: in Windows + we must use the same wait mechanism as for + async I/O */ + + BOOL ret; + + ret = GetOverlappedResult( + slot->file, &slot->control, &slot->n_bytes, + TRUE); + + n_bytes = ret ? slot->n_bytes : -1; + } + + err = (n_bytes == slot->len) ? DB_SUCCESS : DB_IO_ERROR; + } + + if (err == DB_SUCCESS) { + err = AIOHandler::post_io_processing(slot); + } + array->release_with_mutex(slot); -/*******************************************************************//** + return(err); +} +#endif /* WIN_ASYNC_IO */ + +/** NOTE! Use the corresponding macro os_aio(), not directly this function! Requests an asynchronous i/o operation. -@return TRUE if request was queued successfully, FALSE if fail */ -UNIV_INTERN -ibool +@param[in] type IO request context +@param[in] mode IO mode +@param[in] name Name of the file or path as NUL terminated + string +@param[in] file Open file handle +@param[out] buf buffer where to read +@param[in] offset file offset where to read +@param[in] n number of bytes to read +@param[in] read_only if true read only mode checks are enforced +@param[in,out] m1 Message for the AIO handler, (can be used to + identify a completed AIO operation); ignored + if mode is OS_AIO_SYNC +@param[in,out] m2 message for the AIO handler (can be used to + identify a completed AIO operation); ignored + if mode is OS_AIO_SYNC +@return DB_SUCCESS or error code */ +dberr_t os_aio_func( -/*========*/ - ulint type, /*!< in: OS_FILE_READ or OS_FILE_WRITE */ - ulint is_log, /*!< in: 1 is OS_FILE_LOG or 0 */ - ulint mode, /*!< in: OS_AIO_NORMAL, ..., possibly ORed - to OS_AIO_SIMULATED_WAKE_LATER: the - last flag advises this function not to wake - i/o-handler threads, but the caller will - do the waking explicitly later, in this - way the caller can post several requests in - a batch; NOTE that the batch must not be - so big that it exhausts the slots in aio - arrays! NOTE that a simulated batch - may introduce hidden chances of deadlocks, - because i/os are not actually handled until - all have been posted: use with great - caution! */ - const char* name, /*!< in: name of the file or path as a - null-terminated string */ - os_file_t file, /*!< in: handle to a file */ - void* buf, /*!< in: buffer where to read or from which - to write */ - os_offset_t offset, /*!< in: file offset where to read or write */ - ulint n, /*!< in: number of bytes to read or write */ - ulint page_size, /*!< in: page size in bytes */ - fil_node_t* message1,/*!< in: message for the aio handler - (can be used to identify a completed - aio operation); ignored if mode is - OS_AIO_SYNC */ - void* message2,/*!< in: message for the aio handler - (can be used to identify a completed - aio operation); ignored if mode is - OS_AIO_SYNC */ + IORequest& type, + ulint mode, + const char* name, + os_file_t file, + void* buf, + os_offset_t offset, + ulint n, + bool read_only, + fil_node_t* m1, + void* m2, ulint* write_size)/*!< in/out: Actual write size initialized after fist successfull trim operation for this page and if initialized we do not trim again if actual page size does not decrease. */ { - os_aio_array_t* array; - os_aio_slot_t* slot; #ifdef WIN_ASYNC_IO void* buffer = NULL; - ibool retval; - BOOL ret = TRUE; - DWORD len = (DWORD) n; - struct fil_node_t* dummy_mess1; - void* dummy_mess2; - ulint dummy_type; + BOOL ret = TRUE; #endif /* WIN_ASYNC_IO */ - ulint wake_later; - ut_ad(buf); ut_ad(n > 0); - ut_ad(n % OS_FILE_LOG_BLOCK_SIZE == 0); - ut_ad(offset % OS_FILE_LOG_BLOCK_SIZE == 0); + ut_ad((n % OS_FILE_LOG_BLOCK_SIZE) == 0); + ut_ad((offset % OS_FILE_LOG_BLOCK_SIZE) == 0); ut_ad(os_aio_validate_skip()); + #ifdef WIN_ASYNC_IO ut_ad((n & 0xFFFFFFFFUL) == n); -#endif - - - wake_later = mode & OS_AIO_SIMULATED_WAKE_LATER; - mode = mode & (~OS_AIO_SIMULATED_WAKE_LATER); +#endif /* WIN_ASYNC_IO */ DBUG_EXECUTE_IF("ib_os_aio_func_io_failure_28", mode = OS_AIO_SYNC; os_has_said_disk_full = FALSE;); @@ -4909,8 +7261,6 @@ os_aio_func( && !srv_use_native_aio #endif /* WIN_ASYNC_IO */ ) { - ibool ret; - /* This is actually an ordinary synchronous read or write: no need to use an i/o-handler thread. NOTE that if we use Windows async i/o, Windows does not allow us to use @@ -4924,114 +7274,66 @@ os_aio_func( os_file_write(). Instead, we should use os_file_read_func() and os_file_write_func() */ - if (type == OS_FILE_READ) { - ret = os_file_read_func(file, buf, offset, n); - } else { + if (type.is_read()) { + return(os_file_read_func(type, file, buf, offset, n)); + } - ut_ad(!srv_read_only_mode); - ut_a(type == OS_FILE_WRITE); + ut_ad(type.is_write()); - ret = os_file_write_func(name, file, buf, offset, n); + return(os_file_write_func(type, name, file, buf, offset, n)); + } - DBUG_EXECUTE_IF("ib_os_aio_func_io_failure_28", - os_has_said_disk_full = FALSE; ret = 0; errno = 28;); +try_again: - if (!ret) { - os_file_handle_error_cond_exit(name, "os_file_write_func", TRUE, FALSE, - __FILE__, __LINE__); - } - } + AIO* array; - return ret; - } + array = AIO::select_slot_array(type, read_only, mode); -try_again: - switch (mode) { - case OS_AIO_NORMAL: - if (type == OS_FILE_READ) { - array = os_aio_read_array; - } else { - ut_ad(!srv_read_only_mode); - array = os_aio_write_array; - } - break; - case OS_AIO_IBUF: - ut_ad(type == OS_FILE_READ); - /* Reduce probability of deadlock bugs in connection with ibuf: - do not let the ibuf i/o handler sleep */ + Slot* slot; - wake_later = FALSE; + slot = array->reserve_slot(type, m1, m2, file, name, buf, offset, n, write_size); - if (srv_read_only_mode) { - array = os_aio_read_array; - } else { - array = os_aio_ibuf_array; - } - break; - case OS_AIO_LOG: - if (srv_read_only_mode) { - array = os_aio_read_array; - } else { - array = os_aio_log_array; - } - break; - case OS_AIO_SYNC: - array = os_aio_sync_array; -#if defined(LINUX_NATIVE_AIO) - /* In Linux native AIO we don't use sync IO array. */ - ut_a(!srv_use_native_aio); -#endif /* LINUX_NATIVE_AIO */ - break; - default: - ut_error; - array = NULL; /* Eliminate compiler warning */ - } + if (type.is_read()) { - slot = os_aio_array_reserve_slot(type, is_log, array, message1, message2, file, - name, buf, offset, n, page_size, write_size); - if (type == OS_FILE_READ) { if (srv_use_native_aio) { - os_n_file_reads++; + + ++os_n_file_reads; + os_bytes_read_since_printout += n; #ifdef WIN_ASYNC_IO - ret = ReadFile(file, buf, (DWORD) n, &len, - &(slot->control)); + ret = ReadFile( + file, slot->ptr, slot->len, + &slot->n_bytes, &slot->control); #elif defined(LINUX_NATIVE_AIO) - if (!os_aio_linux_dispatch(array, slot)) { + if (!array->linux_dispatch(slot)) { goto err_exit; } #endif /* WIN_ASYNC_IO */ - } else { - if (!wake_later) { - os_aio_simulated_wake_handler_thread( - os_aio_get_segment_no_from_slot( - array, slot)); - } + } else if (type.is_wake()) { + AIO::wake_simulated_handler_thread( + AIO::get_segment_no_from_slot(array, slot)); } - } else if (type == OS_FILE_WRITE) { - ut_ad(!srv_read_only_mode); + } else if (type.is_write()) { + if (srv_use_native_aio) { - os_n_file_writes++; -#ifdef WIN_ASYNC_IO + ++os_n_file_writes; - n = slot->len; - buffer = buf; - ret = WriteFile(file, buffer, (DWORD) n, &len, - &(slot->control)); +#ifdef WIN_ASYNC_IO + ret = WriteFile( + file, slot->ptr, slot->len, + &slot->n_bytes, &slot->control); #elif defined(LINUX_NATIVE_AIO) - if (!os_aio_linux_dispatch(array, slot)) { + if (!array->linux_dispatch(slot)) { goto err_exit; } #endif /* WIN_ASYNC_IO */ - } else { - if (!wake_later) { - os_aio_simulated_wake_handler_thread( - os_aio_get_segment_no_from_slot( - array, slot)); - } + + } else if (type.is_wake()) { + AIO::wake_simulated_handler_thread( + AIO::get_segment_no_from_slot(array, slot)); } } else { ut_error; @@ -5039,991 +7341,753 @@ try_again: #ifdef WIN_ASYNC_IO if (srv_use_native_aio) { - if ((ret && len == n) - || (!ret && GetLastError() == ERROR_IO_PENDING)) { + if ((ret && slot->len == slot->n_bytes) + || (!ret && GetLastError() == ERROR_IO_PENDING)) { /* aio was queued successfully! */ if (mode == OS_AIO_SYNC) { + IORequest dummy_type; + void* dummy_mess2; + struct fil_node_t* dummy_mess1; + /* We want a synchronous i/o operation on a file where we also use async i/o: in Windows we must use the same wait mechanism as for async i/o */ - retval = os_aio_windows_handle( + return(os_aio_windows_handler( ULINT_UNDEFINED, slot->pos, &dummy_mess1, &dummy_mess2, - &dummy_type); - - return(retval); + &dummy_type)); } - return(TRUE); + return(DB_SUCCESS); } goto err_exit; } #endif /* WIN_ASYNC_IO */ - /* aio was queued successfully! */ - return(TRUE); + + /* AIO request was queued successfully! */ + return(DB_SUCCESS); #if defined LINUX_NATIVE_AIO || defined WIN_ASYNC_IO err_exit: #endif /* LINUX_NATIVE_AIO || WIN_ASYNC_IO */ - os_aio_array_free_slot(array, slot); + + array->release_with_mutex(slot); if (os_file_handle_error( - name,type == OS_FILE_READ ? "aio read" : "aio write", __FILE__, __LINE__)) { + name, type.is_read() ? "aio read" : "aio write")) { goto try_again; } - return(FALSE); + return(DB_IO_ERROR); } -#ifdef WIN_ASYNC_IO -/**********************************************************************//** -This function is only used in Windows asynchronous i/o. -Waits for an aio operation to complete. This function is used to wait the -for completed requests. The aio array of pending requests is divided -into segments. The thread specifies which segment or slot it wants to wait -for. NOTE: this function will also take care of freeing the aio slot, -therefore no other thread is allowed to do the freeing! -@return TRUE if the aio operation succeeded */ -UNIV_INTERN -ibool -os_aio_windows_handle( -/*==================*/ - ulint segment, /*!< in: the number of the segment in the aio - arrays to wait for; segment 0 is the ibuf - i/o thread, segment 1 the log i/o thread, - then follow the non-ibuf read threads, and as - the last are the non-ibuf write threads; if - this is ULINT_UNDEFINED, then it means that - sync aio is used, and this parameter is - ignored */ - ulint pos, /*!< this parameter is used only in sync aio: - wait for the aio slot at this position */ - fil_node_t**message1, /*!< out: the messages passed with the aio - request; note that also in the case where - the aio operation failed, these output - parameters are valid and can be used to - restart the operation, for example */ - void** message2, - ulint* type) /*!< out: OS_FILE_WRITE or ..._READ */ -{ - ulint orig_seg = segment; - os_aio_array_t* array; - os_aio_slot_t* slot; - ulint n; - ulint i; - ibool ret_val; - BOOL ret; - DWORD len; - BOOL retry = FALSE; +/** Simulated AIO handler for reaping IO requests */ +class SimulatedAIOHandler { + +public: + + /** Constructor + @param[in,out] array The AIO array + @param[in] segment Local segment in the array */ + SimulatedAIOHandler(AIO* array, ulint segment) + : + m_oldest(), + m_n_elems(), + m_lowest_offset(IB_UINT64_MAX), + m_array(array), + m_n_slots(), + m_segment(segment), + m_ptr(), + m_buf() + { + ut_ad(m_segment < 100); - if (segment == ULINT_UNDEFINED) { - segment = 0; - array = os_aio_sync_array; - } else { - segment = os_aio_get_array_and_local_segment(&array, segment); + m_slots.resize(OS_AIO_MERGE_N_CONSECUTIVE); } - /* NOTE! We only access constant fields in os_aio_array. Therefore - we do not have to acquire the protecting mutex yet */ - - ut_ad(os_aio_validate_skip()); - ut_ad(segment < array->n_segments); - - n = array->n_slots / array->n_segments; - - if (array == os_aio_sync_array) { - - WaitForSingleObject( - os_aio_array_get_nth_slot(array, pos)->handle, - INFINITE); - - i = pos; - - } else { - if (orig_seg != ULINT_UNDEFINED) { - srv_set_io_thread_op_info(orig_seg, "wait Windows aio"); + /** Destructor */ + ~SimulatedAIOHandler() + { + if (m_ptr != NULL) { + ut_free(m_ptr); } - - i = WaitForMultipleObjects( - (DWORD) n, array->handles + segment * n, - FALSE, INFINITE); } - os_mutex_enter(array->mutex); + /** Reset the state of the handler + @param[in] n_slots Number of pending AIO operations supported */ + void init(ulint n_slots) + { + m_oldest = 0; + m_n_elems = 0; + m_n_slots = n_slots; + m_lowest_offset = IB_UINT64_MAX; + + if (m_ptr != NULL) { + ut_free(m_ptr); + m_ptr = m_buf = NULL; + } - if (srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS - && array->n_reserved == 0) { - *message1 = NULL; - *message2 = NULL; - os_mutex_exit(array->mutex); - return(TRUE); + m_slots[0] = NULL; } - ut_a(i >= WAIT_OBJECT_0 && i <= WAIT_OBJECT_0 + n); + /** Check if there is a slot for which the i/o has already been done + @param[out] n_reserved Number of reserved slots + @return the first completed slot that is found. */ + Slot* check_completed(ulint* n_reserved) + { + ulint offset = m_segment * m_n_slots; - slot = os_aio_array_get_nth_slot(array, i + segment * n); + *n_reserved = 0; - ut_a(slot->reserved); + Slot* slot; - if (orig_seg != ULINT_UNDEFINED) { - srv_set_io_thread_op_info( - orig_seg, "get windows aio return value"); - } + slot = m_array->at(offset); - ret = GetOverlappedResult(slot->file, &(slot->control), &len, TRUE); + for (ulint i = 0; i < m_n_slots; ++i, ++slot) { - *message1 = slot->message1; - *message2 = slot->message2; + if (slot->is_reserved) { - *type = slot->type; + if (slot->io_already_done) { - if (ret && len == slot->len) { + ut_a(slot->is_reserved); - ret_val = TRUE; - } else if (!ret || (len != slot->len)) { + return(slot); + } - if (!ret) { - if (os_file_handle_error(slot->name, "Windows aio", __FILE__, __LINE__)) { - retry = TRUE; - } else { - ret_val = FALSE; + ++*n_reserved; } - } else { - retry = TRUE; } - } else { - ret_val = FALSE; + return(NULL); } - os_mutex_exit(array->mutex); - - if (retry) { - /* retry failed read/write operation synchronously. - No need to hold array->mutex. */ - -#ifdef UNIV_PFS_IO - /* This read/write does not go through os_file_read - and os_file_write APIs, need to register with - performance schema explicitly here. */ - struct PSI_file_locker* locker = NULL; - register_pfs_file_io_begin(locker, slot->file, slot->len, - (slot->type == OS_FILE_WRITE) - ? PSI_FILE_WRITE - : PSI_FILE_READ, - __FILE__, __LINE__); -#endif - - ut_a((slot->len & 0xFFFFFFFFUL) == slot->len); - - switch (slot->type) { - case OS_FILE_WRITE: - ret = WriteFile(slot->file, slot->buf, - (DWORD) slot->len, &len, - &(slot->control)); - break; - case OS_FILE_READ: - ret = ReadFile(slot->file, slot->buf, - (DWORD) slot->len, &len, - &(slot->control)); + /** If there are at least 2 seconds old requests, then pick the + oldest one to prevent starvation. If several requests have the + same age, then pick the one at the lowest offset. + @return true if request was selected */ + bool select() + { + if (!select_oldest()) { - break; - default: - ut_error; + return(select_lowest_offset()); } -#ifdef UNIV_PFS_IO - register_pfs_file_io_end(locker, len); -#endif - - if (!ret && GetLastError() == ERROR_IO_PENDING) { - /* aio was queued successfully! - We want a synchronous i/o operation on a - file where we also use async i/o: in Windows - we must use the same wait mechanism as for - async i/o */ + return(true); + } - ret = GetOverlappedResult(slot->file, - &(slot->control), - &len, TRUE); - } + /** Check if there are several consecutive blocks + to read or write. Merge them if found. */ + void merge() + { + /* if m_n_elems != 0, then we have assigned + something valid to consecutive_ios[0] */ + ut_ad(m_n_elems != 0); + ut_ad(first_slot() != NULL); - ret_val = ret && len == slot->len; - } + Slot* slot = first_slot(); - if (slot->type == OS_FILE_WRITE && - !slot->is_log && - srv_use_trim && - os_fallocate_failed == FALSE) { - // Deallocate unused blocks from file system - os_file_trim(slot); + while (!merge_adjacent(slot)) { + /* No op */ + } } - os_aio_array_free_slot(array, slot); - - return(ret_val); -} -#endif + /** We have now collected n_consecutive I/O requests + in the array; allocate a single buffer which can hold + all data, and perform the I/O + @return the length of the buffer */ + ulint allocate_buffer() + __attribute__((warn_unused_result)) + { + ulint len; + Slot* slot = first_slot(); -#if defined(LINUX_NATIVE_AIO) -/******************************************************************//** -This function is only used in Linux native asynchronous i/o. This is -called from within the io-thread. If there are no completed IO requests -in the slot array, the thread calls this function to collect more -requests from the kernel. -The io-thread waits on io_getevents(), which is a blocking call, with -a timeout value. Unless the system is very heavy loaded, keeping the -io-thread very busy, the io-thread will spend most of its time waiting -in this function. -The io-thread also exits in this function. It checks server status at -each wakeup and that is why we use timed wait in io_getevents(). */ -static -void -os_aio_linux_collect( -/*=================*/ - os_aio_array_t* array, /*!< in/out: slot array. */ - ulint segment, /*!< in: local segment no. */ - ulint seg_size) /*!< in: segment size. */ -{ - int i; - int ret; - ulint start_pos; - ulint end_pos; - struct timespec timeout; - struct io_event* events; - struct io_context* io_ctx; + ut_ad(m_ptr == NULL); - /* sanity checks. */ - ut_ad(array != NULL); - ut_ad(seg_size > 0); - ut_ad(segment < array->n_segments); + if (slot->type.is_read() && m_n_elems > 1) { - /* Which part of event array we are going to work on. */ - events = &array->aio_events[segment * seg_size]; + len = 0; - /* Which io_context we are going to use. */ - io_ctx = array->aio_ctx[segment]; + for (ulint i = 0; i < m_n_elems; ++i) { + len += m_slots[i]->len; + } - /* Starting point of the segment we will be working on. */ - start_pos = segment * seg_size; + m_ptr = static_cast<byte*>( + ut_malloc_nokey(len + UNIV_PAGE_SIZE)); - /* End point. */ - end_pos = start_pos + seg_size; + m_buf = static_cast<byte*>( + ut_align(m_ptr, UNIV_PAGE_SIZE)); -retry: + } else { + len = first_slot()->len; + m_buf = first_slot()->buf; + } - /* Initialize the events. The timeout value is arbitrary. - We probably need to experiment with it a little. */ - memset(events, 0, sizeof(*events) * seg_size); - timeout.tv_sec = 0; - timeout.tv_nsec = OS_AIO_REAP_TIMEOUT; + return(len); + } - ret = io_getevents(io_ctx, 1, seg_size, events, &timeout); + /** We have to compress the individual pages and punch + holes in them on a page by page basis when writing to + tables that can be compresed at the IO level. + @param[in] len Value returned by allocate_buffer */ + void copy_to_buffer(ulint len) + { + Slot* slot = first_slot(); - if (ret > 0) { - for (i = 0; i < ret; i++) { - os_aio_slot_t* slot; - struct iocb* control; + if (len > slot->len && slot->type.is_write()) { - control = (struct iocb*) events[i].obj; - ut_a(control != NULL); + byte* ptr = m_buf; - slot = (os_aio_slot_t*) control->data; + ut_ad(ptr != slot->buf); - /* Some sanity checks. */ - ut_a(slot != NULL); - ut_a(slot->reserved); - -#if defined(UNIV_AIO_DEBUG) - fprintf(stderr, - "io_getevents[%c]: slot[%p] ctx[%p]" - " seg[%lu]\n", - (slot->type == OS_FILE_WRITE) ? 'w' : 'r', - slot, io_ctx, segment); -#endif + /* Copy the buffers to the combined buffer */ + for (ulint i = 0; i < m_n_elems; ++i) { - /* We are not scribbling previous segment. */ - ut_a(slot->pos >= start_pos); + slot = m_slots[i]; - /* We have not overstepped to next segment. */ - ut_a(slot->pos < end_pos); + memmove(ptr, slot->buf, slot->len); - if (slot->type == OS_FILE_WRITE && - !slot->is_log && - srv_use_trim && - os_fallocate_failed == FALSE) { - // Deallocate unused blocks from file system - os_file_trim(slot); + ptr += slot->len; } - - /* Mark this request as completed. The error handling - will be done in the calling function. */ - os_mutex_enter(array->mutex); - slot->n_bytes = events[i].res; - slot->ret = events[i].res2; - slot->io_already_done = TRUE; - os_mutex_exit(array->mutex); } - return; } - if (UNIV_UNLIKELY(srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS)) { - return; - } - - /* This error handling is for any error in collecting the - IO requests. The errors, if any, for any particular IO - request are simply passed on to the calling routine. */ - - switch (ret) { - case -EAGAIN: - /* Not enough resources! Try again. */ - case -EINTR: - /* Interrupted! I have tested the behaviour in case of an - interrupt. If we have some completed IOs available then - the return code will be the number of IOs. We get EINTR only - if there are no completed IOs and we have been interrupted. */ - case 0: - /* No pending request! Go back and check again. */ - goto retry; - } - - /* All other errors should cause a trap for now. */ - ut_print_timestamp(stderr); - fprintf(stderr, - " InnoDB: unexpected ret_code[%d] from io_getevents()!\n", - ret); - ut_error; -} - -/**********************************************************************//** -This function is only used in Linux native asynchronous i/o. -Waits for an aio operation to complete. This function is used to wait for -the completed requests. The aio array of pending requests is divided -into segments. The thread specifies which segment or slot it wants to wait -for. NOTE: this function will also take care of freeing the aio slot, -therefore no other thread is allowed to do the freeing! -@return TRUE if the IO was successful */ -UNIV_INTERN -ibool -os_aio_linux_handle( -/*================*/ - ulint global_seg, /*!< in: segment number in the aio array - to wait for; segment 0 is the ibuf - i/o thread, segment 1 is log i/o thread, - then follow the non-ibuf read threads, - and the last are the non-ibuf write - threads. */ - fil_node_t**message1, /*!< out: the messages passed with the */ - void** message2, /*!< aio request; note that in case the - aio operation failed, these output - parameters are valid and can be used to - restart the operation. */ - ulint* type) /*!< out: OS_FILE_WRITE or ..._READ */ -{ - ulint segment; - os_aio_array_t* array; - os_aio_slot_t* slot; - ulint n; - ulint i; - ibool ret = FALSE; + /** Do the I/O with ordinary, synchronous i/o functions: + @param[in] len Length of buffer for IO */ + void io() + { + if (first_slot()->type.is_write()) { - /* Should never be doing Sync IO here. */ - ut_a(global_seg != ULINT_UNDEFINED); + for (ulint i = 0; i < m_n_elems; ++i) { + write(m_slots[i]); + } - /* Find the array and the local segment. */ - segment = os_aio_get_array_and_local_segment(&array, global_seg); - n = array->n_slots / array->n_segments; + } else { - /* Loop until we have found a completed request. */ - for (;;) { - ibool any_reserved = FALSE; - os_mutex_enter(array->mutex); - for (i = 0; i < n; ++i) { - slot = os_aio_array_get_nth_slot( - array, i + segment * n); - if (!slot->reserved) { - continue; - } else if (slot->io_already_done) { - /* Something for us to work on. */ - goto found; - } else { - any_reserved = TRUE; + for (ulint i = 0; i < m_n_elems; ++i) { + read(m_slots[i]); } } - - os_mutex_exit(array->mutex); - - /* There is no completed request. - If there is no pending request at all, - and the system is being shut down, exit. */ - if (UNIV_UNLIKELY - (!any_reserved - && srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS)) { - *message1 = NULL; - *message2 = NULL; - return(TRUE); - } - - /* Wait for some request. Note that we return - from wait iff we have found a request. */ - - srv_set_io_thread_op_info(global_seg, - "waiting for completed aio requests"); - os_aio_linux_collect(array, segment, n); } -found: - /* Note that it may be that there are more then one completed - IO requests. We process them one at a time. We may have a case - here to improve the performance slightly by dealing with all - requests in one sweep. */ - srv_set_io_thread_op_info(global_seg, - "processing completed aio requests"); + /** Do the decompression of the pages read in */ + void io_complete() + { + // Note: For non-compressed tables. Not required + // for correctness. + } - /* Ensure that we are scribbling only our segment. */ - ut_a(i < n); + /** Mark the i/os done in slots */ + void done() + { + for (ulint i = 0; i < m_n_elems; ++i) { + m_slots[i]->io_already_done = true; + } + } - ut_ad(slot != NULL); - ut_ad(slot->reserved); - ut_ad(slot->io_already_done); + /** @return the first slot in the consecutive array */ + Slot* first_slot() + __attribute__((warn_unused_result)) + { + ut_a(m_n_elems > 0); - *message1 = slot->message1; - *message2 = slot->message2; + return(m_slots[0]); + } - *type = slot->type; + /** Wait for I/O requests + @param[in] global_segment The global segment + @param[in,out] event Wait on event if no active requests + @return the number of slots */ + ulint check_pending( + ulint global_segment, + os_event_t event) + __attribute__((warn_unused_result)); +private: - if (slot->ret == 0 && slot->n_bytes == (long) slot->len) { + /** Do the file read + @param[in,out] slot Slot that has the IO context */ + void read(Slot* slot) + { + dberr_t err = os_file_read( + slot->type, + slot->file, + slot->ptr, + slot->offset, + slot->len); - ret = TRUE; - } else { - errno = -slot->ret; + ut_a(err == DB_SUCCESS); + } - if (slot->ret == 0) { - fprintf(stderr, - "InnoDB: Number of bytes after aio %d requested %lu\n" - "InnoDB: from file %s\n", - slot->n_bytes, slot->len, slot->name); - } + /** Do the file read + @param[in,out] slot Slot that has the IO context */ + void write(Slot* slot) + { + dberr_t err = os_file_write( + slot->type, + slot->name, + slot->file, + slot->ptr, + slot->offset, + slot->len); - /* os_file_handle_error does tell us if we should retry - this IO. As it stands now, we don't do this retry when - reaping requests from a different context than - the dispatcher. This non-retry logic is the same for - windows and linux native AIO. - We should probably look into this to transparently - re-submit the IO. */ - os_file_handle_error(slot->name, "Linux aio", __FILE__, __LINE__); + ut_a(err == DB_SUCCESS || err == DB_IO_NO_PUNCH_HOLE); + } - ret = FALSE; + /** @return true if the slots are adjacent and can be merged */ + bool adjacent(const Slot* s1, const Slot* s2) const + { + return(s1 != s2 + && s1->file == s2->file + && s2->offset == s1->offset + s1->len + && s1->type == s2->type); } - os_mutex_exit(array->mutex); + /** @return true if merge limit reached or no adjacent slots found. */ + bool merge_adjacent(Slot*& current) + { + Slot* slot; + ulint offset = m_segment * m_n_slots; - os_aio_array_free_slot(array, slot); + slot = m_array->at(offset); - return(ret); -} -#endif /* LINUX_NATIVE_AIO */ + for (ulint i = 0; i < m_n_slots; ++i, ++slot) { -/**********************************************************************//** -Does simulated aio. This function should be called by an i/o-handler -thread. -@return TRUE if the aio operation succeeded */ -UNIV_INTERN -ibool -os_aio_simulated_handle( -/*====================*/ - ulint global_segment, /*!< in: the number of the segment in the aio - arrays to wait for; segment 0 is the ibuf - i/o thread, segment 1 the log i/o thread, - then follow the non-ibuf read threads, and as - the last are the non-ibuf write threads */ - fil_node_t**message1, /*!< out: the messages passed with the aio - request; note that also in the case where - the aio operation failed, these output - parameters are valid and can be used to - restart the operation, for example */ - void** message2, - ulint* type) /*!< out: OS_FILE_WRITE or ..._READ */ -{ - os_aio_array_t* array; - ulint segment; - os_aio_slot_t* consecutive_ios[OS_AIO_MERGE_N_CONSECUTIVE]; - ulint n_consecutive; - ulint total_len; - ulint offs; - os_offset_t lowest_offset; - ulint biggest_age; - ulint age; - byte* combined_buf; - byte* combined_buf2; - ibool ret; - ibool any_reserved; - ulint n; - os_aio_slot_t* aio_slot; - - /* Fix compiler warning */ - *consecutive_ios = NULL; - - segment = os_aio_get_array_and_local_segment(&array, global_segment); - -restart: - /* NOTE! We only access constant fields in os_aio_array. Therefore - we do not have to acquire the protecting mutex yet */ + if (slot->is_reserved && adjacent(current, slot)) { - srv_set_io_thread_op_info(global_segment, - "looking for i/o requests (a)"); - ut_ad(os_aio_validate_skip()); - ut_ad(segment < array->n_segments); + current = slot; - n = array->n_slots / array->n_segments; + /* Found a consecutive i/o request */ - /* Look through n slots after the segment * n'th slot */ + m_slots[m_n_elems] = slot; - if (array == os_aio_read_array - && os_aio_recommend_sleep_for_read_threads) { + ++m_n_elems; - /* Give other threads chance to add several i/os to the array - at once. */ + return(m_n_elems >= m_slots.capacity()); + } + } - goto recommended_sleep; + return(true); } - srv_set_io_thread_op_info(global_segment, - "looking for i/o requests (b)"); + /** There were no old requests. Look for an I/O request at the lowest + offset in the array (we ignore the high 32 bits of the offset in these + heuristics) */ + bool select_lowest_offset() + { + ut_ad(m_n_elems == 0); - /* Check if there is a slot for which the i/o has already been - done */ - any_reserved = FALSE; + ulint offset = m_segment * m_n_slots; - os_mutex_enter(array->mutex); + m_lowest_offset = IB_UINT64_MAX; - for (ulint i = 0; i < n; i++) { - os_aio_slot_t* slot; + for (ulint i = 0; i < m_n_slots; ++i) { + Slot* slot; - slot = os_aio_array_get_nth_slot(array, i + segment * n); + slot = m_array->at(i + offset); - if (!slot->reserved) { - continue; - } else if (slot->io_already_done) { + if (slot->is_reserved + && slot->offset < m_lowest_offset) { - if (os_aio_print_debug) { - fprintf(stderr, - "InnoDB: i/o for slot %lu" - " already done, returning\n", - (ulong) i); - } + /* Found an i/o request */ + m_slots[0] = slot; - aio_slot = slot; - ret = TRUE; - goto slot_io_done; - } else { - any_reserved = TRUE; + m_n_elems = 1; + + m_lowest_offset = slot->offset; + } } - } - /* There is no completed request. - If there is no pending request at all, - and the system is being shut down, exit. */ - if (!any_reserved && srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS) { - os_mutex_exit(array->mutex); - *message1 = NULL; - *message2 = NULL; - return(TRUE); + return(m_n_elems > 0); } - n_consecutive = 0; + /** Select the slot if it is older than the current oldest slot. + @param[in] slot The slot to check */ + void select_if_older(Slot* slot) + { + ulint age; - /* If there are at least 2 seconds old requests, then pick the oldest - one to prevent starvation. If several requests have the same age, - then pick the one at the lowest offset. */ + age = (ulint) difftime(ut_time(), slot->reservation_time); - biggest_age = 0; - lowest_offset = IB_UINT64_MAX; + if ((age >= 2 && age > m_oldest) + || (age >= 2 + && age == m_oldest + && slot->offset < m_lowest_offset)) { - for (ulint i = 0; i < n; i++) { - os_aio_slot_t* slot; + /* Found an i/o request */ + m_slots[0] = slot; - slot = os_aio_array_get_nth_slot(array, i + segment * n); + m_n_elems = 1; - if (slot->reserved) { + m_oldest = age; - age = (ulint) difftime( - ut_time(), slot->reservation_time); + m_lowest_offset = slot->offset; + } + } - if ((age >= 2 && age > biggest_age) - || (age >= 2 && age == biggest_age - && slot->offset < lowest_offset)) { + /** Select th oldest slot in the array + @return true if oldest slot found */ + bool select_oldest() + { + ut_ad(m_n_elems == 0); - /* Found an i/o request */ - consecutive_ios[0] = slot; + Slot* slot; + ulint offset = m_n_slots * m_segment; - n_consecutive = 1; + slot = m_array->at(offset); - biggest_age = age; - lowest_offset = slot->offset; + for (ulint i = 0; i < m_n_slots; ++i, ++slot) { + + if (slot->is_reserved) { + select_if_older(slot); } } + + return(m_n_elems > 0); } - if (n_consecutive == 0) { - /* There were no old requests. Look for an i/o request at the - lowest offset in the array (we ignore the high 32 bits of the - offset in these heuristics) */ + typedef std::vector<Slot*> slots_t; - lowest_offset = IB_UINT64_MAX; +private: + ulint m_oldest; + ulint m_n_elems; + os_offset_t m_lowest_offset; - for (ulint i = 0; i < n; i++) { - os_aio_slot_t* slot; + AIO* m_array; + ulint m_n_slots; + ulint m_segment; - slot = os_aio_array_get_nth_slot( - array, i + segment * n); + slots_t m_slots; - if (slot->reserved && slot->offset < lowest_offset) { + byte* m_ptr; + byte* m_buf; +}; - /* Found an i/o request */ - consecutive_ios[0] = slot; +/** Wait for I/O requests +@return the number of slots */ +ulint +SimulatedAIOHandler::check_pending( + ulint global_segment, + os_event_t event) +{ + /* NOTE! We only access constant fields in os_aio_array. + Therefore we do not have to acquire the protecting mutex yet */ - n_consecutive = 1; + ut_ad(os_aio_validate_skip()); - lowest_offset = slot->offset; - } - } - } + ut_ad(m_segment < m_array->get_n_segments()); - if (n_consecutive == 0) { + /* Look through n slots after the segment * n'th slot */ - /* No i/o requested at the moment */ + if (AIO::is_read(m_array) + && os_aio_recommend_sleep_for_read_threads) { - goto wait_for_io; - } + /* Give other threads chance to add several + I/Os to the array at once. */ - /* if n_consecutive != 0, then we have assigned - something valid to consecutive_ios[0] */ - ut_ad(n_consecutive != 0); - ut_ad(consecutive_ios[0] != NULL); + srv_set_io_thread_op_info( + global_segment, "waiting for i/o request"); - aio_slot = consecutive_ios[0]; + os_event_wait(event); - /* Check if there are several consecutive blocks to read or write */ + return(0); + } -consecutive_loop: - for (ulint i = 0; i < n; i++) { - os_aio_slot_t* slot; + return(m_array->slots_per_segment()); +} - slot = os_aio_array_get_nth_slot(array, i + segment * n); +/** Does simulated AIO. This function should be called by an i/o-handler +thread. + +@param[in] segment The number of the segment in the aio arrays to wait + for; segment 0 is the ibuf i/o thread, segment 1 the + log i/o thread, then follow the non-ibuf read threads, + and as the last are the non-ibuf write threads +@param[out] m1 the messages passed with the AIO request; note that + also in the case where the AIO operation failed, these + output parameters are valid and can be used to restart + the operation, for example +@param[out] m2 Callback argument +@param[in] type IO context +@return DB_SUCCESS or error code */ +static +dberr_t +os_aio_simulated_handler( + ulint global_segment, + fil_node_t** m1, + void** m2, + IORequest* type) +{ + Slot* slot; + AIO* array; + ulint segment; + os_event_t event = os_aio_segment_wait_events[global_segment]; - if (slot->reserved - && slot != aio_slot - && slot->offset == aio_slot->offset + aio_slot->len - && slot->type == aio_slot->type - && slot->file == aio_slot->file) { + segment = AIO::get_array_and_local_segment(&array, global_segment); - /* Found a consecutive i/o request */ + SimulatedAIOHandler handler(array, segment); - consecutive_ios[n_consecutive] = slot; - n_consecutive++; + for (;;) { - aio_slot = slot; + srv_set_io_thread_op_info( + global_segment, "looking for i/o requests (a)"); - if (n_consecutive < OS_AIO_MERGE_N_CONSECUTIVE) { + ulint n_slots = handler.check_pending(global_segment, event); - goto consecutive_loop; - } else { - break; - } + if (n_slots == 0) { + continue; } - } - srv_set_io_thread_op_info(global_segment, "consecutive i/o requests"); + handler.init(n_slots); - /* We have now collected n_consecutive i/o requests in the array; - allocate a single buffer which can hold all data, and perform the - i/o */ + srv_set_io_thread_op_info( + global_segment, "looking for i/o requests (b)"); - total_len = 0; - aio_slot = consecutive_ios[0]; + array->acquire(); - for (ulint i = 0; i < n_consecutive; i++) { - total_len += consecutive_ios[i]->len; - } + ulint n_reserved; - if (n_consecutive == 1) { - /* We can use the buffer of the i/o request */ - combined_buf = aio_slot->buf; - combined_buf2 = NULL; - } else { - combined_buf2 = static_cast<byte*>( - ut_malloc(total_len + UNIV_PAGE_SIZE)); + slot = handler.check_completed(&n_reserved); - ut_a(combined_buf2); + if (slot != NULL) { - combined_buf = static_cast<byte*>( - ut_align(combined_buf2, UNIV_PAGE_SIZE)); - } + break; - /* We release the array mutex for the time of the i/o: NOTE that - this assumes that there is just one i/o-handler thread serving - a single segment of slots! */ + } else if (n_reserved == 0 + && !buf_page_cleaner_is_active + && srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS) { - os_mutex_exit(array->mutex); + /* There is no completed request. If there + are no pending request at all, and the system + is being shut down, exit. */ - if (aio_slot->type == OS_FILE_WRITE && n_consecutive > 1) { - /* Copy the buffers to the combined buffer */ - offs = 0; + array->release(); - for (ulint i = 0; i < n_consecutive; i++) { + *m1 = NULL; - ut_memcpy(combined_buf + offs, consecutive_ios[i]->buf, - consecutive_ios[i]->len); + *m2 = NULL; - offs += consecutive_ios[i]->len; - } - } + return(DB_SUCCESS); - srv_set_io_thread_op_info(global_segment, "doing file i/o"); + } else if (handler.select()) { - /* Do the i/o with ordinary, synchronous i/o functions: */ - if (aio_slot->type == OS_FILE_WRITE) { - ut_ad(!srv_read_only_mode); - ret = os_file_write( - aio_slot->name, aio_slot->file, combined_buf, - aio_slot->offset, total_len); - - DBUG_EXECUTE_IF("ib_os_aio_func_io_failure_28", - os_has_said_disk_full = FALSE; - ret = 0; - errno = 28;); - - if (!ret) { - os_file_handle_error_cond_exit(aio_slot->name, "os_file_write_func", TRUE, FALSE, - __FILE__, __LINE__); + break; } - } else { - ret = os_file_read( - aio_slot->file, combined_buf, - aio_slot->offset, total_len); - } - - srv_set_io_thread_op_info(global_segment, "file i/o done"); - - if (aio_slot->type == OS_FILE_READ && n_consecutive > 1) { - /* Copy the combined buffer to individual buffers */ - offs = 0; + /* No I/O requested at the moment */ - for (ulint i = 0; i < n_consecutive; i++) { + srv_set_io_thread_op_info( + global_segment, "resetting wait event"); - ut_memcpy(consecutive_ios[i]->buf, combined_buf + offs, - consecutive_ios[i]->len); - offs += consecutive_ios[i]->len; - } - } + /* We wait here until tbere are more IO requests + for this segment. */ - if (combined_buf2) { - ut_free(combined_buf2); - } + os_event_reset(event); - os_mutex_enter(array->mutex); + array->release(); - /* Mark the i/os done in slots */ + srv_set_io_thread_op_info( + global_segment, "waiting for i/o request"); - for (ulint i = 0; i < n_consecutive; i++) { - consecutive_ios[i]->io_already_done = TRUE; + os_event_wait(event); } - /* We return the messages for the first slot now, and if there were - several slots, the messages will be returned with subsequent calls - of this function */ - -slot_io_done: - - ut_a(aio_slot->reserved); + /** Found a slot that has already completed its IO */ - *message1 = aio_slot->message1; - *message2 = aio_slot->message2; + if (slot == NULL) { + /* Merge adjacent requests */ + handler.merge(); - *type = aio_slot->type; + /* Check if there are several consecutive blocks + to read or write */ - os_mutex_exit(array->mutex); + srv_set_io_thread_op_info( + global_segment, "consecutive i/o requests"); - os_aio_array_free_slot(array, aio_slot); + // Note: We don't support write combining for simulated AIO. + //ulint total_len = handler.allocate_buffer(); - return(ret); + /* We release the array mutex for the time of the I/O: NOTE that + this assumes that there is just one i/o-handler thread serving + a single segment of slots! */ -wait_for_io: - srv_set_io_thread_op_info(global_segment, "resetting wait event"); + array->release(); - /* We wait here until there again can be i/os in the segment - of this thread */ + // Note: We don't support write combining for simulated AIO. + //handler.copy_to_buffer(total_len); - os_event_reset(os_aio_segment_wait_events[global_segment]); + srv_set_io_thread_op_info(global_segment, "doing file i/o"); - os_mutex_exit(array->mutex); + handler.io(); -recommended_sleep: - srv_set_io_thread_op_info(global_segment, "waiting for i/o request"); + srv_set_io_thread_op_info(global_segment, "file i/o done"); - os_event_wait(os_aio_segment_wait_events[global_segment]); + handler.io_complete(); - goto restart; -} + array->acquire(); -/**********************************************************************//** -Validates the consistency of an aio array. -@return true if ok */ -static -bool -os_aio_array_validate( -/*==================*/ - os_aio_array_t* array) /*!< in: aio wait array */ -{ - ulint i; - ulint n_reserved = 0; + handler.done(); - os_mutex_enter(array->mutex); + /* We return the messages for the first slot now, and if there + were several slots, the messages will be returned with + subsequent calls of this function */ - ut_a(array->n_slots > 0); - ut_a(array->n_segments > 0); + slot = handler.first_slot(); + } - for (i = 0; i < array->n_slots; i++) { - os_aio_slot_t* slot; + ut_ad(slot->is_reserved); - slot = os_aio_array_get_nth_slot(array, i); + *m1 = slot->m1; + *m2 = slot->m2; - if (slot->reserved) { - n_reserved++; - ut_a(slot->len > 0); - } - } + *type = slot->type; - ut_a(array->n_reserved == n_reserved); + array->release(slot); - os_mutex_exit(array->mutex); + array->release(); - return(true); + return(DB_SUCCESS); } -/**********************************************************************//** -Validates the consistency the aio system. -@return TRUE if ok */ -UNIV_INTERN -ibool -os_aio_validate(void) -/*=================*/ +/** Get the total number of pending IOs +@return the total number of pending IOs */ +ulint +AIO::total_pending_io_count() { - os_aio_array_validate(os_aio_read_array); + ulint count = s_reads->pending_io_count(); - if (os_aio_write_array != 0) { - os_aio_array_validate(os_aio_write_array); + if (s_writes != NULL) { + count += s_writes->pending_io_count(); } - if (os_aio_ibuf_array != 0) { - os_aio_array_validate(os_aio_ibuf_array); + if (s_ibuf != NULL) { + count += s_ibuf->pending_io_count(); } - if (os_aio_log_array != 0) { - os_aio_array_validate(os_aio_log_array); + if (s_log != NULL) { + count += s_log->pending_io_count(); } - if (os_aio_sync_array != 0) { - os_aio_array_validate(os_aio_sync_array); + if (s_sync != NULL) { + count += s_sync->pending_io_count(); } - return(TRUE); + return(count); +} + +/** Validates the consistency the aio system. +@return true if ok */ +static +bool +os_aio_validate() +{ + /* The methods countds and validates, we ignore the count. */ + AIO::total_pending_io_count(); + + return(true); } -/**********************************************************************//** -Prints pending IO requests per segment of an aio array. +/** Prints pending IO requests per segment of an aio array. We probably don't need per segment statistics but they can help us during development phase to see if the IO requests are being -distributed as expected. */ -static +distributed as expected. +@param[in,out] file File where to print +@param[in] segments Pending IO array */ void -os_aio_print_segment_info( -/*======================*/ - FILE* file, /*!< in: file where to print */ - ulint* n_seg, /*!< in: pending IO array */ - os_aio_array_t* array) /*!< in: array to process */ +AIO::print_segment_info( + FILE* file, + const ulint* segments) { - ulint i; + ut_ad(m_n_segments > 0); - ut_ad(array); - ut_ad(n_seg); - ut_ad(array->n_segments > 0); + if (m_n_segments > 1) { - if (array->n_segments == 1) { - return; - } + fprintf(file, " ["); - fprintf(file, " ["); - for (i = 0; i < array->n_segments; i++) { - if (i != 0) { - fprintf(file, ", "); + for (ulint i = 0; i < m_n_segments; ++i, ++segments) { + + if (i != 0) { + fprintf(file, ", "); + } + + fprintf(file, "%lu", *segments); } - fprintf(file, "%lu", n_seg[i]); + fprintf(file, "] "); } - fprintf(file, "] "); } -/**********************************************************************//** -Prints info about the aio array. */ -UNIV_INTERN +/** Prints info about the aio array. +@param[in,out] file Where to print */ void -os_aio_print_array( -/*==============*/ - FILE* file, /*!< in: file where to print */ - os_aio_array_t* array) /*!< in: aio array to print */ +AIO::print(FILE* file) { - ulint n_reserved = 0; - ulint n_res_seg[SRV_MAX_N_IO_THREADS]; + ulint count = 0; + ulint n_res_seg[SRV_MAX_N_IO_THREADS]; - os_mutex_enter(array->mutex); + mutex_enter(&m_mutex); - ut_a(array->n_slots > 0); - ut_a(array->n_segments > 0); + ut_a(!m_slots.empty()); + ut_a(m_n_segments > 0); memset(n_res_seg, 0x0, sizeof(n_res_seg)); - for (ulint i = 0; i < array->n_slots; ++i) { - os_aio_slot_t* slot; - ulint seg_no; + for (ulint i = 0; i < m_slots.size(); ++i) { + Slot& slot = m_slots[i]; + ulint segment = (i * m_n_segments) / m_slots.size(); - slot = os_aio_array_get_nth_slot(array, i); + if (slot.is_reserved) { - seg_no = (i * array->n_segments) / array->n_slots; + ++count; - if (slot->reserved) { - ++n_reserved; - ++n_res_seg[seg_no]; + ++n_res_seg[segment]; - ut_a(slot->len > 0); + ut_a(slot.len > 0); } } - ut_a(array->n_reserved == n_reserved); + ut_a(m_n_reserved == count); + + print_segment_info(file, n_res_seg); + + mutex_exit(&m_mutex); +} + +/** Print all the AIO segments +@param[in,out] file Where to print */ +void +AIO::print_all(FILE* file) +{ + s_reads->print(file); - fprintf(file, " %lu", (ulong) n_reserved); + if (s_writes != NULL) { + fputs(", aio writes:", file); + s_writes->print(file); + } - os_aio_print_segment_info(file, n_res_seg, array); + if (s_ibuf != NULL) { + fputs(",\n ibuf aio reads:", file); + s_ibuf->print(file); + } - os_mutex_exit(array->mutex); + if (s_log != NULL) { + fputs(", log i/o's:", file); + s_log->print(file); + } + + if (s_sync != NULL) { + fputs(", sync i/o's:", file); + s_sync->print(file); + } } -/**********************************************************************//** -Prints info of the aio arrays. */ -UNIV_INTERN +/** Prints info of the aio arrays. +@param[in,out] file file where to print */ void -os_aio_print( -/*=========*/ - FILE* file) /*!< in: file where to print */ +os_aio_print(FILE* file) { time_t current_time; double time_elapsed; @@ -6035,38 +8099,18 @@ os_aio_print( srv_io_thread_op_info[i], srv_io_thread_function[i]); -#ifndef __WIN__ - if (os_aio_segment_wait_events[i]->is_set) { +#ifndef _WIN32 + if (os_event_is_set(os_aio_segment_wait_events[i])) { fprintf(file, " ev set"); } -#endif /* __WIN__ */ +#endif /* _WIN32 */ fprintf(file, "\n"); } fputs("Pending normal aio reads:", file); - os_aio_print_array(file, os_aio_read_array); - - if (os_aio_write_array != 0) { - fputs(", aio writes:", file); - os_aio_print_array(file, os_aio_write_array); - } - - if (os_aio_ibuf_array != 0) { - fputs(",\n ibuf aio reads:", file); - os_aio_print_array(file, os_aio_ibuf_array); - } - - if (os_aio_log_array != 0) { - fputs(", log i/o's:", file); - os_aio_print_array(file, os_aio_log_array); - } - - if (os_aio_sync_array != 0) { - fputs(", sync i/o's:", file); - os_aio_print_array(file, os_aio_sync_array); - } + AIO::print_all(file); putc('\n', file); current_time = ut_time(); @@ -6081,11 +8125,11 @@ os_aio_print( (ulong) os_n_file_writes, (ulong) os_n_fsyncs); - if (os_file_n_pending_preads != 0 || os_file_n_pending_pwrites != 0) { + if (os_n_pending_writes != 0 || os_n_pending_reads != 0) { fprintf(file, "%lu pending preads, %lu pending pwrites\n", - (ulong) os_file_n_pending_preads, - (ulong) os_file_n_pending_pwrites); + (ulint) os_n_pending_reads, + (ulong) os_n_pending_writes); } if (os_n_file_reads == os_n_file_reads_old) { @@ -6114,315 +8158,387 @@ os_aio_print( os_last_printout = current_time; } -/**********************************************************************//** -Refreshes the statistics used to print per-second averages. */ -UNIV_INTERN +/** Refreshes the statistics used to print per-second averages. */ void -os_aio_refresh_stats(void) -/*======================*/ +os_aio_refresh_stats() { + os_n_fsyncs_old = os_n_fsyncs; + + os_bytes_read_since_printout = 0; + os_n_file_reads_old = os_n_file_reads; + os_n_file_writes_old = os_n_file_writes; + os_n_fsyncs_old = os_n_fsyncs; + os_bytes_read_since_printout = 0; - os_last_printout = time(NULL); + os_last_printout = ut_time(); } -#ifdef UNIV_DEBUG -/**********************************************************************//** -Checks that all slots in the system have been freed, that is, there are +/** Checks that all slots in the system have been freed, that is, there are no pending io operations. -@return TRUE if all free */ -UNIV_INTERN -ibool -os_aio_all_slots_free(void) -/*=======================*/ +@return true if all free */ +bool +os_aio_all_slots_free() { - os_aio_array_t* array; - ulint n_res = 0; + return(AIO::total_pending_io_count() == 0); +} - array = os_aio_read_array; +#ifdef UNIV_DEBUG +/** Prints all pending IO for the array +@param[in] file file where to print +@param[in] array array to process */ +void +AIO::to_file(FILE* file) const +{ + acquire(); - os_mutex_enter(array->mutex); + fprintf(file, " %lu\n", static_cast<ulint>(m_n_reserved)); - n_res += array->n_reserved; + for (ulint i = 0; i < m_slots.size(); ++i) { - os_mutex_exit(array->mutex); + const Slot& slot = m_slots[i]; - if (!srv_read_only_mode) { - ut_a(os_aio_write_array == 0); + if (slot.is_reserved) { - array = os_aio_write_array; - - os_mutex_enter(array->mutex); + fprintf(file, + "%s IO for %s (offset=" UINT64PF + ", size=%lu)\n", + slot.type.is_read() ? "read" : "write", + slot.name, slot.offset, slot.len); + } + } - n_res += array->n_reserved; + release(); +} - os_mutex_exit(array->mutex); +/** Print pending IOs for all arrays */ +void +AIO::print_to_file(FILE* file) +{ + fprintf(file, "Pending normal aio reads:"); - ut_a(os_aio_ibuf_array == 0); + s_reads->to_file(file); - array = os_aio_ibuf_array; + if (s_writes != NULL) { + fprintf(file, "Pending normal aio writes:"); + s_writes->to_file(file); + } - os_mutex_enter(array->mutex); + if (s_ibuf != NULL) { + fprintf(file, "Pending ibuf aio reads:"); + s_ibuf->to_file(file); + } - n_res += array->n_reserved; + if (s_log != NULL) { + fprintf(file, "Pending log i/o's:"); + s_log->to_file(file); + } - os_mutex_exit(array->mutex); + if (s_sync != NULL) { + fprintf(file, "Pending sync i/o's:"); + s_sync->to_file(file); } +} - ut_a(os_aio_log_array == 0); +/** Prints all pending IO +@param[in] file File where to print */ +void +os_aio_print_pending_io( + FILE* file) +{ + AIO::print_to_file(file); +} - array = os_aio_log_array; +#endif /* UNIV_DEBUG */ - os_mutex_enter(array->mutex); +/** +Set the file create umask +@param[in] umask The umask to use for file creation. */ +void +os_file_set_umask(ulint umask) +{ + os_innodb_umask = umask; +} - n_res += array->n_reserved; +#else - os_mutex_exit(array->mutex); +#include "univ.i" +#include "db0err.h" +#include "mach0data.h" +#include "fil0fil.h" +#include "os0file.h" + +#ifdef HAVE_LZ4 +#include <lz4.h> +#endif - array = os_aio_sync_array; +#include <zlib.h> - os_mutex_enter(array->mutex); +#endif /* !UNIV_INNOCHECKSUM */ - n_res += array->n_reserved; +/** +@param[in] type The compression type +@return the string representation */ +const char* +Compression::to_string(Type type) +{ + switch(type) { + case NONE: + return("None"); + case ZLIB: + return("Zlib"); + case LZ4: + return("LZ4"); + } + + ut_ad(0); + + return("<UNKNOWN>"); +} - os_mutex_exit(array->mutex); +/** +@param[in] meta Page Meta data +@return the string representation */ +std::string Compression::to_string(const Compression::meta_t& meta) +{ + std::ostringstream stream; - if (n_res == 0) { + stream << "version: " << int(meta.m_version) << " " + << "algorithm: " << meta.m_algorithm << " " + << "(" << to_string(meta.m_algorithm) << ") " + << "orginal_type: " << meta.m_original_type << " " + << "original_size: " << meta.m_original_size << " " + << "compressed_size: " << meta.m_compressed_size; - return(TRUE); - } + return(stream.str()); +} - return(FALSE); +/** @return true if it is a compressed page */ +bool +Compression::is_compressed_page(const byte* page) +{ + return(mach_read_from_2(page + FIL_PAGE_TYPE) == FIL_PAGE_COMPRESSED); } -#endif /* UNIV_DEBUG */ -#ifdef _WIN32 -#include <winioctl.h> -#ifndef FSCTL_FILE_LEVEL_TRIM -#define FSCTL_FILE_LEVEL_TRIM CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 130, METHOD_BUFFERED, FILE_WRITE_DATA) -typedef struct _FILE_LEVEL_TRIM_RANGE { - DWORDLONG Offset; - DWORDLONG Length; -} FILE_LEVEL_TRIM_RANGE, *PFILE_LEVEL_TRIM_RANGE; - -typedef struct _FILE_LEVEL_TRIM { - DWORD Key; - DWORD NumRanges; - FILE_LEVEL_TRIM_RANGE Ranges[1]; -} FILE_LEVEL_TRIM, *PFILE_LEVEL_TRIM; -#endif -#endif +/** Deserizlise the page header compression meta-data +@param[in] page Pointer to the page header +@param[out] control Deserialised data */ +void +Compression::deserialize_header( + const byte* page, + Compression::meta_t* control) +{ + ut_ad(is_compressed_page(page)); -/**********************************************************************//** -Directly manipulate the allocated disk space by deallocating for the file referred to -by fd for the byte range starting at offset and continuing for len bytes. -Within the specified range, partial file system blocks are zeroed, and whole -file system blocks are removed from the file. After a successful call, -subsequent reads from this range will return zeroes. -@return true if success, false if error */ -UNIV_INTERN -ibool -os_file_trim( -/*=========*/ - os_aio_slot_t* slot) /*!< in: slot structure */ -{ - - size_t len = slot->len; - size_t trim_len = slot->page_size - len; - os_offset_t off = slot->offset + len; - size_t bsize = slot->file_block_size; - -#ifdef UNIV_TRIM_DEBUG - fprintf(stderr, "Note: TRIM: write_size %lu trim_len %lu len %lu off %lu bz %lu\n", - slot->write_size ? *slot->write_size : 0, trim_len, len, off, bsize); -#endif + control->m_version = static_cast<uint8_t>( + mach_read_from_1(page + FIL_PAGE_VERSION)); - // Nothing to do if trim length is zero or if actual write - // size is initialized and it is smaller than current write size. - // In first write if we trim we set write_size to actual bytes - // written and rest of the page is trimmed. In following writes - // there is no need to trim again if write_size only increases - // because rest of the page is already trimmed. If actual write - // size decreases we need to trim again. - if (trim_len == 0 || - (slot->write_size && - *slot->write_size > 0 && - len >= *slot->write_size)) { - - if (slot->write_size) { - if (*slot->write_size > 0 && len >= *slot->write_size) { - srv_stats.page_compressed_trim_op_saved.inc(); - } + control->m_original_type = static_cast<uint16_t>( + mach_read_from_2(page + FIL_PAGE_ORIGINAL_TYPE_V1)); - *slot->write_size = len; - } + control->m_compressed_size = static_cast<uint16_t>( + mach_read_from_2(page + FIL_PAGE_COMPRESS_SIZE_V1)); + + control->m_original_size = static_cast<uint16_t>( + mach_read_from_2(page + FIL_PAGE_ORIGINAL_SIZE_V1)); + + control->m_algorithm = static_cast<Type>( + mach_read_from_1(page + FIL_PAGE_ALGORITHM_V1)); +} - return (TRUE); +/** Decompress the page data contents. Page type must be FIL_PAGE_COMPRESSED, if +not then the source contents are left unchanged and DB_SUCCESS is returned. +@param[in] dblwr_recover true of double write recovery in progress +@param[in,out] src Data read from disk, decompressed data will be + copied to this page +@param[in,out] dst Scratch area to use for decompression +@param[in] dst_len Size of the scratch area in bytes +@return DB_SUCCESS or error code */ +dberr_t +Compression::deserialize( + bool dblwr_recover, + byte* src, + byte* dst, + ulint dst_len) +{ + if (!is_compressed_page(src)) { + /* There is nothing we can do. */ + return(DB_SUCCESS); } -#ifdef __linux__ -#if defined(HAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE) - int ret = fallocate(slot->file, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, off, trim_len); + meta_t header; - if (ret) { - /* After first failure do not try to trim again */ - os_fallocate_failed = TRUE; - srv_use_trim = FALSE; - ut_print_timestamp(stderr); - fprintf(stderr, - " InnoDB: Warning: fallocate call failed with error code %d.\n" - " InnoDB: start: %lu len: %lu payload: %lu\n" - " InnoDB: Disabling fallocate for now.\n", errno, (ulong) off, (ulong) trim_len, (ulong) len); - - os_file_handle_error_no_exit(slot->name, - " fallocate(FALLOC_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE) ", - FALSE, __FILE__, __LINE__); - - if (slot->write_size) { - *slot->write_size = 0; - } + deserialize_header(src, &header); - return (FALSE); - } else { - if (slot->write_size) { - *slot->write_size = len; - } - } -#else - ut_print_timestamp(stderr); - fprintf(stderr, - " InnoDB: Warning: fallocate not supported on this installation." - " InnoDB: Disabling fallocate for now."); - os_fallocate_failed = TRUE; - srv_use_trim = FALSE; - if (slot->write_size) { - *slot->write_size = 0; + byte* ptr = src + FIL_PAGE_DATA; + + ut_ad(header.m_version == 1); + + if (header.m_version != 1 + || header.m_original_size < UNIV_PAGE_SIZE_MIN - (FIL_PAGE_DATA + 8) + || header.m_original_size > UNIV_PAGE_SIZE_MAX - FIL_PAGE_DATA + || dst_len < header.m_original_size + FIL_PAGE_DATA) { + + /* The last check could potentially return DB_OVERFLOW, + the caller should be able to retry with a larger buffer. */ + + return(DB_CORRUPTION); } -#endif /* HAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE ... */ + // FIXME: We should use TLS for this and reduce the malloc/free + bool allocated; -#elif defined(_WIN32) - FILE_LEVEL_TRIM flt; - flt.Key = 0; - flt.NumRanges = 1; - flt.Ranges[0].Offset = off; - flt.Ranges[0].Length = trim_len; + /* The caller doesn't know what to expect */ + if (dst == NULL) { - BOOL ret = DeviceIoControl(slot->file, FSCTL_FILE_LEVEL_TRIM, - &flt, sizeof(flt), NULL, NULL, NULL, NULL); + /* Add a safety margin of an additional 50% */ + ulint n_bytes = header.m_original_size + + (header.m_original_size / 2); - if (!ret) { - /* After first failure do not try to trim again */ - os_fallocate_failed = TRUE; - srv_use_trim=FALSE; - ut_print_timestamp(stderr); - fprintf(stderr, - " InnoDB: Warning: fallocate call failed with error.\n" - " InnoDB: start: %lu len: %lu payload: %lu\n" - " InnoDB: Disabling fallocate for now.\n", off, trim_len, len); - - os_file_handle_error_no_exit(slot->name, - " DeviceIOControl(FSCTL_FILE_LEVEL_TRIM) ", - FALSE, __FILE__, __LINE__); - - if (slot->write_size) { - *slot->write_size = 0; +#ifndef UNIV_INNOCHECKSUM + dst = reinterpret_cast<byte*>(ut_malloc_nokey(n_bytes)); +#else + dst = reinterpret_cast<byte*>(malloc(n_bytes)); +#endif /* !UNIV_INNOCHECKSUM */ + + if (dst == NULL) { + + return(DB_OUT_OF_MEMORY); } - return (FALSE); + + allocated = true; } else { - if (slot->write_size) { - *slot->write_size = len; - } + allocated = false; } -#endif - switch(bsize) { - case 512: - srv_stats.page_compression_trim_sect512.add((trim_len / bsize)); - break; - case 1024: - srv_stats.page_compression_trim_sect1024.add((trim_len / bsize)); - break; - case 2948: - srv_stats.page_compression_trim_sect2048.add((trim_len / bsize)); - break; - case 4096: - srv_stats.page_compression_trim_sect4096.add((trim_len / bsize)); - break; - case 8192: - srv_stats.page_compression_trim_sect8192.add((trim_len / bsize)); - break; - case 16384: - srv_stats.page_compression_trim_sect16384.add((trim_len / bsize)); - break; - case 32768: - srv_stats.page_compression_trim_sect32768.add((trim_len / bsize)); - break; - default: - break; - } + Compression compression; + ulint len = header.m_original_size; - srv_stats.page_compressed_trim_op.inc(); + compression.m_type = static_cast<Compression::Type>(header.m_algorithm); - return (TRUE); + switch(compression.m_type) { + case Compression::ZLIB: { -} -#endif /* !UNIV_HOTBACKUP */ + uLongf zlen = header.m_original_size; -/***********************************************************************//** -Try to get number of bytes per sector from file system. -@return file block size */ -UNIV_INTERN -ulint -os_file_get_block_size( -/*===================*/ - os_file_t file, /*!< in: handle to a file */ - const char* name) /*!< in: file name */ -{ - ulint fblock_size = 512; + if (uncompress(dst, &zlen, ptr, header.m_compressed_size) + != Z_OK) { -#if defined(UNIV_LINUX) && defined(HAVE_SYS_STATVFS_H) - struct statvfs fstat; - int err; + if (allocated) { + ut_free(dst); + } + + return(DB_IO_DECOMPRESS_FAIL); + } - err = fstatvfs(file, &fstat); + len = static_cast<ulint>(zlen); - if (err != 0) { - fprintf(stderr, "InnoDB: Warning: fstatvfs() failed on file %s\n", name); - os_file_handle_error_no_exit(name, "fstatvfs()", FALSE, __FILE__, __LINE__); - } else { - fblock_size = fstat.f_bsize; + break; } -#endif /* UNIV_LINUX */ -#ifdef __WIN__ - { - DWORD SectorsPerCluster = 0; - DWORD BytesPerSector = 0; - DWORD NumberOfFreeClusters = 0; - DWORD TotalNumberOfClusters = 0; - - /* - if (GetFreeSpace((LPCTSTR)name, &SectorsPerCluster, &BytesPerSector, &NumberOfFreeClusters, &TotalNumberOfClusters)) { - fblock_size = BytesPerSector; +#ifdef HAVE_LZ4 + case Compression::LZ4: { + int ret; + + if (dblwr_recover) { + + ret = LZ4_decompress_safe( + reinterpret_cast<char*>(ptr), + reinterpret_cast<char*>(dst), + header.m_compressed_size, + header.m_original_size); + } else { - fprintf(stderr, "InnoDB: Warning: GetFreeSpace() failed on file %s\n", name); - os_file_handle_error_no_exit(name, "GetFreeSpace()", FALSE, __FILE__, __LINE__); + + /* This can potentially read beyond the input + buffer if the data is malformed. According to + the LZ4 documentation it is a little faster + than the above function. When recovering from + the double write buffer we can afford to us the + slower function above. */ + + ret = LZ4_decompress_fast( + reinterpret_cast<char*>(ptr), + reinterpret_cast<char*>(dst), + header.m_original_size); + } + + if (ret < 0) { + + if (allocated) { + ut_free(dst); + } + + return(DB_IO_DECOMPRESS_FAIL); } - */ + + break; } -#endif /* __WIN__*/ +#endif + default: +#if !defined(UNIV_INNOCHECKSUM) + ib::error() + << "Compression algorithm support missing: " + << Compression::to_string(compression.m_type); +#else + fprintf(stderr, "Compression algorithm support missing: %s\n", + Compression::to_string(compression.m_type)); +#endif /* !UNIV_INNOCHECKSUM */ - /* Currently we support file block size up to 4Kb */ - if (fblock_size > 4096 || fblock_size < 512) { - if (fblock_size < 512) { - fblock_size = 512; - } else { - fblock_size = 4096; + if (allocated) { + ut_free(dst); } + + return(DB_UNSUPPORTED); } + /* Leave the header alone */ + memmove(src + FIL_PAGE_DATA, dst, len); - return fblock_size; + mach_write_to_2(src + FIL_PAGE_TYPE, header.m_original_type); + + ut_ad(dblwr_recover + || memcmp(src + FIL_PAGE_LSN + 4, + src + (header.m_original_size + FIL_PAGE_DATA) + - FIL_PAGE_END_LSN_OLD_CHKSUM + 4, 4) == 0); + + if (allocated) { + ut_free(dst); + } + return(DB_SUCCESS); +} + +/** Decompress the page data contents. Page type must be FIL_PAGE_COMPRESSED, if +not then the source contents are left unchanged and DB_SUCCESS is returned. +@param[in] dblwr_recover true of double write recovery in progress +@param[in,out] src Data read from disk, decompressed data will be + copied to this page +@param[in,out] dst Scratch area to use for decompression +@param[in] dst_len Size of the scratch area in bytes +@return DB_SUCCESS or error code */ +dberr_t +os_file_decompress_page( + bool dblwr_recover, + byte* src, + byte* dst, + ulint dst_len) +{ + return(Compression::deserialize(dblwr_recover, src, dst, dst_len)); +} + +/** Normalizes a directory path for the current OS: +On Windows, we convert '/' to '\', else we convert '\' to '/'. +@param[in,out] str A null-terminated directory and file path */ +void +os_normalize_path( + char* str) +{ + if (str != NULL) { + for (; *str; str++) { + if (*str == OS_PATH_SEPARATOR_ALT) { + *str = OS_PATH_SEPARATOR; + } + } + } } |