MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_sync.c
1 /* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 #include "mysys_priv.h"
17 #include "mysys_err.h"
18 #include <errno.h>
19 
20 static void (*before_sync_wait)(void)= 0;
21 static void (*after_sync_wait)(void)= 0;
22 
23 void thr_set_sync_wait_callback(void (*before_wait)(void),
24  void (*after_wait)(void))
25 {
26  before_sync_wait= before_wait;
27  after_sync_wait= after_wait;
28 }
29 
30 /*
31  Sync data in file to disk
32 
33  SYNOPSIS
34  my_sync()
35  fd File descritor to sync
36  my_flags Flags (now only MY_WME is supported)
37 
38  NOTE
39  If file system supports its, only file data is synced, not inode data.
40 
41  MY_IGNORE_BADFD is useful when fd is "volatile" - not protected by a
42  mutex. In this case by the time of fsync(), fd may be already closed by
43  another thread, or even reassigned to a different file. With this flag -
44  MY_IGNORE_BADFD - such a situation will not be considered an error.
45  (which is correct behaviour, if we know that the other thread synced the
46  file before closing)
47 
48  RETURN
49  0 ok
50  -1 error
51 */
52 
53 int my_sync(File fd, myf my_flags)
54 {
55  int res;
56  DBUG_ENTER("my_sync");
57  DBUG_PRINT("my",("Fd: %d my_flags: %d", fd, my_flags));
58 
59  if (before_sync_wait)
60  (*before_sync_wait)();
61  do
62  {
63 #if defined(F_FULLFSYNC)
64  /*
65  In Mac OS X >= 10.3 this call is safer than fsync() (it forces the
66  disk's cache and guarantees ordered writes).
67  */
68  if (!(res= fcntl(fd, F_FULLFSYNC, 0)))
69  break; /* ok */
70  /* Some file systems don't support F_FULLFSYNC and fail above: */
71  DBUG_PRINT("info",("fcntl(F_FULLFSYNC) failed, falling back"));
72 #endif
73 #if defined(HAVE_FDATASYNC) && HAVE_DECL_FDATASYNC
74  res= fdatasync(fd);
75 #elif defined(HAVE_FSYNC)
76  res= fsync(fd);
77 #elif defined(_WIN32)
78  res= my_win_fsync(fd);
79 #else
80 #error Cannot find a way to sync a file, durability in danger
81  res= 0; /* No sync (strange OS) */
82 #endif
83  } while (res == -1 && errno == EINTR);
84 
85  if (res)
86  {
87  int er= errno;
88  if (!(my_errno= er))
89  my_errno= -1; /* Unknown error */
90  if (after_sync_wait)
91  (*after_sync_wait)();
92  if ((my_flags & MY_IGNORE_BADFD) &&
93  (er == EBADF || er == EINVAL || er == EROFS))
94  {
95  DBUG_PRINT("info", ("ignoring errno %d", er));
96  res= 0;
97  }
98  else if (my_flags & MY_WME)
99  {
100  char errbuf[MYSYS_STRERROR_SIZE];
101  my_error(EE_SYNC, MYF(ME_BELL+ME_WAITTANG), my_filename(fd),
102  my_errno, my_strerror(errbuf, sizeof(errbuf), my_errno));
103  }
104  }
105  else
106  {
107  if (after_sync_wait)
108  (*after_sync_wait)();
109  }
110  DBUG_RETURN(res);
111 } /* my_sync */
112 
113 
114 static const char cur_dir_name[]= {FN_CURLIB, 0};
115 
116 
117 /*
118  Force directory information to disk.
119 
120  SYNOPSIS
121  my_sync_dir()
122  dir_name the name of the directory
123  my_flags flags (MY_WME etc)
124 
125  RETURN
126  0 if ok, !=0 if error
127 */
128 
129 #ifdef NEED_EXPLICIT_SYNC_DIR
130 
131 int my_sync_dir(const char *dir_name, myf my_flags)
132 {
133  File dir_fd;
134  int res= 0;
135  const char *correct_dir_name;
136  DBUG_ENTER("my_sync_dir");
137  DBUG_PRINT("my",("Dir: '%s' my_flags: %d", dir_name, my_flags));
138  /* Sometimes the path does not contain an explicit directory */
139  correct_dir_name= (dir_name[0] == 0) ? cur_dir_name : dir_name;
140  /*
141  Syncing a dir may give EINVAL on tmpfs on Linux, which is ok.
142  EIO on the other hand is very important. Hence MY_IGNORE_BADFD.
143  */
144  if ((dir_fd= my_open(correct_dir_name, O_RDONLY, MYF(my_flags))) >= 0)
145  {
146  if (my_sync(dir_fd, MYF(my_flags | MY_IGNORE_BADFD)))
147  res= 2;
148  if (my_close(dir_fd, MYF(my_flags)))
149  res= 3;
150  }
151  else
152  res= 1;
153  DBUG_RETURN(res);
154 }
155 
156 #else /* NEED_EXPLICIT_SYNC_DIR */
157 
158 int my_sync_dir(const char *dir_name __attribute__((unused)),
159  myf my_flags __attribute__((unused)))
160 {
161  return 0;
162 }
163 
164 #endif /* NEED_EXPLICIT_SYNC_DIR */
165 
166 
167 /*
168  Force directory information to disk.
169 
170  SYNOPSIS
171  my_sync_dir_by_file()
172  file_name the name of a file in the directory
173  my_flags flags (MY_WME etc)
174 
175  RETURN
176  0 if ok, !=0 if error
177 */
178 
179 #ifdef NEED_EXPLICIT_SYNC_DIR
180 
181 int my_sync_dir_by_file(const char *file_name, myf my_flags)
182 {
183  char dir_name[FN_REFLEN];
184  size_t dir_name_length;
185  dirname_part(dir_name, file_name, &dir_name_length);
186  return my_sync_dir(dir_name, my_flags);
187 }
188 
189 #else /* NEED_EXPLICIT_SYNC_DIR */
190 
191 int my_sync_dir_by_file(const char *file_name __attribute__((unused)),
192  myf my_flags __attribute__((unused)))
193 {
194  return 0;
195 }
196 
197 #endif /* NEED_EXPLICIT_SYNC_DIR */
198