Commit 7d9d7d59 authored by Christoph Hellwig's avatar Christoph Hellwig Committed by Jens Axboe

nvme: replace the fmode_t argument to the nvme ioctl handlers with a simple bool

Instead of passing a fmode_t and only checking it fo0r FMODE_WRITE, pass
a bool open_for_write to prepare for callers that won't have the fmode_t.
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarHannes Reinecke <hare@suse.de>
Acked-by: default avatarChristian Brauner <brauner@kernel.org>
Reviewed-by: default avatarKeith Busch <kbusch@kernel.org>
Link: https://lore.kernel.org/r/20230608110258.189493-22-hch@lst.deSigned-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 1991299e
...@@ -14,7 +14,7 @@ enum { ...@@ -14,7 +14,7 @@ enum {
}; };
static bool nvme_cmd_allowed(struct nvme_ns *ns, struct nvme_command *c, static bool nvme_cmd_allowed(struct nvme_ns *ns, struct nvme_command *c,
unsigned int flags, fmode_t mode) unsigned int flags, bool open_for_write)
{ {
u32 effects; u32 effects;
...@@ -80,7 +80,7 @@ static bool nvme_cmd_allowed(struct nvme_ns *ns, struct nvme_command *c, ...@@ -80,7 +80,7 @@ static bool nvme_cmd_allowed(struct nvme_ns *ns, struct nvme_command *c,
* writing. * writing.
*/ */
if (nvme_is_write(c) || (effects & NVME_CMD_EFFECTS_LBCC)) if (nvme_is_write(c) || (effects & NVME_CMD_EFFECTS_LBCC))
return mode & FMODE_WRITE; return open_for_write;
return true; return true;
} }
...@@ -337,7 +337,7 @@ static bool nvme_validate_passthru_nsid(struct nvme_ctrl *ctrl, ...@@ -337,7 +337,7 @@ static bool nvme_validate_passthru_nsid(struct nvme_ctrl *ctrl,
static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns, static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
struct nvme_passthru_cmd __user *ucmd, unsigned int flags, struct nvme_passthru_cmd __user *ucmd, unsigned int flags,
fmode_t mode) bool open_for_write)
{ {
struct nvme_passthru_cmd cmd; struct nvme_passthru_cmd cmd;
struct nvme_command c; struct nvme_command c;
...@@ -365,7 +365,7 @@ static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns, ...@@ -365,7 +365,7 @@ static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
c.common.cdw14 = cpu_to_le32(cmd.cdw14); c.common.cdw14 = cpu_to_le32(cmd.cdw14);
c.common.cdw15 = cpu_to_le32(cmd.cdw15); c.common.cdw15 = cpu_to_le32(cmd.cdw15);
if (!nvme_cmd_allowed(ns, &c, 0, mode)) if (!nvme_cmd_allowed(ns, &c, 0, open_for_write))
return -EACCES; return -EACCES;
if (cmd.timeout_ms) if (cmd.timeout_ms)
...@@ -385,7 +385,7 @@ static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns, ...@@ -385,7 +385,7 @@ static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
static int nvme_user_cmd64(struct nvme_ctrl *ctrl, struct nvme_ns *ns, static int nvme_user_cmd64(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
struct nvme_passthru_cmd64 __user *ucmd, unsigned int flags, struct nvme_passthru_cmd64 __user *ucmd, unsigned int flags,
fmode_t mode) bool open_for_write)
{ {
struct nvme_passthru_cmd64 cmd; struct nvme_passthru_cmd64 cmd;
struct nvme_command c; struct nvme_command c;
...@@ -412,7 +412,7 @@ static int nvme_user_cmd64(struct nvme_ctrl *ctrl, struct nvme_ns *ns, ...@@ -412,7 +412,7 @@ static int nvme_user_cmd64(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
c.common.cdw14 = cpu_to_le32(cmd.cdw14); c.common.cdw14 = cpu_to_le32(cmd.cdw14);
c.common.cdw15 = cpu_to_le32(cmd.cdw15); c.common.cdw15 = cpu_to_le32(cmd.cdw15);
if (!nvme_cmd_allowed(ns, &c, flags, mode)) if (!nvme_cmd_allowed(ns, &c, flags, open_for_write))
return -EACCES; return -EACCES;
if (cmd.timeout_ms) if (cmd.timeout_ms)
...@@ -583,7 +583,7 @@ static int nvme_uring_cmd_io(struct nvme_ctrl *ctrl, struct nvme_ns *ns, ...@@ -583,7 +583,7 @@ static int nvme_uring_cmd_io(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
c.common.cdw14 = cpu_to_le32(READ_ONCE(cmd->cdw14)); c.common.cdw14 = cpu_to_le32(READ_ONCE(cmd->cdw14));
c.common.cdw15 = cpu_to_le32(READ_ONCE(cmd->cdw15)); c.common.cdw15 = cpu_to_le32(READ_ONCE(cmd->cdw15));
if (!nvme_cmd_allowed(ns, &c, 0, ioucmd->file->f_mode)) if (!nvme_cmd_allowed(ns, &c, 0, ioucmd->file->f_mode & FMODE_WRITE))
return -EACCES; return -EACCES;
d.metadata = READ_ONCE(cmd->metadata); d.metadata = READ_ONCE(cmd->metadata);
...@@ -649,13 +649,13 @@ static bool is_ctrl_ioctl(unsigned int cmd) ...@@ -649,13 +649,13 @@ static bool is_ctrl_ioctl(unsigned int cmd)
} }
static int nvme_ctrl_ioctl(struct nvme_ctrl *ctrl, unsigned int cmd, static int nvme_ctrl_ioctl(struct nvme_ctrl *ctrl, unsigned int cmd,
void __user *argp, fmode_t mode) void __user *argp, bool open_for_write)
{ {
switch (cmd) { switch (cmd) {
case NVME_IOCTL_ADMIN_CMD: case NVME_IOCTL_ADMIN_CMD:
return nvme_user_cmd(ctrl, NULL, argp, 0, mode); return nvme_user_cmd(ctrl, NULL, argp, 0, open_for_write);
case NVME_IOCTL_ADMIN64_CMD: case NVME_IOCTL_ADMIN64_CMD:
return nvme_user_cmd64(ctrl, NULL, argp, 0, mode); return nvme_user_cmd64(ctrl, NULL, argp, 0, open_for_write);
default: default:
return sed_ioctl(ctrl->opal_dev, cmd, argp); return sed_ioctl(ctrl->opal_dev, cmd, argp);
} }
...@@ -680,14 +680,14 @@ struct nvme_user_io32 { ...@@ -680,14 +680,14 @@ struct nvme_user_io32 {
#endif /* COMPAT_FOR_U64_ALIGNMENT */ #endif /* COMPAT_FOR_U64_ALIGNMENT */
static int nvme_ns_ioctl(struct nvme_ns *ns, unsigned int cmd, static int nvme_ns_ioctl(struct nvme_ns *ns, unsigned int cmd,
void __user *argp, unsigned int flags, fmode_t mode) void __user *argp, unsigned int flags, bool open_for_write)
{ {
switch (cmd) { switch (cmd) {
case NVME_IOCTL_ID: case NVME_IOCTL_ID:
force_successful_syscall_return(); force_successful_syscall_return();
return ns->head->ns_id; return ns->head->ns_id;
case NVME_IOCTL_IO_CMD: case NVME_IOCTL_IO_CMD:
return nvme_user_cmd(ns->ctrl, ns, argp, flags, mode); return nvme_user_cmd(ns->ctrl, ns, argp, flags, open_for_write);
/* /*
* struct nvme_user_io can have different padding on some 32-bit ABIs. * struct nvme_user_io can have different padding on some 32-bit ABIs.
* Just accept the compat version as all fields that are used are the * Just accept the compat version as all fields that are used are the
...@@ -702,7 +702,8 @@ static int nvme_ns_ioctl(struct nvme_ns *ns, unsigned int cmd, ...@@ -702,7 +702,8 @@ static int nvme_ns_ioctl(struct nvme_ns *ns, unsigned int cmd,
flags |= NVME_IOCTL_VEC; flags |= NVME_IOCTL_VEC;
fallthrough; fallthrough;
case NVME_IOCTL_IO64_CMD: case NVME_IOCTL_IO64_CMD:
return nvme_user_cmd64(ns->ctrl, ns, argp, flags, mode); return nvme_user_cmd64(ns->ctrl, ns, argp, flags,
open_for_write);
default: default:
return -ENOTTY; return -ENOTTY;
} }
...@@ -712,6 +713,7 @@ int nvme_ioctl(struct block_device *bdev, fmode_t mode, ...@@ -712,6 +713,7 @@ int nvme_ioctl(struct block_device *bdev, fmode_t mode,
unsigned int cmd, unsigned long arg) unsigned int cmd, unsigned long arg)
{ {
struct nvme_ns *ns = bdev->bd_disk->private_data; struct nvme_ns *ns = bdev->bd_disk->private_data;
bool open_for_write = mode & FMODE_WRITE;
void __user *argp = (void __user *)arg; void __user *argp = (void __user *)arg;
unsigned int flags = 0; unsigned int flags = 0;
...@@ -719,19 +721,20 @@ int nvme_ioctl(struct block_device *bdev, fmode_t mode, ...@@ -719,19 +721,20 @@ int nvme_ioctl(struct block_device *bdev, fmode_t mode,
flags |= NVME_IOCTL_PARTITION; flags |= NVME_IOCTL_PARTITION;
if (is_ctrl_ioctl(cmd)) if (is_ctrl_ioctl(cmd))
return nvme_ctrl_ioctl(ns->ctrl, cmd, argp, mode); return nvme_ctrl_ioctl(ns->ctrl, cmd, argp, open_for_write);
return nvme_ns_ioctl(ns, cmd, argp, flags, mode); return nvme_ns_ioctl(ns, cmd, argp, flags, open_for_write);
} }
long nvme_ns_chr_ioctl(struct file *file, unsigned int cmd, unsigned long arg) long nvme_ns_chr_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{ {
struct nvme_ns *ns = struct nvme_ns *ns =
container_of(file_inode(file)->i_cdev, struct nvme_ns, cdev); container_of(file_inode(file)->i_cdev, struct nvme_ns, cdev);
bool open_for_write = file->f_mode & FMODE_WRITE;
void __user *argp = (void __user *)arg; void __user *argp = (void __user *)arg;
if (is_ctrl_ioctl(cmd)) if (is_ctrl_ioctl(cmd))
return nvme_ctrl_ioctl(ns->ctrl, cmd, argp, file->f_mode); return nvme_ctrl_ioctl(ns->ctrl, cmd, argp, open_for_write);
return nvme_ns_ioctl(ns, cmd, argp, 0, file->f_mode); return nvme_ns_ioctl(ns, cmd, argp, 0, open_for_write);
} }
static int nvme_uring_cmd_checks(unsigned int issue_flags) static int nvme_uring_cmd_checks(unsigned int issue_flags)
...@@ -800,7 +803,7 @@ int nvme_ns_chr_uring_cmd_iopoll(struct io_uring_cmd *ioucmd, ...@@ -800,7 +803,7 @@ int nvme_ns_chr_uring_cmd_iopoll(struct io_uring_cmd *ioucmd,
#ifdef CONFIG_NVME_MULTIPATH #ifdef CONFIG_NVME_MULTIPATH
static int nvme_ns_head_ctrl_ioctl(struct nvme_ns *ns, unsigned int cmd, static int nvme_ns_head_ctrl_ioctl(struct nvme_ns *ns, unsigned int cmd,
void __user *argp, struct nvme_ns_head *head, int srcu_idx, void __user *argp, struct nvme_ns_head *head, int srcu_idx,
fmode_t mode) bool open_for_write)
__releases(&head->srcu) __releases(&head->srcu)
{ {
struct nvme_ctrl *ctrl = ns->ctrl; struct nvme_ctrl *ctrl = ns->ctrl;
...@@ -808,7 +811,7 @@ static int nvme_ns_head_ctrl_ioctl(struct nvme_ns *ns, unsigned int cmd, ...@@ -808,7 +811,7 @@ static int nvme_ns_head_ctrl_ioctl(struct nvme_ns *ns, unsigned int cmd,
nvme_get_ctrl(ns->ctrl); nvme_get_ctrl(ns->ctrl);
srcu_read_unlock(&head->srcu, srcu_idx); srcu_read_unlock(&head->srcu, srcu_idx);
ret = nvme_ctrl_ioctl(ns->ctrl, cmd, argp, mode); ret = nvme_ctrl_ioctl(ns->ctrl, cmd, argp, open_for_write);
nvme_put_ctrl(ctrl); nvme_put_ctrl(ctrl);
return ret; return ret;
...@@ -818,6 +821,7 @@ int nvme_ns_head_ioctl(struct block_device *bdev, fmode_t mode, ...@@ -818,6 +821,7 @@ int nvme_ns_head_ioctl(struct block_device *bdev, fmode_t mode,
unsigned int cmd, unsigned long arg) unsigned int cmd, unsigned long arg)
{ {
struct nvme_ns_head *head = bdev->bd_disk->private_data; struct nvme_ns_head *head = bdev->bd_disk->private_data;
bool open_for_write = mode & FMODE_WRITE;
void __user *argp = (void __user *)arg; void __user *argp = (void __user *)arg;
struct nvme_ns *ns; struct nvme_ns *ns;
int srcu_idx, ret = -EWOULDBLOCK; int srcu_idx, ret = -EWOULDBLOCK;
...@@ -838,9 +842,9 @@ int nvme_ns_head_ioctl(struct block_device *bdev, fmode_t mode, ...@@ -838,9 +842,9 @@ int nvme_ns_head_ioctl(struct block_device *bdev, fmode_t mode,
*/ */
if (is_ctrl_ioctl(cmd)) if (is_ctrl_ioctl(cmd))
return nvme_ns_head_ctrl_ioctl(ns, cmd, argp, head, srcu_idx, return nvme_ns_head_ctrl_ioctl(ns, cmd, argp, head, srcu_idx,
mode); open_for_write);
ret = nvme_ns_ioctl(ns, cmd, argp, flags, mode); ret = nvme_ns_ioctl(ns, cmd, argp, flags, open_for_write);
out_unlock: out_unlock:
srcu_read_unlock(&head->srcu, srcu_idx); srcu_read_unlock(&head->srcu, srcu_idx);
return ret; return ret;
...@@ -849,6 +853,7 @@ int nvme_ns_head_ioctl(struct block_device *bdev, fmode_t mode, ...@@ -849,6 +853,7 @@ int nvme_ns_head_ioctl(struct block_device *bdev, fmode_t mode,
long nvme_ns_head_chr_ioctl(struct file *file, unsigned int cmd, long nvme_ns_head_chr_ioctl(struct file *file, unsigned int cmd,
unsigned long arg) unsigned long arg)
{ {
bool open_for_write = file->f_mode & FMODE_WRITE;
struct cdev *cdev = file_inode(file)->i_cdev; struct cdev *cdev = file_inode(file)->i_cdev;
struct nvme_ns_head *head = struct nvme_ns_head *head =
container_of(cdev, struct nvme_ns_head, cdev); container_of(cdev, struct nvme_ns_head, cdev);
...@@ -863,9 +868,9 @@ long nvme_ns_head_chr_ioctl(struct file *file, unsigned int cmd, ...@@ -863,9 +868,9 @@ long nvme_ns_head_chr_ioctl(struct file *file, unsigned int cmd,
if (is_ctrl_ioctl(cmd)) if (is_ctrl_ioctl(cmd))
return nvme_ns_head_ctrl_ioctl(ns, cmd, argp, head, srcu_idx, return nvme_ns_head_ctrl_ioctl(ns, cmd, argp, head, srcu_idx,
file->f_mode); open_for_write);
ret = nvme_ns_ioctl(ns, cmd, argp, 0, file->f_mode); ret = nvme_ns_ioctl(ns, cmd, argp, 0, open_for_write);
out_unlock: out_unlock:
srcu_read_unlock(&head->srcu, srcu_idx); srcu_read_unlock(&head->srcu, srcu_idx);
return ret; return ret;
...@@ -940,7 +945,7 @@ int nvme_dev_uring_cmd(struct io_uring_cmd *ioucmd, unsigned int issue_flags) ...@@ -940,7 +945,7 @@ int nvme_dev_uring_cmd(struct io_uring_cmd *ioucmd, unsigned int issue_flags)
} }
static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp, static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp,
fmode_t mode) bool open_for_write)
{ {
struct nvme_ns *ns; struct nvme_ns *ns;
int ret; int ret;
...@@ -964,7 +969,7 @@ static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp, ...@@ -964,7 +969,7 @@ static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp,
kref_get(&ns->kref); kref_get(&ns->kref);
up_read(&ctrl->namespaces_rwsem); up_read(&ctrl->namespaces_rwsem);
ret = nvme_user_cmd(ctrl, ns, argp, 0, mode); ret = nvme_user_cmd(ctrl, ns, argp, 0, open_for_write);
nvme_put_ns(ns); nvme_put_ns(ns);
return ret; return ret;
...@@ -976,16 +981,17 @@ static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp, ...@@ -976,16 +981,17 @@ static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp,
long nvme_dev_ioctl(struct file *file, unsigned int cmd, long nvme_dev_ioctl(struct file *file, unsigned int cmd,
unsigned long arg) unsigned long arg)
{ {
bool open_for_write = file->f_mode & FMODE_WRITE;
struct nvme_ctrl *ctrl = file->private_data; struct nvme_ctrl *ctrl = file->private_data;
void __user *argp = (void __user *)arg; void __user *argp = (void __user *)arg;
switch (cmd) { switch (cmd) {
case NVME_IOCTL_ADMIN_CMD: case NVME_IOCTL_ADMIN_CMD:
return nvme_user_cmd(ctrl, NULL, argp, 0, file->f_mode); return nvme_user_cmd(ctrl, NULL, argp, 0, open_for_write);
case NVME_IOCTL_ADMIN64_CMD: case NVME_IOCTL_ADMIN64_CMD:
return nvme_user_cmd64(ctrl, NULL, argp, 0, file->f_mode); return nvme_user_cmd64(ctrl, NULL, argp, 0, open_for_write);
case NVME_IOCTL_IO_CMD: case NVME_IOCTL_IO_CMD:
return nvme_dev_user_cmd(ctrl, argp, file->f_mode); return nvme_dev_user_cmd(ctrl, argp, open_for_write);
case NVME_IOCTL_RESET: case NVME_IOCTL_RESET:
if (!capable(CAP_SYS_ADMIN)) if (!capable(CAP_SYS_ADMIN))
return -EACCES; return -EACCES;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment