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