Merge tag 'block-5.9-2020-09-22' of git://git.kernel.dk/linux-block
[platform/kernel/linux-starfive.git] / drivers / nvme / host / core.c
index f3a61a2..2f24905 100644 (file)
@@ -3261,10 +3261,24 @@ static int nvme_dev_open(struct inode *inode, struct file *file)
                return -EWOULDBLOCK;
        }
 
+       nvme_get_ctrl(ctrl);
+       if (!try_module_get(ctrl->ops->module))
+               return -EINVAL;
+
        file->private_data = ctrl;
        return 0;
 }
 
+static int nvme_dev_release(struct inode *inode, struct file *file)
+{
+       struct nvme_ctrl *ctrl =
+               container_of(inode->i_cdev, struct nvme_ctrl, cdev);
+
+       module_put(ctrl->ops->module);
+       nvme_put_ctrl(ctrl);
+       return 0;
+}
+
 static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
 {
        struct nvme_ns *ns;
@@ -3327,6 +3341,7 @@ static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
 static const struct file_operations nvme_dev_fops = {
        .owner          = THIS_MODULE,
        .open           = nvme_dev_open,
+       .release        = nvme_dev_release,
        .unlocked_ioctl = nvme_dev_ioctl,
        .compat_ioctl   = compat_ptr_ioctl,
 };