LCOV - code coverage report
Current view: top level - vhd/lib - libvhd-journal.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 821 0
Test Date: 2026-06-26 00:00:00 Functions: 0.0 % 42 0
Branches: 0.0 % 406 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                 :             : #include <stdio.h>
      36                 :             : #include <errno.h>
      37                 :             : #include <fcntl.h>
      38                 :             : #include <stdlib.h>
      39                 :             : #include <unistd.h>
      40                 :             : #include <string.h>
      41                 :             : 
      42                 :             : #include "atomicio.h"
      43                 :             : #include "libvhd-journal.h"
      44                 :             : 
      45                 :             : #define VHD_JOURNAL_ENTRY_TYPE_FOOTER_P  1
      46                 :             : #define VHD_JOURNAL_ENTRY_TYPE_FOOTER_C  2
      47                 :             : #define VHD_JOURNAL_ENTRY_TYPE_HEADER    3
      48                 :             : #define VHD_JOURNAL_ENTRY_TYPE_LOCATOR   4
      49                 :             : #define VHD_JOURNAL_ENTRY_TYPE_BAT       5
      50                 :             : #define VHD_JOURNAL_ENTRY_TYPE_BATMAP_H  6
      51                 :             : #define VHD_JOURNAL_ENTRY_TYPE_BATMAP_M  7
      52                 :             : #define VHD_JOURNAL_ENTRY_TYPE_DATA      8
      53                 :             : 
      54                 :             : typedef struct vhd_journal_entry {
      55                 :             :         uint64_t                         cookie;
      56                 :             :         uint32_t                         type;
      57                 :             :         uint32_t                         size;
      58                 :             :         uint64_t                         offset;
      59                 :             :         uint32_t                         checksum;
      60                 :             : } vhd_journal_entry_t;
      61                 :             : 
      62                 :             : static inline int
      63                 :           0 : vhd_journal_seek(vhd_journal_t *j, off64_t offset, int whence)
      64                 :             : {
      65                 :           0 :         off64_t off;
      66                 :             : 
      67                 :           0 :         off = lseek64(j->jfd, offset, whence);
      68         [ #  # ]:           0 :         if (off == (off64_t)-1)
      69                 :           0 :                 return -errno;
      70                 :             : 
      71                 :             :         return 0;
      72                 :             : }
      73                 :             : 
      74                 :             : static inline off64_t
      75                 :           0 : vhd_journal_position(vhd_journal_t *j)
      76                 :             : {
      77                 :           0 :         return lseek64(j->jfd, 0, SEEK_CUR);
      78                 :             : }
      79                 :             : 
      80                 :             : static inline int
      81                 :           0 : vhd_journal_read(vhd_journal_t *j, void *buf, size_t size)
      82                 :             : {
      83                 :           0 :         ssize_t ret;
      84                 :             : 
      85                 :           0 :         errno = 0;
      86                 :             : 
      87                 :           0 :         ret = atomicio(read, j->jfd, buf, size);
      88         [ #  # ]:           0 :         if (ret != size)
      89         [ #  # ]:           0 :                 return (errno ? -errno : -EIO);
      90                 :             : 
      91                 :             :         return 0;
      92                 :             : }
      93                 :             : 
      94                 :             : static inline int
      95                 :           0 : vhd_journal_write(vhd_journal_t *j, void *buf, size_t size)
      96                 :             : {
      97                 :           0 :         ssize_t ret;
      98                 :             : 
      99                 :           0 :         errno = 0;
     100                 :             : 
     101                 :           0 :         ret = atomicio(vwrite, j->jfd, buf, size);
     102         [ #  # ]:           0 :         if (ret != size)
     103         [ #  # ]:           0 :                 return (errno ? -errno : -EIO);
     104                 :             : 
     105                 :             :         return 0;
     106                 :             : }
     107                 :             : 
     108                 :             : static inline int
     109                 :           0 : vhd_journal_truncate(vhd_journal_t *j, off64_t length)
     110                 :             : {
     111                 :           0 :         int err;
     112                 :             : 
     113                 :           0 :         err = ftruncate(j->jfd, length);
     114         [ #  # ]:           0 :         if (err == -1)
     115                 :           0 :                 return -errno;
     116                 :             : 
     117                 :             :         return 0;
     118                 :             : }
     119                 :             : 
     120                 :             : static inline int
     121                 :           0 : vhd_journal_sync(vhd_journal_t *j)
     122                 :             : {
     123                 :           0 :         int err;
     124                 :             : 
     125                 :           0 :         err = fdatasync(j->jfd);
     126         [ #  # ]:           0 :         if (err)
     127                 :           0 :                 return -errno;
     128                 :             : 
     129                 :             :         return 0;
     130                 :             : }
     131                 :             : 
     132                 :             : static inline void
     133                 :           0 : vhd_journal_header_in(vhd_journal_header_t *header)
     134                 :             : {
     135                 :           0 :         BE64_IN(&header->vhd_footer_offset);
     136                 :           0 :         BE32_IN(&header->journal_data_entries);
     137                 :           0 :         BE32_IN(&header->journal_metadata_entries);
     138                 :           0 :         BE64_IN(&header->journal_data_offset);
     139                 :           0 :         BE64_IN(&header->journal_metadata_offset);
     140                 :           0 : }
     141                 :             : 
     142                 :             : static inline void
     143                 :           0 : vhd_journal_header_out(vhd_journal_header_t *header)
     144                 :             : {
     145                 :           0 :         BE64_OUT(&header->vhd_footer_offset);
     146                 :           0 :         BE32_OUT(&header->journal_data_entries);
     147                 :           0 :         BE32_OUT(&header->journal_metadata_entries);
     148                 :           0 :         BE64_OUT(&header->journal_data_offset);
     149                 :           0 :         BE64_OUT(&header->journal_metadata_offset);
     150                 :           0 : }
     151                 :             : 
     152                 :             : static int
     153                 :           0 : vhd_journal_validate_header(vhd_journal_t *j, vhd_journal_header_t *header)
     154                 :             : {
     155                 :           0 :         int err;
     156                 :           0 :         off64_t eof;
     157                 :             : 
     158         [ #  # ]:           0 :         if (memcmp(header->cookie,
     159                 :             :                    VHD_JOURNAL_HEADER_COOKIE, sizeof(header->cookie)))
     160                 :             :                 return -EINVAL;
     161                 :             : 
     162                 :           0 :         err = vhd_journal_seek(j, j->header.journal_eof, SEEK_SET);
     163         [ #  # ]:           0 :         if (err)
     164                 :             :                 return err;
     165                 :             : 
     166                 :           0 :         eof = vhd_journal_position(j);
     167         [ #  # ]:           0 :         if (eof == (off64_t)-1)
     168                 :           0 :                 return -errno;
     169                 :             : 
     170         [ #  # ]:           0 :         if (j->header.journal_data_offset > j->header.journal_eof)
     171                 :             :                 return -EINVAL;
     172                 :             : 
     173         [ #  # ]:           0 :         if (j->header.journal_metadata_offset > j->header.journal_eof)
     174                 :             :                 return -EINVAL;
     175                 :             : 
     176                 :             :         return 0;
     177                 :             : }
     178                 :             : 
     179                 :             : static int
     180                 :           0 : vhd_journal_read_journal_header(vhd_journal_t *j, vhd_journal_header_t *header)
     181                 :             : {
     182                 :           0 :         int err;
     183                 :           0 :         size_t size;
     184                 :             : 
     185                 :           0 :         size = sizeof(vhd_journal_header_t);
     186                 :           0 :         err  = vhd_journal_seek(j, 0, SEEK_SET);
     187         [ #  # ]:           0 :         if (err)
     188                 :             :                 return err;
     189                 :             : 
     190                 :           0 :         err  = vhd_journal_read(j, header, size);
     191         [ #  # ]:           0 :         if (err)
     192                 :             :                 return err;
     193                 :             : 
     194                 :           0 :         vhd_journal_header_in(header);
     195                 :             : 
     196                 :           0 :         return vhd_journal_validate_header(j, header);
     197                 :             : }
     198                 :             : 
     199                 :             : static int
     200                 :           0 : vhd_journal_write_header(vhd_journal_t *j, vhd_journal_header_t *header)
     201                 :             : {
     202                 :           0 :         int err;
     203                 :           0 :         size_t size;
     204                 :           0 :         vhd_journal_header_t h;
     205                 :             : 
     206                 :           0 :         memcpy(&h, header, sizeof(vhd_journal_header_t));
     207                 :             : 
     208                 :           0 :         err = vhd_journal_validate_header(j, &h);
     209         [ #  # ]:           0 :         if (err)
     210                 :           0 :                 return err;
     211                 :             : 
     212                 :           0 :         vhd_journal_header_out(&h);
     213                 :           0 :         size = sizeof(vhd_journal_header_t);
     214                 :             : 
     215                 :           0 :         err  = vhd_journal_seek(j, 0, SEEK_SET);
     216         [ #  # ]:           0 :         if (err)
     217                 :             :                 return err;
     218                 :             : 
     219                 :           0 :         err = vhd_journal_write(j, &h, size);
     220                 :             :         if (err)
     221                 :             :                 return err;
     222                 :             : 
     223                 :             :         return 0;
     224                 :             : }
     225                 :             : 
     226                 :             : static int
     227                 :           0 : vhd_journal_add_journal_header(vhd_journal_t *j)
     228                 :             : {
     229                 :           0 :         int err;
     230                 :           0 :         off64_t off;
     231                 :           0 :         vhd_context_t *vhd;
     232                 :             : 
     233                 :           0 :         vhd = &j->vhd;
     234                 :           0 :         memset(&j->header, 0, sizeof(vhd_journal_header_t));
     235                 :             : 
     236                 :           0 :         err = vhd_seek(vhd, 0, SEEK_END);
     237         [ #  # ]:           0 :         if (err)
     238                 :             :                 return err;
     239                 :             : 
     240                 :           0 :         off = vhd_position(vhd);
     241         [ #  # ]:           0 :         if (off == (off64_t)-1)
     242                 :           0 :                 return -errno;
     243                 :             : 
     244                 :           0 :         err = vhd_get_footer(vhd);
     245         [ #  # ]:           0 :         if (err)
     246                 :             :                 return err;
     247                 :             : 
     248                 :           0 :         uuid_copy(j->header.uuid, vhd->footer.uuid);
     249                 :           0 :         memcpy(j->header.cookie,
     250                 :             :                VHD_JOURNAL_HEADER_COOKIE, sizeof(j->header.cookie));
     251                 :           0 :         j->header.vhd_footer_offset = off - sizeof(vhd_footer_t);
     252                 :           0 :         j->header.journal_eof = sizeof(vhd_journal_header_t);
     253                 :             : 
     254                 :           0 :         return vhd_journal_write_header(j, &j->header);
     255                 :             : }
     256                 :             : 
     257                 :             : static void
     258                 :           0 : vhd_journal_entry_in(vhd_journal_entry_t *entry)
     259                 :             : {
     260                 :           0 :         BE32_IN(&entry->type);
     261                 :           0 :         BE32_IN(&entry->size);
     262                 :           0 :         BE64_IN(&entry->offset);
     263                 :           0 :         BE64_IN(&entry->cookie);
     264                 :           0 :         BE32_IN(&entry->checksum);
     265                 :           0 : }
     266                 :             : 
     267                 :             : static void
     268                 :           0 : vhd_journal_entry_out(vhd_journal_entry_t *entry)
     269                 :             : {
     270                 :           0 :         BE32_OUT(&entry->type);
     271                 :           0 :         BE32_OUT(&entry->size);
     272                 :           0 :         BE64_OUT(&entry->offset);
     273                 :           0 :         BE64_OUT(&entry->cookie);
     274                 :           0 :         BE32_OUT(&entry->checksum);
     275                 :           0 : }
     276                 :             : 
     277                 :             : static uint32_t
     278                 :           0 : vhd_journal_checksum_entry(vhd_journal_entry_t *entry, char *buf, size_t size)
     279                 :             : {
     280                 :           0 :         int i;
     281                 :           0 :         unsigned char *blob;
     282                 :           0 :         uint32_t checksum, tmp;
     283                 :             : 
     284                 :           0 :         checksum        = 0;
     285                 :           0 :         tmp             = entry->checksum;
     286                 :           0 :         entry->checksum = 0;
     287                 :             : 
     288                 :           0 :         blob = (unsigned char *)entry;
     289         [ #  # ]:           0 :         for (i = 0; i < sizeof(vhd_journal_entry_t); i++)
     290                 :           0 :                 checksum += blob[i];
     291                 :             : 
     292                 :           0 :         blob = (unsigned char *)buf;
     293         [ #  # ]:           0 :         for (i = 0; i < size; i++)
     294                 :           0 :                 checksum += blob[i];
     295                 :             : 
     296                 :           0 :         entry->checksum = tmp;
     297                 :           0 :         return ~checksum;
     298                 :             : }
     299                 :             : 
     300                 :             : static int
     301                 :           0 : vhd_journal_validate_entry(vhd_journal_entry_t *entry)
     302                 :             : {
     303                 :           0 :         if (entry->size == 0)
     304                 :             :                 return -EINVAL;
     305                 :             : 
     306   [ #  #  #  # ]:           0 :         if (entry->size & (VHD_SECTOR_SIZE - 1))
     307                 :             :                 return -EINVAL;
     308                 :             : 
     309   [ #  #  #  # ]:           0 :         if (entry->cookie != VHD_JOURNAL_ENTRY_COOKIE)
     310                 :             :                 return -EINVAL;
     311                 :             : 
     312                 :             :         return 0;
     313                 :             : }
     314                 :             : 
     315                 :             : static int
     316                 :           0 : vhd_journal_read_entry(vhd_journal_t *j, vhd_journal_entry_t *entry)
     317                 :             : {
     318                 :           0 :         int err;
     319                 :             : 
     320                 :           0 :         err = vhd_journal_read(j, entry, sizeof(vhd_journal_entry_t));
     321         [ #  # ]:           0 :         if (err)
     322                 :             :                 return err;
     323                 :             : 
     324                 :           0 :         vhd_journal_entry_in(entry);
     325         [ #  # ]:           0 :         return vhd_journal_validate_entry(entry);
     326                 :             : }
     327                 :             : 
     328                 :             : static int
     329                 :           0 : vhd_journal_write_entry(vhd_journal_t *j, vhd_journal_entry_t *entry)
     330                 :             : {
     331                 :           0 :         int err;
     332                 :           0 :         vhd_journal_entry_t e;
     333                 :             : 
     334         [ #  # ]:           0 :         err = vhd_journal_validate_entry(entry);
     335                 :           0 :         if (err)
     336                 :           0 :                 return err;
     337                 :             : 
     338                 :           0 :         memcpy(&e, entry, sizeof(vhd_journal_entry_t));
     339                 :           0 :         vhd_journal_entry_out(&e);
     340                 :             : 
     341                 :           0 :         err = vhd_journal_write(j, &e, sizeof(vhd_journal_entry_t));
     342                 :             :         if (err)
     343                 :             :                 return err;
     344                 :             : 
     345                 :             :         return 0;
     346                 :             : }
     347                 :             : 
     348                 :             : static int
     349                 :           0 : vhd_journal_validate_entry_data(vhd_journal_entry_t *entry, char *buf)
     350                 :             : {
     351                 :           0 :         int err;
     352                 :           0 :         uint32_t checksum;
     353                 :             : 
     354                 :           0 :         err      = 0;
     355                 :           0 :         checksum = vhd_journal_checksum_entry(entry, buf, entry->size);
     356                 :             : 
     357         [ #  # ]:           0 :         if (checksum != entry->checksum)
     358                 :           0 :                 return -EINVAL;
     359                 :             : 
     360                 :             :         return err;
     361                 :             : }
     362                 :             : 
     363                 :             : static int
     364                 :           0 : vhd_journal_update(vhd_journal_t *j, off64_t offset,
     365                 :             :                    char *buf, size_t size, uint32_t type)
     366                 :             : {
     367                 :           0 :         int err;
     368                 :           0 :         uint64_t *off, off_bak;
     369                 :           0 :         uint32_t *entries;
     370                 :           0 :         vhd_journal_entry_t entry;
     371                 :             : 
     372                 :           0 :         entry.type     = type;
     373                 :           0 :         entry.size     = size;
     374                 :           0 :         entry.offset   = offset;
     375                 :           0 :         entry.cookie   = VHD_JOURNAL_ENTRY_COOKIE;
     376                 :           0 :         entry.checksum = vhd_journal_checksum_entry(&entry, buf, size);
     377                 :             : 
     378                 :           0 :         err = vhd_journal_seek(j, j->header.journal_eof, SEEK_SET);
     379         [ #  # ]:           0 :         if (err)
     380                 :           0 :                 return err;
     381                 :             : 
     382                 :           0 :         err = vhd_journal_write_entry(j, &entry);
     383         [ #  # ]:           0 :         if (err)
     384                 :           0 :                 goto fail;
     385                 :             : 
     386                 :           0 :         err = vhd_journal_write(j, buf, size);
     387         [ #  # ]:           0 :         if (err)
     388                 :           0 :                 goto fail;
     389                 :             : 
     390         [ #  # ]:           0 :         if (type == VHD_JOURNAL_ENTRY_TYPE_DATA) {
     391                 :           0 :                 off     = &j->header.journal_data_offset;
     392                 :           0 :                 entries = &j->header.journal_data_entries;
     393                 :             :         } else {
     394                 :           0 :                 off     = &j->header.journal_metadata_offset;
     395                 :           0 :                 entries = &j->header.journal_metadata_entries;
     396                 :             :         }
     397                 :             : 
     398                 :           0 :         off_bak = *off;
     399         [ #  # ]:           0 :         if (!(*entries)++)
     400                 :           0 :                 *off = j->header.journal_eof;
     401                 :           0 :         j->header.journal_eof += (size + sizeof(vhd_journal_entry_t));
     402                 :             : 
     403                 :           0 :         err = vhd_journal_write_header(j, &j->header);
     404         [ #  # ]:           0 :         if (err) {
     405         [ #  # ]:           0 :                 if (!--(*entries))
     406                 :           0 :                         *off = off_bak;
     407                 :           0 :                 j->header.journal_eof -= (size + sizeof(vhd_journal_entry_t));
     408                 :           0 :                 goto fail;
     409                 :             :         }
     410                 :             : 
     411                 :             :         return 0;
     412                 :             : 
     413                 :           0 : fail:
     414         [ #  # ]:           0 :         if (!j->is_block)
     415                 :           0 :                 vhd_journal_truncate(j, j->header.journal_eof);
     416                 :             :         return err;
     417                 :             : }
     418                 :             : 
     419                 :             : static int
     420                 :           0 : vhd_journal_add_footer(vhd_journal_t *j)
     421                 :             : {
     422                 :           0 :         int err;
     423                 :           0 :         off64_t off;
     424                 :           0 :         vhd_context_t *vhd;
     425                 :           0 :         vhd_footer_t footer;
     426                 :             : 
     427                 :           0 :         vhd = &j->vhd;
     428                 :             : 
     429                 :           0 :         err = vhd_seek(vhd, 0, SEEK_END);
     430         [ #  # ]:           0 :         if (err)
     431                 :           0 :                 return err;
     432                 :             : 
     433                 :           0 :         off = vhd_position(vhd);
     434         [ #  # ]:           0 :         if (off == (off64_t)-1)
     435                 :           0 :                 return -errno;
     436                 :             : 
     437                 :           0 :         err = vhd_read_footer_at(vhd, &footer, off - sizeof(vhd_footer_t));
     438         [ #  # ]:           0 :         if (err)
     439                 :             :                 return err;
     440                 :             : 
     441                 :           0 :         vhd_footer_out(&footer);
     442                 :           0 :         err = vhd_journal_update(j, off - sizeof(vhd_footer_t),
     443                 :             :                                  (char *)&footer,
     444                 :             :                                  sizeof(vhd_footer_t),
     445                 :             :                                  VHD_JOURNAL_ENTRY_TYPE_FOOTER_P);
     446         [ #  # ]:           0 :         if (err)
     447                 :             :                 return err;
     448                 :             : 
     449         [ #  # ]:           0 :         if (!vhd_type_dynamic(vhd))
     450                 :             :                 return 0;
     451                 :             : 
     452                 :           0 :         err = vhd_read_footer_at(vhd, &footer, 0);
     453         [ #  # ]:           0 :         if (err)
     454                 :             :                 return err;
     455                 :             : 
     456                 :           0 :         vhd_footer_out(&footer);
     457                 :           0 :         err = vhd_journal_update(j, 0,
     458                 :             :                                  (char *)&footer,
     459                 :             :                                  sizeof(vhd_footer_t),
     460                 :             :                                  VHD_JOURNAL_ENTRY_TYPE_FOOTER_C);
     461                 :             : 
     462                 :             :         return err;
     463                 :             : }
     464                 :             : 
     465                 :             : static int
     466                 :           0 : vhd_journal_add_header(vhd_journal_t *j)
     467                 :             : {
     468                 :           0 :         int err;
     469                 :           0 :         off64_t off;
     470                 :           0 :         vhd_context_t *vhd;
     471                 :           0 :         vhd_header_t header;
     472                 :             : 
     473                 :           0 :         vhd = &j->vhd;
     474                 :             : 
     475                 :           0 :         err = vhd_read_header(vhd, &header);
     476         [ #  # ]:           0 :         if (err)
     477                 :           0 :                 return err;
     478                 :             : 
     479                 :           0 :         off = vhd->footer.data_offset;
     480                 :             : 
     481                 :           0 :         vhd_header_out(&header);
     482                 :           0 :         err = vhd_journal_update(j, off,
     483                 :             :                                  (char *)&header,
     484                 :             :                                  sizeof(vhd_header_t),
     485                 :             :                                  VHD_JOURNAL_ENTRY_TYPE_HEADER);
     486                 :             : 
     487                 :             :         return err;
     488                 :             : }
     489                 :             : 
     490                 :             : static int
     491                 :           0 : vhd_journal_add_locators(vhd_journal_t *j)
     492                 :             : {
     493                 :           0 :         int i, n, err;
     494                 :           0 :         vhd_context_t *vhd;
     495                 :             : 
     496                 :           0 :         vhd = &j->vhd;
     497                 :             : 
     498                 :           0 :         err = vhd_get_header(vhd);
     499         [ #  # ]:           0 :         if (err)
     500                 :             :                 return err;
     501                 :             : 
     502                 :             :         n = sizeof(vhd->header.loc) / sizeof(vhd_parent_locator_t);
     503         [ #  # ]:           0 :         for (i = 0; i < n; i++) {
     504                 :           0 :                 void *buf;
     505                 :           0 :                 off64_t off;
     506                 :           0 :                 size_t size;
     507                 :           0 :                 vhd_parent_locator_t *loc;
     508                 :             : 
     509                 :           0 :                 loc  = vhd->header.loc + i;
     510                 :           0 :                 err  = vhd_validate_platform_code(loc->code);
     511         [ #  # ]:           0 :                 if (err)
     512                 :             :                         return err;
     513                 :             : 
     514         [ #  # ]:           0 :                 if (loc->code == PLAT_CODE_NONE)
     515                 :           0 :                         continue;
     516                 :             : 
     517                 :           0 :                 off  = loc->data_offset;
     518         [ #  # ]:           0 :                 size = vhd_parent_locator_size(loc);
     519                 :             : 
     520         [ #  # ]:           0 :                 err  = posix_memalign(&buf, VHD_SECTOR_SIZE, size);
     521                 :           0 :                 if (err)
     522                 :           0 :                         return -err;
     523                 :             : 
     524                 :           0 :                 err  = vhd_seek(vhd, off, SEEK_SET);
     525         [ #  # ]:           0 :                 if (err)
     526                 :           0 :                         goto end;
     527                 :             : 
     528                 :           0 :                 err  = vhd_read(vhd, buf, size);
     529         [ #  # ]:           0 :                 if (err)
     530                 :           0 :                         goto end;
     531                 :             : 
     532                 :           0 :                 err  = vhd_journal_update(j, off, buf, size,
     533                 :             :                                           VHD_JOURNAL_ENTRY_TYPE_LOCATOR);
     534                 :             :                 if (err)
     535                 :             :                         goto end;
     536                 :             : 
     537                 :             :                 err = 0;
     538                 :             : 
     539                 :           0 :         end:
     540                 :           0 :                 free(buf);
     541         [ #  # ]:           0 :                 if (err)
     542                 :             :                         break;
     543                 :             :         }
     544                 :             : 
     545                 :             :         return err;
     546                 :             : }
     547                 :             : 
     548                 :             : static int
     549                 :           0 : vhd_journal_add_bat(vhd_journal_t *j)
     550                 :             : {
     551                 :           0 :         int err;
     552                 :           0 :         off64_t off;
     553                 :           0 :         size_t size;
     554                 :           0 :         vhd_bat_t bat;
     555                 :           0 :         vhd_context_t *vhd;
     556                 :             : 
     557                 :           0 :         vhd  = &j->vhd;
     558                 :             : 
     559                 :           0 :         err  = vhd_get_header(vhd);
     560         [ #  # ]:           0 :         if (err)
     561                 :           0 :                 return err;
     562                 :             : 
     563                 :           0 :         err  = vhd_read_bat(vhd, &bat);
     564         [ #  # ]:           0 :         if (err)
     565                 :             :                 return err;
     566                 :             : 
     567                 :           0 :         off  = vhd->header.table_offset;
     568                 :           0 :         size = vhd_bytes_padded(bat.entries * sizeof(uint32_t));
     569                 :             : 
     570                 :           0 :         vhd_bat_out(&bat);
     571                 :           0 :         err  = vhd_journal_update(j, off, (char *)bat.bat, size,
     572                 :             :                                   VHD_JOURNAL_ENTRY_TYPE_BAT);
     573                 :             : 
     574                 :           0 :         free(bat.bat);
     575                 :           0 :         return err;
     576                 :             : }
     577                 :             : 
     578                 :             : static int
     579                 :           0 : vhd_journal_add_batmap(vhd_journal_t *j)
     580                 :             : {
     581                 :           0 :         int err;
     582                 :           0 :         off64_t off;
     583                 :           0 :         size_t size;
     584                 :           0 :         vhd_context_t *vhd;
     585                 :           0 :         vhd_batmap_t batmap;
     586                 :             : 
     587                 :           0 :         vhd  = &j->vhd;
     588                 :             : 
     589                 :           0 :         off = vhd_batmap_header_offset(vhd);
     590                 :             : 
     591                 :           0 :         err  = vhd_read_batmap(vhd, &batmap);
     592         [ #  # ]:           0 :         if (err)
     593                 :           0 :                 return err;
     594                 :             : 
     595                 :           0 :         size = vhd_bytes_padded(sizeof(struct dd_batmap_hdr));
     596                 :             : 
     597                 :           0 :         vhd_batmap_header_out(&batmap);
     598                 :           0 :         err  = vhd_journal_update(j, off, (char *)&batmap.header, size,
     599                 :             :                                   VHD_JOURNAL_ENTRY_TYPE_BATMAP_H);
     600         [ #  # ]:           0 :         if (err)
     601                 :           0 :                 goto out;
     602                 :             : 
     603                 :           0 :         vhd_batmap_header_in(&batmap);
     604                 :           0 :         off  = batmap.header.batmap_offset;
     605                 :           0 :         size = vhd_sectors_to_bytes(batmap.header.batmap_size);
     606                 :             : 
     607                 :           0 :         err  = vhd_journal_update(j, off, batmap.map, size,
     608                 :             :                                   VHD_JOURNAL_ENTRY_TYPE_BATMAP_M);
     609                 :             : 
     610                 :           0 : out:
     611                 :           0 :         free(batmap.map);
     612                 :           0 :         return err;
     613                 :             : }
     614                 :             : 
     615                 :             : static int
     616                 :           0 : vhd_journal_add_metadata(vhd_journal_t *j)
     617                 :             : {
     618                 :           0 :         int err;
     619                 :           0 :         vhd_context_t *vhd;
     620                 :             : 
     621                 :           0 :         vhd = &j->vhd;
     622                 :             : 
     623                 :           0 :         err = vhd_journal_add_footer(j);
     624         [ #  # ]:           0 :         if (err)
     625                 :             :                 return err;
     626                 :             : 
     627         [ #  # ]:           0 :         if (!vhd_type_dynamic(vhd))
     628                 :             :                 return 0;
     629                 :             : 
     630                 :           0 :         err = vhd_journal_add_header(j);
     631         [ #  # ]:           0 :         if (err)
     632                 :             :                 return err;
     633                 :             : 
     634                 :           0 :         err = vhd_journal_add_locators(j);
     635         [ #  # ]:           0 :         if (err)
     636                 :             :                 return err;
     637                 :             : 
     638                 :           0 :         err = vhd_journal_add_bat(j);
     639         [ #  # ]:           0 :         if (err)
     640                 :             :                 return err;
     641                 :             : 
     642         [ #  # ]:           0 :         if (vhd_has_batmap(vhd)) {
     643                 :           0 :                 err = vhd_journal_add_batmap(j);
     644         [ #  # ]:           0 :                 if (err)
     645                 :             :                         return err;
     646                 :             :         }
     647                 :             : 
     648                 :           0 :         j->header.journal_data_offset = j->header.journal_eof;
     649                 :           0 :         return vhd_journal_write_header(j, &j->header);
     650                 :             : }
     651                 :             : 
     652                 :             : static int
     653                 :           0 : __vhd_journal_read_footer(vhd_journal_t *j,
     654                 :             :                           vhd_footer_t *footer, uint32_t type)
     655                 :             : {
     656                 :           0 :         int err;
     657                 :           0 :         vhd_journal_entry_t entry;
     658                 :             : 
     659                 :           0 :         err = vhd_journal_read_entry(j, &entry);
     660         [ #  # ]:           0 :         if (err)
     661                 :           0 :                 return err;
     662                 :             : 
     663         [ #  # ]:           0 :         if (entry.type != type)
     664                 :             :                 return -EINVAL;
     665                 :             : 
     666         [ #  # ]:           0 :         if (entry.size != sizeof(vhd_footer_t))
     667                 :             :                 return -EINVAL;
     668                 :             : 
     669                 :           0 :         err = vhd_journal_read(j, footer, entry.size);
     670         [ #  # ]:           0 :         if (err)
     671                 :             :                 return err;
     672                 :             : 
     673                 :           0 :         vhd_footer_in(footer);
     674                 :           0 :         return vhd_validate_footer(footer);
     675                 :             : }
     676                 :             : 
     677                 :             : static int
     678                 :           0 : vhd_journal_read_footer(vhd_journal_t *j, vhd_footer_t *footer)
     679                 :             : {
     680                 :           0 :         return __vhd_journal_read_footer(j, footer,
     681                 :             :                                          VHD_JOURNAL_ENTRY_TYPE_FOOTER_P);
     682                 :             : }
     683                 :             : 
     684                 :             : static int
     685                 :           0 : vhd_journal_read_footer_copy(vhd_journal_t *j, vhd_footer_t *footer)
     686                 :             : {
     687                 :           0 :         return __vhd_journal_read_footer(j, footer,
     688                 :             :                                          VHD_JOURNAL_ENTRY_TYPE_FOOTER_C);
     689                 :             : }
     690                 :             : 
     691                 :             : static int
     692                 :           0 : vhd_journal_read_header(vhd_journal_t *j, vhd_header_t *header)
     693                 :             : {
     694                 :           0 :         int err;
     695                 :           0 :         vhd_journal_entry_t entry;
     696                 :             : 
     697                 :           0 :         err = vhd_journal_read_entry(j, &entry);
     698         [ #  # ]:           0 :         if (err)
     699                 :           0 :                 return err;
     700                 :             : 
     701         [ #  # ]:           0 :         if (entry.type != VHD_JOURNAL_ENTRY_TYPE_HEADER)
     702                 :             :                 return -EINVAL;
     703                 :             : 
     704         [ #  # ]:           0 :         if (entry.size != sizeof(vhd_header_t))
     705                 :             :                 return -EINVAL;
     706                 :             : 
     707                 :           0 :         err = vhd_journal_read(j, header, entry.size);
     708         [ #  # ]:           0 :         if (err)
     709                 :             :                 return err;
     710                 :             : 
     711                 :           0 :         vhd_header_in(header);
     712                 :           0 :         return vhd_validate_header(header);
     713                 :             : }
     714                 :             : 
     715                 :             : static int
     716                 :           0 : vhd_journal_read_locators(vhd_journal_t *j, char ***locators, int *locs)
     717                 :             : {
     718                 :           0 :         int err, n, _locs;
     719                 :           0 :         char **_locators;
     720                 :           0 :         void *buf = NULL;
     721                 :           0 :         off_t pos;
     722                 :           0 :         vhd_journal_entry_t entry;
     723                 :             : 
     724                 :           0 :         _locs     = 0;
     725                 :           0 :         *locs     = 0;
     726                 :           0 :         *locators = NULL;
     727                 :             : 
     728                 :           0 :         n = sizeof(j->vhd.header.loc) / sizeof(vhd_parent_locator_t);
     729                 :           0 :         _locators = calloc(n, sizeof(char *));
     730         [ #  # ]:           0 :         if (!_locators)
     731                 :           0 :                 return -ENOMEM;
     732                 :             : 
     733                 :           0 :         for (;;) {
     734                 :           0 :                 pos = vhd_journal_position(j);
     735         [ #  # ]:           0 :                 if (pos == (off64_t)-1) {
     736                 :           0 :                         err = -errno;
     737                 :           0 :                         goto fail;
     738                 :             :                 }
     739                 :             : 
     740                 :           0 :                 err = vhd_journal_read_entry(j, &entry);
     741         [ #  # ]:           0 :                 if (err)
     742                 :           0 :                         goto fail;
     743                 :             : 
     744         [ #  # ]:           0 :                 if (entry.type != VHD_JOURNAL_ENTRY_TYPE_LOCATOR) {
     745                 :           0 :                         err = vhd_journal_seek(j, pos, SEEK_SET);
     746         [ #  # ]:           0 :                         if (err)
     747                 :           0 :                                 goto fail;
     748                 :           0 :                         break;
     749                 :             :                 }
     750                 :             : 
     751         [ #  # ]:           0 :                 if (_locs >= n) {
     752                 :           0 :                         err = -EINVAL;
     753                 :           0 :                         goto fail;
     754                 :             :                 }
     755                 :             : 
     756         [ #  # ]:           0 :                 err = posix_memalign(&buf, VHD_SECTOR_SIZE, entry.size);
     757                 :           0 :                 if (err) {
     758                 :           0 :                         err = -err;
     759                 :           0 :                         goto fail;
     760                 :             :                 }
     761                 :             : 
     762                 :           0 :                 err = vhd_journal_read(j, buf, entry.size);
     763         [ #  # ]:           0 :                 if (err)
     764                 :           0 :                         goto fail;
     765                 :             : 
     766                 :           0 :                 _locators[_locs++] = buf;
     767                 :           0 :                 buf                = NULL;
     768                 :           0 :                 err                = 0;
     769                 :             :         }
     770                 :             : 
     771                 :             : 
     772                 :           0 :         *locs     = _locs;
     773                 :           0 :         *locators = _locators;
     774                 :             : 
     775                 :           0 :         return 0;
     776                 :             : 
     777                 :           0 : fail:
     778                 :           0 :         free(buf);
     779                 :           0 :         if (_locators) {
     780         [ #  # ]:           0 :                 for (n = 0; n < _locs; n++)
     781                 :           0 :                         free(_locators[n]);
     782                 :           0 :                 free(_locators);
     783                 :             :         }
     784                 :           0 :         return err;
     785                 :             : }
     786                 :             : 
     787                 :             : static int
     788                 :           0 : vhd_journal_read_bat(vhd_journal_t *j, vhd_bat_t *bat)
     789                 :             : {
     790                 :           0 :         int err;
     791                 :           0 :         size_t size;
     792                 :           0 :         vhd_context_t *vhd;
     793                 :           0 :         vhd_journal_entry_t entry;
     794                 :           0 :         void *_bat;
     795                 :             : 
     796                 :           0 :         vhd  = &j->vhd;
     797                 :             : 
     798                 :           0 :         size = vhd_bytes_padded(vhd->header.max_bat_size * sizeof(uint32_t));
     799                 :             : 
     800                 :           0 :         err  = vhd_journal_read_entry(j, &entry);
     801         [ #  # ]:           0 :         if (err)
     802                 :           0 :                 return err;
     803                 :             : 
     804         [ #  # ]:           0 :         if (entry.type != VHD_JOURNAL_ENTRY_TYPE_BAT)
     805                 :             :                 return -EINVAL;
     806                 :             : 
     807         [ #  # ]:           0 :         if (entry.size != size)
     808                 :             :                 return -EINVAL;
     809                 :             : 
     810         [ #  # ]:           0 :         if (entry.offset != vhd->header.table_offset)
     811                 :             :                 return -EINVAL;
     812                 :             : 
     813         [ #  # ]:           0 :         err = posix_memalign(&_bat, VHD_SECTOR_SIZE, size);
     814                 :           0 :         if (err)
     815                 :           0 :                 return -err;
     816                 :           0 :         bat->bat = _bat;
     817                 :             : 
     818                 :           0 :         err = vhd_journal_read(j, bat->bat, entry.size);
     819         [ #  # ]:           0 :         if (err)
     820                 :           0 :                 goto fail;
     821                 :             : 
     822                 :           0 :         bat->spb     = vhd->header.block_size >> VHD_SECTOR_SHIFT;
     823                 :           0 :         bat->entries = vhd->header.max_bat_size;
     824                 :           0 :         vhd_bat_in(bat);
     825                 :             : 
     826                 :             :         return 0;
     827                 :             : 
     828                 :           0 : fail:
     829                 :           0 :         free(bat->bat);
     830                 :           0 :         bat->bat = NULL;
     831                 :           0 :         return err;
     832                 :             : }
     833                 :             : 
     834                 :             : static int
     835                 :           0 : vhd_journal_read_batmap_header(vhd_journal_t *j, vhd_batmap_t *batmap)
     836                 :             : {
     837                 :           0 :         int err;
     838                 :           0 :         void *buf;
     839                 :           0 :         size_t size;
     840                 :           0 :         vhd_journal_entry_t entry;
     841                 :             : 
     842                 :           0 :         size = vhd_bytes_padded(sizeof(struct dd_batmap_hdr));
     843                 :             : 
     844                 :           0 :         err  = vhd_journal_read_entry(j, &entry);
     845         [ #  # ]:           0 :         if (err)
     846                 :           0 :                 return err;
     847                 :             : 
     848         [ #  # ]:           0 :         if (entry.type != VHD_JOURNAL_ENTRY_TYPE_BATMAP_H)
     849                 :             :                 return -EINVAL;
     850                 :             : 
     851         [ #  # ]:           0 :         if (entry.size != size)
     852                 :             :                 return -EINVAL;
     853                 :             : 
     854         [ #  # ]:           0 :         err = posix_memalign(&buf, VHD_SECTOR_SIZE, size);
     855                 :           0 :         if (err)
     856                 :             :                 return err;
     857                 :             : 
     858                 :           0 :         err = vhd_journal_read(j, buf, entry.size);
     859         [ #  # ]:           0 :         if (err) {
     860                 :           0 :                 free(buf);
     861                 :           0 :                 return err;
     862                 :             :         }
     863                 :             : 
     864                 :           0 :         memcpy(&batmap->header, buf, sizeof(batmap->header));
     865                 :           0 :         free(buf);
     866                 :             : 
     867                 :           0 :         vhd_batmap_header_in(batmap);
     868                 :           0 :         return vhd_validate_batmap_header(batmap);
     869                 :             : }
     870                 :             : 
     871                 :             : static int
     872                 :           0 : vhd_journal_read_batmap_map(vhd_journal_t *j, vhd_batmap_t *batmap)
     873                 :             : {
     874                 :           0 :         int err;
     875                 :           0 :         vhd_journal_entry_t entry;
     876                 :           0 :         void *map;
     877                 :             : 
     878                 :           0 :         err  = vhd_journal_read_entry(j, &entry);
     879         [ #  # ]:           0 :         if (err)
     880                 :           0 :                 return err;
     881                 :             : 
     882         [ #  # ]:           0 :         if (entry.type != VHD_JOURNAL_ENTRY_TYPE_BATMAP_M)
     883                 :             :                 return -EINVAL;
     884                 :             : 
     885         [ #  # ]:           0 :         if (entry.size != vhd_sectors_to_bytes(batmap->header.batmap_size))
     886                 :             :                 return -EINVAL;
     887                 :             : 
     888         [ #  # ]:           0 :         if (entry.offset != batmap->header.batmap_offset)
     889                 :             :                 return -EINVAL;
     890                 :             : 
     891         [ #  # ]:           0 :         err = posix_memalign(&map, VHD_SECTOR_SIZE, entry.size);
     892                 :           0 :         if (err)
     893                 :           0 :                 return -err;
     894                 :             : 
     895                 :           0 :         batmap->map = map;
     896                 :             : 
     897                 :           0 :         err = vhd_journal_read(j, batmap->map, entry.size);
     898         [ #  # ]:           0 :         if (err) {
     899                 :           0 :                 free(batmap->map);
     900                 :           0 :                 batmap->map = NULL;
     901                 :           0 :                 return err;
     902                 :             :         }
     903                 :             : 
     904                 :             :         return 0;
     905                 :             : }
     906                 :             : 
     907                 :             : static int
     908                 :           0 : vhd_journal_read_batmap(vhd_journal_t *j, vhd_batmap_t *batmap)
     909                 :             : {
     910                 :           0 :         int err;
     911                 :             : 
     912                 :           0 :         err = vhd_journal_read_batmap_header(j, batmap);
     913         [ #  # ]:           0 :         if (err)
     914                 :             :                 return err;
     915                 :             : 
     916                 :           0 :         err = vhd_journal_read_batmap_map(j, batmap);
     917         [ #  # ]:           0 :         if (err)
     918                 :             :                 return err;
     919                 :             : 
     920                 :           0 :         err = vhd_validate_batmap(&j->vhd, batmap);
     921         [ #  # ]:           0 :         if (err) {
     922                 :           0 :                 free(batmap->map);
     923                 :           0 :                 batmap->map = NULL;
     924                 :           0 :                 return err;
     925                 :             :         }
     926                 :             : 
     927                 :             :         return 0;
     928                 :             : }
     929                 :             : 
     930                 :             : static int
     931                 :           0 : vhd_journal_restore_footer(vhd_journal_t *j, vhd_footer_t *footer)
     932                 :             : {
     933                 :           0 :         return vhd_write_footer_at(&j->vhd, footer,
     934                 :           0 :                                    j->header.vhd_footer_offset);
     935                 :             : }
     936                 :             : 
     937                 :             : static int
     938                 :           0 : vhd_journal_restore_footer_copy(vhd_journal_t *j, vhd_footer_t *footer)
     939                 :             : {
     940                 :           0 :         return vhd_write_footer_at(&j->vhd, footer, 0);
     941                 :             : }
     942                 :             : 
     943                 :             : static int
     944                 :           0 : vhd_journal_restore_header(vhd_journal_t *j, vhd_header_t *header)
     945                 :             : {
     946                 :           0 :         off64_t off;
     947                 :           0 :         vhd_context_t *vhd;
     948                 :             : 
     949                 :           0 :         vhd = &j->vhd;
     950                 :           0 :         off = vhd->footer.data_offset;
     951                 :             : 
     952                 :           0 :         return vhd_write_header_at(&j->vhd, header, off);
     953                 :             : }
     954                 :             : 
     955                 :             : static int
     956                 :           0 : vhd_journal_restore_locators(vhd_journal_t *j, char **locators, int locs)
     957                 :             : {
     958                 :           0 :         size_t size;
     959                 :           0 :         vhd_context_t *vhd;
     960                 :           0 :         int i, n, lidx, err;
     961                 :           0 :         vhd_parent_locator_t *loc;
     962                 :             : 
     963                 :           0 :         lidx = 0;
     964                 :           0 :         vhd  = &j->vhd;
     965                 :             : 
     966                 :           0 :         n = sizeof(vhd->header.loc) / sizeof(vhd_parent_locator_t);
     967                 :             : 
     968         [ #  # ]:           0 :         for (i = 0; i < n && lidx < locs; i++) {
     969                 :           0 :                 loc  = vhd->header.loc + i;
     970         [ #  # ]:           0 :                 if (loc->code == PLAT_CODE_NONE)
     971                 :           0 :                         continue;
     972                 :             : 
     973                 :           0 :                 err  = vhd_seek(vhd, loc->data_offset, SEEK_SET);
     974         [ #  # ]:           0 :                 if (err)
     975                 :             :                         return err;
     976                 :             : 
     977         [ #  # ]:           0 :                 size = vhd_parent_locator_size(loc);
     978                 :           0 :                 err  = vhd_write(vhd, locators[lidx++], size);
     979         [ #  # ]:           0 :                 if (err)
     980                 :             :                         return err;
     981                 :             :         }
     982                 :             : 
     983                 :             :         return 0;
     984                 :             : }
     985                 :             : 
     986                 :             : static int
     987                 :           0 : vhd_journal_restore_bat(vhd_journal_t *j, vhd_bat_t *bat)
     988                 :             : {
     989                 :           0 :         return vhd_write_bat(&j->vhd, bat);
     990                 :             : }
     991                 :             : 
     992                 :             : static int
     993                 :           0 : vhd_journal_restore_batmap(vhd_journal_t *j, vhd_batmap_t *batmap)
     994                 :             : {
     995                 :           0 :         return vhd_write_batmap(&j->vhd, batmap);
     996                 :             : }
     997                 :             : 
     998                 :             : static int
     999                 :           0 : vhd_journal_restore_metadata(vhd_journal_t *j)
    1000                 :             : {
    1001                 :           0 :         off64_t off;
    1002                 :           0 :         char **locators;
    1003                 :           0 :         vhd_footer_t copy;
    1004                 :           0 :         vhd_context_t *vhd;
    1005                 :           0 :         int i, locs, hlocs, err;
    1006                 :             : 
    1007                 :           0 :         vhd      = &j->vhd;
    1008                 :           0 :         locs     = 0;
    1009                 :           0 :         hlocs    = 0;
    1010                 :           0 :         locators = NULL;
    1011                 :             : 
    1012                 :           0 :         err = vhd_journal_seek(j, sizeof(vhd_journal_header_t), SEEK_SET);
    1013         [ #  # ]:           0 :         if (err)
    1014                 :           0 :                 return err;
    1015                 :             : 
    1016                 :           0 :         err  = vhd_journal_read_footer(j, &vhd->footer);
    1017         [ #  # ]:           0 :         if (err)
    1018                 :             :                 return err;
    1019                 :             : 
    1020         [ #  # ]:           0 :         if (!vhd_type_dynamic(vhd))
    1021                 :           0 :                 goto restore;
    1022                 :             : 
    1023                 :           0 :         err  = vhd_journal_read_footer_copy(j, &copy);
    1024         [ #  # ]:           0 :         if (err)
    1025                 :             :                 return err;
    1026                 :             : 
    1027                 :           0 :         err  = vhd_journal_read_header(j, &vhd->header);
    1028         [ #  # ]:           0 :         if (err)
    1029                 :             :                 return err;
    1030                 :             : 
    1031         [ #  # ]:           0 :         for (hlocs = 0, i = 0; i < vhd_parent_locator_count(vhd); i++) {
    1032         [ #  # ]:           0 :                 if (vhd_validate_platform_code(vhd->header.loc[i].code))
    1033                 :             :                         return err;
    1034                 :             : 
    1035         [ #  # ]:           0 :                 if (vhd->header.loc[i].code != PLAT_CODE_NONE)
    1036                 :           0 :                         hlocs++;
    1037                 :             :         }
    1038                 :             : 
    1039         [ #  # ]:           0 :         if (hlocs) {
    1040                 :           0 :                 err  = vhd_journal_read_locators(j, &locators, &locs);
    1041         [ #  # ]:           0 :                 if (err)
    1042                 :             :                         return err;
    1043                 :             : 
    1044         [ #  # ]:           0 :                 if (hlocs != locs) {
    1045                 :           0 :                         err = -EINVAL;
    1046                 :           0 :                         goto out;
    1047                 :             :                 }
    1048                 :             :         }
    1049                 :             : 
    1050                 :           0 :         err  = vhd_journal_read_bat(j, &vhd->bat);
    1051         [ #  # ]:           0 :         if (err)
    1052                 :           0 :                 goto out;
    1053                 :             : 
    1054         [ #  # ]:           0 :         if (vhd_has_batmap(vhd)) {
    1055                 :           0 :                 err  = vhd_journal_read_batmap(j, &vhd->batmap);
    1056         [ #  # ]:           0 :                 if (err)
    1057                 :           0 :                         goto out;
    1058                 :             :         }
    1059                 :             : 
    1060                 :           0 : restore:
    1061                 :           0 :         off  = vhd_journal_position(j);
    1062         [ #  # ]:           0 :         if (off == (off64_t)-1) {
    1063                 :           0 :                 err = -errno;
    1064                 :           0 :                 goto out;
    1065                 :             :         }
    1066                 :             : 
    1067         [ #  # ]:           0 :         if (j->header.journal_data_offset != off) {
    1068                 :           0 :                 err = -EINVAL;
    1069                 :           0 :                 goto out;
    1070                 :             :         }
    1071                 :             : 
    1072                 :           0 :         err  = vhd_journal_restore_footer(j, &vhd->footer);
    1073         [ #  # ]:           0 :         if (err)
    1074                 :           0 :                 goto out;
    1075                 :             : 
    1076         [ #  # ]:           0 :         if (!vhd_type_dynamic(vhd))
    1077                 :           0 :                 goto out;
    1078                 :             : 
    1079                 :           0 :         err  = vhd_journal_restore_footer_copy(j, &copy);
    1080         [ #  # ]:           0 :         if (err)
    1081                 :           0 :                 goto out;
    1082                 :             : 
    1083                 :           0 :         err  = vhd_journal_restore_header(j, &vhd->header);
    1084         [ #  # ]:           0 :         if (err)
    1085                 :           0 :                 goto out;
    1086                 :             : 
    1087         [ #  # ]:           0 :         if (locs) {
    1088                 :           0 :                 err = vhd_journal_restore_locators(j, locators, locs);
    1089         [ #  # ]:           0 :                 if (err)
    1090                 :           0 :                         goto out;
    1091                 :             :         }
    1092                 :             : 
    1093                 :           0 :         err  = vhd_journal_restore_bat(j, &vhd->bat);
    1094         [ #  # ]:           0 :         if (err)
    1095                 :           0 :                 goto out;
    1096                 :             : 
    1097         [ #  # ]:           0 :         if (vhd_has_batmap(vhd)) {
    1098                 :           0 :                 err  = vhd_journal_restore_batmap(j, &vhd->batmap);
    1099         [ #  # ]:           0 :                 if (err)
    1100                 :           0 :                         goto out;
    1101                 :             :         }
    1102                 :             : 
    1103                 :             :         err = 0;
    1104                 :             : 
    1105                 :           0 : out:
    1106         [ #  # ]:           0 :         if (locators) {
    1107         [ #  # ]:           0 :                 for (i = 0; i < locs; i++)
    1108                 :           0 :                         free(locators[i]);
    1109                 :           0 :                 free(locators);
    1110                 :             :         }
    1111                 :             : 
    1112   [ #  #  #  # ]:           0 :         if (!err && !vhd->is_block)
    1113                 :           0 :                 err = ftruncate(vhd->fd,
    1114                 :           0 :                           j->header.vhd_footer_offset +
    1115                 :             :                           sizeof(vhd_footer_t));
    1116                 :             : 
    1117                 :             :         return err;
    1118                 :             : }
    1119                 :             : 
    1120                 :             : static int
    1121                 :           0 : vhd_journal_disable_vhd(vhd_journal_t *j)
    1122                 :             : {
    1123                 :           0 :         int err;
    1124                 :           0 :         vhd_context_t *vhd;
    1125                 :             : 
    1126                 :           0 :         vhd = &j->vhd;
    1127                 :             : 
    1128                 :           0 :         err = vhd_get_footer(vhd);
    1129         [ #  # ]:           0 :         if (err)
    1130                 :             :                 return err;
    1131                 :             : 
    1132                 :           0 :         memcpy(&vhd->footer.cookie,
    1133                 :             :                VHD_POISON_COOKIE, sizeof(vhd->footer.cookie));
    1134                 :           0 :         vhd->footer.checksum = vhd_checksum_footer(&vhd->footer);
    1135                 :             : 
    1136                 :           0 :         err = vhd_write_footer(vhd, &vhd->footer);
    1137                 :           0 :         if (err)
    1138                 :             :                 return err;
    1139                 :             : 
    1140                 :             :         return 0;
    1141                 :             : }
    1142                 :             : 
    1143                 :             : static int
    1144                 :           0 : vhd_journal_enable_vhd(vhd_journal_t *j)
    1145                 :             : {
    1146                 :           0 :         int err;
    1147                 :           0 :         vhd_context_t *vhd;
    1148                 :             : 
    1149                 :           0 :         vhd = &j->vhd;
    1150                 :             : 
    1151                 :           0 :         err = vhd_get_footer(vhd);
    1152         [ #  # ]:           0 :         if (err)
    1153                 :             :                 return err;
    1154                 :             : 
    1155         [ #  # ]:           0 :         if (!vhd_disabled(vhd))
    1156                 :             :                 return 0;
    1157                 :             : 
    1158                 :           0 :         memcpy(&vhd->footer.cookie, HD_COOKIE, sizeof(vhd->footer.cookie));
    1159                 :           0 :         vhd->footer.checksum = vhd_checksum_footer(&vhd->footer);
    1160                 :             : 
    1161                 :           0 :         err = vhd_write_footer(vhd, &vhd->footer);
    1162         [ #  # ]:           0 :         if (err)
    1163                 :             :                 return err;
    1164                 :             : 
    1165                 :             :         return 0;
    1166                 :             : }
    1167                 :             : 
    1168                 :             : int
    1169                 :           0 : vhd_journal_close(vhd_journal_t *j)
    1170                 :             : {
    1171         [ #  # ]:           0 :         if (j->jfd)
    1172                 :           0 :                 close(j->jfd);
    1173                 :             : 
    1174                 :           0 :         vhd_close(&j->vhd);
    1175                 :           0 :         free(j->jname);
    1176                 :             : 
    1177                 :           0 :         return 0;
    1178                 :             : }
    1179                 :             : 
    1180                 :             : int
    1181                 :           0 : vhd_journal_remove(vhd_journal_t *j)
    1182                 :             : {
    1183                 :           0 :         int err;
    1184                 :             : 
    1185                 :           0 :         err = vhd_journal_enable_vhd(j);
    1186         [ #  # ]:           0 :         if (err)
    1187                 :             :                 return err;
    1188                 :             : 
    1189         [ #  # ]:           0 :         if (j->jfd) {
    1190                 :           0 :                 close(j->jfd);
    1191         [ #  # ]:           0 :                 if (!j->is_block)
    1192                 :           0 :                         unlink(j->jname);
    1193                 :             :         }
    1194                 :             : 
    1195                 :           0 :         vhd_close(&j->vhd);
    1196                 :           0 :         free(j->jname);
    1197                 :             : 
    1198                 :           0 :         return 0;
    1199                 :             : }
    1200                 :             : 
    1201                 :             : int
    1202                 :           0 : vhd_journal_open(vhd_journal_t *j, const char *file, const char *jfile)
    1203                 :             : {
    1204                 :           0 :         int err;
    1205                 :           0 :         vhd_context_t *vhd;
    1206                 :             : 
    1207         [ #  # ]:           0 :         memset(j, 0, sizeof(vhd_journal_t));
    1208                 :             : 
    1209                 :           0 :         j->jfd = -1;
    1210                 :           0 :         vhd    = &j->vhd;
    1211                 :             : 
    1212                 :           0 :         j->jname = strdup(jfile);
    1213         [ #  # ]:           0 :         if (j->jname == NULL)
    1214                 :             :                 return -ENOMEM;
    1215                 :             : 
    1216                 :           0 :         j->jfd = open(j->jname, O_LARGEFILE | O_RDWR);
    1217         [ #  # ]:           0 :         if (j->jfd == -1) {
    1218                 :           0 :                 err = -errno;
    1219                 :           0 :                 goto fail;
    1220                 :             :         }
    1221                 :             : 
    1222                 :           0 :         err = vhd_test_file_fixed(j->jname, &j->is_block);
    1223         [ #  # ]:           0 :         if (err)
    1224                 :           0 :                 goto fail;
    1225                 :             : 
    1226                 :           0 :         vhd->fd = open_optional_odirect(file, O_LARGEFILE | O_RDWR | O_DIRECT);
    1227         [ #  # ]:           0 :         if (vhd->fd == -1) {
    1228                 :           0 :                 err = -errno;
    1229                 :           0 :                 goto fail;
    1230                 :             :         }
    1231                 :             : 
    1232                 :           0 :         err = vhd_test_file_fixed(file, &vhd->is_block);
    1233         [ #  # ]:           0 :         if (err)
    1234                 :           0 :                 goto fail;
    1235                 :             : 
    1236                 :           0 :         err = vhd_journal_read_journal_header(j, &j->header);
    1237         [ #  # ]:           0 :         if (err)
    1238                 :           0 :                 goto fail;
    1239                 :             : 
    1240                 :           0 :         err = vhd_journal_restore_metadata(j);
    1241         [ #  # ]:           0 :         if (err)
    1242                 :           0 :                 goto fail;
    1243                 :             : 
    1244                 :           0 :         close(vhd->fd);
    1245                 :           0 :         free(vhd->bat.bat);
    1246                 :           0 :         free(vhd->batmap.map);
    1247                 :             : 
    1248                 :           0 :         err = vhd_open(vhd, file, VHD_OPEN_RDWR);
    1249         [ #  # ]:           0 :         if (err)
    1250                 :           0 :                 goto fail;
    1251                 :             : 
    1252                 :           0 :         err = vhd_get_bat(vhd);
    1253         [ #  # ]:           0 :         if (err)
    1254                 :           0 :                 goto fail;
    1255                 :             : 
    1256         [ #  # ]:           0 :         if (vhd_has_batmap(vhd)) {
    1257                 :           0 :                 err = vhd_get_batmap(vhd);
    1258         [ #  # ]:           0 :                 if (err)
    1259                 :           0 :                         goto fail;
    1260                 :             :         }
    1261                 :             : 
    1262                 :           0 :         err = vhd_journal_disable_vhd(j);
    1263         [ #  # ]:           0 :         if (err)
    1264                 :           0 :                 goto fail;
    1265                 :             : 
    1266                 :             :         return 0;
    1267                 :             : 
    1268                 :           0 : fail:
    1269                 :           0 :         vhd_journal_close(j);
    1270                 :           0 :         return err;
    1271                 :             : }
    1272                 :             : 
    1273                 :             : int
    1274                 :           0 : vhd_journal_create(vhd_journal_t *j, const char *file, const char *jfile)
    1275                 :             : {
    1276                 :           0 :         int err;
    1277                 :             : 
    1278         [ #  # ]:           0 :         memset(j, 0, sizeof(vhd_journal_t));
    1279                 :           0 :         j->jfd = -1;
    1280                 :             : 
    1281                 :           0 :         j->jname = strdup(jfile);
    1282         [ #  # ]:           0 :         if (j->jname == NULL) {
    1283                 :           0 :                 err = -ENOMEM;
    1284                 :           0 :                 goto fail1;
    1285                 :             :         }
    1286                 :             : 
    1287         [ #  # ]:           0 :         if (access(j->jname, F_OK) == 0) {
    1288                 :           0 :                 err = vhd_test_file_fixed(j->jname, &j->is_block);
    1289         [ #  # ]:           0 :                 if (err)
    1290                 :           0 :                         goto fail1;
    1291                 :             : 
    1292         [ #  # ]:           0 :                 if (!j->is_block) {
    1293                 :           0 :                         err = -EEXIST;
    1294                 :           0 :                         goto fail1;
    1295                 :             :                 }
    1296                 :             :         }
    1297                 :             : 
    1298         [ #  # ]:           0 :         if (j->is_block)
    1299                 :           0 :                 j->jfd = open(j->jname, O_LARGEFILE | O_RDWR, 0644);
    1300                 :             :         else
    1301                 :           0 :                 j->jfd = open(j->jname,
    1302                 :             :                               O_CREAT | O_TRUNC | O_LARGEFILE | O_RDWR, 0644);
    1303         [ #  # ]:           0 :         if (j->jfd == -1) {
    1304                 :           0 :                 err = -errno;
    1305                 :           0 :                 goto fail1;
    1306                 :             :         }
    1307                 :             : 
    1308                 :           0 :         err = vhd_open(&j->vhd, file, VHD_OPEN_RDWR | VHD_OPEN_STRICT);
    1309         [ #  # ]:           0 :         if (err)
    1310                 :           0 :                 goto fail1;
    1311                 :             : 
    1312                 :           0 :         err = vhd_get_bat(&j->vhd);
    1313         [ #  # ]:           0 :         if (err)
    1314                 :           0 :                 goto fail2;
    1315                 :             : 
    1316         [ #  # ]:           0 :         if (vhd_has_batmap(&j->vhd)) {
    1317                 :           0 :                 err = vhd_get_batmap(&j->vhd);
    1318         [ #  # ]:           0 :                 if (err)
    1319                 :           0 :                         goto fail2;
    1320                 :             :         }
    1321                 :             : 
    1322                 :           0 :         err = vhd_journal_add_journal_header(j);
    1323         [ #  # ]:           0 :         if (err)
    1324                 :           0 :                 goto fail2;
    1325                 :             : 
    1326                 :           0 :         err = vhd_journal_add_metadata(j);
    1327         [ #  # ]:           0 :         if (err)
    1328                 :           0 :                 goto fail2;
    1329                 :             : 
    1330                 :           0 :         err = vhd_journal_disable_vhd(j);
    1331         [ #  # ]:           0 :         if (err)
    1332                 :           0 :                 goto fail2;
    1333                 :             : 
    1334                 :           0 :         err = vhd_journal_sync(j);
    1335         [ #  # ]:           0 :         if (err)
    1336                 :           0 :                 goto fail2;
    1337                 :             : 
    1338                 :             :         return 0;
    1339                 :             : 
    1340                 :           0 : fail1:
    1341         [ #  # ]:           0 :         if (j->jfd != -1) {
    1342                 :           0 :                 close(j->jfd);
    1343         [ #  # ]:           0 :                 if (!j->is_block)
    1344                 :           0 :                         unlink(j->jname);
    1345                 :             :         }
    1346                 :           0 :         free(j->jname);
    1347                 :           0 :         memset(j, 0, sizeof(vhd_journal_t));
    1348                 :             : 
    1349                 :           0 :         return err;
    1350                 :             : 
    1351                 :           0 : fail2:
    1352                 :           0 :         vhd_journal_remove(j);
    1353                 :           0 :         return err;
    1354                 :             : }
    1355                 :             : 
    1356                 :             : int
    1357                 :           0 : vhd_journal_add_block(vhd_journal_t *j, uint32_t block, char mode)
    1358                 :             : {
    1359                 :           0 :         int err;
    1360                 :           0 :         char *buf;
    1361                 :           0 :         off64_t off;
    1362                 :           0 :         size_t size;
    1363                 :           0 :         uint64_t blk;
    1364                 :           0 :         vhd_context_t *vhd;
    1365                 :             : 
    1366                 :           0 :         buf = NULL;
    1367                 :           0 :         vhd = &j->vhd;
    1368                 :             : 
    1369         [ #  # ]:           0 :         if (!vhd_type_dynamic(vhd))
    1370                 :           0 :                 return -EINVAL;
    1371                 :             : 
    1372                 :           0 :         err = vhd_get_bat(vhd);
    1373         [ #  # ]:           0 :         if (err)
    1374                 :             :                 return err;
    1375                 :             : 
    1376         [ #  # ]:           0 :         if (block >= vhd->bat.entries)
    1377                 :             :                 return -ERANGE;
    1378                 :             : 
    1379                 :           0 :         blk = vhd->bat.bat[block];
    1380         [ #  # ]:           0 :         if (blk == DD_BLK_UNUSED)
    1381                 :             :                 return 0;
    1382                 :             : 
    1383         [ #  # ]:           0 :         off = vhd_sectors_to_bytes(blk);
    1384                 :             : 
    1385         [ #  # ]:           0 :         if (mode & VHD_JOURNAL_METADATA) {
    1386                 :           0 :                 size = vhd_sectors_to_bytes(vhd->bm_secs);
    1387                 :             : 
    1388                 :           0 :                 err  = vhd_read_bitmap(vhd, block, &buf);
    1389         [ #  # ]:           0 :                 if (err)
    1390                 :             :                         return err;
    1391                 :             : 
    1392                 :           0 :                 err  = vhd_journal_update(j, off, buf, size,
    1393                 :             :                                           VHD_JOURNAL_ENTRY_TYPE_DATA);
    1394                 :             : 
    1395                 :           0 :                 free(buf);
    1396                 :             : 
    1397         [ #  # ]:           0 :                 if (err)
    1398                 :             :                         return err;
    1399                 :             :         }
    1400                 :             : 
    1401         [ #  # ]:           0 :         if (mode & VHD_JOURNAL_DATA) {
    1402                 :           0 :                 off += vhd_sectors_to_bytes(vhd->bm_secs);
    1403                 :           0 :                 size = vhd_sectors_to_bytes(vhd->spb);
    1404                 :             : 
    1405                 :           0 :                 err  = vhd_read_block(vhd, block, &buf);
    1406         [ #  # ]:           0 :                 if (err)
    1407                 :             :                         return err;
    1408                 :             : 
    1409                 :           0 :                 err  = vhd_journal_update(j, off, buf, size,
    1410                 :             :                                           VHD_JOURNAL_ENTRY_TYPE_DATA);
    1411                 :           0 :                 free(buf);
    1412                 :             : 
    1413         [ #  # ]:           0 :                 if (err)
    1414                 :             :                         return err;
    1415                 :             :         }
    1416                 :             : 
    1417                 :           0 :         return vhd_journal_sync(j);
    1418                 :             : }
    1419                 :             : 
    1420                 :             : /*
    1421                 :             :  * commit indicates the transaction completed 
    1422                 :             :  * successfully and we can remove the undo log
    1423                 :             :  */
    1424                 :             : int
    1425                 :           0 : vhd_journal_commit(vhd_journal_t *j)
    1426                 :             : {
    1427                 :           0 :         int err;
    1428                 :             : 
    1429                 :           0 :         j->header.journal_data_entries     = 0;
    1430                 :           0 :         j->header.journal_metadata_entries = 0;
    1431                 :           0 :         j->header.journal_data_offset      = 0;
    1432                 :           0 :         j->header.journal_metadata_offset  = 0;
    1433                 :             : 
    1434                 :           0 :         err = vhd_journal_write_header(j, &j->header);
    1435         [ #  # ]:           0 :         if (err)
    1436                 :             :                 return err;
    1437                 :             : 
    1438         [ #  # ]:           0 :         if (!j->is_block)
    1439                 :           0 :                 err = vhd_journal_truncate(j, sizeof(vhd_journal_header_t));
    1440         [ #  # ]:           0 :         if (err)
    1441                 :           0 :                 return -errno;
    1442                 :             : 
    1443                 :             :         return 0;
    1444                 :             : }
    1445                 :             : 
    1446                 :             : /*
    1447                 :             :  * revert indicates the transaction failed
    1448                 :             :  * and we should revert any changes via the undo log
    1449                 :             :  */
    1450                 :             : int
    1451                 :           0 : vhd_journal_revert(vhd_journal_t *j)
    1452                 :             : {
    1453                 :           0 :         int i, err;
    1454                 :           0 :         char *file;
    1455                 :           0 :         void *buf;
    1456                 :           0 :         vhd_context_t *vhd;
    1457                 :           0 :         vhd_journal_entry_t entry;
    1458                 :             : 
    1459                 :           0 :         err  = 0;
    1460                 :           0 :         vhd  = &j->vhd;
    1461                 :           0 :         buf  = NULL;
    1462                 :             : 
    1463                 :           0 :         file = strdup(vhd->file);
    1464         [ #  # ]:           0 :         if (!file)
    1465                 :           0 :                 return -ENOMEM;
    1466                 :             : 
    1467                 :           0 :         vhd_close(&j->vhd);
    1468                 :           0 :         j->vhd.fd = open_optional_odirect(file, O_RDWR | O_DIRECT | O_LARGEFILE);
    1469         [ #  # ]:           0 :         if (j->vhd.fd == -1) {
    1470                 :           0 :                 free(file);
    1471                 :           0 :                 return -errno;
    1472                 :             :         }
    1473                 :             : 
    1474                 :           0 :         err = vhd_test_file_fixed(file, &vhd->is_block);
    1475         [ #  # ]:           0 :         if (err) {
    1476                 :           0 :                 free(file);
    1477                 :           0 :                 return err;
    1478                 :             :         }
    1479                 :             : 
    1480                 :           0 :         err  = vhd_journal_restore_metadata(j);
    1481         [ #  # ]:           0 :         if (err) {
    1482                 :           0 :                 free(file);
    1483                 :           0 :                 return err;
    1484                 :             :         }
    1485                 :             : 
    1486                 :           0 :         close(vhd->fd);
    1487                 :           0 :         free(vhd->bat.bat);
    1488                 :           0 :         free(vhd->batmap.map);
    1489                 :             : 
    1490                 :           0 :         err = vhd_open(vhd, file, VHD_OPEN_RDWR);
    1491                 :           0 :         free(file);
    1492         [ #  # ]:           0 :         if (err)
    1493                 :             :                 return err;
    1494                 :             : 
    1495                 :           0 :         err = vhd_journal_seek(j, j->header.journal_data_offset, SEEK_SET);
    1496         [ #  # ]:           0 :         if (err)
    1497                 :             :                 return err;
    1498                 :             : 
    1499         [ #  # ]:           0 :         for (i = 0; i < j->header.journal_data_entries; i++) {
    1500                 :           0 :                 err = vhd_journal_read_entry(j, &entry);
    1501         [ #  # ]:           0 :                 if (err)
    1502                 :           0 :                         goto end;
    1503                 :             : 
    1504         [ #  # ]:           0 :                 err = posix_memalign(&buf, VHD_SECTOR_SIZE, entry.size);
    1505                 :           0 :                 if (err) {
    1506                 :           0 :                         err = -err;
    1507                 :           0 :                         buf = NULL;
    1508                 :           0 :                         goto end;
    1509                 :             :                 }
    1510                 :             : 
    1511                 :           0 :                 err = vhd_journal_read(j, buf, entry.size);
    1512         [ #  # ]:           0 :                 if (err)
    1513                 :           0 :                         goto end;
    1514                 :             : 
    1515                 :           0 :                 err = vhd_journal_validate_entry_data(&entry, buf);
    1516         [ #  # ]:           0 :                 if (err)
    1517                 :           0 :                         goto end;
    1518                 :             : 
    1519                 :           0 :                 err = vhd_seek(vhd, entry.offset, SEEK_SET);
    1520         [ #  # ]:           0 :                 if (err)
    1521                 :           0 :                         goto end;
    1522                 :             : 
    1523                 :           0 :                 err = vhd_write(vhd, buf, entry.size);
    1524                 :             :                 if (err)
    1525                 :             :                         goto end;
    1526                 :             : 
    1527                 :             :                 err = 0;
    1528                 :             : 
    1529                 :           0 :         end:
    1530                 :           0 :                 free(buf);
    1531                 :           0 :                 buf = NULL;
    1532         [ #  # ]:           0 :                 if (err)
    1533                 :             :                         break;
    1534                 :             :         }
    1535                 :             : 
    1536         [ #  # ]:           0 :         if (err)
    1537                 :             :                 return err;
    1538                 :             : 
    1539         [ #  # ]:           0 :         if (!vhd->is_block) {
    1540                 :           0 :                 err = ftruncate(vhd->fd, j->header.vhd_footer_offset +
    1541                 :             :                                 sizeof(vhd_footer_t));
    1542         [ #  # ]:           0 :                 if (err)
    1543                 :           0 :                         return -errno;
    1544                 :             :         }
    1545                 :             : 
    1546                 :           0 :         return vhd_journal_sync(j);
    1547                 :             : }
        

Generated by: LCOV version 2.0-1