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 #ifdef __WIN__
380 /***********************************************************************/
384 UNIV_INTERN
385 ulint
386 os_get_os_version(void);
387 /*===================*/
388 #endif /* __WIN__ */
389 #ifndef UNIV_HOTBACKUP
390 /****************************************************************/
392 UNIV_INTERN
393 void
394 os_io_init_simple(void);
395 /*===================*/
396 /***********************************************************************/
401 FILE*
403 /*========================*/
404 #endif /* !UNIV_HOTBACKUP */
405 /***********************************************************************/
411 UNIV_INTERN
414 /*============*/
415  const char* dirname,
417  ibool error_is_fatal);
422 /***********************************************************************/
425 UNIV_INTERN
426 int
428 /*=============*/
429  os_file_dir_t dir);
430 /***********************************************************************/
434 UNIV_INTERN
435 int
437 /*======================*/
438  const char* dirname,
439  os_file_dir_t dir,
440  os_file_stat_t* info);
441 /*****************************************************************/
447 UNIV_INTERN
448 ibool
450 /*=====================*/
451  const char* pathname,
453  ibool fail_if_exists);
455 /****************************************************************/
461 UNIV_INTERN
462 os_file_t
464 /*=======================*/
465  const char* name,
467  ulint create_mode,
474  ulint access_type,
476  ibool* success);
477 /****************************************************************/
483 UNIV_INTERN
484 os_file_t
486 /*=========================================*/
487  const char* name,
489  ulint create_mode,
493  ulint access_type,
497  ibool* success);
498 /****************************************************************/
500 UNIV_INTERN
501 void
503 /*================*/
504  int fd,
505  const char* file_name,
507  const char* operation_name);
509 /****************************************************************/
515 UNIV_INTERN
516 os_file_t
518 /*================*/
519  const char* name,
521  ulint create_mode,
529  ulint purpose,
536  ulint type,
537  ibool* success);
538 /***********************************************************************/
541 UNIV_INTERN
542 ibool
544 /*===========*/
545  const char* name);
547 /***********************************************************************/
550 UNIV_INTERN
551 ibool
553 /*=====================*/
554  const char* name);
555 /***********************************************************************/
561 UNIV_INTERN
562 ibool
564 /*================*/
565  const char* oldpath,
567  const char* newpath);
568 /***********************************************************************/
574 UNIV_INTERN
575 ibool
577 /*===============*/
578  os_file_t file);
580 #ifdef UNIV_PFS_IO
581 /****************************************************************/
588 UNIV_INLINE
589 os_file_t
590 pfs_os_file_create_simple_func(
591 /*===========================*/
592  mysql_pfs_key_t key,
593  const char* name,
595  ulint create_mode,
602  ulint access_type,
604  ibool* success,
605  const char* src_file,
606  ulint src_line);
608 /****************************************************************/
616 UNIV_INLINE
617 os_file_t
618 pfs_os_file_create_simple_no_error_handling_func(
619 /*=============================================*/
620  mysql_pfs_key_t key,
621  const char* name,
623  ulint create_mode,
627  ulint access_type,
631  ibool* success,
632  const char* src_file,
633  ulint src_line);
635 /****************************************************************/
642 UNIV_INLINE
643 os_file_t
644 pfs_os_file_create_func(
645 /*====================*/
646  mysql_pfs_key_t key,
647  const char* name,
649  ulint create_mode,
657  ulint purpose,
664  ulint type,
665  ibool* success,
666  const char* src_file,
667  ulint src_line);
669 /***********************************************************************/
674 UNIV_INLINE
675 ibool
676 pfs_os_file_close_func(
677 /*===================*/
678  os_file_t file,
679  const char* src_file,
680  ulint src_line);
681 /*******************************************************************/
687 UNIV_INLINE
688 ibool
689 pfs_os_file_read_func(
690 /*==================*/
691  os_file_t file,
692  void* buf,
693  ulint offset,
695  ulint offset_high,
697  ulint n,
698  const char* src_file,
699  ulint src_line);
701 /*******************************************************************/
708 UNIV_INLINE
709 ibool
710 pfs_os_file_read_no_error_handling_func(
711 /*====================================*/
712  os_file_t file,
713  void* buf,
714  ulint offset,
716  ulint offset_high,
718  ulint n,
719  const char* src_file,
720  ulint src_line);
722 /*******************************************************************/
728 UNIV_INLINE
729 ibool
730 pfs_os_aio_func(
731 /*============*/
732  ulint type,
733  ulint mode,
734  const char* name,
736  os_file_t file,
737  void* buf,
739  ulint offset,
741  ulint offset_high,
743  ulint n,
744  fil_node_t* message1,
748  void* message2,
752  const char* src_file,
753  ulint src_line);
754 /*******************************************************************/
760 UNIV_INLINE
761 ibool
762 pfs_os_file_write_func(
763 /*===================*/
764  const char* name,
766  os_file_t file,
767  const void* buf,
768  ulint offset,
770  ulint offset_high,
772  ulint n,
773  const char* src_file,
774  ulint src_line);
775 /***********************************************************************/
782 UNIV_INLINE
783 ibool
784 pfs_os_file_flush_func(
785 /*===================*/
786  os_file_t file,
787  const char* src_file,
788  ulint src_line);
790 /***********************************************************************/
796 UNIV_INLINE
797 ibool
798 pfs_os_file_rename_func(
799 /*====================*/
800  mysql_pfs_key_t key,
801  const char* oldpath,
803  const char* newpath,
804  const char* src_file,
805  ulint src_line);
806 #endif /* UNIV_PFS_IO */
807 
808 #ifdef UNIV_HOTBACKUP
809 /***********************************************************************/
812 UNIV_INTERN
813 ibool
814 os_file_close_no_error_handling(
815 /*============================*/
816  os_file_t file);
817 #endif /* UNIV_HOTBACKUP */
818 /***********************************************************************/
821 UNIV_INTERN
822 ibool
824 /*=============*/
825  os_file_t file,
826  ulint* size,
828  ulint* size_high);
829 /***********************************************************************/
832 UNIV_INTERN
833 ib_int64_t
835 /*===========================*/
836  os_file_t file);
837 /***********************************************************************/
840 UNIV_INTERN
841 ibool
843 /*=============*/
844  const char* name,
846  os_file_t file,
847  ulint size,
849  ulint size_high);
850 /***********************************************************************/
853 UNIV_INTERN
854 ibool
856 /*============*/
857  FILE* file);
858 /***********************************************************************/
862 UNIV_INTERN
863 ibool
865 /*===============*/
866  os_file_t file);
867 /***********************************************************************/
873 UNIV_INTERN
874 ulint
876 /*===================*/
877  ibool report_all_errors);
879 /*******************************************************************/
883 UNIV_INTERN
884 ibool
886 /*==============*/
887  os_file_t file,
888  void* buf,
889  ulint offset,
891  ulint offset_high,
893  ulint n);
894 /*******************************************************************/
898 UNIV_INTERN
899 void
901 /*================*/
902  FILE* file,
903  char* str,
904  ulint size);
905 /*******************************************************************/
911 UNIV_INTERN
912 ibool
914 /*================================*/
915  os_file_t file,
916  void* buf,
917  ulint offset,
919  ulint offset_high,
921  ulint n);
923 /*******************************************************************/
928 UNIV_INTERN
929 ibool
931 /*===============*/
932  const char* name,
934  os_file_t file,
935  const void* buf,
936  ulint offset,
938  ulint offset_high,
940  ulint n);
941 /*******************************************************************/
944 UNIV_INTERN
945 ibool
947 /*===========*/
948  const char* path,
949  ibool* exists,
950  os_file_type_t* type);
951 /****************************************************************/
979 UNIV_INTERN
980 char*
982 /*============*/
983  const char* path);
984 /****************************************************************/
987 UNIV_INTERN
988 ibool
990 /*=============================*/
991  const char* path);
992 /***********************************************************************
993 Initializes the asynchronous io system. Creates one array each for ibuf
994 and log i/o. Also creates one array each for read and write where each
995 array is divided logically into n_read_segs and n_write_segs
996 respectively. The caller must create an i/o handler thread for each
997 segment in these arrays. This function also creates the sync array.
998 No i/o handler thread needs to be created for that */
999 UNIV_INTERN
1000 ibool
1001 os_aio_init(
1002 /*========*/
1003  ulint n_per_seg, /*<! in: maximum number of pending aio
1004  operations allowed per segment */
1005  ulint n_read_segs, /*<! in: number of reader threads */
1006  ulint n_write_segs, /*<! in: number of writer threads */
1007  ulint n_slots_sync); /*<! in: number of slots in the sync aio
1008  array */
1009 /***********************************************************************
1010 Frees the asynchronous io system. */
1011 UNIV_INTERN
1012 void
1013 os_aio_free(void);
1014 /*=============*/
1015 
1016 /*******************************************************************/
1020 UNIV_INTERN
1021 ibool
1022 os_aio_func(
1023 /*========*/
1024  ulint type,
1025  ulint mode,
1038  const char* name,
1040  os_file_t file,
1041  void* buf,
1043  ulint offset,
1045  ulint offset_high,
1047  ulint n,
1048  fil_node_t* message1,
1052  void* message2);
1056 /************************************************************************/
1059 UNIV_INTERN
1060 void
1062 /*=====================================*/
1063 /************************************************************************/
1066 UNIV_INTERN
1067 void
1069 /*=====================================*/
1070 /**********************************************************************/
1072 UNIV_INTERN
1073 void
1075 /*=======================================*/
1076 /**********************************************************************/
1081 UNIV_INTERN
1082 void
1084 /*============================================*/
1085 
1086 #ifdef WIN_ASYNC_IO
1087 /**********************************************************************/
1095 UNIV_INTERN
1096 ibool
1097 os_aio_windows_handle(
1098 /*==================*/
1099  ulint segment,
1107  ulint pos,
1109  fil_node_t**message1,
1114  void** message2,
1115  ulint* type);
1116 #endif
1117 
1118 /**********************************************************************/
1122 UNIV_INTERN
1123 ibool
1125 /*====================*/
1126  ulint segment,
1131  fil_node_t**message1,
1136  void** message2,
1137  ulint* type);
1138 /**********************************************************************/
1141 UNIV_INTERN
1142 ibool
1143 os_aio_validate(void);
1144 /*=================*/
1145 /**********************************************************************/
1147 UNIV_INTERN
1148 void
1149 os_aio_print(
1150 /*=========*/
1151  FILE* file);
1152 /**********************************************************************/
1154 UNIV_INTERN
1155 void
1156 os_aio_refresh_stats(void);
1157 /*======================*/
1158 
1159 #ifdef UNIV_DEBUG
1160 /**********************************************************************/
1163 UNIV_INTERN
1164 ibool
1165 os_aio_all_slots_free(void);
1166 /*=======================*/
1167 #endif /* UNIV_DEBUG */
1168 
1169 /*******************************************************************/
1172 UNIV_INTERN
1173 ibool
1175 /*===============*/
1176  const char* path,
1177  os_file_stat_t* stat_info);
1180 #if !defined(UNIV_HOTBACKUP)
1181 /*********************************************************************/
1185 UNIV_INTERN
1186 int
1188 /*========================*/
1189 #endif /* !UNIV_HOTBACKUP */
1190 
1191 
1192 #if defined(LINUX_NATIVE_AIO)
1193 /**************************************************************************
1194 This function is only used in Linux native asynchronous i/o.
1195 Waits for an aio operation to complete. This function is used to wait the
1196 for completed requests. The aio array of pending requests is divided
1197 into segments. The thread specifies which segment or slot it wants to wait
1198 for. NOTE: this function will also take care of freeing the aio slot,
1199 therefore no other thread is allowed to do the freeing!
1200 @return TRUE if the IO was successful */
1201 UNIV_INTERN
1202 ibool
1203 os_aio_linux_handle(
1204 /*================*/
1205  ulint global_seg,
1211  fil_node_t**message1,
1212  void** message2,
1216  ulint* type);
1217 #endif /* LINUX_NATIVE_AIO */
1218 
1219 #ifndef UNIV_NONINL
1220 #include "os0file.ic"
1221 #endif
1222 
1223 #endif