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