Skip to content

Commit

Permalink
Opal: Add locking range support
Browse files Browse the repository at this point in the history
Change-Id: I4974d4134aed3b63e204b79c9292ce940e32d40c
Signed-off-by: Chunyang Hui <[email protected]>
Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/455175
Tested-by: SPDK CI Jenkins <[email protected]>
Reviewed-by: Changpeng Liu <[email protected]>
Reviewed-by: Shuhei Matsumoto <[email protected]>
  • Loading branch information
jessehui authored and Changpeng Liu committed Jul 5, 2019
1 parent 755b439 commit 505dbf5
Show file tree
Hide file tree
Showing 5 changed files with 491 additions and 30 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ Nvme Opal library spdk_opal_cmd deprecated. Adding seperate command APIs.
NVMe Opal library add support for activating locking SP which will make the transaction
from "Manufactured-Inactive" state to "Manufactured" state. Upon successfully invoking
of this method, lock and unlock features will be enabled.
NVMe Opal library add support for locking/unlocking range.


Added spdk_nvme_ctrlr_io_cmd_raw_no_payload_build() allowing a caller to pass
a completely formed command to an NVMe submission queue (buffer addresses and all).
Expand Down
93 changes: 91 additions & 2 deletions examples/nvme/nvme_manage/nvme_manage.c
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,8 @@ opal_usage(void)
printf("\n");
printf("\t[1: scan device]\n");
printf("\t[2: init - take ownership and activate locking]\n");
printf("\t[3: revert tper]\n");
printf("\t[3: lock/unlock the range]\n");
printf("\t[9: revert tper]\n");
printf("\t[0: quit]\n");
}

Expand Down Expand Up @@ -1009,6 +1010,91 @@ opal_init(struct dev *iter)
}
}

static void
opal_locking_usage(void)
{
printf("Choose Opal locking state:\n");
printf("\n");
printf("\t[1: read write lock]\n");
printf("\t[2: read only]\n");
printf("\t[3: read write unlock]\n");
}

static void
opal_lock_range(struct dev *iter)
{
char passwd[MAX_PASSWORD_SIZE] = {0};
char *passwd_p;
int ret;
int ch;
int state;
enum spdk_opal_lock_state state_flag;

if (spdk_nvme_ctrlr_get_flags(iter->ctrlr) & SPDK_NVME_CTRLR_SECURITY_SEND_RECV_SUPPORTED) {
iter->opal_dev = spdk_opal_init_dev(iter->ctrlr);
if (iter->opal_dev == NULL) {
return;
}
if (spdk_opal_supported(iter->opal_dev)) {
printf("Please input the password for locking the range:\n");
while ((ch = getchar()) != '\n' && ch != EOF);
passwd_p = getpass(passwd);
if (passwd_p) {
ret = spdk_opal_cmd_lock_unlock(iter->opal_dev, OPAL_ADMIN1, OPAL_READWRITE,
OPAL_LOCKING_RANGE_GLOBAL, passwd_p);
if (ret) {
printf("Unlock range failure: %d\n", ret);
return;
}

ret = spdk_opal_cmd_setup_locking_range(iter->opal_dev,
OPAL_ADMIN1, OPAL_LOCKING_RANGE_GLOBAL, 0, 0, passwd_p); /* just put here for testing */
if (ret) {
printf("Setup locking range failure: %d\n", ret);
return;
}

opal_locking_usage();
ret = scanf("%d", &state);
if (ret != 1) {
printf("Invalid input\n");
return;
}

switch (state) {
case 1:
state_flag = OPAL_RWLOCK;
break;
case 2:
state_flag = OPAL_READONLY;
break;
case 3:
state_flag = OPAL_READWRITE;
break;
default:
printf("Invalid options\n");
return;
}

ret = spdk_opal_cmd_lock_unlock(iter->opal_dev, OPAL_ADMIN1, state_flag,
OPAL_LOCKING_RANGE_GLOBAL, passwd_p);
if (ret) {
printf("lock range failure: %d\n", ret);
return;
}

printf("...\n...\nOpal setup locking range success\n");
} else {
printf("Input password invalid. Opal setup locking range failure\n");
}
}
spdk_opal_close(iter->opal_dev);
} else {
printf("%04x:%02x:%02x.%02x: NVMe Security Support/Receive Not supported.\nOpal Not Supported\n\n\n",
iter->pci_addr.domain, iter->pci_addr.bus, iter->pci_addr.dev, iter->pci_addr.func);
}
}

static void
opal_revert_tper(struct dev *iter)
{
Expand Down Expand Up @@ -1062,7 +1148,7 @@ test_opal(void)
while (!exit_flag) {
int cmd;
if (!scanf("%d", &cmd)) {
printf("Invalid Command: command must be number 0-2\n");
printf("Invalid Command: command must be number 0-9\n");
while (getchar() != '\n');
opal_usage();
continue;
Expand All @@ -1079,6 +1165,9 @@ test_opal(void)
opal_init(ctrlr); /* Take ownership, Activate Locking SP */
break;
case 3:
opal_lock_range(ctrlr);
break;
case 9:
opal_revert_tper(ctrlr);
break;

Expand Down
13 changes: 9 additions & 4 deletions include/spdk/opal.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,9 @@ struct spdk_opal_info {
};

enum spdk_opal_lock_state {
OPAL_LS_DISALBELOCKING = 0x00,
OPAL_LS_READLOCK_ENABLE = 0x01,
OPAL_LS_WRITELOCK_ENABLE = 0x02,
OPAL_LS_RWLOCK_ENABLE = 0x04,
OPAL_READONLY = 0x01,
OPAL_RWLOCK = 0x02,
OPAL_READWRITE = 0x04,
};

enum spdk_opal_user {
Expand Down Expand Up @@ -165,5 +164,11 @@ int spdk_opal_cmd_scan(struct spdk_opal_dev *dev);
int spdk_opal_cmd_take_ownership(struct spdk_opal_dev *dev, char *new_passwd);
int spdk_opal_cmd_revert_tper(struct spdk_opal_dev *dev, const char *passwd);
int spdk_opal_cmd_activate_locking_sp(struct spdk_opal_dev *dev, const char *passwd);
int spdk_opal_cmd_lock_unlock(struct spdk_opal_dev *dev, enum spdk_opal_user user,
enum spdk_opal_lock_state flag, enum spdk_opal_locking_range locking_range,
const char *passwd);
int spdk_opal_cmd_setup_locking_range(struct spdk_opal_dev *dev, enum spdk_opal_user user,
enum spdk_opal_locking_range locking_range_id, uint64_t range_start,
uint64_t range_length, const char *passwd);

#endif
Loading

0 comments on commit 505dbf5

Please sign in to comment.