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