Drizzled Public API Documentation

os0file.h
Go to the documentation of this file.
1 /***********************************************************************
2 
3 Copyright (C) 1995, 2010, Innobase Oy. All Rights Reserved.
4 Copyright (C) 2009, Percona Inc.
5 
6 Portions of this file contain modifications contributed and copyrighted
7 by Percona Inc.. Those modifications are
8 gratefully acknowledged and are described briefly in the InnoDB
9 documentation. The contributions by Percona Inc. are incorporated with
10 their permission, and subject to the conditions contained in the file
11 COPYING.Percona.
12 
13 This program is free software; you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by the
15 Free Software Foundation; version 2 of the License.
16 
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
20 Public License for more details.
21 
22 You should have received a copy of the GNU General Public License along
23 with this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 
26 ***********************************************************************/
27 
28 /**************************************************/
35 #pragma once
36 #ifndef os0file_h
37 #define os0file_h
38 
39 #include "univ.i"
40 
41 #ifndef __WIN__
42 #include <dirent.h>
43 #include <sys/stat.h>
44 #include <time.h>
45 #endif
46 
48 typedef struct fil_node_struct fil_node_t;
49 
50 #ifdef UNIV_DO_FLUSH
51 extern ibool os_do_not_call_flush_at_each_write;
52 #endif /* UNIV_DO_FLUSH */
53 extern ibool os_has_said_disk_full;
55 extern ibool os_aio_print_debug;
56 
58 extern ulint os_file_n_pending_preads;
60 extern ulint os_file_n_pending_pwrites;
61 
63 extern ulint os_n_pending_reads;
65 extern ulint os_n_pending_writes;
66 
67 #ifdef __WIN__
68 
71 #define WIN_ASYNC_IO
72 
74 #define UNIV_NON_BUFFERED_IO
75 
76 #endif
77 
78 #ifdef __WIN__
79 
80 # define os_file_t HANDLE
81 
84 # define OS_FILE_FROM_FD(fd) (HANDLE) _get_osfhandle(fd)
85 #else
86 
87 typedef int os_file_t;
91 # define OS_FILE_FROM_FD(fd) fd
92 #endif
93 
95 extern ulint os_innodb_umask;
96 
104 #define OS_FILE_LOG_BLOCK_SIZE srv_log_block_size
105 
107 #define OS_FILE_OPEN 51
108 #define OS_FILE_CREATE 52
109 #define OS_FILE_OVERWRITE 53
110 #define OS_FILE_OPEN_RAW 54
111 #define OS_FILE_CREATE_PATH 55
112 #define OS_FILE_OPEN_RETRY 56 /* for os_file_create() on
113  the first ibdata file */
114 
115 #define OS_FILE_READ_ONLY 333
116 #define OS_FILE_READ_WRITE 444
117 #define OS_FILE_READ_ALLOW_DELETE 555 /* for ibbackup */
118 
119 /* Options for file_create */
120 #define OS_FILE_AIO 61
121 #define OS_FILE_NORMAL 62
122 /* @} */
123 
125 #define OS_DATA_FILE 100
126 #define OS_LOG_FILE 101
127 /* @} */
128 
130 #define OS_FILE_NOT_FOUND 71
131 #define OS_FILE_DISK_FULL 72
132 #define OS_FILE_ALREADY_EXISTS 73
133 #define OS_FILE_PATH_ERROR 74
134 #define OS_FILE_AIO_RESOURCES_RESERVED 75 /* wait for OS aio resources
135  to become available again */
136 #define OS_FILE_SHARING_VIOLATION 76
137 #define OS_FILE_ERROR_NOT_SPECIFIED 77
138 #define OS_FILE_INSUFFICIENT_RESOURCE 78
139 #define OS_FILE_AIO_INTERRUPTED 79
140 #define OS_FILE_OPERATION_ABORTED 80
141 /* @} */
142 
144 #define OS_FILE_READ 10
145 #define OS_FILE_WRITE 11
146 
147 #define OS_FILE_LOG 256 /* This can be ORed to type */
148 /* @} */
149 
150 #define OS_AIO_N_PENDING_IOS_PER_THREAD 32
154 #define OS_AIO_NORMAL 21
156 #define OS_AIO_IBUF 22
158 #define OS_AIO_LOG 23
159 #define OS_AIO_SYNC 24
169 #define OS_AIO_SIMULATED_WAKE_LATER 512
175 /* @} */
176 
177 #define OS_WIN31 1
178 #define OS_WIN95 2
179 #define OS_WINNT 3
180 #define OS_WIN2000 4
181 #define OS_WINXP 5
183 #define OS_WINVISTA 6
185 #define OS_WIN7 7
189 extern ulint os_n_file_reads;
190 extern ulint os_n_file_writes;
191 extern ulint os_n_fsyncs;
192 
193 extern uint32_t srv_log_block_size;
195 #ifdef UNIV_PFS_IO
196 /* Keys to register InnoDB I/O with performance schema */
197 extern mysql_pfs_key_t innodb_file_data_key;
198 extern mysql_pfs_key_t innodb_file_log_key;
199 extern mysql_pfs_key_t innodb_file_temp_key;
201 /* Following four macros are instumentations to register
202 various file I/O operations with performance schema.
203 1) register_pfs_file_open_begin() and register_pfs_file_open_end() are
204 used to register file creation, opening, closing and renaming.
205 2) register_pfs_file_io_begin() and register_pfs_file_io_end() are
206 used to register actual file read, write and flush */
207 # define register_pfs_file_open_begin(state, locker, key, op, name, \
208  src_file, src_line) \
209 do { \
210  if (PSI_server) { \
211  locker = PSI_server->get_thread_file_name_locker( \
212  state, key, op, name, &locker); \
213  if (locker) { \
214  PSI_server->start_file_open_wait( \
215  locker, src_file, src_line); \
216  } \
217  } \
218 } while (0)
219 
220 # define register_pfs_file_open_end(locker, file) \
221 do { \
222  if (locker) { \
223  PSI_server->end_file_open_wait_and_bind_to_descriptor( \
224  locker, file); \
225  } \
226 } while (0)
227 
228 # define register_pfs_file_io_begin(state, locker, file, count, op, \
229  src_file, src_line) \
230 do { \
231  if (PSI_server) { \
232  locker = PSI_server->get_thread_file_descriptor_locker( \
233  state, file, op); \
234  if (locker) { \
235  PSI_server->start_file_wait( \
236  locker, count, src_file, src_line); \
237  } \
238  } \
239 } while (0)
240 
241 # define register_pfs_file_io_end(locker, count) \
242 do { \
243  if (locker) { \
244  PSI_server->end_file_wait(locker, count); \
245  } \
246 } while (0)
247 #endif /* UNIV_PFS_IO */
248 
249 /* Following macros/functions are file I/O APIs that would be performance
250 schema instrumented if "UNIV_PFS_IO" is defined. They would point to
251 wrapper functions with performance schema instrumentation in such case.
252 
253 os_file_create
254 os_file_create_simple
255 os_file_create_simple_no_error_handling
256 os_file_close
257 os_file_rename
258 os_aio
259 os_file_read
260 os_file_read_no_error_handling
261 os_file_write
262 
263 The wrapper functions have the prefix of "innodb_". */
264 
265 #ifdef UNIV_PFS_IO
266 # define os_file_create(key, name, create, purpose, type, success) \
267  pfs_os_file_create_func(key, name, create, purpose, type, \
268  success, __FILE__, __LINE__)
269 
270 # define os_file_create_simple(key, name, create, access, success) \
271  pfs_os_file_create_simple_func(key, name, create, access, \
272  success, __FILE__, __LINE__)
273 
274 # define os_file_create_simple_no_error_handling( \
275  key, name, create_mode, access, success) \
276  pfs_os_file_create_simple_no_error_handling_func( \
277  key, name, create_mode, access, success, __FILE__, __LINE__)
278 
279 # define os_file_close(file) \
280  pfs_os_file_close_func(file, __FILE__, __LINE__)
281 
282 # define os_aio(type, mode, name, file, buf, offset, offset_high, \
283  n, message1, message2) \
284  pfs_os_aio_func(type, mode, name, file, buf, offset, \
285  offset_high, n, message1, message2, \
286  __FILE__, __LINE__)
287 
288 # define os_file_read(file, buf, offset, offset_high, n) \
289  pfs_os_file_read_func(file, buf, offset, offset_high, n, \
290  __FILE__, __LINE__)
291 
292 # define os_file_read_no_error_handling(file, buf, offset, \
293  offset_high, n) \
294  pfs_os_file_read_no_error_handling_func(file, buf, offset, \
295  offset_high, n, \
296  __FILE__, __LINE__)
297 
298 # define os_file_write(name, file, buf, offset, offset_high, n) \
299  pfs_os_file_write_func(name, file, buf, offset, offset_high, \
300  n, __FILE__, __LINE__)
301 
302 # define os_file_flush(file) \
303  pfs_os_file_flush_func(file, __FILE__, __LINE__)
304 
305 # define os_file_rename(key, oldpath, newpath) \
306  pfs_os_file_rename_func(key, oldpath, newpath, __FILE__, __LINE__)
307 #else /* UNIV_PFS_IO */
308 
309 /* If UNIV_PFS_IO is not defined, these I/O APIs point
310 to original un-instrumented file I/O APIs */
311 # define os_file_create(key, name, create, purpose, type, success) \
312  os_file_create_func(name, create, purpose, type, success)
313 
314 # define os_file_create_simple(key, name, create, access, success) \
315  os_file_create_simple_func(name, create_mode, access, success)
316 
317 # define os_file_create_simple_no_error_handling( \
318  key, name, create_mode, access, success) \
319  os_file_create_simple_no_error_handling_func( \
320  name, create_mode, access, success)
321 
322 # define os_file_close(file) os_file_close_func(file)
323 
324 # define os_aio(type, mode, name, file, buf, offset, offset_high, \
325  n, message1, message2) \
326  os_aio_func(type, mode, name, file, buf, offset, offset_high, n,\
327  message1, message2)
328 
329 # define os_file_read(file, buf, offset, offset_high, n) \
330  os_file_read_func(file, buf, offset, offset_high, n)
331 
332 # define os_file_read_no_error_handling(file, buf, offset, \
333  offset_high, n) \
334  os_file_read_no_error_handling_func(file, buf, offset, offset_high, n)
335 
336 # define os_file_write(name, file, buf, offset, offset_high, n) \
337  os_file_write_func(name, file, buf, offset, offset_high, n)
338 
339 # define os_file_flush(file) os_file_flush_func(file)
340 
341 # define os_file_rename(key, oldpath, newpath) \
342  os_file_rename_func(oldpath, newpath)
343 
344 #endif /* UNIV_PFS_IO */
345 
346 /* File types for directory entry data type */
347 
348 enum os_file_type_enum{
349  OS_FILE_TYPE_UNKNOWN = 0,
350  OS_FILE_TYPE_FILE, /* regular file */
351  OS_FILE_TYPE_DIR, /* directory */
352  OS_FILE_TYPE_LINK /* symbolic link */
353 };
354 typedef enum os_file_type_enum os_file_type_t;
355 
356 /* Maximum path string length in bytes when referring to tables with in the
357 './databasename/tablename.ibd' path format; we can allocate at least 2 buffers
358 of this size from the thread stack; that is why this should not be made much
359 bigger than 4000 bytes */
360 #define OS_FILE_MAX_PATH 4000
361 
362 /* Struct used in fetching information of a file in a directory */
363 struct os_file_stat_struct{
364  char name[OS_FILE_MAX_PATH];
365  os_file_type_t type;
366  ib_int64_t size;
367  time_t ctime;
368  time_t mtime;
369  time_t atime;
370 };
371 typedef struct os_file_stat_struct os_file_stat_t;
372 
373 #ifdef __WIN__
374 typedef HANDLE os_file_dir_t;
375 #else
376 typedef DIR* os_file_dir_t;
377 #endif
378 
379 /***********************************************************************/
383 UNIV_INTERN
384 ulint
386 /*===================*/
387 #ifndef UNIV_HOTBACKUP
388 /****************************************************************/
390 UNIV_INTERN
391 void
392 os_io_init_simple(void);
393 /*===================*/
394 /***********************************************************************/
399 FILE*
401 /*========================*/
402 #endif /* !UNIV_HOTBACKUP */
403 /***********************************************************************/
409 UNIV_INTERN
412 /*============*/
413  const char* dirname,
415  ibool error_is_fatal);
420 /***********************************************************************/
423 UNIV_INTERN
424 int
426 /*=============*/
427  os_file_dir_t dir);
428 /***********************************************************************/
432 UNIV_INTERN
433 int
435 /*======================*/
436  const char* dirname,
437  os_file_dir_t dir,
438  os_file_stat_t* info);
439 /*****************************************************************/
445 UNIV_INTERN
446 ibool
448 /*=====================*/
449  const char* pathname,
451  ibool fail_if_exists);
453 /****************************************************************/
459 UNIV_INTERN
460 os_file_t
462 /*=======================*/
463  const char* name,
465  ulint create_mode,
472  ulint access_type,
474  ibool* success);
475 /****************************************************************/
481 UNIV_INTERN
482 os_file_t
484 /*=========================================*/
485  const char* name,
487  ulint create_mode,
491  ulint access_type,
495  ibool* success);
496 /****************************************************************/
498 UNIV_INTERN
499 void
501 /*================*/
502  int fd,
503  const char* file_name,
505  const char* operation_name);
507 /****************************************************************/
513 UNIV_INTERN
514 os_file_t
516 /*================*/
517  const char* name,
519  ulint create_mode,
527  ulint purpose,
534  ulint type,
535  ibool* success);
536 /***********************************************************************/
539 UNIV_INTERN
540 ibool
542 /*===========*/
543  const char* name);
545 /***********************************************************************/
548 UNIV_INTERN
549 ibool
551 /*=====================*/
552  const char* name);
553 /***********************************************************************/
559 UNIV_INTERN
560 ibool
562 /*================*/
563  const char* oldpath,
565  const char* newpath);
566 /***********************************************************************/
572 UNIV_INTERN
573 ibool
575 /*===============*/
576  os_file_t file);
578 #ifdef UNIV_PFS_IO
579 /****************************************************************/
586 UNIV_INLINE
587 os_file_t
588 pfs_os_file_create_simple_func(
589 /*===========================*/
590  mysql_pfs_key_t key,
591  const char* name,
593  ulint create_mode,
600  ulint access_type,
602  ibool* success,
603  const char* src_file,
604  ulint src_line);
606 /****************************************************************/
614 UNIV_INLINE
615 os_file_t
616 pfs_os_file_create_simple_no_error_handling_func(
617 /*=============================================*/
618  mysql_pfs_key_t key,
619  const char* name,
621  ulint create_mode,
625  ulint access_type,
629  ibool* success,
630  const char* src_file,
631  ulint src_line);
633 /****************************************************************/
640 UNIV_INLINE
641 os_file_t
642 pfs_os_file_create_func(
643 /*====================*/
644  mysql_pfs_key_t key,
645  const char* name,
647  ulint create_mode,
655  ulint purpose,
662  ulint type,
663  ibool* success,
664  const char* src_file,
665  ulint src_line);
667 /***********************************************************************/
672 UNIV_INLINE
673 ibool
674 pfs_os_file_close_func(
675 /*===================*/
676  os_file_t file,
677  const char* src_file,
678  ulint src_line);
679 /*******************************************************************/
685 UNIV_INLINE
686 ibool
687 pfs_os_file_read_func(
688 /*==================*/
689  os_file_t file,
690  void* buf,
691  ulint offset,
693  ulint offset_high,
695  ulint n,
696  const char* src_file,
697  ulint src_line);
699 /*******************************************************************/
706 UNIV_INLINE
707 ibool
708 pfs_os_file_read_no_error_handling_func(
709 /*====================================*/
710  os_file_t file,
711  void* buf,
712  ulint offset,
714  ulint offset_high,
716  ulint n,
717  const char* src_file,
718  ulint src_line);
720 /*******************************************************************/
726 UNIV_INLINE
727 ibool
728 pfs_os_aio_func(
729 /*============*/
730  ulint type,
731  ulint mode,
732  const char* name,
734  os_file_t file,
735  void* buf,
737  ulint offset,
739  ulint offset_high,
741  ulint n,
742  fil_node_t* message1,
746  void* message2,
750  const char* src_file,
751  ulint src_line);
752 /*******************************************************************/
758 UNIV_INLINE
759 ibool
760 pfs_os_file_write_func(
761 /*===================*/
762  const char* name,
764  os_file_t file,
765  const void* buf,
766  ulint offset,
768  ulint offset_high,
770  ulint n,
771  const char* src_file,
772  ulint src_line);
773 /***********************************************************************/
780 UNIV_INLINE
781 ibool
782 pfs_os_file_flush_func(
783 /*===================*/
784  os_file_t file,
785  const char* src_file,
786  ulint src_line);
788 /***********************************************************************/
794 UNIV_INLINE
795 ibool
796 pfs_os_file_rename_func(
797 /*====================*/
798  mysql_pfs_key_t key,
799  const char* oldpath,
801  const char* newpath,
802  const char* src_file,
803  ulint src_line);
804 #endif /* UNIV_PFS_IO */
805 
806 #ifdef UNIV_HOTBACKUP
807 /***********************************************************************/
810 UNIV_INTERN
811 ibool
812 os_file_close_no_error_handling(
813 /*============================*/
814  os_file_t file);
815 #endif /* UNIV_HOTBACKUP */
816 /***********************************************************************/
819 UNIV_INTERN
820 ibool
822 /*=============*/
823  os_file_t file,
824  ulint* size,
826  ulint* size_high);
827 /***********************************************************************/
830 UNIV_INTERN
831 ib_int64_t
833 /*===========================*/
834  os_file_t file);
835 /***********************************************************************/
838 UNIV_INTERN
839 ibool
841 /*=============*/
842  const char* name,
844  os_file_t file,
845  ulint size,
847  ulint size_high);
848 /***********************************************************************/
851 UNIV_INTERN
852 ibool
854 /*============*/
855  FILE* file);
856 /***********************************************************************/
860 UNIV_INTERN
861 ibool
863 /*===============*/
864  os_file_t file);
865 /***********************************************************************/
871 UNIV_INTERN
872 ulint
874 /*===================*/
875  ibool report_all_errors);
877 /*******************************************************************/
881 UNIV_INTERN
882 ibool
884 /*==============*/
885  os_file_t file,
886  void* buf,
887  ulint offset,
889  ulint offset_high,
891  ulint n);
892 /*******************************************************************/
896 UNIV_INTERN
897 void
899 /*================*/
900  FILE* file,
901  char* str,
902  ulint size);
903 /*******************************************************************/
909 UNIV_INTERN
910 ibool
912 /*================================*/
913  os_file_t file,
914  void* buf,
915  ulint offset,
917  ulint offset_high,
919  ulint n);
921 /*******************************************************************/
926 UNIV_INTERN
927 ibool
929 /*===============*/
930  const char* name,
932  os_file_t file,
933  const void* buf,
934  ulint offset,
936  ulint offset_high,
938  ulint n);
939 /*******************************************************************/
942 UNIV_INTERN
943 ibool
945 /*===========*/
946  const char* path,
947  ibool* exists,
948  os_file_type_t* type);
949 /****************************************************************/
977 UNIV_INTERN
978 char*
980 /*============*/
981  const char* path);
982 /****************************************************************/
985 UNIV_INTERN
986 ibool
988 /*=============================*/
989  const char* path);
990 /***********************************************************************
991 Initializes the asynchronous io system. Creates one array each for ibuf
992 and log i/o. Also creates one array each for read and write where each
993 array is divided logically into n_read_segs and n_write_segs
994 respectively. The caller must create an i/o handler thread for each
995 segment in these arrays. This function also creates the sync array.
996 No i/o handler thread needs to be created for that */
997 UNIV_INTERN
998 ibool
999 os_aio_init(
1000 /*========*/
1001  ulint n_per_seg, /*<! in: maximum number of pending aio
1002  operations allowed per segment */
1003  ulint n_read_segs, /*<! in: number of reader threads */
1004  ulint n_write_segs, /*<! in: number of writer threads */
1005  ulint n_slots_sync); /*<! in: number of slots in the sync aio
1006  array */
1007 /***********************************************************************
1008 Frees the asynchronous io system. */
1009 UNIV_INTERN
1010 void
1011 os_aio_free(void);
1012 /*=============*/
1013 
1014 /*******************************************************************/
1018 UNIV_INTERN
1019 ibool
1020 os_aio_func(
1021 /*========*/
1022  ulint type,
1023  ulint mode,
1036  const char* name,
1038  os_file_t file,
1039  void* buf,
1041  ulint offset,
1043  ulint offset_high,
1045  ulint n,
1046  fil_node_t* message1,
1050  void* message2);
1054 /************************************************************************/
1057 UNIV_INTERN
1058 void
1060 /*=====================================*/
1061 /************************************************************************/
1064 UNIV_INTERN
1065 void
1067 /*=====================================*/
1068 /**********************************************************************/
1070 UNIV_INTERN
1071 void
1073 /*=======================================*/
1074 /**********************************************************************/
1079 UNIV_INTERN
1080 void
1082 /*============================================*/
1083 
1084 #ifdef WIN_ASYNC_IO
1085 /**********************************************************************/
1093 UNIV_INTERN
1094 ibool
1095 os_aio_windows_handle(
1096 /*==================*/
1097  ulint segment,
1105  ulint pos,
1107  fil_node_t**message1,
1112  void** message2,
1113  ulint* type);
1114 #endif
1115 
1116 /**********************************************************************/
1120 UNIV_INTERN
1121 ibool
1123 /*====================*/
1124  ulint segment,
1129  fil_node_t**message1,
1134  void** message2,
1135  ulint* type);
1136 /**********************************************************************/
1139 UNIV_INTERN
1140 ibool
1141 os_aio_validate(void);
1142 /*=================*/
1143 /**********************************************************************/
1145 UNIV_INTERN
1146 void
1147 os_aio_print(
1148 /*=========*/
1149  FILE* file);
1150 /**********************************************************************/
1152 UNIV_INTERN
1153 void
1154 os_aio_refresh_stats(void);
1155 /*======================*/
1156 
1157 #ifdef UNIV_DEBUG
1158 /**********************************************************************/
1161 UNIV_INTERN
1162 ibool
1163 os_aio_all_slots_free(void);
1164 /*=======================*/
1165 #endif /* UNIV_DEBUG */
1166 
1167 /*******************************************************************/
1170 UNIV_INTERN
1171 ibool
1173 /*===============*/
1174  const char* path,
1175  os_file_stat_t* stat_info);
1178 #if !defined(UNIV_HOTBACKUP)
1179 /*********************************************************************/
1183 UNIV_INTERN
1184 int
1186 /*========================*/
1187 #endif /* !UNIV_HOTBACKUP */
1188 
1189 
1190 #if defined(LINUX_NATIVE_AIO)
1191 /**************************************************************************
1192 This function is only used in Linux native asynchronous i/o.
1193 Waits for an aio operation to complete. This function is used to wait the
1194 for completed requests. The aio array of pending requests is divided
1195 into segments. The thread specifies which segment or slot it wants to wait
1196 for. NOTE: this function will also take care of freeing the aio slot,
1197 therefore no other thread is allowed to do the freeing!
1198 @return TRUE if the IO was successful */
1199 UNIV_INTERN
1200 ibool
1201 os_aio_linux_handle(
1202 /*================*/
1203  ulint global_seg,
1209  fil_node_t**message1,
1210  void** message2,
1214  ulint* type);
1215 #endif /* LINUX_NATIVE_AIO */
1216 
1217 #ifndef UNIV_NONINL
1218 #include "os0file.ic"
1219 #endif
1220 
1221 #endif