1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <sys/sendfile.h>
4 
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(). */
8 #include <libgen.h>
9 #undef basename
10 
11 #include "sd-daemon.h"
12 
13 #include "alloc-util.h"
14 #include "btrfs-util.h"
15 #include "copy.h"
16 #include "export-raw.h"
17 #include "fd-util.h"
18 #include "fs-util.h"
19 #include "import-common.h"
20 #include "missing_fcntl.h"
21 #include "ratelimit.h"
22 #include "stat-util.h"
23 #include "string-util.h"
24 #include "tmpfile-util.h"
25 #include "util.h"
26 
27 #define COPY_BUFFER_SIZE (16*1024)
28 
29 struct RawExport {
30         sd_event *event;
31 
32         RawExportFinished on_finished;
33         void *userdata;
34 
35         char *path;
36 
37         int input_fd;
38         int output_fd;
39 
40         ImportCompress compress;
41 
42         sd_event_source *output_event_source;
43 
44         void *buffer;
45         size_t buffer_size;
46         size_t buffer_allocated;
47 
48         uint64_t written_compressed;
49         uint64_t written_uncompressed;
50 
51         unsigned last_percent;
52         RateLimit progress_ratelimit;
53 
54         struct stat st;
55 
56         bool eof;
57         bool tried_reflink;
58         bool tried_sendfile;
59 };
60 
raw_export_unref(RawExport * e)61 RawExport *raw_export_unref(RawExport *e) {
62         if (!e)
63                 return NULL;
64 
65         sd_event_source_unref(e->output_event_source);
66 
67         import_compress_free(&e->compress);
68 
69         sd_event_unref(e->event);
70 
71         safe_close(e->input_fd);
72 
73         free(e->buffer);
74         free(e->path);
75         return mfree(e);
76 }
77 
raw_export_new(RawExport ** ret,sd_event * event,RawExportFinished on_finished,void * userdata)78 int raw_export_new(
79                 RawExport **ret,
80                 sd_event *event,
81                 RawExportFinished on_finished,
82                 void *userdata) {
83 
84         _cleanup_(raw_export_unrefp) RawExport *e = NULL;
85         int r;
86 
87         assert(ret);
88 
89         e = new(RawExport, 1);
90         if (!e)
91                 return -ENOMEM;
92 
93         *e = (RawExport) {
94                 .output_fd = -1,
95                 .input_fd = -1,
96                 .on_finished = on_finished,
97                 .userdata = userdata,
98                 .last_percent = UINT_MAX,
99                 .progress_ratelimit = { 100 * USEC_PER_MSEC, 1 },
100         };
101 
102         if (event)
103                 e->event = sd_event_ref(event);
104         else {
105                 r = sd_event_default(&e->event);
106                 if (r < 0)
107                         return r;
108         }
109 
110         *ret = TAKE_PTR(e);
111 
112         return 0;
113 }
114 
raw_export_report_progress(RawExport * e)115 static void raw_export_report_progress(RawExport *e) {
116         unsigned percent;
117         assert(e);
118 
119         if (e->written_uncompressed >= (uint64_t) e->st.st_size)
120                 percent = 100;
121         else
122                 percent = (unsigned) ((e->written_uncompressed * UINT64_C(100)) / (uint64_t) e->st.st_size);
123 
124         if (percent == e->last_percent)
125                 return;
126 
127         if (!ratelimit_below(&e->progress_ratelimit))
128                 return;
129 
130         sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
131         log_info("Exported %u%%.", percent);
132 
133         e->last_percent = percent;
134 }
135 
raw_export_process(RawExport * e)136 static int raw_export_process(RawExport *e) {
137         ssize_t l;
138         int r;
139 
140         assert(e);
141 
142         if (!e->tried_reflink && e->compress.type == IMPORT_COMPRESS_UNCOMPRESSED) {
143 
144                 /* If we shall take an uncompressed snapshot we can
145                  * reflink source to destination directly. Let's see
146                  * if this works. */
147 
148                 r = btrfs_reflink(e->input_fd, e->output_fd);
149                 if (r >= 0) {
150                         r = 0;
151                         goto finish;
152                 }
153 
154                 e->tried_reflink = true;
155         }
156 
157         if (!e->tried_sendfile && e->compress.type == IMPORT_COMPRESS_UNCOMPRESSED) {
158 
159                 l = sendfile(e->output_fd, e->input_fd, NULL, COPY_BUFFER_SIZE);
160                 if (l < 0) {
161                         if (errno == EAGAIN)
162                                 return 0;
163 
164                         e->tried_sendfile = true;
165                 } else if (l == 0) {
166                         r = 0;
167                         goto finish;
168                 } else {
169                         e->written_uncompressed += l;
170                         e->written_compressed += l;
171 
172                         raw_export_report_progress(e);
173 
174                         return 0;
175                 }
176         }
177 
178         while (e->buffer_size <= 0) {
179                 uint8_t input[COPY_BUFFER_SIZE];
180 
181                 if (e->eof) {
182                         r = 0;
183                         goto finish;
184                 }
185 
186                 l = read(e->input_fd, input, sizeof(input));
187                 if (l < 0) {
188                         r = log_error_errno(errno, "Failed to read raw file: %m");
189                         goto finish;
190                 }
191 
192                 if (l == 0) {
193                         e->eof = true;
194                         r = import_compress_finish(&e->compress, &e->buffer, &e->buffer_size, &e->buffer_allocated);
195                 } else {
196                         e->written_uncompressed += l;
197                         r = import_compress(&e->compress, input, l, &e->buffer, &e->buffer_size, &e->buffer_allocated);
198                 }
199                 if (r < 0) {
200                         r = log_error_errno(r, "Failed to encode: %m");
201                         goto finish;
202                 }
203         }
204 
205         l = write(e->output_fd, e->buffer, e->buffer_size);
206         if (l < 0) {
207                 if (errno == EAGAIN)
208                         return 0;
209 
210                 r = log_error_errno(errno, "Failed to write output file: %m");
211                 goto finish;
212         }
213 
214         assert((size_t) l <= e->buffer_size);
215         memmove(e->buffer, (uint8_t*) e->buffer + l, e->buffer_size - l);
216         e->buffer_size -= l;
217         e->written_compressed += l;
218 
219         raw_export_report_progress(e);
220 
221         return 0;
222 
223 finish:
224         if (r >= 0) {
225                 (void) copy_times(e->input_fd, e->output_fd, COPY_CRTIME);
226                 (void) copy_xattr(e->input_fd, e->output_fd, 0);
227         }
228 
229         if (e->on_finished)
230                 e->on_finished(e, r, e->userdata);
231         else
232                 sd_event_exit(e->event, r);
233 
234         return 0;
235 }
236 
raw_export_on_output(sd_event_source * s,int fd,uint32_t revents,void * userdata)237 static int raw_export_on_output(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
238         RawExport *i = userdata;
239 
240         return raw_export_process(i);
241 }
242 
raw_export_on_defer(sd_event_source * s,void * userdata)243 static int raw_export_on_defer(sd_event_source *s, void *userdata) {
244         RawExport *i = userdata;
245 
246         return raw_export_process(i);
247 }
248 
reflink_snapshot(int fd,const char * path)249 static int reflink_snapshot(int fd, const char *path) {
250         int new_fd, r;
251 
252         new_fd = open_parent(path, O_TMPFILE|O_CLOEXEC|O_RDWR, 0600);
253         if (new_fd < 0) {
254                 _cleanup_free_ char *t = NULL;
255 
256                 r = tempfn_random(path, NULL, &t);
257                 if (r < 0)
258                         return r;
259 
260                 new_fd = open(t, O_CLOEXEC|O_CREAT|O_NOCTTY|O_RDWR, 0600);
261                 if (new_fd < 0)
262                         return -errno;
263 
264                 (void) unlink(t);
265         }
266 
267         r = btrfs_reflink(fd, new_fd);
268         if (r < 0) {
269                 safe_close(new_fd);
270                 return r;
271         }
272 
273         return new_fd;
274 }
275 
raw_export_start(RawExport * e,const char * path,int fd,ImportCompressType compress)276 int raw_export_start(RawExport *e, const char *path, int fd, ImportCompressType compress) {
277         _cleanup_close_ int sfd = -1, tfd = -1;
278         int r;
279 
280         assert(e);
281         assert(path);
282         assert(fd >= 0);
283         assert(compress < _IMPORT_COMPRESS_TYPE_MAX);
284         assert(compress != IMPORT_COMPRESS_UNKNOWN);
285 
286         if (e->output_fd >= 0)
287                 return -EBUSY;
288 
289         r = fd_nonblock(fd, true);
290         if (r < 0)
291                 return r;
292 
293         r = free_and_strdup(&e->path, path);
294         if (r < 0)
295                 return r;
296 
297         sfd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
298         if (sfd < 0)
299                 return -errno;
300 
301         if (fstat(sfd, &e->st) < 0)
302                 return -errno;
303         r = stat_verify_regular(&e->st);
304         if (r < 0)
305                 return r;
306 
307         /* Try to take a reflink snapshot of the file, if we can t make the export atomic */
308         tfd = reflink_snapshot(sfd, path);
309         if (tfd >= 0)
310                 e->input_fd = TAKE_FD(tfd);
311         else
312                 e->input_fd = TAKE_FD(sfd);
313 
314         r = import_compress_init(&e->compress, compress);
315         if (r < 0)
316                 return r;
317 
318         r = sd_event_add_io(e->event, &e->output_event_source, fd, EPOLLOUT, raw_export_on_output, e);
319         if (r == -EPERM) {
320                 r = sd_event_add_defer(e->event, &e->output_event_source, raw_export_on_defer, e);
321                 if (r < 0)
322                         return r;
323 
324                 r = sd_event_source_set_enabled(e->output_event_source, SD_EVENT_ON);
325         }
326         if (r < 0)
327                 return r;
328 
329         e->output_fd = fd;
330         return r;
331 }
332