1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "btrfs-util.h"
7 #include "export-tar.h"
10 #include "import-common.h"
11 #include "process-util.h"
12 #include "ratelimit.h"
13 #include "string-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
= new0(TarExport
, 1);
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;
100 RATELIMIT_INIT(e
->progress_rate_limit
, 100 * USEC_PER_MSEC
, 1);
101 e
->last_percent
= (unsigned) -1;
104 e
->event
= sd_event_ref(event
);
106 r
= sd_event_default(&e
->event
);
116 static void tar_export_report_progress(TarExport
*e
) {
120 /* Do we have any quota info? If not, we don't know anything about the progress */
121 if (e
->quota_referenced
== (uint64_t) -1)
124 if (e
->written_uncompressed
>= e
->quota_referenced
)
127 percent
= (unsigned) ((e
->written_uncompressed
* UINT64_C(100)) / e
->quota_referenced
);
129 if (percent
== e
->last_percent
)
132 if (!ratelimit_below(&e
->progress_rate_limit
))
135 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent
);
136 log_info("Exported %u%%.", percent
);
138 e
->last_percent
= percent
;
141 static int tar_export_process(TarExport
*e
) {
147 if (!e
->tried_splice
&& e
->compress
.type
== IMPORT_COMPRESS_UNCOMPRESSED
) {
149 l
= splice(e
->tar_fd
, NULL
, e
->output_fd
, NULL
, COPY_BUFFER_SIZE
, 0);
154 e
->tried_splice
= true;
159 e
->written_uncompressed
+= l
;
160 e
->written_compressed
+= l
;
162 tar_export_report_progress(e
);
168 while (e
->buffer_size
<= 0) {
169 uint8_t input
[COPY_BUFFER_SIZE
];
176 l
= read(e
->tar_fd
, input
, sizeof(input
));
178 r
= log_error_errno(errno
, "Failed to read tar file: %m");
184 r
= import_compress_finish(&e
->compress
, &e
->buffer
, &e
->buffer_size
, &e
->buffer_allocated
);
186 e
->written_uncompressed
+= l
;
187 r
= import_compress(&e
->compress
, input
, l
, &e
->buffer
, &e
->buffer_size
, &e
->buffer_allocated
);
190 r
= log_error_errno(r
, "Failed to encode: %m");
195 l
= write(e
->output_fd
, e
->buffer
, e
->buffer_size
);
200 r
= log_error_errno(errno
, "Failed to write output file: %m");
204 assert((size_t) l
<= e
->buffer_size
);
205 memmove(e
->buffer
, (uint8_t*) e
->buffer
+ l
, e
->buffer_size
- l
);
207 e
->written_compressed
+= l
;
209 tar_export_report_progress(e
);
215 e
->on_finished(e
, r
, e
->userdata
);
217 sd_event_exit(e
->event
, r
);
222 static int tar_export_on_output(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
223 TarExport
*i
= userdata
;
225 return tar_export_process(i
);
228 static int tar_export_on_defer(sd_event_source
*s
, void *userdata
) {
229 TarExport
*i
= userdata
;
231 return tar_export_process(i
);
234 int tar_export_start(TarExport
*e
, const char *path
, int fd
, ImportCompressType compress
) {
235 _cleanup_close_
int sfd
= -1;
241 assert(compress
< _IMPORT_COMPRESS_TYPE_MAX
);
242 assert(compress
!= IMPORT_COMPRESS_UNKNOWN
);
244 if (e
->output_fd
>= 0)
247 sfd
= open(path
, O_DIRECTORY
|O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
251 if (fstat(sfd
, &e
->st
) < 0)
254 r
= fd_nonblock(fd
, true);
258 r
= free_and_strdup(&e
->path
, path
);
262 e
->quota_referenced
= (uint64_t) -1;
264 if (e
->st
.st_ino
== 256) { /* might be a btrfs subvolume? */
267 r
= btrfs_subvol_get_subtree_quota_fd(sfd
, 0, &q
);
269 e
->quota_referenced
= q
.referenced
;
271 e
->temp_path
= mfree(e
->temp_path
);
273 r
= tempfn_random(path
, NULL
, &e
->temp_path
);
277 /* Let's try to make a snapshot, if we can, so that the export is atomic */
278 r
= btrfs_subvol_snapshot_fd(sfd
, e
->temp_path
, BTRFS_SNAPSHOT_READ_ONLY
|BTRFS_SNAPSHOT_RECURSIVE
);
280 log_debug_errno(r
, "Couldn't create snapshot %s of %s, not exporting atomically: %m", e
->temp_path
, path
);
281 e
->temp_path
= mfree(e
->temp_path
);
285 r
= import_compress_init(&e
->compress
, compress
);
289 r
= sd_event_add_io(e
->event
, &e
->output_event_source
, fd
, EPOLLOUT
, tar_export_on_output
, e
);
291 r
= sd_event_add_defer(e
->event
, &e
->output_event_source
, tar_export_on_defer
, e
);
295 r
= sd_event_source_set_enabled(e
->output_event_source
, SD_EVENT_ON
);
300 e
->tar_fd
= import_fork_tar_c(e
->temp_path
?: e
->path
, &e
->tar_pid
);
302 e
->output_event_source
= sd_event_source_unref(e
->output_event_source
);