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