File: hdimage.cc

package info (click to toggle)
bochs 2.3-2etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 14,116 kB
  • ctags: 16,927
  • sloc: cpp: 130,524; ansic: 18,822; sh: 7,922; makefile: 3,836; yacc: 1,056; asm: 463; perl: 381; lex: 280; csh: 3
file content (1734 lines) | stat: -rw-r--r-- 45,898 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
/////////////////////////////////////////////////////////////////////////
// $Id: hdimage.cc,v 1.8 2006/06/16 08:56:13 vruppert Exp $
/////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2002  MandrakeSoft S.A.
//
//    MandrakeSoft S.A.
//    43, rue d'Aboukir
//    75002 Paris - France
//    http://www.linux-mandrake.com/
//    http://www.mandrakesoft.com/
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA


// Define BX_PLUGGABLE in files that can be compiled into plugins.  For
// platforms that require a special tag on exported symbols, BX_PLUGGABLE
// is used to know when we are exporting symbols and when we are importing.
#define BX_PLUGGABLE

#define NO_DEVICE_INCLUDES
#include "iodev.h"
#include "hdimage.h"

#if BX_HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif

#define LOG_THIS bx_devices.pluginHardDrive->

/*** base class device_image_t ***/

device_image_t::device_image_t()
{
  hd_size = 0;
}

/*** default_image_t function definitions ***/

int default_image_t::open(const char* pathname)
{
  return open(pathname, O_RDWR);
}

int default_image_t::open(const char* pathname, int flags)
{
  fd = ::open(pathname, flags
#ifdef O_BINARY
              | O_BINARY
#endif
              );

  if (fd < 0) {
    return fd;
  }

  /* look at size of image file to calculate disk geometry */
  struct stat stat_buf;
  int ret = fstat(fd, &stat_buf);
  if (ret) {
    BX_PANIC(("fstat() returns error!"));
  }
  if ((stat_buf.st_size % 512) != 0) {
    BX_PANIC(("size of disk image must be multiple of 512 bytes"));
  }
  hd_size = (Bit64u)stat_buf.st_size;

  return fd;
}

void default_image_t::close()
{
  if (fd > -1) {
    ::close(fd);
  }
}

off_t default_image_t::lseek(off_t offset, int whence)
{
  return ::lseek(fd, offset, whence);
}

ssize_t default_image_t::read(void* buf, size_t count)
{
  return ::read(fd, (char*) buf, count);
}

ssize_t default_image_t::write(const void* buf, size_t count)
{
  return ::write(fd, (char*) buf, count);
}

char increment_string(char *str, int diff)
{
  // find the last character of the string, and increment it.
  char *p = str;
  while (*p != 0) p++;
  BX_ASSERT(p>str);  // choke on zero length strings
  p--;  // point to last character of the string
  (*p) += diff;  // increment to next/previous ascii code.
  BX_DEBUG(("increment string returning '%s'", str));
 return (*p);
}

/*** concat_image_t function definitions ***/

concat_image_t::concat_image_t()
{
  fd = -1;
}

void concat_image_t::increment_string(char *str)
{
 ::increment_string(str, +1);
}

int concat_image_t::open(const char* pathname0)
{
  char *pathname = strdup(pathname0);
  BX_DEBUG(("concat_image_t.open"));
  Bit64s start_offset = 0;
  for (int i=0; i<BX_CONCAT_MAX_IMAGES; i++) {
    fd_table[i] = ::open(pathname, O_RDWR
#ifdef O_BINARY
		| O_BINARY
#endif
	  );
    if (fd_table[i] < 0) {
      // open failed.
      // if no FD was opened successfully, return -1 (fail).
      if (i==0) return -1;
      // otherwise, it only means that all images in the series have 
      // been opened.  Record the number of fds opened successfully.
      maxfd = i; 
      break;
    }
    BX_DEBUG(("concat_image: open image %s, fd[%d] = %d", pathname, i, fd_table[i]));
    /* look at size of image file to calculate disk geometry */
    struct stat stat_buf;
    int ret = fstat(fd_table[i], &stat_buf);
    if (ret) {
      BX_PANIC(("fstat() returns error!"));
    }
#ifdef S_ISBLK
    if (S_ISBLK(stat_buf.st_mode)) {
      BX_PANIC(("block devices should REALLY NOT be used as concat images"));
    }
#endif
    if ((stat_buf.st_size % 512) != 0) {
      BX_PANIC(("size of disk image must be multiple of 512 bytes"));
    }
    length_table[i] = stat_buf.st_size;
    start_offset_table[i] = start_offset;
    start_offset += stat_buf.st_size;
    increment_string(pathname);
  }
  // start up with first image selected
  index = 0;
  fd = fd_table[0];
  thismin = 0;
  thismax = length_table[0]-1;
  seek_was_last_op = 0;
  hd_size = start_offset;
  return 0; // success.
}

void concat_image_t::close()
{
  BX_DEBUG(("concat_image_t.close"));
  if (fd > -1) {
    ::close(fd);
  }
}

Bit64s concat_image_t::lseek(Bit64s offset, int whence)
{
  if ((offset % 512) != 0) 
    BX_PANIC( ("lseek HD with offset not multiple of 512"));
  BX_DEBUG(("concat_image_t.lseek(%d)", whence));
  // is this offset in this disk image?
  if (offset < thismin) {
    // no, look at previous images
    for (int i=index-1; i>=0; i--) {
      if (offset >= start_offset_table[i]) {
	index = i;
	fd = fd_table[i];
	thismin = start_offset_table[i];
	thismax = thismin + length_table[i] - 1;
	BX_DEBUG(("concat_image_t.lseek to earlier image, index=%d", index));
	break;
      }
    }
  } else if (offset > thismax) {
    // no, look at later images
    for (int i=index+1; i<maxfd; i++) {
      if (offset < start_offset_table[i] + length_table[i]) {
	index = i;
	fd = fd_table[i];
	thismin = start_offset_table[i];
	thismax = thismin + length_table[i] - 1;
	BX_DEBUG(("concat_image_t.lseek to earlier image, index=%d", index));
	break;
      }
    }
  }
  // now offset should be within the current image.
  offset -= start_offset_table[index];
  if (offset < 0 || offset >= length_table[index]) {
    BX_PANIC(("concat_image_t.lseek to byte %ld failed", (long)offset));
    return -1;
  }

  seek_was_last_op = 1;
  return ::lseek(fd, (off_t)offset, whence);
}

ssize_t concat_image_t::read(void* buf, size_t count)
{
  if (bx_dbg.disk)
    BX_DEBUG(("concat_image_t.read %ld bytes", (long)count));
  // notice if anyone does sequential read or write without seek in between.
  // This can be supported pretty easily, but needs additional checks for
  // end of a partial image.
  if (!seek_was_last_op)
    BX_PANIC( ("no seek before read"));
  return ::read(fd, (char*) buf, count);
}

ssize_t concat_image_t::write(const void* buf, size_t count)
{
  BX_DEBUG(("concat_image_t.write %ld bytes", (long)count));
  // notice if anyone does sequential read or write without seek in between.
  // This can be supported pretty easily, but needs additional checks for
  // end of a partial image.
  if (!seek_was_last_op)
    BX_PANIC( ("no seek before write"));
  return ::write(fd, (char*) buf, count);
}

/*** sparse_image_t function definitions ***/

sparse_image_t::sparse_image_t ()
{
  fd = -1;
  pathname = NULL;
#ifdef _POSIX_MAPPED_FILES
 mmap_header = NULL;
#endif
 pagetable = NULL;
}


/*
void showpagetable(Bit32u * pagetable, size_t numpages)
{
 printf("Non null pages: ");
 for (int i = 0; i < numpages; i++)
 {
   if (pagetable[i] != 0xffffffff)
   {
     printf("%d ", i);
   }
 }
 printf("\n");
}
*/


void sparse_image_t::read_header()
{
 BX_ASSERT(sizeof(header) == SPARSE_HEADER_SIZE);

 int ret = ::read(fd, &header, sizeof(header));

 if (-1 == ret)
 {
     panic(strerror(errno));
 }

 if (sizeof(header) != ret)
 {
   panic("could not read entire header");
 }

 if (dtoh32(header.magic) != SPARSE_HEADER_MAGIC)
 {
   panic("failed header magic check");
 }

 if ((dtoh32(header.version) != SPARSE_HEADER_VERSION) &&
     (dtoh32(header.version) != SPARSE_HEADER_V1))
 {
   panic("unknown version in header");
 }

 pagesize = dtoh32(header.pagesize);
 Bit32u numpages = dtoh32(header.numpages);

 total_size = pagesize;
 total_size *= numpages;

 pagesize_shift = 0;
 while ((pagesize >> pagesize_shift) > 1) pagesize_shift++;

 if ((Bit32u)(1 << pagesize_shift) != pagesize)
 {
   panic("failed block size header check");
 }

 pagesize_mask = pagesize - 1;

 size_t  preamble_size = (sizeof(Bit32u) * numpages) + sizeof(header);
 data_start = 0;
 while (data_start < preamble_size) data_start += pagesize;

 bx_bool did_mmap = 0;

#ifdef _POSIX_MAPPED_FILES
// Try to memory map from the beginning of the file (0 is trivially a page multiple)
 void * mmap_header = mmap(NULL, preamble_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
 if (mmap_header == MAP_FAILED)
 {
   BX_INFO(("failed to mmap sparse disk file - using conventional file access"));
   mmap_header = NULL;
 }
 else
 {
   mmap_length = preamble_size;
   did_mmap = 1;
   pagetable = ((Bit32u *) (((Bit8u *) mmap_header) + sizeof(header)));

//   system_pagesize = getpagesize();
   system_pagesize_mask = getpagesize() - 1;
 }
#endif

 if (!did_mmap)
 {
   pagetable = new Bit32u[numpages];

   if (pagetable == NULL)
   {
     panic("could not allocate memory for sparse disk block table");
   }

   ret = ::read(fd, pagetable, sizeof(Bit32u) * numpages);

   if (-1 == ret)
   {
       panic(strerror(errno));
   }

   if ((int)(sizeof(Bit32u) * numpages) != ret)
   {
     panic("could not read entire block table");
   }
 }
}

int sparse_image_t::open (const char* pathname0)
{
 pathname = strdup(pathname0);
 BX_DEBUG(("sparse_image_t.open"));

 fd = ::open(pathname, O_RDWR
#ifdef O_BINARY
   | O_BINARY
#endif
   );

 if (fd < 0)
 {
   // open failed.
   return -1;
 }
 BX_DEBUG(("sparse_image: open image %s", pathname));

 read_header();

 struct stat stat_buf;
 if (0 != fstat(fd, &stat_buf)) panic(("fstat() returns error!"));

 underlying_filesize = stat_buf.st_size;

 if ((underlying_filesize % pagesize) != 0)
   panic("size of sparse disk image is not multiple of page size");

 underlying_current_filepos = 0;
 if (-1 == ::lseek(fd, 0, SEEK_SET))
   panic("error while seeking to start of file");

 lseek(0, SEEK_SET);

 //showpagetable(pagetable, header.numpages);

 char * parentpathname = strdup(pathname);
 char lastchar = ::increment_string(parentpathname, -1);

 if ((lastchar >= '0') && (lastchar <= '9'))
 {
   struct stat stat_buf;
   if (0 == stat(parentpathname, &stat_buf))
   {
     parent_image = new sparse_image_t();
     int ret = parent_image->open(parentpathname);
     if (ret != 0) return ret;
     if (    (parent_image->pagesize != pagesize)
         ||  (parent_image->total_size != total_size))
     {
       panic("child drive image does not have same page count/page size configuration");
     }
   }
 }

 if (parentpathname != NULL) free(parentpathname);

 if (dtoh32(header.version) == SPARSE_HEADER_VERSION) {
   hd_size = dtoh64(header.disk);
 }
 return 0; // success.
}

void sparse_image_t::close()
{
  BX_DEBUG(("concat_image_t.close"));
  if (pathname != NULL)
  {
   free(pathname);
 }
#ifdef _POSIX_MAPPED_FILES
 if (mmap_header != NULL)
 {
   int ret = munmap(mmap_header, mmap_length);
   if (ret != 0)
     BX_INFO(("failed to un-memory map sparse disk file"));
 }
 pagetable = NULL; // We didn't malloc it
#endif
  if (fd > -1) {
    ::close(fd);
  }
 if (pagetable != NULL)
 {
   delete [] pagetable;
 }
 if (parent_image != NULL)
 {
   delete parent_image;
 }
}

off_t sparse_image_t::lseek (off_t offset, int whence)
{
 //showpagetable(pagetable, header.numpages);

 if ((offset % 512) != 0)
    BX_PANIC( ("lseek HD with offset not multiple of 512"));
 if (whence != SEEK_SET)
   BX_PANIC( ("lseek HD with whence not SEEK_SET"));

 BX_DEBUG(("sparse_image_t.lseek(%d)", whence));

 if (offset > total_size)
 {
   BX_PANIC(("sparse_image_t.lseek to byte %ld failed", (long)offset));
    return -1;
  }

 //printf("Seeking to position %ld\n", (long) offset);

 set_virtual_page((Bit32u)(offset >> pagesize_shift));
 position_page_offset = (Bit32u)(offset & pagesize_mask);

 return 0;
}

inline off_t sparse_image_t::get_physical_offset()
{
 off_t physical_offset = data_start;
 physical_offset += ((off_t)position_physical_page << pagesize_shift);
 physical_offset += position_page_offset;

 return physical_offset;
}

void sparse_image_t::set_virtual_page(Bit32u new_virtual_page)
{
 position_virtual_page = new_virtual_page;

 position_physical_page = dtoh32(pagetable[position_virtual_page]);
}

ssize_t sparse_image_t::read_page_fragment(Bit32u read_virtual_page, Bit32u read_page_offset, size_t read_size, void * buf)
{
 if (read_virtual_page != position_virtual_page)
 {
   set_virtual_page(read_virtual_page);
 }

 position_page_offset = read_page_offset;

 if (position_physical_page == SPARSE_PAGE_NOT_ALLOCATED)
 {
   if (parent_image != NULL)
   {
     return parent_image->read_page_fragment(read_virtual_page, read_page_offset, read_size, buf);
   }
   else
   {
     memset(buf, 0, read_size);
   }
 }
 else
 {
   off_t physical_offset = get_physical_offset();

   if (physical_offset != underlying_current_filepos)
   {
     off_t ret = ::lseek(fd, physical_offset, SEEK_SET);
     // underlying_current_filepos update deferred
     if (ret == -1)
       panic(strerror(errno));
   }

   //printf("Reading %s at position %ld size %d\n", pathname, (long) physical_offset, (long) read_size);
   ssize_t readret = ::read(fd, buf, read_size);

   if (readret == -1)
   {
     panic(strerror(errno));
   }

   if ((size_t)readret != read_size)
   {
     panic("could not read block contents from file");
   }

   underlying_current_filepos = physical_offset + read_size;
 }

 return read_size;
}

ssize_t sparse_image_t::read(void* buf, size_t count)
{
 //showpagetable(pagetable, header.numpages);
 ssize_t total_read = 0;

 if (bx_dbg.disk)
    BX_DEBUG(("sparse_image_t.read %ld bytes", (long)count));

 while (count != 0)
 {
   size_t can_read = pagesize - position_page_offset;
   if (count < can_read) can_read = count;

   BX_ASSERT (can_read != 0);

   size_t  was_read = read_page_fragment(position_virtual_page, position_page_offset, can_read, buf);

   BX_ASSERT(was_read == can_read);

   total_read += can_read;

   position_page_offset += can_read;
   if (position_page_offset == pagesize)
   {
     position_page_offset = 0;
     set_virtual_page(position_virtual_page + 1);
   }

   BX_ASSERT(position_page_offset < pagesize);

   buf = (((Bit8u *) buf) + can_read);
   count -= can_read;
 }

 return total_read;
}

void sparse_image_t::panic(const char * message)
{
 char buffer[1024];
 if (message == NULL)
 {
   snprintf(buffer, sizeof(buffer), "error with sparse disk image %s", pathname);
 }
 else
 {
   snprintf(buffer, sizeof(buffer), "error with sparse disk image %s - %s", pathname, message);
 }
 BX_PANIC((buffer));
}

ssize_t sparse_image_t::write (const void* buf, size_t count)
{
 //showpagetable(pagetable, header.numpages);

 ssize_t total_written = 0;

 Bit32u  update_pagetable_start = position_virtual_page;
 Bit32u  update_pagetable_count = 0;

 if (bx_dbg.disk)
    BX_DEBUG(("sparse_image_t.write %ld bytes", (long)count));

 while (count != 0)
 {
   size_t can_write = pagesize - position_page_offset;
   if (count < can_write) can_write = count;

   BX_ASSERT (can_write != 0);

   if (position_physical_page == SPARSE_PAGE_NOT_ALLOCATED)
   {
     // We just add on another page at the end of the file
     // Reclamation, compaction etc should currently be done off-line

     off_t  data_size = underlying_filesize - data_start;
     BX_ASSERT((data_size % pagesize) == 0);


     Bit32u  data_size_pages = (Bit32u)(data_size / pagesize);
     Bit32u  next_data_page = data_size_pages;

     pagetable[position_virtual_page] = htod32(next_data_page);
     position_physical_page = next_data_page;

     off_t page_file_start = data_start + ((off_t)position_physical_page << pagesize_shift);

     if (parent_image != NULL)
     {
       // If we have a parent, we must merge our portion with the parent
       void * writebuffer = NULL;

       if (can_write == pagesize)
       {
         writebuffer = (void *) buf;
       }
       else
       {
         writebuffer = malloc(pagesize);
         if (writebuffer == NULL)
           panic("Cannot allocate sufficient memory for page-merge in write");

         // Read entire page - could optimize, but simple for now
         parent_image->read_page_fragment(position_virtual_page, 0, pagesize, writebuffer);

         void * dest_start = ((Bit8u *) writebuffer) + position_page_offset;
         memcpy(dest_start, buf, can_write);
       }

       int ret;
       ret = (int)::lseek(fd, page_file_start, SEEK_SET);
       // underlying_current_filepos update deferred
       if (-1 == ret) panic(strerror(errno));

       ret = ::write(fd, writebuffer, pagesize);

       if (-1 == ret) panic(strerror(errno));

       if (pagesize != (Bit32u)ret) panic("failed to write entire merged page to disk");

       if (can_write != pagesize)
       {
         free(writebuffer);
       }
     }
     else
     {
       // We need to write a zero page because read has been returning zeroes
       // We seek as close to the page end as possible, and then write a little
       // This produces a sparse file which has blanks
       // Also very quick, even when pagesize is massive
       int ret;
       ret = (int)::lseek(fd, page_file_start + pagesize - 4, SEEK_SET);
       // underlying_current_filepos update deferred
       if (-1 == ret) panic(strerror(errno));

       Bit32u  zero = 0;
       ret = ::write(fd, &zero, 4);

       if (-1 == ret) panic(strerror(errno));

       if (4 != ret) panic("failed to write entire blank page to disk");
     }

     update_pagetable_count = (position_virtual_page - update_pagetable_start) + 1;
     underlying_filesize = underlying_current_filepos = page_file_start + pagesize;
   }

   BX_ASSERT(position_physical_page != SPARSE_PAGE_NOT_ALLOCATED);

   off_t physical_offset = get_physical_offset();

   if (physical_offset != underlying_current_filepos)
   {
     off_t ret = ::lseek(fd, physical_offset, SEEK_SET);
     // underlying_current_filepos update deferred
     if (ret == -1)
       panic(strerror(errno));
   }

   //printf("Writing at position %ld size %d\n", (long) physical_offset, can_write);
   ssize_t writeret = ::write(fd, buf, can_write);

   if (writeret == -1)
   {
     panic(strerror(errno));
   }

   if ((size_t)writeret != can_write)
   {
     panic("could not write block contents to file");
   }

   underlying_current_filepos = physical_offset + can_write;

   total_written += can_write;

   position_page_offset += can_write;
   if (position_page_offset == pagesize)
   {
     position_page_offset = 0;
     set_virtual_page(position_virtual_page + 1);
   }

   BX_ASSERT(position_page_offset < pagesize);

   buf = (((Bit8u *) buf) + can_write);
   count -= can_write;
 }

 if (update_pagetable_count != 0)
 {
   bx_bool done = 0;
   off_t pagetable_write_from = sizeof(header) + (sizeof(Bit32u) * update_pagetable_start);
   size_t  write_bytecount = update_pagetable_count * sizeof(Bit32u);

#ifdef _POSIX_MAPPED_FILES
   if (mmap_header != NULL)
   {
     // Sync from the beginning of the page
     size_t system_page_offset = pagetable_write_from & system_pagesize_mask;
     void * start = ((Bit8u *) mmap_header + pagetable_write_from - system_page_offset);

     int ret = msync(start, system_page_offset + write_bytecount, MS_ASYNC);

     if (ret != 0)
       panic(strerror(errno));

     done = 1;
   }
#endif

   if (!done)
   {
     int ret = (int)::lseek(fd, pagetable_write_from, SEEK_SET);
     // underlying_current_filepos update deferred
     if (ret == -1) panic(strerror(errno));

     //printf("Writing header at position %ld size %ld\n", (long) pagetable_write_from, (long) write_bytecount);
     ret = ::write(fd, &pagetable[update_pagetable_start], write_bytecount);
     if (ret == -1) panic(strerror(errno));
     if ((size_t)ret != write_bytecount) panic("could not write entire updated block header");

     underlying_current_filepos = pagetable_write_from + write_bytecount;
   }
 }

 return total_written;
}

#if DLL_HD_SUPPORT

/*** dll_image_t function definitions ***/

/*
function vdisk_open(path:PChar;numclusters,clustersize:integer):integer;
procedure vdisk_read(vunit:integer;blk:integer;var buf:TBlock);
procedure vdisk_write(vunit:integer;blk:integer;var buf:TBlock);
procedure vdisk_close(vunit:integer);
*/

HINSTANCE hlib_vdisk = 0;

int (*vdisk_open)  (const char *path,int numclusters,int clustersize);
void (*vdisk_read)   (int vunit,int blk,void *buf);
void (*vdisk_write)  (int vunit,int blk,const void *buf);
void (*vdisk_close) (int vunit);

int dll_image_t::open (const char* pathname)
{
    if (hlib_vdisk == 0) {
      hlib_vdisk = LoadLibrary("vdisk.dll");
      if (hlib_vdisk != 0) {
        vdisk_read = (void (*)(int,int,void*))        GetProcAddress(hlib_vdisk,"vdisk_read");
        vdisk_write = (void (*)(int,int,const void*)) GetProcAddress(hlib_vdisk,"vdisk_write");
        vdisk_open = (int (*)(const char *,int,int))  GetProcAddress(hlib_vdisk,"vdisk_open");
        vdisk_close = (void (*)(int))                 GetProcAddress(hlib_vdisk,"vdisk_close");
      }
    }
    if (hlib_vdisk != 0) {
      vunit = vdisk_open(pathname,0x10000,64);
      vblk = 0;
    } else {
      vunit = -2;
    }
    return vunit;
}

void dll_image_t::close ()
{
   if (vunit >= 0 && hlib_vdisk != 0) {
     vdisk_close(vunit);
   }
}

off_t dll_image_t::lseek (off_t offset, int whence)
{
      vblk = offset >> 9;
      return 0;
}

ssize_t dll_image_t::read (void* buf, size_t count)
{
      if (vunit >= 0 && hlib_vdisk != 0) {
         vdisk_read(vunit,vblk,buf);
         return count;
      } else {
         return -1;
      }
}

ssize_t dll_image_t::write (const void* buf, size_t count)
{
      if (vunit >= 0 && hlib_vdisk != 0) {
        vdisk_write(vunit,vblk,buf);
        return count;
      } else {
         return -1;
      }
}
#endif // DLL_HD_SUPPORT

// redolog implementation
redolog_t::redolog_t()
{
  fd = -1;
  catalog = NULL;
  bitmap = NULL;
  extent_index = (Bit32u)0;
  extent_offset = (Bit32u)0;
  extent_next = (Bit32u)0;
}

void
redolog_t::print_header()
{
  BX_INFO(("redolog : Standard Header : magic='%s', type='%s', subtype='%s', version = %d.%d",
           header.standard.magic, header.standard.type, header.standard.subtype,
           dtoh32(header.standard.version)/0x10000,
           dtoh32(header.standard.version)%0x10000));
  if (dtoh32(header.standard.version) == STANDARD_HEADER_VERSION) {
    BX_INFO(("redolog : Specific Header : #entries=%d, bitmap size=%d, exent size = %d disk size = " FMT_LL "d",
             dtoh32(header.specific.catalog),
             dtoh32(header.specific.bitmap),
             dtoh32(header.specific.extent),
             dtoh64(header.specific.disk)));
  } else if (dtoh32(header.standard.version) == STANDARD_HEADER_V1) {
    redolog_header_v1_t header_v1;

    memcpy(&header_v1, &header, STANDARD_HEADER_SIZE);
    BX_INFO(("redolog : Specific Header : #entries=%d, bitmap size=%d, exent size = %d disk size = " FMT_LL "d",
             dtoh32(header_v1.specific.catalog),
             dtoh32(header_v1.specific.bitmap),
             dtoh32(header_v1.specific.extent),
             dtoh64(header_v1.specific.disk)));
  }
}

int 
redolog_t::make_header (const char* type, Bit64u size)
{
        Bit32u entries, extent_size, bitmap_size;
        Bit64u maxsize;
        Bit32u flip=0;

        // Set standard header values
        strcpy((char*)header.standard.magic, STANDARD_HEADER_MAGIC);
        strcpy((char*)header.standard.type, REDOLOG_TYPE);
        strcpy((char*)header.standard.subtype, type);
        header.standard.version = htod32(STANDARD_HEADER_VERSION);
        header.standard.header = htod32(STANDARD_HEADER_SIZE);

        entries = 512;
        bitmap_size = 1;

        // Compute #entries and extent size values
        do {
                extent_size = 8 * bitmap_size * 512;

                header.specific.catalog = htod32(entries);
                header.specific.bitmap = htod32(bitmap_size);
                header.specific.extent = htod32(extent_size);
                
                maxsize = (Bit64u)entries * (Bit64u)extent_size;

                flip++;

                if(flip&0x01) bitmap_size *= 2;
                else entries *= 2;
        } while (maxsize < size);

        header.specific.disk = htod64(size);
        
        print_header();

        catalog = (Bit32u*)malloc(dtoh32(header.specific.catalog) * sizeof(Bit32u));
        bitmap = (Bit8u*)malloc(dtoh32(header.specific.bitmap));

        if ((catalog == NULL) || (bitmap==NULL))
                BX_PANIC(("redolog : could not malloc catalog or bitmap"));

        for (Bit32u i=0; i<dtoh32(header.specific.catalog); i++)
                catalog[i] = htod32(REDOLOG_PAGE_NOT_ALLOCATED);

        bitmap_blocs = 1 + (dtoh32(header.specific.bitmap) - 1) / 512;
        extent_blocs = 1 + (dtoh32(header.specific.extent) - 1) / 512;

        BX_DEBUG(("redolog : each bitmap is %d blocs", bitmap_blocs));
        BX_DEBUG(("redolog : each extent is %d blocs", extent_blocs));

        return 0;
}

int 
redolog_t::create (const char* filename, const char* type, Bit64u size)
{
        int filedes;

        BX_INFO(("redolog : creating redolog %s", filename));

        filedes = ::open(filename, O_RDWR | O_CREAT | O_TRUNC
#ifdef O_BINARY
            | O_BINARY
#endif
              , S_IWUSR | S_IRUSR | S_IRGRP | S_IWGRP);

        return create(filedes, type, size);
}

int 
redolog_t::create (int filedes, const char* type, Bit64u size)
{
        fd = filedes;

        if (fd < 0)
        {
                // open failed.
                return -1;
        }

        if (make_header(type, size) < 0)
        {
                return -1;
        }

        // Write header
        ::write(fd, &header, dtoh32(header.standard.header));

        // Write catalog
        // FIXME could mmap
        ::write(fd, catalog, dtoh32(header.specific.catalog) * sizeof (Bit32u));

        return 0;
}

int 
redolog_t::open(const char* filename, const char *type)
{
  int res;

  fd = ::open(filename, O_RDWR
#ifdef O_BINARY
              | O_BINARY
#endif
              );
  if (fd < 0)
  {
    BX_INFO(("redolog : could not open image %s", filename));
    // open failed.
    return -1;
  }
  BX_INFO(("redolog : open image %s", filename));

  res = ::read(fd, &header, sizeof(header));
  if (res != STANDARD_HEADER_SIZE)
  {
    BX_PANIC(("redolog : could not read header")); 
    return -1;
  }

  print_header();

  if (strcmp((char*)header.standard.magic, STANDARD_HEADER_MAGIC) != 0)
  {
    BX_PANIC(("redolog : Bad header magic")); 
    return -1;
  }

  if (strcmp((char*)header.standard.type, REDOLOG_TYPE) != 0)
  {
    BX_PANIC(("redolog : Bad header type")); 
    return -1;
  }
  if (strcmp((char*)header.standard.subtype, type) != 0)
  {
    BX_PANIC(("redolog : Bad header subtype")); 
    return -1;
  }

  if ((dtoh32(header.standard.version) != STANDARD_HEADER_VERSION) &&
      (dtoh32(header.standard.version) != STANDARD_HEADER_V1))
  {
    BX_PANIC(("redolog : Bad header version")); 
    return -1;
  }

  if (dtoh32(header.standard.version) == STANDARD_HEADER_V1) {
    redolog_header_v1_t header_v1;

    memcpy(&header_v1, &header, STANDARD_HEADER_SIZE);
    header.specific.disk = header_v1.specific.disk;
  }

  catalog = (Bit32u*)malloc(dtoh32(header.specific.catalog) * sizeof(Bit32u));
        
  // FIXME could mmap
  ::lseek(fd,dtoh32(header.standard.header),SEEK_SET);
  res = ::read(fd, catalog, dtoh32(header.specific.catalog) * sizeof(Bit32u));

  if (res !=  (ssize_t)(dtoh32(header.specific.catalog) * sizeof(Bit32u)))
  {
    BX_PANIC(("redolog : could not read catalog %d=%d",res, dtoh32(header.specific.catalog))); 
    return -1;
  }

  // check last used extent
  extent_next = 0;
  for (Bit32u i=0; i < dtoh32(header.specific.catalog); i++)
  {
    if (dtoh32(catalog[i]) != REDOLOG_PAGE_NOT_ALLOCATED)
    {
      if (dtoh32(catalog[i]) >= extent_next)
        extent_next = dtoh32(catalog[i]) + 1;
    }
  }
  BX_INFO(("redolog : next extent will be at index %d",extent_next));

  // memory used for storing bitmaps
  bitmap = (Bit8u *)malloc(dtoh32(header.specific.bitmap));

  bitmap_blocs = 1 + (dtoh32(header.specific.bitmap) - 1) / 512;
  extent_blocs = 1 + (dtoh32(header.specific.extent) - 1) / 512;

  BX_DEBUG(("redolog : each bitmap is %d blocs", bitmap_blocs));
  BX_DEBUG(("redolog : each extent is %d blocs", extent_blocs));

  return 0;
}

void redolog_t::close()
{
  if (fd >= 0)
    ::close(fd);

  if (catalog != NULL)
    free(catalog);

  if (bitmap != NULL)
    free(bitmap);
}

Bit64u redolog_t::get_size()
{
  return dtoh64(header.specific.disk);
}

Bit64s redolog_t::lseek(Bit64s offset, int whence)
{
  if ((offset % 512) != 0) {
    BX_PANIC( ("redolog : lseek HD with offset not multiple of 512"));
    return -1;
  }
  if (whence != SEEK_SET) {
    BX_PANIC( ("redolog : lseek HD with whence not SEEK_SET"));
    return -1;
  }
  if (offset > (off_t)dtoh64(header.specific.disk))
  {
    BX_PANIC(("redolog : lseek to byte %ld failed", (long)offset));
    return -1;
  }

  extent_index = (Bit32u)(offset / dtoh32(header.specific.extent));
  extent_offset = (Bit32u)((offset % dtoh32(header.specific.extent)) / 512);

  BX_DEBUG(("redolog : lseeking extent index %d, offset %d",extent_index, extent_offset));

  return offset;
}

ssize_t
redolog_t::read (void* buf, size_t count)
{
        off_t bloc_offset, bitmap_offset;

        if (count != 512)
                BX_PANIC( ("redolog : read HD with count not 512"));

        BX_DEBUG(("redolog : reading index %d, mapping to %d", extent_index, dtoh32(catalog[extent_index])));

        if (dtoh32(catalog[extent_index]) == REDOLOG_PAGE_NOT_ALLOCATED)
        {
                // page not allocated
                return 0;
        }

        bitmap_offset  = (off_t)STANDARD_HEADER_SIZE + (dtoh32(header.specific.catalog) * sizeof(Bit32u));
        bitmap_offset += (off_t)512 * dtoh32(catalog[extent_index]) * (extent_blocs + bitmap_blocs); 
        bloc_offset    = bitmap_offset + ((off_t)512 * (bitmap_blocs + extent_offset));

        BX_DEBUG(("redolog : bitmap offset is %x", (Bit32u)bitmap_offset));
        BX_DEBUG(("redolog : bloc offset is %x", (Bit32u)bloc_offset));


        // FIXME if same extent_index as before we can skip bitmap read

        ::lseek(fd, bitmap_offset, SEEK_SET);

        if (::read(fd, bitmap,  dtoh32(header.specific.bitmap)) != (ssize_t)dtoh32(header.specific.bitmap))
        {
                BX_PANIC(("redolog : failed to read bitmap for extent %d", extent_index));
                return 0;
        }

        if ( ((bitmap[extent_offset/8] >> (extent_offset%8)) & 0x01) == 0x00 )
        {
                BX_DEBUG(("read not in redolog"));

                // bitmap says bloc not in reloglog
                return 0;
        }
        
        ::lseek(fd, bloc_offset, SEEK_SET);

        return (::read(fd, buf, count));
}

ssize_t
redolog_t::write (const void* buf, size_t count)
{
        Bit32u i;
        off_t bloc_offset, bitmap_offset, catalog_offset;
        ssize_t written;
        bx_bool update_catalog = 0;

        if (count != 512)
                BX_PANIC( ("redolog : write HD with count not 512"));

        BX_DEBUG(("redolog : writing index %d, mapping to %d", extent_index, dtoh32(catalog[extent_index])));
        if (dtoh32(catalog[extent_index]) == REDOLOG_PAGE_NOT_ALLOCATED)
        {
                if(extent_next >= dtoh32(header.specific.catalog))
                {
                        BX_PANIC(("redolog : can't allocate new extent... catalog is full"));
                        return 0;
                }

                BX_DEBUG(("redolog : allocating new extent at %d", extent_next));

                // Extent not allocated, allocate new
                catalog[extent_index] = htod32(extent_next);
                
                extent_next += 1;

                char *zerobuffer = (char*)malloc(512);
                memset(zerobuffer, 0, 512);

                // Write bitmap
                bitmap_offset  = (off_t)STANDARD_HEADER_SIZE + (dtoh32(header.specific.catalog) * sizeof(Bit32u));
                bitmap_offset += (off_t)512 * dtoh32(catalog[extent_index]) * (extent_blocs + bitmap_blocs); 
                ::lseek(fd, bitmap_offset, SEEK_SET);
                for(i=0; i<bitmap_blocs; i++)
                {
                        ::write(fd, zerobuffer, 512);
                }
                // Write extent
                for(i=0; i<extent_blocs; i++)
                {
                        ::write(fd, zerobuffer, 512);
                }

                free(zerobuffer);

                update_catalog = 1;
        }

        bitmap_offset  = (off_t)STANDARD_HEADER_SIZE + (dtoh32(header.specific.catalog) * sizeof(Bit32u));
        bitmap_offset += (off_t)512 * dtoh32(catalog[extent_index]) * (extent_blocs + bitmap_blocs); 
        bloc_offset    = bitmap_offset + ((off_t)512 * (bitmap_blocs + extent_offset));

        BX_DEBUG(("redolog : bitmap offset is %x", (Bit32u)bitmap_offset));
        BX_DEBUG(("redolog : bloc offset is %x", (Bit32u)bloc_offset));

        // Write bloc
        ::lseek(fd, bloc_offset, SEEK_SET);
        written = ::write(fd, buf, count);

        // Write bitmap
        // FIXME if same extent_index as before we can skip bitmap read
        ::lseek(fd, bitmap_offset, SEEK_SET);
        if (::read(fd, bitmap,  dtoh32(header.specific.bitmap)) != (ssize_t)dtoh32(header.specific.bitmap))
        {
                BX_PANIC(("redolog : failed to read bitmap for extent %d", extent_index));
                return 0;
        }

        // If bloc does not belong to extent yet
        if ( ((bitmap[extent_offset/8] >> (extent_offset%8)) & 0x01) == 0x00 )
        {
                bitmap[extent_offset/8] |= 1 << (extent_offset%8);
                ::lseek(fd, bitmap_offset, SEEK_SET);
                ::write(fd, bitmap,  dtoh32(header.specific.bitmap));
        }

        // Write catalog
        if (update_catalog)
        {
                // FIXME if mmap
                catalog_offset  = (off_t)STANDARD_HEADER_SIZE + (extent_index * sizeof(Bit32u));

                BX_DEBUG(("redolog : writing catalog at offset %x", (Bit32u)catalog_offset));

                ::lseek(fd, catalog_offset, SEEK_SET);
                ::write(fd, &catalog[extent_index], sizeof(Bit32u));
        }

        return written;
}


/*** growing_image_t function definitions ***/

growing_image_t::growing_image_t()
{
  redolog = new redolog_t();
}

int growing_image_t::open(const char* pathname)
{
  int filedes = redolog->open(pathname, REDOLOG_SUBTYPE_GROWING);
  hd_size = redolog->get_size();
  BX_INFO(("'growing' disk opened, growing file is '%s'", pathname));
  return filedes;
}

void growing_image_t::close()
{
  redolog->close();
}

Bit64s growing_image_t::lseek(Bit64s offset, int whence)
{
  return redolog->lseek(offset, whence);
}

ssize_t growing_image_t::read(void* buf, size_t count)
{
  memset(buf, 0, count);
  redolog->read((char*) buf, count);
  return count;
}

ssize_t growing_image_t::write(const void* buf, size_t count)
{
  return redolog->write((char*) buf, count);
}


/*** undoable_image_t function definitions ***/

undoable_image_t::undoable_image_t(const char* _redolog_name)
{
  redolog = new redolog_t();
  ro_disk = new default_image_t();
  redolog_name = NULL;
  if (_redolog_name != NULL) {
    if (strcmp(_redolog_name,"") != 0) {
      redolog_name = strdup(_redolog_name);
    }
  }
}

int undoable_image_t::open(const char* pathname)
{
  char *logname=NULL;

  if (ro_disk->open(pathname, O_RDONLY)<0)
    return -1;

  hd_size = ro_disk->hd_size;
  // if redolog name was set 
  if ( redolog_name != NULL) {
    if ( strcmp(redolog_name, "") != 0 ) {
      logname = (char*)malloc(strlen(redolog_name) + 1);
      strcpy(logname, redolog_name);
    }
  }

  // Otherwise we make up the redolog filename from the pathname
  if ( logname == NULL) {
    logname = (char*)malloc(strlen(pathname) + UNDOABLE_REDOLOG_EXTENSION_LENGTH + 1);
    sprintf(logname, "%s%s", pathname, UNDOABLE_REDOLOG_EXTENSION);
  }

  if (redolog->open(logname,REDOLOG_SUBTYPE_UNDOABLE) < 0)
  {
    if (redolog->create(logname, REDOLOG_SUBTYPE_UNDOABLE, hd_size) < 0)
    {
      BX_PANIC(("Can't open or create redolog '%s'",logname));
      return -1;
    }
    if (hd_size != redolog->get_size())
    {
      BX_PANIC(("size reported by redolog doesn't match r/o disk size"));
      free(logname);
      return -1;
    }
  }

  BX_INFO(("'undoable' disk opened: ro-file is '%s', redolog is '%s'", pathname, logname));
  free(logname);

  return 0;
}

void undoable_image_t::close ()
{
  redolog->close();
  ro_disk->close();

  if (redolog_name!=NULL)
    free(redolog_name);
}

Bit64s undoable_image_t::lseek(Bit64s offset, int whence)
{
  redolog->lseek(offset, whence);
  return ro_disk->lseek((off_t)offset, whence);
}

ssize_t undoable_image_t::read(void* buf, size_t count)
{
  // This should be fixed if count != 512
  if ((size_t)redolog->read((char*) buf, count) != count)
    return ro_disk->read((char*) buf, count);
  else 
    return count;
}

ssize_t undoable_image_t::write(const void* buf, size_t count)
{
  return redolog->write((char*) buf, count);
}


/*** volatile_image_t function definitions ***/

volatile_image_t::volatile_image_t(const char* _redolog_name)
{
  redolog = new redolog_t();
  ro_disk = new default_image_t();
  redolog_temp = NULL;
  redolog_name = NULL;
  if (_redolog_name != NULL) {
    if (strcmp(_redolog_name,"") != 0) {
      redolog_name = strdup(_redolog_name);
    }
  }
}

int volatile_image_t::open(const char* pathname)
{
  int filedes;
  const char *logname=NULL;

  if (ro_disk->open(pathname, O_RDONLY)<0)
    return -1;

  hd_size = ro_disk->hd_size;
  // if redolog name was set 
  if ( redolog_name != NULL) {
    if ( strcmp(redolog_name, "") != 0 ) {
      logname = redolog_name;
    }
  }

  // otherwise use pathname as template
  if (logname == NULL) {
    logname = pathname;
  }

  redolog_temp = (char*)malloc(strlen(logname) + VOLATILE_REDOLOG_EXTENSION_LENGTH + 1);
  sprintf (redolog_temp, "%s%s", logname, VOLATILE_REDOLOG_EXTENSION);

  filedes = mkstemp (redolog_temp);

  if (filedes < 0)
  {
    BX_PANIC(("Can't create volatile redolog '%s'", redolog_temp));
    return -1;
  }
  if (redolog->create(filedes, REDOLOG_SUBTYPE_VOLATILE, hd_size) < 0)
  {
    BX_PANIC(("Can't create volatile redolog '%s'", redolog_temp));
    return -1;
  }
        
#if (!defined(WIN32)) && !BX_WITH_MACOS
  // on unix it is legal to delete an open file
  unlink(redolog_temp);
#endif

  BX_INFO(("'volatile' disk opened: ro-file is '%s', redolog is '%s'", pathname, redolog_temp));

  return 0;
}

void volatile_image_t::close()
{
  redolog->close();
  ro_disk->close();

#if defined(WIN32) || BX_WITH_MACOS
  // on non-unix we have to wait till the file is closed to delete it
  unlink(redolog_temp);
#endif
  if (redolog_temp!=NULL)
    free(redolog_temp);

  if (redolog_name!=NULL)
    free(redolog_name);
}

Bit64s volatile_image_t::lseek(Bit64s offset, int whence)
{
  redolog->lseek(offset, whence);
  return ro_disk->lseek(offset, whence);
}

ssize_t volatile_image_t::read(void* buf, size_t count)
{
  // This should be fixed if count != 512
  if ((size_t)redolog->read((char*) buf, count) != count)
    return ro_disk->read((char*) buf, count);
  else 
    return count;
}

ssize_t volatile_image_t::write(const void* buf, size_t count)
{
  return redolog->write((char*) buf, count);
}

#if BX_COMPRESSED_HD_SUPPORT

/*** z_ro_image_t function definitions ***/

z_ro_image_t::z_ro_image_t()
{
  offset = (off_t)0;
}

int z_ro_image_t::open (const char* pathname)
{
  fd = ::open(pathname, O_RDONLY
#ifdef O_BINARY
              | O_BINARY
#endif
             );

  if (fd < 0)
  {
    BX_PANIC(("Could not open '%s' file", pathname));
    return fd;
  }

  gzfile = gzdopen(fd, "rb");
  return 0;
}

void z_ro_image_t::close ()
{
  if (fd > -1) {
    gzclose(gzfile);
    // ::close(fd);
  }
}

off_t z_ro_image_t::lseek (off_t _offset, int whence)
{
  // Only SEEK_SET supported
  if (whence != SEEK_SET)
  {
    BX_PANIC(("lseek on compressed images : only SEEK_SET supported"));
  }

  // Seeking is expensive on compressed files, so we do it
  // only when necessary, at the latest moment
  offset = _offset;

  return offset;
}

ssize_t z_ro_image_t::read (void* buf, size_t count)
{
  gzseek(gzfile, offset, SEEK_SET);
  return gzread(gzfile, buf, count);
}

ssize_t z_ro_image_t::write (const void* buf, size_t count)
{
  BX_PANIC(("z_ro_image: write not supported"));
  return 0;
}


/*** z_undoable_image_t function definitions ***/

z_undoable_image_t::z_undoable_image_t(Bit64u _size, const char* _redolog_name)
{
        redolog = new redolog_t();
        ro_disk = new z_ro_image_t();
        size = _size;

        redolog_name = NULL;
        if (_redolog_name != NULL) {
          if (strcmp(_redolog_name,"") != 0) {
            redolog_name = strdup(_redolog_name);
          }
        }
}

int z_undoable_image_t::open (const char* pathname)
{
        char *logname=NULL;

        if (ro_disk->open(pathname)<0)
                return -1;

        // If redolog name was set 
        if ( redolog_name != NULL) {
                if ( strcmp(redolog_name, "") != 0) {
                        logname = (char*)malloc(strlen(redolog_name) + 1);
                        strcpy (logname, redolog_name);
                }
        }

        // Otherwise we make up the redolog filename from the pathname
        if ( logname == NULL) {
                logname = (char*)malloc(strlen(pathname) + UNDOABLE_REDOLOG_EXTENSION_LENGTH + 1);
                sprintf (logname, "%s%s", pathname, UNDOABLE_REDOLOG_EXTENSION);
        }

        if (redolog->open(logname,REDOLOG_SUBTYPE_UNDOABLE,size) < 0)
        {
                if (redolog->create(logname, REDOLOG_SUBTYPE_UNDOABLE, size) < 0)
                {
                        BX_PANIC(("Can't open or create redolog '%s'",logname));
                        return -1;
                }
        }

        BX_INFO(("'z-undoable' disk opened, z-ro-file is '%s', redolog is '%s'", pathname, logname));
        free(logname);

        return 0;
}

void z_undoable_image_t::close ()
{
        redolog->close();
        ro_disk->close();

        if (redolog_name!=NULL)
          free(redolog_name);
}

off_t z_undoable_image_t::lseek (off_t offset, int whence)
{
      redolog->lseek(offset, whence);
      return ro_disk->lseek(offset, whence);
}

ssize_t z_undoable_image_t::read (void* buf, size_t count)
{
      // This should be fixed if count != 512
      if (redolog->read((char*) buf, count) != count)
              return ro_disk->read((char*) buf, count);
      else 
              return count;
}

ssize_t z_undoable_image_t::write (const void* buf, size_t count)
{
      return redolog->write((char*) buf, count);
}


/*** z_volatile_image_t function definitions ***/

z_volatile_image_t::z_volatile_image_t(Bit64u _size, const char* _redolog_name)
{
        redolog = new redolog_t();
        ro_disk = new z_ro_image_t();
        size = _size;

        redolog_temp = NULL;
        redolog_name = NULL;
        if (_redolog_name != NULL) {
          if (strcmp(_redolog_name,"") != 0) {
            redolog_name = strdup(_redolog_name);
          }
        }
}

int z_volatile_image_t::open (const char* pathname)
{
        int filedes;
        const char *logname=NULL;

        if (ro_disk->open(pathname)<0)
                return -1;

        // if redolog name was set 
        if ( redolog_name != NULL) {
                if ( strcmp(redolog_name, "") !=0 ) {
                        logname = redolog_name;
                }
        }

        // otherwise use pathname as template
        if (logname == NULL) {
                logname = pathname;
        }

        redolog_temp = (char*)malloc(strlen(logname) + VOLATILE_REDOLOG_EXTENSION_LENGTH + 1);
        sprintf (redolog_temp, "%s%s", logname, VOLATILE_REDOLOG_EXTENSION);

        filedes = mkstemp (redolog_temp);

        if (filedes < 0)
        {
                BX_PANIC(("Can't create volatile redolog '%s'", redolog_temp));
                return -1;
        }
        if (redolog->create(filedes, REDOLOG_SUBTYPE_VOLATILE, size) < 0)
        {
                BX_PANIC(("Can't create volatile redolog '%s'", redolog_temp));
                return -1;
        }
        
#if (!defined(WIN32)) && !BX_WITH_MACOS
        // on unix it is legal to delete an open file
        unlink(redolog_temp);
#endif

        BX_INFO(("'z-volatile' disk opened: z-ro-file is '%s', redolog is '%s'", pathname, redolog_temp));

        return 0;
}

void z_volatile_image_t::close ()
{
        redolog->close();
        ro_disk->close();

#if defined(WIN32) || BX_WITH_MACOS
        // on non-unix we have to wait till the file is closed to delete it
        unlink(redolog_temp);
#endif

        if (redolog_temp!=NULL)
          free(redolog_temp);

        if (redolog_name!=NULL)
          free(redolog_name);
}

off_t z_volatile_image_t::lseek (off_t offset, int whence)
{
      redolog->lseek(offset, whence);
      return ro_disk->lseek(offset, whence);
}

ssize_t z_volatile_image_t::read (void* buf, size_t count)
{
      // This should be fixed if count != 512
      if (redolog->read((char*) buf, count) != count)
              return ro_disk->read((char*) buf, count);
      else 
              return count;
}

ssize_t z_volatile_image_t::write (const void* buf, size_t count)
{
      return redolog->write((char*) buf, count);
}

#endif