LCOV - code coverage report
Current view: top level - import - import-raw.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 0 226 0.0 %
Date: 2019-08-22 15:41:25 Functions: 0 12 0.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <linux/fs.h>
       4             : 
       5             : #include "sd-daemon.h"
       6             : #include "sd-event.h"
       7             : 
       8             : #include "alloc-util.h"
       9             : #include "btrfs-util.h"
      10             : #include "chattr-util.h"
      11             : #include "copy.h"
      12             : #include "fd-util.h"
      13             : #include "fs-util.h"
      14             : #include "hostname-util.h"
      15             : #include "import-common.h"
      16             : #include "import-compress.h"
      17             : #include "import-raw.h"
      18             : #include "io-util.h"
      19             : #include "machine-pool.h"
      20             : #include "mkdir.h"
      21             : #include "path-util.h"
      22             : #include "qcow2-util.h"
      23             : #include "ratelimit.h"
      24             : #include "rm-rf.h"
      25             : #include "string-util.h"
      26             : #include "tmpfile-util.h"
      27             : #include "util.h"
      28             : 
      29             : struct RawImport {
      30             :         sd_event *event;
      31             : 
      32             :         char *image_root;
      33             : 
      34             :         RawImportFinished on_finished;
      35             :         void *userdata;
      36             : 
      37             :         char *local;
      38             :         bool force_local;
      39             :         bool read_only;
      40             : 
      41             :         char *temp_path;
      42             :         char *final_path;
      43             : 
      44             :         int input_fd;
      45             :         int output_fd;
      46             : 
      47             :         ImportCompress compress;
      48             : 
      49             :         sd_event_source *input_event_source;
      50             : 
      51             :         uint8_t buffer[16*1024];
      52             :         size_t buffer_size;
      53             : 
      54             :         uint64_t written_compressed;
      55             :         uint64_t written_uncompressed;
      56             : 
      57             :         struct stat st;
      58             : 
      59             :         unsigned last_percent;
      60             :         RateLimit progress_rate_limit;
      61             : };
      62             : 
      63           0 : RawImport* raw_import_unref(RawImport *i) {
      64           0 :         if (!i)
      65           0 :                 return NULL;
      66             : 
      67           0 :         sd_event_unref(i->event);
      68             : 
      69           0 :         if (i->temp_path) {
      70           0 :                 (void) unlink(i->temp_path);
      71           0 :                 free(i->temp_path);
      72             :         }
      73             : 
      74           0 :         import_compress_free(&i->compress);
      75             : 
      76           0 :         sd_event_source_unref(i->input_event_source);
      77             : 
      78           0 :         safe_close(i->output_fd);
      79             : 
      80           0 :         free(i->final_path);
      81           0 :         free(i->image_root);
      82           0 :         free(i->local);
      83           0 :         return mfree(i);
      84             : }
      85             : 
      86           0 : int raw_import_new(
      87             :                 RawImport **ret,
      88             :                 sd_event *event,
      89             :                 const char *image_root,
      90             :                 RawImportFinished on_finished,
      91             :                 void *userdata) {
      92             : 
      93           0 :         _cleanup_(raw_import_unrefp) RawImport *i = NULL;
      94           0 :         _cleanup_free_ char *root = NULL;
      95             :         int r;
      96             : 
      97           0 :         assert(ret);
      98             : 
      99           0 :         root = strdup(image_root ?: "/var/lib/machines");
     100           0 :         if (!root)
     101           0 :                 return -ENOMEM;
     102             : 
     103           0 :         i = new(RawImport, 1);
     104           0 :         if (!i)
     105           0 :                 return -ENOMEM;
     106             : 
     107           0 :         *i = (RawImport) {
     108             :                 .input_fd = -1,
     109             :                 .output_fd = -1,
     110             :                 .on_finished = on_finished,
     111             :                 .userdata = userdata,
     112             :                 .last_percent = (unsigned) -1,
     113           0 :                 .image_root = TAKE_PTR(root),
     114             :         };
     115             : 
     116           0 :         RATELIMIT_INIT(i->progress_rate_limit, 100 * USEC_PER_MSEC, 1);
     117             : 
     118           0 :         if (event)
     119           0 :                 i->event = sd_event_ref(event);
     120             :         else {
     121           0 :                 r = sd_event_default(&i->event);
     122           0 :                 if (r < 0)
     123           0 :                         return r;
     124             :         }
     125             : 
     126           0 :         *ret = TAKE_PTR(i);
     127             : 
     128           0 :         return 0;
     129             : }
     130             : 
     131           0 : static void raw_import_report_progress(RawImport *i) {
     132             :         unsigned percent;
     133           0 :         assert(i);
     134             : 
     135             :         /* We have no size information, unless the source is a regular file */
     136           0 :         if (!S_ISREG(i->st.st_mode))
     137           0 :                 return;
     138             : 
     139           0 :         if (i->written_compressed >= (uint64_t) i->st.st_size)
     140           0 :                 percent = 100;
     141             :         else
     142           0 :                 percent = (unsigned) ((i->written_compressed * UINT64_C(100)) / (uint64_t) i->st.st_size);
     143             : 
     144           0 :         if (percent == i->last_percent)
     145           0 :                 return;
     146             : 
     147           0 :         if (!ratelimit_below(&i->progress_rate_limit))
     148           0 :                 return;
     149             : 
     150           0 :         sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
     151           0 :         log_info("Imported %u%%.", percent);
     152             : 
     153           0 :         i->last_percent = percent;
     154             : }
     155             : 
     156           0 : static int raw_import_maybe_convert_qcow2(RawImport *i) {
     157           0 :         _cleanup_close_ int converted_fd = -1;
     158           0 :         _cleanup_free_ char *t = NULL;
     159             :         int r;
     160             : 
     161           0 :         assert(i);
     162             : 
     163           0 :         r = qcow2_detect(i->output_fd);
     164           0 :         if (r < 0)
     165           0 :                 return log_error_errno(r, "Failed to detect whether this is a QCOW2 image: %m");
     166           0 :         if (r == 0)
     167           0 :                 return 0;
     168             : 
     169             :         /* This is a QCOW2 image, let's convert it */
     170           0 :         r = tempfn_random(i->final_path, NULL, &t);
     171           0 :         if (r < 0)
     172           0 :                 return log_oom();
     173             : 
     174           0 :         converted_fd = open(t, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
     175           0 :         if (converted_fd < 0)
     176           0 :                 return log_error_errno(errno, "Failed to create %s: %m", t);
     177             : 
     178           0 :         r = chattr_fd(converted_fd, FS_NOCOW_FL, FS_NOCOW_FL, NULL);
     179           0 :         if (r < 0)
     180           0 :                 log_warning_errno(r, "Failed to set file attributes on %s: %m", t);
     181             : 
     182           0 :         log_info("Unpacking QCOW2 file.");
     183             : 
     184           0 :         r = qcow2_convert(i->output_fd, converted_fd);
     185           0 :         if (r < 0) {
     186           0 :                 (void) unlink(t);
     187           0 :                 return log_error_errno(r, "Failed to convert qcow2 image: %m");
     188             :         }
     189             : 
     190           0 :         (void) unlink(i->temp_path);
     191           0 :         free_and_replace(i->temp_path, t);
     192             : 
     193           0 :         safe_close(i->output_fd);
     194           0 :         i->output_fd = TAKE_FD(converted_fd);
     195             : 
     196           0 :         return 1;
     197             : }
     198             : 
     199           0 : static int raw_import_finish(RawImport *i) {
     200             :         int r;
     201             : 
     202           0 :         assert(i);
     203           0 :         assert(i->output_fd >= 0);
     204           0 :         assert(i->temp_path);
     205           0 :         assert(i->final_path);
     206             : 
     207             :         /* In case this was a sparse file, make sure the file system is right */
     208           0 :         if (i->written_uncompressed > 0) {
     209           0 :                 if (ftruncate(i->output_fd, i->written_uncompressed) < 0)
     210           0 :                         return log_error_errno(errno, "Failed to truncate file: %m");
     211             :         }
     212             : 
     213           0 :         r = raw_import_maybe_convert_qcow2(i);
     214           0 :         if (r < 0)
     215           0 :                 return r;
     216             : 
     217           0 :         if (S_ISREG(i->st.st_mode)) {
     218           0 :                 (void) copy_times(i->input_fd, i->output_fd, COPY_CRTIME);
     219           0 :                 (void) copy_xattr(i->input_fd, i->output_fd);
     220             :         }
     221             : 
     222           0 :         if (i->read_only) {
     223           0 :                 r = import_make_read_only_fd(i->output_fd);
     224           0 :                 if (r < 0)
     225           0 :                         return r;
     226             :         }
     227             : 
     228           0 :         if (i->force_local)
     229           0 :                 (void) rm_rf(i->final_path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
     230             : 
     231           0 :         r = rename_noreplace(AT_FDCWD, i->temp_path, AT_FDCWD, i->final_path);
     232           0 :         if (r < 0)
     233           0 :                 return log_error_errno(r, "Failed to move image into place: %m");
     234             : 
     235           0 :         i->temp_path = mfree(i->temp_path);
     236             : 
     237           0 :         return 0;
     238             : }
     239             : 
     240           0 : static int raw_import_open_disk(RawImport *i) {
     241             :         int r;
     242             : 
     243           0 :         assert(i);
     244             : 
     245           0 :         assert(!i->final_path);
     246           0 :         assert(!i->temp_path);
     247           0 :         assert(i->output_fd < 0);
     248             : 
     249           0 :         i->final_path = strjoin(i->image_root, "/", i->local, ".raw");
     250           0 :         if (!i->final_path)
     251           0 :                 return log_oom();
     252             : 
     253           0 :         r = tempfn_random(i->final_path, NULL, &i->temp_path);
     254           0 :         if (r < 0)
     255           0 :                 return log_oom();
     256             : 
     257           0 :         (void) mkdir_parents_label(i->temp_path, 0700);
     258             : 
     259           0 :         i->output_fd = open(i->temp_path, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
     260           0 :         if (i->output_fd < 0)
     261           0 :                 return log_error_errno(errno, "Failed to open destination %s: %m", i->temp_path);
     262             : 
     263           0 :         r = chattr_fd(i->output_fd, FS_NOCOW_FL, FS_NOCOW_FL, NULL);
     264           0 :         if (r < 0)
     265           0 :                 log_warning_errno(r, "Failed to set file attributes on %s: %m", i->temp_path);
     266             : 
     267           0 :         return 0;
     268             : }
     269             : 
     270           0 : static int raw_import_try_reflink(RawImport *i) {
     271             :         off_t p;
     272             :         int r;
     273             : 
     274           0 :         assert(i);
     275           0 :         assert(i->input_fd >= 0);
     276           0 :         assert(i->output_fd >= 0);
     277             : 
     278           0 :         if (i->compress.type != IMPORT_COMPRESS_UNCOMPRESSED)
     279           0 :                 return 0;
     280             : 
     281           0 :         if (!S_ISREG(i->st.st_mode))
     282           0 :                 return 0;
     283             : 
     284           0 :         p = lseek(i->input_fd, 0, SEEK_CUR);
     285           0 :         if (p == (off_t) -1)
     286           0 :                 return log_error_errno(errno, "Failed to read file offset of input file: %m");
     287             : 
     288             :         /* Let's only try a btrfs reflink, if we are reading from the beginning of the file */
     289           0 :         if ((uint64_t) p != (uint64_t) i->buffer_size)
     290           0 :                 return 0;
     291             : 
     292           0 :         r = btrfs_reflink(i->input_fd, i->output_fd);
     293           0 :         if (r >= 0)
     294           0 :                 return 1;
     295             : 
     296           0 :         return 0;
     297             : }
     298             : 
     299           0 : static int raw_import_write(const void *p, size_t sz, void *userdata) {
     300           0 :         RawImport *i = userdata;
     301             :         ssize_t n;
     302             : 
     303           0 :         n = sparse_write(i->output_fd, p, sz, 64);
     304           0 :         if (n < 0)
     305           0 :                 return (int) n;
     306           0 :         if ((size_t) n < sz)
     307           0 :                 return -EIO;
     308             : 
     309           0 :         i->written_uncompressed += sz;
     310             : 
     311           0 :         return 0;
     312             : }
     313             : 
     314           0 : static int raw_import_process(RawImport *i) {
     315             :         ssize_t l;
     316             :         int r;
     317             : 
     318           0 :         assert(i);
     319           0 :         assert(i->buffer_size < sizeof(i->buffer));
     320             : 
     321           0 :         l = read(i->input_fd, i->buffer + i->buffer_size, sizeof(i->buffer) - i->buffer_size);
     322           0 :         if (l < 0) {
     323           0 :                 if (errno == EAGAIN)
     324           0 :                         return 0;
     325             : 
     326           0 :                 r = log_error_errno(errno, "Failed to read input file: %m");
     327           0 :                 goto finish;
     328             :         }
     329           0 :         if (l == 0) {
     330           0 :                 if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
     331           0 :                         log_error("Premature end of file.");
     332           0 :                         r = -EIO;
     333           0 :                         goto finish;
     334             :                 }
     335             : 
     336           0 :                 r = raw_import_finish(i);
     337           0 :                 goto finish;
     338             :         }
     339             : 
     340           0 :         i->buffer_size += l;
     341             : 
     342           0 :         if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
     343           0 :                 r = import_uncompress_detect(&i->compress, i->buffer, i->buffer_size);
     344           0 :                 if (r < 0) {
     345           0 :                         log_error_errno(r, "Failed to detect file compression: %m");
     346           0 :                         goto finish;
     347             :                 }
     348           0 :                 if (r == 0) /* Need more data */
     349           0 :                         return 0;
     350             : 
     351           0 :                 r = raw_import_open_disk(i);
     352           0 :                 if (r < 0)
     353           0 :                         goto finish;
     354             : 
     355           0 :                 r = raw_import_try_reflink(i);
     356           0 :                 if (r < 0)
     357           0 :                         goto finish;
     358           0 :                 if (r > 0) {
     359           0 :                         r = raw_import_finish(i);
     360           0 :                         goto finish;
     361             :                 }
     362             :         }
     363             : 
     364           0 :         r = import_uncompress(&i->compress, i->buffer, i->buffer_size, raw_import_write, i);
     365           0 :         if (r < 0) {
     366           0 :                 log_error_errno(r, "Failed to decode and write: %m");
     367           0 :                 goto finish;
     368             :         }
     369             : 
     370           0 :         i->written_compressed += i->buffer_size;
     371           0 :         i->buffer_size = 0;
     372             : 
     373           0 :         raw_import_report_progress(i);
     374             : 
     375           0 :         return 0;
     376             : 
     377           0 : finish:
     378           0 :         if (i->on_finished)
     379           0 :                 i->on_finished(i, r, i->userdata);
     380             :         else
     381           0 :                 sd_event_exit(i->event, r);
     382             : 
     383           0 :         return 0;
     384             : }
     385             : 
     386           0 : static int raw_import_on_input(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
     387           0 :         RawImport *i = userdata;
     388             : 
     389           0 :         return raw_import_process(i);
     390             : }
     391             : 
     392           0 : static int raw_import_on_defer(sd_event_source *s, void *userdata) {
     393           0 :         RawImport *i = userdata;
     394             : 
     395           0 :         return raw_import_process(i);
     396             : }
     397             : 
     398           0 : int raw_import_start(RawImport *i, int fd, const char *local, bool force_local, bool read_only) {
     399             :         int r;
     400             : 
     401           0 :         assert(i);
     402           0 :         assert(fd >= 0);
     403           0 :         assert(local);
     404             : 
     405           0 :         if (!machine_name_is_valid(local))
     406           0 :                 return -EINVAL;
     407             : 
     408           0 :         if (i->input_fd >= 0)
     409           0 :                 return -EBUSY;
     410             : 
     411           0 :         r = fd_nonblock(fd, true);
     412           0 :         if (r < 0)
     413           0 :                 return r;
     414             : 
     415           0 :         r = free_and_strdup(&i->local, local);
     416           0 :         if (r < 0)
     417           0 :                 return r;
     418           0 :         i->force_local = force_local;
     419           0 :         i->read_only = read_only;
     420             : 
     421           0 :         if (fstat(fd, &i->st) < 0)
     422           0 :                 return -errno;
     423             : 
     424           0 :         r = sd_event_add_io(i->event, &i->input_event_source, fd, EPOLLIN, raw_import_on_input, i);
     425           0 :         if (r == -EPERM) {
     426             :                 /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
     427           0 :                 r = sd_event_add_defer(i->event, &i->input_event_source, raw_import_on_defer, i);
     428           0 :                 if (r < 0)
     429           0 :                         return r;
     430             : 
     431           0 :                 r = sd_event_source_set_enabled(i->input_event_source, SD_EVENT_ON);
     432             :         }
     433           0 :         if (r < 0)
     434           0 :                 return r;
     435             : 
     436           0 :         i->input_fd = fd;
     437           0 :         return r;
     438             : }

Generated by: LCOV version 1.14