]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/import/export-raw.c
tree-wide: remove Lennart's copyright lines
[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 "import-common.h"
20 #include "missing.h"
21 #include "ratelimit.h"
22 #include "stat-util.h"
23 #include "string-util.h"
24 #include "util.h"
25
26 #define COPY_BUFFER_SIZE (16*1024)
27
28 struct RawExport {
29 sd_event *event;
30
31 RawExportFinished on_finished;
32 void *userdata;
33
34 char *path;
35
36 int input_fd;
37 int output_fd;
38
39 ImportCompress compress;
40
41 sd_event_source *output_event_source;
42
43 void *buffer;
44 size_t buffer_size;
45 size_t buffer_allocated;
46
47 uint64_t written_compressed;
48 uint64_t written_uncompressed;
49
50 unsigned last_percent;
51 RateLimit progress_rate_limit;
52
53 struct stat st;
54
55 bool eof;
56 bool tried_reflink;
57 bool tried_sendfile;
58 };
59
60 RawExport *raw_export_unref(RawExport *e) {
61 if (!e)
62 return NULL;
63
64 sd_event_source_unref(e->output_event_source);
65
66 import_compress_free(&e->compress);
67
68 sd_event_unref(e->event);
69
70 safe_close(e->input_fd);
71
72 free(e->buffer);
73 free(e->path);
74 return mfree(e);
75 }
76
77 int raw_export_new(
78 RawExport **ret,
79 sd_event *event,
80 RawExportFinished on_finished,
81 void *userdata) {
82
83 _cleanup_(raw_export_unrefp) RawExport *e = NULL;
84 int r;
85
86 assert(ret);
87
88 e = new0(RawExport, 1);
89 if (!e)
90 return -ENOMEM;
91
92 e->output_fd = e->input_fd = -1;
93 e->on_finished = on_finished;
94 e->userdata = userdata;
95
96 RATELIMIT_INIT(e->progress_rate_limit, 100 * USEC_PER_MSEC, 1);
97 e->last_percent = (unsigned) -1;
98
99 if (event)
100 e->event = sd_event_ref(event);
101 else {
102 r = sd_event_default(&e->event);
103 if (r < 0)
104 return r;
105 }
106
107 *ret = TAKE_PTR(e);
108
109 return 0;
110 }
111
112 static void raw_export_report_progress(RawExport *e) {
113 unsigned percent;
114 assert(e);
115
116 if (e->written_uncompressed >= (uint64_t) e->st.st_size)
117 percent = 100;
118 else
119 percent = (unsigned) ((e->written_uncompressed * UINT64_C(100)) / (uint64_t) e->st.st_size);
120
121 if (percent == e->last_percent)
122 return;
123
124 if (!ratelimit_below(&e->progress_rate_limit))
125 return;
126
127 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
128 log_info("Exported %u%%.", percent);
129
130 e->last_percent = percent;
131 }
132
133 static int raw_export_process(RawExport *e) {
134 ssize_t l;
135 int r;
136
137 assert(e);
138
139 if (!e->tried_reflink && e->compress.type == IMPORT_COMPRESS_UNCOMPRESSED) {
140
141 /* If we shall take an uncompressed snapshot we can
142 * reflink source to destination directly. Let's see
143 * if this works. */
144
145 r = btrfs_reflink(e->input_fd, e->output_fd);
146 if (r >= 0) {
147 r = 0;
148 goto finish;
149 }
150
151 e->tried_reflink = true;
152 }
153
154 if (!e->tried_sendfile && e->compress.type == IMPORT_COMPRESS_UNCOMPRESSED) {
155
156 l = sendfile(e->output_fd, e->input_fd, NULL, COPY_BUFFER_SIZE);
157 if (l < 0) {
158 if (errno == EAGAIN)
159 return 0;
160
161 e->tried_sendfile = true;
162 } else if (l == 0) {
163 r = 0;
164 goto finish;
165 } else {
166 e->written_uncompressed += l;
167 e->written_compressed += l;
168
169 raw_export_report_progress(e);
170
171 return 0;
172 }
173 }
174
175 while (e->buffer_size <= 0) {
176 uint8_t input[COPY_BUFFER_SIZE];
177
178 if (e->eof) {
179 r = 0;
180 goto finish;
181 }
182
183 l = read(e->input_fd, input, sizeof(input));
184 if (l < 0) {
185 r = log_error_errno(errno, "Failed to read raw file: %m");
186 goto finish;
187 }
188
189 if (l == 0) {
190 e->eof = true;
191 r = import_compress_finish(&e->compress, &e->buffer, &e->buffer_size, &e->buffer_allocated);
192 } else {
193 e->written_uncompressed += l;
194 r = import_compress(&e->compress, input, l, &e->buffer, &e->buffer_size, &e->buffer_allocated);
195 }
196 if (r < 0) {
197 r = log_error_errno(r, "Failed to encode: %m");
198 goto finish;
199 }
200 }
201
202 l = write(e->output_fd, e->buffer, e->buffer_size);
203 if (l < 0) {
204 if (errno == EAGAIN)
205 return 0;
206
207 r = log_error_errno(errno, "Failed to write output file: %m");
208 goto finish;
209 }
210
211 assert((size_t) l <= e->buffer_size);
212 memmove(e->buffer, (uint8_t*) e->buffer + l, e->buffer_size - l);
213 e->buffer_size -= l;
214 e->written_compressed += l;
215
216 raw_export_report_progress(e);
217
218 return 0;
219
220 finish:
221 if (r >= 0) {
222 (void) copy_times(e->input_fd, e->output_fd);
223 (void) copy_xattr(e->input_fd, e->output_fd);
224 }
225
226 if (e->on_finished)
227 e->on_finished(e, r, e->userdata);
228 else
229 sd_event_exit(e->event, r);
230
231 return 0;
232 }
233
234 static int raw_export_on_output(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
235 RawExport *i = userdata;
236
237 return raw_export_process(i);
238 }
239
240 static int raw_export_on_defer(sd_event_source *s, void *userdata) {
241 RawExport *i = userdata;
242
243 return raw_export_process(i);
244 }
245
246 static int reflink_snapshot(int fd, const char *path) {
247 char *p, *d;
248 int new_fd, r;
249
250 p = strdupa(path);
251 d = dirname(p);
252
253 new_fd = open(d, O_TMPFILE|O_CLOEXEC|O_NOCTTY|O_RDWR, 0600);
254 if (new_fd < 0) {
255 _cleanup_free_ char *t = NULL;
256
257 r = tempfn_random(path, NULL, &t);
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
277 int 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;
304 r = stat_verify_regular(&e->st);
305 if (r < 0)
306 return r;
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);
310 if (tfd >= 0)
311 e->input_fd = TAKE_FD(tfd);
312 else
313 e->input_fd = TAKE_FD(sfd);
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 }