Skip to content

Commit d01d6a8

Browse files
author
Keith Busch
committed
nvme-cli: Enhance format FNA detection
Always check the format attributes to verify if the controller will apply the format to all namespaces, or if they can be individually formatted. This is used to properly warn of the implications from sending this command. Signed-off-by: Keith Busch <[email protected]>
1 parent f7cc369 commit d01d6a8

1 file changed

Lines changed: 28 additions & 23 deletions

File tree

nvme.c

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3580,6 +3580,7 @@ static int format(int argc, char **argv, struct command *cmd, struct plugin *plu
35803580
const char *bs = "target block size";
35813581
const char *force = "The \"I know what I'm doing\" flag, skip confirmation before sending command";
35823582
struct nvme_id_ns ns;
3583+
struct nvme_id_ctrl ctrl;
35833584
int err, fd, i;
35843585
__u8 prev_lbaf = 0;
35853586
__u8 lbads = 0;
@@ -3644,34 +3645,37 @@ static int format(int argc, char **argv, struct command *cmd, struct plugin *plu
36443645
}
36453646
}
36463647

3647-
if (S_ISBLK(nvme_stat.st_mode)) {
3648+
memset(&ctrl, 0, sizeof (struct nvme_id_ctrl));
3649+
err = nvme_identify_ctrl(fd, &ctrl);
3650+
if (err) {
3651+
perror("identify-ctrl");
3652+
goto close_fd;
3653+
}
3654+
3655+
if ((ctrl.fna & 1) == 1) {
3656+
/*
3657+
* FNA bit 0 set to 1: all namespaces ... shall be configured with the same
3658+
* attributes and a format (excluding secure erase) of any namespace results in a
3659+
* format of all namespaces.
3660+
*/
3661+
cfg.namespace_id = NVME_NSID_ALL;
3662+
} else if (S_ISBLK(nvme_stat.st_mode)) {
36483663
cfg.namespace_id = get_nsid(fd);
36493664
if (cfg.namespace_id == 0) {
36503665
err = -EINVAL;
36513666
goto close_fd;
36523667
}
3653-
} else if (cfg.namespace_id == 0) {
3654-
struct nvme_id_ctrl ctrl;
3668+
}
36553669

3656-
memset(&ctrl, 0, sizeof (struct nvme_id_ctrl));
3657-
err = nvme_identify_ctrl(fd, &ctrl);
3658-
if (err) {
3659-
perror("identify-ctrl");
3660-
goto close_fd;
3661-
}
3662-
if ((ctrl.fna & 1) == 1) {
3663-
/*
3664-
* FNA bit 0 set to 1: all namespaces ... shall be configured with the same
3665-
* attributes and a format (excluding secure erase) of any namespace results in a
3666-
* format of all namespaces.
3667-
*/
3668-
cfg.namespace_id = NVME_NSID_ALL;
3669-
} else {
3670-
fprintf(stderr, "Invalid namespace ID, specify a namespace to format or use '-n 0xffffffff' to format all namespaces on this controller.\n");
3671-
err = -EINVAL;
3672-
goto close_fd;
3673-
}
3670+
if (cfg.namespace_id == 0) {
3671+
fprintf(stderr,
3672+
"Invalid namespace ID, "
3673+
"specify a namespace to format or use '-n 0xffffffff' "
3674+
"to format all namespaces on this controller.\n");
3675+
err = -EINVAL;
3676+
goto close_fd;
36743677
}
3678+
36753679
if (cfg.namespace_id != NVME_NSID_ALL) {
36763680
err = nvme_identify_ns(fd, cfg.namespace_id, 0, &ns);
36773681
if (err) {
@@ -3734,8 +3738,9 @@ static int format(int argc, char **argv, struct command *cmd, struct plugin *plu
37343738
}
37353739

37363740
if (!cfg.force) {
3737-
fprintf(stderr, "You are about to format %s, namespace %#x.\n",
3738-
devicename, cfg.namespace_id);
3741+
fprintf(stderr, "You are about to format %s, namespace %#x%s.\n",
3742+
devicename, cfg.namespace_id,
3743+
cfg.namespace_id == NVME_NSID_ALL ? "(ALL namespaces)" : "");
37393744
print_relatives();
37403745
fprintf(stderr, "WARNING: Format may irrevocably delete this device's data.\n"
37413746
"You have 10 seconds to press Ctrl-C to cancel this operation.\n\n"

0 commit comments

Comments
 (0)