diff mbox series

[2/5] sed-opal: add helper for adding user authorities in ACE.

Message ID 20230322151604.401680-3-okozina@redhat.com (mailing list archive)
State New, archived
Headers show
Series sed-opal: add command to read locking range attributes | expand

Commit Message

Ondrej Kozina March 22, 2023, 3:16 p.m. UTC
Moves ACE construction away from add_user_to_lr routine
to be used later in added code.

Signed-off-by: Ondrej Kozina <okozina@redhat.com>
Tested-by: Luca Boccassi <bluca@debian.org>
Tested-by: Milan Broz <gmazyland@gmail.com>
---
 block/sed-opal.c | 64 +++++++++++++++++++++++++++++++++---------------
 1 file changed, 44 insertions(+), 20 deletions(-)

Comments

Christian Brauner March 29, 2023, 3:28 p.m. UTC | #1
On Wed, Mar 22, 2023 at 04:16:01PM +0100, Ondrej Kozina wrote:
> Moves ACE construction away from add_user_to_lr routine
> to be used later in added code.
> 
> Signed-off-by: Ondrej Kozina <okozina@redhat.com>
> Tested-by: Luca Boccassi <bluca@debian.org>
> Tested-by: Milan Broz <gmazyland@gmail.com>
> ---

Seems fine,
Acked-by: Christian Brauner <brauner@kernel.org>
Christoph Hellwig April 4, 2023, 3:25 p.m. UTC | #2
On Wed, Mar 22, 2023 at 04:16:01PM +0100, Ondrej Kozina wrote:
> Moves ACE construction away from add_user_to_lr routine
> to be used later in added code.
> 
> Signed-off-by: Ondrej Kozina <okozina@redhat.com>
> Tested-by: Luca Boccassi <bluca@debian.org>
> Tested-by: Milan Broz <gmazyland@gmail.com>
> ---
>  block/sed-opal.c | 64 +++++++++++++++++++++++++++++++++---------------
>  1 file changed, 44 insertions(+), 20 deletions(-)
> 
> diff --git a/block/sed-opal.c b/block/sed-opal.c
> index d86d3e5f5a44..2c3e38df9c65 100644
> --- a/block/sed-opal.c
> +++ b/block/sed-opal.c
> @@ -1759,25 +1759,16 @@ static int set_sid_cpin_pin(struct opal_dev *dev, void *data)
>  	return finalize_and_send(dev, parse_and_check_status);
>  }
>  
> -static int add_user_to_lr(struct opal_dev *dev, void *data)
> +static int set_lr_boolean_ace(struct opal_dev *dev, unsigned int opal_uid, u8 lr,
> +			      const u8 *users, size_t users_len)

Please avoid the overly long line here.

> +	for (u = 0; u < users_len; u++) {
> +		if (users[u] == OPAL_ADMIN1)
> +			memcpy(user_uid, opaluid[OPAL_ADMIN1_UID], OPAL_UID_LENGTH);
> +		else {
> +			memcpy(user_uid, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
> +			user_uid[7] = users[u];
> +		}
> +		add_token_u8(&err, dev, OPAL_STARTNAME);
> +		add_token_bytestring(&err, dev,
> +				     opaluid[OPAL_HALF_UID_AUTHORITY_OBJ_REF],
> +				     OPAL_UID_LENGTH/2);
> +		add_token_bytestring(&err, dev, user_uid, OPAL_UID_LENGTH);
> +		add_token_u8(&err, dev, OPAL_ENDNAME);

Please facto the logic for adding each user into a nice little helper,
which wil also avoid the overly long lines.

> +		if (u > 0) {
> +			add_token_u8(&err, dev, OPAL_STARTNAME);
> +			add_token_bytestring(&err, dev, opaluid[OPAL_HALF_UID_BOOLEAN_ACE],
> +					     OPAL_UID_LENGTH/2);
> +			add_token_u8(&err, dev, 1);
> +			add_token_u8(&err, dev, OPAL_ENDNAME);
> +		}

And this would also benefit from both a helper and a comment.
diff mbox series

Patch

diff --git a/block/sed-opal.c b/block/sed-opal.c
index d86d3e5f5a44..2c3e38df9c65 100644
--- a/block/sed-opal.c
+++ b/block/sed-opal.c
@@ -1759,25 +1759,16 @@  static int set_sid_cpin_pin(struct opal_dev *dev, void *data)
 	return finalize_and_send(dev, parse_and_check_status);
 }
 
-static int add_user_to_lr(struct opal_dev *dev, void *data)
+static int set_lr_boolean_ace(struct opal_dev *dev, unsigned int opal_uid, u8 lr,
+			      const u8 *users, size_t users_len)
 {
 	u8 lr_buffer[OPAL_UID_LENGTH];
 	u8 user_uid[OPAL_UID_LENGTH];
-	struct opal_lock_unlock *lkul = data;
+	u8 u;
 	int err;
 
-	memcpy(lr_buffer, opaluid[OPAL_LOCKINGRANGE_ACE_RDLOCKED],
-	       OPAL_UID_LENGTH);
-
-	if (lkul->l_state == OPAL_RW)
-		memcpy(lr_buffer, opaluid[OPAL_LOCKINGRANGE_ACE_WRLOCKED],
-		       OPAL_UID_LENGTH);
-
-	lr_buffer[7] = lkul->session.opal_key.lr;
-
-	memcpy(user_uid, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
-
-	user_uid[7] = lkul->session.who;
+	memcpy(lr_buffer, opaluid[opal_uid], OPAL_UID_LENGTH);
+	lr_buffer[7] = lr;
 
 	err = cmd_start(dev, lr_buffer, opalmethod[OPAL_SET]);
 
@@ -1790,19 +1781,52 @@  static int add_user_to_lr(struct opal_dev *dev, void *data)
 
 	add_token_u8(&err, dev, OPAL_STARTLIST);
 
+	for (u = 0; u < users_len; u++) {
+		if (users[u] == OPAL_ADMIN1)
+			memcpy(user_uid, opaluid[OPAL_ADMIN1_UID], OPAL_UID_LENGTH);
+		else {
+			memcpy(user_uid, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
+			user_uid[7] = users[u];
+		}
 
-	add_token_u8(&err, dev, OPAL_STARTNAME);
-	add_token_bytestring(&err, dev,
-			     opaluid[OPAL_HALF_UID_AUTHORITY_OBJ_REF],
-			     OPAL_UID_LENGTH/2);
-	add_token_bytestring(&err, dev, user_uid, OPAL_UID_LENGTH);
-	add_token_u8(&err, dev, OPAL_ENDNAME);
+		add_token_u8(&err, dev, OPAL_STARTNAME);
+		add_token_bytestring(&err, dev,
+				     opaluid[OPAL_HALF_UID_AUTHORITY_OBJ_REF],
+				     OPAL_UID_LENGTH/2);
+		add_token_bytestring(&err, dev, user_uid, OPAL_UID_LENGTH);
+		add_token_u8(&err, dev, OPAL_ENDNAME);
+
+		if (u > 0) {
+			add_token_u8(&err, dev, OPAL_STARTNAME);
+			add_token_bytestring(&err, dev, opaluid[OPAL_HALF_UID_BOOLEAN_ACE],
+					     OPAL_UID_LENGTH/2);
+			add_token_u8(&err, dev, 1);
+			add_token_u8(&err, dev, OPAL_ENDNAME);
+		}
+	}
 
 	add_token_u8(&err, dev, OPAL_ENDLIST);
 	add_token_u8(&err, dev, OPAL_ENDNAME);
 	add_token_u8(&err, dev, OPAL_ENDLIST);
 	add_token_u8(&err, dev, OPAL_ENDNAME);
 
+	return err;
+}
+
+static int add_user_to_lr(struct opal_dev *dev, void *data)
+{
+	int err;
+	struct opal_lock_unlock *lkul = data;
+	const u8 users[] = {
+		lkul->session.who
+	};
+
+	err = set_lr_boolean_ace(dev,
+				 lkul->l_state == OPAL_RW ?
+					OPAL_LOCKINGRANGE_ACE_WRLOCKED :
+					OPAL_LOCKINGRANGE_ACE_RDLOCKED,
+				 lkul->session.opal_key.lr, users,
+				 ARRAY_SIZE(users));
 	if (err) {
 		pr_debug("Error building add user to locking range command.\n");
 		return err;