Skip to content

Commit

Permalink
create-ns: align the namespaces to 1Mib boundaries when using SI suff…
Browse files Browse the repository at this point in the history
…ixes

Some controllers refuse to create namespaces with a size not aligned to
a 1 megabyte boundary, this happens when using the -S and -C options.

$ nvme create-ns /dev/nvme0 -S 80M -C 80M -f 0
NVMe status: Invalid Field in Command: A reserved coded value or
an unsupported value in a defined field(0x2)

Fix this problem by modifying create_ns() parse_lba_num_si() to align
the values to 1 Megabyte boundaries. If granularity is supported, we will
use the speciefied values instead of 1Mb.

Signed-off-by: Maurizio Lombardi <[email protected]>
  • Loading branch information
maurizio-lombardi committed Dec 12, 2023
1 parent 10b742e commit e68e99b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Documentation/nvme-create-ns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ OPTIONS

-S::
--nsze-si::
The namespace size (NSZE) in standard SI units.
The namespace size (NSZE) in standard SI units (aligned on 1Mib boundaries).
The value SI suffixed is divided by the namespace LBA size to set as NSZE.
If the value not suffixed it is set as same with the nsze option.

-C::
--ncap-si::
The namespace capacity (NCAP) in standard SI units.
The namespace capacity (NCAP) in standard SI units (aligned on 1Mib boundaries).
The value SI suffixed is divided by the namespace LBA size to set as NCAP.
If the value not suffixed it is set as same with the ncap option.

Expand Down
49 changes: 45 additions & 4 deletions nvme.c
Original file line number Diff line number Diff line change
Expand Up @@ -2833,12 +2833,13 @@ static int detach_ns(int argc, char **argv, struct command *cmd, struct plugin *
}

static int parse_lba_num_si(struct nvme_dev *dev, const char *opt,
const char *val, __u8 flbas, __u64 *num)
const char *val, __u8 flbas, __u64 *num, __u32 align)
{
_cleanup_free_ struct nvme_ns_list *ns_list = NULL;
_cleanup_free_ struct nvme_id_ctrl *ctrl = NULL;
_cleanup_free_ struct nvme_id_ns *ns = NULL;
__u32 nsid = 1;
unsigned int remainder;
char *endptr;
int err = -EINVAL;
int i;
Expand Down Expand Up @@ -2916,6 +2917,12 @@ static int parse_lba_num_si(struct nvme_dev *dev, const char *opt,
return -errno;
}

if (endptr[0]) {
remainder = *num % align;
if (remainder)
*num += align - remainder;
}

if (endptr[0] != '\0')
*num /= lbas;

Expand Down Expand Up @@ -2955,9 +2962,11 @@ static int create_ns(int argc, char **argv, struct command *cmd, struct plugin *
_cleanup_free_ struct nvme_id_ns *ns = NULL;
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;
int err = 0, i;
__u32 nsid;
__u32 nsid, align_nsze, align_ncap;
uint16_t num_phandle;
uint16_t phndl[128] = { 0, };
struct nvme_id_ctrl *id;
struct nvme_id_ns_granularity_list *gr_list;

struct config {
__u64 nsze;
Expand Down Expand Up @@ -3075,11 +3084,43 @@ static int create_ns(int argc, char **argv, struct command *cmd, struct plugin *
return -EINVAL;
}

err = parse_lba_num_si(dev, "nsze", cfg.nsze_si, cfg.flbas, &cfg.nsze);
id = nvme_alloc(sizeof(*id));
if (!id)
return -ENOMEM;

err = nvme_identify_ctrl(dev_fd(dev), id);
if (err) {
if (err < 0) {
nvme_show_error("identify-controller: %s", nvme_strerror(errno));
} else {
fprintf(stderr, "identify controller failed\n");
nvme_show_status(err);
}
return err;
}

align_nsze = align_ncap = 1 << 20; /* Default 1 Mb */
if (id->ctratt & NVME_CTRL_CTRATT_NAMESPACE_GRANULARITY) {
gr_list = nvme_alloc(sizeof(*gr_list));
if (!gr_list)
return -ENOMEM;

if (!nvme_identify_ns_granularity(dev_fd(dev), gr_list)) {
struct nvme_id_ns_granularity_desc *desc;
desc = &gr_list->entry[cfg.flbas];

if (desc->nszegran)
align_nsze = desc->nszegran;
if (desc->ncapgran)
align_ncap = desc->ncapgran;
}
}

err = parse_lba_num_si(dev, "nsze", cfg.nsze_si, cfg.flbas, &cfg.nsze, align_nsze);
if (err)
return err;

err = parse_lba_num_si(dev, "ncap", cfg.ncap_si, cfg.flbas, &cfg.ncap);
err = parse_lba_num_si(dev, "ncap", cfg.ncap_si, cfg.flbas, &cfg.ncap, align_ncap);
if (err)
return err;

Expand Down

0 comments on commit e68e99b

Please sign in to comment.