1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "btrfs-util.h"
7 #include "export-tar.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"
16 #define COPY_BUFFER_SIZE (16*1024)
21 TarExportFinished on_finished
;
30 ImportCompress compress
;
32 sd_event_source
*output_event_source
;
36 size_t buffer_allocated
;
38 uint64_t written_compressed
;
39 uint64_t written_uncompressed
;
44 uint64_t quota_referenced
;
46 unsigned last_percent
;
47 RateLimit progress_rate_limit
;
53 TarExport
*tar_export_unref(TarExport
*e
) {
57 sd_event_source_unref(e
->output_event_source
);
60 (void) kill_and_sigcont(e
->tar_pid
, SIGKILL
);
61 (void) wait_for_terminate(e
->tar_pid
, NULL
);
65 (void) btrfs_subvol_remove(e
->temp_path
, BTRFS_REMOVE_QUOTA
);
69 import_compress_free(&e
->compress
);
71 sd_event_unref(e
->event
);
73 safe_close(e
->tar_fd
);
83 TarExportFinished on_finished
,
86 _cleanup_(tar_export_unrefp
) TarExport
*e
= NULL
;
91 e
= new(TarExport
, 1);
98 .on_finished
= on_finished
,
100 .quota_referenced
= (uint64_t) -1,
101 .last_percent
= (unsigned) -1,
104 RATELIMIT_INIT(e
->progress_rate_limit
, 100 * USEC_PER_MSEC
, 1);
107 e
->event
= sd_event_ref(event
);
109 r
= sd_event_default(&e
->event
);
119 static void tar_export_report_progress(TarExport
*e
) {
123 /* Do we have any quota info? If not, we don't know anything about the progress */
124 if (e
->quota_referenced
== (uint64_t) -1)
127 if (e
->written_uncompressed
>= e
->quota_referenced
)
130 percent
= (unsigned) ((e
->written_uncompressed
* UINT64_C(100)) / e
->quota_referenced
);
132 if (percent
== e
->last_percent
)
135 if (!ratelimit_below(&e
->progress_rate_limit
))
138 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent
);
139 log_info("Exported %u%%.", percent
);
141 e
->last_percent
= percent
;
144 static int tar_export_finish(TarExport
*e
) {
148 assert(e
->tar_fd
>= 0);
150 if (e
->tar_pid
> 0) {
151 r
= wait_for_terminate_and_check("tar", e
->tar_pid
, WAIT_LOG
);
155 if (r
!= EXIT_SUCCESS
)
159 e
->tar_fd
= safe_close(e
->tar_fd
);
164 static int tar_export_process(TarExport
*e
) {
170 if (!e
->tried_splice
&& e
->compress
.type
== IMPORT_COMPRESS_UNCOMPRESSED
) {
172 l
= splice(e
->tar_fd
, NULL
, e
->output_fd
, NULL
, COPY_BUFFER_SIZE
, 0);
177 e
->tried_splice
= true;
179 r
= tar_export_finish(e
);
182 e
->written_uncompressed
+= l
;
183 e
->written_compressed
+= l
;
185 tar_export_report_progress(e
);
191 while (e
->buffer_size
<= 0) {
192 uint8_t input
[COPY_BUFFER_SIZE
];
195 r
= tar_export_finish(e
);
199 l
= read(e
->tar_fd
, input
, sizeof(input
));
201 r
= log_error_errno(errno
, "Failed to read tar file: %m");
207 r
= import_compress_finish(&e
->compress
, &e
->buffer
, &e
->buffer_size
, &e
->buffer_allocated
);
209 e
->written_uncompressed
+= l
;
210 r
= import_compress(&e
->compress
, input
, l
, &e
->buffer
, &e
->buffer_size
, &e
->buffer_allocated
);
213 r
= log_error_errno(r
, "Failed to encode: %m");
218 l
= write(e
->output_fd
, e
->buffer
, e
->buffer_size
);
223 r
= log_error_errno(errno
, "Failed to write output file: %m");
227 assert((size_t) l
<= e
->buffer_size
);
228 memmove(e
->buffer
, (uint8_t*) e
->buffer
+ l
, e
->buffer_size
- l
);
230 e
->written_compressed
+= l
;
232 tar_export_report_progress(e
);
238 e
->on_finished(e
, r
, e
->userdata
);
240 sd_event_exit(e
->event
, r
);
245 static int tar_export_on_output(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
246 TarExport
*i
= userdata
;
248 return tar_export_process(i
);
251 static int tar_export_on_defer(sd_event_source
*s
, void *userdata
) {
252 TarExport
*i
= userdata
;
254 return tar_export_process(i
);
257 int tar_export_start(TarExport
*e
, const char *path
, int fd
, ImportCompressType compress
) {
258 _cleanup_close_
int sfd
= -1;
264 assert(compress
< _IMPORT_COMPRESS_TYPE_MAX
);
265 assert(compress
!= IMPORT_COMPRESS_UNKNOWN
);
267 if (e
->output_fd
>= 0)
270 sfd
= open(path
, O_DIRECTORY
|O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
274 if (fstat(sfd
, &e
->st
) < 0)
277 r
= fd_nonblock(fd
, true);
281 r
= free_and_strdup(&e
->path
, path
);
285 e
->quota_referenced
= (uint64_t) -1;
287 if (e
->st
.st_ino
== 256) { /* might be a btrfs subvolume? */
290 r
= btrfs_subvol_get_subtree_quota_fd(sfd
, 0, &q
);
292 e
->quota_referenced
= q
.referenced
;
294 e
->temp_path
= mfree(e
->temp_path
);
296 r
= tempfn_random(path
, NULL
, &e
->temp_path
);
300 /* Let's try to make a snapshot, if we can, so that the export is atomic */
301 r
= btrfs_subvol_snapshot_fd(sfd
, e
->temp_path
, BTRFS_SNAPSHOT_READ_ONLY
|BTRFS_SNAPSHOT_RECURSIVE
);
303 log_debug_errno(r
, "Couldn't create snapshot %s of %s, not exporting atomically: %m", e
->temp_path
, path
);
304 e
->temp_path
= mfree(e
->temp_path
);
308 r
= import_compress_init(&e
->compress
, compress
);
312 r
= sd_event_add_io(e
->event
, &e
->output_event_source
, fd
, EPOLLOUT
, tar_export_on_output
, e
);
314 r
= sd_event_add_defer(e
->event
, &e
->output_event_source
, tar_export_on_defer
, e
);
318 r
= sd_event_source_set_enabled(e
->output_event_source
, SD_EVENT_ON
);
323 e
->tar_fd
= import_fork_tar_c(e
->temp_path
?: e
->path
, &e
->tar_pid
);
325 e
->output_event_source
= sd_event_source_unref(e
->output_event_source
);