1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <sys/sendfile.h>
5 /* When we include libgen.h because we need dirname() we immediately
6 * undefine basename() since libgen.h defines it as a macro to the POSIX
7 * version which is really broken. We prefer GNU basename(). */
11 #include "sd-daemon.h"
13 #include "alloc-util.h"
14 #include "btrfs-util.h"
16 #include "export-raw.h"
19 #include "import-common.h"
21 #include "ratelimit.h"
22 #include "stat-util.h"
23 #include "string-util.h"
24 #include "tmpfile-util.h"
27 #define COPY_BUFFER_SIZE (16*1024)
32 RawExportFinished on_finished
;
40 ImportCompress compress
;
42 sd_event_source
*output_event_source
;
46 size_t buffer_allocated
;
48 uint64_t written_compressed
;
49 uint64_t written_uncompressed
;
51 unsigned last_percent
;
52 RateLimit progress_rate_limit
;
61 RawExport
*raw_export_unref(RawExport
*e
) {
65 sd_event_source_unref(e
->output_event_source
);
67 import_compress_free(&e
->compress
);
69 sd_event_unref(e
->event
);
71 safe_close(e
->input_fd
);
81 RawExportFinished on_finished
,
84 _cleanup_(raw_export_unrefp
) RawExport
*e
= NULL
;
89 e
= new(RawExport
, 1);
96 .on_finished
= on_finished
,
98 .last_percent
= (unsigned) -1,
101 RATELIMIT_INIT(e
->progress_rate_limit
, 100 * USEC_PER_MSEC
, 1);
104 e
->event
= sd_event_ref(event
);
106 r
= sd_event_default(&e
->event
);
116 static void raw_export_report_progress(RawExport
*e
) {
120 if (e
->written_uncompressed
>= (uint64_t) e
->st
.st_size
)
123 percent
= (unsigned) ((e
->written_uncompressed
* UINT64_C(100)) / (uint64_t) e
->st
.st_size
);
125 if (percent
== e
->last_percent
)
128 if (!ratelimit_below(&e
->progress_rate_limit
))
131 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent
);
132 log_info("Exported %u%%.", percent
);
134 e
->last_percent
= percent
;
137 static int raw_export_process(RawExport
*e
) {
143 if (!e
->tried_reflink
&& e
->compress
.type
== IMPORT_COMPRESS_UNCOMPRESSED
) {
145 /* If we shall take an uncompressed snapshot we can
146 * reflink source to destination directly. Let's see
149 r
= btrfs_reflink(e
->input_fd
, e
->output_fd
);
155 e
->tried_reflink
= true;
158 if (!e
->tried_sendfile
&& e
->compress
.type
== IMPORT_COMPRESS_UNCOMPRESSED
) {
160 l
= sendfile(e
->output_fd
, e
->input_fd
, NULL
, COPY_BUFFER_SIZE
);
165 e
->tried_sendfile
= true;
170 e
->written_uncompressed
+= l
;
171 e
->written_compressed
+= l
;
173 raw_export_report_progress(e
);
179 while (e
->buffer_size
<= 0) {
180 uint8_t input
[COPY_BUFFER_SIZE
];
187 l
= read(e
->input_fd
, input
, sizeof(input
));
189 r
= log_error_errno(errno
, "Failed to read raw file: %m");
195 r
= import_compress_finish(&e
->compress
, &e
->buffer
, &e
->buffer_size
, &e
->buffer_allocated
);
197 e
->written_uncompressed
+= l
;
198 r
= import_compress(&e
->compress
, input
, l
, &e
->buffer
, &e
->buffer_size
, &e
->buffer_allocated
);
201 r
= log_error_errno(r
, "Failed to encode: %m");
206 l
= write(e
->output_fd
, e
->buffer
, e
->buffer_size
);
211 r
= log_error_errno(errno
, "Failed to write output file: %m");
215 assert((size_t) l
<= e
->buffer_size
);
216 memmove(e
->buffer
, (uint8_t*) e
->buffer
+ l
, e
->buffer_size
- l
);
218 e
->written_compressed
+= l
;
220 raw_export_report_progress(e
);
226 (void) copy_times(e
->input_fd
, e
->output_fd
);
227 (void) copy_xattr(e
->input_fd
, e
->output_fd
);
231 e
->on_finished(e
, r
, e
->userdata
);
233 sd_event_exit(e
->event
, r
);
238 static int raw_export_on_output(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
239 RawExport
*i
= userdata
;
241 return raw_export_process(i
);
244 static int raw_export_on_defer(sd_event_source
*s
, void *userdata
) {
245 RawExport
*i
= userdata
;
247 return raw_export_process(i
);
250 static int reflink_snapshot(int fd
, const char *path
) {
253 new_fd
= open_parent(path
, O_TMPFILE
|O_CLOEXEC
|O_RDWR
, 0600);
255 _cleanup_free_
char *t
= NULL
;
257 r
= tempfn_random(path
, NULL
, &t
);
261 new_fd
= open(t
, O_CLOEXEC
|O_CREAT
|O_NOCTTY
|O_RDWR
, 0600);
268 r
= btrfs_reflink(fd
, new_fd
);
277 int raw_export_start(RawExport
*e
, const char *path
, int fd
, ImportCompressType compress
) {
278 _cleanup_close_
int sfd
= -1, tfd
= -1;
284 assert(compress
< _IMPORT_COMPRESS_TYPE_MAX
);
285 assert(compress
!= IMPORT_COMPRESS_UNKNOWN
);
287 if (e
->output_fd
>= 0)
290 r
= fd_nonblock(fd
, true);
294 r
= free_and_strdup(&e
->path
, path
);
298 sfd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
302 if (fstat(sfd
, &e
->st
) < 0)
304 r
= stat_verify_regular(&e
->st
);
308 /* Try to take a reflink snapshot of the file, if we can t make the export atomic */
309 tfd
= reflink_snapshot(sfd
, path
);
311 e
->input_fd
= TAKE_FD(tfd
);
313 e
->input_fd
= TAKE_FD(sfd
);
315 r
= import_compress_init(&e
->compress
, compress
);
319 r
= sd_event_add_io(e
->event
, &e
->output_event_source
, fd
, EPOLLOUT
, raw_export_on_output
, e
);
321 r
= sd_event_add_defer(e
->event
, &e
->output_event_source
, raw_export_on_defer
, e
);
325 r
= sd_event_source_set_enabled(e
->output_event_source
, SD_EVENT_ON
);