Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/run-nightly-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
#BDEV0 is an environment variable of the self-hosted runner instance
#that contains a valid nvme ctrl name which is capable of the nvm
#command set.
options: '--privileged -e BDEV0'
options: '--privileged -v "/dev":"/dev":z -e BDEV0'
steps:
- name: Output kernel version
run: |
Expand Down
84 changes: 13 additions & 71 deletions nvme.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,48 +300,7 @@ static bool is_blkdev(struct nvme_dev *dev)
return S_ISBLK(dev->direct.stat.st_mode);
}

static bool is_nvme_dev(char *dev)
{
int instance;
int head_instance;

return sscanf(basename(dev), "nvme%dn%d", &instance, &head_instance) == 1;
}

static __u32 get_nsid(struct argconfig_commandline_options *opts)
{
__u32 *nsid = argconfig_get_value(opts, "namespace-id");

if (nsid && *nsid != NVME_NSID_ALL)
return *nsid;

return NVME_NSID_NONE;
}

static int open_blkdev_direct(char *dev, int flags, __u32 nsid)
{
_cleanup_free_ char *blkdev = NULL;
int fd = -1;

if (is_nvme_dev(dev) && nsid) {
if (asprintf(&blkdev, "%sn%d", dev, nsid) < 0)
blkdev = NULL;
}

if (blkdev) {
fd = open(blkdev, flags);
print_debug("blkdev: %s, fd: %d\n", blkdev, fd);
}

if (fd < 0) {
fd = open(dev, flags);
print_debug("dev: %s, fd: %d\n", dev, fd);
}

return fd;
}

static int open_dev_direct(struct nvme_dev **devp, char *devstr, int flags, __u32 nsid)
static int open_dev_direct(struct nvme_dev **devp, char *devstr, int flags)
{
struct nvme_dev *dev;
int err;
Expand All @@ -352,7 +311,7 @@ static int open_dev_direct(struct nvme_dev **devp, char *devstr, int flags, __u3

dev->type = NVME_DEV_DIRECT;
dev->name = basename(devstr);
err = open_blkdev_direct(devstr, flags, nsid);
err = open(devstr, flags);
if (err < 0) {
nvme_show_perror(devstr);
goto err_free;
Expand Down Expand Up @@ -454,12 +413,10 @@ static int check_arg_dev(int argc, char **argv)
return 0;
}

static int get_dev(struct nvme_dev **dev, int argc, char **argv, int flags,
struct argconfig_commandline_options *opts)
static int get_dev(struct nvme_dev **dev, int argc, char **argv, int flags)
{
char *devname;
int ret;
__u32 nsid = get_nsid(opts);

ret = check_arg_dev(argc, argv);
if (ret)
Expand All @@ -471,7 +428,7 @@ static int get_dev(struct nvme_dev **dev, int argc, char **argv, int flags,
if (!strncmp(devname, "mctp:", strlen("mctp:")))
ret = open_dev_mi_mctp(dev, devname);
else
ret = open_dev_direct(dev, devname, flags, nsid);
ret = open_dev_direct(dev, devname, flags);

return ret ? -errno : 0;
}
Expand Down Expand Up @@ -501,22 +458,22 @@ int parse_and_open(struct nvme_dev **dev, int argc, char **argv,
if (ret)
return ret;

ret = get_dev(dev, argc, argv, O_RDONLY, opts);
ret = get_dev(dev, argc, argv, O_RDONLY);
if (ret < 0)
argconfig_print_help(desc, opts);

return ret;
}

int open_exclusive(struct nvme_dev **dev, int argc, char **argv,
int ignore_exclusive, struct argconfig_commandline_options *opts)
int ignore_exclusive)
{
int flags = O_RDONLY;

if (!ignore_exclusive)
flags |= O_EXCL;

return get_dev(dev, argc, argv, flags, opts);
return get_dev(dev, argc, argv, flags);
}

int validate_output_format(const char *format, nvme_print_flags_t *flags)
Expand Down Expand Up @@ -6275,7 +6232,7 @@ static int format_cmd(int argc, char **argv, struct command *cmd, struct plugin
if (err)
return err;

err = open_exclusive(&dev, argc, argv, cfg.force, opts);
err = open_exclusive(&dev, argc, argv, cfg.force);
if (err) {
if (errno == EBUSY) {
fprintf(stderr, "Failed to open %s.\n", basename(argv[optind]));
Expand Down Expand Up @@ -7465,23 +7422,6 @@ static int copy_cmd(int argc, char **argv, struct command *cmd, struct plugin *p
return err;
}

static void io_cmd_show_error(struct nvme_dev *dev, int err, const char *cmd)
{
if (err > 0) {
nvme_show_status(err);
return;
}

nvme_show_init();

nvme_show_error("%s: %s", cmd, nvme_strerror(errno));

if (is_chardev(dev))
nvme_show_result("char device provided but blkdev is needed, e.g. /dev/nvme0n1");

nvme_show_finish();
}

static int flush_cmd(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Commit data and metadata associated with\n"
Expand Down Expand Up @@ -7517,8 +7457,10 @@ static int flush_cmd(int argc, char **argv, struct command *cmd, struct plugin *
}

err = nvme_flush(dev_fd(dev), cfg.namespace_id);
if (err)
io_cmd_show_error(dev, err, "flush");
if (err < 0)
nvme_show_error("flush: %s", nvme_strerror(errno));
else if (err != 0)
nvme_show_status(err);
else
printf("NVMe Flush: success\n");

Expand Down Expand Up @@ -7985,7 +7927,7 @@ static int submit_io(int opcode, char *command, const char *desc, int argc, char
err = parse_args(argc, argv, desc, opts);
if (err)
return err;
err = open_exclusive(&dev, argc, argv, cfg.force, opts);
err = open_exclusive(&dev, argc, argv, cfg.force);
if (err) {
if (errno == EBUSY) {
fprintf(stderr, "Failed to open %s.\n", basename(argv[optind]));
Expand Down
13 changes: 0 additions & 13 deletions util/argconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -429,16 +429,3 @@ bool argconfig_parse_seen(struct argconfig_commandline_options *s,

return false;
}

void *argconfig_get_value(struct argconfig_commandline_options *s, const char *option)
{
for (; s && s->option; s++) {
if (!strcmp(s->option, option)) {
if (s->seen)
return s->default_value;
break;
}
}

return NULL;
}
1 change: 0 additions & 1 deletion util/argconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,4 @@ int argconfig_parse_comma_sep_array_u64(char *string, __u64 *val,
void print_word_wrapped(const char *s, int indent, int start, FILE *stream);
bool argconfig_parse_seen(struct argconfig_commandline_options *options,
const char *option);
void *argconfig_get_value(struct argconfig_commandline_options *s, const char *option);
#endif
Loading