Export per lun information to user, let the user
know more detail information about underlying device.

Signed-off-by: Wenwei Tao <ww.tao0...@gmail.com>
---
 drivers/lightnvm/core.c       | 59 ++++++++++++++++++++++++++++++++++++++++++-
 drivers/lightnvm/gennvm.c     | 34 +++++++++++++++++++++++++
 include/linux/lightnvm.h      |  3 +++
 include/uapi/linux/lightnvm.h | 23 +++++++++++++++++
 4 files changed, 118 insertions(+), 1 deletion(-)

diff --git a/drivers/lightnvm/core.c b/drivers/lightnvm/core.c
index 95c5445..7da3b6b 100644
--- a/drivers/lightnvm/core.c
+++ b/drivers/lightnvm/core.c
@@ -28,7 +28,6 @@
 #include <linux/miscdevice.h>
 #include <linux/lightnvm.h>
 #include <linux/sched/sysctl.h>
-#include <uapi/linux/lightnvm.h>
 
 static LIST_HEAD(nvm_targets);
 static LIST_HEAD(nvm_mgrs);
@@ -1042,6 +1041,62 @@ static long nvm_ioctl_dev_remove(struct file *file, void 
__user *arg)
        return __nvm_configure_remove(&remove);
 }
 
+static long nvm_ioctl_dev_luns_status(struct file *file, void __user *arg)
+{
+       struct nvm_dev *dev;
+       struct nvm_ioctl_luns_status *status;
+       long ret = -EINVAL;
+
+       if (!capable(CAP_SYS_ADMIN))
+               return -EPERM;
+
+       status = kzalloc(sizeof(struct nvm_ioctl_luns_status), GFP_KERNEL);
+       if (!status)
+               return -ENOMEM;
+
+       if (copy_from_user(status, arg,
+               sizeof(struct nvm_ioctl_luns_status))) {
+               ret = -EFAULT;
+               goto out;
+       }
+
+       status->dev[DISK_NAME_LEN - 1] = '\0';
+       down_write(&nvm_lock);
+       dev = nvm_find_nvm_dev(status->dev);
+       up_write(&nvm_lock);
+       if (!dev) {
+               pr_err("nvm: device not found\n");
+               goto out;
+       }
+
+       if (!dev->mt) {
+               pr_info("nvm: device has no media manager registered.\n");
+               ret = -ENODEV;
+               goto out;
+       }
+
+       if (status->lun_begin > status->lun_end ||
+                       status->lun_end > dev->nr_luns) {
+               pr_err("nvm: lun out of bound (%u:%u > %u)\n",
+                       status->lun_begin, status->lun_end, dev->nr_luns);
+               goto out;
+       }
+
+       dev->mt->get_luns_status(dev, status);
+
+       if (copy_to_user(arg, status,
+                        sizeof(struct nvm_ioctl_luns_status))) {
+               ret = -EFAULT;
+               goto out;
+       }
+
+       ret = 0;
+
+out:
+       kfree(status);
+       return ret;
+}
+
 static void nvm_setup_nvm_sb_info(struct nvm_sb_info *info)
 {
        info->seqnr = 1;
@@ -1151,6 +1206,8 @@ static long nvm_ctl_ioctl(struct file *file, uint cmd, 
unsigned long arg)
                return nvm_ioctl_dev_create(file, argp);
        case NVM_DEV_REMOVE:
                return nvm_ioctl_dev_remove(file, argp);
+       case NVM_DEV_LUNS_STATUS:
+               return nvm_ioctl_dev_luns_status(file, argp);
        case NVM_DEV_INIT:
                return nvm_ioctl_dev_init(file, argp);
        case NVM_DEV_FACTORY:
diff --git a/drivers/lightnvm/gennvm.c b/drivers/lightnvm/gennvm.c
index b97801c..222c7e8 100644
--- a/drivers/lightnvm/gennvm.c
+++ b/drivers/lightnvm/gennvm.c
@@ -505,6 +505,39 @@ static struct nvm_lun *gennvm_get_lun(struct nvm_dev *dev, 
int lunid)
        return &gn->luns[lunid].vlun;
 }
 
+static void gennvm_get_luns_status(struct nvm_dev *dev,
+               struct nvm_ioctl_luns_status *status)
+{
+       int i, lun_begin = status->lun_begin;
+       int nr_luns, lun_end = status->lun_end;
+       struct gen_nvm *gn = dev->mp;
+       struct nvm_lun_status *lun_stat;
+       struct gen_lun *lun;
+
+       status->nr_luns = dev->nr_luns;
+       nr_luns = min(lun_end - lun_begin + 1,
+                               NVM_LUNS_STATUS);
+
+       for (i = 0;  i < nr_luns; i++) {
+               lun = &gn->luns[i + lun_begin];
+               lun_stat = &status->status[i];
+
+               lun_stat->reserved =
+                       test_bit(i + lun_begin, dev->lun_map);
+               lun_stat->id = lun->vlun.id;
+               lun_stat->lun_id = lun->vlun.lun_id;
+               lun_stat->chnl_id = lun->vlun.chnl_id;
+               lun_stat->nr_free_blocks =
+                       lun->vlun.nr_free_blocks;
+               lun_stat->nr_open_blocks =
+                       lun->vlun.nr_open_blocks;
+               lun_stat->nr_closed_blocks =
+                       lun->vlun.nr_closed_blocks;
+               lun_stat->nr_bad_blocks =
+                       lun->vlun.nr_bad_blocks;
+       }
+}
+
 static void gennvm_lun_info_print(struct nvm_dev *dev)
 {
        struct gen_nvm *gn = dev->mp;
@@ -545,6 +578,7 @@ static struct nvmm_type gennvm = {
        .get_lun                = gennvm_get_lun,
        .reserve_lun            = gennvm_reserve_lun,
        .release_lun            = gennvm_release_lun,
+       .get_luns_status        = gennvm_get_luns_status,
        .lun_info_print         = gennvm_lun_info_print,
 
        .get_area               = gennvm_get_area,
diff --git a/include/linux/lightnvm.h b/include/linux/lightnvm.h
index e90a761..0c815ae 100644
--- a/include/linux/lightnvm.h
+++ b/include/linux/lightnvm.h
@@ -472,6 +472,8 @@ typedef int (nvmm_erase_blk_fn)(struct nvm_dev *, struct 
nvm_block *,
 typedef struct nvm_lun *(nvmm_get_lun_fn)(struct nvm_dev *, int);
 typedef int (nvmm_reserve_lun)(struct nvm_dev *, int);
 typedef void (nvmm_release_lun)(struct nvm_dev *, int);
+typedef void (nvmm_get_luns_status)(struct nvm_dev *,
+                               struct nvm_ioctl_luns_status *);
 typedef void (nvmm_lun_info_print_fn)(struct nvm_dev *);
 
 typedef int (nvmm_get_area_fn)(struct nvm_dev *, sector_t *, sector_t);
@@ -502,6 +504,7 @@ struct nvmm_type {
        nvmm_release_lun *release_lun;
 
        /* Statistics */
+       nvmm_get_luns_status *get_luns_status;
        nvmm_lun_info_print_fn *lun_info_print;
 
        nvmm_get_area_fn *get_area;
diff --git a/include/uapi/linux/lightnvm.h b/include/uapi/linux/lightnvm.h
index 6dbf6a0..2d52d6d 100644
--- a/include/uapi/linux/lightnvm.h
+++ b/include/uapi/linux/lightnvm.h
@@ -36,6 +36,7 @@
 #define NVM_MMTYPE_LEN 8
 
 #define NVM_LUNS_MAX 768
+#define NVM_LUNS_STATUS 64
 
 #define NVM_CTRL_FILE "/dev/lightnvm/control"
 
@@ -110,6 +111,25 @@ struct nvm_ioctl_remove {
        __u32 flags;
 };
 
+struct nvm_lun_status {
+       bool reserved;
+       __u32 id;
+       __u32 lun_id;
+       __u32 chnl_id;
+       __u32 nr_open_blocks;
+       __u32 nr_closed_blocks;
+       __u32 nr_free_blocks;
+       __u32 nr_bad_blocks;
+};
+
+struct nvm_ioctl_luns_status {
+       char dev[DISK_NAME_LEN];
+       __u32 nr_luns;
+       __u32 lun_begin;
+       __u32 lun_end;
+       struct nvm_lun_status status[NVM_LUNS_STATUS];
+};
+
 struct nvm_ioctl_dev_init {
        char dev[DISK_NAME_LEN];                /* open-channel SSD device */
        char mmtype[NVM_MMTYPE_LEN];            /* register to media manager */
@@ -140,6 +160,7 @@ enum {
        /* device level cmds */
        NVM_DEV_CREATE_CMD,
        NVM_DEV_REMOVE_CMD,
+       NVM_DEV_LUNS_STATUS_CMD,
 
        /* Init a device to support LightNVM media managers */
        NVM_DEV_INIT_CMD,
@@ -158,6 +179,8 @@ enum {
                                                struct nvm_ioctl_create)
 #define NVM_DEV_REMOVE         _IOW(NVM_IOCTL, NVM_DEV_REMOVE_CMD, \
                                                struct nvm_ioctl_remove)
+#define NVM_DEV_LUNS_STATUS    _IOW(NVM_IOCTL, NVM_DEV_LUNS_STATUS_CMD, \
+                                               struct nvm_ioctl_luns_status)
 #define NVM_DEV_INIT           _IOW(NVM_IOCTL, NVM_DEV_INIT_CMD, \
                                                struct nvm_ioctl_dev_init)
 #define NVM_DEV_FACTORY                _IOW(NVM_IOCTL, NVM_DEV_FACTORY_CMD, \
-- 
1.8.3.1

Reply via email to