Skip to content

Commit 1609ce2

Browse files
committed
sed: incorporate review comment on pull request
- Fix compile errors with older kernels - address all checkpatch error/warnings - correct typos Signed-off-by: Greg Joyce <gjoyce@linux.ibm.com>
1 parent 14f761a commit 1609ce2

8 files changed

+120
-114
lines changed

meson.build

+14
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,20 @@ conf.set10(
153153
cc.get_id() == 'clang',
154154
description: 'Is compiler warning about unused static line function?'
155155
)
156+
conf.set10(
157+
'HAVE_KEY_TYPE',
158+
cc.compiles(
159+
'''
160+
#include <linux/sed-opal.h>
161+
int main(void) {
162+
struct opal_key key;
163+
key.key_type = OPAL_INCLUDED;
164+
}
165+
''',
166+
name: 'key_type'
167+
),
168+
description: 'Does struct opal_key have a key_type field?'
169+
)
156170

157171
if cc.has_function_attribute('fallthrough')
158172
conf.set('fallthrough', '__attribute__((__fallthrough__))')

plugins/meson.build

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ if json_c_dep.found()
2626
'plugins/wdc/wdc-utils.c',
2727
'plugins/ymtc/ymtc-nvme.c',
2828
'plugins/zns/zns.c',
29-
'plugins/sed/sed.c',
3029
]
3130
subdir('solidigm')
3231
subdir('ocp')

plugins/sed/meson.build

-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ sources += [
22
'plugins/sed/sed.c',
33
'plugins/sed/sedopal_cmd.c',
44
]
5-

plugins/sed/sed.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,20 @@ OPT_ARGS(key_opts) = {
3232
OPT_ARGS(revert_opts) = {
3333
OPT_FLAG("destructive", 'e', &sedopal_destructive_revert,
3434
"destructive revert"),
35+
#ifdef IOC_OPAL_PSID_REVERT_TPR
3536
OPT_FLAG("psid", 'p', &sedopal_psid_revert, "PSID revert"),
37+
#endif
3638
OPT_END()
3739
};
3840

3941

4042
/*
4143
* Open the NVMe device specified on the command line. It must be the
42-
* NVMe block device (e.g. /deve/nvme0n1).
44+
* NVMe block device (e.g. /dev/nvme0n1).
4345
*/
4446
static int sed_opal_open_device(struct nvme_dev **dev, int argc, char **argv,
45-
const char *desc, struct argconfig_commandline_options *opts) {
47+
const char *desc, struct argconfig_commandline_options *opts)
48+
{
4649
int err;
4750

4851
err = parse_and_open(dev, argc, argv, desc, opts);
@@ -59,6 +62,7 @@ static int sed_opal_open_device(struct nvme_dev **dev, int argc, char **argv,
5962
return err;
6063
}
6164

65+
#ifdef IOC_OPAL_DISCOVERY
6266
static int sed_opal_discover(int argc, char **argv, struct command *cmd,
6367
struct plugin *plugin)
6468
{
@@ -75,6 +79,7 @@ static int sed_opal_discover(int argc, char **argv, struct command *cmd,
7579
dev_close(dev);
7680
return err;
7781
}
82+
#endif
7883

7984
static int sed_opal_initialize(int argc, char **argv, struct command *cmd,
8085
struct plugin *plugin)

plugins/sed/sed.h

+3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
#define CMD_INC_FILE plugins/sed/sed
44

55
#include "cmd.h"
6+
#include <linux/sed-opal.h>
67

78
PLUGIN(NAME("sed", "SED Opal Command Set", NVME_VERSION),
89
COMMAND_LIST(
10+
#ifdef IOC_OPAL_DISCOVERY
911
ENTRY("discover", "Discover SED Opal Locking Features", sed_opal_discover, "1")
12+
#endif
1013
ENTRY("initialize", "Initialize a SED Opal Device for locking", sed_opal_initialize)
1114
ENTRY("revert", "Revert a SED Opal Device from locking", sed_opal_revert)
1215
ENTRY("lock", "Lock a SED Opal Device", sed_opal_lock)

plugins/sed/sedopal_cmd.c

+66-46
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,4 @@
1-
/*
2-
* Copyright (C) 2023, 2024 IBM Corporation
3-
*
4-
* This program is free software; you can redistribute it and/or
5-
* modify it under the terms of the GNU General Public License
6-
* as published by the Free Software Foundation; either version 2
7-
* of the License, or (at your option) any later version.
8-
*
9-
* This program is distributed in the hope that it will be useful,
10-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12-
* GNU General Public License for more details.
13-
*
14-
* You should have received a copy of the GNU General Public License
15-
* along with this program; if not, write to the Free Software
16-
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17-
* USA.
18-
*/
1+
// SPDX-License-Identifier: GPL-2.0-or-later
192

203
#include <ctype.h>
214
#include <stdio.h>
@@ -34,22 +17,22 @@
3417
/*
3518
* ask user for key rather than obtaining it from kernel keyring
3619
*/
37-
bool sedopal_ask_key = false;
20+
bool sedopal_ask_key;
3821

3922
/*
4023
* initiate dialog to ask for and confirm new password
4124
*/
42-
bool sedopal_ask_new_key = false;
25+
bool sedopal_ask_new_key;
4326

4427
/*
4528
* perform a destructive drive revert
4629
*/
47-
bool sedopal_destructive_revert = false;
30+
bool sedopal_destructive_revert;
4831

4932
/*
5033
* perform a PSID drive revert
5134
*/
52-
bool sedopal_psid_revert = false;
35+
bool sedopal_psid_revert;
5336

5437
/*
5538
* Map method status codes to error text
@@ -94,7 +77,8 @@ const char *sedopal_error_to_text(int code)
9477
/*
9578
* Read a user entered password and do some basic validity checks.
9679
*/
97-
char * sedopal_get_password(char * prompt) {
80+
char *sedopal_get_password(char *prompt)
81+
{
9882
char *pass;
9983
int len;
10084

@@ -117,7 +101,14 @@ char * sedopal_get_password(char * prompt) {
117101
* key should be looked up in the kernel keyring, or it should be
118102
* populated in the key by prompting the user.
119103
*/
120-
int sedopal_set_key(struct opal_key *key) {
104+
int sedopal_set_key(struct opal_key *key)
105+
{
106+
#ifndef OPAL_KEY_TYPE
107+
/*
108+
* If key_type isn't avaialable, force key prompt
109+
*/
110+
sedopal_ask_key = true;
111+
#endif
121112

122113
if (sedopal_ask_key) {
123114
char *pass;
@@ -139,7 +130,9 @@ int sedopal_set_key(struct opal_key *key) {
139130
if (pass == NULL)
140131
return -EINVAL;
141132

133+
#if HAVE_KEY_TYPE
142134
key->key_type = OPAL_INCLUDED;
135+
#endif
143136
key->key_len = strlen(pass);
144137
memcpy(key->key, pass, key->key_len);
145138

@@ -156,8 +149,10 @@ int sedopal_set_key(struct opal_key *key) {
156149
}
157150
}
158151
} else {
159-
key->key_type = OPAL_KEYRING;
160-
key->key_len = 0;
152+
#if HAVE_KEY_TYPE
153+
key->key_type = OPAL_KEYRING;
154+
#endif
155+
key->key_len = 0;
161156
}
162157

163158
key->lr = 0;
@@ -168,7 +163,8 @@ int sedopal_set_key(struct opal_key *key) {
168163
/*
169164
* Prepare a drive for SED Opal locking.
170165
*/
171-
int sedopal_cmd_initialize(int fd) {
166+
int sedopal_cmd_initialize(int fd)
167+
{
172168
int rc;
173169
struct opal_key key;
174170
struct opal_lr_act lr_act = {};
@@ -227,25 +223,28 @@ int sedopal_cmd_initialize(int fd) {
227223
/*
228224
* Lock a SED Opal drive
229225
*/
230-
int sedopal_cmd_lock(int fd) {
226+
int sedopal_cmd_lock(int fd)
227+
{
231228

232229
return sedopal_lock_unlock(fd, OPAL_LK);
233230
}
234231

235232
/*
236233
* Unlock a SED Opal drive
237234
*/
238-
int sedopal_cmd_unlock(int fd) {
235+
int sedopal_cmd_unlock(int fd)
236+
{
239237

240238
return sedopal_lock_unlock(fd, OPAL_RW);
241239
}
242240

243241
/*
244242
* Prepare and issue an ioctl to lock/unlock a drive
245243
*/
246-
int sedopal_lock_unlock(int fd, int lock_state) {
244+
int sedopal_lock_unlock(int fd, int lock_state)
245+
{
247246
int rc;
248-
struct opal_lock_unlock opal_lu;
247+
struct opal_lock_unlock opal_lu = {};
249248

250249
rc = sedopal_set_key(&opal_lu.session.opal_key);
251250
if (rc != 0)
@@ -254,16 +253,15 @@ int sedopal_lock_unlock(int fd, int lock_state) {
254253
opal_lu.session.sum = 0;
255254
opal_lu.session.who = OPAL_ADMIN1;
256255
opal_lu.l_state = lock_state;
257-
opal_lu.flags = 0;
258256

259257
rc = ioctl(fd, IOC_OPAL_LOCK_UNLOCK, &opal_lu);
260258
if (rc != 0)
261259
fprintf(stderr,
262260
"Error: failed locking or unlocking - %d\n", rc);
263261

264262
/*
265-
* If the unlock was successful, force a re-read of the
266-
* partion table.
263+
* If the unlock was successful, force a re-read of the
264+
* partition table.
267265
*/
268266
if (rc == 0) {
269267
rc = ioctl(fd, BLKRRPART, 0);
@@ -278,7 +276,8 @@ int sedopal_lock_unlock(int fd, int lock_state) {
278276
/*
279277
* Confirm a destructive drive so that data is inadvertently erased
280278
*/
281-
static bool sedopal_confirm_revert() {
279+
static bool sedopal_confirm_revert(void)
280+
{
282281
int rc;
283282
char ans;
284283
bool confirmed = false;
@@ -302,7 +301,8 @@ static bool sedopal_confirm_revert() {
302301
/*
303302
* perform a destructive drive revert
304303
*/
305-
static int sedopal_revert_destructive(int fd) {
304+
static int sedopal_revert_destructive(int fd)
305+
{
306306
struct opal_key key;
307307
int rc;
308308

@@ -323,10 +323,12 @@ static int sedopal_revert_destructive(int fd) {
323323
return rc;
324324
}
325325

326+
#ifdef IOC_OPAL_PSID_REVERT_TPR
326327
/*
327328
* perform a PSID drive revert
328329
*/
329-
static int sedopal_revert_psid(int fd) {
330+
static int sedopal_revert_psid(int fd)
331+
{
330332
struct opal_key key;
331333
int rc;
332334

@@ -344,33 +346,44 @@ static int sedopal_revert_psid(int fd) {
344346

345347
return rc;
346348
}
349+
#endif /* IOC_OPAL_PSID_REVERT_TPR */
347350

348351
/*
349352
* revert a drive from the provisioned state to a state where locking
350353
* is disabled.
351354
*/
352-
int sedopal_cmd_revert(int fd) {
355+
int sedopal_cmd_revert(int fd)
356+
{
353357
int rc;
354-
struct opal_revert_lsp revert_lsp;
355358

356359
/*
357360
* for revert, require that key/PSID is provided
358361
*/
359362
sedopal_ask_key = true;
360363

361364
if (sedopal_psid_revert) {
365+
#ifdef IOC_OPAL_PSID_REVERT_TPR
362366
rc = sedopal_revert_psid(fd);
367+
#else
368+
rc = -EOPNOTSUPP;
369+
#endif
363370
} else if (sedopal_destructive_revert) {
364371
rc = sedopal_revert_destructive(fd);
365372
} else {
373+
#ifdef IOC_OPAL_REVERT_LSP
374+
struct opal_revert_lsp revert_lsp;
375+
366376
rc = sedopal_set_key(&revert_lsp.key);
367377
if (rc != 0)
368378
return rc;
369379

370380
revert_lsp.options = OPAL_PRESERVE;
371-
revert_lsp.__pad = 0;
381+
revert_lsp.__pad = 0;
372382

373383
rc = ioctl(fd, IOC_OPAL_REVERT_LSP, &revert_lsp);
384+
#else
385+
rc = -EOPNOTSUPP;
386+
#endif
374387
}
375388

376389
if (rc != 0)
@@ -383,7 +396,8 @@ int sedopal_cmd_revert(int fd) {
383396
* Change the password of a drive. The existing password must be
384397
* provided and the new password is confirmed by re-entry.
385398
*/
386-
int sedopal_cmd_password(int fd) {
399+
int sedopal_cmd_password(int fd)
400+
{
387401
int rc;
388402
struct opal_new_pw new_pw = {};
389403

@@ -417,7 +431,8 @@ int sedopal_cmd_password(int fd) {
417431
/*
418432
* Print the state of locking features.
419433
*/
420-
void sedopal_print_locking_features(uint8_t features) {
434+
void sedopal_print_locking_features(uint8_t features)
435+
{
421436
printf("Locking Features:\n");
422437
printf("\tLocking Supported: %s\n",
423438
(features & OPAL_FEATURE_LOCKING_SUPPORTED) ? "Yes" : "No");
@@ -428,10 +443,12 @@ void sedopal_print_locking_features(uint8_t features) {
428443
}
429444

430445
/*
431-
* Query a drive to determine if it's SED Opal capabled and
446+
* Query a drive to determine if it's SED Opal capable and
432447
* it's current locking status.
433448
*/
434-
int sedopal_cmd_discover(int fd) {
449+
int sedopal_cmd_discover(int fd)
450+
{
451+
#ifdef IOC_OPAL_DISCOVERY
435452
int rc;
436453
bool sedopal_locking_supported = false;
437454
struct opal_discovery discover;
@@ -442,7 +459,7 @@ int sedopal_cmd_discover(int fd) {
442459
uint8_t locking_flags;
443460
char buf[4096];
444461

445-
discover.data = (__u64)buf;
462+
discover.data = (__u64)buf;
446463
discover.size = sizeof(buf);
447464

448465
rc = ioctl(fd, IOC_OPAL_DISCOVERY, &discover);
@@ -467,7 +484,7 @@ int sedopal_cmd_discover(int fd) {
467484
*/
468485
while (feat < feat_end) {
469486
code = be16toh(feat->code);
470-
switch(code) {
487+
switch (code) {
471488
case OPAL_FEATURE_CODE_LOCKING:
472489
locking_flags = feat->feature;
473490
break;
@@ -489,4 +506,7 @@ int sedopal_cmd_discover(int fd) {
489506
sedopal_print_locking_features(locking_flags);
490507

491508
return rc;
509+
#else /* IOC_OPAL_DISCOVERY */
510+
return -EOPNOTSUPP;
511+
#endif
492512
}

0 commit comments

Comments
 (0)