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