1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2015 Lennart Poettering
8 #include <sys/sendfile.h>
10 /* When we include libgen.h because we need dirname() we immediately
11 * undefine basename() since libgen.h defines it as a macro to the POSIX
12 * version which is really broken. We prefer GNU basename(). */
16 #include "sd-daemon.h"
18 #include "alloc-util.h"
19 #include "btrfs-util.h"
21 #include "export-raw.h"
24 #include "import-common.h"
26 #include "ratelimit.h"
27 #include "stat-util.h"
28 #include "string-util.h"
31 #define COPY_BUFFER_SIZE (16*1024)
36 RawExportFinished on_finished
;
44 ImportCompress compress
;
46 sd_event_source
*output_event_source
;
50 size_t buffer_allocated
;
52 uint64_t written_compressed
;
53 uint64_t written_uncompressed
;
55 unsigned last_percent
;
56 RateLimit progress_rate_limit
;
65 RawExport
*raw_export_unref(RawExport
*e
) {
69 sd_event_source_unref(e
->output_event_source
);
71 import_compress_free(&e
->compress
);
73 sd_event_unref(e
->event
);
75 safe_close(e
->input_fd
);
85 RawExportFinished on_finished
,
88 _cleanup_(raw_export_unrefp
) RawExport
*e
= NULL
;
93 e
= new0(RawExport
, 1);
97 e
->output_fd
= e
->input_fd
= -1;
98 e
->on_finished
= on_finished
;
99 e
->userdata
= userdata
;
101 RATELIMIT_INIT(e
->progress_rate_limit
, 100 * USEC_PER_MSEC
, 1);
102 e
->last_percent
= (unsigned) -1;
105 e
->event
= sd_event_ref(event
);
107 r
= sd_event_default(&e
->event
);
117 static void raw_export_report_progress(RawExport
*e
) {
121 if (e
->written_uncompressed
>= (uint64_t) e
->st
.st_size
)
124 percent
= (unsigned) ((e
->written_uncompressed
* UINT64_C(100)) / (uint64_t) e
->st
.st_size
);
126 if (percent
== e
->last_percent
)
129 if (!ratelimit_test(&e
->progress_rate_limit
))
132 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent
);
133 log_info("Exported %u%%.", percent
);
135 e
->last_percent
= percent
;
138 static int raw_export_process(RawExport
*e
) {
144 if (!e
->tried_reflink
&& e
->compress
.type
== IMPORT_COMPRESS_UNCOMPRESSED
) {
146 /* If we shall take an uncompressed snapshot we can
147 * reflink source to destination directly. Let's see
150 r
= btrfs_reflink(e
->input_fd
, e
->output_fd
);
156 e
->tried_reflink
= true;
159 if (!e
->tried_sendfile
&& e
->compress
.type
== IMPORT_COMPRESS_UNCOMPRESSED
) {
161 l
= sendfile(e
->output_fd
, e
->input_fd
, NULL
, COPY_BUFFER_SIZE
);
166 e
->tried_sendfile
= true;
171 e
->written_uncompressed
+= l
;
172 e
->written_compressed
+= l
;
174 raw_export_report_progress(e
);
180 while (e
->buffer_size
<= 0) {
181 uint8_t input
[COPY_BUFFER_SIZE
];
188 l
= read(e
->input_fd
, input
, sizeof(input
));
190 r
= log_error_errno(errno
, "Failed to read raw file: %m");
196 r
= import_compress_finish(&e
->compress
, &e
->buffer
, &e
->buffer_size
, &e
->buffer_allocated
);
198 e
->written_uncompressed
+= l
;
199 r
= import_compress(&e
->compress
, input
, l
, &e
->buffer
, &e
->buffer_size
, &e
->buffer_allocated
);
202 r
= log_error_errno(r
, "Failed to encode: %m");
207 l
= write(e
->output_fd
, e
->buffer
, e
->buffer_size
);
212 r
= log_error_errno(errno
, "Failed to write output file: %m");
216 assert((size_t) l
<= e
->buffer_size
);
217 memmove(e
->buffer
, (uint8_t*) e
->buffer
+ l
, e
->buffer_size
- l
);
219 e
->written_compressed
+= l
;
221 raw_export_report_progress(e
);
227 (void) copy_times(e
->input_fd
, e
->output_fd
);
228 (void) copy_xattr(e
->input_fd
, e
->output_fd
);
232 e
->on_finished(e
, r
, e
->userdata
);
234 sd_event_exit(e
->event
, r
);
239 static int raw_export_on_output(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
240 RawExport
*i
= userdata
;
242 return raw_export_process(i
);
245 static int raw_export_on_defer(sd_event_source
*s
, void *userdata
) {
246 RawExport
*i
= userdata
;
248 return raw_export_process(i
);
251 static int reflink_snapshot(int fd
, const char *path
) {
258 new_fd
= open(d
, O_TMPFILE
|O_CLOEXEC
|O_NOCTTY
|O_RDWR
, 0600);
260 _cleanup_free_
char *t
= NULL
;
262 r
= tempfn_random(path
, NULL
, &t
);
266 new_fd
= open(t
, O_CLOEXEC
|O_CREAT
|O_NOCTTY
|O_RDWR
, 0600);
273 r
= btrfs_reflink(fd
, new_fd
);
282 int raw_export_start(RawExport
*e
, const char *path
, int fd
, ImportCompressType compress
) {
283 _cleanup_close_
int sfd
= -1, tfd
= -1;
289 assert(compress
< _IMPORT_COMPRESS_TYPE_MAX
);
290 assert(compress
!= IMPORT_COMPRESS_UNKNOWN
);
292 if (e
->output_fd
>= 0)
295 r
= fd_nonblock(fd
, true);
299 r
= free_and_strdup(&e
->path
, path
);
303 sfd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
307 if (fstat(sfd
, &e
->st
) < 0)
309 r
= stat_verify_regular(&e
->st
);
313 /* Try to take a reflink snapshot of the file, if we can t make the export atomic */
314 tfd
= reflink_snapshot(sfd
, path
);
316 e
->input_fd
= TAKE_FD(tfd
);
318 e
->input_fd
= TAKE_FD(sfd
);
320 r
= import_compress_init(&e
->compress
, compress
);
324 r
= sd_event_add_io(e
->event
, &e
->output_event_source
, fd
, EPOLLOUT
, raw_export_on_output
, e
);
326 r
= sd_event_add_defer(e
->event
, &e
->output_event_source
, raw_export_on_defer
, e
);
330 r
= sd_event_source_set_enabled(e
->output_event_source
, SD_EVENT_ON
);