LCOV - code coverage report
Current view: top level - vhd/lib - libvhd.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 1.6 % 2539 41
Test Date: 2026-06-26 00:00:00 Functions: 2.1 % 140 3
Branches: 2.0 % 1419 28

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

Generated by: LCOV version 2.0-1