1 /******************************************************
2 Copyright (c) 2011-2013 Percona LLC and/or its affiliates.
3
4 Local datasink implementation for XtraBackup.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; version 2 of the License.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
18
19 *******************************************************/
20
21 #include <my_global.h>
22 #include <my_base.h>
23 #include <mysys_err.h>
24 #include "common.h"
25 #include "datasink.h"
26 #include "fsp0fsp.h"
27 #ifdef _WIN32
28 #include <winioctl.h>
29 #endif
30
31 typedef struct {
32 File fd;
33 my_bool init_ibd_done;
34 my_bool is_ibd;
35 my_bool compressed;
36 size_t pagesize;
37 } ds_local_file_t;
38
39 static ds_ctxt_t *local_init(const char *root);
40 static ds_file_t *local_open(ds_ctxt_t *ctxt, const char *path,
41 MY_STAT *mystat);
42 static int local_write(ds_file_t *file, const uchar *buf, size_t len);
43 static int local_close(ds_file_t *file);
44 static void local_deinit(ds_ctxt_t *ctxt);
45
local_remove(const char * path)46 static int local_remove(const char *path)
47 {
48 return unlink(path);
49 }
50
51 extern "C" {
52 datasink_t datasink_local = {
53 &local_init,
54 &local_open,
55 &local_write,
56 &local_close,
57 &local_remove,
58 &local_deinit
59 };
60 }
61
62 static
63 ds_ctxt_t *
local_init(const char * root)64 local_init(const char *root)
65 {
66 ds_ctxt_t *ctxt;
67
68 if (my_mkdir(root, 0777, MYF(0)) < 0
69 && my_errno != EEXIST && my_errno != EISDIR)
70 {
71 char errbuf[MYSYS_STRERROR_SIZE];
72 my_strerror(errbuf, sizeof(errbuf),my_errno);
73 my_error(EE_CANT_MKDIR, MYF(ME_BELL | ME_WAITTANG),
74 root, my_errno,errbuf, my_errno);
75 return NULL;
76 }
77
78 ctxt = (ds_ctxt_t *)my_malloc(sizeof(ds_ctxt_t), MYF(MY_FAE));
79
80 ctxt->root = my_strdup(root, MYF(MY_FAE));
81
82 return ctxt;
83 }
84
85 static
86 ds_file_t *
local_open(ds_ctxt_t * ctxt,const char * path,MY_STAT * mystat)87 local_open(ds_ctxt_t *ctxt, const char *path,
88 MY_STAT *mystat __attribute__((unused)))
89 {
90 char fullpath[FN_REFLEN];
91 char dirpath[FN_REFLEN];
92 size_t dirpath_len;
93 size_t path_len;
94 ds_local_file_t *local_file;
95 ds_file_t *file;
96 File fd;
97
98 fn_format(fullpath, path, ctxt->root, "", MYF(MY_RELATIVE_PATH));
99
100 /* Create the directory if needed */
101 dirname_part(dirpath, fullpath, &dirpath_len);
102 if (my_mkdir(dirpath, 0777, MYF(0)) < 0 && my_errno != EEXIST) {
103 char errbuf[MYSYS_STRERROR_SIZE];
104 my_strerror(errbuf, sizeof(errbuf), my_errno);
105 my_error(EE_CANT_MKDIR, MYF(ME_BELL | ME_WAITTANG),
106 dirpath, my_errno, errbuf);
107 return NULL;
108 }
109
110 fd = my_create(fullpath, 0, O_WRONLY | O_BINARY | O_EXCL | O_NOFOLLOW,
111 MYF(MY_WME));
112 if (fd < 0) {
113 return NULL;
114 }
115
116 path_len = strlen(fullpath) + 1; /* terminating '\0' */
117
118 file = (ds_file_t *) my_malloc(sizeof(ds_file_t) +
119 sizeof(ds_local_file_t) +
120 path_len,
121 MYF(MY_FAE));
122 local_file = (ds_local_file_t *) (file + 1);
123
124 local_file->fd = fd;
125 local_file->init_ibd_done = 0;
126 local_file->is_ibd = (path_len > 5) && !strcmp(fullpath + path_len - 5, ".ibd");
127 local_file->compressed = 0;
128 local_file->pagesize = 0;
129 file->path = (char *) local_file + sizeof(ds_local_file_t);
130 memcpy(file->path, fullpath, path_len);
131
132 file->ptr = local_file;
133
134 return file;
135 }
136
137 /* Calculate size of data without trailing zero bytes. */
trim_binary_zeros(uchar * buf,size_t pagesize)138 static size_t trim_binary_zeros(uchar *buf, size_t pagesize)
139 {
140 size_t i;
141 for (i = pagesize; (i > 0) && (buf[i - 1] == 0); i--) {};
142 return i;
143 }
144
145
146 /* Write data to the output file, and punch "holes" if needed. */
write_compressed(File fd,uchar * data,size_t len,size_t pagesize)147 static int write_compressed(File fd, uchar *data, size_t len, size_t pagesize)
148 {
149 uchar *ptr = data;
150 for (size_t written= 0; written < len;)
151 {
152 size_t n_bytes = MY_MIN(pagesize, len - written);
153 size_t datasize= trim_binary_zeros(ptr,n_bytes);
154 if (datasize > 0) {
155 if (!my_write(fd, ptr, datasize, MYF(MY_WME | MY_NABP)))
156 posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
157 else
158 return 1;
159 }
160 if (datasize < n_bytes) {
161 /* This punches a "hole" in the file. */
162 size_t hole_bytes = n_bytes - datasize;
163 if (my_seek(fd, hole_bytes, MY_SEEK_CUR, MYF(MY_WME | MY_NABP))
164 == MY_FILEPOS_ERROR)
165 return 1;
166 }
167 written += n_bytes;
168 ptr += n_bytes;
169 }
170 return 0;
171 }
172
173
174 /* Calculate Innodb tablespace specific data, when first page is written.
175 We're interested in page compression and page size.
176 */
init_ibd_data(ds_local_file_t * local_file,const uchar * buf,size_t len)177 static void init_ibd_data(ds_local_file_t *local_file, const uchar *buf, size_t len)
178 {
179 if (len < FIL_PAGE_DATA + FSP_SPACE_FLAGS) {
180 /* Weird, bail out.*/
181 return;
182 }
183
184 ulint flags = mach_read_from_4(&buf[FIL_PAGE_DATA + FSP_SPACE_FLAGS]);
185 ulint ssize = FSP_FLAGS_GET_PAGE_SSIZE(flags);
186 local_file->pagesize= ssize == 0 ? UNIV_PAGE_SIZE_ORIG : ((UNIV_ZIP_SIZE_MIN >> 1) << ssize);
187 local_file->compressed = (my_bool)FSP_FLAGS_HAS_PAGE_COMPRESSION(flags);
188
189 #if defined(_WIN32) && (MYSQL_VERSION_ID > 100200)
190 /* Make compressed file sparse, on Windows.
191 In 10.1, we do not use sparse files. */
192 if (local_file->compressed) {
193 HANDLE handle= my_get_osfhandle(local_file->fd);
194 if (!DeviceIoControl(handle, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, NULL, 0)) {
195 fprintf(stderr, "Warning: cannot make file sparse");
196 local_file->compressed = 0;
197 }
198 }
199 #endif
200 }
201
202
203 static
204 int
local_write(ds_file_t * file,const uchar * buf,size_t len)205 local_write(ds_file_t *file, const uchar *buf, size_t len)
206 {
207 uchar *b = (uchar*)buf;
208 ds_local_file_t *local_file= (ds_local_file_t *)file->ptr;
209 File fd = local_file->fd;
210
211 if (local_file->is_ibd && !local_file->init_ibd_done) {
212 init_ibd_data(local_file, b , len);
213 local_file->init_ibd_done= 1;
214 }
215
216 if (local_file->compressed) {
217 return write_compressed(fd, b, len, local_file->pagesize);
218 }
219
220 if (!my_write(fd, b , len, MYF(MY_WME | MY_NABP))) {
221 posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
222 return 0;
223 }
224 return 1;
225 }
226
227 /* Set EOF at file's current position.*/
set_eof(File fd)228 static int set_eof(File fd)
229 {
230 #ifdef _WIN32
231 return !SetEndOfFile(my_get_osfhandle(fd));
232 #elif defined(HAVE_FTRUNCATE)
233 return ftruncate(fd, my_tell(fd, MYF(MY_WME)));
234 #else
235 #error no ftruncate
236 #endif
237 }
238
239
240 static
241 int
local_close(ds_file_t * file)242 local_close(ds_file_t *file)
243 {
244 ds_local_file_t *local_file= (ds_local_file_t *)file->ptr;
245 File fd = local_file->fd;
246 int ret= 0;
247
248 if (local_file->compressed) {
249 ret = set_eof(fd);
250 }
251
252 my_close(fd, MYF(MY_WME));
253 my_free(file);
254 return ret;
255 }
256
257 static
258 void
local_deinit(ds_ctxt_t * ctxt)259 local_deinit(ds_ctxt_t *ctxt)
260 {
261 my_free(ctxt->root);
262 my_free(ctxt);
263 }
264