]>
Commit | Line | Data |
---|---|---|
587fec42 LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2015 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
20 | #include <sys/sendfile.h> | |
cf0fbc49 TA |
21 | |
22 | /* When we include libgen.h because we need dirname() we immediately | |
23 | * undefine basename() since libgen.h defines it as a macro to the POSIX | |
24 | * version which is really broken. We prefer GNU basename(). */ | |
587fec42 LP |
25 | #include <libgen.h> |
26 | #undef basename | |
27 | ||
28 | #include "sd-daemon.h" | |
07630cea | 29 | |
b5efdb8a | 30 | #include "alloc-util.h" |
587fec42 LP |
31 | #include "btrfs-util.h" |
32 | #include "copy.h" | |
587fec42 | 33 | #include "export-raw.h" |
3ffd4af2 | 34 | #include "fd-util.h" |
0d39fa9c | 35 | #include "fileio.h" |
07630cea | 36 | #include "import-common.h" |
4a6d3523 | 37 | #include "missing.h" |
07630cea LP |
38 | #include "ratelimit.h" |
39 | #include "string-util.h" | |
40 | #include "util.h" | |
587fec42 LP |
41 | |
42 | #define COPY_BUFFER_SIZE (16*1024) | |
43 | ||
44 | struct RawExport { | |
45 | sd_event *event; | |
46 | ||
47 | RawExportFinished on_finished; | |
48 | void *userdata; | |
49 | ||
50 | char *path; | |
51 | ||
52 | int input_fd; | |
53 | int output_fd; | |
54 | ||
55 | ImportCompress compress; | |
56 | ||
57 | sd_event_source *output_event_source; | |
58 | ||
59 | void *buffer; | |
60 | size_t buffer_size; | |
61 | size_t buffer_allocated; | |
62 | ||
63 | uint64_t written_compressed; | |
64 | uint64_t written_uncompressed; | |
65 | ||
66 | unsigned last_percent; | |
67 | RateLimit progress_rate_limit; | |
68 | ||
69 | struct stat st; | |
70 | ||
71 | bool eof; | |
72 | bool tried_reflink; | |
73 | bool tried_sendfile; | |
74 | }; | |
75 | ||
76 | RawExport *raw_export_unref(RawExport *e) { | |
77 | if (!e) | |
78 | return NULL; | |
79 | ||
80 | sd_event_source_unref(e->output_event_source); | |
81 | ||
82 | import_compress_free(&e->compress); | |
83 | ||
84 | sd_event_unref(e->event); | |
85 | ||
86 | safe_close(e->input_fd); | |
87 | ||
88 | free(e->buffer); | |
89 | free(e->path); | |
6b430fdb | 90 | return mfree(e); |
587fec42 LP |
91 | } |
92 | ||
93 | int raw_export_new( | |
94 | RawExport **ret, | |
95 | sd_event *event, | |
96 | RawExportFinished on_finished, | |
97 | void *userdata) { | |
98 | ||
99 | _cleanup_(raw_export_unrefp) RawExport *e = NULL; | |
100 | int r; | |
101 | ||
102 | assert(ret); | |
103 | ||
104 | e = new0(RawExport, 1); | |
105 | if (!e) | |
106 | return -ENOMEM; | |
107 | ||
108 | e->output_fd = e->input_fd = -1; | |
109 | e->on_finished = on_finished; | |
110 | e->userdata = userdata; | |
111 | ||
112 | RATELIMIT_INIT(e->progress_rate_limit, 100 * USEC_PER_MSEC, 1); | |
113 | e->last_percent = (unsigned) -1; | |
114 | ||
115 | if (event) | |
116 | e->event = sd_event_ref(event); | |
117 | else { | |
118 | r = sd_event_default(&e->event); | |
119 | if (r < 0) | |
120 | return r; | |
121 | } | |
122 | ||
123 | *ret = e; | |
124 | e = NULL; | |
125 | ||
126 | return 0; | |
127 | } | |
128 | ||
129 | static void raw_export_report_progress(RawExport *e) { | |
130 | unsigned percent; | |
131 | assert(e); | |
132 | ||
133 | if (e->written_uncompressed >= (uint64_t) e->st.st_size) | |
134 | percent = 100; | |
135 | else | |
136 | percent = (unsigned) ((e->written_uncompressed * UINT64_C(100)) / (uint64_t) e->st.st_size); | |
137 | ||
138 | if (percent == e->last_percent) | |
139 | return; | |
140 | ||
141 | if (!ratelimit_test(&e->progress_rate_limit)) | |
142 | return; | |
143 | ||
144 | sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent); | |
145 | log_info("Exported %u%%.", percent); | |
146 | ||
147 | e->last_percent = percent; | |
148 | } | |
149 | ||
150 | static int raw_export_process(RawExport *e) { | |
151 | ssize_t l; | |
152 | int r; | |
153 | ||
154 | assert(e); | |
155 | ||
156 | if (!e->tried_reflink && e->compress.type == IMPORT_COMPRESS_UNCOMPRESSED) { | |
157 | ||
158 | /* If we shall take an uncompressed snapshot we can | |
159 | * reflink source to destination directly. Let's see | |
160 | * if this works. */ | |
161 | ||
162 | r = btrfs_reflink(e->input_fd, e->output_fd); | |
163 | if (r >= 0) { | |
164 | r = 0; | |
165 | goto finish; | |
166 | } | |
167 | ||
168 | e->tried_reflink = true; | |
169 | } | |
170 | ||
171 | if (!e->tried_sendfile && e->compress.type == IMPORT_COMPRESS_UNCOMPRESSED) { | |
172 | ||
173 | l = sendfile(e->output_fd, e->input_fd, NULL, COPY_BUFFER_SIZE); | |
174 | if (l < 0) { | |
175 | if (errno == EAGAIN) | |
176 | return 0; | |
177 | ||
178 | e->tried_sendfile = true; | |
179 | } else if (l == 0) { | |
180 | r = 0; | |
181 | goto finish; | |
182 | } else { | |
183 | e->written_uncompressed += l; | |
184 | e->written_compressed += l; | |
185 | ||
186 | raw_export_report_progress(e); | |
187 | ||
188 | return 0; | |
189 | } | |
190 | } | |
191 | ||
192 | while (e->buffer_size <= 0) { | |
193 | uint8_t input[COPY_BUFFER_SIZE]; | |
194 | ||
195 | if (e->eof) { | |
196 | r = 0; | |
197 | goto finish; | |
198 | } | |
199 | ||
200 | l = read(e->input_fd, input, sizeof(input)); | |
201 | if (l < 0) { | |
202 | r = log_error_errno(errno, "Failed to read raw file: %m"); | |
203 | goto finish; | |
204 | } | |
205 | ||
206 | if (l == 0) { | |
207 | e->eof = true; | |
208 | r = import_compress_finish(&e->compress, &e->buffer, &e->buffer_size, &e->buffer_allocated); | |
209 | } else { | |
210 | e->written_uncompressed += l; | |
211 | r = import_compress(&e->compress, input, l, &e->buffer, &e->buffer_size, &e->buffer_allocated); | |
212 | } | |
213 | if (r < 0) { | |
214 | r = log_error_errno(r, "Failed to encode: %m"); | |
215 | goto finish; | |
216 | } | |
217 | } | |
218 | ||
219 | l = write(e->output_fd, e->buffer, e->buffer_size); | |
220 | if (l < 0) { | |
221 | if (errno == EAGAIN) | |
222 | return 0; | |
223 | ||
224 | r = log_error_errno(errno, "Failed to write output file: %m"); | |
225 | goto finish; | |
226 | } | |
227 | ||
228 | assert((size_t) l <= e->buffer_size); | |
229 | memmove(e->buffer, (uint8_t*) e->buffer + l, e->buffer_size - l); | |
230 | e->buffer_size -= l; | |
231 | e->written_compressed += l; | |
232 | ||
233 | raw_export_report_progress(e); | |
234 | ||
235 | return 0; | |
236 | ||
237 | finish: | |
238 | if (r >= 0) { | |
239 | (void) copy_times(e->input_fd, e->output_fd); | |
240 | (void) copy_xattr(e->input_fd, e->output_fd); | |
241 | } | |
242 | ||
243 | if (e->on_finished) | |
244 | e->on_finished(e, r, e->userdata); | |
245 | else | |
246 | sd_event_exit(e->event, r); | |
247 | ||
248 | return 0; | |
249 | } | |
250 | ||
251 | static int raw_export_on_output(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
252 | RawExport *i = userdata; | |
253 | ||
254 | return raw_export_process(i); | |
255 | } | |
256 | ||
257 | static int raw_export_on_defer(sd_event_source *s, void *userdata) { | |
258 | RawExport *i = userdata; | |
259 | ||
260 | return raw_export_process(i); | |
261 | } | |
262 | ||
263 | static int reflink_snapshot(int fd, const char *path) { | |
264 | char *p, *d; | |
265 | int new_fd, r; | |
266 | ||
267 | p = strdupa(path); | |
268 | d = dirname(p); | |
269 | ||
270 | new_fd = open(d, O_TMPFILE|O_CLOEXEC|O_NOCTTY|O_RDWR, 0600); | |
271 | if (new_fd < 0) { | |
272 | _cleanup_free_ char *t = NULL; | |
273 | ||
14bcf25c | 274 | r = tempfn_random(path, NULL, &t); |
587fec42 LP |
275 | if (r < 0) |
276 | return r; | |
277 | ||
278 | new_fd = open(t, O_CLOEXEC|O_CREAT|O_NOCTTY|O_RDWR, 0600); | |
279 | if (new_fd < 0) | |
280 | return -errno; | |
281 | ||
282 | (void) unlink(t); | |
283 | } | |
284 | ||
285 | r = btrfs_reflink(fd, new_fd); | |
286 | if (r < 0) { | |
287 | safe_close(new_fd); | |
288 | return r; | |
289 | } | |
290 | ||
291 | return new_fd; | |
292 | } | |
293 | ||
294 | int raw_export_start(RawExport *e, const char *path, int fd, ImportCompressType compress) { | |
295 | _cleanup_close_ int sfd = -1, tfd = -1; | |
296 | int r; | |
297 | ||
298 | assert(e); | |
299 | assert(path); | |
300 | assert(fd >= 0); | |
301 | assert(compress < _IMPORT_COMPRESS_TYPE_MAX); | |
302 | assert(compress != IMPORT_COMPRESS_UNKNOWN); | |
303 | ||
304 | if (e->output_fd >= 0) | |
305 | return -EBUSY; | |
306 | ||
307 | r = fd_nonblock(fd, true); | |
308 | if (r < 0) | |
309 | return r; | |
310 | ||
311 | r = free_and_strdup(&e->path, path); | |
312 | if (r < 0) | |
313 | return r; | |
314 | ||
315 | sfd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY); | |
316 | if (sfd < 0) | |
317 | return -errno; | |
318 | ||
319 | if (fstat(sfd, &e->st) < 0) | |
320 | return -errno; | |
321 | if (!S_ISREG(e->st.st_mode)) | |
322 | return -ENOTTY; | |
323 | ||
324 | /* Try to take a reflink snapshot of the file, if we can t make the export atomic */ | |
325 | tfd = reflink_snapshot(sfd, path); | |
326 | if (tfd >= 0) { | |
327 | e->input_fd = tfd; | |
328 | tfd = -1; | |
329 | } else { | |
330 | e->input_fd = sfd; | |
331 | sfd = -1; | |
332 | } | |
333 | ||
334 | r = import_compress_init(&e->compress, compress); | |
335 | if (r < 0) | |
336 | return r; | |
337 | ||
338 | r = sd_event_add_io(e->event, &e->output_event_source, fd, EPOLLOUT, raw_export_on_output, e); | |
339 | if (r == -EPERM) { | |
340 | r = sd_event_add_defer(e->event, &e->output_event_source, raw_export_on_defer, e); | |
341 | if (r < 0) | |
342 | return r; | |
343 | ||
344 | r = sd_event_source_set_enabled(e->output_event_source, SD_EVENT_ON); | |
345 | } | |
346 | if (r < 0) | |
347 | return r; | |
348 | ||
349 | e->output_fd = fd; | |
350 | return r; | |
351 | } |