2 * QEMU block full disk encryption
4 * Copyright (c) 2015-2016 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
23 #include "block/block_int.h"
24 #include "block/qdict.h"
25 #include "sysemu/block-backend.h"
26 #include "crypto/block.h"
27 #include "qapi/opts-visitor.h"
28 #include "qapi/qapi-visit-crypto.h"
29 #include "qapi/qobject-input-visitor.h"
30 #include "qapi/error.h"
31 #include "qemu/module.h"
32 #include "qemu/option.h"
33 #include "qemu/cutils.h"
34 #include "qemu/memalign.h"
37 typedef struct BlockCrypto BlockCrypto
;
45 static int block_crypto_probe_generic(QCryptoBlockFormat format
,
50 if (qcrypto_block_has_format(format
, buf
, buf_size
)) {
58 static int block_crypto_read_func(QCryptoBlock
*block
,
65 BlockDriverState
*bs
= opaque
;
69 GRAPH_RDLOCK_GUARD_MAINLOOP();
71 ret
= bdrv_pread(bs
->file
, offset
, buflen
, buf
, 0);
73 error_setg_errno(errp
, -ret
, "Could not read encryption header");
79 static int block_crypto_write_func(QCryptoBlock
*block
,
86 BlockDriverState
*bs
= opaque
;
90 GRAPH_RDLOCK_GUARD_MAINLOOP();
92 ret
= bdrv_pwrite(bs
->file
, offset
, buflen
, buf
, 0);
94 error_setg_errno(errp
, -ret
, "Could not write encryption header");
101 struct BlockCryptoCreateData
{
104 PreallocMode prealloc
;
108 static int coroutine_fn GRAPH_UNLOCKED
109 block_crypto_create_write_func(QCryptoBlock
*block
, size_t offset
,
110 const uint8_t *buf
, size_t buflen
, void *opaque
,
113 struct BlockCryptoCreateData
*data
= opaque
;
116 ret
= blk_pwrite(data
->blk
, offset
, buflen
, buf
, 0);
118 error_setg_errno(errp
, -ret
, "Could not write encryption header");
124 static int coroutine_fn GRAPH_UNLOCKED
125 block_crypto_create_init_func(QCryptoBlock
*block
, size_t headerlen
,
126 void *opaque
, Error
**errp
)
128 struct BlockCryptoCreateData
*data
= opaque
;
129 Error
*local_error
= NULL
;
132 if (data
->size
> INT64_MAX
|| headerlen
> INT64_MAX
- data
->size
) {
137 /* User provided size should reflect amount of space made
138 * available to the guest, so we must take account of that
139 * which will be used by the crypto header
141 ret
= blk_truncate(data
->blk
, data
->size
+ headerlen
, false,
142 data
->prealloc
, 0, &local_error
);
150 /* Replace the error message with a better one */
151 error_free(local_error
);
152 error_setg(errp
, "The requested file size is too large");
154 error_propagate(errp
, local_error
);
161 static QemuOptsList block_crypto_runtime_opts_luks
= {
163 .head
= QTAILQ_HEAD_INITIALIZER(block_crypto_runtime_opts_luks
.head
),
165 BLOCK_CRYPTO_OPT_DEF_LUKS_KEY_SECRET(""),
166 { /* end of list */ }
171 static QemuOptsList block_crypto_create_opts_luks
= {
173 .head
= QTAILQ_HEAD_INITIALIZER(block_crypto_create_opts_luks
.head
),
176 .name
= BLOCK_OPT_SIZE
,
177 .type
= QEMU_OPT_SIZE
,
178 .help
= "Virtual disk size"
180 BLOCK_CRYPTO_OPT_DEF_LUKS_KEY_SECRET(""),
181 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG(""),
182 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE(""),
183 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG(""),
184 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG(""),
185 BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG(""),
186 BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME(""),
187 { /* end of list */ }
192 static QemuOptsList block_crypto_amend_opts_luks
= {
194 .head
= QTAILQ_HEAD_INITIALIZER(block_crypto_create_opts_luks
.head
),
196 BLOCK_CRYPTO_OPT_DEF_LUKS_STATE(""),
197 BLOCK_CRYPTO_OPT_DEF_LUKS_KEYSLOT(""),
198 BLOCK_CRYPTO_OPT_DEF_LUKS_OLD_SECRET(""),
199 BLOCK_CRYPTO_OPT_DEF_LUKS_NEW_SECRET(""),
200 BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME(""),
201 { /* end of list */ }
205 QCryptoBlockOpenOptions
*
206 block_crypto_open_opts_init(QDict
*opts
, Error
**errp
)
209 QCryptoBlockOpenOptions
*ret
;
211 v
= qobject_input_visitor_new_flat_confused(opts
, errp
);
216 visit_type_QCryptoBlockOpenOptions(v
, NULL
, &ret
, errp
);
223 QCryptoBlockCreateOptions
*
224 block_crypto_create_opts_init(QDict
*opts
, Error
**errp
)
227 QCryptoBlockCreateOptions
*ret
;
229 v
= qobject_input_visitor_new_flat_confused(opts
, errp
);
234 visit_type_QCryptoBlockCreateOptions(v
, NULL
, &ret
, errp
);
240 QCryptoBlockAmendOptions
*
241 block_crypto_amend_opts_init(QDict
*opts
, Error
**errp
)
244 QCryptoBlockAmendOptions
*ret
;
246 v
= qobject_input_visitor_new_flat_confused(opts
, errp
);
251 visit_type_QCryptoBlockAmendOptions(v
, NULL
, &ret
, errp
);
258 static int block_crypto_open_generic(QCryptoBlockFormat format
,
259 QemuOptsList
*opts_spec
,
260 BlockDriverState
*bs
,
265 BlockCrypto
*crypto
= bs
->opaque
;
266 QemuOpts
*opts
= NULL
;
268 QCryptoBlockOpenOptions
*open_opts
= NULL
;
269 unsigned int cflags
= 0;
270 QDict
*cryptoopts
= NULL
;
274 ret
= bdrv_open_file_child(NULL
, options
, "file", bs
, errp
);
279 GRAPH_RDLOCK_GUARD_MAINLOOP();
281 bs
->supported_write_flags
= BDRV_REQ_FUA
&
282 bs
->file
->bs
->supported_write_flags
;
284 opts
= qemu_opts_create(opts_spec
, NULL
, 0, &error_abort
);
285 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
290 cryptoopts
= qemu_opts_to_qdict(opts
, NULL
);
291 qdict_put_str(cryptoopts
, "format", QCryptoBlockFormat_str(format
));
293 open_opts
= block_crypto_open_opts_init(cryptoopts
, errp
);
299 if (flags
& BDRV_O_NO_IO
) {
300 cflags
|= QCRYPTO_BLOCK_OPEN_NO_IO
;
302 crypto
->block
= qcrypto_block_open(open_opts
, NULL
,
303 block_crypto_read_func
,
309 if (!crypto
->block
) {
314 bs
->encrypted
= true;
318 qobject_unref(cryptoopts
);
319 qapi_free_QCryptoBlockOpenOptions(open_opts
);
324 static int coroutine_fn GRAPH_UNLOCKED
325 block_crypto_co_create_generic(BlockDriverState
*bs
, int64_t size
,
326 QCryptoBlockCreateOptions
*opts
,
327 PreallocMode prealloc
, Error
**errp
)
331 QCryptoBlock
*crypto
= NULL
;
332 struct BlockCryptoCreateData data
;
334 blk
= blk_co_new_with_bs(bs
, BLK_PERM_WRITE
| BLK_PERM_RESIZE
, BLK_PERM_ALL
,
341 if (prealloc
== PREALLOC_MODE_METADATA
) {
342 prealloc
= PREALLOC_MODE_OFF
;
345 data
= (struct BlockCryptoCreateData
) {
348 .prealloc
= prealloc
,
351 crypto
= qcrypto_block_create(opts
, NULL
,
352 block_crypto_create_init_func
,
353 block_crypto_create_write_func
,
364 qcrypto_block_free(crypto
);
369 static int coroutine_fn GRAPH_RDLOCK
370 block_crypto_co_truncate(BlockDriverState
*bs
, int64_t offset
, bool exact
,
371 PreallocMode prealloc
, BdrvRequestFlags flags
,
374 BlockCrypto
*crypto
= bs
->opaque
;
375 uint64_t payload_offset
=
376 qcrypto_block_get_payload_offset(crypto
->block
);
378 if (payload_offset
> INT64_MAX
- offset
) {
379 error_setg(errp
, "The requested file size is too large");
383 offset
+= payload_offset
;
385 return bdrv_co_truncate(bs
->file
, offset
, exact
, prealloc
, 0, errp
);
388 static void block_crypto_close(BlockDriverState
*bs
)
390 BlockCrypto
*crypto
= bs
->opaque
;
391 qcrypto_block_free(crypto
->block
);
394 static int block_crypto_reopen_prepare(BDRVReopenState
*state
,
395 BlockReopenQueue
*queue
, Error
**errp
)
397 /* nothing needs checking */
402 * 1 MB bounce buffer gives good performance / memory tradeoff
403 * when using cache=none|directsync.
405 #define BLOCK_CRYPTO_MAX_IO_SIZE (1024 * 1024)
407 static int coroutine_fn GRAPH_RDLOCK
408 block_crypto_co_preadv(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
409 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
411 BlockCrypto
*crypto
= bs
->opaque
;
412 uint64_t cur_bytes
; /* number of bytes in current iteration */
413 uint64_t bytes_done
= 0;
414 uint8_t *cipher_data
= NULL
;
415 QEMUIOVector hd_qiov
;
417 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
418 uint64_t payload_offset
= qcrypto_block_get_payload_offset(crypto
->block
);
420 assert(payload_offset
< INT64_MAX
);
421 assert(QEMU_IS_ALIGNED(offset
, sector_size
));
422 assert(QEMU_IS_ALIGNED(bytes
, sector_size
));
424 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
426 /* Bounce buffer because we don't wish to expose cipher text
427 * in qiov which points to guest memory.
430 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_IO_SIZE
,
432 if (cipher_data
== NULL
) {
438 cur_bytes
= MIN(bytes
, BLOCK_CRYPTO_MAX_IO_SIZE
);
440 qemu_iovec_reset(&hd_qiov
);
441 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_bytes
);
443 ret
= bdrv_co_preadv(bs
->file
, payload_offset
+ offset
+ bytes_done
,
444 cur_bytes
, &hd_qiov
, 0);
449 if (qcrypto_block_decrypt(crypto
->block
, offset
+ bytes_done
,
450 cipher_data
, cur_bytes
, NULL
) < 0) {
455 qemu_iovec_from_buf(qiov
, bytes_done
, cipher_data
, cur_bytes
);
458 bytes_done
+= cur_bytes
;
462 qemu_iovec_destroy(&hd_qiov
);
463 qemu_vfree(cipher_data
);
469 static int coroutine_fn GRAPH_RDLOCK
470 block_crypto_co_pwritev(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
471 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
473 BlockCrypto
*crypto
= bs
->opaque
;
474 uint64_t cur_bytes
; /* number of bytes in current iteration */
475 uint64_t bytes_done
= 0;
476 uint8_t *cipher_data
= NULL
;
477 QEMUIOVector hd_qiov
;
479 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
480 uint64_t payload_offset
= qcrypto_block_get_payload_offset(crypto
->block
);
482 flags
&= ~BDRV_REQ_REGISTERED_BUF
;
484 assert(payload_offset
< INT64_MAX
);
485 assert(QEMU_IS_ALIGNED(offset
, sector_size
));
486 assert(QEMU_IS_ALIGNED(bytes
, sector_size
));
488 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
490 /* Bounce buffer because we're not permitted to touch
491 * contents of qiov - it points to guest memory.
494 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_IO_SIZE
,
496 if (cipher_data
== NULL
) {
502 cur_bytes
= MIN(bytes
, BLOCK_CRYPTO_MAX_IO_SIZE
);
504 qemu_iovec_to_buf(qiov
, bytes_done
, cipher_data
, cur_bytes
);
506 if (qcrypto_block_encrypt(crypto
->block
, offset
+ bytes_done
,
507 cipher_data
, cur_bytes
, NULL
) < 0) {
512 qemu_iovec_reset(&hd_qiov
);
513 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_bytes
);
515 ret
= bdrv_co_pwritev(bs
->file
, payload_offset
+ offset
+ bytes_done
,
516 cur_bytes
, &hd_qiov
, flags
);
522 bytes_done
+= cur_bytes
;
526 qemu_iovec_destroy(&hd_qiov
);
527 qemu_vfree(cipher_data
);
532 static void block_crypto_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
534 BlockCrypto
*crypto
= bs
->opaque
;
535 uint64_t sector_size
= qcrypto_block_get_sector_size(crypto
->block
);
536 bs
->bl
.request_alignment
= sector_size
; /* No sub-sector I/O */
540 static int64_t coroutine_fn GRAPH_RDLOCK
541 block_crypto_co_getlength(BlockDriverState
*bs
)
543 BlockCrypto
*crypto
= bs
->opaque
;
544 int64_t len
= bdrv_co_getlength(bs
->file
->bs
);
546 uint64_t offset
= qcrypto_block_get_payload_offset(crypto
->block
);
547 assert(offset
< INT64_MAX
);
559 static BlockMeasureInfo
*block_crypto_measure(QemuOpts
*opts
,
560 BlockDriverState
*in_bs
,
563 g_autoptr(QCryptoBlockCreateOptions
) create_opts
= NULL
;
564 Error
*local_err
= NULL
;
565 BlockMeasureInfo
*info
;
567 size_t luks_payload_size
;
571 * Preallocation mode doesn't affect size requirements but we must consume
574 g_free(qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
));
576 size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0);
579 int64_t ssize
= bdrv_getlength(in_bs
);
582 error_setg_errno(&local_err
, -ssize
,
583 "Unable to get image virtual_size");
590 cryptoopts
= qemu_opts_to_qdict_filtered(opts
, NULL
,
591 &block_crypto_create_opts_luks
, true);
592 qdict_put_str(cryptoopts
, "format", "luks");
593 create_opts
= block_crypto_create_opts_init(cryptoopts
, &local_err
);
594 qobject_unref(cryptoopts
);
599 if (!qcrypto_block_calculate_payload_offset(create_opts
, NULL
,
606 * Unallocated blocks are still encrypted so allocation status makes no
607 * difference to the file size.
609 info
= g_new0(BlockMeasureInfo
, 1);
610 info
->fully_allocated
= luks_payload_size
+ size
;
611 info
->required
= luks_payload_size
+ size
;
615 error_propagate(errp
, local_err
);
620 static int block_crypto_probe_luks(const uint8_t *buf
,
622 const char *filename
) {
623 return block_crypto_probe_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
624 buf
, buf_size
, filename
);
627 static int block_crypto_open_luks(BlockDriverState
*bs
,
632 return block_crypto_open_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
633 &block_crypto_runtime_opts_luks
,
634 bs
, options
, flags
, errp
);
637 static int coroutine_fn GRAPH_UNLOCKED
638 block_crypto_co_create_luks(BlockdevCreateOptions
*create_options
, Error
**errp
)
640 BlockdevCreateOptionsLUKS
*luks_opts
;
641 BlockDriverState
*bs
= NULL
;
642 QCryptoBlockCreateOptions create_opts
;
643 PreallocMode preallocation
= PREALLOC_MODE_OFF
;
646 assert(create_options
->driver
== BLOCKDEV_DRIVER_LUKS
);
647 luks_opts
= &create_options
->u
.luks
;
649 bs
= bdrv_co_open_blockdev_ref(luks_opts
->file
, errp
);
654 create_opts
= (QCryptoBlockCreateOptions
) {
655 .format
= Q_CRYPTO_BLOCK_FORMAT_LUKS
,
656 .u
.luks
= *qapi_BlockdevCreateOptionsLUKS_base(luks_opts
),
659 if (luks_opts
->has_preallocation
) {
660 preallocation
= luks_opts
->preallocation
;
663 ret
= block_crypto_co_create_generic(bs
, luks_opts
->size
, &create_opts
,
664 preallocation
, errp
);
675 static int coroutine_fn GRAPH_UNLOCKED
676 block_crypto_co_create_opts_luks(BlockDriver
*drv
, const char *filename
,
677 QemuOpts
*opts
, Error
**errp
)
679 QCryptoBlockCreateOptions
*create_opts
= NULL
;
680 BlockDriverState
*bs
= NULL
;
682 PreallocMode prealloc
;
686 Error
*local_err
= NULL
;
689 size
= qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0);
691 buf
= qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
);
692 prealloc
= qapi_enum_parse(&PreallocMode_lookup
, buf
,
693 PREALLOC_MODE_OFF
, &local_err
);
696 error_propagate(errp
, local_err
);
700 cryptoopts
= qemu_opts_to_qdict_filtered(opts
, NULL
,
701 &block_crypto_create_opts_luks
,
704 qdict_put_str(cryptoopts
, "format", "luks");
705 create_opts
= block_crypto_create_opts_init(cryptoopts
, errp
);
711 /* Create protocol layer */
712 ret
= bdrv_co_create_file(filename
, opts
, errp
);
717 bs
= bdrv_co_open(filename
, NULL
, NULL
,
718 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
, errp
);
724 /* Create format layer */
725 ret
= block_crypto_co_create_generic(bs
, size
, create_opts
, prealloc
, errp
);
733 * If an error occurred, delete 'filename'. Even if the file existed
734 * beforehand, it has been truncated and corrupted in the process.
737 bdrv_graph_co_rdlock();
738 bdrv_co_delete_file_noerr(bs
);
739 bdrv_graph_co_rdunlock();
743 qapi_free_QCryptoBlockCreateOptions(create_opts
);
744 qobject_unref(cryptoopts
);
748 static int coroutine_fn GRAPH_RDLOCK
749 block_crypto_co_get_info_luks(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
751 BlockDriverInfo subbdi
;
754 ret
= bdrv_co_get_info(bs
->file
->bs
, &subbdi
);
759 bdi
->cluster_size
= subbdi
.cluster_size
;
764 static ImageInfoSpecific
*
765 block_crypto_get_specific_info_luks(BlockDriverState
*bs
, Error
**errp
)
767 BlockCrypto
*crypto
= bs
->opaque
;
768 ImageInfoSpecific
*spec_info
;
769 QCryptoBlockInfo
*info
;
771 info
= qcrypto_block_get_info(crypto
->block
, errp
);
775 assert(info
->format
== Q_CRYPTO_BLOCK_FORMAT_LUKS
);
777 spec_info
= g_new(ImageInfoSpecific
, 1);
778 spec_info
->type
= IMAGE_INFO_SPECIFIC_KIND_LUKS
;
779 spec_info
->u
.luks
.data
= g_new(QCryptoBlockInfoLUKS
, 1);
780 *spec_info
->u
.luks
.data
= info
->u
.luks
;
782 /* Blank out pointers we've just stolen to avoid double free */
783 memset(&info
->u
.luks
, 0, sizeof(info
->u
.luks
));
785 qapi_free_QCryptoBlockInfo(info
);
790 static int GRAPH_RDLOCK
791 block_crypto_amend_prepare(BlockDriverState
*bs
, Error
**errp
)
793 BlockCrypto
*crypto
= bs
->opaque
;
796 /* apply for exclusive read/write permissions to the underlying file */
797 crypto
->updating_keys
= true;
798 ret
= bdrv_child_refresh_perms(bs
, bs
->file
, errp
);
800 /* Well, in this case we will not be updating any keys */
801 crypto
->updating_keys
= false;
806 static void GRAPH_RDLOCK
807 block_crypto_amend_cleanup(BlockDriverState
*bs
)
809 BlockCrypto
*crypto
= bs
->opaque
;
812 /* release exclusive read/write permissions to the underlying file */
813 crypto
->updating_keys
= false;
814 bdrv_child_refresh_perms(bs
, bs
->file
, &errp
);
817 error_report_err(errp
);
822 block_crypto_amend_options_generic_luks(BlockDriverState
*bs
,
823 QCryptoBlockAmendOptions
*amend_options
,
827 BlockCrypto
*crypto
= bs
->opaque
;
830 assert(crypto
->block
);
832 return qcrypto_block_amend_options(crypto
->block
,
833 block_crypto_read_func
,
834 block_crypto_write_func
,
841 static int GRAPH_RDLOCK
842 block_crypto_amend_options_luks(BlockDriverState
*bs
,
844 BlockDriverAmendStatusCB
*status_cb
,
849 BlockCrypto
*crypto
= bs
->opaque
;
850 QDict
*cryptoopts
= NULL
;
851 QCryptoBlockAmendOptions
*amend_options
= NULL
;
855 assert(crypto
->block
);
857 cryptoopts
= qemu_opts_to_qdict(opts
, NULL
);
858 qdict_put_str(cryptoopts
, "format", "luks");
859 amend_options
= block_crypto_amend_opts_init(cryptoopts
, errp
);
860 qobject_unref(cryptoopts
);
861 if (!amend_options
) {
865 ret
= block_crypto_amend_prepare(bs
, errp
);
869 ret
= block_crypto_amend_options_generic_luks(bs
, amend_options
,
873 block_crypto_amend_cleanup(bs
);
875 qapi_free_QCryptoBlockAmendOptions(amend_options
);
880 coroutine_fn
block_crypto_co_amend_luks(BlockDriverState
*bs
,
881 BlockdevAmendOptions
*opts
,
885 QCryptoBlockAmendOptions amend_opts
;
887 amend_opts
= (QCryptoBlockAmendOptions
) {
888 .format
= Q_CRYPTO_BLOCK_FORMAT_LUKS
,
889 .u
.luks
= *qapi_BlockdevAmendOptionsLUKS_base(&opts
->u
.luks
),
891 return block_crypto_amend_options_generic_luks(bs
, &amend_opts
,
896 block_crypto_child_perms(BlockDriverState
*bs
, BdrvChild
*c
,
897 const BdrvChildRole role
,
898 BlockReopenQueue
*reopen_queue
,
899 uint64_t perm
, uint64_t shared
,
900 uint64_t *nperm
, uint64_t *nshared
)
903 BlockCrypto
*crypto
= bs
->opaque
;
905 bdrv_default_perms(bs
, c
, role
, reopen_queue
, perm
, shared
, nperm
, nshared
);
908 * For backward compatibility, manually share the write
909 * and resize permission
911 *nshared
|= shared
& (BLK_PERM_WRITE
| BLK_PERM_RESIZE
);
913 * Since we are not fully a format driver, don't always request
914 * the read/resize permission but only when explicitly
917 *nperm
&= ~(BLK_PERM_WRITE
| BLK_PERM_RESIZE
);
918 *nperm
|= perm
& (BLK_PERM_WRITE
| BLK_PERM_RESIZE
);
921 * This driver doesn't modify LUKS metadata except
922 * when updating the encryption slots.
923 * Thus unlike a proper format driver we don't ask for
924 * shared write/read permission. However we need it
925 * when we are updating the keys, to ensure that only we
926 * have access to the device.
928 * Encryption update will set the crypto->updating_keys
929 * during that period and refresh permissions
932 if (crypto
->updating_keys
) {
933 /* need exclusive write access for header update */
934 *nperm
|= BLK_PERM_WRITE
;
935 /* unshare read and write permission */
936 *nshared
&= ~(BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE
);
941 static const char *const block_crypto_strong_runtime_opts
[] = {
942 BLOCK_CRYPTO_OPT_LUKS_KEY_SECRET
,
947 static BlockDriver bdrv_crypto_luks
= {
948 .format_name
= "luks",
949 .instance_size
= sizeof(BlockCrypto
),
950 .bdrv_probe
= block_crypto_probe_luks
,
951 .bdrv_open
= block_crypto_open_luks
,
952 .bdrv_close
= block_crypto_close
,
953 .bdrv_child_perm
= block_crypto_child_perms
,
954 .bdrv_co_create
= block_crypto_co_create_luks
,
955 .bdrv_co_create_opts
= block_crypto_co_create_opts_luks
,
956 .bdrv_co_truncate
= block_crypto_co_truncate
,
957 .create_opts
= &block_crypto_create_opts_luks
,
958 .amend_opts
= &block_crypto_amend_opts_luks
,
960 .bdrv_reopen_prepare
= block_crypto_reopen_prepare
,
961 .bdrv_refresh_limits
= block_crypto_refresh_limits
,
962 .bdrv_co_preadv
= block_crypto_co_preadv
,
963 .bdrv_co_pwritev
= block_crypto_co_pwritev
,
964 .bdrv_co_getlength
= block_crypto_co_getlength
,
965 .bdrv_measure
= block_crypto_measure
,
966 .bdrv_co_get_info
= block_crypto_co_get_info_luks
,
967 .bdrv_get_specific_info
= block_crypto_get_specific_info_luks
,
968 .bdrv_amend_options
= block_crypto_amend_options_luks
,
969 .bdrv_co_amend
= block_crypto_co_amend_luks
,
970 .bdrv_amend_pre_run
= block_crypto_amend_prepare
,
971 .bdrv_amend_clean
= block_crypto_amend_cleanup
,
975 .strong_runtime_opts
= block_crypto_strong_runtime_opts
,
978 static void block_crypto_init(void)
980 bdrv_register(&bdrv_crypto_luks
);
983 block_init(block_crypto_init
);