qcow2.c (67f17e23baca5dd545fe98b01169cc351a70fe35) qcow2.c (6d49d3a859b0f19226dbb0df5e7f50267b42f45c)
1/*
2 * Block driver for the QCOW version 2 format
3 *
4 * Copyright (c) 2004-2006 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights

--- 4594 unchanged lines hidden (view full) ---

4603
4604 qemu_co_mutex_lock(&s->lock);
4605 ret = qcow2_write_caches(bs);
4606 qemu_co_mutex_unlock(&s->lock);
4607
4608 return ret;
4609}
4610
1/*
2 * Block driver for the QCOW version 2 format
3 *
4 * Copyright (c) 2004-2006 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights

--- 4594 unchanged lines hidden (view full) ---

4603
4604 qemu_co_mutex_lock(&s->lock);
4605 ret = qcow2_write_caches(bs);
4606 qemu_co_mutex_unlock(&s->lock);
4607
4608 return ret;
4609}
4610
4611static ssize_t qcow2_measure_crypto_hdr_init_func(QCryptoBlock *block,
4612 size_t headerlen, void *opaque, Error **errp)
4613{
4614 size_t *headerlenp = opaque;
4615
4616 /* Stash away the payload size */
4617 *headerlenp = headerlen;
4618 return 0;
4619}
4620
4621static ssize_t qcow2_measure_crypto_hdr_write_func(QCryptoBlock *block,
4622 size_t offset, const uint8_t *buf, size_t buflen,
4623 void *opaque, Error **errp)
4624{
4625 /* Discard the bytes, we're not actually writing to an image */
4626 return buflen;
4627}
4628
4629/* Determine the number of bytes for the LUKS payload */
4630static bool qcow2_measure_luks_headerlen(QemuOpts *opts, size_t *len,
4631 Error **errp)
4632{
4633 QDict *opts_qdict;
4634 QDict *cryptoopts_qdict;
4635 QCryptoBlockCreateOptions *cryptoopts;
4636 QCryptoBlock *crypto;
4637
4638 /* Extract "encrypt." options into a qdict */
4639 opts_qdict = qemu_opts_to_qdict(opts, NULL);
4640 qdict_extract_subqdict(opts_qdict, &cryptoopts_qdict, "encrypt.");
4641 qobject_unref(opts_qdict);
4642
4643 /* Build QCryptoBlockCreateOptions object from qdict */
4644 qdict_put_str(cryptoopts_qdict, "format", "luks");
4645 cryptoopts = block_crypto_create_opts_init(cryptoopts_qdict, errp);
4646 qobject_unref(cryptoopts_qdict);
4647 if (!cryptoopts) {
4648 return false;
4649 }
4650
4651 /* Fake LUKS creation in order to determine the payload size */
4652 crypto = qcrypto_block_create(cryptoopts, "encrypt.",
4653 qcow2_measure_crypto_hdr_init_func,
4654 qcow2_measure_crypto_hdr_write_func,
4655 len, errp);
4656 qapi_free_QCryptoBlockCreateOptions(cryptoopts);
4657 if (!crypto) {
4658 return false;
4659 }
4660
4661 qcrypto_block_free(crypto);
4662 return true;
4663}
4664
4665static BlockMeasureInfo *qcow2_measure(QemuOpts *opts, BlockDriverState *in_bs,
4666 Error **errp)
4667{
4668 Error *local_err = NULL;
4669 BlockMeasureInfo *info;
4670 uint64_t required = 0; /* bytes that contribute to required size */
4671 uint64_t virtual_size; /* disk size as seen by guest */
4672 uint64_t refcount_bits;

--- 34 unchanged lines hidden (view full) ---

4707 has_backing_file = !!optstr;
4708 g_free(optstr);
4709
4710 optstr = qemu_opt_get_del(opts, BLOCK_OPT_ENCRYPT_FORMAT);
4711 has_luks = optstr && strcmp(optstr, "luks") == 0;
4712 g_free(optstr);
4713
4714 if (has_luks) {
4611static BlockMeasureInfo *qcow2_measure(QemuOpts *opts, BlockDriverState *in_bs,
4612 Error **errp)
4613{
4614 Error *local_err = NULL;
4615 BlockMeasureInfo *info;
4616 uint64_t required = 0; /* bytes that contribute to required size */
4617 uint64_t virtual_size; /* disk size as seen by guest */
4618 uint64_t refcount_bits;

--- 34 unchanged lines hidden (view full) ---

4653 has_backing_file = !!optstr;
4654 g_free(optstr);
4655
4656 optstr = qemu_opt_get_del(opts, BLOCK_OPT_ENCRYPT_FORMAT);
4657 has_luks = optstr && strcmp(optstr, "luks") == 0;
4658 g_free(optstr);
4659
4660 if (has_luks) {
4661 g_autoptr(QCryptoBlockCreateOptions) create_opts = NULL;
4662 QDict *opts_qdict;
4663 QDict *cryptoopts;
4715 size_t headerlen;
4716
4664 size_t headerlen;
4665
4717 if (!qcow2_measure_luks_headerlen(opts, &headerlen, &local_err)) {
4666 opts_qdict = qemu_opts_to_qdict(opts, NULL);
4667 qdict_extract_subqdict(opts_qdict, &cryptoopts, "encrypt.");
4668 qobject_unref(opts_qdict);
4669
4670 qdict_put_str(cryptoopts, "format", "luks");
4671
4672 create_opts = block_crypto_create_opts_init(cryptoopts, errp);
4673 qobject_unref(cryptoopts);
4674 if (!create_opts) {
4718 goto err;
4719 }
4720
4675 goto err;
4676 }
4677
4678 if (!qcrypto_block_calculate_payload_offset(create_opts,
4679 "encrypt.",
4680 &headerlen,
4681 &local_err)) {
4682 goto err;
4683 }
4684
4721 luks_payload_size = ROUND_UP(headerlen, cluster_size);
4722 }
4723
4724 virtual_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0);
4725 virtual_size = ROUND_UP(virtual_size, cluster_size);
4726
4727 /* Check that virtual disk size is valid */
4728 l2_tables = DIV_ROUND_UP(virtual_size / cluster_size,

--- 873 unchanged lines hidden ---
4685 luks_payload_size = ROUND_UP(headerlen, cluster_size);
4686 }
4687
4688 virtual_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0);
4689 virtual_size = ROUND_UP(virtual_size, cluster_size);
4690
4691 /* Check that virtual disk size is valid */
4692 l2_tables = DIV_ROUND_UP(virtual_size / cluster_size,

--- 873 unchanged lines hidden ---