1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2015 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/sendfile.h>
24 #include "sd-daemon.h"
26 #include "ratelimit.h"
27 #include "btrfs-util.h"
28 #include "import-common.h"
29 #include "export-tar.h"
30 #include "process-util.h"
32 #define COPY_BUFFER_SIZE (16*1024)
37 TarExportFinished on_finished
;
46 ImportCompress compress
;
48 sd_event_source
*output_event_source
;
52 size_t buffer_allocated
;
54 uint64_t written_compressed
;
55 uint64_t written_uncompressed
;
60 uint64_t quota_referenced
;
62 unsigned last_percent
;
63 RateLimit progress_rate_limit
;
69 TarExport
*tar_export_unref(TarExport
*e
) {
73 sd_event_source_unref(e
->output_event_source
);
76 (void) kill_and_sigcont(e
->tar_pid
, SIGKILL
);
77 (void) wait_for_terminate(e
->tar_pid
, NULL
);
81 (void) btrfs_subvol_remove(e
->temp_path
, false);
85 import_compress_free(&e
->compress
);
87 sd_event_unref(e
->event
);
89 safe_close(e
->tar_fd
);
101 TarExportFinished on_finished
,
104 _cleanup_(tar_export_unrefp
) TarExport
*e
= NULL
;
109 e
= new0(TarExport
, 1);
113 e
->output_fd
= e
->tar_fd
= -1;
114 e
->on_finished
= on_finished
;
115 e
->userdata
= userdata
;
116 e
->quota_referenced
= (uint64_t) -1;
118 RATELIMIT_INIT(e
->progress_rate_limit
, 100 * USEC_PER_MSEC
, 1);
119 e
->last_percent
= (unsigned) -1;
122 e
->event
= sd_event_ref(event
);
124 r
= sd_event_default(&e
->event
);
135 static void tar_export_report_progress(TarExport
*e
) {
139 /* Do we have any quota info? If not, we don't know anything about the progress */
140 if (e
->quota_referenced
== (uint64_t) -1)
143 if (e
->written_uncompressed
>= e
->quota_referenced
)
146 percent
= (unsigned) ((e
->written_uncompressed
* UINT64_C(100)) / e
->quota_referenced
);
148 if (percent
== e
->last_percent
)
151 if (!ratelimit_test(&e
->progress_rate_limit
))
154 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent
);
155 log_info("Exported %u%%.", percent
);
157 e
->last_percent
= percent
;
160 static int tar_export_process(TarExport
*e
) {
166 if (!e
->tried_splice
&& e
->compress
.type
== IMPORT_COMPRESS_UNCOMPRESSED
) {
168 l
= splice(e
->tar_fd
, NULL
, e
->output_fd
, NULL
, COPY_BUFFER_SIZE
, 0);
173 e
->tried_splice
= true;
178 e
->written_uncompressed
+= l
;
179 e
->written_compressed
+= l
;
181 tar_export_report_progress(e
);
187 while (e
->buffer_size
<= 0) {
188 uint8_t input
[COPY_BUFFER_SIZE
];
195 l
= read(e
->tar_fd
, input
, sizeof(input
));
197 r
= log_error_errno(errno
, "Failed to read tar file: %m");
203 r
= import_compress_finish(&e
->compress
, &e
->buffer
, &e
->buffer_size
, &e
->buffer_allocated
);
205 e
->written_uncompressed
+= l
;
206 r
= import_compress(&e
->compress
, input
, l
, &e
->buffer
, &e
->buffer_size
, &e
->buffer_allocated
);
209 r
= log_error_errno(r
, "Failed to encode: %m");
214 l
= write(e
->output_fd
, e
->buffer
, e
->buffer_size
);
219 r
= log_error_errno(errno
, "Failed to write output file: %m");
223 assert((size_t) l
<= e
->buffer_size
);
224 memmove(e
->buffer
, (uint8_t*) e
->buffer
+ l
, e
->buffer_size
- l
);
226 e
->written_compressed
+= l
;
228 tar_export_report_progress(e
);
234 e
->on_finished(e
, r
, e
->userdata
);
236 sd_event_exit(e
->event
, r
);
241 static int tar_export_on_output(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
242 TarExport
*i
= userdata
;
244 return tar_export_process(i
);
247 static int tar_export_on_defer(sd_event_source
*s
, void *userdata
) {
248 TarExport
*i
= userdata
;
250 return tar_export_process(i
);
253 int tar_export_start(TarExport
*e
, const char *path
, int fd
, ImportCompressType compress
) {
254 _cleanup_close_
int sfd
= -1;
260 assert(compress
< _IMPORT_COMPRESS_TYPE_MAX
);
261 assert(compress
!= IMPORT_COMPRESS_UNKNOWN
);
263 if (e
->output_fd
>= 0)
266 sfd
= open(path
, O_DIRECTORY
|O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
270 if (fstat(sfd
, &e
->st
) < 0)
273 r
= fd_nonblock(fd
, true);
277 r
= free_and_strdup(&e
->path
, path
);
281 e
->quota_referenced
= (uint64_t) -1;
283 if (e
->st
.st_ino
== 256) { /* might be a btrfs subvolume? */
286 r
= btrfs_subvol_get_quota_fd(sfd
, &q
);
288 e
->quota_referenced
= q
.referenced
;
293 r
= tempfn_random(path
, &e
->temp_path
);
297 /* Let's try to make a snapshot, if we can, so that the export is atomic */
298 r
= btrfs_subvol_snapshot_fd(sfd
, e
->temp_path
, BTRFS_SNAPSHOT_READ_ONLY
|BTRFS_SNAPSHOT_RECURSIVE
);
300 log_debug_errno(r
, "Couldn't create snapshot %s of %s, not exporting atomically: %m", e
->temp_path
, path
);
306 r
= import_compress_init(&e
->compress
, compress
);
310 r
= sd_event_add_io(e
->event
, &e
->output_event_source
, fd
, EPOLLOUT
, tar_export_on_output
, e
);
312 r
= sd_event_add_defer(e
->event
, &e
->output_event_source
, tar_export_on_defer
, e
);
316 r
= sd_event_source_set_enabled(e
->output_event_source
, SD_EVENT_ON
);
321 e
->tar_fd
= import_fork_tar_c(e
->temp_path
?: e
->path
, &e
->tar_pid
);
323 e
->output_event_source
= sd_event_source_unref(e
->output_event_source
);