Lines Matching refs:ctx
412 vhd_batmap_header_offset(vhd_context_t *ctx, off_t *_off) in vhd_batmap_header_offset() argument
419 off = ctx->header.table_offset; in vhd_batmap_header_offset()
420 bat = ctx->header.max_bat_size * sizeof(uint32_t); in vhd_batmap_header_offset()
446 vhd_parent_locator_count(vhd_context_t *ctx) in vhd_parent_locator_count() argument
448 return (sizeof(ctx->header.loc) / sizeof(vhd_parent_locator_t)); in vhd_parent_locator_count()
452 vhd_hidden(vhd_context_t *ctx, int *hidden) in vhd_hidden() argument
458 if (vhd_type_dynamic(ctx) && vhd_creator_tapdisk(ctx) && in vhd_hidden()
459 (ctx->footer.crtr_ver == VHD_VERSION(0, 1) || in vhd_hidden()
460 ctx->footer.crtr_ver == VHD_VERSION(1, 1))) { in vhd_hidden()
463 err = vhd_read_footer_at(ctx, ©, 0); in vhd_hidden()
466 ctx->file, err); in vhd_hidden()
471 *hidden = ctx->footer.hidden; in vhd_hidden()
477 vhd_chain_depth(vhd_context_t *ctx, int *depth) in vhd_chain_depth() argument
487 cur = ctx; in vhd_chain_depth()
506 if (cur != ctx) { in vhd_chain_depth()
521 if (cur && cur != ctx) in vhd_chain_depth()
531 vhd_batmap_test(vhd_context_t *ctx, vhd_batmap_t *batmap, uint32_t block) in vhd_batmap_test() argument
533 if (!vhd_has_batmap(ctx) || !batmap->map) in vhd_batmap_test()
543 vhd_batmap_set(vhd_context_t *ctx, vhd_batmap_t *batmap, uint32_t block) in vhd_batmap_set() argument
545 if (!vhd_has_batmap(ctx) || !batmap->map) in vhd_batmap_set()
555 vhd_batmap_clear(vhd_context_t *ctx, vhd_batmap_t *batmap, uint32_t block) in vhd_batmap_clear() argument
557 if (!vhd_has_batmap(ctx) || !batmap->map) in vhd_batmap_clear()
567 vhd_bitmap_test(vhd_context_t *ctx, char *map, uint32_t block) in vhd_bitmap_test() argument
569 if (vhd_creator_tapdisk(ctx) && in vhd_bitmap_test()
570 ctx->footer.crtr_ver == 0x00000001) in vhd_bitmap_test()
577 vhd_bitmap_set(vhd_context_t *ctx, char *map, uint32_t block) in vhd_bitmap_set() argument
579 if (vhd_creator_tapdisk(ctx) && in vhd_bitmap_set()
580 ctx->footer.crtr_ver == 0x00000001) in vhd_bitmap_set()
587 vhd_bitmap_clear(vhd_context_t *ctx, char *map, uint32_t block) in vhd_bitmap_clear() argument
589 if (vhd_creator_tapdisk(ctx) && in vhd_bitmap_clear()
590 ctx->footer.crtr_ver == 0x00000001) in vhd_bitmap_clear()
601 vhd_end_of_headers(vhd_context_t *ctx, off_t *end) in vhd_end_of_headers() argument
610 if (!vhd_type_dynamic(ctx)) in vhd_end_of_headers()
613 eom = ctx->footer.data_offset + sizeof(vhd_header_t); in vhd_end_of_headers()
615 bat_bytes = vhd_bytes_padded(ctx->header.max_bat_size * sizeof(uint32_t)); in vhd_end_of_headers()
616 bat_end = ctx->header.table_offset + bat_bytes; in vhd_end_of_headers()
620 if (vhd_has_batmap(ctx)) { in vhd_end_of_headers()
623 err = vhd_get_batmap(ctx); in vhd_end_of_headers()
628 err = vhd_batmap_header_offset(ctx, &hdr_end); in vhd_end_of_headers()
635 map_secs = ctx->batmap.header.batmap_size; in vhd_end_of_headers()
636 map_end = (ctx->batmap.header.batmap_offset + in vhd_end_of_headers()
642 n = sizeof(ctx->header.loc) / sizeof(vhd_parent_locator_t); in vhd_end_of_headers()
647 loc = &ctx->header.loc[i]; in vhd_end_of_headers()
660 vhd_end_of_data(vhd_context_t *ctx, off_t *end) in vhd_end_of_data() argument
666 if (!vhd_type_dynamic(ctx)) { in vhd_end_of_data()
667 err = vhd_seek(ctx, 0, SEEK_END); in vhd_end_of_data()
671 max = vhd_position(ctx); in vhd_end_of_data()
679 err = vhd_end_of_headers(ctx, &max); in vhd_end_of_data()
683 err = vhd_get_bat(ctx); in vhd_end_of_data()
689 for (i = 0; i < ctx->bat.entries; i++) { in vhd_end_of_data()
690 blk = ctx->bat.bat[i]; in vhd_end_of_data()
693 blk += ctx->spb + ctx->bm_secs; in vhd_end_of_data()
772 vhd_get_footer(vhd_context_t *ctx) in vhd_get_footer() argument
774 if (!vhd_validate_footer(&ctx->footer)) in vhd_get_footer()
777 return vhd_read_footer(ctx, &ctx->footer); in vhd_get_footer()
781 vhd_get_header(vhd_context_t *ctx) in vhd_get_header() argument
783 if (!vhd_type_dynamic(ctx)) in vhd_get_header()
786 if (!vhd_validate_header(&ctx->header)) in vhd_get_header()
789 return vhd_read_header(ctx, &ctx->header); in vhd_get_header()
793 vhd_get_bat(vhd_context_t *ctx) in vhd_get_bat() argument
795 if (!vhd_type_dynamic(ctx)) in vhd_get_bat()
798 if (!vhd_validate_bat(&ctx->bat)) in vhd_get_bat()
801 vhd_put_bat(ctx); in vhd_get_bat()
802 return vhd_read_bat(ctx, &ctx->bat); in vhd_get_bat()
806 vhd_get_batmap(vhd_context_t *ctx) in vhd_get_batmap() argument
808 if (!vhd_has_batmap(ctx)) in vhd_get_batmap()
811 if (!vhd_validate_batmap(&ctx->batmap)) in vhd_get_batmap()
814 vhd_put_batmap(ctx); in vhd_get_batmap()
815 return vhd_read_batmap(ctx, &ctx->batmap); in vhd_get_batmap()
819 vhd_put_footer(vhd_context_t *ctx) in vhd_put_footer() argument
821 memset(&ctx->footer, 0, sizeof(vhd_footer_t)); in vhd_put_footer()
825 vhd_put_header(vhd_context_t *ctx) in vhd_put_header() argument
827 memset(&ctx->header, 0, sizeof(vhd_header_t)); in vhd_put_header()
831 vhd_put_bat(vhd_context_t *ctx) in vhd_put_bat() argument
833 if (!vhd_type_dynamic(ctx)) in vhd_put_bat()
836 free(ctx->bat.bat); in vhd_put_bat()
837 memset(&ctx->bat, 0, sizeof(vhd_bat_t)); in vhd_put_bat()
841 vhd_put_batmap(vhd_context_t *ctx) in vhd_put_batmap() argument
843 if (!vhd_type_dynamic(ctx)) in vhd_put_batmap()
846 if (!vhd_has_batmap(ctx)) in vhd_put_batmap()
849 free(ctx->batmap.map); in vhd_put_batmap()
850 memset(&ctx->batmap, 0, sizeof(vhd_batmap_t)); in vhd_put_batmap()
857 vhd_read_short_footer(vhd_context_t *ctx, vhd_footer_t *footer) in vhd_read_short_footer() argument
865 err = vhd_seek(ctx, 0, SEEK_END); in vhd_read_short_footer()
869 eof = vhd_position(ctx); in vhd_read_short_footer()
875 err = vhd_seek(ctx, eof - 511, SEEK_SET); in vhd_read_short_footer()
892 vhd_read(ctx, buf, sizeof(vhd_footer_t)); in vhd_read_short_footer()
902 ctx->file, err); in vhd_read_short_footer()
908 vhd_read_footer_at(vhd_context_t *ctx, vhd_footer_t *footer, off_t off) in vhd_read_footer_at() argument
915 err = vhd_seek(ctx, off, SEEK_SET); in vhd_read_footer_at()
927 err = vhd_read(ctx, buf, sizeof(vhd_footer_t)); in vhd_read_footer_at()
939 ctx->file, off, err); in vhd_read_footer_at()
945 vhd_read_footer(vhd_context_t *ctx, vhd_footer_t *footer) in vhd_read_footer() argument
950 err = vhd_seek(ctx, 0, SEEK_END); in vhd_read_footer()
954 off = vhd_position(ctx); in vhd_read_footer()
958 err = vhd_read_footer_at(ctx, footer, off - 512); in vhd_read_footer()
962 err = vhd_read_short_footer(ctx, footer); in vhd_read_footer()
966 if (ctx->oflags & VHD_OPEN_STRICT) in vhd_read_footer()
969 return vhd_read_footer_at(ctx, footer, 0); in vhd_read_footer()
973 vhd_read_header_at(vhd_context_t *ctx, vhd_header_t *header, off_t off) in vhd_read_header_at() argument
980 if (!vhd_type_dynamic(ctx)) { in vhd_read_header_at()
985 err = vhd_seek(ctx, off, SEEK_SET); in vhd_read_header_at()
997 err = vhd_read(ctx, buf, sizeof(vhd_header_t)); in vhd_read_header_at()
1009 ctx->file, off, err); in vhd_read_header_at()
1015 vhd_read_header(vhd_context_t *ctx, vhd_header_t *header) in vhd_read_header() argument
1020 if (!vhd_type_dynamic(ctx)) { in vhd_read_header()
1021 VHDLOG("%s is not dynamic!\n", ctx->file); in vhd_read_header()
1025 off = ctx->footer.data_offset; in vhd_read_header()
1026 return vhd_read_header_at(ctx, header, off); in vhd_read_header()
1030 vhd_read_bat(vhd_context_t *ctx, vhd_bat_t *bat) in vhd_read_bat() argument
1039 if (!vhd_type_dynamic(ctx)) { in vhd_read_bat()
1044 off = ctx->header.table_offset; in vhd_read_bat()
1045 size = vhd_bytes_padded(ctx->header.max_bat_size * sizeof(uint32_t)); in vhd_read_bat()
1054 err = vhd_seek(ctx, off, SEEK_SET); in vhd_read_bat()
1058 err = vhd_read(ctx, buf, size); in vhd_read_bat()
1062 bat->spb = ctx->header.block_size >> VHD_SECTOR_SHIFT; in vhd_read_bat()
1063 bat->entries = ctx->header.max_bat_size; in vhd_read_bat()
1073 VHDLOG("%s: failed to read bat: %d\n", ctx->file, err); in vhd_read_bat()
1078 vhd_read_batmap_header(vhd_context_t *ctx, vhd_batmap_t *batmap) in vhd_read_batmap_header() argument
1087 err = vhd_batmap_header_offset(ctx, &off); in vhd_read_batmap_header()
1091 err = vhd_seek(ctx, off, SEEK_SET); in vhd_read_batmap_header()
1103 err = vhd_read(ctx, buf, size); in vhd_read_batmap_header()
1118 VHDLOG("%s: failed to read batmap header: %d\n", ctx->file, err); in vhd_read_batmap_header()
1123 vhd_read_batmap_map(vhd_context_t *ctx, vhd_batmap_t *batmap) in vhd_read_batmap_map() argument
1140 err = vhd_seek(ctx, off, SEEK_SET); in vhd_read_batmap_map()
1144 err = vhd_read(ctx, buf, map_size); in vhd_read_batmap_map()
1154 VHDLOG("%s: failed to read batmap: %d\n", ctx->file, err); in vhd_read_batmap_map()
1159 vhd_read_batmap(vhd_context_t *ctx, vhd_batmap_t *batmap) in vhd_read_batmap() argument
1163 if (!vhd_has_batmap(ctx)) in vhd_read_batmap()
1168 err = vhd_read_batmap_header(ctx, batmap); in vhd_read_batmap()
1176 err = vhd_read_batmap_map(ctx, batmap); in vhd_read_batmap()
1193 vhd_has_batmap(vhd_context_t *ctx) in vhd_has_batmap() argument
1195 if (!vhd_type_dynamic(ctx)) in vhd_has_batmap()
1198 if (!vhd_creator_tapdisk(ctx)) in vhd_has_batmap()
1201 if (ctx->footer.crtr_ver <= VHD_VERSION(0, 1)) in vhd_has_batmap()
1204 if (ctx->footer.crtr_ver >= VHD_VERSION(1, 2)) in vhd_has_batmap()
1211 if (!vhd_validate_batmap_header(&ctx->batmap)) in vhd_has_batmap()
1214 if (vhd_read_batmap_header(ctx, &ctx->batmap)) in vhd_has_batmap()
1217 return (!vhd_validate_batmap_header(&ctx->batmap)); in vhd_has_batmap()
1239 vhd_find_parent(vhd_context_t *ctx, const char *parent, char **_location) in vhd_find_parent() argument
1264 cpath = realpath(ctx->file, NULL); in vhd_find_parent()
1523 vhd_header_decode_parent(vhd_context_t *ctx, vhd_header_t *header, char **buf) in vhd_header_decode_parent() argument
1527 if (vhd_creator_tapdisk(ctx) && in vhd_header_decode_parent()
1528 ctx->footer.crtr_ver == VHD_VERSION(0, 1)) in vhd_header_decode_parent()
1538 vhd_parent_locator_read(vhd_context_t *ctx, in vhd_parent_locator_read() argument
1549 if (ctx->footer.type != HD_TYPE_DIFF) { in vhd_parent_locator_read()
1564 err = vhd_seek(ctx, loc->data_offset, SEEK_SET); in vhd_parent_locator_read()
1581 err = vhd_read(ctx, raw, size); in vhd_parent_locator_read()
1616 ctx->file, err); in vhd_parent_locator_read()
1618 "off 0x%"PRIx64"\n", ctx->file, loc->code, loc->data_space, in vhd_parent_locator_read()
1626 vhd_parent_locator_get(vhd_context_t *ctx, char **parent) in vhd_parent_locator_get() argument
1635 if (ctx->footer.type != HD_TYPE_DIFF) in vhd_parent_locator_get()
1638 n = vhd_parent_locator_count(ctx); in vhd_parent_locator_get()
1640 loc = ctx->header.loc + i; in vhd_parent_locator_get()
1641 err = vhd_parent_locator_read(ctx, loc, &name); in vhd_parent_locator_get()
1645 err = vhd_find_parent(ctx, name, &location); in vhd_parent_locator_get()
1648 ctx->file, name, err); in vhd_parent_locator_get()
1661 vhd_parent_locator_write_at(vhd_context_t *ctx, in vhd_parent_locator_write_at() argument
1671 if (ctx->footer.type != HD_TYPE_DIFF) in vhd_parent_locator_write_at()
1707 relative_path = relative_path_to(ctx->file, absolute_path, &err); in vhd_parent_locator_write_at()
1728 err = vhd_seek(ctx, off, SEEK_SET); in vhd_parent_locator_write_at()
1749 err = vhd_write(ctx, block, size); in vhd_parent_locator_write_at()
1778 vhd_footer_offset_at_eof(vhd_context_t *ctx, off_t *off) in vhd_footer_offset_at_eof() argument
1781 if ((err = vhd_seek(ctx, 0, SEEK_END))) in vhd_footer_offset_at_eof()
1783 *off = vhd_position(ctx) - sizeof(vhd_footer_t); in vhd_footer_offset_at_eof()
1788 vhd_read_bitmap(vhd_context_t *ctx, uint32_t block, char **bufp) in vhd_read_bitmap() argument
1799 if (!vhd_type_dynamic(ctx)) in vhd_read_bitmap()
1802 err = vhd_get_bat(ctx); in vhd_read_bitmap()
1806 if (block >= ctx->bat.entries) in vhd_read_bitmap()
1809 blk = ctx->bat.bat[block]; in vhd_read_bitmap()
1814 size = vhd_bytes_padded(ctx->spb >> 3); in vhd_read_bitmap()
1816 err = vhd_seek(ctx, off, SEEK_SET); in vhd_read_bitmap()
1824 err = vhd_read(ctx, buf, size); in vhd_read_bitmap()
1837 vhd_read_block(vhd_context_t *ctx, uint32_t block, char **bufp) in vhd_read_block() argument
1848 if (!vhd_type_dynamic(ctx)) in vhd_read_block()
1851 err = vhd_get_bat(ctx); in vhd_read_block()
1855 if (block >= ctx->bat.entries) in vhd_read_block()
1858 blk = ctx->bat.bat[block]; in vhd_read_block()
1862 off = vhd_sectors_to_bytes(blk + ctx->bm_secs); in vhd_read_block()
1863 size = vhd_sectors_to_bytes(ctx->spb); in vhd_read_block()
1865 err = vhd_footer_offset_at_eof(ctx, &end); in vhd_read_block()
1875 if (end < off + ctx->header.block_size) { in vhd_read_block()
1877 memset(buf + size, 0, ctx->header.block_size - size); in vhd_read_block()
1880 err = vhd_seek(ctx, off, SEEK_SET); in vhd_read_block()
1884 err = vhd_read(ctx, buf, size); in vhd_read_block()
1897 vhd_write_footer_at(vhd_context_t *ctx, vhd_footer_t *footer, off_t off) in vhd_write_footer_at() argument
1919 err = vhd_seek(ctx, off, SEEK_SET); in vhd_write_footer_at()
1925 err = vhd_write(ctx, f, sizeof(vhd_footer_t)); in vhd_write_footer_at()
1930 ctx->file, off, err); in vhd_write_footer_at()
1936 vhd_write_footer(vhd_context_t *ctx, vhd_footer_t *footer) in vhd_write_footer() argument
1941 if (ctx->is_block) in vhd_write_footer()
1942 err = vhd_footer_offset_at_eof(ctx, &off); in vhd_write_footer()
1944 err = vhd_end_of_data(ctx, &off); in vhd_write_footer()
1948 err = vhd_write_footer_at(ctx, footer, off); in vhd_write_footer()
1952 if (!vhd_type_dynamic(ctx)) in vhd_write_footer()
1955 return vhd_write_footer_at(ctx, footer, 0); in vhd_write_footer()
1959 vhd_write_header_at(vhd_context_t *ctx, vhd_header_t *header, off_t off) in vhd_write_header_at() argument
1966 if (!vhd_type_dynamic(ctx)) { in vhd_write_header_at()
1988 err = vhd_seek(ctx, off, SEEK_SET); in vhd_write_header_at()
1992 err = vhd_write(ctx, h, sizeof(vhd_header_t)); in vhd_write_header_at()
1997 ctx->file, off, err); in vhd_write_header_at()
2003 vhd_write_header(vhd_context_t *ctx, vhd_header_t *header) in vhd_write_header() argument
2008 if (!vhd_type_dynamic(ctx)) in vhd_write_header()
2011 off = ctx->footer.data_offset; in vhd_write_header()
2012 return vhd_write_header_at(ctx, header, off); in vhd_write_header()
2016 vhd_write_bat(vhd_context_t *ctx, vhd_bat_t *bat) in vhd_write_bat() argument
2023 if (!vhd_type_dynamic(ctx)) in vhd_write_bat()
2026 err = vhd_validate_bat(&ctx->bat); in vhd_write_bat()
2036 off = ctx->header.table_offset; in vhd_write_bat()
2039 err = vhd_seek(ctx, off, SEEK_SET); in vhd_write_bat()
2052 err = vhd_write(ctx, b.bat, size); in vhd_write_bat()
2059 vhd_write_batmap(vhd_context_t *ctx, vhd_batmap_t *batmap) in vhd_write_batmap() argument
2070 if (!vhd_has_batmap(ctx)) { in vhd_write_batmap()
2086 err = vhd_seek(ctx, off, SEEK_SET); in vhd_write_batmap()
2099 err = vhd_write(ctx, map, map_size); in vhd_write_batmap()
2103 err = vhd_batmap_header_offset(ctx, &off); in vhd_write_batmap()
2109 err = vhd_seek(ctx, off, SEEK_SET); in vhd_write_batmap()
2124 err = vhd_write(ctx, buf, size); in vhd_write_batmap()
2128 VHDLOG("%s: failed writing batmap: %d\n", ctx->file, err); in vhd_write_batmap()
2135 vhd_write_bitmap(vhd_context_t *ctx, uint32_t block, char *bitmap) in vhd_write_bitmap() argument
2142 if (!vhd_type_dynamic(ctx)) in vhd_write_bitmap()
2145 err = vhd_validate_bat(&ctx->bat); in vhd_write_bitmap()
2149 if (block >= ctx->bat.entries) in vhd_write_bitmap()
2155 blk = ctx->bat.bat[block]; in vhd_write_bitmap()
2160 size = vhd_sectors_to_bytes(ctx->bm_secs); in vhd_write_bitmap()
2162 err = vhd_seek(ctx, off, SEEK_SET); in vhd_write_bitmap()
2166 err = vhd_write(ctx, bitmap, size); in vhd_write_bitmap()
2174 vhd_write_block(vhd_context_t *ctx, uint32_t block, char *data) in vhd_write_block() argument
2181 if (!vhd_type_dynamic(ctx)) in vhd_write_block()
2184 err = vhd_validate_bat(&ctx->bat); in vhd_write_block()
2188 if (block >= ctx->bat.entries) in vhd_write_block()
2194 blk = ctx->bat.bat[block]; in vhd_write_block()
2198 off = vhd_sectors_to_bytes(blk + ctx->bm_secs); in vhd_write_block()
2199 size = vhd_sectors_to_bytes(ctx->spb); in vhd_write_block()
2201 err = vhd_seek(ctx, off, SEEK_SET); in vhd_write_block()
2205 err = vhd_write(ctx, data, size); in vhd_write_block()
2228 vhd_seek(vhd_context_t *ctx, off_t offset, int whence) in vhd_seek() argument
2232 off = lseek(ctx->fd, offset, whence); in vhd_seek()
2235 ctx->file, offset, whence, -errno); in vhd_seek()
2243 vhd_position(vhd_context_t *ctx) in vhd_position() argument
2245 return lseek(ctx->fd, 0, SEEK_CUR); in vhd_position()
2249 vhd_read(vhd_context_t *ctx, void *buf, size_t size) in vhd_read() argument
2255 ret = read(ctx->fd, buf, size); in vhd_read()
2260 ctx->file, size, ret, -errno); in vhd_read()
2266 vhd_write(vhd_context_t *ctx, void *buf, size_t size) in vhd_write() argument
2272 ret = write(ctx->fd, buf, size); in vhd_write()
2277 ctx->file, size, ret, -errno); in vhd_write()
2283 vhd_offset(vhd_context_t *ctx, uint32_t sector, uint32_t *offset) in vhd_offset() argument
2288 if (!vhd_type_dynamic(ctx)) in vhd_offset()
2291 err = vhd_get_bat(ctx); in vhd_offset()
2295 block = sector / ctx->spb; in vhd_offset()
2296 if (ctx->bat.bat[block] == DD_BLK_UNUSED) in vhd_offset()
2299 *offset = ctx->bat.bat[block] + in vhd_offset()
2300 ctx->bm_secs + (sector % ctx->spb); in vhd_offset()
2306 vhd_open_fast(vhd_context_t *ctx) in vhd_open_fast() argument
2315 VHDLOG("failed allocating %s: %d\n", ctx->file, -err); in vhd_open_fast()
2319 err = vhd_read(ctx, buf, size); in vhd_open_fast()
2321 VHDLOG("failed reading %s: %d\n", ctx->file, err); in vhd_open_fast()
2325 memcpy(&ctx->footer, buf, sizeof(vhd_footer_t)); in vhd_open_fast()
2326 vhd_footer_in(&ctx->footer); in vhd_open_fast()
2327 err = vhd_validate_footer(&ctx->footer); in vhd_open_fast()
2331 if (vhd_type_dynamic(ctx)) { in vhd_open_fast()
2332 if (ctx->footer.data_offset != sizeof(vhd_footer_t)) in vhd_open_fast()
2333 err = vhd_read_header(ctx, &ctx->header); in vhd_open_fast()
2335 memcpy(&ctx->header, in vhd_open_fast()
2338 vhd_header_in(&ctx->header); in vhd_open_fast()
2339 err = vhd_validate_header(&ctx->header); in vhd_open_fast()
2345 ctx->spb = ctx->header.block_size >> VHD_SECTOR_SHIFT; in vhd_open_fast()
2346 ctx->bm_secs = secs_round_up_no_zero(ctx->spb >> 3); in vhd_open_fast()
2355 vhd_open(vhd_context_t *ctx, const char *file, int flags) in vhd_open() argument
2362 memset(ctx, 0, sizeof(vhd_context_t)); in vhd_open()
2363 ctx->fd = -1; in vhd_open()
2364 ctx->oflags = flags; in vhd_open()
2366 err = namedup(&ctx->file, file); in vhd_open()
2376 ctx->fd = open(ctx->file, oflags, 0644); in vhd_open()
2377 if (ctx->fd == -1) { in vhd_open()
2379 VHDLOG("failed to open %s: %d\n", ctx->file, err); in vhd_open()
2383 err = vhd_test_file_fixed(ctx->file, &ctx->is_block); in vhd_open()
2388 err = vhd_open_fast(ctx); in vhd_open()
2395 err = vhd_read_footer(ctx, &ctx->footer); in vhd_open()
2399 if (!(flags & VHD_OPEN_IGNORE_DISABLED) && vhd_disabled(ctx)) { in vhd_open()
2404 if (vhd_type_dynamic(ctx)) { in vhd_open()
2405 err = vhd_read_header(ctx, &ctx->header); in vhd_open()
2409 ctx->spb = ctx->header.block_size >> VHD_SECTOR_SHIFT; in vhd_open()
2410 ctx->bm_secs = secs_round_up_no_zero(ctx->spb >> 3); in vhd_open()
2416 if (ctx->fd != -1) in vhd_open()
2417 close(ctx->fd); in vhd_open()
2418 free(ctx->file); in vhd_open()
2419 memset(ctx, 0, sizeof(vhd_context_t)); in vhd_open()
2424 vhd_close(vhd_context_t *ctx) in vhd_close() argument
2426 if (ctx->file) in vhd_close()
2427 close(ctx->fd); in vhd_close()
2428 free(ctx->file); in vhd_close()
2429 free(ctx->bat.bat); in vhd_close()
2430 free(ctx->batmap.map); in vhd_close()
2431 memset(ctx, 0, sizeof(vhd_context_t)); in vhd_close()
2435 vhd_initialize_footer(vhd_context_t *ctx, int type, uint64_t size) in vhd_initialize_footer() argument
2437 memset(&ctx->footer, 0, sizeof(vhd_footer_t)); in vhd_initialize_footer()
2438 memcpy(ctx->footer.cookie, HD_COOKIE, sizeof(ctx->footer.cookie)); in vhd_initialize_footer()
2439 ctx->footer.features = HD_RESERVED; in vhd_initialize_footer()
2440 ctx->footer.ff_version = HD_FF_VERSION; in vhd_initialize_footer()
2441 ctx->footer.timestamp = vhd_time(time(NULL)); in vhd_initialize_footer()
2442 ctx->footer.crtr_ver = VHD_CURRENT_VERSION; in vhd_initialize_footer()
2443 ctx->footer.crtr_os = 0x00000000; in vhd_initialize_footer()
2444 ctx->footer.orig_size = size; in vhd_initialize_footer()
2445 ctx->footer.curr_size = size; in vhd_initialize_footer()
2446 ctx->footer.geometry = vhd_chs(size); in vhd_initialize_footer()
2447 ctx->footer.type = type; in vhd_initialize_footer()
2448 ctx->footer.saved = 0; in vhd_initialize_footer()
2449 ctx->footer.data_offset = 0xFFFFFFFFFFFFFFFF; in vhd_initialize_footer()
2450 strcpy(ctx->footer.crtr_app, "tap"); in vhd_initialize_footer()
2451 vhd_uuid_generate(&ctx->footer.uuid); in vhd_initialize_footer()
2455 vhd_initialize_header_parent_name(vhd_context_t *ctx, const char *parent_path) in vhd_initialize_header_parent_name() argument
2492 obl = sizeof(ctx->header.prt_name); in vhd_initialize_header_parent_name()
2493 dst = ctx->header.prt_name; in vhd_initialize_header_parent_name()
2527 vhd_initialize_header(vhd_context_t *ctx, const char *parent_path, in vhd_initialize_header() argument
2534 if (!vhd_type_dynamic(ctx)) in vhd_initialize_header()
2537 memset(&ctx->header, 0, sizeof(vhd_header_t)); in vhd_initialize_header()
2538 memcpy(ctx->header.cookie, DD_COOKIE, sizeof(ctx->header.cookie)); in vhd_initialize_header()
2539 ctx->header.data_offset = (uint64_t)-1; in vhd_initialize_header()
2540 ctx->header.table_offset = VHD_SECTOR_SIZE * 3; /* 1 ftr + 2 hdr */ in vhd_initialize_header()
2541 ctx->header.hdr_ver = DD_VERSION; in vhd_initialize_header()
2542 ctx->header.block_size = VHD_BLOCK_SIZE; in vhd_initialize_header()
2543 ctx->header.prt_ts = 0; in vhd_initialize_header()
2544 ctx->header.res1 = 0; in vhd_initialize_header()
2545 ctx->header.max_bat_size = (ctx->footer.curr_size + in vhd_initialize_header()
2548 ctx->footer.data_offset = VHD_SECTOR_SIZE; in vhd_initialize_header()
2550 if (ctx->footer.type == HD_TYPE_DYNAMIC) in vhd_initialize_header()
2558 ctx->header.prt_ts = vhd_time(stats.st_mtime); in vhd_initialize_header()
2567 ctx->header.prt_ts = vhd_time(stats.st_mtime); in vhd_initialize_header()
2568 vhd_uuid_copy(&ctx->header.prt_uuid, &parent.footer.uuid); in vhd_initialize_header()
2573 ctx->footer.orig_size = size; in vhd_initialize_header()
2574 ctx->footer.curr_size = size; in vhd_initialize_header()
2575 ctx->footer.geometry = vhd_chs(size); in vhd_initialize_header()
2576 ctx->header.max_bat_size = in vhd_initialize_header()
2579 return vhd_initialize_header_parent_name(ctx, parent_path); in vhd_initialize_header()
2583 vhd_write_parent_locators(vhd_context_t *ctx, const char *parent) in vhd_write_parent_locators() argument
2591 if (ctx->footer.type != HD_TYPE_DIFF) in vhd_write_parent_locators()
2594 off = ctx->batmap.header.batmap_offset + in vhd_write_parent_locators()
2595 vhd_sectors_to_bytes(ctx->batmap.header.batmap_size); in vhd_write_parent_locators()
2612 err = vhd_parent_locator_write_at(ctx, parent, off, code, in vhd_write_parent_locators()
2613 0, ctx->header.loc + i); in vhd_write_parent_locators()
2617 off += vhd_parent_locator_size(ctx->header.loc + i); in vhd_write_parent_locators()
2704 vhd_create_batmap(vhd_context_t *ctx) in vhd_create_batmap() argument
2710 if (!vhd_type_dynamic(ctx)) in vhd_create_batmap()
2713 map_bytes = (ctx->header.max_bat_size + 7) >> 3; in vhd_create_batmap()
2714 header = &ctx->batmap.header; in vhd_create_batmap()
2719 err = vhd_batmap_header_offset(ctx, &off); in vhd_create_batmap()
2730 err = posix_memalign((void **)&ctx->batmap.map, in vhd_create_batmap()
2733 ctx->batmap.map = NULL; in vhd_create_batmap()
2737 memset(ctx->batmap.map, 0, map_bytes); in vhd_create_batmap()
2739 return vhd_write_batmap(ctx, &ctx->batmap); in vhd_create_batmap()
2743 vhd_create_bat(vhd_context_t *ctx) in vhd_create_bat() argument
2748 if (!vhd_type_dynamic(ctx)) in vhd_create_bat()
2751 size = vhd_bytes_padded(ctx->header.max_bat_size * sizeof(uint32_t)); in vhd_create_bat()
2752 err = posix_memalign((void **)&ctx->bat.bat, VHD_SECTOR_SIZE, size); in vhd_create_bat()
2754 ctx->bat.bat = NULL; in vhd_create_bat()
2758 memset(ctx->bat.bat, 0, size); in vhd_create_bat()
2759 for (i = 0; i < ctx->header.max_bat_size; i++) in vhd_create_bat()
2760 ctx->bat.bat[i] = DD_BLK_UNUSED; in vhd_create_bat()
2762 err = vhd_seek(ctx, ctx->header.table_offset, SEEK_SET); in vhd_create_bat()
2766 ctx->bat.entries = ctx->header.max_bat_size; in vhd_create_bat()
2767 ctx->bat.spb = ctx->header.block_size >> VHD_SECTOR_SHIFT; in vhd_create_bat()
2769 return vhd_write_bat(ctx, &ctx->bat); in vhd_create_bat()
2773 vhd_initialize_fixed_disk(vhd_context_t *ctx) in vhd_initialize_fixed_disk() argument
2778 if (ctx->footer.type != HD_TYPE_FIXED) in vhd_initialize_fixed_disk()
2781 err = vhd_seek(ctx, 0, SEEK_SET); in vhd_initialize_fixed_disk()
2790 for (i = 0; i < ctx->footer.curr_size >> VHD_BLOCK_SHIFT; i++) { in vhd_initialize_fixed_disk()
2791 err = vhd_write(ctx, buf, VHD_BLOCK_SIZE); in vhd_initialize_fixed_disk()
2804 vhd_get_phys_size(vhd_context_t *ctx, off_t *size) in vhd_get_phys_size() argument
2808 if ((err = vhd_end_of_data(ctx, size))) in vhd_get_phys_size()
2815 vhd_set_phys_size(vhd_context_t *ctx, off_t size) in vhd_set_phys_size() argument
2820 err = vhd_get_phys_size(ctx, &phys_size); in vhd_set_phys_size()
2829 return vhd_write_footer_at(ctx, &ctx->footer, in vhd_set_phys_size()
2839 vhd_context_t ctx; in __vhd_create() local
2858 memset(&ctx, 0, sizeof(vhd_context_t)); in __vhd_create()
2859 footer = &ctx.footer; in __vhd_create()
2860 header = &ctx.header; in __vhd_create()
2864 ctx.fd = open(name, O_WRONLY | O_CREAT | in __vhd_create()
2866 if (ctx.fd == -1) in __vhd_create()
2869 ctx.file = strdup(name); in __vhd_create()
2870 if (!ctx.file) { in __vhd_create()
2875 err = vhd_test_file_fixed(ctx.file, &ctx.is_block); in __vhd_create()
2879 vhd_initialize_footer(&ctx, type, size); in __vhd_create()
2882 err = vhd_initialize_fixed_disk(&ctx); in __vhd_create()
2887 err = vhd_initialize_header(&ctx, parent, size, raw); in __vhd_create()
2891 err = vhd_write_footer_at(&ctx, &ctx.footer, 0); in __vhd_create()
2895 err = vhd_write_header_at(&ctx, &ctx.header, VHD_SECTOR_SIZE); in __vhd_create()
2899 err = vhd_create_batmap(&ctx); in __vhd_create()
2903 err = vhd_create_bat(&ctx); in __vhd_create()
2908 err = vhd_write_parent_locators(&ctx, parent); in __vhd_create()
2914 err = vhd_write_header_at(&ctx, &ctx.header, VHD_SECTOR_SIZE); in __vhd_create()
2919 err = vhd_seek(&ctx, 0, SEEK_END); in __vhd_create()
2923 off = vhd_position(&ctx); in __vhd_create()
2929 if (ctx.is_block) in __vhd_create()
2932 err = vhd_write_footer_at(&ctx, &ctx.footer, off); in __vhd_create()
2939 vhd_close(&ctx); in __vhd_create()
2940 if (err && !ctx.is_block) in __vhd_create()
2959 __vhd_io_fixed_read(vhd_context_t *ctx, in __vhd_io_fixed_read() argument
2964 err = vhd_seek(ctx, vhd_sectors_to_bytes(sec), SEEK_SET); in __vhd_io_fixed_read()
2968 return vhd_read(ctx, buf, vhd_sectors_to_bytes(secs)); in __vhd_io_fixed_read()
2972 __vhd_io_dynamic_copy_data(vhd_context_t *ctx, in __vhd_io_dynamic_copy_data() argument
2983 if (ctx && !vhd_bitmap_test(ctx, bitmap, bitmap_off + i)) in __vhd_io_dynamic_copy_data()
2996 __vhd_io_dynamic_read_link(vhd_context_t *ctx, char *map, in __vhd_io_dynamic_read_link() argument
3007 blk = sector / ctx->spb; in __vhd_io_dynamic_read_link()
3008 sec = sector % ctx->spb; in __vhd_io_dynamic_read_link()
3009 off = ctx->bat.bat[blk]; in __vhd_io_dynamic_read_link()
3014 cnt = MIN(secs, ctx->spb); in __vhd_io_dynamic_read_link()
3018 err = vhd_read_bitmap(ctx, blk, &bitmap); in __vhd_io_dynamic_read_link()
3022 err = vhd_read_block(ctx, blk, &data); in __vhd_io_dynamic_read_link()
3028 cnt = MIN(secs, ctx->spb - sec); in __vhd_io_dynamic_read_link()
3031 __vhd_io_dynamic_copy_data(ctx, in __vhd_io_dynamic_read_link()
3098 __vhd_io_dynamic_read(vhd_context_t *ctx, in __vhd_io_dynamic_read() argument
3106 err = vhd_get_bat(ctx); in __vhd_io_dynamic_read()
3110 vhd = ctx; in __vhd_io_dynamic_read()
3146 if (vhd != ctx) in __vhd_io_dynamic_read()
3163 if (vhd != ctx) in __vhd_io_dynamic_read()
3172 vhd_io_read(vhd_context_t *ctx, char *buf, uint64_t sec, uint32_t secs) in vhd_io_read() argument
3174 if (vhd_sectors_to_bytes(sec + secs) > ctx->footer.curr_size) in vhd_io_read()
3177 if (!vhd_type_dynamic(ctx)) in vhd_io_read()
3178 return __vhd_io_fixed_read(ctx, buf, sec, secs); in vhd_io_read()
3180 return __vhd_io_dynamic_read(ctx, buf, sec, secs); in vhd_io_read()
3184 __vhd_io_fixed_write(vhd_context_t *ctx, in __vhd_io_fixed_write() argument
3189 err = vhd_seek(ctx, vhd_sectors_to_bytes(sec), SEEK_SET); in __vhd_io_fixed_write()
3193 return vhd_write(ctx, buf, vhd_sectors_to_bytes(secs)); in __vhd_io_fixed_write()
3197 __vhd_io_allocate_block(vhd_context_t *ctx, uint32_t block) in __vhd_io_allocate_block() argument
3206 err = vhd_end_of_data(ctx, &max); in __vhd_io_allocate_block()
3215 if ((max + ctx->bm_secs) % spp) { in __vhd_io_allocate_block()
3216 gap = (spp - ((max + ctx->bm_secs) % spp)); in __vhd_io_allocate_block()
3220 err = vhd_seek(ctx, off, SEEK_SET); in __vhd_io_allocate_block()
3224 size = vhd_sectors_to_bytes(ctx->spb + ctx->bm_secs + gap); in __vhd_io_allocate_block()
3229 err = vhd_write(ctx, buf, size); in __vhd_io_allocate_block()
3233 ctx->bat.bat[block] = max; in __vhd_io_allocate_block()
3234 err = vhd_write_bat(ctx, &ctx->bat); in __vhd_io_allocate_block()
3246 __vhd_io_dynamic_write(vhd_context_t *ctx, in __vhd_io_dynamic_write() argument
3254 if (vhd_sectors_to_bytes(sector + secs) > ctx->footer.curr_size) in __vhd_io_dynamic_write()
3257 err = vhd_get_bat(ctx); in __vhd_io_dynamic_write()
3261 if (vhd_has_batmap(ctx)) { in __vhd_io_dynamic_write()
3262 err = vhd_get_batmap(ctx); in __vhd_io_dynamic_write()
3268 blk = sector / ctx->spb; in __vhd_io_dynamic_write()
3269 sec = sector % ctx->spb; in __vhd_io_dynamic_write()
3271 off = ctx->bat.bat[blk]; in __vhd_io_dynamic_write()
3273 err = __vhd_io_allocate_block(ctx, blk); in __vhd_io_dynamic_write()
3277 off = ctx->bat.bat[blk]; in __vhd_io_dynamic_write()
3280 off += ctx->bm_secs + sec; in __vhd_io_dynamic_write()
3281 err = vhd_seek(ctx, vhd_sectors_to_bytes(off), SEEK_SET); in __vhd_io_dynamic_write()
3285 cnt = MIN(secs, ctx->spb - sec); in __vhd_io_dynamic_write()
3286 err = vhd_write(ctx, buf, vhd_sectors_to_bytes(cnt)); in __vhd_io_dynamic_write()
3290 if (vhd_has_batmap(ctx) && in __vhd_io_dynamic_write()
3291 vhd_batmap_test(ctx, &ctx->batmap, blk)) in __vhd_io_dynamic_write()
3294 err = vhd_read_bitmap(ctx, blk, &map); in __vhd_io_dynamic_write()
3299 vhd_bitmap_set(ctx, map, sec + i); in __vhd_io_dynamic_write()
3301 err = vhd_write_bitmap(ctx, blk, map); in __vhd_io_dynamic_write()
3305 if (vhd_has_batmap(ctx)) { in __vhd_io_dynamic_write()
3306 for (i = 0; i < ctx->spb; i++) in __vhd_io_dynamic_write()
3307 if (!vhd_bitmap_test(ctx, map, i)) { in __vhd_io_dynamic_write()
3312 vhd_batmap_set(ctx, &ctx->batmap, blk); in __vhd_io_dynamic_write()
3313 err = vhd_write_batmap(ctx, &ctx->batmap); in __vhd_io_dynamic_write()
3330 ret = vhd_write_footer(ctx, &ctx->footer); in __vhd_io_dynamic_write()
3339 vhd_io_write(vhd_context_t *ctx, char *buf, uint64_t sec, uint32_t secs) in vhd_io_write() argument
3341 if (vhd_sectors_to_bytes(sec + secs) > ctx->footer.curr_size) in vhd_io_write()
3344 if (!vhd_type_dynamic(ctx)) in vhd_io_write()
3345 return __vhd_io_fixed_write(ctx, buf, sec, secs); in vhd_io_write()
3347 return __vhd_io_dynamic_write(ctx, buf, sec, secs); in vhd_io_write()