LCOV - code coverage report
Current view: top level - vhd/lib - libvhd.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 30 2050 1.5 %
Date: 2025-07-10 10:47:11 Functions: 3 144 2.1 %
Branches: 29 1434 2.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (c) 2016, Citrix Systems, Inc.
       3                 :            :  *
       4                 :            :  * All rights reserved.
       5                 :            :  *
       6                 :            :  * Redistribution and use in source and binary forms, with or without
       7                 :            :  * modification, are permitted provided that the following conditions are met:
       8                 :            :  * 
       9                 :            :  *  1. Redistributions of source code must retain the above copyright
      10                 :            :  *     notice, this list of conditions and the following disclaimer.
      11                 :            :  *  2. Redistributions in binary form must reproduce the above copyright
      12                 :            :  *     notice, this list of conditions and the following disclaimer in the
      13                 :            :  *     documentation and/or other materials provided with the distribution.
      14                 :            :  *  3. Neither the name of the copyright holder nor the names of its 
      15                 :            :  *     contributors may be used to endorse or promote products derived from 
      16                 :            :  *     this software without specific prior written permission.
      17                 :            :  *
      18                 :            :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      19                 :            :  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      20                 :            :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      21                 :            :  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
      22                 :            :  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
      23                 :            :  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
      24                 :            :  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
      25                 :            :  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
      26                 :            :  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
      27                 :            :  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
      28                 :            :  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      29                 :            :  */
      30                 :            : 
      31                 :            : #ifdef HAVE_CONFIG_H
      32                 :            : #include "config.h"
      33                 :            : #endif
      34                 :            : 
      35                 :            : #ifndef _GNU_SOURCE
      36                 :            : #define _GNU_SOURCE
      37                 :            : #endif
      38                 :            : #include <dirent.h>
      39                 :            : #include <stdio.h>
      40                 :            : #include <errno.h>
      41                 :            : #include <fcntl.h>
      42                 :            : #include <stdlib.h>
      43                 :            : #include <unistd.h>
      44                 :            : #include <string.h>
      45                 :            : #include <libgen.h>
      46                 :            : #include <iconv.h>
      47                 :            : #include <limits.h>
      48                 :            : #include <stdarg.h>
      49                 :            : #include <sys/mman.h>
      50                 :            : #include <sys/stat.h>
      51                 :            : #include <sys/sysmacros.h>
      52                 :            : #include <sys/types.h>
      53                 :            : 
      54                 :            : #include "debug.h"
      55                 :            : #include "xattr.h"
      56                 :            : #include "libvhd.h"
      57                 :            : #include "relative-path.h"
      58                 :            : #include "canonpath.h"
      59                 :            : #include "compiler.h"
      60                 :            : #include "util.h"
      61                 :            : 
      62                 :            : /* VHD uses an epoch of 12:00AM, Jan 1, 2000. This is the Unix timestamp for 
      63                 :            :  * the start of the VHD epoch. */
      64                 :            : #define VHD_EPOCH_START 946684800
      65                 :            : 
      66                 :            : /* VHD uses an epoch of 12:00AM, Jan 1, 2000. This is the Unix timestamp for 
      67                 :            :  * the start of the VHD epoch. */
      68                 :            : #define VHD_EPOCH_START 946684800
      69                 :            : 
      70                 :            : #define VHD_HEADER_MAX_RETRIES 10
      71                 :            : 
      72                 :            : static int libvhd_dbg = 0;
      73                 :            : 
      74                 :            : void
      75                 :          0 : libvhd_set_log_level(int level)
      76                 :            : {
      77         [ #  # ]:          0 :         if (level)
      78                 :          0 :                 libvhd_dbg = 1;
      79                 :          0 : }
      80                 :            : 
      81                 :            : #define VHDLOG(_f, _a...)                                               \
      82                 :            :         do {                                                            \
      83                 :            :                 if (libvhd_dbg)                                         \
      84                 :            :                         syslog(LOG_INFO, "libvhd::%s: "_f,            \
      85                 :            :                                __func__, ##_a);                         \
      86                 :            :         } while (0)
      87                 :            : 
      88                 :            : #ifdef ENABLE_FAILURE_TESTING
      89                 :            : const char* ENV_VAR_FAIL[NUM_FAIL_TESTS] = {
      90                 :            :         "VHD_UTIL_TEST_FAIL_REPARENT_BEGIN",
      91                 :            :         "VHD_UTIL_TEST_FAIL_REPARENT_LOCATOR",
      92                 :            :         "VHD_UTIL_TEST_FAIL_REPARENT_END",
      93                 :            :         "VHD_UTIL_TEST_FAIL_RESIZE_BEGIN",
      94                 :            :         "VHD_UTIL_TEST_FAIL_RESIZE_DATA_MOVED",
      95                 :            :         "VHD_UTIL_TEST_FAIL_RESIZE_METADATA_MOVED",
      96                 :            :         "VHD_UTIL_TEST_FAIL_RESIZE_END"
      97                 :            : };
      98                 :            : int TEST_FAIL[NUM_FAIL_TESTS];
      99                 :            : #endif // ENABLE_FAILURE_TESTING
     100                 :            : 
     101                 :            : static void vhd_cache_init(vhd_context_t *);
     102                 :            : static int vhd_cache_enabled(vhd_context_t *);
     103                 :            : static int vhd_cache_load(vhd_context_t *);
     104                 :            : static int vhd_cache_unload(vhd_context_t *);
     105                 :            : static vhd_context_t * vhd_cache_get_parent(vhd_context_t *);
     106                 :            : 
     107                 :            : static int
     108                 :          0 : makedev_from_file(const char *file, dev_t *dev)
     109                 :            : {
     110                 :          0 :         *dev = 0;
     111                 :            : 
     112                 :          0 :         FILE *f = fopen(file, "r");
     113         [ #  # ]:          0 :         if (!f)
     114                 :          0 :                 return -1;
     115                 :            : 
     116                 :            :         unsigned int dev_major;
     117                 :            :         unsigned int dev_minor;
     118                 :          0 :         const int ret = fscanf(f, "%u:%u", &dev_major, &dev_minor);
     119                 :          0 :         fclose(f);
     120                 :            : 
     121         [ #  # ]:          0 :         if (ret != 2)
     122                 :            :                 return -1;
     123                 :            : 
     124                 :          0 :         *dev = makedev(dev_major, dev_minor);
     125                 :          0 :         return 0;
     126                 :            : }
     127                 :            : 
     128                 :            : static int
     129                 :          0 : is_drbd_device(const struct stat *stats)
     130                 :            : {
     131                 :            :         static const int drbd_major = 147;
     132                 :            : 
     133 [ #  # ][ #  # ]:          0 :         if (!S_ISBLK(stats->st_mode))
     134                 :            :                 return 0;
     135                 :            : 
     136                 :          0 :         const unsigned int dev_major = major(stats->st_rdev);
     137 [ #  # ][ #  # ]:          0 :         if (dev_major != drbd_major)
     138                 :            :                 return 0;
     139                 :            : 
     140                 :          0 :         return 1;
     141                 :            : }
     142                 :            : 
     143                 :            : static int
     144                 :          0 : drbd_is_up_to_date(const struct stat *stats)
     145                 :            : {
     146                 :            :         static const char up_to_date_prefix[] = "UpToDate";
     147                 :            : 
     148         [ #  # ]:          0 :         if (!is_drbd_device(stats))
     149                 :          0 :                 return 0;
     150                 :            : 
     151                 :            :         char cmd[64];
     152                 :          0 :         snprintf(cmd, sizeof cmd, "drbdsetup dstate %u 2> /dev/null", minor(stats->st_rdev));
     153                 :            : 
     154                 :          0 :         int ok = 0;
     155                 :            : 
     156                 :          0 :         FILE *stream = popen(cmd, "r");
     157                 :          0 :         char line[1024] = {0};
     158         [ #  # ]:          0 :         if (
     159 [ #  # ][ #  # ]:          0 :                 stream && fscanf(stream, "%1023[^\n]", line) == 1 &&
     160                 :          0 :                 !strncmp(line, up_to_date_prefix, sizeof up_to_date_prefix - 1)
     161                 :            :         )
     162                 :          0 :                 ok = 1;
     163                 :            : 
     164         [ #  # ]:          0 :         if (stream)
     165                 :          0 :                 pclose(stream);
     166                 :            : 
     167                 :          0 :         return ok;
     168                 :            : }
     169                 :            : 
     170                 :            : static int
     171                 :          0 : drbd_to_mapper(const struct stat *stats, dev_t *dev)
     172                 :            : {
     173                 :          0 :         *dev = 0;
     174                 :            : 
     175         [ #  # ]:          0 :         if (!is_drbd_device(stats))
     176                 :          0 :                 return -1;
     177                 :            : 
     178                 :            :         /* Ok we can try to search a valid slave device. */
     179                 :            :         /* Note: If it's a diskless DRBD device, there is no slave. */
     180                 :          0 :         const unsigned int dev_major = major(stats->st_rdev);
     181                 :          0 :         const unsigned int dev_minor = minor(stats->st_rdev);
     182                 :            :         char slaves_dir[PATH_MAX];
     183                 :            :         snprintf(slaves_dir, sizeof slaves_dir, "/sys/dev/block/%u:%u/slaves", dev_major, dev_minor);
     184                 :            : 
     185                 :          0 :         DIR *dir = opendir(slaves_dir);
     186         [ #  # ]:          0 :         if (!dir)
     187                 :            :                 return -1;
     188                 :            : 
     189                 :            :         struct dirent *ent;
     190         [ #  # ]:          0 :         while ((ent = readdir(dir))) {
     191                 :            :                 /* Find a valid symlink to a slave. */
     192                 :            :                 #ifdef _DIRENT_HAVE_D_TYPE
     193         [ #  # ]:          0 :                         if (ent->d_type == DT_LNK) {
     194                 :            :                                 /* Ok here, we have a symlink! */
     195         [ #  # ]:          0 :                         } else if (ent->d_type != DT_UNKNOWN)
     196                 :          0 :                                 continue;
     197                 :            :                         else
     198                 :            :                 #endif
     199                 :            :                 {
     200                 :            :                         struct stat slave_stats;
     201         [ #  # ]:          0 :                         if (stat(ent->d_name, &slave_stats) == -1)
     202                 :          0 :                                 continue;
     203         [ #  # ]:          0 :                         if (!S_ISLNK(slave_stats.st_mode))
     204                 :          0 :                                 continue;
     205                 :            :                 }
     206                 :            : 
     207                 :            :                 /* Check if this entry is a valid device mapper. */
     208                 :            :                 static const char dm_prefix[] = "dm-";
     209         [ #  # ]:          0 :                 if (strncmp(dm_prefix, ent->d_name, sizeof dm_prefix - 1))
     210                 :          0 :                         continue;
     211                 :            : 
     212                 :            :                 char *p;
     213                 :          0 :                 char *dm_number = ent->d_name + sizeof dm_prefix - 1;
     214                 :          0 :                 strtol(dm_number, &p, 10);
     215 [ #  # ][ #  # ]:          0 :                 if (p == dm_number || errno == ERANGE || *p != '\0')
                 [ #  # ]
     216                 :          0 :                         continue;
     217                 :            : 
     218                 :            :                 /* Use this device mapper. */
     219                 :            :                 char dev_path[PATH_MAX];
     220                 :          0 :                 snprintf(dev_path, sizeof dev_path, "/sys/block/%s/dev", ent->d_name);
     221                 :            : 
     222                 :          0 :                 closedir(dir);
     223                 :          0 :                 return makedev_from_file(dev_path, dev);
     224                 :            :         }
     225                 :            : 
     226                 :          0 :         closedir(dir);
     227                 :            :         return -1;
     228                 :            : }
     229                 :            : 
     230                 :            : int
     231                 :          0 : open_optional_odirect(const char *pathname, int flags, ...)
     232                 :            : {
     233                 :          0 :         mode_t mode = 0;
     234         [ #  # ]:          0 :         if (flags & O_CREAT) {
     235                 :            :                 va_list arg;
     236                 :          0 :                 va_start(arg, flags);
     237         [ #  # ]:          0 :                 mode = va_arg(arg, mode_t);
     238                 :          0 :                 va_end(arg);
     239                 :            :         }
     240                 :          0 :         int fd = open(pathname, flags, mode);
     241 [ #  # ][ #  # ]:          0 :         if (fd == -1 && (flags & O_DIRECT) && errno == EINVAL) {
                 [ #  # ]
     242         [ #  # ]:          0 :                 VHDLOG("could not open() file '%s', retrying without O_DIRECT and using O_DSYNC\n", pathname);
     243                 :          0 :                 return open(pathname, ((flags & ~O_DIRECT) | O_DSYNC), mode);
     244                 :            :         }
     245                 :            :         return fd;
     246                 :            : }
     247                 :            : 
     248                 :            : void
     249                 :          0 : vhd_footer_in(vhd_footer_t *footer)
     250                 :            : {
     251                 :          0 :         BE32_IN(&footer->features);
     252                 :          0 :         BE32_IN(&footer->ff_version);
     253                 :          0 :         BE64_IN(&footer->data_offset);
     254                 :          0 :         BE32_IN(&footer->timestamp);
     255                 :          0 :         BE32_IN(&footer->crtr_ver);
     256                 :          0 :         BE32_IN(&footer->crtr_os);
     257                 :          0 :         BE64_IN(&footer->orig_size);
     258                 :          0 :         BE64_IN(&footer->curr_size);
     259                 :          0 :         BE32_IN(&footer->geometry);
     260                 :          0 :         BE32_IN(&footer->type);
     261                 :          0 :         BE32_IN(&footer->checksum);
     262                 :          0 : }
     263                 :            : 
     264                 :            : void
     265                 :          0 : vhd_footer_out(vhd_footer_t *footer)
     266                 :            : {
     267                 :          0 :         BE32_OUT(&footer->features);
     268                 :          0 :         BE32_OUT(&footer->ff_version);
     269                 :          0 :         BE64_OUT(&footer->data_offset);
     270                 :          0 :         BE32_OUT(&footer->timestamp);
     271                 :          0 :         BE32_OUT(&footer->crtr_ver);
     272                 :          0 :         BE32_OUT(&footer->crtr_os);
     273                 :          0 :         BE64_OUT(&footer->orig_size);
     274                 :          0 :         BE64_OUT(&footer->curr_size);
     275                 :          0 :         BE32_OUT(&footer->geometry);
     276                 :          0 :         BE32_OUT(&footer->type);
     277                 :          0 :         BE32_OUT(&footer->checksum);
     278                 :          0 : }
     279                 :            : 
     280                 :            : void
     281                 :          0 : vhd_header_in(vhd_header_t *header)
     282                 :            : {
     283                 :            :         int i, n;
     284                 :            : 
     285                 :          0 :         BE64_IN(&header->data_offset);
     286                 :          0 :         BE64_IN(&header->table_offset);
     287                 :          0 :         BE32_IN(&header->hdr_ver);
     288                 :          0 :         BE32_IN(&header->max_bat_size);
     289                 :          0 :         BE32_IN(&header->block_size);
     290                 :          0 :         BE32_IN(&header->checksum);
     291                 :          0 :         BE32_IN(&header->prt_ts);
     292                 :            : 
     293                 :          0 :         n = sizeof(header->loc) / sizeof(vhd_parent_locator_t);
     294                 :            : 
     295         [ #  # ]:          0 :         for (i = 0; i < n; i++) {
     296                 :          0 :                 BE32_IN(&header->loc[i].code);
     297                 :          0 :                 BE32_IN(&header->loc[i].data_space);
     298                 :          0 :                 BE32_IN(&header->loc[i].data_len);
     299                 :          0 :                 BE64_IN(&header->loc[i].data_offset);
     300                 :            :         }
     301                 :          0 : }
     302                 :            : 
     303                 :            : void
     304                 :          0 : vhd_header_out(vhd_header_t *header)
     305                 :            : {
     306                 :            :         int i, n;
     307                 :            : 
     308                 :          0 :         BE64_OUT(&header->data_offset);
     309                 :          0 :         BE64_OUT(&header->table_offset);
     310                 :          0 :         BE32_OUT(&header->hdr_ver);
     311                 :          0 :         BE32_OUT(&header->max_bat_size);
     312                 :          0 :         BE32_OUT(&header->block_size);
     313                 :          0 :         BE32_OUT(&header->checksum);
     314                 :          0 :         BE32_OUT(&header->prt_ts);
     315                 :            : 
     316                 :          0 :         n = sizeof(header->loc) / sizeof(vhd_parent_locator_t);
     317                 :            : 
     318         [ #  # ]:          0 :         for (i = 0; i < n; i++) {
     319                 :          0 :                 BE32_OUT(&header->loc[i].code);
     320                 :          0 :                 BE32_OUT(&header->loc[i].data_space);
     321                 :          0 :                 BE32_OUT(&header->loc[i].data_len);
     322                 :          0 :                 BE64_OUT(&header->loc[i].data_offset);
     323                 :            :         }
     324                 :          0 : }
     325                 :            : 
     326                 :            : void
     327                 :          0 : vhd_batmap_header_in(vhd_batmap_t *batmap)
     328                 :            : {
     329                 :          0 :         BE64_IN(&batmap->header.batmap_offset);
     330                 :          0 :         BE32_IN(&batmap->header.batmap_size);
     331                 :          0 :         BE32_IN(&batmap->header.batmap_version);
     332                 :          0 :         BE32_IN(&batmap->header.checksum);
     333                 :          0 : }
     334                 :            : 
     335                 :            : void
     336                 :          0 : vhd_batmap_header_out(vhd_batmap_t *batmap)
     337                 :            : {
     338                 :          0 :         BE64_OUT(&batmap->header.batmap_offset);
     339                 :          0 :         BE32_OUT(&batmap->header.batmap_size);
     340                 :          0 :         BE32_OUT(&batmap->header.batmap_version);
     341                 :          0 :         BE32_OUT(&batmap->header.checksum);
     342                 :          0 :         memset(batmap->header.res, 0, sizeof(batmap->header.res));
     343                 :          0 : }
     344                 :            : 
     345                 :            : void
     346                 :          0 : vhd_bat_in(vhd_bat_t *bat)
     347                 :            : {
     348                 :            :         int i;
     349                 :            : 
     350 [ #  # ][ #  # ]:          0 :         for (i = 0; i < bat->entries; i++)
     351                 :          0 :                 BE32_IN(&bat->bat[i]);
     352                 :          0 : }
     353                 :            : 
     354                 :            : void
     355                 :          0 : vhd_bat_out(vhd_bat_t *bat)
     356                 :            : {
     357                 :            :         int i;
     358                 :            : 
     359 [ #  # ][ #  # ]:          0 :         for (i = 0; i < bat->entries; i++)
     360                 :          0 :                 BE32_OUT(&bat->bat[i]);
     361                 :          0 : }
     362                 :            : 
     363                 :            : uint32_t
     364                 :          0 : vhd_checksum_footer(vhd_footer_t *footer)
     365                 :            : {
     366                 :            :         int i;
     367                 :            :         unsigned char *blob;
     368                 :            :         uint32_t checksum, tmp;
     369                 :            : 
     370                 :          0 :         checksum         = 0;
     371                 :          0 :         tmp              = footer->checksum;
     372                 :          0 :         footer->checksum = 0;
     373                 :            : 
     374                 :          0 :         blob = (unsigned char *)footer;
     375 [ #  # ][ #  # ]:          0 :         for (i = 0; i < sizeof(vhd_footer_t); i++)
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     376                 :          0 :                 checksum += (uint32_t)blob[i];
     377                 :            : 
     378                 :          0 :         footer->checksum = tmp;
     379                 :          0 :         return ~checksum;
     380                 :            : }
     381                 :            : 
     382                 :            : static int
     383                 :          0 : vhd_validate_footer_impl(vhd_footer_t *footer, bool suppress_invalid_footer_warning)
     384                 :            : {
     385                 :            :         int csize;
     386                 :            :         uint32_t checksum;
     387                 :            : 
     388                 :          0 :         csize = sizeof(footer->cookie);
     389 [ #  # ][ #  # ]:          0 :         if (memcmp(footer->cookie, HD_COOKIE, csize) != 0 &&
     390                 :          0 :             memcmp(footer->cookie, VHD_POISON_COOKIE, csize) != 0) {
     391                 :            :                 char buf[9];
     392                 :          0 :                 memcpy(buf, footer->cookie, 8);
     393                 :          0 :                 buf[8]= '\0';
     394         [ #  # ]:          0 :                 if (!suppress_invalid_footer_warning)
     395         [ #  # ]:          0 :                         VHDLOG("invalid footer cookie: %s\n", buf);
     396                 :          0 :                 return -EINVAL;
     397                 :            :         }
     398                 :            : 
     399                 :          0 :         checksum = vhd_checksum_footer(footer);
     400 [ #  # ][ #  # ]:          0 :         if (checksum != footer->checksum) {
     401                 :            :                 /*
     402                 :            :                  * early td-util did not re-calculate
     403                 :            :                  * checksum when marking vhds 'hidden'
     404                 :            :                  */
     405 [ #  # ][ #  # ]:          0 :                 if (footer->hidden &&
     406         [ #  # ]:          0 :                     !strncmp(footer->crtr_app, "tap", 3) &&
     407                 :          0 :                     (footer->crtr_ver == VHD_VERSION(0, 1) ||
     408                 :            :                      footer->crtr_ver == VHD_VERSION(1, 1))) {
     409                 :          0 :                         char tmp = footer->hidden;
     410                 :          0 :                         footer->hidden = 0;
     411                 :          0 :                         checksum = vhd_checksum_footer(footer);
     412                 :          0 :                         footer->hidden = tmp;
     413                 :            : 
     414 [ #  # ][ #  # ]:          0 :                         if (checksum == footer->checksum)
     415                 :            :                                 return 0;
     416                 :            :                 }
     417                 :            : 
     418         [ #  # ]:          0 :                 if (!suppress_invalid_footer_warning)
     419                 :            :                 {
     420         [ #  # ]:          0 :                         VHDLOG("invalid footer checksum: "
     421                 :            :                                "footer = 0x%08x, calculated = 0x%08x\n",
     422                 :            :                                footer->checksum, checksum);
     423                 :            :                 }
     424                 :            :                 return -EINVAL;
     425                 :            :         }
     426                 :            : 
     427                 :            :         return 0;
     428                 :            : }
     429                 :            : 
     430                 :            : int
     431                 :          0 : vhd_validate_footer(vhd_footer_t *footer)
     432                 :            : {
     433                 :          0 :     return vhd_validate_footer_impl(footer, false);
     434                 :            : }
     435                 :            : 
     436                 :            : uint32_t
     437                 :          8 : vhd_checksum_header(vhd_header_t *header)
     438                 :            : {
     439                 :            :         int i;
     440                 :            :         unsigned char *blob;
     441                 :            :         uint32_t checksum, tmp;
     442                 :            : 
     443                 :         10 :         checksum         = 0;
     444                 :         10 :         tmp              = header->checksum;
     445                 :         10 :         header->checksum = 0;
     446                 :            : 
     447                 :         10 :         blob = (unsigned char *)header;
     448 [ #  # ][ +  + ]:      10250 :         for (i = 0; i < sizeof(vhd_header_t); i++)
                 [ +  + ]
     449                 :      10240 :                 checksum += (uint32_t)blob[i];
     450                 :            : 
     451                 :         10 :         header->checksum = tmp;
     452                 :         10 :         return ~checksum;
     453                 :            : }
     454                 :            : 
     455                 :            : bool
     456                 :          0 : isPowerOf2(uint32_t value)
     457                 :            : {
     458 [ +  + ][ +  + ]:          4 :         return value && ((value & (value - 1)) == 0);
         [ #  # ][ #  # ]
     459                 :            : }
     460                 :            : 
     461                 :            : int
     462                 :          8 : vhd_validate_header(vhd_header_t *header)
     463                 :            : {
     464                 :            :         int i, n;
     465                 :            :         uint32_t checksum;
     466                 :            : 
     467         [ +  + ]:          8 :         if (memcmp(header->cookie, DD_COOKIE, 8) != 0) {
     468                 :            :                 char buf[9];
     469                 :          1 :                 memcpy(buf, header->cookie, 8);
     470                 :          1 :                 buf[8] = '\0';
     471         [ -  + ]:          1 :                 VHDLOG("invalid header cookie: %s\n", buf);
     472                 :          1 :                 return -EINVAL;
     473                 :            :         }
     474                 :            : 
     475         [ +  + ]:          7 :         if (header->hdr_ver != 0x00010000) {
     476         [ -  + ]:          1 :                 VHDLOG("invalid header version 0x%08x\n", header->hdr_ver);
     477                 :            :                 return -EINVAL;
     478                 :            :         }
     479                 :            : 
     480         [ +  + ]:          6 :         if (header->data_offset != 0xFFFFFFFFFFFFFFFFULL) {
     481         [ -  + ]:          1 :                 VHDLOG("invalid header data_offset 0x%016"PRIx64"\n",
     482                 :            :                        header->data_offset);
     483                 :            :                 return -EINVAL;
     484                 :            :         }
     485                 :            : 
     486   [ +  +  #  # ]:          9 :         if (header->block_size > VHD_BLOCK_SIZE ||
                 [ +  + ]
     487                 :          4 :             !isPowerOf2(header->block_size)) {
     488         [ -  + ]:          3 :                 VHDLOG("invalid header blocksize 0x%u\n",
     489                 :            :                         header->block_size);
     490                 :            :                 return -EINVAL;
     491                 :            :         }
     492                 :            : 
     493                 :            :         n = sizeof(header->loc) / sizeof(vhd_parent_locator_t);
     494         [ +  + ]:         18 :         for (i = 0; i < n; i++)
     495         [ +  - ]:         16 :                 if (vhd_validate_platform_code(header->loc[i].code))
     496                 :            :                         return -EINVAL;
     497                 :            : 
     498                 :          2 :         checksum = vhd_checksum_header(header);
     499 [ #  # ][ +  + ]:          2 :         if (checksum != header->checksum) {
     500         [ -  + ]:          1 :                 VHDLOG("invalid header checksum: "
     501                 :            :                        "header = 0x%08x, calculated = 0x%08x\n",
     502                 :            :                        header->checksum, checksum);
     503                 :            :                 return -EINVAL;
     504                 :            :         }
     505                 :            : 
     506                 :            :         return 0;
     507                 :            : }
     508                 :            : 
     509                 :            : static inline int
     510                 :          0 : vhd_validate_bat(vhd_bat_t *bat)
     511                 :            : {
     512 [ #  # ][ #  # ]:          0 :         if (!bat->bat)
         [ #  # ][ #  # ]
                 [ #  # ]
     513                 :            :                 return -EINVAL;
     514                 :            : 
     515                 :          0 :         return 0;
     516                 :            : }
     517                 :            : 
     518                 :            : uint32_t
     519                 :          0 : vhd_checksum_batmap(vhd_context_t *ctx, vhd_batmap_t *batmap)
     520                 :            : {
     521                 :            :         int i;
     522                 :            :         char *blob;
     523                 :            :         uint32_t checksum;
     524                 :            :         size_t map_size;
     525                 :            : 
     526                 :          0 :         blob     = batmap->map;
     527                 :          0 :         checksum = 0;
     528                 :            : 
     529                 :          0 :         map_size = vhd_sectors_to_bytes(secs_round_up_no_zero(
     530                 :          0 :                         ctx->footer.curr_size >> (VHD_BLOCK_SHIFT + 3)));
     531                 :            : 
     532         [ #  # ]:          0 :         for (i = 0; i < map_size; i++) {
     533         [ #  # ]:          0 :                 if (batmap->header.batmap_version == VHD_BATMAP_VERSION(1, 1))
     534                 :          0 :                         checksum += (uint32_t)blob[i];
     535                 :            :                 else
     536                 :          0 :                         checksum += (uint32_t)(unsigned char)blob[i];
     537                 :            :         }
     538                 :            : 
     539                 :          0 :         return ~checksum;
     540                 :            : }
     541                 :            : 
     542                 :            : int
     543                 :          0 : vhd_validate_batmap_header(vhd_batmap_t *batmap)
     544                 :            : {
     545         [ #  # ]:          0 :         if (memcmp(batmap->header.cookie, VHD_BATMAP_COOKIE, 8))
     546                 :            :                 return -EINVAL;
     547                 :            : 
     548         [ #  # ]:          0 :         if (batmap->header.batmap_version > VHD_BATMAP_CURRENT_VERSION)
     549                 :            :                 return -EINVAL;
     550                 :            : 
     551                 :          0 :         return 0;
     552                 :            : }
     553                 :            : 
     554                 :            : int
     555                 :          0 : vhd_validate_batmap(vhd_context_t *ctx, vhd_batmap_t *batmap)
     556                 :            : {
     557                 :            :         uint32_t checksum;
     558                 :            : 
     559         [ #  # ]:          0 :         if (!batmap->map)
           [ #  #  #  # ]
         [ #  # ][ #  # ]
     560                 :            :                 return -EINVAL;
     561                 :            : 
     562                 :          0 :         checksum = vhd_checksum_batmap(ctx, batmap);
     563 [ #  # ][ #  # ]:          0 :         if (checksum != batmap->header.checksum)
         [ #  # ][ #  # ]
     564                 :            :                 return -EINVAL;
     565                 :            : 
     566                 :          0 :         return 0;
     567                 :            : }
     568                 :            : 
     569                 :            : int
     570                 :          0 : vhd_batmap_header_offset(vhd_context_t *ctx, off64_t *_off)
     571                 :            : {
     572                 :            :         off64_t off;
     573                 :            :         size_t  bat;
     574                 :            : 
     575                 :          0 :         *_off = 0;
     576                 :            : 
     577                 :          0 :         off  = ctx->header.table_offset;
     578                 :          0 :         bat  = ctx->header.max_bat_size * sizeof(uint32_t);
     579                 :          0 :         off += vhd_bytes_padded(bat);
     580                 :            : 
     581                 :          0 :         *_off = off;
     582                 :          0 :         return 0;
     583                 :            : }
     584                 :            : 
     585                 :            : int
     586                 :         16 : vhd_validate_platform_code(uint32_t code)
     587                 :            : {
     588         [ -  + ]:         16 :         switch (code) {
     589                 :            :         case PLAT_CODE_NONE:
     590                 :            :         case PLAT_CODE_WI2R:
     591                 :            :         case PLAT_CODE_WI2K:
     592                 :            :         case PLAT_CODE_W2RU:
     593                 :            :         case PLAT_CODE_W2KU:
     594                 :            :         case PLAT_CODE_MAC:
     595                 :            :         case PLAT_CODE_MACX:
     596                 :            :                 return 0;
     597                 :            :         default:
     598         [ #  # ]:          0 :                 VHDLOG("invalid parent locator code %u\n", code);
     599                 :            :                 return -EINVAL;
     600                 :            :         }
     601                 :            : }
     602                 :            : 
     603                 :            : int
     604                 :          0 : vhd_parent_locator_count(vhd_context_t *ctx)
     605                 :            : {
     606                 :          0 :         return (sizeof(ctx->header.loc) / sizeof(vhd_parent_locator_t));
     607                 :            : }
     608                 :            : 
     609                 :            : int
     610                 :          0 : vhd_hidden(vhd_context_t *ctx, int *hidden)
     611                 :            : {
     612                 :            :         int err;
     613                 :            : 
     614                 :          0 :         *hidden = 0;
     615                 :            : 
     616 [ #  # ][ #  # ]:          0 :         if (vhd_type_dynamic(ctx) && vhd_creator_tapdisk(ctx) &&
                 [ #  # ]
     617                 :          0 :             (ctx->footer.crtr_ver == VHD_VERSION(0, 1) ||
     618                 :          0 :              ctx->footer.crtr_ver == VHD_VERSION(1, 1))) {
     619                 :            :                 vhd_footer_t copy;
     620                 :            : 
     621                 :          0 :                 err = vhd_read_footer_at(ctx, &copy, 0);
     622         [ #  # ]:          0 :                 if (err) {
     623         [ #  # ]:          0 :                         VHDLOG("error reading backup footer of %s: %d\n",
     624                 :            :                                ctx->file, err);
     625                 :          0 :                         return err;
     626                 :            :                 }
     627                 :          0 :                 *hidden = copy.hidden;
     628                 :            :         } else
     629                 :          0 :                 *hidden = ctx->footer.hidden;
     630                 :            : 
     631                 :            :         return 0;
     632                 :            : }
     633                 :            : 
     634                 :            : int
     635                 :          0 : vhd_chain_depth(vhd_context_t *ctx, int *depth)
     636                 :            : {
     637                 :            :         char *file;
     638                 :            :         int err, cnt;
     639                 :            :         vhd_context_t vhd, *cur;
     640                 :            : 
     641                 :          0 :         err    = 0;
     642                 :          0 :         cnt    = 0;
     643                 :          0 :         *depth = 0;
     644                 :          0 :         file   = NULL;
     645                 :          0 :         cur    = ctx;
     646                 :            : 
     647                 :            :         for (;;) {
     648                 :          0 :                 cnt++;
     649                 :            : 
     650         [ #  # ]:          0 :                 if (cur->footer.type != HD_TYPE_DIFF)
     651                 :            :                         break;
     652                 :            : 
     653         [ #  # ]:          0 :                 if (vhd_parent_raw(cur)) {
     654                 :          0 :                         cnt++;
     655                 :          0 :                         break;
     656                 :            :                 }
     657                 :            : 
     658                 :          0 :                 err = vhd_parent_locator_get(cur, &file);
     659         [ #  # ]:          0 :                 if (err) {
     660                 :          0 :                         file = NULL;
     661                 :          0 :                         break;
     662                 :            :                 }
     663                 :            : 
     664         [ #  # ]:          0 :                 if (cur != ctx) {
     665                 :          0 :                         vhd_close(cur);
     666                 :            :                         cur = NULL;
     667                 :            :                 }
     668                 :            : 
     669                 :          0 :                 err = vhd_open(&vhd, file, VHD_OPEN_RDONLY);
     670         [ #  # ]:          0 :                 if (err)
     671                 :            :                         break;
     672                 :            : 
     673                 :          0 :                 cur = &vhd;
     674                 :          0 :                 free(file);
     675                 :          0 :                 file = NULL;
     676                 :          0 :         }
     677                 :            : 
     678                 :          0 :         free(file);
     679         [ #  # ]:          0 :         if (cur && cur != ctx)
     680                 :          0 :                 vhd_close(cur);
     681                 :            : 
     682         [ #  # ]:          0 :         if (!err)
     683                 :          0 :                 *depth = cnt;
     684                 :            : 
     685                 :          0 :         return err;
     686                 :            : }
     687                 :            : 
     688                 :            : int
     689                 :          0 : vhd_batmap_test(vhd_context_t *ctx, vhd_batmap_t *batmap, uint32_t block)
     690                 :            : {
     691 [ #  # ][ #  # ]:          0 :         if (!vhd_has_batmap(ctx) || !batmap->map)
     692                 :            :                 return 0;
     693                 :            : 
     694         [ #  # ]:          0 :         if (block >= (batmap->header.batmap_size << (VHD_SECTOR_SHIFT + 3)))
     695                 :            :                 return 0;
     696                 :            : 
     697                 :          0 :         return test_bit(batmap->map, block);
     698                 :            : }
     699                 :            : 
     700                 :            : void
     701                 :          0 : vhd_batmap_set(vhd_context_t *ctx, vhd_batmap_t *batmap, uint32_t block)
     702                 :            : {
     703 [ #  # ][ #  # ]:          0 :         if (!vhd_has_batmap(ctx) || !batmap->map)
     704                 :            :                 return;
     705                 :            : 
     706         [ #  # ]:          0 :         if (block >= (batmap->header.batmap_size << (VHD_SECTOR_SHIFT + 3)))
     707                 :            :                 return;
     708                 :            : 
     709                 :          0 :         set_bit(batmap->map, block);
     710                 :            : }
     711                 :            : 
     712                 :            : void
     713                 :          0 : vhd_batmap_clear(vhd_context_t *ctx, vhd_batmap_t *batmap, uint32_t block)
     714                 :            : {
     715 [ #  # ][ #  # ]:          0 :         if (!vhd_has_batmap(ctx) || !batmap->map)
     716                 :            :                 return;
     717                 :            : 
     718         [ #  # ]:          0 :         if (block >= (batmap->header.batmap_size << (VHD_SECTOR_SHIFT + 3)))
     719                 :            :                 return;
     720                 :            : 
     721                 :          0 :         clear_bit(batmap->map, block);
     722                 :            : }
     723                 :            : 
     724                 :            : int
     725                 :          0 : vhd_bitmap_test(vhd_context_t *ctx, char *map, uint32_t block)
     726                 :            : {
     727                 :          0 :         return test_bit(map, block);
     728                 :            : }
     729                 :            : 
     730                 :            : void
     731                 :          0 : vhd_bitmap_set(vhd_context_t *ctx, char *map, uint32_t block)
     732                 :            : {
     733                 :          0 :         return set_bit(map, block);
     734                 :            : }
     735                 :            : 
     736                 :            : void
     737                 :          0 : vhd_bitmap_clear(vhd_context_t *ctx, char *map, uint32_t block)
     738                 :            : {
     739                 :          0 :         return clear_bit(map, block);
     740                 :            : }
     741                 :            : 
     742                 :            : /*
     743                 :            :  * returns absolute offset of the first 
     744                 :            :  * byte of the file which is not vhd metadata
     745                 :            :  */
     746                 :            : int
     747                 :          0 : vhd_end_of_headers(vhd_context_t *ctx, off64_t *end)
     748                 :            : {
     749                 :            :         int err, i, n;
     750                 :            :         uint32_t bat_bytes;
     751                 :            :         off64_t eom, bat_end;
     752                 :          0 :         vhd_parent_locator_t *loc;
     753                 :            : 
     754                 :          0 :         *end = 0;
     755                 :            : 
     756         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx))
     757                 :            :                 return 0;
     758                 :            : 
     759                 :          0 :         eom       = ctx->footer.data_offset + sizeof(vhd_header_t);
     760                 :            : 
     761                 :          0 :         bat_bytes = vhd_bytes_padded(ctx->header.max_bat_size * sizeof(uint32_t));
     762                 :          0 :         bat_end   = ctx->header.table_offset + bat_bytes;
     763                 :            : 
     764                 :          0 :         eom       = MAX(eom, bat_end);
     765                 :            : 
     766         [ #  # ]:          0 :         if (vhd_has_batmap(ctx)) {
     767                 :            :                 off64_t hdr_end, hdr_secs, map_end, map_secs;
     768                 :            : 
     769                 :          0 :                 err = vhd_get_batmap(ctx);
     770         [ #  # ]:          0 :                 if (err)
     771                 :          0 :                         return err;
     772                 :            : 
     773                 :          0 :                 hdr_secs = secs_round_up_no_zero(sizeof(vhd_batmap_header_t));
     774                 :          0 :                 err      = vhd_batmap_header_offset(ctx, &hdr_end);
     775         [ #  # ]:          0 :                 if (err)
     776                 :            :                         return err;
     777                 :            : 
     778                 :          0 :                 hdr_end += vhd_sectors_to_bytes(hdr_secs);
     779                 :          0 :                 eom      = MAX(eom, hdr_end);
     780                 :            : 
     781                 :          0 :                 map_secs = ctx->batmap.header.batmap_size;
     782                 :          0 :                 map_end  = (ctx->batmap.header.batmap_offset +
     783                 :          0 :                             vhd_sectors_to_bytes(map_secs));
     784                 :          0 :                 eom      = MAX(eom, map_end);
     785                 :            :         }
     786                 :            : 
     787                 :            :         /* parent locators */
     788                 :          0 :         n = sizeof(ctx->header.loc) / sizeof(vhd_parent_locator_t);
     789                 :            : 
     790         [ #  # ]:          0 :         for (i = 0; i < n; i++) {
     791                 :            :                 off64_t loc_end;
     792                 :            : 
     793                 :          0 :                 loc = &ctx->header.loc[i];
     794         [ #  # ]:          0 :                 if (loc->code == PLAT_CODE_NONE)
     795                 :          0 :                         continue;
     796                 :            : 
     797                 :          0 :                 loc_end = loc->data_offset + vhd_parent_locator_size(loc);
     798                 :          0 :                 eom     = MAX(eom, loc_end);
     799                 :            :         }
     800                 :            : 
     801                 :          0 :         *end = eom;
     802                 :          0 :         return 0;
     803                 :            : }
     804                 :            : 
     805                 :            : int
     806                 :          0 : vhd_end_of_data(vhd_context_t *ctx, off64_t *end)
     807                 :            : {
     808                 :            :         int i, err;
     809                 :            :         off64_t max;
     810                 :            :         uint64_t blk;
     811                 :            : 
     812         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx)) {
     813                 :          0 :                 err = vhd_seek(ctx, 0, SEEK_END);
     814         [ #  # ]:          0 :                 if (err)
     815                 :          0 :                         return err;
     816                 :            : 
     817                 :          0 :                 max = vhd_position(ctx);
     818 [ #  # ][ #  # ]:          0 :                 if (max == (off64_t)-1)
     819                 :          0 :                         return -errno;
     820                 :            : 
     821                 :          0 :                 *end = max - sizeof(vhd_footer_t);
     822                 :          0 :                 return 0;
     823                 :            :         }
     824                 :            : 
     825                 :          0 :         err = vhd_end_of_headers(ctx, &max);
     826         [ #  # ]:          0 :         if (err)
     827                 :            :                 return err;
     828                 :            : 
     829                 :          0 :         err = vhd_get_bat(ctx);
     830         [ #  # ]:          0 :         if (err)
     831                 :            :                 return err;
     832                 :            : 
     833                 :          0 :         max >>= VHD_SECTOR_SHIFT;
     834                 :            : 
     835         [ #  # ]:          0 :         for (i = 0; i < ctx->bat.entries; i++) {
     836                 :          0 :                 blk = ctx->bat.bat[i];
     837                 :            : 
     838         [ #  # ]:          0 :                 if (blk != DD_BLK_UNUSED) {
     839                 :          0 :                         blk += ctx->spb + ctx->bm_secs;
     840                 :          0 :                         max  = MAX(blk, max);
     841                 :            :                 }
     842                 :            :         }
     843                 :            : 
     844                 :          0 :         *end = vhd_sectors_to_bytes(max);
     845                 :          0 :         return 0;
     846                 :            : }
     847                 :            : 
     848                 :            : uint32_t inline
     849                 :          0 : vhd_time(time_t time)
     850                 :            : {
     851                 :          0 :         return (uint32_t)(time - VHD_EPOCH_START);
     852                 :            : }
     853                 :            : 
     854                 :            : /* 
     855                 :            :  * Stringify the VHD timestamp for printing.
     856                 :            :  * As with ctime_r, target must be >=26 bytes.
     857                 :            :  */
     858                 :            : size_t 
     859                 :          0 : vhd_time_to_string(uint32_t timestamp, char *target)
     860                 :            : {
     861                 :            :         char *cr;
     862                 :            :         time_t unix_timestamp;
     863                 :            : 
     864                 :          0 :         unix_timestamp = (time_t)timestamp + VHD_EPOCH_START;
     865                 :          0 :         ctime_r(&unix_timestamp, target);
     866                 :            : 
     867                 :            :         /* handle mad ctime_r newline appending. */
     868         [ #  # ]:          0 :         if ((cr = strchr(target, '\n')) != NULL)
     869                 :          0 :                 *cr = '\0';
     870                 :            : 
     871                 :          0 :         return (strlen(target));
     872                 :            : }
     873                 :            : 
     874                 :            : /*
     875                 :            :  * nabbed from vhd specs.
     876                 :            :  */
     877                 :            : uint32_t
     878                 :          0 : vhd_chs(uint64_t size)
     879                 :            : {
     880                 :            :         uint32_t secs, cylinders, heads, spt, cth;
     881                 :            : 
     882                 :          0 :         secs = secs_round_up_no_zero(size);
     883                 :            : 
     884         [ #  # ]:          0 :         if (secs > 65535 * 16 * 255)
     885                 :          0 :                 secs = 65535 * 16 * 255;
     886                 :            : 
     887         [ #  # ]:          0 :         if (secs >= 65535 * 16 * 63) {
     888                 :          0 :                 spt   = 255;
     889                 :          0 :                 cth   = secs / spt;
     890                 :          0 :                 heads = 16;
     891                 :            :         } else {
     892                 :          0 :                 spt   = 17;
     893                 :          0 :                 cth   = secs / spt;
     894                 :          0 :                 heads = (cth + 1023) / 1024;
     895                 :            : 
     896         [ #  # ]:          0 :                 if (heads < 4)
     897                 :          0 :                         heads = 4;
     898                 :            : 
     899 [ #  # ][ #  # ]:          0 :                 if (cth >= (heads * 1024) || heads > 16) {
     900                 :          0 :                         spt   = 31;
     901                 :          0 :                         cth   = secs / spt;
     902                 :          0 :                         heads = 16;
     903                 :            :                 }
     904                 :            : 
     905         [ #  # ]:          0 :                 if (cth >= heads * 1024) {
     906                 :          0 :                         spt   = 63;
     907                 :          0 :                         cth   = secs / spt;
     908                 :          0 :                         heads = 16;
     909                 :            :                 }
     910                 :            :         }
     911                 :            : 
     912                 :          0 :         cylinders = cth / heads;
     913                 :            : 
     914                 :          0 :         return GEOM_ENCODE(cylinders, heads, spt);
     915                 :            : }
     916                 :            : 
     917                 :            : int
     918                 :          0 : vhd_get_footer(vhd_context_t *ctx)
     919                 :            : {
     920 [ #  # ][ #  # ]:          0 :         if (!vhd_validate_footer(&ctx->footer))
     921                 :            :                 return 0;
     922                 :            : 
     923                 :          0 :         return vhd_read_footer(ctx, &ctx->footer, false);
     924                 :            : }
     925                 :            : 
     926                 :            : int
     927                 :          0 : vhd_get_header(vhd_context_t *ctx)
     928                 :            : {
     929         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx))
     930                 :            :                 return -EINVAL;
     931                 :            : 
     932         [ #  # ]:          0 :         if (!vhd_validate_header(&ctx->header))
     933                 :            :                 return 0;
     934                 :            : 
     935                 :          0 :         return vhd_read_header(ctx, &ctx->header);
     936                 :            : }
     937                 :            : 
     938                 :            : int
     939                 :          0 : vhd_get_bat(vhd_context_t *ctx)
     940                 :            : {
     941         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx))
     942                 :            :                 return -EINVAL;
     943                 :            : 
     944         [ #  # ]:          0 :         if (!vhd_validate_bat(&ctx->bat))
     945                 :            :                 return 0;
     946                 :            : 
     947                 :          0 :         vhd_put_bat(ctx);
     948                 :          0 :         return vhd_read_bat(ctx, &ctx->bat);
     949                 :            : }
     950                 :            : 
     951                 :            : int
     952                 :          0 : vhd_get_batmap(vhd_context_t *ctx)
     953                 :            : {
     954         [ #  # ]:          0 :         if (!vhd_has_batmap(ctx))
     955                 :            :                 return -EINVAL;
     956                 :            : 
     957      [ #  #  # ]:          0 :         if (!vhd_validate_batmap(ctx, &ctx->batmap))
     958                 :            :                 return 0;
     959                 :            : 
     960                 :          0 :         vhd_put_batmap(ctx);
     961                 :          0 :         return vhd_read_batmap(ctx, &ctx->batmap);
     962                 :            : }
     963                 :            : 
     964                 :            : void
     965                 :          0 : vhd_put_footer(vhd_context_t *ctx)
     966                 :            : {
     967                 :          0 :         memset(&ctx->footer, 0, sizeof(vhd_footer_t));
     968                 :          0 : }
     969                 :            : 
     970                 :            : void
     971                 :          0 : vhd_put_header(vhd_context_t *ctx)
     972                 :            : {
     973                 :          0 :         memset(&ctx->header, 0, sizeof(vhd_header_t));
     974                 :          0 : }
     975                 :            : 
     976                 :            : void
     977                 :          0 : vhd_put_bat(vhd_context_t *ctx)
     978                 :            : {
     979         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx))
     980                 :          0 :                 return;
     981                 :            : 
     982                 :          0 :         free(ctx->bat.bat);
     983                 :          0 :         memset(&ctx->bat, 0, sizeof(vhd_bat_t));
     984                 :            : }
     985                 :            : 
     986                 :            : void
     987                 :          0 : vhd_put_batmap(vhd_context_t *ctx)
     988                 :            : {
     989         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx))
     990                 :            :                 return;
     991                 :            : 
     992         [ #  # ]:          0 :         if (!vhd_has_batmap(ctx))
     993                 :            :                 return;
     994                 :            : 
     995                 :          0 :         free(ctx->batmap.map);
     996                 :          0 :         memset(&ctx->batmap, 0, sizeof(vhd_batmap_t));
     997                 :            : }
     998                 :            : 
     999                 :            : /*
    1000                 :            :  * look for 511 byte footer at end of file
    1001                 :            :  */
    1002                 :            : static int
    1003                 :          0 : vhd_read_short_footer_impl(vhd_context_t *ctx, vhd_footer_t *footer, bool suppress_invalid_footer_warning)
    1004                 :            : {
    1005                 :            :         off64_t eof;
    1006                 :            :         void *buf;
    1007                 :            :         int err;
    1008                 :            : 
    1009                 :          0 :         buf = NULL;
    1010                 :            : 
    1011                 :          0 :         err = vhd_seek(ctx, 0, SEEK_END);
    1012         [ #  # ]:          0 :         if (err)
    1013                 :            :                 goto out;
    1014                 :            : 
    1015                 :          0 :         eof = vhd_position(ctx);
    1016 [ #  # ][ #  # ]:          0 :         if (eof == (off64_t)-1) {
    1017                 :          0 :                 err = -errno;
    1018                 :          0 :                 goto out;
    1019                 :            :         }
    1020                 :            : 
    1021         [ #  # ]:          0 :         if (((eof - 511) % VHD_SECTOR_SIZE) != 0) {
    1022                 :            :                 /*
    1023                 :            :                  * The VHD file with short footer should have the size in the form 512 * n + 511,
    1024                 :            :                  * also vhd_read on block VHDs won't succeed if trying to read from a position
    1025                 :            :                  * that is not a multiple of 512.
    1026                 :            :                  */
    1027         [ #  # ]:          0 :                 if (!suppress_invalid_footer_warning)
    1028         [ #  # ]:          0 :                         VHDLOG("%s: failed reading short footer: file size does not meet requirement",
    1029                 :            :                                         ctx->file);
    1030                 :            :                 err = -EINVAL;
    1031                 :            :                 goto out;
    1032                 :            :         }
    1033                 :            : 
    1034                 :          0 :         err = vhd_seek(ctx, eof - 511, SEEK_SET);
    1035         [ #  # ]:          0 :         if (err)
    1036                 :            :                 goto out;
    1037                 :            : 
    1038                 :          0 :         err = posix_memalign(&buf, VHD_SECTOR_SIZE, sizeof(vhd_footer_t));
    1039         [ #  # ]:          0 :         if (err) {
    1040                 :          0 :                 buf = NULL;
    1041                 :          0 :                 err = -err;
    1042                 :          0 :                 goto out;
    1043                 :            :         }
    1044                 :            : 
    1045                 :          0 :         memset(buf, 0, sizeof(vhd_footer_t));
    1046                 :            : 
    1047                 :            :         /*
    1048                 :            :          * expecting short read here
    1049                 :            :          */
    1050                 :          0 :         vhd_read(ctx, buf, sizeof(vhd_footer_t));
    1051                 :            : 
    1052                 :          0 :         memcpy(footer, buf, sizeof(vhd_footer_t));
    1053                 :            : 
    1054                 :          0 :         vhd_footer_in(footer);
    1055                 :          0 :         err = vhd_validate_footer_impl(footer, suppress_invalid_footer_warning);
    1056                 :            : 
    1057                 :            : out:
    1058         [ #  # ]:          0 :         if (err && !suppress_invalid_footer_warning)
    1059         [ #  # ]:          0 :                 VHDLOG("%s: failed reading short footer: %d\n",
    1060                 :            :                        ctx->file, err);
    1061                 :          0 :         free(buf);
    1062                 :          0 :         return err;
    1063                 :            : }
    1064                 :            : 
    1065                 :            : int
    1066                 :          0 : vhd_read_short_footer(vhd_context_t *ctx, vhd_footer_t *footer)
    1067                 :            : {
    1068                 :          0 :     return vhd_read_short_footer_impl(ctx, footer, false);
    1069                 :            : }
    1070                 :            : 
    1071                 :            : static int
    1072                 :          0 : vhd_read_footer_at_impl(vhd_context_t *ctx, vhd_footer_t *footer, off64_t off, bool suppress_invalid_footer_warning)
    1073                 :            : {
    1074                 :            :         void *buf;
    1075                 :            :         int err;
    1076                 :            : 
    1077                 :          0 :         buf = NULL;
    1078                 :            : 
    1079                 :          0 :         err = vhd_seek(ctx, off, SEEK_SET);
    1080         [ #  # ]:          0 :         if (err)
    1081                 :            :                 goto out;
    1082                 :            : 
    1083                 :          0 :         err = posix_memalign(&buf, VHD_SECTOR_SIZE, sizeof(vhd_footer_t));
    1084         [ #  # ]:          0 :         if (err) {
    1085                 :          0 :                 buf = NULL;
    1086                 :          0 :                 err = -err;
    1087                 :          0 :                 goto out;
    1088                 :            :         }
    1089                 :            : 
    1090                 :          0 :         err = vhd_read(ctx, buf, sizeof(vhd_footer_t));
    1091         [ #  # ]:          0 :         if (err)
    1092                 :            :                 goto out;
    1093                 :            : 
    1094                 :          0 :         memcpy(footer, buf, sizeof(vhd_footer_t));
    1095                 :            : 
    1096                 :          0 :         vhd_footer_in(footer);
    1097                 :          0 :         err = vhd_validate_footer_impl(footer, suppress_invalid_footer_warning);
    1098                 :            : 
    1099                 :            : out:
    1100         [ #  # ]:          0 :         if (err && !suppress_invalid_footer_warning)
    1101         [ #  # ]:          0 :                 VHDLOG("%s: reading footer at 0x%08"PRIx64" failed: %d\n",
    1102                 :            :                        ctx->file, off, err);
    1103                 :          0 :         free(buf);
    1104                 :          0 :         return err;
    1105                 :            : }
    1106                 :            : 
    1107                 :            : int
    1108                 :          0 : vhd_read_footer_at(vhd_context_t *ctx, vhd_footer_t *footer, off64_t off)
    1109                 :            : {
    1110                 :          0 :     return vhd_read_footer_at_impl(ctx, footer, off, false);
    1111                 :            : }
    1112                 :            : 
    1113                 :            : int
    1114                 :          0 : vhd_read_footer(vhd_context_t *ctx, vhd_footer_t *footer, bool use_bkp_footer)
    1115                 :            : {
    1116                 :            :         int err;
    1117                 :            :         off64_t off;
    1118                 :            : 
    1119                 :          0 :         err = vhd_seek(ctx, 0, SEEK_END);
    1120         [ #  # ]:          0 :         if (err)
    1121                 :            :                 return err;
    1122                 :            : 
    1123                 :          0 :         off = vhd_position(ctx);
    1124 [ #  # ][ #  # ]:          0 :         if (off == (off64_t)-1)
    1125                 :          0 :                 return -errno;
    1126                 :            : 
    1127         [ #  # ]:          0 :         if (!use_bkp_footer) {
    1128                 :            :                 /*
    1129                 :            :                  * As we will read the backup footer if the primary one is invalid,
    1130                 :            :                  * stop complaining the primary one is invalid
    1131                 :            :                  */
    1132                 :            : 
    1133                 :          0 :                 err = vhd_read_footer_at_impl(ctx, footer, off - 512, true);
    1134         [ #  # ]:          0 :                 if (err != -EINVAL)
    1135                 :            :                         return err;
    1136                 :            : 
    1137                 :          0 :                 err = vhd_read_short_footer_impl(ctx, footer, true);
    1138         [ #  # ]:          0 :                 if (err != -EINVAL)
    1139                 :            :                         return err;
    1140                 :            :         }
    1141                 :            : 
    1142                 :            :         /* 
    1143                 :            :          * Disable the enforcement of VHD_OPEN_STRICT until we figure out how 
    1144                 :            :          * to recover from crashes. Note that we never enforced it before 
    1145                 :            :          * anyways due to a bug (CA-28285) and everything was ok.
    1146                 :            :          */
    1147                 :            :         /* if (ctx->oflags & VHD_OPEN_STRICT)
    1148                 :            :                 return -EINVAL; */
    1149                 :            : 
    1150                 :          0 :         return vhd_read_footer_at(ctx, footer, 0);
    1151                 :            : }
    1152                 :            : 
    1153                 :            : int
    1154                 :          0 : vhd_read_header_at(vhd_context_t *ctx, vhd_header_t *header, off64_t off)
    1155                 :            : {
    1156                 :            :         void *buf;
    1157                 :            :         int err;
    1158                 :            : 
    1159                 :          0 :         buf = NULL;
    1160                 :            : 
    1161         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx)) {
    1162                 :            :                 err = -EINVAL;
    1163                 :            :                 goto out;
    1164                 :            :         }
    1165                 :            : 
    1166                 :          0 :         err = vhd_seek(ctx, off, SEEK_SET);
    1167         [ #  # ]:          0 :         if (err)
    1168                 :            :                 goto out;
    1169                 :            : 
    1170                 :          0 :         err = posix_memalign(&buf, VHD_SECTOR_SIZE, sizeof(vhd_header_t));
    1171         [ #  # ]:          0 :         if (err) {
    1172                 :          0 :                 buf = NULL;
    1173                 :          0 :                 err = -err;
    1174                 :          0 :                 goto out;
    1175                 :            :         }
    1176                 :            : 
    1177                 :          0 :         err = vhd_read(ctx, buf, sizeof(vhd_header_t));
    1178         [ #  # ]:          0 :         if (err)
    1179                 :            :                 goto out;
    1180                 :            : 
    1181                 :          0 :         memcpy(header, buf, sizeof(vhd_header_t));
    1182                 :            : 
    1183                 :          0 :         vhd_header_in(header);
    1184                 :          0 :         err = vhd_validate_header(header);
    1185                 :            : 
    1186                 :            : out:
    1187         [ #  # ]:          0 :         if (err)
    1188         [ #  # ]:          0 :                 VHDLOG("%s: reading header at 0x%08"PRIx64" failed: %d\n",
    1189                 :            :                        ctx->file, off, err);
    1190                 :          0 :         free(buf);
    1191                 :          0 :         return err;
    1192                 :            : }
    1193                 :            : 
    1194                 :            : int
    1195                 :          0 : vhd_read_header(vhd_context_t *ctx, vhd_header_t *header)
    1196                 :            : {
    1197                 :            :         off64_t off;
    1198                 :            : 
    1199         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx)) {
    1200         [ #  # ]:          0 :                 VHDLOG("%s is not dynamic!\n", ctx->file);
    1201                 :            :                 return -EINVAL;
    1202                 :            :         }
    1203                 :            : 
    1204                 :          0 :         off = ctx->footer.data_offset;
    1205                 :          0 :         return vhd_read_header_at(ctx, header, off);
    1206                 :            : }
    1207                 :            : 
    1208                 :            : int
    1209                 :          0 : vhd_read_bat(vhd_context_t *ctx, vhd_bat_t *bat)
    1210                 :            : {
    1211                 :            :         int err;
    1212                 :            :         void *buf;
    1213                 :            :         off64_t off;
    1214                 :            :         uint32_t vhd_blks;
    1215                 :            :         size_t size;
    1216                 :            : 
    1217                 :          0 :         buf  = NULL;
    1218                 :            : 
    1219         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx)) {
    1220                 :            :                 err = -EINVAL;
    1221                 :            :                 goto fail;
    1222                 :            :         }
    1223                 :            : 
    1224                 :          0 :         off  = ctx->header.table_offset;
    1225                 :            :         /* The BAT size is stored in ctx->header.max_bat_size. However, we
    1226                 :            :          * sometimes preallocate BAT + batmap for max VHD size, so only read in
    1227                 :            :          * the BAT entries that are in use for curr_size */
    1228                 :          0 :         vhd_blks = (ctx->footer.curr_size + ((1 << VHD_BLOCK_SHIFT) - 1)) >> VHD_BLOCK_SHIFT;
    1229         [ #  # ]:          0 :         if (ctx->header.max_bat_size < vhd_blks) {
    1230         [ #  # ]:          0 :                 VHDLOG("more VHD blocks (%u) than possible (%u)\n",
    1231                 :            :                        vhd_blks, ctx->header.max_bat_size);
    1232                 :            :                 err = -EINVAL;
    1233                 :            :                 goto fail;
    1234                 :            :         }
    1235                 :          0 :         size = vhd_bytes_padded(vhd_blks * sizeof(uint32_t));
    1236                 :            : 
    1237                 :          0 :         err  = posix_memalign(&buf, VHD_SECTOR_SIZE, size);
    1238         [ #  # ]:          0 :         if (err) {
    1239                 :          0 :                 buf = NULL;
    1240                 :          0 :                 err = -err;
    1241                 :          0 :                 goto fail;
    1242                 :            :         }
    1243                 :            : 
    1244                 :          0 :         err = vhd_seek(ctx, off, SEEK_SET);
    1245         [ #  # ]:          0 :         if (err)
    1246                 :            :                 goto fail;
    1247                 :            : 
    1248                 :          0 :         err = vhd_read(ctx, buf, size);
    1249         [ #  # ]:          0 :         if (err)
    1250                 :            :                 goto fail;
    1251                 :            : 
    1252                 :          0 :         bat->spb     = ctx->header.block_size >> VHD_SECTOR_SHIFT;
    1253                 :          0 :         bat->entries = vhd_blks;
    1254                 :          0 :         bat->bat     = (uint32_t *)buf;
    1255                 :            : 
    1256                 :          0 :         vhd_bat_in(bat);
    1257                 :            : 
    1258                 :          0 :         return 0;
    1259                 :            : 
    1260                 :            : fail:
    1261                 :          0 :         free(buf);
    1262                 :            :         memset(bat, 0, sizeof(vhd_bat_t));
    1263         [ #  # ]:          0 :         VHDLOG("%s: failed to read bat: %d\n", ctx->file, err);
    1264                 :          0 :         return err;
    1265                 :            : }
    1266                 :            : 
    1267                 :            : static int
    1268                 :          0 : vhd_read_batmap_header(vhd_context_t *ctx, vhd_batmap_t *batmap)
    1269                 :            : {
    1270                 :            :         int err;
    1271                 :            :         void *buf;
    1272                 :            :         off64_t off;
    1273                 :            :         size_t size;
    1274                 :            : 
    1275                 :          0 :         buf = NULL;
    1276                 :            : 
    1277                 :          0 :         err = vhd_batmap_header_offset(ctx, &off);
    1278         [ #  # ]:          0 :         if (err)
    1279                 :            :                 goto fail;
    1280                 :            : 
    1281                 :          0 :         err = vhd_seek(ctx, off, SEEK_SET);
    1282         [ #  # ]:          0 :         if (err)
    1283                 :            :                 goto fail;
    1284                 :            : 
    1285                 :          0 :         size = vhd_bytes_padded(sizeof(vhd_batmap_header_t));
    1286                 :          0 :         err  = posix_memalign(&buf, VHD_SECTOR_SIZE, size);
    1287         [ #  # ]:          0 :         if (err) {
    1288                 :          0 :                 buf = NULL;
    1289                 :          0 :                 err = -err;
    1290                 :          0 :                 goto fail;
    1291                 :            :         }
    1292                 :            : 
    1293                 :          0 :         err = vhd_read(ctx, buf, size);
    1294         [ #  # ]:          0 :         if (err)
    1295                 :            :                 goto fail;
    1296                 :            : 
    1297                 :          0 :         memcpy(&batmap->header, buf, sizeof(vhd_batmap_header_t));
    1298                 :          0 :         free(buf);
    1299                 :          0 :         buf = NULL;
    1300                 :            : 
    1301                 :          0 :         vhd_batmap_header_in(batmap);
    1302                 :            : 
    1303                 :          0 :         return 0;
    1304                 :            : 
    1305                 :            : fail:
    1306                 :          0 :         free(buf);
    1307                 :          0 :         memset(&batmap->header, 0, sizeof(vhd_batmap_header_t));
    1308         [ #  # ]:          0 :         VHDLOG("%s: failed to read batmap header: %d\n", ctx->file, err);
    1309                 :          0 :         return err;
    1310                 :            : }
    1311                 :            : 
    1312                 :            : static int
    1313                 :          0 : vhd_read_batmap_map(vhd_context_t *ctx, vhd_batmap_t *batmap)
    1314                 :            : {
    1315                 :            :         int err;
    1316                 :            :         void *buf;
    1317                 :            :         off64_t off;
    1318                 :            :         size_t map_size;
    1319                 :            : 
    1320                 :          0 :         map_size = vhd_sectors_to_bytes(secs_round_up_no_zero(
    1321                 :          0 :                         ctx->footer.curr_size >> (VHD_BLOCK_SHIFT + 3)));
    1322         [ #  # ]:          0 :         ASSERT(vhd_sectors_to_bytes(batmap->header.batmap_size) >= map_size);
    1323                 :            : 
    1324                 :          0 :         err = posix_memalign(&buf, VHD_SECTOR_SIZE, map_size);
    1325         [ #  # ]:          0 :         if (err) {
    1326                 :          0 :                 buf = NULL;
    1327                 :          0 :                 err = -err;
    1328                 :          0 :                 goto fail;
    1329                 :            :         }
    1330                 :            : 
    1331                 :          0 :         off  = batmap->header.batmap_offset;
    1332                 :          0 :         err  = vhd_seek(ctx, off, SEEK_SET);
    1333         [ #  # ]:          0 :         if (err)
    1334                 :            :                 goto fail;
    1335                 :            : 
    1336                 :          0 :         err  = vhd_read(ctx, buf, map_size);
    1337         [ #  # ]:          0 :         if (err)
    1338                 :            :                 goto fail;
    1339                 :            : 
    1340                 :          0 :         batmap->map = buf;
    1341                 :          0 :         return 0;
    1342                 :            : 
    1343                 :            : fail:
    1344                 :          0 :         free(buf);
    1345                 :          0 :         batmap->map = NULL;
    1346         [ #  # ]:          0 :         VHDLOG("%s: failed to read batmap: %d\n", ctx->file, err);
    1347                 :          0 :         return err;
    1348                 :            : }
    1349                 :            : 
    1350                 :            : int
    1351                 :          0 : vhd_read_batmap(vhd_context_t *ctx, vhd_batmap_t *batmap)
    1352                 :            : {
    1353                 :            :         int err;
    1354                 :            : 
    1355         [ #  # ]:          0 :         if (!vhd_has_batmap(ctx))
    1356                 :            :                 return -EINVAL;
    1357                 :            : 
    1358                 :            :         memset(batmap, 0, sizeof(vhd_batmap_t));
    1359                 :            : 
    1360                 :          0 :         err = vhd_read_batmap_header(ctx, batmap);
    1361         [ #  # ]:          0 :         if (err)
    1362                 :            :                 return err;
    1363                 :            : 
    1364                 :          0 :         err = vhd_validate_batmap_header(batmap);
    1365         [ #  # ]:          0 :         if (err)
    1366                 :            :                 return err;
    1367                 :            : 
    1368                 :          0 :         err = vhd_read_batmap_map(ctx, batmap);
    1369         [ #  # ]:          0 :         if (err)
    1370                 :            :                 return err;
    1371                 :            : 
    1372                 :          0 :         err = vhd_validate_batmap(ctx, batmap);
    1373 [ #  # ][ #  # ]:          0 :         if (err)
    1374                 :            :                 goto fail;
    1375                 :            : 
    1376                 :            :         return 0;
    1377                 :            : 
    1378                 :            : fail:
    1379                 :          0 :         free(batmap->map);
    1380                 :            :         memset(batmap, 0, sizeof(vhd_batmap_t));
    1381                 :          0 :         return err;
    1382                 :            : }
    1383                 :            : 
    1384                 :            : int
    1385                 :          0 : vhd_has_batmap(vhd_context_t *ctx)
    1386                 :            : {
    1387         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx))
    1388                 :            :                 return 0;
    1389                 :            : 
    1390         [ #  # ]:          0 :         if (!vhd_creator_tapdisk(ctx))
    1391                 :            :                 return 0;
    1392                 :            : 
    1393         [ #  # ]:          0 :         if (ctx->footer.crtr_ver <= VHD_VERSION(0, 1))
    1394                 :            :                 return 0;
    1395                 :            : 
    1396         [ #  # ]:          0 :         if (ctx->footer.crtr_ver >= VHD_VERSION(1, 2))
    1397                 :            :                 return 1;
    1398                 :            : 
    1399                 :            :         /*
    1400                 :            :          * VHDs of version 1.1 probably have a batmap, but may not 
    1401                 :            :          * if they were updated from version 0.1 via vhd-update.
    1402                 :            :          */
    1403         [ #  # ]:          0 :         if (!vhd_validate_batmap_header(&ctx->batmap))
    1404                 :            :                 return 1;
    1405                 :            : 
    1406         [ #  # ]:          0 :         if (vhd_read_batmap_header(ctx, &ctx->batmap))
    1407                 :            :                 return 0;
    1408                 :            : 
    1409                 :          0 :         return (!vhd_validate_batmap_header(&ctx->batmap));
    1410                 :            : }
    1411                 :            : 
    1412                 :            : /* 
    1413                 :            :  * Is this a block device (with a fixed size)? This affects whether the file 
    1414                 :            :  * can be truncated and where the footer is written for VHDs.
    1415                 :            :  */
    1416                 :            : int
    1417                 :          0 : vhd_test_file_fixed(const char *file, int *is_block)
    1418                 :            : {
    1419                 :            :         int err;
    1420                 :            :         struct stat stats;
    1421                 :            : 
    1422                 :          0 :         err = stat(file, &stats);
    1423         [ #  # ]:          0 :         if (err == -1)
    1424                 :          0 :                 return -errno;
    1425                 :            : 
    1426                 :          0 :         *is_block = !!(S_ISBLK(stats.st_mode));
    1427                 :          0 :         return err;
    1428                 :            : }
    1429                 :            : 
    1430                 :            : int
    1431                 :          0 : vhd_find_parent(vhd_context_t *ctx, const char *parent, char **_location)
    1432                 :            : {
    1433                 :            :         char *location, __location[PATH_MAX];
    1434                 :            :         char *cpath, __cpath[PATH_MAX];
    1435                 :            :         char *cdir, *path;
    1436                 :            :         int err;
    1437                 :            : 
    1438                 :          0 :         err        = 0;
    1439                 :          0 :         path       = NULL;
    1440                 :          0 :         cpath      = NULL;
    1441                 :          0 :         location   = NULL;
    1442                 :          0 :         *_location = NULL;
    1443                 :            : 
    1444         [ #  # ]:          0 :         if (!parent)
    1445                 :          0 :                 return -EINVAL;
    1446                 :            : 
    1447         [ #  # ]:          0 :         if (parent[0] == '/') {
    1448         [ #  # ]:          0 :                 if (!access(parent, R_OK)) {
    1449                 :          0 :                         *_location = strdup(parent);
    1450         [ #  # ]:          0 :                         if (!*_location)
    1451                 :          0 :                                 return -errno;
    1452                 :            :                         return 0;
    1453                 :            :                 }
    1454                 :            :         }
    1455                 :            : 
    1456                 :            :         /* check parent path relative to child's directory */
    1457                 :          0 :         cpath = canonpath(ctx->file, __cpath, sizeof(__cpath));
    1458         [ #  # ]:          0 :         if (!cpath) {
    1459                 :          0 :                 err = -errno;
    1460                 :          0 :                 goto out;
    1461                 :            :         }
    1462                 :            : 
    1463                 :          0 :         cdir = dirname(cpath);
    1464         [ #  # ]:          0 :         if (asprintf(&location, "%s/%s", cdir, parent) == -1) {
    1465                 :          0 :                 err = -errno;
    1466                 :          0 :                 location = NULL;
    1467                 :          0 :                 goto out;
    1468                 :            :         }
    1469                 :            : 
    1470         [ #  # ]:          0 :         if (!access(location, R_OK)) {
    1471                 :          0 :                 path = canonpath(location, __location, sizeof(__location));
    1472         [ #  # ]:          0 :                 if (path) {
    1473                 :          0 :                         *_location = strdup(path);
    1474         [ #  # ]:          0 :                         if (*_location)
    1475                 :            :                                 goto out;
    1476                 :            :                 }
    1477                 :            :         }
    1478                 :          0 :         err = -errno;
    1479                 :            : 
    1480                 :            : out:
    1481                 :          0 :         free(location);
    1482                 :          0 :         return err;
    1483                 :            : }
    1484                 :            : 
    1485                 :            : int 
    1486                 :          0 : vhd_macx_encode_location(char *name, char **out, int *outlen)
    1487                 :            : {
    1488                 :            :         iconv_t cd;
    1489                 :            :         int len, err;
    1490                 :            :         size_t ibl, obl;
    1491                 :            :         char *uri, *urip, *uri_utf8, *uri_utf8p;
    1492                 :            : 
    1493                 :          0 :         err     = 0;
    1494                 :          0 :         *out    = NULL;
    1495                 :          0 :         *outlen = 0;
    1496                 :          0 :         len     = strlen(name) + strlen("file://");
    1497                 :            : 
    1498                 :          0 :         ibl     = len;
    1499                 :          0 :         obl     = len;
    1500                 :            : 
    1501                 :          0 :         uri = urip = malloc(ibl + 1);
    1502                 :          0 :         uri_utf8 = uri_utf8p = malloc(obl);
    1503                 :            : 
    1504         [ #  # ]:          0 :         if (!uri || !uri_utf8) {
    1505                 :          0 :                 free(uri);
    1506                 :          0 :                 free(uri_utf8);
    1507                 :          0 :                 return -ENOMEM;
    1508                 :            :         }
    1509                 :            : 
    1510                 :          0 :         cd = iconv_open("UTF-8", "ASCII");
    1511         [ #  # ]:          0 :         if (cd == (iconv_t)-1) {
    1512                 :          0 :                 free(uri_utf8);
    1513                 :          0 :                 err = -errno;
    1514                 :          0 :                 goto out;
    1515                 :            :         }
    1516                 :            : 
    1517                 :          0 :         snprintf(uri, ibl+1, "file://%s", name);
    1518                 :            : 
    1519 [ #  # ][ #  # ]:          0 :         if (iconv(cd, &urip, &ibl, &uri_utf8p, &obl) == (size_t)-1 ||
    1520         [ #  # ]:          0 :             ibl || obl) {
    1521                 :          0 :                 free(uri_utf8);
    1522         [ #  # ]:          0 :                 err = (errno ? -errno : -EIO);
    1523                 :          0 :                 goto out;
    1524                 :            :         }
    1525                 :            : 
    1526                 :          0 :         *outlen = len;
    1527                 :          0 :         *out    = uri_utf8;
    1528                 :            : 
    1529                 :            :  out:
    1530                 :          0 :         free(uri);
    1531         [ #  # ]:          0 :         if (cd != (iconv_t)-1)
    1532                 :          0 :                 iconv_close(cd);
    1533                 :            : 
    1534                 :          0 :         return err;
    1535                 :            : }
    1536                 :            : 
    1537                 :            : int
    1538                 :          0 : vhd_w2u_encode_location(char *name, char **out, int *outlen)
    1539                 :            : {
    1540                 :            :         iconv_t cd;
    1541                 :            :         int len, err;
    1542                 :            :         size_t ibl, obl;
    1543                 :            :         char *uri, *urip, *uri_utf16, *uri_utf16p, *tmp;
    1544                 :            : 
    1545                 :          0 :         err     = 0;
    1546                 :          0 :         *out    = NULL;
    1547                 :          0 :         *outlen = 0;
    1548                 :          0 :         cd      = (iconv_t) -1;
    1549                 :            : 
    1550                 :            :         /* 
    1551                 :            :          * MICROSOFT_COMPAT
    1552                 :            :          * relative paths must start with ".\" 
    1553                 :            :          */
    1554         [ #  # ]:          0 :         if (name[0] != '/') {
    1555                 :          0 :                 tmp = strstr(name, "./");
    1556         [ #  # ]:          0 :                 if (tmp == name)
    1557                 :          0 :                         tmp += strlen("./");
    1558                 :            :                 else
    1559                 :            :                         tmp = name;
    1560                 :            : 
    1561                 :          0 :                 err = asprintf(&uri, ".\\%s", tmp);
    1562                 :            :         } else
    1563                 :          0 :                 err = asprintf(&uri, "%s", name);
    1564                 :            : 
    1565         [ #  # ]:          0 :         if (err == -1)
    1566                 :          0 :                 return -ENOMEM;
    1567                 :            : 
    1568                 :          0 :         tmp = uri;
    1569         [ #  # ]:          0 :         while (*tmp != '\0') {
    1570         [ #  # ]:          0 :                 if (*tmp == '/')
    1571                 :          0 :                         *tmp = '\\';
    1572                 :          0 :                 tmp++;
    1573                 :            :         }
    1574                 :            : 
    1575                 :          0 :         len  = strlen(uri);
    1576                 :          0 :         ibl  = len;
    1577                 :          0 :         obl  = len * 2;
    1578                 :          0 :         urip = uri;
    1579                 :            : 
    1580                 :          0 :         uri_utf16 = uri_utf16p = malloc(obl);
    1581         [ #  # ]:          0 :         if (!uri_utf16) {
    1582                 :            :                 err = -ENOMEM;
    1583                 :            :                 goto out;
    1584                 :            :         }
    1585                 :            : 
    1586                 :            :         /* 
    1587                 :            :          * MICROSOFT_COMPAT
    1588                 :            :          * little endian unicode here 
    1589                 :            :          */
    1590                 :          0 :         cd = iconv_open("UTF-16LE", "ASCII");
    1591         [ #  # ]:          0 :         if (cd == (iconv_t)-1) {
    1592                 :          0 :                 free(uri_utf16);
    1593                 :          0 :                 err = -errno;
    1594                 :          0 :                 goto out;
    1595                 :            :         }
    1596                 :            : 
    1597 [ #  # ][ #  # ]:          0 :         if (iconv(cd, &urip, &ibl, &uri_utf16p, &obl) == (size_t)-1 ||
    1598         [ #  # ]:          0 :             ibl || obl) {
    1599                 :          0 :                 free(uri_utf16);
    1600         [ #  # ]:          0 :                 err = (errno ? -errno : -EIO);
    1601                 :          0 :                 goto out;
    1602                 :            :         }
    1603                 :            : 
    1604                 :          0 :         len = len * 2;
    1605                 :          0 :         *outlen = len;
    1606                 :          0 :         *out    = uri_utf16;
    1607                 :          0 :         err     = 0;
    1608                 :            : 
    1609                 :            :  out:
    1610                 :          0 :         free(uri);
    1611         [ #  # ]:          0 :         if (cd != (iconv_t)-1)
    1612                 :          0 :                 iconv_close(cd);
    1613                 :            : 
    1614                 :          0 :         return err;
    1615                 :            : }
    1616                 :            : 
    1617                 :            : static char *
    1618                 :          0 : vhd_macx_decode_location(char *in, char *out, int len)
    1619                 :            : {
    1620                 :            :         iconv_t cd;
    1621                 :            :         char *name;
    1622                 :            :         size_t ibl, obl;
    1623                 :            : 
    1624                 :          0 :         name = out;
    1625                 :          0 :         ibl  = obl = len;
    1626                 :            :         
    1627         [ #  # ]:          0 :         if ( (cd = iconv_open("ASCII", "UTF-8")) == (iconv_t)-1 ) {
    1628                 :          0 :                 return NULL;
    1629                 :            :         }
    1630                 :            : 
    1631 [ #  # ][ #  # ]:          0 :         if (iconv(cd, &in, &ibl, &out, &obl) == (size_t)-1 || ibl) {
    1632                 :          0 :                 iconv_close(cd);
    1633                 :            :                 return NULL;
    1634                 :            :         }
    1635                 :            : 
    1636                 :          0 :         iconv_close(cd);
    1637                 :          0 :         *out = '\0';
    1638                 :            : 
    1639         [ #  # ]:          0 :         if (strstr(name, "file://") != name)
    1640                 :            :                 return NULL;
    1641                 :            : 
    1642                 :          0 :         name += strlen("file://");
    1643                 :            : 
    1644                 :          0 :         return strdup(name);
    1645                 :            : }
    1646                 :            : 
    1647                 :            : static char *
    1648                 :          0 : vhd_w2u_decode_location(char *in, char *out, int len, char *utf_type)
    1649                 :            : {
    1650                 :            :         iconv_t cd;
    1651                 :            :         char *name, *tmp;
    1652                 :            :         size_t ibl, obl;
    1653                 :            : 
    1654                 :          0 :         tmp = name = out;
    1655                 :          0 :         ibl = obl  = len;
    1656                 :            : 
    1657                 :          0 :         cd = iconv_open("ASCII", utf_type);
    1658         [ #  # ]:          0 :         if (cd == (iconv_t)-1) 
    1659                 :          0 :                 return NULL;
    1660                 :            : 
    1661 [ #  # ][ #  # ]:          0 :         if (iconv(cd, &in, &ibl, &out, &obl) == (size_t)-1 || ibl) {
    1662                 :          0 :                 iconv_close(cd);
    1663                 :            :                 return NULL;
    1664                 :            :         }
    1665                 :            : 
    1666                 :          0 :         iconv_close(cd);
    1667                 :          0 :         *out = '\0';
    1668                 :            : 
    1669                 :            :         /* TODO: spaces */
    1670         [ #  # ]:          0 :         while (tmp != out) {
    1671         [ #  # ]:          0 :                 if (*tmp == '\\')
    1672                 :          0 :                         *tmp = '/';
    1673                 :          0 :                 tmp++;
    1674                 :            :         }
    1675                 :            : 
    1676 [ #  # ][ #  # ]:          0 :         if (strstr(name, "C:") == name || strstr(name, "c:") == name)
    1677                 :          0 :                 name += strlen("c:");
    1678                 :            : 
    1679                 :          0 :         return strdup(name);
    1680                 :            : }
    1681                 :            : 
    1682                 :            : int
    1683                 :          0 : vhd_header_decode_parent(vhd_context_t *ctx, vhd_header_t *header, char **buf)
    1684                 :            : {
    1685                 :            :         char *code, out[512];
    1686                 :            : 
    1687 [ #  # ][ #  # ]:          0 :         if (vhd_creator_tapdisk(ctx) &&
    1688                 :          0 :             ctx->footer.crtr_ver == VHD_VERSION(0, 1))
    1689                 :            :                 code = UTF_16;
    1690                 :            :         else
    1691                 :          0 :                 code = UTF_16BE;
    1692                 :            : 
    1693                 :          0 :         *buf = vhd_w2u_decode_location(header->prt_name, out, 512, code);
    1694         [ #  # ]:          0 :         return (*buf == NULL ? -EINVAL : 0);
    1695                 :            : }
    1696                 :            : 
    1697                 :            : int
    1698                 :          0 : vhd_parent_locator_read(vhd_context_t *ctx,
    1699                 :          0 :                         vhd_parent_locator_t *loc, char **parent)
    1700                 :            : {
    1701                 :            :         int err, size;
    1702                 :            :         void *raw, *out, *name;
    1703                 :            : 
    1704                 :          0 :         raw     = NULL;
    1705                 :          0 :         out     = NULL;
    1706                 :          0 :         name    = NULL;
    1707                 :          0 :         *parent = NULL;
    1708                 :            : 
    1709         [ #  # ]:          0 :         if (ctx->footer.type != HD_TYPE_DIFF) {
    1710                 :            :                 err = -EINVAL;
    1711                 :            :                 goto out;
    1712                 :            :         }
    1713                 :            : 
    1714         [ #  # ]:          0 :         switch (loc->code) {
    1715                 :            :         case PLAT_CODE_MACX:
    1716                 :            :         case PLAT_CODE_W2KU:
    1717                 :            :         case PLAT_CODE_W2RU:
    1718                 :            :                 break;
    1719                 :            :         default:
    1720                 :            :                 err = -EINVAL;
    1721                 :            :                 goto out;
    1722                 :            :         }
    1723                 :            : 
    1724                 :          0 :         err = vhd_seek(ctx, loc->data_offset, SEEK_SET);
    1725         [ #  # ]:          0 :         if (err)
    1726                 :            :                 goto out;
    1727                 :            : 
    1728                 :          0 :         size = vhd_parent_locator_size(loc);
    1729         [ #  # ]:          0 :         if (size <= 0) {
    1730                 :            :                 err = -EINVAL;
    1731                 :            :                 goto out;
    1732                 :            :         }
    1733                 :            : 
    1734                 :          0 :         err = posix_memalign(&raw, VHD_SECTOR_SIZE, size);
    1735         [ #  # ]:          0 :         if (err) {
    1736                 :          0 :                 raw = NULL;
    1737                 :          0 :                 err = -err;
    1738                 :          0 :                 goto out;
    1739                 :            :         }
    1740                 :            : 
    1741                 :          0 :         err = vhd_read(ctx, raw, size);
    1742         [ #  # ]:          0 :         if (err)
    1743                 :            :                 goto out;
    1744                 :            : 
    1745                 :          0 :         out = malloc(loc->data_len + 1);
    1746         [ #  # ]:          0 :         if (!out) {
    1747                 :            :                 err = -ENOMEM;
    1748                 :            :                 goto out;
    1749                 :            :         }
    1750                 :            : 
    1751      [ #  #  # ]:          0 :         switch (loc->code) {
    1752                 :            :         case PLAT_CODE_MACX:
    1753                 :          0 :                 name = vhd_macx_decode_location(raw, out, loc->data_len);
    1754                 :            :                 break;
    1755                 :            :         case PLAT_CODE_W2KU:
    1756                 :            :         case PLAT_CODE_W2RU:
    1757                 :          0 :                 name = vhd_w2u_decode_location(raw, out,
    1758                 :            :                                                loc->data_len, UTF_16LE);
    1759                 :            :                 break;
    1760                 :            :         }
    1761                 :            : 
    1762         [ #  # ]:          0 :         if (!name) {
    1763                 :            :                 err = -EINVAL;
    1764                 :            :                 goto out;
    1765                 :            :         }
    1766                 :            : 
    1767                 :          0 :         err     = 0;
    1768                 :          0 :         *parent = name;
    1769                 :            : 
    1770                 :            : out:
    1771                 :          0 :         free(raw);
    1772                 :          0 :         free(out);
    1773                 :            : 
    1774         [ #  # ]:          0 :         if (err) {
    1775         [ #  # ]:          0 :                 VHDLOG("%s: error reading parent locator: %d\n",
    1776                 :            :                        ctx->file, err);
    1777         [ #  # ]:          0 :                 VHDLOG("%s: locator: code %u, space 0x%x, len 0x%x, "
    1778                 :            :                        "off 0x%"PRIx64"\n", ctx->file, loc->code, loc->data_space,
    1779                 :            :                        loc->data_len, loc->data_offset);
    1780                 :            :         }
    1781                 :            : 
    1782                 :          0 :         return err;
    1783                 :            : }
    1784                 :            : 
    1785                 :            : static int
    1786                 :          0 : vhd_parent_locator_get_impl(vhd_context_t *ctx, char **parent, bool resolve_parent)
    1787                 :            : {
    1788                 :            :         int i, n, err;
    1789                 :            :         char *name, *location;
    1790                 :            :         vhd_parent_locator_t *loc;
    1791                 :            : 
    1792                 :          0 :         err     = -EINVAL;
    1793                 :          0 :         *parent = NULL;
    1794                 :            : 
    1795         [ #  # ]:          0 :         if (ctx->footer.type != HD_TYPE_DIFF)
    1796                 :          0 :                 return -EINVAL;
    1797                 :            : 
    1798         [ #  # ]:          0 :         if (ctx->custom_parent)
    1799                 :          0 :                 return vhd_find_parent(ctx, ctx->custom_parent, parent);
    1800                 :            : 
    1801                 :          0 :         n = vhd_parent_locator_count(ctx);
    1802         [ #  # ]:          0 :         for (i = 0; i < n; i++) {
    1803                 :            :                 int _err;
    1804                 :            : 
    1805                 :          0 :                 loc = ctx->header.loc + i;
    1806                 :          0 :                 _err = vhd_parent_locator_read(ctx, loc, &name);
    1807         [ #  # ]:          0 :                 if (_err)
    1808                 :          0 :                         continue;
    1809                 :            : 
    1810         [ #  # ]:          0 :                 if (!resolve_parent) {
    1811                 :          0 :                         *parent = name;
    1812                 :          0 :                         return 0;
    1813                 :            :                 }
    1814                 :            : 
    1815                 :          0 :                 err = vhd_find_parent(ctx, name, &location);
    1816         [ #  # ]:          0 :                 if (err)
    1817         [ #  # ]:          0 :                         VHDLOG("%s: couldn't find parent %s (%d)\n",
    1818                 :            :                                ctx->file, name, err);
    1819                 :          0 :                 free(name);
    1820                 :            : 
    1821         [ #  # ]:          0 :                 if (!err) {
    1822                 :          0 :                         *parent = location;
    1823                 :          0 :                         return 0;
    1824                 :            :                 }
    1825                 :            :         }
    1826                 :            : 
    1827                 :            :         return err;
    1828                 :            : }
    1829                 :            : 
    1830                 :            : int
    1831                 :          0 : vhd_parent_locator_get(vhd_context_t *ctx, char **parent)
    1832                 :            : {
    1833                 :          0 :         return vhd_parent_locator_get_impl(ctx, parent, true);
    1834                 :            : }
    1835                 :            : 
    1836                 :            : int
    1837                 :          0 : vhd_parent_locator_unresolved_get(vhd_context_t *ctx, char **parent)
    1838                 :            : {
    1839                 :          0 :         return vhd_parent_locator_get_impl(ctx, parent, false);
    1840                 :            : }
    1841                 :            : 
    1842                 :            : /**
    1843                 :            :  * Overrides the parent with the supplied one.
    1844                 :            :  *
    1845                 :            :  * XXX Writing the header/footer after calling this function may lead to
    1846                 :            :  * undefined results.
    1847                 :            :  */
    1848                 :            : int
    1849                 :          0 : vhd_custom_parent_set(vhd_context_t *ctx, const char *parent) {
    1850         [ #  # ]:          0 :         ASSERT(ctx);
    1851         [ #  # ]:          0 :         ASSERT(parent);
    1852                 :          0 :         free(ctx->custom_parent);
    1853                 :          0 :         ctx->custom_parent = strdup(parent);
    1854         [ #  # ]:          0 :         if (!ctx->custom_parent)
    1855                 :            :                 return -ENOMEM;
    1856                 :          0 :         return 0;
    1857                 :            : }
    1858                 :            : 
    1859                 :            : int
    1860                 :          0 : vhd_parent_locator_write_at(vhd_context_t *ctx,
    1861                 :            :                             const char *parent, off64_t off, uint32_t code,
    1862                 :            :                             size_t max_bytes, vhd_parent_locator_t *loc)
    1863                 :            : {
    1864                 :            :         struct stat stats;
    1865                 :            :         int err, len, size;
    1866                 :            :         char *absolute_path, *relative_path, *encoded;
    1867                 :            :         char __parent[PATH_MAX];
    1868                 :            :         void *block;
    1869                 :            :         int (*encode_location_fn)(char*, char**, int*);
    1870                 :            : 
    1871                 :            :         memset(loc, 0, sizeof(vhd_parent_locator_t));
    1872                 :            : 
    1873         [ #  # ]:          0 :         if (ctx->footer.type != HD_TYPE_DIFF)
    1874                 :          0 :                 return -EINVAL;
    1875                 :            : 
    1876                 :          0 :         absolute_path = NULL;
    1877                 :          0 :         relative_path = NULL;
    1878                 :          0 :         encoded       = NULL;
    1879                 :          0 :         block         = NULL;
    1880                 :          0 :         size          = 0;
    1881                 :          0 :         len           = 0;
    1882                 :            : 
    1883      [ #  #  # ]:          0 :         switch (code) {
    1884                 :            :         case PLAT_CODE_MACX:
    1885                 :            :                 encode_location_fn = &vhd_macx_encode_location;
    1886                 :            :                 break;
    1887                 :            :         case PLAT_CODE_W2KU:
    1888                 :            :         case PLAT_CODE_W2RU:
    1889                 :          0 :                 encode_location_fn = &vhd_w2u_encode_location;
    1890                 :          0 :                 break;
    1891                 :            :         default:
    1892                 :            :                 return -EINVAL;
    1893                 :            :         }
    1894                 :            : 
    1895                 :          0 :         absolute_path = canonpath(parent, __parent, sizeof(__parent));
    1896         [ #  # ]:          0 :         if (!absolute_path) {
    1897                 :          0 :                 err = -errno;
    1898                 :          0 :                 goto out;
    1899                 :            :         }
    1900                 :            : 
    1901                 :          0 :         err = stat(absolute_path, &stats);
    1902         [ #  # ]:          0 :         if (err) {
    1903                 :          0 :                 err = -errno;
    1904                 :          0 :                 goto out;
    1905                 :            :         }
    1906                 :            : 
    1907         [ #  # ]:          0 :         if (!S_ISREG(stats.st_mode) && !S_ISBLK(stats.st_mode)) {
    1908                 :          0 :                 err = -EINVAL;
    1909                 :          0 :                 goto out;
    1910                 :            :         }
    1911                 :            : 
    1912                 :          0 :         relative_path = relative_path_to(ctx->file, absolute_path, &err);
    1913 [ #  # ][ #  # ]:          0 :         if (!relative_path || err) {
    1914         [ #  # ]:          0 :                 err = (err ? err : -EINVAL);
    1915                 :          0 :                 goto out;
    1916                 :            :         }
    1917                 :            : 
    1918                 :          0 :         err = encode_location_fn(relative_path, &encoded, &len);
    1919                 :            : 
    1920         [ #  # ]:          0 :         if (err)
    1921                 :            :                 goto out;
    1922                 :            : 
    1923                 :          0 :         err = vhd_seek(ctx, off, SEEK_SET);
    1924         [ #  # ]:          0 :         if (err)
    1925                 :            :                 goto out;
    1926                 :            : 
    1927                 :          0 :         size = vhd_bytes_padded(len);
    1928                 :            : 
    1929 [ #  # ][ #  # ]:          0 :         if (max_bytes && size > max_bytes) {
    1930                 :          0 :                 err = -ENAMETOOLONG;
    1931                 :          0 :                 goto out;
    1932                 :            :         }
    1933                 :            : 
    1934                 :          0 :         err  = posix_memalign(&block, VHD_SECTOR_SIZE, size);
    1935         [ #  # ]:          0 :         if (err) {
    1936                 :          0 :                 block = NULL;
    1937                 :          0 :                 err   = -err;
    1938                 :          0 :                 goto out;
    1939                 :            :         }
    1940                 :            : 
    1941                 :          0 :         memset(block, 0, size);
    1942                 :          0 :         memcpy(block, encoded, len);
    1943                 :            : 
    1944                 :          0 :         err = vhd_write(ctx, block, size);
    1945         [ #  # ]:          0 :         if (err)
    1946                 :            :                 goto out;
    1947                 :            : 
    1948                 :          0 :         err = 0;
    1949                 :            : 
    1950                 :            : out:
    1951                 :          0 :         free(relative_path);
    1952                 :          0 :         free(encoded);
    1953                 :          0 :         free(block);
    1954                 :            : 
    1955         [ #  # ]:          0 :         if (!err) {
    1956                 :          0 :                 loc->res         = 0;
    1957                 :          0 :                 loc->code        = code;
    1958                 :          0 :                 loc->data_len    = len;
    1959                 :            :                 /*
    1960                 :            :                  * write number of bytes ('size') instead of number of sectors
    1961                 :            :                  * into loc->data_space to be compatible with MSFT, even though
    1962                 :            :                  * this goes against the specs
    1963                 :            :                  */
    1964                 :          0 :                 loc->data_space  = size; 
    1965                 :          0 :                 loc->data_offset = off;
    1966                 :            :         }
    1967                 :            : 
    1968                 :          0 :         return err;
    1969                 :            : }
    1970                 :            : 
    1971                 :            : static int
    1972                 :          0 : vhd_footer_offset_at_eof(vhd_context_t *ctx, off64_t *off)
    1973                 :            : {
    1974                 :            :         int err;
    1975 [ #  # ][ #  # ]:          0 :         if ((err = vhd_seek(ctx, 0, SEEK_END)))
    1976                 :            :                 return err;
    1977                 :          0 :         *off = vhd_position(ctx) - sizeof(vhd_footer_t);
    1978                 :          0 :         return 0;
    1979                 :            : }
    1980                 :            : 
    1981                 :            : int
    1982                 :          0 : vhd_read_bitmap(vhd_context_t *ctx, uint32_t block, char **bufp)
    1983                 :            : {
    1984                 :            :         int err;
    1985                 :            :         void *buf;
    1986                 :            :         size_t size;
    1987                 :            :         off64_t off;
    1988                 :            :         uint64_t blk;
    1989                 :            : 
    1990                 :          0 :         buf   = NULL;
    1991                 :          0 :         *bufp = NULL;
    1992                 :            : 
    1993         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx))
    1994                 :          0 :                 return -EINVAL;
    1995                 :            : 
    1996                 :          0 :         err = vhd_get_bat(ctx);
    1997         [ #  # ]:          0 :         if (err)
    1998                 :            :                 return err;
    1999                 :            : 
    2000         [ #  # ]:          0 :         if (block >= ctx->bat.entries)
    2001                 :            :                 return -ERANGE;
    2002                 :            : 
    2003                 :          0 :         blk  = ctx->bat.bat[block];
    2004         [ #  # ]:          0 :         if (blk == DD_BLK_UNUSED)
    2005                 :            :                 return -EINVAL;
    2006                 :            : 
    2007                 :          0 :         off  = vhd_sectors_to_bytes(blk);
    2008                 :          0 :         size = vhd_bytes_padded(ctx->spb >> 3);
    2009                 :            : 
    2010                 :          0 :         err  = vhd_seek(ctx, off, SEEK_SET);
    2011         [ #  # ]:          0 :         if (err)
    2012                 :            :                 return err;
    2013                 :            : 
    2014                 :          0 :         err  = posix_memalign(&buf, VHD_SECTOR_SIZE, size);
    2015         [ #  # ]:          0 :         if (err)
    2016                 :          0 :                 return -err;
    2017                 :            : 
    2018                 :          0 :         err  = vhd_read(ctx, buf, size);
    2019         [ #  # ]:          0 :         if (err)
    2020                 :            :                 goto fail;
    2021                 :            : 
    2022                 :          0 :         *bufp = buf;
    2023                 :          0 :         return 0;
    2024                 :            : 
    2025                 :            : fail:
    2026                 :          0 :         free(buf);
    2027                 :          0 :         return err;
    2028                 :            : }
    2029                 :            : 
    2030                 :            : int
    2031                 :          0 : vhd_read_at(vhd_context_t *ctx, uint64_t block, uint32_t from, size_t size, char *buf)
    2032                 :            : {
    2033                 :            :         /*
    2034                 :            :          * Sub call function, assumes that dynamic disk has been checked
    2035                 :            :          */
    2036                 :            :         int err;
    2037                 :            :         uint64_t blk;
    2038                 :            :         off64_t end, off;
    2039                 :            : 
    2040                 :          0 :         err = vhd_get_bat(ctx);
    2041         [ #  # ]:          0 :         if (err)
    2042                 :          0 :                 return err;
    2043                 :            : 
    2044         [ #  # ]:          0 :         if (block >= ctx->bat.entries)
    2045                 :            :                 return -ERANGE;
    2046                 :            : 
    2047                 :          0 :         blk  = ctx->bat.bat[block];
    2048         [ #  # ]:          0 :         if (blk == DD_BLK_UNUSED)
    2049                 :            :                 return -EINVAL;
    2050                 :            : 
    2051                 :          0 :         off  = vhd_sectors_to_bytes(blk + ctx->bm_secs);
    2052                 :            : 
    2053                 :          0 :         err  = vhd_footer_offset_at_eof(ctx, &end);
    2054 [ #  # ][ #  # ]:          0 :         if (err)
    2055                 :            :                 return err;
    2056                 :            : 
    2057         [ #  # ]:          0 :         if (end < off + ctx->header.block_size) {
    2058                 :          0 :                 size = end - off;
    2059                 :          0 :                 memset(buf + size, 0, ctx->header.block_size - size);
    2060                 :            :         }
    2061                 :            : 
    2062                 :          0 :         off = off + vhd_sectors_to_bytes(from);
    2063                 :            : 
    2064                 :          0 :         err  = vhd_seek(ctx, off, SEEK_SET);
    2065         [ #  # ]:          0 :         if (err)
    2066                 :            :                 return err;
    2067                 :            : 
    2068                 :          0 :         err  = vhd_read(ctx, buf, size);
    2069         [ #  # ]:          0 :         if (err)
    2070                 :          0 :                 return err;
    2071                 :            : 
    2072                 :            :         return 0;
    2073                 :            : }
    2074                 :            : 
    2075                 :            : int
    2076                 :          0 : vhd_read_block(vhd_context_t *ctx, uint32_t block, char **bufp)
    2077                 :            : {
    2078                 :            :         int err;
    2079                 :            :         void *buf;
    2080                 :            :         size_t size;
    2081                 :            : 
    2082                 :          0 :         buf   = NULL;
    2083                 :          0 :         *bufp = NULL;
    2084                 :            : 
    2085         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx))
    2086                 :          0 :                 return -EINVAL;
    2087                 :            : 
    2088                 :          0 :         size = vhd_sectors_to_bytes(ctx->spb);
    2089                 :          0 :         err  = posix_memalign(&buf, VHD_SECTOR_SIZE, size);
    2090         [ #  # ]:          0 :         if (err) {
    2091                 :          0 :                 err = -err;
    2092                 :          0 :                 goto fail;
    2093                 :            :         }
    2094                 :            : 
    2095                 :          0 :         err = vhd_read_at(ctx, block, 0, size, buf);
    2096         [ #  # ]:          0 :         if (err)
    2097                 :            :                 goto fail;
    2098                 :            : 
    2099                 :          0 :         *bufp = buf;
    2100                 :          0 :         return 0;
    2101                 :            : fail:
    2102                 :          0 :         free(buf);
    2103                 :          0 :         return err;
    2104                 :            : }
    2105                 :            : 
    2106                 :            : int
    2107                 :          0 : vhd_write_footer_at(vhd_context_t *ctx, vhd_footer_t *footer, off64_t off)
    2108                 :            : {
    2109                 :            :         int err;
    2110                 :            :         void *buf;
    2111                 :            :         vhd_footer_t *f;
    2112                 :            : 
    2113                 :          0 :         f = NULL;
    2114                 :            : 
    2115                 :          0 :         err = posix_memalign(&buf, VHD_SECTOR_SIZE, sizeof(vhd_footer_t));
    2116         [ #  # ]:          0 :         if (err) {
    2117                 :          0 :                 err = -err;
    2118                 :          0 :                 goto out;
    2119                 :            :         }
    2120                 :          0 :         f = buf;
    2121                 :            : 
    2122                 :            :         memcpy(f, footer, sizeof(vhd_footer_t));
    2123                 :          0 :         f->checksum = vhd_checksum_footer(f);
    2124                 :            : 
    2125                 :          0 :         err = vhd_validate_footer(f);
    2126         [ #  # ]:          0 :         if (err)
    2127                 :            :                 goto out;
    2128                 :            : 
    2129                 :          0 :         err = vhd_seek(ctx, off, SEEK_SET);
    2130         [ #  # ]:          0 :         if (err)
    2131                 :            :                 goto out;
    2132                 :            : 
    2133                 :          0 :         vhd_footer_out(f);
    2134                 :            : 
    2135                 :          0 :         err = vhd_write(ctx, f, sizeof(vhd_footer_t));
    2136                 :            : 
    2137                 :            : out:
    2138         [ #  # ]:          0 :         if (err)
    2139         [ #  # ]:          0 :                 VHDLOG("%s: failed writing footer at 0x%08"PRIx64": %d\n",
    2140                 :            :                        ctx->file, off, err);
    2141                 :          0 :         free(f);
    2142                 :          0 :         return err;
    2143                 :            : }
    2144                 :            : 
    2145                 :            : int
    2146                 :          0 : vhd_write_footer(vhd_context_t *ctx, vhd_footer_t *footer)
    2147                 :            : {
    2148                 :            :         int err;
    2149                 :            :         off64_t off;
    2150                 :            : 
    2151         [ #  # ]:          0 :         if (ctx->is_block)
    2152                 :          0 :                 err = vhd_footer_offset_at_eof(ctx, &off);
    2153                 :            :         else
    2154                 :          0 :                 err = vhd_end_of_data(ctx, &off);
    2155         [ #  # ]:          0 :         if (err)
    2156                 :          0 :                 return err;
    2157                 :            : 
    2158                 :          0 :         err = vhd_write_footer_at(ctx, footer, off);
    2159         [ #  # ]:          0 :         if (err)
    2160                 :            :                 return err;
    2161                 :            : 
    2162         [ #  # ]:          0 :         if (!ctx->is_block) {
    2163                 :          0 :                 err = ftruncate(ctx->fd, off + sizeof(vhd_footer_t));
    2164         [ #  # ]:          0 :                 if (err)
    2165                 :          0 :                         return -errno;
    2166                 :            :         }
    2167                 :            : 
    2168         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx))
    2169                 :            :                 return 0;
    2170                 :            : 
    2171                 :          0 :         return vhd_write_footer_at(ctx, footer, 0);
    2172                 :            : }
    2173                 :            : 
    2174                 :            : int
    2175                 :          0 : vhd_write_header_at(vhd_context_t *ctx, vhd_header_t *header, off64_t off)
    2176                 :            : {
    2177                 :            :         int err;
    2178                 :            :         vhd_header_t *h;
    2179                 :            :         void *buf;
    2180                 :            : 
    2181                 :          0 :         h = NULL;
    2182                 :            : 
    2183         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx)) {
    2184                 :            :                 err = -EINVAL;
    2185                 :            :                 goto out;
    2186                 :            :         }
    2187                 :            : 
    2188                 :          0 :         err = posix_memalign(&buf, VHD_SECTOR_SIZE, sizeof(vhd_header_t));
    2189         [ #  # ]:          0 :         if (err) {
    2190                 :          0 :                 err = -err;
    2191                 :          0 :                 goto out;
    2192                 :            :         }
    2193                 :          0 :         h = buf;
    2194                 :            : 
    2195                 :            :         memcpy(h, header, sizeof(vhd_header_t));
    2196                 :            : 
    2197                 :          0 :         h->checksum = vhd_checksum_header(h);
    2198                 :          0 :         err = vhd_validate_header(h);
    2199         [ #  # ]:          0 :         if (err)
    2200                 :            :                 goto out;
    2201                 :            : 
    2202                 :          0 :         vhd_header_out(h);
    2203                 :            : 
    2204                 :          0 :         err = vhd_seek(ctx, off, SEEK_SET);
    2205         [ #  # ]:          0 :         if (err)
    2206                 :            :                 goto out;
    2207                 :            : 
    2208                 :          0 :         err = vhd_write(ctx, h, sizeof(vhd_header_t));
    2209                 :            : 
    2210                 :            : out:
    2211         [ #  # ]:          0 :         if (err)
    2212         [ #  # ]:          0 :                 VHDLOG("%s: failed writing header at 0x%08"PRIx64": %d\n",
    2213                 :            :                        ctx->file, off, err);
    2214                 :          0 :         free(h);
    2215                 :          0 :         return err;
    2216                 :            : }
    2217                 :            : 
    2218                 :            : int
    2219                 :          0 : vhd_write_header(vhd_context_t *ctx, vhd_header_t *header)
    2220                 :            : {
    2221                 :            :         off64_t off;
    2222                 :            : 
    2223 [ #  # ][ #  # ]:          0 :         if (!vhd_type_dynamic(ctx))
    2224                 :            :                 return -EINVAL;
    2225                 :            : 
    2226                 :          0 :         off = ctx->footer.data_offset;
    2227                 :          0 :         return vhd_write_header_at(ctx, header, off);
    2228                 :            : }
    2229                 :            : 
    2230                 :            : int
    2231                 :          0 : vhd_write_bat(vhd_context_t *ctx, vhd_bat_t *bat)
    2232                 :            : {
    2233                 :            :         int err;
    2234                 :            :         off64_t off;
    2235                 :            :         vhd_bat_t b;
    2236                 :            :         void *buf;
    2237                 :            :         size_t size;
    2238                 :            : 
    2239         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx))
    2240                 :          0 :                 return -EINVAL;
    2241                 :            : 
    2242                 :          0 :         err = vhd_validate_bat(&ctx->bat);
    2243         [ #  # ]:          0 :         if (err)
    2244                 :            :                 return err;
    2245                 :            : 
    2246                 :          0 :         err = vhd_validate_bat(bat);
    2247         [ #  # ]:          0 :         if (err)
    2248                 :            :                 return err;
    2249                 :            : 
    2250                 :            :         memset(&b, 0, sizeof(vhd_bat_t));
    2251                 :            : 
    2252                 :          0 :         off  = ctx->header.table_offset;
    2253                 :          0 :         size = vhd_bytes_padded(bat->entries * sizeof(uint32_t));
    2254                 :            : 
    2255                 :          0 :         err  = vhd_seek(ctx, off, SEEK_SET);
    2256         [ #  # ]:          0 :         if (err)
    2257                 :            :                 return err;
    2258                 :            : 
    2259                 :          0 :         err  = posix_memalign(&buf, VHD_SECTOR_SIZE, size);
    2260         [ #  # ]:          0 :         if (err)
    2261                 :          0 :                 return -err;
    2262                 :          0 :         b.bat = buf;
    2263                 :            : 
    2264                 :          0 :         memcpy(b.bat, bat->bat, size);
    2265                 :          0 :         b.spb     = bat->spb;
    2266                 :          0 :         b.entries = bat->entries;
    2267                 :          0 :         vhd_bat_out(&b);
    2268                 :            : 
    2269                 :          0 :         err = vhd_write(ctx, b.bat, size);
    2270                 :          0 :         free(b.bat);
    2271                 :            : 
    2272                 :          0 :         return err;
    2273                 :            : }
    2274                 :            : 
    2275                 :            : static int
    2276                 :          0 : vhd_write_batmap_header(vhd_context_t *ctx, vhd_batmap_t *batmap)
    2277                 :            : {
    2278                 :            :         int err;
    2279                 :            :         size_t size;
    2280                 :            :         off64_t off;
    2281                 :          0 :         void *buf = NULL;
    2282                 :            : 
    2283                 :          0 :         err = vhd_batmap_header_offset(ctx, &off);
    2284         [ #  # ]:          0 :         if (err)
    2285                 :            :                 goto out;
    2286                 :            : 
    2287                 :          0 :         size = vhd_bytes_padded(sizeof(*batmap));
    2288                 :            : 
    2289                 :          0 :         err = vhd_seek(ctx, off, SEEK_SET);
    2290         [ #  # ]:          0 :         if (err)
    2291                 :            :                 goto out;
    2292                 :            : 
    2293                 :          0 :         err = posix_memalign(&buf, VHD_SECTOR_SIZE, size);
    2294         [ #  # ]:          0 :         if (err) {
    2295                 :          0 :                 err = -err;
    2296                 :          0 :                 goto out;
    2297                 :            :         }
    2298                 :            : 
    2299                 :          0 :         vhd_batmap_header_out(batmap);
    2300                 :          0 :         memset(buf, 0, size);
    2301                 :          0 :         memcpy(buf, &batmap->header, sizeof(batmap->header));
    2302                 :            : 
    2303                 :          0 :         err = vhd_write(ctx, buf, size);
    2304                 :            : 
    2305                 :            : out:
    2306         [ #  # ]:          0 :         if (err)
    2307         [ #  # ]:          0 :                 VHDLOG("%s: failed writing batmap: %d\n", ctx->file, err);
    2308                 :          0 :         free(buf);
    2309                 :          0 :         return err;
    2310                 :            : }
    2311                 :            : 
    2312                 :            : int
    2313                 :          0 : vhd_write_batmap(vhd_context_t *ctx, vhd_batmap_t *batmap)
    2314                 :            : {
    2315                 :            :         int err;
    2316                 :            :         off64_t off;
    2317                 :            :         vhd_batmap_t b;
    2318                 :            :         void *buf, *map;
    2319                 :            :         size_t size, map_size;
    2320                 :            : 
    2321                 :          0 :         buf      = NULL;
    2322                 :          0 :         map      = NULL;
    2323                 :            : 
    2324         [ #  # ]:          0 :         if (!vhd_has_batmap(ctx)) {
    2325                 :            :                 err = -EINVAL;
    2326                 :            :                 goto out;
    2327                 :            :         }
    2328                 :            : 
    2329                 :          0 :         b.header = batmap->header;
    2330                 :          0 :         b.map    = batmap->map;
    2331                 :            : 
    2332                 :          0 :         b.header.checksum = vhd_checksum_batmap(ctx, &b);
    2333                 :          0 :         err = vhd_validate_batmap(ctx, &b);
    2334 [ #  # ][ #  # ]:          0 :         if (err)
    2335                 :            :                 goto out;
    2336                 :            : 
    2337                 :          0 :         off      = b.header.batmap_offset;
    2338                 :          0 :         map_size = vhd_sectors_to_bytes(secs_round_up_no_zero(
    2339                 :          0 :                         ctx->footer.curr_size >> (VHD_BLOCK_SHIFT + 3)));
    2340         [ #  # ]:          0 :         ASSERT(vhd_sectors_to_bytes(b.header.batmap_size) >= map_size);
    2341                 :            : 
    2342                 :          0 :         err  = vhd_seek(ctx, off, SEEK_SET);
    2343         [ #  # ]:          0 :         if (err)
    2344                 :            :                 goto out;
    2345                 :            : 
    2346                 :          0 :         err  = posix_memalign(&map, VHD_SECTOR_SIZE, map_size);
    2347         [ #  # ]:          0 :         if (err) {
    2348                 :          0 :                 map = NULL;
    2349                 :          0 :                 err = -err;
    2350                 :          0 :                 goto out;
    2351                 :            :         }
    2352                 :            : 
    2353                 :          0 :         memcpy(map, b.map, map_size);
    2354                 :            : 
    2355                 :          0 :         err  = vhd_write(ctx, map, map_size);
    2356         [ #  # ]:          0 :         if (err)
    2357                 :            :                 goto out;
    2358                 :            : 
    2359                 :          0 :         err  = vhd_batmap_header_offset(ctx, &off);
    2360         [ #  # ]:          0 :         if (err)
    2361                 :            :                 goto out;
    2362                 :            : 
    2363                 :          0 :         size = vhd_bytes_padded(sizeof(vhd_batmap_header_t));
    2364                 :            : 
    2365                 :          0 :         err  = vhd_seek(ctx, off, SEEK_SET);
    2366         [ #  # ]:          0 :         if (err)
    2367                 :            :                 goto out;
    2368                 :            : 
    2369                 :          0 :         err  = posix_memalign(&buf, VHD_SECTOR_SIZE, size);
    2370         [ #  # ]:          0 :         if (err) {
    2371                 :          0 :                 err = -err;
    2372                 :          0 :                 buf = NULL;
    2373                 :          0 :                 goto out;
    2374                 :            :         }
    2375                 :            : 
    2376                 :          0 :         vhd_batmap_header_out(&b);
    2377                 :          0 :         memset(buf, 0, size);
    2378                 :          0 :         memcpy(buf, &b.header, sizeof(vhd_batmap_header_t));
    2379                 :            : 
    2380                 :          0 :         err  = vhd_write(ctx, buf, size);
    2381                 :            : 
    2382                 :            : out:
    2383         [ #  # ]:          0 :         if (err)
    2384         [ #  # ]:          0 :                 VHDLOG("%s: failed writing batmap: %d\n", ctx->file, err);
    2385                 :          0 :         free(buf);
    2386                 :          0 :         free(map);
    2387                 :          0 :         return 0;
    2388                 :            : }
    2389                 :            : 
    2390                 :            : int
    2391                 :          0 : vhd_write_bitmap(vhd_context_t *ctx, uint32_t block, char *bitmap)
    2392                 :            : {
    2393                 :            :         int err;
    2394                 :            :         off64_t off;
    2395                 :            :         uint64_t blk;
    2396                 :            :         size_t size;
    2397                 :            : 
    2398         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx))
    2399                 :            :                 return -EINVAL;
    2400                 :            : 
    2401                 :          0 :         err = vhd_validate_bat(&ctx->bat);
    2402         [ #  # ]:          0 :         if (err)
    2403                 :            :                 return err;
    2404                 :            : 
    2405         [ #  # ]:          0 :         if (block >= ctx->bat.entries)
    2406                 :            :                 return -ERANGE;
    2407                 :            : 
    2408         [ #  # ]:          0 :         if ((unsigned long)bitmap & (VHD_SECTOR_SIZE - 1))
    2409                 :            :                 return -EINVAL;
    2410                 :            : 
    2411                 :          0 :         blk  = ctx->bat.bat[block];
    2412         [ #  # ]:          0 :         if (blk == DD_BLK_UNUSED)
    2413                 :            :                 return -EINVAL;
    2414                 :            : 
    2415                 :          0 :         off  = vhd_sectors_to_bytes(blk);
    2416                 :          0 :         size = vhd_sectors_to_bytes(ctx->bm_secs);
    2417                 :            : 
    2418                 :          0 :         err  = vhd_seek(ctx, off, SEEK_SET);
    2419         [ #  # ]:          0 :         if (err)
    2420                 :            :                 return err;
    2421                 :            : 
    2422                 :          0 :         err  = vhd_write(ctx, bitmap, size);
    2423         [ #  # ]:          0 :         if (err)
    2424                 :          0 :                 return err;
    2425                 :            : 
    2426                 :            :         return 0;
    2427                 :            : }
    2428                 :            : 
    2429                 :            : int
    2430                 :          0 : vhd_write_block(vhd_context_t *ctx, uint32_t block, char *data)
    2431                 :            : {
    2432                 :            :         int err;
    2433                 :            :         off64_t off;
    2434                 :            :         size_t size;
    2435                 :            :         uint64_t blk;
    2436                 :            : 
    2437         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx))
    2438                 :            :                 return -EINVAL;
    2439                 :            : 
    2440                 :          0 :         err = vhd_validate_bat(&ctx->bat);
    2441         [ #  # ]:          0 :         if (err)
    2442                 :            :                 return err;
    2443                 :            : 
    2444         [ #  # ]:          0 :         if (block >= ctx->bat.entries)
    2445                 :            :                 return -ERANGE;
    2446                 :            : 
    2447         [ #  # ]:          0 :         if ((unsigned long)data & (VHD_SECTOR_SIZE - 1))
    2448                 :            :                 return -EINVAL;
    2449                 :            : 
    2450                 :          0 :         blk  = ctx->bat.bat[block];
    2451         [ #  # ]:          0 :         if (blk == DD_BLK_UNUSED)
    2452                 :            :                 return -EINVAL;
    2453                 :            : 
    2454                 :          0 :         off  = vhd_sectors_to_bytes(blk + ctx->bm_secs);
    2455                 :          0 :         size = vhd_sectors_to_bytes(ctx->spb);
    2456                 :            : 
    2457                 :          0 :         err  = vhd_seek(ctx, off, SEEK_SET);
    2458         [ #  # ]:          0 :         if (err)
    2459                 :            :                 return err;
    2460                 :            : 
    2461                 :          0 :         err  = vhd_write(ctx, data, size);
    2462         [ #  # ]:          0 :         if (err)
    2463                 :          0 :                 return err;
    2464                 :            : 
    2465                 :            :         return 0;
    2466                 :            : }
    2467                 :            : 
    2468                 :            : static inline int
    2469                 :          0 : namedup(char **dup, const char *name)
    2470                 :            : {
    2471                 :          0 :         *dup = NULL;
    2472                 :            : 
    2473         [ #  # ]:          0 :         if (strnlen(name, MAX_NAME_LEN) >= MAX_NAME_LEN)
    2474                 :            :                 return -ENAMETOOLONG;
    2475                 :            :         
    2476                 :          0 :         *dup = strdup(name);
    2477         [ #  # ]:          0 :         if (*dup == NULL)
    2478                 :            :                 return -ENOMEM;
    2479                 :            : 
    2480                 :          0 :         return 0;
    2481                 :            : }
    2482                 :            : 
    2483                 :            : #define vwrite (ssize_t (*)(int, void *, size_t))write
    2484                 :            : #define vpwrite (ssize_t (*)(int, void *, size_t, off_t))pwrite
    2485                 :            : 
    2486                 :            : static ssize_t
    2487                 :          0 : vhd_atomic_pio(ssize_t (*f) (int, void *, size_t, off_t),
    2488                 :            :                int fd, void *_s, size_t n, off_t off)
    2489                 :            : {
    2490                 :          0 :         char *s = _s;
    2491                 :          0 :         size_t pos = 0;
    2492                 :            :         ssize_t res;
    2493                 :            :         struct stat st;
    2494                 :            : 
    2495                 :            :         memset(&st, 0, sizeof(st));
    2496                 :            : 
    2497                 :            :         for (;;) {
    2498                 :          0 :                 res = (f) (fd, s + pos, n - pos, off + pos);
    2499      [ #  #  # ]:          0 :                 switch (res) {
    2500                 :            :                 case -1:
    2501         [ #  # ]:          0 :                         if (errno == EINTR || errno == EAGAIN)
    2502                 :          0 :                                 continue;
    2503                 :            :                         else
    2504                 :          0 :                                 return 0;
    2505                 :            :                         break;
    2506                 :            :                 case 0:
    2507                 :          0 :                         errno = EPIPE;
    2508                 :          0 :                         return pos;
    2509                 :            :                 }
    2510                 :            : 
    2511         [ #  # ]:          0 :                 if (pos + res == n)
    2512                 :          0 :                         return n;
    2513                 :            : 
    2514         [ #  # ]:          0 :                 if (!st.st_size)
    2515         [ #  # ]:          0 :                         if (fstat(fd, &st) == -1)
    2516                 :            :                                 return -1;
    2517                 :            : 
    2518         [ #  # ]:          0 :                 if (off + pos + res == st.st_size)
    2519                 :          0 :                         return pos + res;
    2520                 :            : 
    2521                 :          0 :                 pos += (res & ~(VHD_SECTOR_SIZE - 1));
    2522                 :            :         }
    2523                 :            : 
    2524                 :            :         return -1;
    2525                 :            : }
    2526                 :            : 
    2527                 :            : static ssize_t
    2528                 :          0 : vhd_atomic_io(ssize_t (*f) (int, void *, size_t), vhd_context_t *ctx, void *_s, size_t n)
    2529                 :            : {
    2530                 :            :         ssize_t res;
    2531                 :            :         ssize_t (*pf) (int, void *, size_t, off_t);
    2532                 :            : 
    2533         [ #  # ]:          0 :         pf = (f == read ? pread : vpwrite);
    2534                 :          0 :         res = vhd_atomic_pio(pf, ctx->fd, _s, n, ctx->offset);
    2535                 :            : 
    2536         [ #  # ]:          0 :         if (res > 0)
    2537                 :          0 :                 ctx->offset += res;
    2538                 :            : 
    2539                 :          0 :         return res;
    2540                 :            : }
    2541                 :            : 
    2542                 :            : int
    2543                 :          0 : vhd_seek(vhd_context_t *ctx, off64_t offset, int whence)
    2544                 :            : {
    2545                 :            :         off64_t off;
    2546                 :            : 
    2547                 :          0 :         off = lseek64(ctx->fd, offset, whence);
    2548         [ #  # ]:          0 :         if (off == (off64_t)-1) {
    2549         [ #  # ]:          0 :                 VHDLOG("%s: seek(0x%08"PRIx64", %d) failed: %d\n",
    2550                 :            :                        ctx->file, offset, whence, -errno);
    2551                 :          0 :                 return -errno;
    2552                 :            :         }
    2553                 :            : 
    2554                 :          0 :         ctx->offset = off;
    2555                 :            : 
    2556                 :          0 :         return 0;
    2557                 :            : }
    2558                 :            : 
    2559                 :            : off64_t
    2560                 :          0 : vhd_position(vhd_context_t *ctx)
    2561                 :            : {
    2562                 :          0 :         return ctx->offset;
    2563                 :            : }
    2564                 :            : 
    2565                 :            : int
    2566                 :          0 : vhd_read(vhd_context_t *ctx, void *buf, size_t size)
    2567                 :            : {
    2568                 :            :         size_t ret;
    2569                 :            : 
    2570                 :          0 :         errno = 0;
    2571                 :            : 
    2572                 :          0 :         ret = vhd_atomic_io(read, ctx, buf, size);
    2573         [ #  # ]:          0 :         if (ret == size)
    2574                 :            :                 return 0;
    2575                 :            : 
    2576         [ #  # ]:          0 :         VHDLOG("%s: read of %zu returned %zd, errno: %d\n",
    2577                 :            :                ctx->file, size, ret, -errno);
    2578                 :            : 
    2579         [ #  # ]:          0 :         return (errno ? -errno : -EIO);
    2580                 :            : }
    2581                 :            : 
    2582                 :            : int
    2583                 :          0 : vhd_write(vhd_context_t *ctx, void *buf, size_t size)
    2584                 :            : {
    2585                 :            :         size_t ret;
    2586                 :            : 
    2587                 :          0 :         errno = 0;
    2588                 :            : 
    2589                 :          0 :         ret = vhd_atomic_io(vwrite, ctx, buf, size);
    2590         [ #  # ]:          0 :         if (ret == size)
    2591                 :            :                 return 0;
    2592                 :            : 
    2593         [ #  # ]:          0 :         VHDLOG("%s: write of %zu returned %zd, errno: %d\n",
    2594                 :            :                ctx->file, size, ret, -errno);
    2595                 :            : 
    2596         [ #  # ]:          0 :         return (errno ? -errno : -EIO);
    2597                 :            : }
    2598                 :            : 
    2599                 :            : static int
    2600                 :          0 : vhd_pread(vhd_context_t *ctx, void *buf, size_t size, off64_t offset)
    2601                 :            : {
    2602                 :            :         ssize_t ret;
    2603                 :            : 
    2604                 :          0 :         errno = 0;
    2605                 :            : 
    2606                 :          0 :         ret = vhd_atomic_pio(pread, ctx->fd, buf, size, offset);
    2607         [ #  # ]:          0 :         if (ret == size)
    2608                 :            :                 return 0;
    2609                 :            : 
    2610         [ #  # ]:          0 :         VHDLOG("%s: pread of %zu returned %zd, errno: %d\n",
    2611                 :            :                ctx->file, size, ret, -errno);
    2612                 :            : 
    2613         [ #  # ]:          0 :         return (errno ? -errno : -EIO);
    2614                 :            : }
    2615                 :            : 
    2616                 :            : static int
    2617                 :          0 : vhd_pwrite(vhd_context_t *ctx, void *buf, size_t size, off64_t offset)
    2618                 :            : {
    2619                 :            :         ssize_t ret;
    2620                 :            : 
    2621                 :          0 :         errno = 0;
    2622                 :            : 
    2623                 :          0 :         ret = vhd_atomic_pio(vpwrite, ctx->fd, buf, size, offset);
    2624         [ #  # ]:          0 :         if (ret == size)
    2625                 :            :                 return 0;
    2626                 :            : 
    2627         [ #  # ]:          0 :         VHDLOG("%s: pwrite of %zu returned %zd, errno: %d\n",
    2628                 :            :                ctx->file, size, ret, -errno);
    2629                 :            : 
    2630         [ #  # ]:          0 :         return (errno ? -errno : -EIO);
    2631                 :            : }
    2632                 :            : 
    2633                 :            : int
    2634                 :          0 : vhd_offset(vhd_context_t *ctx, uint32_t sector, uint32_t *offset)
    2635                 :            : {
    2636                 :            :         int err;
    2637                 :            :         uint32_t block;
    2638                 :            : 
    2639         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx))
    2640                 :          0 :                 return sector;
    2641                 :            : 
    2642                 :          0 :         err = vhd_get_bat(ctx);
    2643         [ #  # ]:          0 :         if (err)
    2644                 :            :                 return err;
    2645                 :            : 
    2646                 :          0 :         block = sector / ctx->spb;
    2647         [ #  # ]:          0 :         if (ctx->bat.bat[block] == DD_BLK_UNUSED)
    2648                 :          0 :                 *offset = DD_BLK_UNUSED;
    2649                 :            :         else
    2650                 :          0 :                 *offset = ctx->bat.bat[block] +
    2651                 :          0 :                         ctx->bm_secs + (sector % ctx->spb);
    2652                 :            : 
    2653                 :            :         return 0;
    2654                 :            : }
    2655                 :            : 
    2656                 :            : int
    2657                 :          0 : vhd_open_fast(vhd_context_t *ctx)
    2658                 :            : {
    2659                 :            :         int err;
    2660                 :            :         void *buf;
    2661                 :            :         size_t size;
    2662                 :            : 
    2663                 :          0 :         size = sizeof(vhd_footer_t) + sizeof(vhd_header_t);
    2664                 :          0 :         err  = posix_memalign(&buf, VHD_SECTOR_SIZE, size);
    2665         [ #  # ]:          0 :         if (err) {
    2666         [ #  # ]:          0 :                 VHDLOG("failed allocating %s: %d\n", ctx->file, -err);
    2667                 :          0 :                 return -err;
    2668                 :            :         }
    2669                 :            : 
    2670                 :          0 :         err = vhd_read(ctx, buf, size);
    2671         [ #  # ]:          0 :         if (err) {
    2672         [ #  # ]:          0 :                 VHDLOG("failed reading %s: %d\n", ctx->file, err);
    2673                 :            :                 goto out;
    2674                 :            :         }
    2675                 :            : 
    2676                 :          0 :         memcpy(&ctx->footer, buf, sizeof(vhd_footer_t));
    2677                 :          0 :         vhd_footer_in(&ctx->footer);
    2678                 :          0 :         err = vhd_validate_footer(&ctx->footer);
    2679         [ #  # ]:          0 :         if (err)
    2680                 :            :                 goto out;
    2681                 :            : 
    2682         [ #  # ]:          0 :         if (vhd_type_dynamic(ctx)) {
    2683         [ #  # ]:          0 :                 if (ctx->footer.data_offset != sizeof(vhd_footer_t))
    2684                 :          0 :                         err = vhd_read_header(ctx, &ctx->header);
    2685                 :            :                 else {
    2686                 :          0 :                         memcpy(&ctx->header,
    2687                 :          0 :                                buf + sizeof(vhd_footer_t),
    2688                 :            :                                sizeof(vhd_header_t));
    2689                 :          0 :                         vhd_header_in(&ctx->header);
    2690                 :          0 :                         err = vhd_validate_header(&ctx->header);
    2691                 :            :                 }
    2692                 :            : 
    2693         [ #  # ]:          0 :                 if (err)
    2694                 :            :                         goto out;
    2695                 :            : 
    2696                 :          0 :                 ctx->spb     = ctx->header.block_size >> VHD_SECTOR_SHIFT;
    2697                 :          0 :                 ctx->bm_secs = secs_round_up_no_zero(ctx->spb >> 3);
    2698                 :            :         }
    2699                 :            : 
    2700                 :            : out:
    2701                 :          0 :         free(buf);
    2702                 :          0 :         return err;
    2703                 :            : }
    2704                 :            : 
    2705                 :            : int
    2706                 :          0 : vhd_open(vhd_context_t *ctx, const char *file, int flags)
    2707                 :            : {
    2708                 :            :         int i, err, oflags;
    2709                 :            : 
    2710         [ #  # ]:          0 :         if (flags & VHD_OPEN_STRICT)
    2711                 :          0 :                 vhd_flag_clear(flags, VHD_OPEN_FAST);
    2712                 :            : 
    2713                 :            :         memset(ctx, 0, sizeof(vhd_context_t));
    2714                 :            :         vhd_cache_init(ctx);
    2715                 :            : 
    2716                 :          0 :         ctx->fd     = -1;
    2717                 :          0 :         ctx->oflags = flags;
    2718                 :            : 
    2719                 :          0 :         char bypass_file[PATH_MAX] = "";
    2720         [ #  # ]:          0 :         if (flags & VHD_OPEN_RDONLY) {
    2721                 :            :                 struct stat stats;
    2722                 :          0 :                 err = stat(file, &stats);
    2723         [ #  # ]:          0 :                 if (err == -1)
    2724                 :            :                         /* Coverity seems not understand that stat sets errno. */
    2725         [ #  # ]:          0 :                         return errno ? -errno : -EINVAL;
    2726                 :            : 
    2727                 :            :                 /* If a DRBD path is used, we try to use the real physical device. */
    2728                 :            :                 /* Why? Because the device can be locked by any program. */
    2729                 :            :                 /* Note: We can't use the physical data if the DRBD is a diskless device... */
    2730                 :            :                 /* Logic but annoying. Same idea: if the disk is not up to date we can't use it. */
    2731                 :            :                 dev_t mapper_dev;
    2732 [ #  # ][ #  # ]:          0 :                 if (drbd_is_up_to_date(&stats) && drbd_to_mapper(&stats, &mapper_dev) == 0) {
    2733         [ #  # ]:          0 :                         if (snprintf(bypass_file, sizeof bypass_file, "/dev/block/%u:%u", major(mapper_dev), minor(mapper_dev)) == -1)
    2734                 :            :                                 return -EINVAL;
    2735                 :            :                 }
    2736                 :            :         }
    2737                 :            : 
    2738                 :          0 :         err = namedup(&ctx->file, file);
    2739         [ #  # ]:          0 :         if (err)
    2740                 :            :                 return err;
    2741                 :            : 
    2742                 :          0 :         oflags = O_LARGEFILE;
    2743         [ #  # ]:          0 :         if (!(flags & VHD_OPEN_CACHED)) {
    2744                 :          0 :                 oflags |= O_DIRECT;
    2745         [ #  # ]:          0 :                 if (access("/etc/tapdisk_use_dsync", F_OK) != -1) {
    2746                 :            :                 /* tapdisk_use_dsync exists, it means we need to open
    2747                 :            :                  * the leaf with O_DSYNC as well.
    2748                 :            :                  * This is not a public tapdisk interface
    2749                 :            :                  */
    2750         [ #  # ]:          0 :                         if (!(flags & VHD_OPEN_RDONLY))
    2751                 :          0 :                                 oflags |= O_DSYNC;
    2752                 :            :                 }
    2753                 :            :         }
    2754                 :            :         if (flags & VHD_OPEN_RDONLY)
    2755                 :            :                 oflags |= O_RDONLY;
    2756         [ #  # ]:          0 :         if (flags & VHD_OPEN_RDWR)
    2757                 :          0 :                 oflags |= O_RDWR;
    2758                 :            : 
    2759         [ #  # ]:          0 :         ctx->fd = open_optional_odirect(*bypass_file ? bypass_file : ctx->file, oflags, 0644);
    2760         [ #  # ]:          0 :         if (ctx->fd == -1) {
    2761                 :          0 :                 err = -errno;
    2762         [ #  # ]:          0 :                 VHDLOG("failed to open %s: %d\n", ctx->file, err);
    2763                 :            :                 goto fail;
    2764                 :            :         }
    2765         [ #  # ]:          0 :         if (flags & VHD_OPEN_CACHED) {
    2766                 :            :                 struct stat st;
    2767                 :            : 
    2768                 :            :                 /* Ensure that we only open regular files without O_DIRECT */
    2769         [ #  # ]:          0 :                 if (fstat(ctx->fd, &st) < 0) {
    2770                 :          0 :                         err = -errno;
    2771         [ #  # ]:          0 :                         VHDLOG("failed to stat %s: %d\n", ctx->file, err);
    2772                 :          0 :                         goto fail;
    2773                 :            :                 }
    2774         [ #  # ]:          0 :                 if (!S_ISREG(st.st_mode)) {
    2775                 :          0 :                         err = -EINVAL;
    2776         [ #  # ]:          0 :                         VHDLOG("cannot open non-regular file (%s) "
    2777                 :            :                                "with read caching enabled\n", ctx->file);
    2778                 :            :                         goto fail;
    2779                 :            :                 }
    2780                 :            : 
    2781                 :            :         }
    2782                 :            : 
    2783                 :          0 :         err = vhd_test_file_fixed(ctx->file, &ctx->is_block);
    2784         [ #  # ]:          0 :         if (err)
    2785                 :            :                 goto fail;
    2786                 :            : 
    2787         [ #  # ]:          0 :         if (flags & VHD_OPEN_FAST) {
    2788                 :          0 :                 err = vhd_open_fast(ctx);
    2789         [ #  # ]:          0 :                 if (err)
    2790                 :            :                         goto fail;
    2791                 :            : 
    2792                 :            :                 return 0;
    2793                 :            :         }
    2794                 :            : 
    2795                 :          0 :         err = vhd_read_footer(ctx, &ctx->footer,
    2796                 :          0 :                         (flags & VHD_OPEN_USE_BKP_FOOTER) == VHD_OPEN_USE_BKP_FOOTER);
    2797         [ #  # ]:          0 :         if (err)
    2798                 :            :                 goto fail;
    2799                 :            : 
    2800 [ #  # ][ #  # ]:          0 :         if (!(flags & VHD_OPEN_IGNORE_DISABLED) && vhd_disabled(ctx)) {
    2801                 :            :                 err = -EINVAL;
    2802                 :            :                 goto fail;
    2803                 :            :         }
    2804                 :            : 
    2805         [ #  # ]:          0 :         if (vhd_type_dynamic(ctx)) {
    2806         [ #  # ]:          0 :                 for (i = 0; i < VHD_HEADER_MAX_RETRIES; i++) {
    2807                 :          0 :                         err = vhd_read_header(ctx, &ctx->header);
    2808         [ #  # ]:          0 :                         if (!err)
    2809                 :            :                                 break;
    2810         [ #  # ]:          0 :                         VHDLOG("Error reading header, retry %d\n", i);
    2811                 :          0 :                         sleep(1);
    2812                 :            :                 }
    2813         [ #  # ]:          0 :                 if (err)
    2814                 :            :                         goto fail;
    2815                 :            : 
    2816                 :          0 :                 ctx->spb     = ctx->header.block_size >> VHD_SECTOR_SHIFT;
    2817                 :          0 :                 ctx->bm_secs = secs_round_up_no_zero(ctx->spb >> 3);
    2818                 :            :         }
    2819                 :            : 
    2820                 :            :         /* Check for obsolete, broken VHD version */
    2821 [ #  # ][ #  # ]:          0 :         if (vhd_creator_tapdisk(ctx) &&
    2822                 :          0 :             ctx->footer.crtr_ver == 0x00000001) {
    2823         [ #  # ]:          0 :                 VHDLOG("error unsupported version 0.1 VHD");
    2824                 :            :                 err = -EINVAL;
    2825                 :            :                 goto fail;
    2826                 :            :         }
    2827                 :            : 
    2828                 :          0 :         err = vhd_cache_load(ctx);
    2829         [ #  # ]:          0 :         if (err) {
    2830         [ #  # ]:          0 :                 VHDLOG("failed to load cache: %d\n", err);
    2831                 :            :                 goto fail;
    2832                 :            :         }
    2833                 :            : 
    2834                 :            :         return 0;
    2835                 :            : 
    2836                 :            : fail:
    2837         [ #  # ]:          0 :         if (ctx->fd != -1)
    2838                 :          0 :                 close(ctx->fd);
    2839                 :          0 :         free(ctx->file);
    2840                 :            :         memset(ctx, 0, sizeof(vhd_context_t));
    2841                 :          0 :         return err;
    2842                 :            : }
    2843                 :            : 
    2844                 :            : void
    2845                 :          0 : vhd_close(vhd_context_t *ctx)
    2846                 :            : {
    2847                 :          0 :         vhd_cache_unload(ctx);
    2848                 :            : 
    2849         [ #  # ]:          0 :         if (ctx->fd != -1) {
    2850                 :          0 :                 fsync(ctx->fd);
    2851                 :          0 :                 close(ctx->fd);
    2852                 :            :         }
    2853                 :            : 
    2854                 :          0 :         free(ctx->file);
    2855                 :          0 :         free(ctx->bat.bat);
    2856                 :          0 :         free(ctx->batmap.map);
    2857                 :          0 :         free(ctx->custom_parent);
    2858                 :            :         memset(ctx, 0, sizeof(vhd_context_t));
    2859                 :          0 : }
    2860                 :            : 
    2861                 :            : static inline void
    2862                 :          0 : vhd_initialize_footer(vhd_context_t *ctx, int type, uint64_t size)
    2863                 :            : {
    2864                 :          0 :         memset(&ctx->footer, 0, sizeof(vhd_footer_t));
    2865                 :          0 :         memcpy(ctx->footer.cookie, HD_COOKIE, sizeof(ctx->footer.cookie));
    2866                 :          0 :         ctx->footer.features     = HD_RESERVED;
    2867                 :          0 :         ctx->footer.ff_version   = HD_FF_VERSION;
    2868                 :          0 :         ctx->footer.timestamp    = vhd_time(time(NULL));
    2869                 :          0 :         ctx->footer.crtr_ver     = VHD_CURRENT_VERSION;
    2870                 :          0 :         ctx->footer.crtr_os      = 0x00000000;
    2871                 :          0 :         ctx->footer.orig_size    = size;
    2872                 :          0 :         ctx->footer.curr_size    = size;
    2873                 :          0 :         ctx->footer.geometry     = vhd_chs(size);
    2874                 :          0 :         ctx->footer.type         = type;
    2875                 :          0 :         ctx->footer.saved        = 0;
    2876                 :          0 :         ctx->footer.data_offset  = 0xFFFFFFFFFFFFFFFFULL;
    2877                 :          0 :         safe_strncpy(ctx->footer.crtr_app, "tap", sizeof(ctx->footer.crtr_app));
    2878                 :          0 :         uuid_generate(ctx->footer.uuid);
    2879                 :          0 : }
    2880                 :            : 
    2881                 :            : int
    2882                 :          0 : vhd_initialize_header_parent_name(vhd_context_t *ctx, const char *parent_path)
    2883                 :            : {
    2884                 :            :         int err;
    2885                 :            :         iconv_t cd;
    2886                 :            :         size_t ibl, obl;
    2887                 :            :         char *pname, *ppath, *dst;
    2888                 :            : 
    2889                 :          0 :         err   = 0;
    2890                 :          0 :         pname = NULL;
    2891                 :          0 :         ppath = NULL;
    2892                 :            : 
    2893                 :            :         /*
    2894                 :            :          * MICROSOFT_COMPAT
    2895                 :            :          * big endian unicode here 
    2896                 :            :          */
    2897                 :          0 :         cd = iconv_open(UTF_16BE, "ASCII");
    2898         [ #  # ]:          0 :         if (cd == (iconv_t)-1) {
    2899                 :          0 :                 err = -errno;
    2900                 :          0 :                 return err;
    2901                 :            :         }
    2902                 :            : 
    2903                 :          0 :         ppath = strdup(parent_path);
    2904         [ #  # ]:          0 :         if (!ppath) {
    2905                 :            :                 err = -ENOMEM;
    2906                 :            :                 goto out;
    2907                 :            :         }
    2908                 :            : 
    2909                 :          0 :         pname = basename(ppath);
    2910         [ #  # ]:          0 :         if (!strcmp(pname, "")) {
    2911                 :            :                 err = -EINVAL;
    2912                 :            :                 goto out;
    2913                 :            :         }
    2914                 :            : 
    2915                 :          0 :         ibl = strlen(pname);
    2916                 :          0 :         obl = sizeof(ctx->header.prt_name);
    2917                 :          0 :         dst = ctx->header.prt_name;
    2918                 :            : 
    2919                 :          0 :         memset(dst, 0, obl);
    2920                 :            : 
    2921 [ #  # ][ #  # ]:          0 :         if (iconv(cd, &pname, &ibl, &dst, &obl) == (size_t)-1 || ibl)
    2922         [ #  # ]:          0 :                 err = (errno ? -errno : -EINVAL);
    2923                 :            : 
    2924                 :            : out:
    2925                 :          0 :         iconv_close(cd);
    2926                 :          0 :         free(ppath);
    2927                 :          0 :         return err;
    2928                 :            : }
    2929                 :            : 
    2930                 :            : static off64_t
    2931                 :          0 : get_file_size(const char *name)
    2932                 :            : {
    2933                 :            :         int fd;
    2934                 :            :         off64_t end;
    2935                 :            : 
    2936                 :          0 :         fd = open(name, O_LARGEFILE | O_RDONLY);
    2937         [ #  # ]:          0 :         if (fd == -1) {
    2938         [ #  # ]:          0 :                 VHDLOG("unable to open '%s': %d\n", name, errno);
    2939                 :          0 :                 return -errno;
    2940                 :            :         }
    2941                 :          0 :         end = lseek64(fd, 0, SEEK_END);
    2942                 :          0 :         close(fd); 
    2943                 :          0 :         return end;
    2944                 :            : }
    2945                 :            : 
    2946                 :            : static int
    2947                 :          0 : vhd_initialize_header(vhd_context_t *ctx, const char *parent_path, 
    2948                 :            :                 uint64_t size, int raw, uint64_t *psize)
    2949                 :            : {
    2950                 :            :         int err;
    2951                 :            :         struct stat stats;
    2952                 :            :         vhd_context_t parent;
    2953                 :            :         uint64_t _max_bat_size;
    2954                 :            : 
    2955         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx))
    2956                 :          0 :                 return -EINVAL;
    2957                 :            : 
    2958                 :          0 :         memset(&ctx->header, 0, sizeof(vhd_header_t));
    2959                 :          0 :         memcpy(ctx->header.cookie, DD_COOKIE, sizeof(ctx->header.cookie));
    2960                 :          0 :         ctx->header.data_offset  = (uint64_t)-1;
    2961                 :          0 :         ctx->header.table_offset = VHD_SECTOR_SIZE * 3; /* 1 ftr + 2 hdr */
    2962                 :          0 :         ctx->header.hdr_ver      = DD_VERSION;
    2963                 :          0 :         ctx->header.block_size   = VHD_BLOCK_SIZE;
    2964                 :          0 :         ctx->header.prt_ts       = 0;
    2965                 :          0 :         ctx->header.res1         = 0;
    2966                 :            : 
    2967                 :          0 :         _max_bat_size = (ctx->footer.curr_size +
    2968                 :          0 :                         VHD_BLOCK_SIZE - 1) >> VHD_BLOCK_SHIFT;
    2969         [ #  # ]:          0 :         if (unlikely(_max_bat_size > UINT_MAX))
    2970                 :            :                 return -EINVAL;
    2971                 :          0 :         ctx->header.max_bat_size = _max_bat_size;
    2972                 :            : 
    2973                 :          0 :         ctx->footer.data_offset  = VHD_SECTOR_SIZE;
    2974                 :            : 
    2975         [ #  # ]:          0 :         if (ctx->footer.type == HD_TYPE_DYNAMIC)
    2976                 :            :                 return 0;
    2977                 :            : 
    2978                 :          0 :         err = stat(parent_path, &stats);
    2979         [ #  # ]:          0 :         if (err == -1)
    2980                 :          0 :                 return -errno;
    2981                 :            : 
    2982         [ #  # ]:          0 :         if (raw) {
    2983                 :          0 :                 ctx->header.prt_ts = vhd_time(stats.st_mtime);
    2984                 :          0 :                 *psize = get_file_size(parent_path);
    2985         [ #  # ]:          0 :                 if (!size)
    2986                 :          0 :                         size = *psize;
    2987                 :            :         }
    2988                 :            :         else {
    2989                 :          0 :                 err = vhd_open(&parent, parent_path, VHD_OPEN_RDONLY);
    2990         [ #  # ]:          0 :                 if (err)
    2991                 :            :                         return err;
    2992                 :            : 
    2993                 :          0 :                 ctx->header.prt_ts = vhd_time(stats.st_mtime);
    2994                 :          0 :                 uuid_copy(ctx->header.prt_uuid, parent.footer.uuid);
    2995         [ #  # ]:          0 :                 if (uuid_is_null(ctx->header.prt_uuid)) {
    2996                 :          0 :                         vhd_close(&parent);
    2997                 :            :                         return -EINVAL;
    2998                 :            :                 }
    2999                 :          0 :                 *psize = parent.footer.curr_size;
    3000         [ #  # ]:          0 :                 if (!size)
    3001                 :          0 :                         size = *psize;
    3002                 :          0 :                 vhd_close(&parent);
    3003                 :            :         }
    3004         [ #  # ]:          0 :         if (size < *psize) {
    3005         [ #  # ]:          0 :                 VHDLOG("snapshot size (%"PRIu64") < parent size (%"PRIu64")\n",
    3006                 :            :                                 size, *psize);
    3007                 :            :                 return -EINVAL;
    3008                 :            :         }
    3009                 :          0 :         ctx->footer.orig_size    = size;
    3010                 :          0 :         ctx->footer.curr_size    = size;
    3011                 :          0 :         ctx->footer.geometry     = vhd_chs(size);
    3012                 :          0 :         ctx->header.max_bat_size = 
    3013                 :          0 :                 (size + VHD_BLOCK_SIZE - 1) >> VHD_BLOCK_SHIFT;
    3014                 :            : 
    3015                 :          0 :         return vhd_initialize_header_parent_name(ctx, parent_path);
    3016                 :            : }
    3017                 :            : 
    3018                 :            : int
    3019                 :          0 : vhd_write_parent_locators(vhd_context_t *ctx, const char *parent)
    3020                 :            : {
    3021                 :            :         int i, err;
    3022                 :            :         off64_t off;
    3023                 :            :         uint32_t code;
    3024                 :            : 
    3025                 :          0 :         code = PLAT_CODE_NONE;
    3026                 :            : 
    3027         [ #  # ]:          0 :         if (ctx->footer.type != HD_TYPE_DIFF)
    3028                 :            :                 return -EINVAL;
    3029                 :            : 
    3030                 :          0 :         off = ctx->batmap.header.batmap_offset + 
    3031                 :          0 :                 vhd_sectors_to_bytes(ctx->batmap.header.batmap_size);
    3032         [ #  # ]:          0 :         if (off & (VHD_SECTOR_SIZE - 1))
    3033                 :          0 :                 off = vhd_bytes_padded(off);
    3034                 :            : 
    3035         [ #  # ]:          0 :         for (i = 0; i < 3; i++) {
    3036   [ #  #  #  # ]:          0 :                 switch (i) {
    3037                 :            :                 case 0:
    3038                 :          0 :                         code = PLAT_CODE_MACX;
    3039                 :          0 :                         break;
    3040                 :            :                 case 1:
    3041                 :          0 :                         code = PLAT_CODE_W2KU;
    3042                 :          0 :                         break;
    3043                 :            :                 case 2:
    3044                 :          0 :                         code = PLAT_CODE_W2RU;
    3045                 :          0 :                         break;
    3046                 :            :                 }
    3047                 :            : 
    3048                 :          0 :                 err = vhd_parent_locator_write_at(ctx, parent, off, code,
    3049                 :          0 :                                                   0, ctx->header.loc + i);
    3050         [ #  # ]:          0 :                 if (err)
    3051                 :            :                         return err;
    3052                 :            : 
    3053                 :          0 :                 off += vhd_parent_locator_size(ctx->header.loc + i);
    3054                 :            :         }
    3055                 :            : 
    3056                 :            :         return 0;
    3057                 :            : }
    3058                 :            : 
    3059                 :            : int
    3060                 :          0 : vhd_change_parent(vhd_context_t *child, char *parent_path, int raw)
    3061                 :            : {
    3062                 :            :         int i, err;
    3063                 :            :         char *ppath;
    3064                 :            :         struct stat stats;
    3065                 :            :         vhd_context_t parent;
    3066                 :            :         char __parent_path[PATH_MAX];
    3067                 :            : 
    3068         [ #  # ]:          0 :         if (child->footer.type != HD_TYPE_DIFF) {
    3069         [ #  # ]:          0 :                 VHDLOG("would-be child is not a differencing disk\n");
    3070                 :          0 :                 return -EINVAL;
    3071                 :            :         }
    3072                 :            : 
    3073                 :          0 :         ppath = canonpath(parent_path, __parent_path, sizeof(__parent_path));
    3074         [ #  # ]:          0 :         if (!ppath) {
    3075         [ #  # ]:          0 :                 VHDLOG("error resolving parent path %s for %s: %d\n",
    3076                 :            :                        parent_path, child->file, errno);
    3077                 :          0 :                 return -errno;
    3078                 :            :         }
    3079                 :            : 
    3080                 :          0 :         err = stat(ppath, &stats);
    3081         [ #  # ]:          0 :         if (err == -1) {
    3082                 :          0 :                 err = -errno;
    3083                 :          0 :                 goto out;
    3084                 :            :         }
    3085                 :            : 
    3086         [ #  # ]:          0 :         if (!S_ISREG(stats.st_mode) && !S_ISBLK(stats.st_mode)) {
    3087                 :            :                 err = -EINVAL;
    3088                 :            :                 goto out;
    3089                 :            :         }
    3090                 :            : 
    3091         [ #  # ]:          0 :         if (raw) {
    3092                 :          0 :                 uuid_clear(child->header.prt_uuid);
    3093                 :            :         } else {
    3094                 :          0 :                 err = vhd_open(&parent, ppath, VHD_OPEN_RDONLY);
    3095         [ #  # ]:          0 :                 if (err) {
    3096         [ #  # ]:          0 :                         VHDLOG("error opening parent %s for %s: %d\n",
    3097                 :            :                                ppath, child->file, err);
    3098                 :            :                         goto out;
    3099                 :            :                 }
    3100                 :          0 :                 uuid_copy(child->header.prt_uuid, parent.footer.uuid);
    3101                 :          0 :                 vhd_close(&parent);
    3102                 :            :         }
    3103                 :            : 
    3104                 :          0 :         vhd_initialize_header_parent_name(child, ppath);
    3105                 :          0 :         child->header.prt_ts = vhd_time(stats.st_mtime);
    3106                 :            : 
    3107      [ #  #  # ]:          0 :         for (i = 0; i < vhd_parent_locator_count(child); i++) {
    3108                 :          0 :                 vhd_parent_locator_t *loc = child->header.loc + i;
    3109                 :          0 :                 size_t max = vhd_parent_locator_size(loc);
    3110                 :            : 
    3111         [ #  # ]:          0 :                 switch (loc->code) {
    3112                 :            :                 case PLAT_CODE_MACX:
    3113                 :            :                 case PLAT_CODE_W2KU:
    3114                 :            :                 case PLAT_CODE_W2RU:
    3115                 :            :                         break;
    3116                 :            :                 default:
    3117                 :          0 :                         continue;
    3118                 :            :                 }
    3119                 :            : 
    3120                 :          0 :                 err = vhd_parent_locator_write_at(child, ppath,
    3121                 :          0 :                                                   loc->data_offset,
    3122                 :            :                                                   loc->code, max, loc);
    3123         [ #  # ]:          0 :                 if (err) {
    3124         [ #  # ]:          0 :                         VHDLOG("error writing parent locator %d for %s: %d\n",
    3125                 :            :                                i, child->file, err);
    3126                 :            :                         goto out;
    3127                 :            :                 }
    3128                 :            :         }
    3129                 :            : 
    3130         [ #  # ]:          0 :         TEST_FAIL_AT(FAIL_REPARENT_LOCATOR);
    3131                 :            : 
    3132                 :          0 :         err = vhd_write_header(child, &child->header);
    3133 [ #  # ][ #  # ]:          0 :         if (err) {
    3134         [ #  # ]:          0 :                 VHDLOG("error writing header for %s: %d\n", child->file, err);
    3135                 :            :                 goto out;
    3136                 :            :         }
    3137                 :            : 
    3138                 :            :         err = 0;
    3139                 :            : 
    3140                 :            : out:
    3141                 :          0 :         return err;
    3142                 :            : }
    3143                 :            : 
    3144                 :            : static int
    3145                 :          0 : vhd_create_batmap(vhd_context_t *ctx)
    3146                 :            : {
    3147                 :            :         off64_t off;
    3148                 :            :         int err, map_bytes;
    3149                 :            :         vhd_batmap_header_t *header;
    3150                 :            :         void *map;
    3151                 :            : 
    3152         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx))
    3153                 :          0 :                 return -EINVAL;
    3154                 :            : 
    3155                 :          0 :         map_bytes = (ctx->header.max_bat_size + 7) >> 3;
    3156                 :          0 :         header    = &ctx->batmap.header;
    3157                 :            : 
    3158                 :            :         memset(header, 0, sizeof(vhd_batmap_header_t));
    3159                 :          0 :         memcpy(header->cookie, VHD_BATMAP_COOKIE, sizeof(header->cookie));
    3160                 :            : 
    3161                 :          0 :         err = vhd_batmap_header_offset(ctx, &off);
    3162         [ #  # ]:          0 :         if (err)
    3163                 :            :                 return err;
    3164                 :            : 
    3165                 :          0 :         header->batmap_offset  = off +
    3166                 :            :                 vhd_bytes_padded(sizeof(vhd_batmap_header_t));
    3167                 :          0 :         header->batmap_size    = secs_round_up_no_zero(map_bytes);
    3168                 :          0 :         header->batmap_version = VHD_BATMAP_CURRENT_VERSION;
    3169                 :            : 
    3170                 :          0 :         map_bytes = vhd_sectors_to_bytes(header->batmap_size);
    3171                 :            : 
    3172                 :          0 :         err = posix_memalign(&map, VHD_SECTOR_SIZE, map_bytes);
    3173         [ #  # ]:          0 :         if (err)
    3174                 :          0 :                 return -err;
    3175                 :            : 
    3176                 :          0 :         memset(map, 0, map_bytes);
    3177                 :          0 :         ctx->batmap.map = map;
    3178                 :            : 
    3179                 :          0 :         return vhd_write_batmap(ctx, &ctx->batmap);
    3180                 :            : }
    3181                 :            : 
    3182                 :            : static int
    3183                 :          0 : vhd_create_bat(vhd_context_t *ctx)
    3184                 :            : {
    3185                 :            :         int i, err;
    3186                 :            :         size_t size;
    3187                 :            :         void *bat;
    3188                 :            : 
    3189         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx))
    3190                 :          0 :                 return -EINVAL;
    3191                 :            : 
    3192                 :          0 :         size = vhd_bytes_padded(ctx->header.max_bat_size * sizeof(uint32_t));
    3193                 :          0 :         err  = posix_memalign(&bat, VHD_SECTOR_SIZE, size);
    3194         [ #  # ]:          0 :         if (err)
    3195                 :            :                 return err;
    3196                 :            : 
    3197                 :          0 :         ctx->bat.bat = bat;
    3198                 :            : 
    3199                 :          0 :         memset(ctx->bat.bat, 0, size);
    3200         [ #  # ]:          0 :         for (i = 0; i < ctx->header.max_bat_size; i++)
    3201                 :          0 :                 ctx->bat.bat[i] = DD_BLK_UNUSED;
    3202                 :            : 
    3203                 :          0 :         err = vhd_seek(ctx, ctx->header.table_offset, SEEK_SET);
    3204         [ #  # ]:          0 :         if (err)
    3205                 :            :                 return err;
    3206                 :            : 
    3207                 :          0 :         ctx->bat.entries = ctx->header.max_bat_size;
    3208                 :          0 :         ctx->bat.spb     = ctx->header.block_size >> VHD_SECTOR_SHIFT;
    3209                 :            : 
    3210                 :          0 :         return vhd_write_bat(ctx, &ctx->bat);
    3211                 :            : }
    3212                 :            : 
    3213                 :            : static int
    3214                 :          0 : vhd_initialize_fixed_disk(vhd_context_t *ctx)
    3215                 :            : {
    3216                 :            :         char *buf;
    3217                 :            :         int i, err;
    3218                 :            : 
    3219         [ #  # ]:          0 :         if (ctx->footer.type != HD_TYPE_FIXED)
    3220                 :            :                 return -EINVAL;
    3221                 :            : 
    3222                 :          0 :         err = vhd_seek(ctx, 0, SEEK_SET);
    3223         [ #  # ]:          0 :         if (err)
    3224                 :            :                 return err;
    3225                 :            : 
    3226                 :          0 :         buf = mmap(0, VHD_BLOCK_SIZE, PROT_READ,
    3227                 :            :                    MAP_SHARED | MAP_ANONYMOUS, -1, 0);
    3228         [ #  # ]:          0 :         if (buf == MAP_FAILED)
    3229                 :          0 :                 return -errno;
    3230                 :            : 
    3231         [ #  # ]:          0 :         for (i = 0; i < ctx->footer.curr_size >> VHD_BLOCK_SHIFT; i++) {
    3232                 :          0 :                 err = vhd_write(ctx, buf, VHD_BLOCK_SIZE);
    3233         [ #  # ]:          0 :                 if (err)
    3234                 :            :                         goto out;
    3235                 :            :         }
    3236                 :            : 
    3237                 :            :         err = 0;
    3238                 :            : 
    3239                 :            : out:
    3240                 :          0 :         munmap(buf, VHD_BLOCK_SIZE);
    3241                 :          0 :         return err;
    3242                 :            : }
    3243                 :            : 
    3244                 :            : int 
    3245                 :          0 : vhd_get_phys_size(vhd_context_t *ctx, off64_t *size)
    3246                 :            : {
    3247                 :            :         int err;
    3248                 :            : 
    3249 [ #  # ][ #  # ]:          0 :         if ((err = vhd_end_of_data(ctx, size)))
    3250                 :            :                 return err;
    3251                 :          0 :         *size += sizeof(vhd_footer_t);
    3252                 :          0 :         return 0;
    3253                 :            : }
    3254                 :            : 
    3255                 :            : int 
    3256                 :          0 : vhd_set_phys_size(vhd_context_t *ctx, off64_t size)
    3257                 :            : {
    3258                 :            :         off64_t phys_size;
    3259                 :            :         int err;
    3260                 :            : 
    3261                 :          0 :         err = vhd_get_phys_size(ctx, &phys_size);
    3262 [ #  # ][ #  # ]:          0 :         if (err)
    3263                 :          0 :                 return err;
    3264         [ #  # ]:          0 :         if (size < phys_size) {
    3265                 :            :                 // would result in data loss
    3266         [ #  # ]:          0 :                 VHDLOG("ERROR: new size (%"PRIu64") < phys size (%"PRIu64")\n",
    3267                 :            :                                 size, phys_size);
    3268                 :            :                 return -EINVAL;
    3269                 :            :         }
    3270                 :          0 :         return vhd_write_footer_at(ctx, &ctx->footer, 
    3271                 :          0 :                         size - sizeof(vhd_footer_t));
    3272                 :            : }
    3273                 :            : 
    3274                 :            : static int
    3275                 :          0 : vhd_set_virt_size_no_write(vhd_context_t *ctx, uint64_t size)
    3276                 :            : {
    3277         [ #  # ]:          0 :         if ((size >> VHD_BLOCK_SHIFT) > ctx->header.max_bat_size) {
    3278         [ #  # ]:          0 :                 VHDLOG("not enough metadata space reserved for fast "
    3279                 :            :                                 "resize (BAT size %u, need %"PRIu64")\n",
    3280                 :            :                                 ctx->header.max_bat_size, 
    3281                 :            :                                 size >> VHD_BLOCK_SHIFT);
    3282                 :            :                 return -EINVAL;
    3283                 :            :         }
    3284                 :            : 
    3285                 :            :         /* update footer */
    3286                 :          0 :         ctx->footer.curr_size = size;
    3287                 :          0 :         ctx->footer.geometry  = vhd_chs(ctx->footer.curr_size);
    3288                 :          0 :         ctx->footer.checksum  = vhd_checksum_footer(&ctx->footer);
    3289                 :          0 :         return 0;
    3290                 :            : }
    3291                 :            : 
    3292                 :            : int
    3293                 :          0 : vhd_set_virt_size(vhd_context_t *ctx, uint64_t size)
    3294                 :            : {
    3295                 :            :         int err;
    3296                 :            : 
    3297                 :          0 :         err = vhd_set_virt_size_no_write(ctx, size);
    3298         [ #  # ]:          0 :         if (err)
    3299                 :            :                 return err;
    3300                 :          0 :         return vhd_write_footer(ctx, &ctx->footer);
    3301                 :            : }
    3302                 :            : 
    3303                 :            : static int
    3304                 :          0 : __vhd_create(const char *name, const char *parent, uint64_t bytes, int type,
    3305                 :            :                 uint64_t mbytes, vhd_flag_creat_t flags)
    3306                 :            : {
    3307                 :            :         int err;
    3308                 :            :         off64_t off;
    3309                 :            :         vhd_context_t ctx;
    3310                 :            :         uint64_t size, psize, blks;
    3311                 :            : 
    3312      [ #  #  # ]:          0 :         switch (type) {
    3313                 :            :         case HD_TYPE_DIFF:
    3314         [ #  # ]:          0 :                 if (!parent)
    3315                 :          0 :                         return -EINVAL;
    3316                 :            :         case HD_TYPE_FIXED:
    3317                 :            :         case HD_TYPE_DYNAMIC:
    3318                 :            :                 break;
    3319                 :            :         default:
    3320                 :            :                 return -EINVAL;
    3321                 :            :         }
    3322                 :            : 
    3323         [ #  # ]:          0 :         if (strnlen(name, VHD_MAX_NAME_LEN - 1) == VHD_MAX_NAME_LEN - 1)
    3324                 :            :                 return -ENAMETOOLONG;
    3325                 :            : 
    3326 [ #  # ][ #  # ]:          0 :         if (bytes && mbytes && mbytes < bytes)
    3327                 :            :                 return -EINVAL;
    3328                 :            : 
    3329                 :            :         memset(&ctx, 0, sizeof(vhd_context_t));
    3330                 :          0 :         psize = 0;
    3331                 :          0 :         blks   = (bytes + VHD_BLOCK_SIZE - 1) >> VHD_BLOCK_SHIFT;
    3332                 :            :         /* If mbytes is provided (virtual-size-for-metadata-preallocation),
    3333                 :            :          * create the VHD of size mbytes, which will create the BAT & the 
    3334                 :            :          * batmap of the appropriate size. Once the BAT & batmap are 
    3335                 :            :          * initialized, reset the virtual size to the requested one.
    3336                 :            :          */
    3337         [ #  # ]:          0 :         if (mbytes)
    3338                 :          0 :                 blks = (mbytes + VHD_BLOCK_SIZE - 1) >> VHD_BLOCK_SHIFT;
    3339                 :          0 :         size = blks << VHD_BLOCK_SHIFT;
    3340                 :            : 
    3341                 :          0 :         ctx.fd = open_optional_odirect(name, O_WRONLY | O_CREAT |
    3342                 :            :                       O_TRUNC | O_LARGEFILE | O_DIRECT, 0644);
    3343         [ #  # ]:          0 :         if (ctx.fd == -1) {
    3344                 :          0 :         fprintf(stderr, "%s: failed to create: %d\n", name, -errno);
    3345                 :          0 :         return -errno;
    3346                 :            :     }
    3347                 :            : 
    3348                 :          0 :         ctx.file = strdup(name);
    3349         [ #  # ]:          0 :         if (!ctx.file) {
    3350                 :            :                 err = -ENOMEM;
    3351                 :            :                 goto out;
    3352                 :            :         }
    3353                 :            : 
    3354                 :          0 :         err = vhd_test_file_fixed(ctx.file, &ctx.is_block);
    3355         [ #  # ]:          0 :         if (err)
    3356                 :            :                 goto out;
    3357                 :            : 
    3358                 :          0 :         vhd_initialize_footer(&ctx, type, size);
    3359                 :            : 
    3360         [ #  # ]:          0 :         if (type == HD_TYPE_FIXED) {
    3361                 :          0 :                 err = vhd_initialize_fixed_disk(&ctx);
    3362         [ #  # ]:          0 :                 if (err)
    3363                 :            :                         goto out;
    3364                 :            :         } else {
    3365                 :          0 :                 int raw = vhd_flag_test(flags, VHD_FLAG_CREAT_PARENT_RAW);
    3366                 :          0 :                 err = vhd_initialize_header(&ctx, parent, size, raw, &psize);
    3367         [ #  # ]:          0 :                 if (err)
    3368                 :            :                         goto out;
    3369                 :            : 
    3370                 :          0 :                 err = vhd_create_batmap(&ctx);
    3371         [ #  # ]:          0 :                 if (err)
    3372                 :            :                         goto out;
    3373                 :            : 
    3374                 :          0 :                 err = vhd_create_bat(&ctx);
    3375         [ #  # ]:          0 :                 if (err)
    3376                 :            :                         goto out;
    3377                 :            : 
    3378         [ #  # ]:          0 :                 if (type == HD_TYPE_DIFF) {
    3379                 :          0 :                         err = vhd_write_parent_locators(&ctx, parent);
    3380         [ #  # ]:          0 :                         if (err)
    3381                 :            :                                 goto out;
    3382                 :            :                 }
    3383                 :            :         }
    3384                 :            : 
    3385         [ #  # ]:          0 :         if (mbytes) {
    3386                 :            :                 /* set the virtual size to the requested size */
    3387         [ #  # ]:          0 :                 if (bytes) {
    3388                 :          0 :                         blks = (bytes + VHD_BLOCK_SIZE - 1) >> VHD_BLOCK_SHIFT;
    3389                 :          0 :                         size = blks << VHD_BLOCK_SHIFT;
    3390                 :            : 
    3391                 :            :                 }
    3392                 :            :                 else {
    3393                 :          0 :                         size = psize;
    3394                 :            :                 }
    3395                 :          0 :                 ctx.footer.orig_size = size;
    3396                 :          0 :                 err = vhd_set_virt_size_no_write(&ctx, size);
    3397         [ #  # ]:          0 :                 if (err)
    3398                 :            :                         goto out;
    3399                 :            :         }
    3400                 :            : 
    3401         [ #  # ]:          0 :         if (type != HD_TYPE_FIXED) {
    3402                 :          0 :                 err = vhd_write_footer_at(&ctx, &ctx.footer, 0);
    3403         [ #  # ]:          0 :                 if (err)
    3404                 :            :                         goto out;
    3405                 :            : 
    3406                 :          0 :                 err = vhd_write_header_at(&ctx, &ctx.header, VHD_SECTOR_SIZE);
    3407         [ #  # ]:          0 :                 if (err)
    3408                 :            :                         goto out;
    3409                 :            :         }
    3410                 :            : 
    3411                 :          0 :         err = vhd_seek(&ctx, 0, SEEK_END);
    3412         [ #  # ]:          0 :         if (err)
    3413                 :            :                 goto out;
    3414                 :            : 
    3415                 :          0 :         off = vhd_position(&ctx);
    3416 [ #  # ][ #  # ]:          0 :         if (off == (off64_t)-1) {
    3417                 :          0 :                 err = -errno;
    3418                 :          0 :                 goto out;
    3419                 :            :         }
    3420                 :            : 
    3421         [ #  # ]:          0 :         if (ctx.is_block)
    3422                 :          0 :                 off -= sizeof(vhd_footer_t);
    3423                 :            : 
    3424                 :          0 :         err = vhd_write_footer_at(&ctx, &ctx.footer, off);
    3425         [ #  # ]:          0 :         if (err)
    3426                 :            :                 goto out;
    3427                 :            : 
    3428                 :          0 :         err = 0;
    3429                 :            : 
    3430                 :            : out:
    3431                 :          0 :         vhd_close(&ctx);
    3432 [ #  # ][ #  # ]:          0 :         if (err && !ctx.is_block)
    3433                 :          0 :                 unlink(name);
    3434                 :          0 :         return err;
    3435                 :            : }
    3436                 :            : 
    3437                 :            : int
    3438                 :          0 : vhd_create(const char *name, uint64_t bytes, int type, uint64_t mbytes,
    3439                 :            :                 vhd_flag_creat_t flags)
    3440                 :            : {
    3441                 :          0 :         return __vhd_create(name, NULL, bytes, type, mbytes, flags);
    3442                 :            : }
    3443                 :            : 
    3444                 :            : int
    3445                 :          0 : vhd_snapshot(const char *name, uint64_t bytes, const char *parent,
    3446                 :            :                 uint64_t mbytes, vhd_flag_creat_t flags)
    3447                 :            : {
    3448                 :          0 :         return __vhd_create(name, parent, bytes, HD_TYPE_DIFF, mbytes, flags);
    3449                 :            : }
    3450                 :            : 
    3451                 :            : static int
    3452                 :          0 : __vhd_io_fixed_read(vhd_context_t *ctx,
    3453                 :            :                     char *buf, uint64_t sec, uint32_t secs)
    3454                 :            : {
    3455                 :            :         int err;
    3456                 :            : 
    3457                 :          0 :         err = vhd_seek(ctx, vhd_sectors_to_bytes(sec), SEEK_SET);
    3458         [ #  # ]:          0 :         if (err)
    3459                 :            :                 return err;
    3460                 :            : 
    3461                 :          0 :         return vhd_read(ctx, buf, vhd_sectors_to_bytes(secs));
    3462                 :            : }
    3463                 :            : 
    3464                 :            : static void
    3465                 :          0 : __vhd_io_dynamic_copy_data(vhd_context_t *ctx,
    3466                 :            :                            char *map, int map_off,
    3467                 :            :                            char *bitmap, int bitmap_off,
    3468                 :            :                            char *dst, char *src, int secs)
    3469                 :            : {
    3470                 :            :         int i;
    3471                 :            : 
    3472         [ #  # ]:          0 :         for (i = 0; i < secs; i++) {
    3473         [ #  # ]:          0 :                 if (test_bit(map, map_off + i))
    3474                 :            :                         goto next;
    3475                 :            : 
    3476         [ #  # ]:          0 :                 if (ctx && !vhd_bitmap_test(ctx, bitmap, bitmap_off + i))
              [ #  #  # ]
    3477                 :            :                         goto next;
    3478                 :            : 
    3479                 :            :                 memcpy(dst, src, VHD_SECTOR_SIZE);
    3480                 :          0 :                 set_bit(map, map_off + i);
    3481                 :            : 
    3482                 :            :         next:
    3483                 :          0 :                 src += VHD_SECTOR_SIZE;
    3484                 :          0 :                 dst += VHD_SECTOR_SIZE;
    3485                 :            :         }
    3486                 :          0 : }
    3487                 :            : 
    3488                 :            : static int
    3489                 :          0 : __vhd_io_dynamic_read_link(vhd_context_t *ctx, char *map,
    3490                 :            :                            char *buf, uint64_t sector, uint32_t secs)
    3491                 :            : {
    3492                 :            :         off64_t off;
    3493                 :            :         uint32_t blk, sec;
    3494                 :            :         int err, cnt, map_off, i;
    3495                 :            :         char *bitmap, *data, *src;
    3496                 :            : 
    3497                 :          0 :         map_off = 0;
    3498                 :            : 
    3499                 :            :         do {
    3500                 :          0 :                 data   = NULL;
    3501                 :          0 :                 bitmap = NULL;
    3502         [ #  # ]:          0 :                 if (sector >= ctx->footer.curr_size >> VHD_SECTOR_SHIFT) {
    3503                 :          0 :                         cnt = secs;
    3504         [ #  # ]:          0 :                         for (i = 0; i < cnt; i++)
    3505                 :          0 :                                 set_bit(map, map_off + i);
    3506                 :            :                         /* buf has already been zeroed out */
    3507                 :            :                         goto next;
    3508                 :            :                 }
    3509                 :            : 
    3510                 :          0 :                 blk = sector / ctx->spb;
    3511                 :          0 :                 sec = sector % ctx->spb;
    3512                 :          0 :                 cnt = MIN(secs, ctx->spb - sec);
    3513                 :          0 :                 off = ctx->bat.bat[blk];
    3514                 :            : 
    3515         [ #  # ]:          0 :                 if (off == DD_BLK_UNUSED)
    3516                 :            :                         goto next;
    3517                 :            : 
    3518                 :          0 :                 err = vhd_read_bitmap(ctx, blk, &bitmap);
    3519         [ #  # ]:          0 :                 if (err)
    3520                 :          0 :                         return err;
    3521                 :            : 
    3522                 :          0 :                 err = vhd_read_block(ctx, blk, &data);
    3523         [ #  # ]:          0 :                 if (err) {
    3524                 :          0 :                         free(bitmap);
    3525                 :          0 :                         return err;
    3526                 :            :                 }
    3527                 :            : 
    3528                 :          0 :                 src = data + vhd_sectors_to_bytes(sec);
    3529                 :            : 
    3530                 :          0 :                 __vhd_io_dynamic_copy_data(ctx,
    3531                 :            :                                            map, map_off,
    3532                 :            :                                            bitmap, sec,
    3533                 :            :                                            buf, src, cnt);
    3534                 :            : 
    3535                 :            :         next:
    3536                 :          0 :                 free(data);
    3537                 :          0 :                 free(bitmap);
    3538                 :            : 
    3539                 :          0 :                 secs    -= cnt;
    3540                 :          0 :                 sector  += cnt;
    3541                 :          0 :                 map_off += cnt;
    3542                 :          0 :                 buf     += vhd_sectors_to_bytes(cnt);
    3543                 :            : 
    3544         [ #  # ]:          0 :         } while (secs);
    3545                 :            : 
    3546                 :            :         return 0;
    3547                 :            : }
    3548                 :            : 
    3549                 :            : static int
    3550                 :          0 : __raw_read_link(char *filename,
    3551                 :            :                 char *map, char *buf, uint64_t sec, uint32_t secs)
    3552                 :            : {
    3553                 :            :         int fd, err;
    3554                 :            :         off64_t off;
    3555                 :            :         uint64_t size;
    3556                 :            :         void *data;
    3557                 :            : 
    3558                 :          0 :         err = 0;
    3559                 :          0 :         errno = 0;
    3560                 :          0 :         fd = open_optional_odirect(filename, O_RDONLY | O_DIRECT | O_LARGEFILE);
    3561         [ #  # ]:          0 :         if (fd == -1) {
    3562         [ #  # ]:          0 :                 VHDLOG("%s: failed to open: %d\n", filename, -errno);
    3563                 :          0 :                 return -errno;
    3564                 :            :         }
    3565                 :            : 
    3566                 :          0 :         off = lseek64(fd, vhd_sectors_to_bytes(sec), SEEK_SET);
    3567         [ #  # ]:          0 :         if (off == (off64_t)-1) {
    3568         [ #  # ]:          0 :                 VHDLOG("%s: seek(0x%08"PRIx64") failed: %d\n",
    3569                 :            :                        filename, vhd_sectors_to_bytes(sec), -errno);
    3570                 :          0 :                 err = -errno;
    3571                 :          0 :                 goto close;
    3572                 :            :         }
    3573                 :            : 
    3574                 :          0 :         size = vhd_sectors_to_bytes(secs);
    3575                 :          0 :         err = posix_memalign(&data, VHD_SECTOR_SIZE, size);
    3576         [ #  # ]:          0 :         if (err)
    3577                 :            :                 goto close;
    3578                 :            : 
    3579                 :          0 :         err = read(fd, data, size);
    3580         [ #  # ]:          0 :         if (err != size) {
    3581         [ #  # ]:          0 :                 VHDLOG("%s: reading of %"PRIu64" returned %d, errno: %d\n",
    3582                 :            :                                 filename, size, err, -errno);
    3583                 :          0 :                 free(data);
    3584         [ #  # ]:          0 :                 err = errno ? -errno : -EIO;
    3585                 :          0 :                 goto close;
    3586                 :            :         }
    3587                 :          0 :         __vhd_io_dynamic_copy_data(NULL, map, 0, NULL, 0, buf, data, secs);
    3588                 :          0 :         free(data);
    3589                 :          0 :         err = 0;
    3590                 :            : 
    3591                 :            : close:
    3592                 :          0 :         close(fd);
    3593                 :            :         return err;
    3594                 :            : }
    3595                 :            : 
    3596                 :            : static int
    3597                 :          0 : __vhd_io_dynamic_read(vhd_context_t *ctx,
    3598                 :            :                       char *buf, uint64_t sec, uint32_t secs)
    3599                 :            : {
    3600                 :            :         int err;
    3601                 :            :         uint32_t i, done;
    3602                 :            :         char *map, *next;
    3603                 :            :         vhd_context_t parent, *vhd;
    3604                 :            : 
    3605                 :          0 :         err  = vhd_get_bat(ctx);
    3606         [ #  # ]:          0 :         if (err)
    3607                 :          0 :                 return err;
    3608                 :            : 
    3609                 :          0 :         vhd  = ctx;
    3610                 :          0 :         next = NULL;
    3611                 :          0 :         map  = calloc(1, secs << (VHD_SECTOR_SHIFT - 3));
    3612         [ #  # ]:          0 :         if (!map)
    3613                 :            :                 return -ENOMEM;
    3614                 :            : 
    3615                 :          0 :         memset(buf, 0, vhd_sectors_to_bytes(secs));
    3616                 :            : 
    3617                 :            :         for (;;) {
    3618                 :          0 :                 err = __vhd_io_dynamic_read_link(vhd, map, buf, sec, secs);
    3619         [ #  # ]:          0 :                 if (err)
    3620                 :            :                         goto close;
    3621                 :            : 
    3622         [ #  # ]:          0 :                 for (done = 0, i = 0; i < secs; i++)
    3623         [ #  # ]:          0 :                         if (test_bit(map, i))
    3624                 :          0 :                                 done++;
    3625                 :            : 
    3626         [ #  # ]:          0 :                 if (done == secs) {
    3627                 :            :                         err = 0;
    3628                 :            :                         goto close;
    3629                 :            :                 }
    3630                 :            : 
    3631         [ #  # ]:          0 :                 if (vhd->footer.type == HD_TYPE_DIFF) {
    3632                 :            :                         vhd_context_t *p;
    3633                 :          0 :                         p = vhd_cache_get_parent(vhd);
    3634         [ #  # ]:          0 :                         if (p) {
    3635                 :          0 :                                 vhd = p;
    3636                 :          0 :                                 err = vhd_get_bat(vhd);
    3637         [ #  # ]:          0 :                                 if (err)
    3638                 :            :                                         goto out;
    3639                 :          0 :                                 continue;
    3640                 :            :                         }
    3641                 :            : 
    3642                 :          0 :                         err = vhd_parent_locator_get(vhd, &next);
    3643         [ #  # ]:          0 :                         if (err)
    3644                 :            :                                 goto close;
    3645         [ #  # ]:          0 :                         if (vhd_parent_raw(vhd)) {
    3646                 :          0 :                                 err = __raw_read_link(next, map, buf, sec,
    3647                 :            :                                                 secs);
    3648                 :            :                                 goto close;
    3649                 :            :                         }
    3650                 :            :                 } else {
    3651                 :            :                         err = 0;
    3652                 :            :                         goto close;
    3653                 :            :                 }
    3654                 :            : 
    3655         [ #  # ]:          0 :                 if (vhd != ctx)
    3656                 :          0 :                         vhd_close(vhd);
    3657                 :          0 :                 vhd = &parent;
    3658                 :            : 
    3659                 :          0 :                 err = vhd_open(vhd, next, VHD_OPEN_RDONLY);
    3660         [ #  # ]:          0 :                 if (err)
    3661                 :            :                         goto out;
    3662                 :            : 
    3663                 :          0 :                 err = vhd_get_bat(vhd);
    3664         [ #  # ]:          0 :                 if (err)
    3665                 :            :                         goto close;
    3666                 :            : 
    3667                 :          0 :                 free(next);
    3668                 :          0 :                 next = NULL;
    3669                 :            :         }
    3670                 :            : 
    3671                 :            : close:
    3672 [ #  # ][ #  # ]:          0 :         if (vhd != ctx && !vhd_flag_test(vhd->oflags, VHD_OPEN_CACHED))
    3673                 :          0 :                 vhd_close(vhd);
    3674                 :            : out:
    3675                 :          0 :         free(map);
    3676                 :          0 :         free(next);
    3677                 :          0 :         return err;
    3678                 :            : }
    3679                 :            : 
    3680                 :            : int
    3681                 :          0 : vhd_io_read(vhd_context_t *ctx, char *buf, uint64_t sec, uint32_t secs)
    3682                 :            : {
    3683         [ #  # ]:          0 :         if (vhd_sectors_to_bytes(sec + secs) > ctx->footer.curr_size)
    3684                 :            :                 return -ERANGE;
    3685                 :            : 
    3686         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx))
    3687                 :          0 :                 return __vhd_io_fixed_read(ctx, buf, sec, secs);
    3688                 :            : 
    3689                 :          0 :         return __vhd_io_dynamic_read(ctx, buf, sec, secs);
    3690                 :            : }
    3691                 :            : 
    3692                 :            : static int
    3693                 :          0 : __vhd_io_fixed_write(vhd_context_t *ctx,
    3694                 :            :                      char *buf, uint64_t sec, uint32_t secs)
    3695                 :            : {
    3696                 :            :         int err;
    3697                 :            : 
    3698                 :          0 :         err = vhd_seek(ctx, vhd_sectors_to_bytes(sec), SEEK_SET);
    3699         [ #  # ]:          0 :         if (err)
    3700                 :            :                 return err;
    3701                 :            : 
    3702                 :          0 :         return vhd_write(ctx, buf, vhd_sectors_to_bytes(secs));
    3703                 :            : }
    3704                 :            : 
    3705                 :            : static int
    3706                 :          0 : __vhd_io_allocate_block(vhd_context_t *ctx, uint32_t block, bool zero)
    3707                 :            : {
    3708                 :          0 :         char *buf = NULL;
    3709                 :            :         size_t size;
    3710                 :            :         off64_t off, max;
    3711                 :            :         int err, gap, spp, secs;
    3712                 :            : 
    3713                 :          0 :         spp = getpagesize() >> VHD_SECTOR_SHIFT;
    3714                 :            : 
    3715                 :          0 :         err = vhd_end_of_data(ctx, &max);
    3716         [ #  # ]:          0 :         if (err)
    3717                 :          0 :                 return err;
    3718                 :            : 
    3719                 :          0 :         gap   = 0;
    3720                 :          0 :         off   = max;
    3721                 :          0 :         max >>= VHD_SECTOR_SHIFT;
    3722                 :            : 
    3723                 :            :         /* data region of segment should begin on page boundary */
    3724         [ #  # ]:          0 :         if ((max + ctx->bm_secs) % spp) {
    3725                 :          0 :                 gap  = (spp - ((max + ctx->bm_secs) % spp));
    3726                 :          0 :                 max += gap;
    3727                 :            :         }
    3728                 :            : 
    3729         [ #  # ]:          0 :         if (max > UINT32_MAX)
    3730                 :            :                 return -EIO;
    3731                 :            : 
    3732                 :            :         /*
    3733                 :            :          * This seek is deliberately left here and not moved inside
    3734                 :            :          * the if (zero) {} block because if it is impossible to
    3735                 :            :          * seek to the block start we want the block allocation
    3736                 :            :          * to fail early.
    3737                 :            :          */
    3738                 :          0 :         err = vhd_seek(ctx, off, SEEK_SET);
    3739         [ #  # ]:          0 :         if (err)
    3740                 :            :                 return err;
    3741                 :            : 
    3742                 :          0 :         secs = ctx->bm_secs + gap;
    3743         [ #  # ]:          0 :         if (!vhd_flag_test(ctx->oflags, VHD_OPEN_IO_WRITE_SPARSE))
    3744                 :          0 :                 secs += ctx->spb;
    3745                 :            : 
    3746                 :            :         /* Fill the block with zeros if requested to do so */
    3747         [ #  # ]:          0 :         if (zero) {
    3748                 :          0 :                 size = vhd_sectors_to_bytes(secs);
    3749                 :          0 :                 buf  = mmap(0, size, PROT_READ, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
    3750         [ #  # ]:          0 :                 if (buf == MAP_FAILED)
    3751                 :          0 :                         return -errno;
    3752                 :            : 
    3753                 :          0 :                 err = vhd_write(ctx, buf, size);
    3754                 :          0 :                 munmap(buf, size);
    3755         [ #  # ]:          0 :                 if (err)
    3756                 :            :                         goto out;
    3757                 :            :         }
    3758                 :            : 
    3759                 :          0 :         ctx->bat.bat[block] = max;
    3760                 :          0 :         err = vhd_write_bat(ctx, &ctx->bat);
    3761         [ #  # ]:          0 :         if (err)
    3762                 :            :                 goto out;
    3763                 :            : 
    3764                 :          0 :         err = 0;
    3765                 :            : 
    3766                 :            : out:
    3767                 :          0 :         return err;
    3768                 :            : }
    3769                 :            : 
    3770                 :            : static int
    3771                 :          0 : __vhd_io_dynamic_write(vhd_context_t *ctx,
    3772                 :            :                        char *buf, uint64_t sector, uint32_t secs)
    3773                 :            : {
    3774                 :            :         char *map;
    3775                 :            :         off64_t off;
    3776                 :            :         uint32_t blk, sec;
    3777                 :            :         int i, err, cnt, ret;
    3778                 :            : 
    3779         [ #  # ]:          0 :         if (vhd_sectors_to_bytes(sector + secs) > ctx->footer.curr_size)
    3780                 :          0 :                 return -ERANGE;
    3781                 :            : 
    3782                 :          0 :         err = vhd_get_bat(ctx);
    3783         [ #  # ]:          0 :         if (err)
    3784                 :            :                 return err;
    3785                 :            : 
    3786         [ #  # ]:          0 :         if (vhd_has_batmap(ctx)) {
    3787                 :          0 :                 err = vhd_get_batmap(ctx);
    3788         [ #  # ]:          0 :                 if (err)
    3789                 :            :                         return err;
    3790                 :            :         }
    3791                 :            : 
    3792                 :            :         do {
    3793                 :          0 :                 blk = sector / ctx->spb;
    3794                 :          0 :                 sec = sector % ctx->spb;
    3795                 :            : 
    3796                 :          0 :                 cnt = MIN(secs, ctx->spb - sec);
    3797                 :          0 :                 off = ctx->bat.bat[blk];
    3798         [ #  # ]:          0 :                 if (off == DD_BLK_UNUSED) {
    3799                 :            :                         /*
    3800                 :            :                          * When allocating a block, fill it with zeroes unless we
    3801                 :            :                          * are about to write the entire block, in which case
    3802                 :            :                          * don't bother.
    3803                 :            :                          */
    3804                 :          0 :                         err = __vhd_io_allocate_block(ctx, blk, (cnt < ctx->spb)?true:false);
    3805         [ #  # ]:          0 :                         if (err)
    3806                 :            :                                 return err;
    3807                 :            : 
    3808                 :          0 :                         off = ctx->bat.bat[blk];
    3809                 :            :                 }
    3810                 :            : 
    3811                 :            :                 /*
    3812                 :            :                  * If we allocated a new block above this seek should not fail,
    3813                 :            :                  * because it already succeeded once during the block allocation
    3814                 :            :                  * even if we requested not to zero out the new allocation.
    3815                 :            :                  * Thus we will get to the write and make sure to overwrite the
    3816                 :            :                  * pre-existing contents, avoiding data leak.
    3817                 :            :                  */
    3818                 :          0 :                 off += ctx->bm_secs + sec;
    3819                 :          0 :                 err  = vhd_seek(ctx, vhd_sectors_to_bytes(off), SEEK_SET);
    3820         [ #  # ]:          0 :                 if (err)
    3821                 :            :                         return err;
    3822                 :            : 
    3823                 :          0 :                 err = vhd_write(ctx, buf, vhd_sectors_to_bytes(cnt));
    3824         [ #  # ]:          0 :                 if (err)
    3825                 :            :                         return err;
    3826                 :            : 
    3827   [ #  #  #  # ]:          0 :                 if (vhd_has_batmap(ctx) &&
    3828                 :          0 :                     vhd_batmap_test(ctx, &ctx->batmap, blk))
    3829                 :            :                         goto next;
    3830                 :            : 
    3831                 :          0 :                 err = vhd_read_bitmap(ctx, blk, &map);
    3832         [ #  # ]:          0 :                 if (err)
    3833                 :            :                         return err;
    3834                 :            : 
    3835 [ #  # ][ #  # ]:          0 :                 for (i = 0; i < cnt; i++)
    3836                 :          0 :                         vhd_bitmap_set(ctx, map, sec + i);
    3837                 :            : 
    3838                 :          0 :                 err = vhd_write_bitmap(ctx, blk, map);
    3839         [ #  # ]:          0 :                 if (err)
    3840                 :            :                         goto fail;
    3841                 :            : 
    3842         [ #  # ]:          0 :                 if (vhd_has_batmap(ctx)) {
    3843         [ #  # ]:          0 :                         for (i = 0; i < ctx->spb; i++)
    3844      [ #  #  # ]:          0 :                                 if (!vhd_bitmap_test(ctx, map, i)) {
    3845                 :          0 :                                         free(map);
    3846                 :          0 :                                         goto next;
    3847                 :            :                                 }
    3848                 :            : 
    3849                 :          0 :                         vhd_batmap_set(ctx, &ctx->batmap, blk);
    3850                 :          0 :                         err = vhd_write_batmap(ctx, &ctx->batmap);
    3851         [ #  # ]:          0 :                         if (err)
    3852                 :            :                                 goto fail;
    3853                 :            :                 }
    3854                 :            : 
    3855                 :          0 :                 free(map);
    3856                 :          0 :                 map = NULL;
    3857                 :            : 
    3858                 :            :         next:
    3859                 :          0 :                 secs   -= cnt;
    3860                 :          0 :                 sector += cnt;
    3861                 :          0 :                 buf    += vhd_sectors_to_bytes(cnt);
    3862         [ #  # ]:          0 :         } while (secs);
    3863                 :            : 
    3864                 :            :         err = 0;
    3865                 :            : 
    3866                 :            : out:
    3867                 :          0 :         ret = vhd_write_footer(ctx, &ctx->footer);
    3868         [ #  # ]:          0 :         return (err ? err : ret);
    3869                 :            : 
    3870                 :            : fail:
    3871                 :          0 :         free(map);
    3872                 :          0 :         goto out;
    3873                 :            : }
    3874                 :            : 
    3875                 :            : int
    3876                 :          0 : vhd_io_write(vhd_context_t *ctx, char *buf, uint64_t sec, uint32_t secs)
    3877                 :            : {
    3878         [ #  # ]:          0 :         if (vhd_sectors_to_bytes(sec + secs) > ctx->footer.curr_size)
    3879                 :            :                 return -ERANGE;
    3880                 :            : 
    3881         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx))
    3882                 :          0 :                 return __vhd_io_fixed_write(ctx, buf, sec, secs);
    3883                 :            : 
    3884                 :          0 :         return __vhd_io_dynamic_write(ctx, buf, sec, secs);
    3885                 :            : }
    3886                 :            : 
    3887                 :            : static void
    3888                 :            : vhd_cache_init(vhd_context_t *ctx)
    3889                 :            : {
    3890                 :          0 :         INIT_LIST_HEAD(&ctx->next);
    3891                 :            : }
    3892                 :            : 
    3893                 :            : static int
    3894                 :            : vhd_cache_enabled(vhd_context_t *ctx)
    3895                 :            : {
    3896                 :          0 :         return vhd_flag_test(ctx->oflags, VHD_OPEN_CACHED) &&
    3897                 :            :         !vhd_flag_test(ctx->oflags, VHD_OPEN_RDWR);
    3898                 :            : }
    3899                 :            : 
    3900                 :            : static int
    3901                 :          0 : vhd_cache_load(vhd_context_t *ctx)
    3902                 :            : {
    3903                 :            :         char *next;
    3904                 :            :         int err, pflags, fd_flags;
    3905                 :            :         int fcntl_res __attribute__((unused));
    3906                 :            :         vhd_context_t *vhd;
    3907                 :            : 
    3908                 :          0 :         pflags = ctx->oflags;
    3909                 :          0 :         vhd    = ctx;
    3910                 :          0 :         next   = NULL;
    3911                 :            : 
    3912                 :          0 :         vhd_flag_set(pflags, VHD_OPEN_RDONLY);
    3913                 :          0 :         vhd_flag_clear(pflags, VHD_OPEN_CACHED);
    3914                 :            : 
    3915         [ #  # ]:          0 :         if (!vhd_cache_enabled(vhd))
    3916                 :            :                 goto done;
    3917                 :            : 
    3918         [ #  # ]:          0 :         while (vhd->footer.type == HD_TYPE_DIFF) {
    3919                 :            :                 vhd_context_t *parent;
    3920                 :            : 
    3921                 :          0 :                 parent = NULL;
    3922                 :            : 
    3923         [ #  # ]:          0 :                 if (vhd_parent_raw(vhd))
    3924                 :            :                         goto done;
    3925                 :            : 
    3926                 :          0 :                 err = vhd_parent_locator_get(vhd, &next);
    3927         [ #  # ]:          0 :                 if (err) {
    3928         [ #  # ]:          0 :                         VHDLOG("%s: error reading parent locator for %s: %d\n",
    3929                 :            :                                ctx->file, vhd->file, err);
    3930                 :            :                         goto out;
    3931                 :            :                 }
    3932                 :            : 
    3933                 :          0 :                 parent = calloc(1, sizeof(*parent));
    3934         [ #  # ]:          0 :                 if (!parent)
    3935                 :            :                         goto out;
    3936                 :            : 
    3937                 :          0 :                 err = vhd_open(parent, next, pflags);
    3938         [ #  # ]:          0 :                 if (err) {
    3939         [ #  # ]:          0 :                         VHDLOG("%s: vhd_open failed: %d\n", next, err);
    3940                 :          0 :                         free(parent);
    3941                 :          0 :                         goto out;
    3942                 :            :                 }
    3943                 :            : 
    3944                 :          0 :                 fd_flags = fcntl(parent->fd, F_GETFL);
    3945         [ #  # ]:          0 :                 if (fd_flags > 0)
    3946                 :          0 :                         fcntl_res = fcntl(parent->fd, F_SETFL, fd_flags & ~O_DIRECT);
    3947                 :          0 :                 vhd_flag_set(parent->oflags, VHD_OPEN_CACHED);
    3948                 :          0 :                 list_add(&parent->next, &vhd->next);
    3949                 :            : 
    3950                 :          0 :                 free(next);
    3951                 :          0 :                 next = NULL;
    3952                 :          0 :                 vhd  = parent;
    3953                 :            :         }
    3954                 :            : 
    3955                 :            : done:
    3956                 :            :         err = 0;
    3957                 :            : out:
    3958                 :          0 :         free(next);
    3959         [ #  # ]:          0 :         if (err)
    3960                 :          0 :                 vhd_cache_unload(vhd);
    3961                 :            : 
    3962                 :          0 :         return err;
    3963                 :            : }
    3964                 :            : 
    3965                 :            : static int
    3966                 :          0 : vhd_cache_unload(vhd_context_t *ctx)
    3967                 :            : {
    3968                 :            :         vhd_context_t *vhd, *tmp;
    3969                 :            : 
    3970         [ #  # ]:          0 :         if (!vhd_cache_enabled(ctx))
    3971                 :            :                 goto out;
    3972                 :            : 
    3973         [ #  # ]:          0 :         list_for_each_entry_safe(vhd, tmp, &ctx->next, next) {
    3974                 :          0 :                 list_del_init(&vhd->next);
    3975                 :          0 :                 vhd_close(vhd);
    3976                 :          0 :                 free(vhd);
    3977                 :            :         }
    3978                 :            : 
    3979                 :          0 :         INIT_LIST_HEAD(&ctx->next);
    3980                 :            : 
    3981                 :            : out:
    3982                 :          0 :         return 0;
    3983                 :            : }
    3984                 :            : 
    3985                 :            : static vhd_context_t *
    3986                 :            : vhd_cache_get_parent(vhd_context_t *ctx)
    3987                 :            : {
    3988                 :            :         vhd_context_t *vhd;
    3989                 :            : 
    3990                 :          0 :         vhd = NULL;
    3991                 :            : 
    3992 [ #  # ][ #  # ]:          0 :         if (!vhd_cache_enabled(ctx))
    3993                 :            :                 goto out;
    3994                 :            : 
    3995 [ #  # ][ #  # ]:          0 :         if (list_empty(&ctx->next))
    3996                 :            :                 goto out;
    3997                 :            : 
    3998                 :          0 :         vhd = list_entry(ctx->next.next, vhd_context_t, next);
    3999                 :            : 
    4000                 :            : out:
    4001                 :            :         return vhd;
    4002                 :            : }
    4003                 :            : 
    4004                 :            : typedef struct vhd_block_vector vhd_block_vector_t;
    4005                 :            : typedef struct vhd_block_vector_entry vhd_block_vector_entry_t;
    4006                 :            : 
    4007                 :            : struct vhd_block_vector_entry {
    4008                 :            :         uint64_t                   off;       /* byte offset from block */
    4009                 :            :         uint32_t                   bytes;     /* size in bytes */
    4010                 :            :         char                      *buf;       /* destination buffer */
    4011                 :            : };
    4012                 :            : 
    4013                 :            : struct vhd_block_vector {
    4014                 :            :         uint32_t                   block;     /* logical block in vhd */
    4015                 :            :         int                        entries;   /* number of vector entries */
    4016                 :            :         vhd_block_vector_entry_t  *array;     /* vector list */
    4017                 :            : };
    4018                 :            : 
    4019                 :            : /**
    4020                 :            :  * @param[in]           ctx             VHD context
    4021                 :            :  * @param[in,out]       vec             block vector describing read
    4022                 :            :  *
    4023                 :            :  * @return 0 on success, -errno on error
    4024                 :            :  * 
    4025                 :            :  * \p vec describes a list of byte-spans within a given block
    4026                 :            :  * and a corresponding list of destination buffers.
    4027                 :            :  */
    4028                 :            : static int
    4029                 :          0 : vhd_block_vector_read(vhd_context_t *ctx, vhd_block_vector_t *vec)
    4030                 :            : {
    4031                 :            :         int err, i;
    4032                 :            :         off64_t off;
    4033                 :            :         uint32_t blk;
    4034                 :            : 
    4035                 :          0 :         err = vhd_get_bat(ctx);
    4036         [ #  # ]:          0 :         if (err)
    4037                 :            :                 goto out;
    4038                 :            : 
    4039         [ #  # ]:          0 :         if (vec->block >= ctx->bat.entries) {
    4040                 :            :                 err = -ERANGE;
    4041                 :            :                 goto out;
    4042                 :            :         }
    4043                 :            : 
    4044                 :          0 :         blk = ctx->bat.bat[vec->block];
    4045         [ #  # ]:          0 :         if (blk == DD_BLK_UNUSED) {
    4046                 :            :                 err = -EINVAL;
    4047                 :            :                 goto out;
    4048                 :            :         }
    4049                 :            : 
    4050                 :          0 :         off = vhd_sectors_to_bytes(blk + ctx->bm_secs);
    4051                 :            : 
    4052         [ #  # ]:          0 :         for (i = 0; i < vec->entries; i++) {
    4053                 :          0 :                 vhd_block_vector_entry_t *v = vec->array + i;
    4054                 :          0 :                 err = vhd_pread(ctx, v->buf, v->bytes, off + v->off);
    4055         [ #  # ]:          0 :                 if (err)
    4056                 :            :                         goto out;
    4057                 :            :         }
    4058                 :            : 
    4059                 :            : out:
    4060                 :          0 :         return err;
    4061                 :            : }
    4062                 :            : 
    4063                 :            : /**
    4064                 :            :  * @param[in]   ctx                     VHD context
    4065                 :            :  * @param[out]  vec             block vector to initialize
    4066                 :            :  * @param[in]   block           vhd block number
    4067                 :            :  * @param[in]   map                     optional bitmap of sectors to map (relative to beginning of block)
    4068                 :            :  * @param[out]  buf                     destination buffer
    4069                 :            :  * @param[in]   blk_start       byte offset relative to beginning of block
    4070                 :            :  * @param[in]   blk_end         byte offset relative to beginning of block
    4071                 :            :  *
    4072                 :            :  * @return 0 on success, -errno on error
    4073                 :            :  * 
    4074                 :            :  * Initializes \p vec to describe a read into a contiguous buffer
    4075                 :            :  * of potentially non-contiguous byte ranges in a given vhd block.
    4076                 :            :  * 
    4077                 :            :  * Only sectors with corresponding bits set in \p map (if it is not NULL)
    4078                 :            :  * will be mapped; bits corresponding to unmapped sectors will be cleared.
    4079                 :            :  * 
    4080                 :            :  * First and last sector maps may be smaller than vhd sector size.
    4081                 :            :  */
    4082                 :            : static int
    4083                 :          0 : vhd_block_vector_init(vhd_context_t *ctx,
    4084                 :            :                       vhd_block_vector_t *vec, uint32_t block, char *map,
    4085                 :            :                       char *buf, uint64_t blk_start, uint64_t blk_end)
    4086                 :            : {
    4087                 :            :         int err, sec;
    4088                 :            :         char *bitmap;
    4089                 :            :         uint32_t first_sec, last_sec;
    4090                 :            : 
    4091                 :          0 :         bitmap = NULL;
    4092                 :            :         memset(vec, 0, sizeof(*vec));
    4093                 :            : 
    4094                 :          0 :         first_sec = blk_start >> VHD_SECTOR_SHIFT;
    4095                 :          0 :         last_sec  = secs_round_up_no_zero(blk_end);
    4096                 :            : 
    4097                 :          0 :         err = vhd_read_bitmap(ctx, block, &bitmap);
    4098         [ #  # ]:          0 :         if (err)
    4099                 :            :                 goto out;
    4100                 :            : 
    4101                 :          0 :         vec->array = calloc(ctx->spb, sizeof(vhd_block_vector_entry_t));
    4102         [ #  # ]:          0 :         if (!vec->array) {
    4103                 :            :                 err = -ENOMEM;
    4104                 :            :                 goto out;
    4105                 :            :         }
    4106                 :            : 
    4107         [ #  # ]:          0 :         for (sec = first_sec; sec < last_sec; sec++) {
    4108                 :            :                 uint32_t cnt;
    4109                 :            :                 vhd_block_vector_entry_t *v;
    4110                 :            : 
    4111                 :          0 :                 cnt = VHD_SECTOR_SIZE - (blk_start & (VHD_SECTOR_SIZE - 1));
    4112         [ #  # ]:          0 :                 if (cnt > blk_end - blk_start)
    4113                 :          0 :                         cnt = blk_end - blk_start;
    4114                 :            : 
    4115 [ #  # ][ #  # ]:          0 :                 if (map && !test_bit(map, sec))
    4116                 :            :                         goto next;
    4117                 :            : 
    4118      [ #  #  # ]:          0 :                 if (vhd_bitmap_test(ctx, bitmap, sec)) {
    4119         [ #  # ]:          0 :                         if (vec->entries > 0) {
    4120                 :          0 :                                 v = vec->array + vec->entries - 1;
    4121         [ #  # ]:          0 :                                 if (v->off + v->bytes == blk_start) {
    4122                 :          0 :                                         v->bytes += cnt;
    4123                 :          0 :                                         goto next;
    4124                 :            :                                 }
    4125                 :            :                         }
    4126                 :            : 
    4127                 :          0 :                         v        = vec->array + vec->entries;
    4128                 :          0 :                         v->off   = blk_start;
    4129                 :          0 :                         v->bytes = cnt;
    4130                 :          0 :                         v->buf   = buf;
    4131                 :            : 
    4132                 :          0 :                         vec->entries++;
    4133                 :            : 
    4134         [ #  # ]:          0 :                 } else if (map) {
    4135                 :          0 :                         clear_bit(map, sec);
    4136                 :            :                 }
    4137                 :            : 
    4138                 :            :         next:
    4139                 :          0 :                 blk_start += cnt;
    4140                 :          0 :                 buf       += cnt;
    4141                 :            :         }
    4142                 :            : 
    4143                 :          0 :         vec->block = block;
    4144                 :            : 
    4145                 :            : out:
    4146                 :          0 :         free(bitmap);
    4147                 :          0 :         return err;
    4148                 :            : }
    4149                 :            : 
    4150                 :            : #if 0
    4151                 :            : /**
    4152                 :            :  * @block: vhd block number
    4153                 :            :  * @buf: buffer to place data in
    4154                 :            :  * @size: number of bytes to read
    4155                 :            :  * @start: byte offset into block from which to start reading
    4156                 :            :  * @end: byte offset in block at which to stop reading
    4157                 :            :  *
    4158                 :            :  * reads data (if it exists) into @buf.  partial reads may occur
    4159                 :            :  * for the first and last sectors if @start and @end are not multiples
    4160                 :            :  * of vhd sector size.
    4161                 :            :  */
    4162                 :            : static int
    4163                 :            : vhd_block_vector_read_allocated(vhd_context_t *ctx, uint32_t block,
    4164                 :            :                                 char *buf, uint64_t start, uint64_t end)
    4165                 :            : {
    4166                 :            :         int err;
    4167                 :            :         vhd_block_vector_t vec;
    4168                 :            : 
    4169                 :            :         vec.array = NULL;
    4170                 :            : 
    4171                 :            :         err = vhd_block_vector_init(ctx, &vec, block, NULL, buf, start, end);
    4172                 :            :         if (err)
    4173                 :            :                 goto out;
    4174                 :            : 
    4175                 :            :         err = vhd_block_vector_read(ctx, &vec);
    4176                 :            : 
    4177                 :            : out:
    4178                 :            :         free(vec.array);
    4179                 :            :         return err;
    4180                 :            : }
    4181                 :            : #endif
    4182                 :            : 
    4183                 :            : /**
    4184                 :            :  * @param[in]           ctx                     VHD context
    4185                 :            :  * @param[in]           block           vhd block number
    4186                 :            :  * @param[in,out]       map                     bitmap of sectors in block which should be read
    4187                 :            :  * @param[out]          buf                     buffer to place data in
    4188                 :            :  * @param[in]           start           byte offset into block from which to start reading
    4189                 :            :  * @param[in]           end                     byte offset in block at which to stop reading
    4190                 :            :  *
    4191                 :            :  * @return 0 on success, -errno on error
    4192                 :            :  * 
    4193                 :            :  * For every bit set in \p map (corresponding to sectors in \p block),
    4194                 :            :  * reads data (if it exists) into \p buf.
    4195                 :            :  * 
    4196                 :            :  * If data does not exist, clears corresponding bit in \p map.
    4197                 :            :  *
    4198                 :            :  * Partial reads may occur for the first and last sectors if \p start and \p end
    4199                 :            :  * are not multiples of vhd sector size.
    4200                 :            :  */
    4201                 :            : static int
    4202                 :          0 : vhd_block_vector_read_allocated_selective(vhd_context_t *ctx,
    4203                 :            :                                           uint32_t block, char *map, char *buf,
    4204                 :            :                                           uint64_t start, uint64_t end)
    4205                 :            : {
    4206                 :            :         int err;
    4207                 :            :         vhd_block_vector_t vec;
    4208                 :            : 
    4209                 :          0 :         vec.array = NULL;
    4210                 :            : 
    4211                 :          0 :         err = vhd_block_vector_init(ctx, &vec, block, map, buf, start, end);
    4212         [ #  # ]:          0 :         if (err)
    4213                 :            :                 goto out;
    4214                 :            : 
    4215                 :          0 :         err = vhd_block_vector_read(ctx, &vec);
    4216                 :            : 
    4217                 :            : out:
    4218                 :          0 :         free(vec.array);
    4219                 :          0 :         return err;
    4220                 :            : }
    4221                 :            : 
    4222                 :            : /**
    4223                 :            :  * @param[in]   ctx             VHD context
    4224                 :            :  * @param[in]   map             bitmap of sectors which have already been read
    4225                 :            :  * @param[out]  buf             destination buffer
    4226                 :            :  * @param[in]   size    size in bytes to read
    4227                 :            :  * @param[in]   off             byte offset in virtual disk to read
    4228                 :            :  *
    4229                 :            :  * @return 0 on success, -errno on error
    4230                 :            :  * 
    4231                 :            :  * Reads \p size bytes into \p buf, starting at \p off, skipping sectors
    4232                 :            :  * which have corresponding bits set in \p map
    4233                 :            :  */
    4234                 :            : static int
    4235                 :          0 : __vhd_io_dynamic_read_link_bytes(vhd_context_t *ctx, char *map,
    4236                 :            :                                  char *buf, size_t size, uint64_t off)
    4237                 :            : {
    4238                 :            :         char *blkmap;
    4239                 :            :         int i, err, map_off;
    4240                 :            :         off64_t blk_off, blk_size;
    4241                 :            :         uint32_t blk, bytes, first_sec, last_sec;
    4242                 :            : 
    4243                 :          0 :         blkmap = malloc((ctx->spb + 7) >> 3);
    4244         [ #  # ]:          0 :         if (!blkmap) {
    4245                 :            :                 err = -ENOMEM;
    4246                 :            :                 goto out;
    4247                 :            :         }
    4248                 :            : 
    4249                 :          0 :         map_off  = 0;
    4250                 :          0 :         blk_size = vhd_sectors_to_bytes(ctx->spb);
    4251                 :            : 
    4252                 :            :         do {
    4253                 :          0 :                 blk     = off / blk_size;
    4254                 :          0 :                 blk_off = off % blk_size;
    4255                 :          0 :                 bytes   = MIN(blk_size - blk_off, size);
    4256                 :            : 
    4257                 :          0 :                 first_sec = blk_off >> VHD_SECTOR_SHIFT;
    4258                 :          0 :                 last_sec  = secs_round_up_no_zero(blk_off + bytes);
    4259                 :            : 
    4260         [ #  # ]:          0 :                 if (ctx->bat.bat[blk] == DD_BLK_UNUSED)
    4261                 :            :                         goto next;
    4262                 :            : 
    4263                 :          0 :                 memset(blkmap, 0, (ctx->spb + 7) >> 3);
    4264                 :            : 
    4265         [ #  # ]:          0 :                 for (i = 0; i < (last_sec - first_sec); i++)
    4266         [ #  # ]:          0 :                         if (!test_bit(map, map_off + i))
    4267                 :          0 :                                 set_bit(blkmap, first_sec + i);
    4268                 :            : 
    4269                 :          0 :                 err = vhd_block_vector_read_allocated_selective(ctx, blk,
    4270                 :            :                                                                 blkmap, buf,
    4271                 :            :                                                                 blk_off,
    4272                 :            :                                                                 blk_off +
    4273                 :            :                                                                 bytes);
    4274         [ #  # ]:          0 :                 if (err)
    4275                 :            :                         goto out;
    4276                 :            : 
    4277         [ #  # ]:          0 :                 for (i = 0; i < (last_sec - first_sec); i++)
    4278         [ #  # ]:          0 :                         if (test_bit(blkmap, first_sec + i))
    4279                 :          0 :                                 set_bit(map, map_off + i);
    4280                 :            : 
    4281                 :            :         next:
    4282                 :          0 :                 size    -= bytes;
    4283                 :          0 :                 off     += bytes;
    4284                 :          0 :                 map_off += (last_sec - first_sec);
    4285                 :          0 :                 buf     += bytes;
    4286                 :            : 
    4287         [ #  # ]:          0 :         } while (size);
    4288                 :            : 
    4289                 :            :         err = 0;
    4290                 :            : out:
    4291                 :          0 :         free(blkmap);
    4292                 :          0 :         return err;
    4293                 :            : }
    4294                 :            : 
    4295                 :            : static int
    4296                 :          0 : __raw_read_link_bytes(const char *filename,
    4297                 :            :                       char *map, char *buf, size_t size, uint64_t off)
    4298                 :            : {
    4299                 :            :         int fd, err;
    4300                 :            :         uint32_t i, first_sec, last_sec;
    4301                 :            : 
    4302                 :          0 :         fd = open(filename, O_RDONLY | O_LARGEFILE);
    4303         [ #  # ]:          0 :         if (fd == -1) {
    4304         [ #  # ]:          0 :                 VHDLOG("%s: failed to open: %d\n", filename, -errno);
    4305                 :          0 :                 return -errno;
    4306                 :            :         }
    4307                 :            : 
    4308                 :          0 :         first_sec = off >> VHD_SECTOR_SHIFT;
    4309                 :          0 :         last_sec  = secs_round_up_no_zero(off + size);
    4310                 :            : 
    4311         [ #  # ]:          0 :         for (i = first_sec; i < last_sec; i++) {
    4312         [ #  # ]:          0 :                 if (!test_bit(map, i - first_sec)) {
    4313                 :            :                         uint32_t secs = 0;
    4314                 :            :                         uint64_t coff, csize;
    4315                 :            : 
    4316 [ #  # ][ #  # ]:          0 :                         while (i + secs < last_sec &&
    4317                 :          0 :                                !test_bit(map, i + secs - first_sec))
    4318                 :          0 :                                 secs++;
    4319                 :            : 
    4320                 :          0 :                         coff  = vhd_sectors_to_bytes(i);
    4321                 :          0 :                         csize = vhd_sectors_to_bytes(secs);
    4322                 :            : 
    4323         [ #  # ]:          0 :                         if (i == first_sec)
    4324                 :          0 :                                 coff = off;
    4325         [ #  # ]:          0 :                         if (secs == last_sec - 1)
    4326                 :          0 :                                 csize = (off + size) - coff;
    4327                 :            : 
    4328         [ #  # ]:          0 :                         if (pread(fd, buf + coff - off, csize, coff) != csize) {
    4329         [ #  # ]:          0 :                                 err = (errno ? -errno : -EIO);
    4330                 :          0 :                                 goto close;
    4331                 :            :                         }
    4332                 :            : 
    4333                 :          0 :                         i += secs - 1;
    4334                 :            :                 }
    4335                 :            :         }
    4336                 :            : 
    4337                 :            :         err = 0;
    4338                 :            : 
    4339                 :            : close:
    4340                 :          0 :         close(fd);
    4341                 :          0 :         return err;
    4342                 :            : }
    4343                 :            : 
    4344                 :            : static int
    4345                 :          0 : __vhd_io_dynamic_read_bytes(vhd_context_t *ctx,
    4346                 :            :                             char *buf, size_t size, uint64_t off)
    4347                 :            : {
    4348                 :            :         int err;
    4349                 :            :         char *next, *map;
    4350                 :            :         vhd_context_t parent, *vhd;
    4351                 :            :         uint32_t i, done, first_sec, last_sec;
    4352                 :            : 
    4353                 :          0 :         err  = vhd_get_bat(ctx);
    4354         [ #  # ]:          0 :         if (err)
    4355                 :          0 :                 return err;
    4356                 :            : 
    4357                 :          0 :         first_sec = off >> VHD_SECTOR_SHIFT;
    4358                 :          0 :         last_sec  = secs_round_up_no_zero(off + size);
    4359                 :            : 
    4360                 :          0 :         vhd  = ctx;
    4361                 :          0 :         next = NULL;
    4362                 :          0 :         map  = calloc(1, ((last_sec - first_sec) + 7) >> 3);
    4363         [ #  # ]:          0 :         if (!map) {
    4364                 :            :                 err = -ENOMEM;
    4365                 :            :                 goto out;
    4366                 :            :         }
    4367                 :            : 
    4368                 :            :         for (;;) {
    4369                 :          0 :                 err = __vhd_io_dynamic_read_link_bytes(vhd, map,
    4370                 :            :                                                        buf, size, off);
    4371         [ #  # ]:          0 :                 if (err)
    4372                 :            :                         goto close;
    4373                 :            : 
    4374         [ #  # ]:          0 :                 for (done = 0, i = 0; i < (last_sec - first_sec); i++)
    4375         [ #  # ]:          0 :                         if (test_bit(map, i))
    4376                 :          0 :                                 done++;
    4377                 :            : 
    4378         [ #  # ]:          0 :                 if (done == last_sec - first_sec) {
    4379                 :            :                         err = 0;
    4380                 :            :                         goto close;
    4381                 :            :                 }
    4382                 :            : 
    4383         [ #  # ]:          0 :                 if (vhd->footer.type == HD_TYPE_DIFF) {
    4384                 :            :                         vhd_context_t *p;
    4385                 :          0 :                         p = vhd_cache_get_parent(vhd);
    4386         [ #  # ]:          0 :                         if (p) {
    4387                 :          0 :                                 vhd = p;
    4388                 :          0 :                                 err = vhd_get_bat(vhd);
    4389         [ #  # ]:          0 :                                 if (err)
    4390                 :            :                                         goto out;
    4391                 :          0 :                                 continue;
    4392                 :            :                         }
    4393                 :            : 
    4394                 :          0 :                         err = vhd_parent_locator_get(vhd, &next);
    4395         [ #  # ]:          0 :                         if (err)
    4396                 :            :                                 goto close;
    4397                 :            : 
    4398         [ #  # ]:          0 :                         if (vhd_parent_raw(vhd)) {
    4399                 :          0 :                                 err = __raw_read_link_bytes(next, map,
    4400                 :            :                                                             buf, size, off);
    4401                 :            :                                 goto close;
    4402                 :            :                         }
    4403                 :            :                 } else {
    4404                 :            :                         err = 0;
    4405                 :            :                         goto close;
    4406                 :            :                 }
    4407                 :            : 
    4408         [ #  # ]:          0 :                 if (vhd != ctx)
    4409                 :          0 :                         vhd_close(vhd);
    4410                 :          0 :                 vhd = &parent;
    4411                 :            : 
    4412                 :          0 :                 err = vhd_open(vhd, next, VHD_OPEN_RDONLY);
    4413         [ #  # ]:          0 :                 if (err)
    4414                 :            :                         goto out;
    4415                 :            : 
    4416                 :          0 :                 err = vhd_get_bat(vhd);
    4417         [ #  # ]:          0 :                 if (err)
    4418                 :            :                         goto close;
    4419                 :            : 
    4420                 :          0 :                 free(next);
    4421                 :          0 :                 next = NULL;
    4422                 :            :         }
    4423                 :            : 
    4424                 :            : close:
    4425         [ #  # ]:          0 :         if (!err) {
    4426                 :            :                 /*
    4427                 :            :                  * clear any regions not present on disk
    4428                 :            :                  */
    4429         [ #  # ]:          0 :                 for (i = first_sec; i < last_sec; i++) {
    4430         [ #  # ]:          0 :                         if (!test_bit(map, i - first_sec)) {
    4431                 :          0 :                                 uint64_t coff  = vhd_sectors_to_bytes(i);
    4432                 :          0 :                                 uint32_t csize = VHD_SECTOR_SIZE;
    4433                 :            : 
    4434         [ #  # ]:          0 :                                 if (i == first_sec)
    4435                 :          0 :                                         coff = off;
    4436         [ #  # ]:          0 :                                 if (i == last_sec - 1)
    4437                 :          0 :                                         csize = (off + size) - coff;
    4438                 :            : 
    4439                 :          0 :                                 memset(buf + coff - off, 0, csize);
    4440                 :            :                         }
    4441                 :            :                 }
    4442                 :            :         }
    4443                 :            : 
    4444 [ #  # ][ #  # ]:          0 :         if (vhd != ctx && !vhd_flag_test(vhd->oflags, VHD_OPEN_CACHED))
    4445                 :          0 :                 vhd_close(vhd);
    4446                 :            : out:
    4447                 :          0 :         free(map);
    4448                 :          0 :         free(next);
    4449                 :          0 :         return err;
    4450                 :            : }
    4451                 :            : 
    4452                 :            : int
    4453                 :          0 : vhd_io_read_bytes(vhd_context_t *ctx, void *buf, size_t size, uint64_t off)
    4454                 :            : {
    4455         [ #  # ]:          0 :         if (off + size > ctx->footer.curr_size)
    4456                 :            :                 return -ERANGE;
    4457                 :            : 
    4458         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx))
    4459                 :          0 :                 return vhd_pread(ctx, buf, size, off);
    4460                 :            : 
    4461                 :          0 :         return __vhd_io_dynamic_read_bytes(ctx, buf, size, off);
    4462                 :            : }
    4463                 :            : 
    4464                 :            : static int
    4465                 :          0 : __vhd_io_dynamic_write_bytes_aligned(vhd_context_t *ctx,
    4466                 :            :                                      char *buf, size_t size, uint64_t off)
    4467                 :            : {
    4468                 :            :         char *map;
    4469                 :            :         int i, err, ret;
    4470                 :            :         uint64_t blk_off, blk_size, blk_start;
    4471                 :            :         uint32_t blk, bytes, first_sec, last_sec;
    4472                 :            : 
    4473 [ #  # ][ #  # ]:          0 :         if (off & (VHD_SECTOR_SIZE - 1) || size & (VHD_SECTOR_SIZE - 1))
    4474                 :          0 :                 return -EINVAL;
    4475                 :            : 
    4476                 :          0 :         err = vhd_get_bat(ctx);
    4477         [ #  # ]:          0 :         if (err)
    4478                 :            :                 return err;
    4479                 :            : 
    4480         [ #  # ]:          0 :         if (vhd_has_batmap(ctx)) {
    4481                 :          0 :                 err = vhd_get_batmap(ctx);
    4482         [ #  # ]:          0 :                 if (err)
    4483                 :            :                         return err;
    4484                 :            :         }
    4485                 :            : 
    4486                 :          0 :         map      = NULL;
    4487                 :          0 :         blk_size = vhd_sectors_to_bytes(ctx->spb);
    4488                 :            : 
    4489                 :            :         do {
    4490                 :          0 :                 blk     = off / blk_size;
    4491                 :          0 :                 blk_off = off % blk_size;
    4492                 :          0 :                 bytes   = MIN(blk_size - blk_off, size);
    4493                 :            : 
    4494                 :          0 :                 first_sec = blk_off >> VHD_SECTOR_SHIFT;
    4495                 :          0 :                 last_sec  = secs_round_up_no_zero(blk_off + bytes);
    4496                 :            : 
    4497                 :          0 :                 blk_start = ctx->bat.bat[blk];
    4498         [ #  # ]:          0 :                 if (blk_start == DD_BLK_UNUSED) {
    4499                 :            :                         /*
    4500                 :            :                          * When allocating a block, fill it with zeroes unless we
    4501                 :            :                          * are about to write the entire block, in which case
    4502                 :            :                          * don't bother.
    4503                 :            :                          */
    4504                 :          0 :                         err = __vhd_io_allocate_block(ctx, blk, (bytes < blk_size)?true:false);
    4505         [ #  # ]:          0 :                         if (err)
    4506                 :            :                                 goto fail;
    4507                 :            : 
    4508                 :          0 :                         blk_start = ctx->bat.bat[blk];
    4509                 :            :                 }
    4510                 :            : 
    4511                 :          0 :                 blk_start = vhd_sectors_to_bytes(blk_start + ctx->bm_secs);
    4512                 :            : 
    4513                 :          0 :                 err = vhd_pwrite(ctx, buf, bytes, blk_start + blk_off);
    4514         [ #  # ]:          0 :                 if (err)
    4515                 :            :                         goto fail;
    4516                 :            : 
    4517   [ #  #  #  # ]:          0 :                 if (vhd_has_batmap(ctx) &&
    4518                 :          0 :                     vhd_batmap_test(ctx, &ctx->batmap, blk))
    4519                 :            :                         goto next;
    4520                 :            : 
    4521                 :          0 :                 err = vhd_read_bitmap(ctx, blk, &map);
    4522         [ #  # ]:          0 :                 if (err) {
    4523                 :          0 :                         map = NULL;
    4524                 :          0 :                         goto fail;
    4525                 :            :                 }
    4526                 :            : 
    4527 [ #  # ][ #  # ]:          0 :                 for (i = first_sec; i < last_sec; i++)
    4528                 :          0 :                         vhd_bitmap_set(ctx, map, i);
    4529                 :            : 
    4530                 :          0 :                 err = vhd_write_bitmap(ctx, blk, map);
    4531         [ #  # ]:          0 :                 if (err)
    4532                 :            :                         goto fail;
    4533                 :            : 
    4534         [ #  # ]:          0 :                 if (vhd_has_batmap(ctx)) {
    4535         [ #  # ]:          0 :                         for (i = 0; i < ctx->spb; i++)
    4536      [ #  #  # ]:          0 :                                 if (!vhd_bitmap_test(ctx, map, i)) {
    4537                 :          0 :                                         free(map);
    4538                 :          0 :                                         map = NULL;
    4539                 :          0 :                                         goto next;
    4540                 :            :                                 }
    4541                 :            : 
    4542                 :          0 :                         vhd_batmap_set(ctx, &ctx->batmap, blk);
    4543                 :          0 :                         err = vhd_write_batmap(ctx, &ctx->batmap);
    4544         [ #  # ]:          0 :                         if (err)
    4545                 :            :                                 goto fail;
    4546                 :            :                 }
    4547                 :            : 
    4548                 :          0 :                 free(map);
    4549                 :          0 :                 map = NULL;
    4550                 :            : 
    4551                 :            :         next:
    4552                 :          0 :                 size   -= bytes;
    4553                 :          0 :                 off    += bytes;
    4554                 :          0 :                 buf    += bytes;
    4555                 :            : 
    4556         [ #  # ]:          0 :         } while (size);
    4557                 :            : 
    4558                 :            :         err = 0;
    4559                 :            : 
    4560                 :            : out:
    4561                 :          0 :         ret = vhd_write_footer(ctx, &ctx->footer);
    4562         [ #  # ]:          0 :         return (err ? err : ret);
    4563                 :            : 
    4564                 :            : fail:
    4565                 :          0 :         free(map);
    4566                 :          0 :         goto out;
    4567                 :            : }
    4568                 :            : 
    4569                 :            : static int
    4570                 :          0 : __vhd_io_dynamic_write_bytes(vhd_context_t *ctx,
    4571                 :            :                              char *buf, size_t size, uint64_t off)
    4572                 :            : {
    4573                 :            :         int err;
    4574                 :            :         char *tmp;
    4575                 :            :         uint32_t first_sec, last_sec, first_sec_off, last_sec_off;
    4576                 :            : 
    4577                 :          0 :         err = 0;
    4578                 :          0 :         tmp = NULL;
    4579                 :            : 
    4580                 :          0 :         first_sec = off >> VHD_SECTOR_SHIFT;
    4581                 :          0 :         last_sec  = secs_round_up_no_zero(off + size);
    4582                 :            : 
    4583                 :          0 :         first_sec_off = off & (VHD_SECTOR_SIZE - 1);
    4584                 :          0 :         last_sec_off  = (off + size) & (VHD_SECTOR_SIZE - 1);
    4585                 :            : 
    4586         [ #  # ]:          0 :         if (first_sec_off || last_sec_off) {
    4587                 :          0 :                 tmp = malloc(VHD_SECTOR_SIZE);
    4588         [ #  # ]:          0 :                 if (!tmp) {
    4589                 :            :                         err = -ENOMEM;
    4590                 :            :                         goto out;
    4591                 :            :                 }
    4592                 :            : 
    4593         [ #  # ]:          0 :                 if (first_sec_off) {
    4594                 :          0 :                         uint32_t new = VHD_SECTOR_SIZE - first_sec_off;
    4595         [ #  # ]:          0 :                         if (new > size)
    4596                 :          0 :                                 new = size;
    4597                 :            : 
    4598                 :          0 :                         err = vhd_io_read_bytes(
    4599                 :            :                                 ctx, tmp, VHD_SECTOR_SIZE,
    4600                 :            :                                 vhd_sectors_to_bytes(first_sec));
    4601         [ #  # ]:          0 :                         if (err)
    4602                 :            :                                 goto out;
    4603                 :            : 
    4604                 :          0 :                         memcpy(tmp + first_sec_off, buf, new);
    4605                 :            : 
    4606                 :          0 :                         err = __vhd_io_dynamic_write_bytes_aligned(
    4607                 :            :                                 ctx, tmp, VHD_SECTOR_SIZE,
    4608                 :            :                                 vhd_sectors_to_bytes(first_sec));
    4609         [ #  # ]:          0 :                         if (err)
    4610                 :            :                                 goto out;
    4611                 :            : 
    4612                 :          0 :                         buf  += new;
    4613                 :          0 :                         off  += new;
    4614                 :          0 :                         size -= new;
    4615                 :            :                 }
    4616                 :            : 
    4617 [ #  # ][ #  # ]:          0 :                 if (last_sec_off &&
    4618         [ #  # ]:          0 :                     (last_sec - first_sec > 1 || !first_sec_off)) {
    4619                 :          0 :                         uint32_t new = last_sec_off;
    4620                 :            : 
    4621                 :          0 :                         err = vhd_io_read_bytes(
    4622                 :            :                                 ctx, tmp, VHD_SECTOR_SIZE,
    4623                 :          0 :                                 vhd_sectors_to_bytes(last_sec - 1));
    4624         [ #  # ]:          0 :                         if (err)
    4625                 :            :                                 goto out;
    4626                 :            : 
    4627                 :          0 :                         memcpy(tmp, buf + size - new, new);
    4628                 :            : 
    4629                 :          0 :                         err = __vhd_io_dynamic_write_bytes_aligned(
    4630                 :            :                                 ctx, tmp, VHD_SECTOR_SIZE,
    4631                 :            :                                 vhd_sectors_to_bytes(last_sec - 1));
    4632         [ #  # ]:          0 :                         if (err)
    4633                 :            :                                 goto out;
    4634                 :            : 
    4635                 :            :                         size -= new;
    4636                 :            :                 }
    4637                 :            :         }
    4638                 :            : 
    4639         [ #  # ]:          0 :         if (size)
    4640                 :          0 :                 err = __vhd_io_dynamic_write_bytes_aligned(ctx, buf, size, off);
    4641                 :            : 
    4642                 :            : out:
    4643                 :          0 :         free(tmp);
    4644                 :          0 :         return err;
    4645                 :            : }
    4646                 :            : 
    4647                 :            : int
    4648                 :          0 : vhd_io_write_bytes(vhd_context_t *ctx, void *buf, size_t size, uint64_t off)
    4649                 :            : {
    4650         [ #  # ]:          0 :         if (off + size > ctx->footer.curr_size)
    4651                 :            :                 return -ERANGE;
    4652                 :            : 
    4653         [ #  # ]:          0 :         if (!vhd_type_dynamic(ctx))
    4654                 :          0 :                 return vhd_pwrite(ctx, buf, size, off);
    4655                 :            : 
    4656                 :          0 :         return __vhd_io_dynamic_write_bytes(ctx, buf, size, off);
    4657                 :            : }
    4658                 :            : 
    4659                 :            : int
    4660                 :          0 : vhd_marker(vhd_context_t *ctx, char *marker)
    4661                 :            : {
    4662                 :            :         int err;
    4663                 :            :         vhd_batmap_t batmap;
    4664                 :            : 
    4665                 :          0 :         *marker = 0;
    4666                 :            : 
    4667         [ #  # ]:          0 :         if (!vhd_has_batmap(ctx))
    4668                 :          0 :                 return xattr_get(ctx->fd,
    4669                 :            :                                  VHD_XATTR_MARKER,
    4670                 :            :                                  (void *)marker,
    4671                 :            :                                  sizeof(*marker));
    4672                 :            : 
    4673                 :          0 :         err = vhd_read_batmap_header(ctx, &batmap);
    4674         [ #  # ]:          0 :         if (err)
    4675                 :            :                 return err;
    4676                 :            : 
    4677                 :          0 :         *marker = batmap.header.marker;
    4678                 :          0 :         return 0;
    4679                 :            : }
    4680                 :            : 
    4681                 :            : int
    4682                 :          0 : vhd_set_marker(vhd_context_t *ctx, char marker)
    4683                 :            : {
    4684                 :            :         int err;
    4685                 :            :         vhd_batmap_t batmap;
    4686                 :            : 
    4687         [ #  # ]:          0 :         if (!vhd_has_batmap(ctx))
    4688                 :          0 :                 return xattr_set(ctx->fd,
    4689                 :            :                                  VHD_XATTR_MARKER,
    4690                 :            :                                  (void *)&marker,
    4691                 :            :                                  sizeof(marker));
    4692                 :            : 
    4693                 :          0 :         err = vhd_read_batmap_header(ctx, &batmap);
    4694         [ #  # ]:          0 :         if (err)
    4695                 :            :                 return err;
    4696                 :            : 
    4697                 :          0 :         batmap.header.marker = marker;
    4698                 :          0 :         return vhd_write_batmap_header(ctx, &batmap);
    4699                 :            : }
    4700                 :            : 
    4701                 :            : int
    4702                 :          0 : vhd_get_keyhash(vhd_context_t *ctx, struct vhd_keyhash *keyhash)
    4703                 :            : {
    4704                 :            :         int err;
    4705                 :            :         vhd_batmap_t batmap;
    4706                 :            : 
    4707         [ #  # ]:          0 :         if (!vhd_has_batmap(ctx))
    4708                 :          0 :                 return xattr_get(ctx->fd,
    4709                 :            :                                  VHD_XATTR_KEYHASH,
    4710                 :            :                                  (void *)keyhash,
    4711                 :            :                                  sizeof(*keyhash));
    4712                 :            : 
    4713                 :          0 :         err = vhd_read_batmap_header(ctx, &batmap);
    4714         [ #  # ]:          0 :         if (err)
    4715                 :            :                 return err;
    4716                 :            : 
    4717                 :            :         memcpy(keyhash, &batmap.header.keyhash, sizeof(*keyhash));
    4718                 :          0 :         return 0;
    4719                 :            : }
    4720                 :            : 
    4721                 :            : int
    4722                 :          0 : vhd_set_keyhash(vhd_context_t *ctx, const struct vhd_keyhash *keyhash)
    4723                 :            : {
    4724                 :            :         int err;
    4725                 :            :         vhd_batmap_t batmap;
    4726                 :            : 
    4727         [ #  # ]:          0 :         if (!vhd_has_batmap(ctx))
    4728                 :          0 :                 return xattr_set(ctx->fd,
    4729                 :            :                                  VHD_XATTR_KEYHASH,
    4730                 :            :                                  (void *)keyhash,
    4731                 :            :                                  sizeof(*keyhash));
    4732                 :            : 
    4733                 :          0 :         err = vhd_read_batmap_header(ctx, &batmap);
    4734         [ #  # ]:          0 :         if (err)
    4735                 :            :                 return err;
    4736                 :            : 
    4737                 :            :         memcpy(&batmap.header.keyhash, keyhash, sizeof(*keyhash));
    4738                 :          0 :         memcpy(&(ctx->batmap.header.keyhash), keyhash, sizeof(*keyhash));
    4739                 :          0 :         return vhd_write_batmap_header(ctx, &batmap);
    4740                 :            : }

Generated by: LCOV version 1.13