From nobody Wed Nov 27 04:57:21 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1701706245; cv=none; d=zohomail.com; s=zohoarc; b=NH7eHp0L2Z2+7pO/wV/gwfJr37dd2TGXkURc/1CUnJtGoluaDDZYf69M9+iap4Hc7qtE8JJwbao2bhexOulV3Gn+lCB9IYtNLdy5EVkoCokMjdoHGgXHbWbGL2dMTw8DkS2bD2S/ejsTVI/TYCZmJls/c7KYStlVz+wluZmZaLg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1701706245; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=curXvDp0ioN47tzvE2UhnUysHr4g1/8N1rFgo2OTs0o=; b=QoN+W3TH6UTqzaJaEX3h3CEVuhIY+l9yqJTp20MJFSgjQp2n3AGh+SxMTLtGvp44iYyRDdF8As6Wt8sBb2PxHy4W0ScwhFdq6CbTmoJDJgnyXaA1j7l4Kj4bOm4GKxZVhxMa2Mm1zbGj/Mj35KqTt6uzlp28oybQxEkj+/UGxBs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1701706245176752.421351795851; Mon, 4 Dec 2023 08:10:45 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rABVP-0006uA-3o; Mon, 04 Dec 2023 11:09:11 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rABVN-0006tj-E3 for qemu-devel@nongnu.org; Mon, 04 Dec 2023 11:09:09 -0500 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rABVL-0005JB-JT for qemu-devel@nongnu.org; Mon, 04 Dec 2023 11:09:09 -0500 Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-6ce3534bf44so1281469b3a.1 for ; Mon, 04 Dec 2023 08:07:07 -0800 (PST) Received: from localhost.localdomain ([125.71.95.66]) by smtp.gmail.com with ESMTPSA id m17-20020aa78a11000000b006be4bb0d2dcsm7993064pfa.149.2023.12.04.08.07.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 08:07:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=smartx-com.20230601.gappssmtp.com; s=20230601; t=1701706026; x=1702310826; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=curXvDp0ioN47tzvE2UhnUysHr4g1/8N1rFgo2OTs0o=; b=nZVR3CrGUjiZHaArqr/Un5P9d0LejVZJanFS3zsA0Qox6gTcOad5UVG2R1LUj3dEvj wQZFDkMPo2VCYH6ANULh097lT5okIJfi7vCFkrI95wDA0IttX8uKqzW2bQg0zGM0iXl+ 92dqEBkfl1kswHWb1kymu7QVqQTLS1JoKjQ6rMqCozeyj/vunc3wIH5v7Y5+iwD/CQR+ BXlPcv/yUnmikKkIFX55jAex6fE6Ph1dlDTRl48vyTqF5IGygFBqNmYwSOng9y5ZFuJ6 KW7zo5GbnTFALzr9xAu1FEIRW2ySw+HVkJFALaldUF8ixZ34x03dKSXLkkd/CnblNp0v VtBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701706026; x=1702310826; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=curXvDp0ioN47tzvE2UhnUysHr4g1/8N1rFgo2OTs0o=; b=TPkSiJ8W/Gd3rc60dVlycr2mkObuu0kWt/m6fA29jtX1Z7qoU9KyYUpU/Zy1DTA3yg a5HLMsts8FXo4f5tlfQpz2ik45UkAJ2FjkVayD069DTm3P4Bc8MgbXuDhzlAUVmmGLig FHZZvLlVdCSbo8EI6LwZlcqDpd7b7eoZLLsPSu8Q3pdvQyHReAE0xuoDv7/t5jMutm3d +SPWDvThVoELN7ezXqHV5SMkMRj+AjKijv3EKkZklsm+ktwkESmSTas702WFx/ndDnD4 tM4aDmK6QE/P/o65oRWqTiuUAfbSv78lznn908/mcVK/etq8uwlEnuz2fosHv9HAFBFU h/Jg== X-Gm-Message-State: AOJu0YzjooYLhmr60AhnEOknObIB0wCI0nI6tTdSCY9OWSfnNZeBLXuv x7PaM63td2WkxwXmdM2ZJhGEnKN+e22YWxuKVbeXFUly X-Google-Smtp-Source: AGHT+IGt4SNvnbcsvJibYv0gG/z0Pbgh7veSbdcdt4YsJ5Yapgp4n0b3xKIKzyTEEks9cfTC2n0eBA== X-Received: by 2002:a05:6a20:3ca6:b0:18b:e5e6:576c with SMTP id b38-20020a056a203ca600b0018be5e6576cmr5046995pzj.3.1701706025666; Mon, 04 Dec 2023 08:07:05 -0800 (PST) From: Hyman Huang To: qemu-devel@nongnu.org Cc: Kevin Wolf , Hanna Reitz , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Eric Blake , Markus Armbruster , yong.huang@smartx.com Subject: [RFC 7/8] Gluks: Implement the fundamental block layer driver hooks Date: Tue, 5 Dec 2023 00:06:24 +0800 Message-Id: X-Mailer: git-send-email 2.39.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=2607:f8b0:4864:20::432; envelope-from=yong.huang@smartx.com; helo=mail-pf1-x432.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @smartx-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1701706246153100007 Content-Type: text/plain; charset="utf-8" Signed-off-by: Hyman Huang --- block/generic-luks.c | 104 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 102 insertions(+), 2 deletions(-) diff --git a/block/generic-luks.c b/block/generic-luks.c index ebc0365d40..32cbedc86f 100644 --- a/block/generic-luks.c +++ b/block/generic-luks.c @@ -23,8 +23,14 @@ #include "qemu/osdep.h" =20 #include "block/block_int.h" +#include "block/block-io.h" #include "block/crypto.h" +#include "block/qdict.h" #include "crypto/block.h" +#include "qapi/error.h" +#include "qemu/error-report.h" +#include "qemu/module.h" +#include "qemu/option.h" =20 #include "generic-luks.h" =20 @@ -50,10 +56,89 @@ static QemuOptsList gluks_create_opts_luks =3D { }, }; =20 +static int gluks_read_func(QCryptoBlock *block, + size_t offset, + uint8_t *buf, + size_t buflen, + void *opaque, + Error **errp) +{ + + BlockDriverState *bs =3D opaque; + BDRVGLUKSState *s =3D bs->opaque; + ssize_t ret; + + GLOBAL_STATE_CODE(); + GRAPH_RDLOCK_GUARD_MAINLOOP(); + + ret =3D bdrv_pread(s->header, offset, buflen, buf, 0); + if (ret < 0) { + error_setg_errno(errp, -ret, "Could not read generic luks header"); + return ret; + } + return 0; +} + static int gluks_open(BlockDriverState *bs, QDict *options, int flags, Error **errp) { - return 0; + BDRVGLUKSState *s =3D bs->opaque; + QemuOpts *opts =3D NULL; + QCryptoBlockOpenOptions *open_opts =3D NULL; + QDict *cryptoopts =3D NULL; + unsigned int cflags =3D 0; + int ret; + + GLOBAL_STATE_CODE(); + + if (!bdrv_open_child(NULL, options, "file", bs, &child_of_bds, + (BDRV_CHILD_DATA | BDRV_CHILD_PRIMARY), false, er= rp)) { + return -EINVAL; + } + s->header =3D bdrv_open_child(NULL, options, "header", bs, + &child_of_bds, BDRV_CHILD_METADATA, false, + errp); + if (!s->header) { + return -EINVAL; + } + + GRAPH_RDLOCK_GUARD_MAINLOOP(); + + opts =3D qemu_opts_create(&block_crypto_runtime_opts_luks, + NULL, 0, &error_abort); + if (!qemu_opts_absorb_qdict(opts, options, errp)) { + ret =3D -EINVAL; + goto cleanup; + } + + cryptoopts =3D qemu_opts_to_qdict(opts, NULL); + qdict_put_str(cryptoopts, "format", + QCryptoBlockFormat_str(Q_CRYPTO_BLOCK_FORMAT_GLUKS)); + + open_opts =3D block_crypto_open_opts_init(cryptoopts, errp); + if (!open_opts) { + goto cleanup; + } + + s->crypto.block =3D qcrypto_block_open(open_opts, NULL, + gluks_read_func, + bs, + cflags, + 1, + errp); + if (!s->crypto.block) { + ret =3D -EIO; + goto cleanup; + } + + s->header_size =3D qcrypto_block_get_payload_offset(s->crypto.block); + qcrypto_block_set_payload_offset(s->crypto.block, 0); + + ret =3D 0; + cleanup: + qobject_unref(cryptoopts); + qapi_free_QCryptoBlockOpenOptions(open_opts); + return ret; } =20 static int coroutine_fn GRAPH_UNLOCKED @@ -70,13 +155,24 @@ gluks_child_perms(BlockDriverState *bs, BdrvChild *c, uint64_t perm, uint64_t shared, uint64_t *nperm, uint64_t *nshared) { + if (role & BDRV_CHILD_METADATA) { + /* assign read permission only */ + perm |=3D BLK_PERM_CONSISTENT_READ; + /* share all permissions */ + shared |=3D BLK_PERM_ALL; =20 + *nperm =3D perm; + *nshared =3D shared; + return; + } + + bdrv_default_perms(bs, c, role, reopen_queue, perm, shared, nperm, nsh= ared); } =20 static int64_t coroutine_fn GRAPH_RDLOCK gluks_co_getlength(BlockDriverState *bs) { - return 0; + return bdrv_co_getlength(bs->file->bs); } =20 static BlockDriver bdrv_generic_luks =3D { @@ -87,8 +183,12 @@ static BlockDriver bdrv_generic_luks =3D { .bdrv_child_perm =3D gluks_child_perms, .bdrv_co_getlength =3D gluks_co_getlength, =20 + .bdrv_close =3D block_crypto_close, + .bdrv_co_preadv =3D block_crypto_co_preadv, + .bdrv_co_pwritev =3D block_crypto_co_pwritev, .create_opts =3D &gluks_create_opts_luks, .amend_opts =3D &block_crypto_amend_opts_luks, + .is_format =3D false, }; =20 static void block_generic_luks_init(void) --=20 2.39.1