LCOV - code coverage report
Current view: top level - vhd/lib - libvhd.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 30 2058 1.5 %
Date: 2024-12-18 23:41:32 Functions: 3 144 2.1 %
Branches: 29 1432 2.0 %

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

Generated by: LCOV version 1.13