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