]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/import/export-raw.c
Merge pull request #10919 from yuwata/sd-device-monitor-fixes
[thirdparty/systemd.git] / src / import / export-raw.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
587fec42
LP
2
3#include <sys/sendfile.h>
cf0fbc49
TA
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(). */
587fec42
LP
8#include <libgen.h>
9#undef basename
10
11#include "sd-daemon.h"
07630cea 12
b5efdb8a 13#include "alloc-util.h"
587fec42
LP
14#include "btrfs-util.h"
15#include "copy.h"
587fec42 16#include "export-raw.h"
3ffd4af2 17#include "fd-util.h"
0d39fa9c 18#include "fileio.h"
0c462ea4 19#include "fs-util.h"
07630cea 20#include "import-common.h"
4a6d3523 21#include "missing.h"
07630cea 22#include "ratelimit.h"
3cc44114 23#include "stat-util.h"
07630cea
LP
24#include "string-util.h"
25#include "util.h"
587fec42
LP
26
27#define COPY_BUFFER_SIZE (16*1024)
28
29struct 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
61RawExport *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);
6b430fdb 75 return mfree(e);
587fec42
LP
76}
77
78int 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
0d94088e 89 e = new(RawExport, 1);
587fec42
LP
90 if (!e)
91 return -ENOMEM;
92
0d94088e
YW
93 *e = (RawExport) {
94 .output_fd = -1,
95 .input_fd = -1,
96 .on_finished = on_finished,
97 .userdata = userdata,
98 .last_percent = (unsigned) -1,
99 };
587fec42
LP
100
101 RATELIMIT_INIT(e->progress_rate_limit, 100 * USEC_PER_MSEC, 1);
587fec42
LP
102
103 if (event)
104 e->event = sd_event_ref(event);
105 else {
106 r = sd_event_default(&e->event);
107 if (r < 0)
108 return r;
109 }
110
1cc6c93a 111 *ret = TAKE_PTR(e);
587fec42
LP
112
113 return 0;
114}
115
116static void raw_export_report_progress(RawExport *e) {
117 unsigned percent;
118 assert(e);
119
120 if (e->written_uncompressed >= (uint64_t) e->st.st_size)
121 percent = 100;
122 else
123 percent = (unsigned) ((e->written_uncompressed * UINT64_C(100)) / (uint64_t) e->st.st_size);
124
125 if (percent == e->last_percent)
126 return;
127
7994ac1d 128 if (!ratelimit_below(&e->progress_rate_limit))
587fec42
LP
129 return;
130
131 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
132 log_info("Exported %u%%.", percent);
133
134 e->last_percent = percent;
135}
136
137static int raw_export_process(RawExport *e) {
138 ssize_t l;
139 int r;
140
141 assert(e);
142
143 if (!e->tried_reflink && e->compress.type == IMPORT_COMPRESS_UNCOMPRESSED) {
144
145 /* If we shall take an uncompressed snapshot we can
146 * reflink source to destination directly. Let's see
147 * if this works. */
148
149 r = btrfs_reflink(e->input_fd, e->output_fd);
150 if (r >= 0) {
151 r = 0;
152 goto finish;
153 }
154
155 e->tried_reflink = true;
156 }
157
158 if (!e->tried_sendfile && e->compress.type == IMPORT_COMPRESS_UNCOMPRESSED) {
159
160 l = sendfile(e->output_fd, e->input_fd, NULL, COPY_BUFFER_SIZE);
161 if (l < 0) {
162 if (errno == EAGAIN)
163 return 0;
164
165 e->tried_sendfile = true;
166 } else if (l == 0) {
167 r = 0;
168 goto finish;
169 } else {
170 e->written_uncompressed += l;
171 e->written_compressed += l;
172
173 raw_export_report_progress(e);
174
175 return 0;
176 }
177 }
178
179 while (e->buffer_size <= 0) {
180 uint8_t input[COPY_BUFFER_SIZE];
181
182 if (e->eof) {
183 r = 0;
184 goto finish;
185 }
186
187 l = read(e->input_fd, input, sizeof(input));
188 if (l < 0) {
189 r = log_error_errno(errno, "Failed to read raw file: %m");
190 goto finish;
191 }
192
193 if (l == 0) {
194 e->eof = true;
195 r = import_compress_finish(&e->compress, &e->buffer, &e->buffer_size, &e->buffer_allocated);
196 } else {
197 e->written_uncompressed += l;
198 r = import_compress(&e->compress, input, l, &e->buffer, &e->buffer_size, &e->buffer_allocated);
199 }
200 if (r < 0) {
201 r = log_error_errno(r, "Failed to encode: %m");
202 goto finish;
203 }
204 }
205
206 l = write(e->output_fd, e->buffer, e->buffer_size);
207 if (l < 0) {
208 if (errno == EAGAIN)
209 return 0;
210
211 r = log_error_errno(errno, "Failed to write output file: %m");
212 goto finish;
213 }
214
215 assert((size_t) l <= e->buffer_size);
216 memmove(e->buffer, (uint8_t*) e->buffer + l, e->buffer_size - l);
217 e->buffer_size -= l;
218 e->written_compressed += l;
219
220 raw_export_report_progress(e);
221
222 return 0;
223
224finish:
225 if (r >= 0) {
226 (void) copy_times(e->input_fd, e->output_fd);
227 (void) copy_xattr(e->input_fd, e->output_fd);
228 }
229
230 if (e->on_finished)
231 e->on_finished(e, r, e->userdata);
232 else
233 sd_event_exit(e->event, r);
234
235 return 0;
236}
237
238static int raw_export_on_output(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
239 RawExport *i = userdata;
240
241 return raw_export_process(i);
242}
243
244static int raw_export_on_defer(sd_event_source *s, void *userdata) {
245 RawExport *i = userdata;
246
247 return raw_export_process(i);
248}
249
250static int reflink_snapshot(int fd, const char *path) {
587fec42
LP
251 int new_fd, r;
252
0c462ea4 253 new_fd = open_parent(path, O_TMPFILE|O_CLOEXEC|O_RDWR, 0600);
587fec42
LP
254 if (new_fd < 0) {
255 _cleanup_free_ char *t = NULL;
256
14bcf25c 257 r = tempfn_random(path, NULL, &t);
587fec42
LP
258 if (r < 0)
259 return r;
260
261 new_fd = open(t, O_CLOEXEC|O_CREAT|O_NOCTTY|O_RDWR, 0600);
262 if (new_fd < 0)
263 return -errno;
264
265 (void) unlink(t);
266 }
267
268 r = btrfs_reflink(fd, new_fd);
269 if (r < 0) {
270 safe_close(new_fd);
271 return r;
272 }
273
274 return new_fd;
275}
276
277int raw_export_start(RawExport *e, const char *path, int fd, ImportCompressType compress) {
278 _cleanup_close_ int sfd = -1, tfd = -1;
279 int r;
280
281 assert(e);
282 assert(path);
283 assert(fd >= 0);
284 assert(compress < _IMPORT_COMPRESS_TYPE_MAX);
285 assert(compress != IMPORT_COMPRESS_UNKNOWN);
286
287 if (e->output_fd >= 0)
288 return -EBUSY;
289
290 r = fd_nonblock(fd, true);
291 if (r < 0)
292 return r;
293
294 r = free_and_strdup(&e->path, path);
295 if (r < 0)
296 return r;
297
298 sfd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
299 if (sfd < 0)
300 return -errno;
301
302 if (fstat(sfd, &e->st) < 0)
303 return -errno;
3cc44114
LP
304 r = stat_verify_regular(&e->st);
305 if (r < 0)
306 return r;
587fec42
LP
307
308 /* Try to take a reflink snapshot of the file, if we can t make the export atomic */
309 tfd = reflink_snapshot(sfd, path);
c10d6bdb
LP
310 if (tfd >= 0)
311 e->input_fd = TAKE_FD(tfd);
312 else
313 e->input_fd = TAKE_FD(sfd);
587fec42
LP
314
315 r = import_compress_init(&e->compress, compress);
316 if (r < 0)
317 return r;
318
319 r = sd_event_add_io(e->event, &e->output_event_source, fd, EPOLLOUT, raw_export_on_output, e);
320 if (r == -EPERM) {
321 r = sd_event_add_defer(e->event, &e->output_event_source, raw_export_on_defer, e);
322 if (r < 0)
323 return r;
324
325 r = sd_event_source_set_enabled(e->output_event_source, SD_EVENT_ON);
326 }
327 if (r < 0)
328 return r;
329
330 e->output_fd = fd;
331 return r;
332}