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