]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/import/export-tar.c
tree-wide: use -EBADF for fd initialization
[thirdparty/systemd.git] / src / import / export-tar.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-daemon.h"
4
5 #include "alloc-util.h"
6 #include "btrfs-util.h"
7 #include "export-tar.h"
8 #include "fd-util.h"
9 #include "import-common.h"
10 #include "process-util.h"
11 #include "ratelimit.h"
12 #include "string-util.h"
13 #include "tmpfile-util.h"
14
15 #define COPY_BUFFER_SIZE (16*1024)
16
17 struct TarExport {
18 sd_event *event;
19
20 TarExportFinished on_finished;
21 void *userdata;
22
23 char *path;
24 char *temp_path;
25
26 int output_fd;
27 int tar_fd;
28
29 ImportCompress compress;
30
31 sd_event_source *output_event_source;
32
33 void *buffer;
34 size_t buffer_size;
35 size_t buffer_allocated;
36
37 uint64_t written_compressed;
38 uint64_t written_uncompressed;
39
40 pid_t tar_pid;
41
42 struct stat st;
43 uint64_t quota_referenced;
44
45 unsigned last_percent;
46 RateLimit progress_ratelimit;
47
48 bool eof;
49 bool tried_splice;
50 };
51
52 TarExport *tar_export_unref(TarExport *e) {
53 if (!e)
54 return NULL;
55
56 sd_event_source_unref(e->output_event_source);
57
58 if (e->tar_pid > 1)
59 sigkill_wait(e->tar_pid);
60
61 if (e->temp_path) {
62 (void) btrfs_subvol_remove(e->temp_path, BTRFS_REMOVE_QUOTA);
63 free(e->temp_path);
64 }
65
66 import_compress_free(&e->compress);
67
68 sd_event_unref(e->event);
69
70 safe_close(e->tar_fd);
71
72 free(e->buffer);
73 free(e->path);
74 return mfree(e);
75 }
76
77 int tar_export_new(
78 TarExport **ret,
79 sd_event *event,
80 TarExportFinished on_finished,
81 void *userdata) {
82
83 _cleanup_(tar_export_unrefp) TarExport *e = NULL;
84 int r;
85
86 assert(ret);
87
88 e = new(TarExport, 1);
89 if (!e)
90 return -ENOMEM;
91
92 *e = (TarExport) {
93 .output_fd = -EBADF,
94 .tar_fd = -EBADF,
95 .on_finished = on_finished,
96 .userdata = userdata,
97 .quota_referenced = UINT64_MAX,
98 .last_percent = UINT_MAX,
99 .progress_ratelimit = { 100 * USEC_PER_MSEC, 1 },
100 };
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
110 *ret = TAKE_PTR(e);
111
112 return 0;
113 }
114
115 static void tar_export_report_progress(TarExport *e) {
116 unsigned percent;
117 assert(e);
118
119 /* Do we have any quota info? If not, we don't know anything about the progress */
120 if (e->quota_referenced == UINT64_MAX)
121 return;
122
123 if (e->written_uncompressed >= e->quota_referenced)
124 percent = 100;
125 else
126 percent = (unsigned) ((e->written_uncompressed * UINT64_C(100)) / e->quota_referenced);
127
128 if (percent == e->last_percent)
129 return;
130
131 if (!ratelimit_below(&e->progress_ratelimit))
132 return;
133
134 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
135 log_info("Exported %u%%.", percent);
136
137 e->last_percent = percent;
138 }
139
140 static int tar_export_finish(TarExport *e) {
141 int r;
142
143 assert(e);
144 assert(e->tar_fd >= 0);
145
146 if (e->tar_pid > 0) {
147 r = wait_for_terminate_and_check("tar", TAKE_PID(e->tar_pid), WAIT_LOG);
148 if (r < 0)
149 return r;
150 if (r != EXIT_SUCCESS)
151 return -EPROTO;
152 }
153
154 e->tar_fd = safe_close(e->tar_fd);
155
156 return 0;
157 }
158
159 static int tar_export_process(TarExport *e) {
160 ssize_t l;
161 int r;
162
163 assert(e);
164
165 if (!e->tried_splice && e->compress.type == IMPORT_COMPRESS_UNCOMPRESSED) {
166
167 l = splice(e->tar_fd, NULL, e->output_fd, NULL, COPY_BUFFER_SIZE, 0);
168 if (l < 0) {
169 if (errno == EAGAIN)
170 return 0;
171
172 e->tried_splice = true;
173 } else if (l == 0) {
174 r = tar_export_finish(e);
175 goto finish;
176 } else {
177 e->written_uncompressed += l;
178 e->written_compressed += l;
179
180 tar_export_report_progress(e);
181
182 return 0;
183 }
184 }
185
186 while (e->buffer_size <= 0) {
187 uint8_t input[COPY_BUFFER_SIZE];
188
189 if (e->eof) {
190 r = tar_export_finish(e);
191 goto finish;
192 }
193
194 l = read(e->tar_fd, input, sizeof(input));
195 if (l < 0) {
196 r = log_error_errno(errno, "Failed to read tar file: %m");
197 goto finish;
198 }
199
200 if (l == 0) {
201 e->eof = true;
202 r = import_compress_finish(&e->compress, &e->buffer, &e->buffer_size, &e->buffer_allocated);
203 } else {
204 e->written_uncompressed += l;
205 r = import_compress(&e->compress, input, l, &e->buffer, &e->buffer_size, &e->buffer_allocated);
206 }
207 if (r < 0) {
208 r = log_error_errno(r, "Failed to encode: %m");
209 goto finish;
210 }
211 }
212
213 l = write(e->output_fd, e->buffer, e->buffer_size);
214 if (l < 0) {
215 if (errno == EAGAIN)
216 return 0;
217
218 r = log_error_errno(errno, "Failed to write output file: %m");
219 goto finish;
220 }
221
222 assert((size_t) l <= e->buffer_size);
223 memmove(e->buffer, (uint8_t*) e->buffer + l, e->buffer_size - l);
224 e->buffer_size -= l;
225 e->written_compressed += l;
226
227 tar_export_report_progress(e);
228
229 return 0;
230
231 finish:
232 if (e->on_finished)
233 e->on_finished(e, r, e->userdata);
234 else
235 sd_event_exit(e->event, r);
236
237 return 0;
238 }
239
240 static int tar_export_on_output(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
241 TarExport *i = userdata;
242
243 return tar_export_process(i);
244 }
245
246 static int tar_export_on_defer(sd_event_source *s, void *userdata) {
247 TarExport *i = userdata;
248
249 return tar_export_process(i);
250 }
251
252 int tar_export_start(TarExport *e, const char *path, int fd, ImportCompressType compress) {
253 _cleanup_close_ int sfd = -EBADF;
254 int r;
255
256 assert(e);
257 assert(path);
258 assert(fd >= 0);
259 assert(compress < _IMPORT_COMPRESS_TYPE_MAX);
260 assert(compress != IMPORT_COMPRESS_UNKNOWN);
261
262 if (e->output_fd >= 0)
263 return -EBUSY;
264
265 sfd = open(path, O_DIRECTORY|O_RDONLY|O_NOCTTY|O_CLOEXEC);
266 if (sfd < 0)
267 return -errno;
268
269 if (fstat(sfd, &e->st) < 0)
270 return -errno;
271
272 r = fd_nonblock(fd, true);
273 if (r < 0)
274 return r;
275
276 r = free_and_strdup(&e->path, path);
277 if (r < 0)
278 return r;
279
280 e->quota_referenced = UINT64_MAX;
281
282 if (btrfs_might_be_subvol(&e->st)) {
283 BtrfsQuotaInfo q;
284
285 r = btrfs_subvol_get_subtree_quota_fd(sfd, 0, &q);
286 if (r >= 0)
287 e->quota_referenced = q.referenced;
288
289 e->temp_path = mfree(e->temp_path);
290
291 r = tempfn_random(path, NULL, &e->temp_path);
292 if (r < 0)
293 return r;
294
295 /* Let's try to make a snapshot, if we can, so that the export is atomic */
296 r = btrfs_subvol_snapshot_fd(sfd, e->temp_path, BTRFS_SNAPSHOT_READ_ONLY|BTRFS_SNAPSHOT_RECURSIVE);
297 if (r < 0) {
298 log_debug_errno(r, "Couldn't create snapshot %s of %s, not exporting atomically: %m", e->temp_path, path);
299 e->temp_path = mfree(e->temp_path);
300 }
301 }
302
303 r = import_compress_init(&e->compress, compress);
304 if (r < 0)
305 return r;
306
307 r = sd_event_add_io(e->event, &e->output_event_source, fd, EPOLLOUT, tar_export_on_output, e);
308 if (r == -EPERM) {
309 r = sd_event_add_defer(e->event, &e->output_event_source, tar_export_on_defer, e);
310 if (r < 0)
311 return r;
312
313 r = sd_event_source_set_enabled(e->output_event_source, SD_EVENT_ON);
314 }
315 if (r < 0)
316 return r;
317
318 e->tar_fd = import_fork_tar_c(e->temp_path ?: e->path, &e->tar_pid);
319 if (e->tar_fd < 0) {
320 e->output_event_source = sd_event_source_unref(e->output_event_source);
321 return e->tar_fd;
322 }
323
324 e->output_fd = fd;
325 return r;
326 }