[ndctl PATCH] ndctl, firmware: fix kernel version check

2018-02-09 Thread Dan Williams
The firmware update passes when it should skip due to a misplaced
newline.

Cc: Dave Jiang 
Signed-off-by: Dan Williams 
---
 test/firmware-update.sh |3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/test/firmware-update.sh b/test/firmware-update.sh
index 79767d2dc41f..0d5bcdb3cc42 100755
--- a/test/firmware-update.sh
+++ b/test/firmware-update.sh
@@ -53,8 +53,7 @@ do_tests()
$ndctl update-firmware -d $dev -f $image
 }
 
-check_min_kver "4.16" || { echo "kernel $KVER may lack firmware update test 
handling"; exit
-$rc; }
+check_min_kver "4.16" || { echo "kernel $KVER may lack firmware update test 
handling"; exit $rc; }
 modprobe nfit_test
 rc=1
 reset

___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[ndctl PATCH] ndctl, hugetlb: skip, don't fail

2018-02-09 Thread Dan Williams
The hugetlb test has x86 assumptions. Skip instead of fail to enable
package build environments that run the unit tests.

Signed-off-by: Dan Williams 
---
 test/hugetlb.c |6 ++
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/test/hugetlb.c b/test/hugetlb.c
index a08a93ad40f3..11b7a3d25bb2 100644
--- a/test/hugetlb.c
+++ b/test/hugetlb.c
@@ -13,12 +13,10 @@ static int test_hugetlb(void)
for (i = 0; i < (int) ARRAY_SIZE(aligns); i++) {
fprintf(stderr, "%s: page_size: %#lx\n", __func__, aligns[i]);
rc = test_dax_directio(-1, aligns[i], NULL, 0);
-   if (rc == -ENOMEM || rc == -EACCES)
-   return 77;
-   else if (rc == -ENOENT && aligns[i] == SZ_1G)
+   if (rc == -ENOENT && aligns[i] == SZ_1G)
continue; /* system not configured for 1G pages */
else if (rc)
-   return rc;
+   return 77;
}
return 0;
 }

___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[ndctl PATCH] ndctl: fallback to sysconf for PAGE_SIZE

2018-02-09 Thread Dan Williams
On ARM and PowerPC PAGE_SIZE is not defined. Fall back to sysconf().

Signed-off-by: Dan Williams 
---
 ndctl/ndctl.h |5 +
 1 file changed, 5 insertions(+)

diff --git a/ndctl/ndctl.h b/ndctl/ndctl.h
index eb5a9b387b3d..ebd2a41cce46 100644
--- a/ndctl/ndctl.h
+++ b/ndctl/ndctl.h
@@ -16,6 +16,7 @@
 #include 
 #include 
 #include 
+#include 
 
 struct nd_cmd_dimm_flags {
__u32 status;
@@ -210,9 +211,13 @@ enum nd_driver_flags {
ND_DRIVER_DAX_PMEM= 1 << ND_DEVICE_DAX_PMEM,
 };
 
+#ifdef PAGE_SIZE
 enum {
ND_MIN_NAMESPACE_SIZE = PAGE_SIZE,
 };
+#else
+#define ND_MIN_NAMESPACE_SIZE ((unsigned) sysconf(_SC_PAGESIZE))
+#endif
 
 enum ars_masks {
ARS_STATUS_MASK = 0x,

___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


Re: [RFC PATCH v3 0/5] ndctl: monitor: monitor the smart events of

2018-02-09 Thread Dan Williams
On Fri, Feb 9, 2018 at 12:02 AM, QI Fuli  wrote:
> This is a patch set of ndctl monitor, a tiny daemon to monitor the smart
> events of nvdimm dimms. When a smart event fires, monitor will output
> the notification which including dimm health status to syslog or a
> special file according to users' configuration. The output notification
> follows json format and can be consumed by log collectors like Fluentd.
>
> Currently, I implemeted the following four commands to control monitor daemon.
> $ndctl create-monitor
> $ndctl list-monitor
> $ndctl show-monitor
> $ndclt destroy-monitor
>
> I will appreciate if you could give some comments.

Nice to see this new update.

I have a high level comment on the proposed command structure, as far
as I can see the only new command we need is:

   ndctl monitor

...that launches a monitor. We can have options to control whether
that monitor is a one-shot check of events sources, or a daemon that
forks into the background, but the 'create', 'list', 'show', and
'destroy' are the responsibility of systemd or System V services. For
example:

create: systemctl start ndctl-monitor
show: systemctl status ndctl-monitor
destroy: systemctl stop ndctl-monitor
___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[PATCH v2 2/2] device-dax: implement ->pagesize() for smaps to report MMUPageSize

2018-02-09 Thread Dave Jiang
From: Dan Williams 

Given that device-dax is making similar page mapping size guarantees as
hugetlbfs, emit the size in smaps and any other kernel path that
requests the mapping size of a vma.

Reported-by: Jane Chu 
Signed-off-by: Dan Williams 
---
 drivers/dax/device.c |   10 ++
 1 file changed, 10 insertions(+)

diff --git a/drivers/dax/device.c b/drivers/dax/device.c
index 7b0bf825c4e7..b57cd5a7b0bd 100644
--- a/drivers/dax/device.c
+++ b/drivers/dax/device.c
@@ -439,10 +439,20 @@ static int dev_dax_split(struct vm_area_struct *vma, 
unsigned long addr)
return 0;
 }
 
+static unsigned long dev_dax_pagesize(struct vm_area_struct *vma)
+{
+   struct file *filp = vma->vm_file;
+   struct dev_dax *dev_dax = filp->private_data;
+   struct dax_region *dax_region = dev_dax->region;
+
+   return dax_region->align;
+}
+
 static const struct vm_operations_struct dax_vm_ops = {
.fault = dev_dax_fault,
.huge_fault = dev_dax_huge_fault,
.split = dev_dax_split,
+   .pagesize = dev_dax_pagesize,
 };
 
 static int dax_mmap(struct file *filp, struct vm_area_struct *vma)

___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[PATCH v2 0/2] mm, smaps: MMUPageSize for device-dax

2018-02-09 Thread Dave Jiang
Andrew,

>From Dan Williams:
Here is another occasion where we want special-case hugetlbfs enabling
to also apply to device-dax. I started to wonder what other hstate
conversions we might do beyond ->split() and ->pagesize(), but this
appears to be the last of the usages of hstate_vma() in
generic/non-hugetlbfs specific code paths.

v2:
I fixed up the powerpc build issue that Michal reported by restoring the
original location of the function and making the symbol weak.

---

Dan Williams (2):
  mm, hugetlbfs: introduce ->pagesize() to vm_operations_struct
  device-dax: implement ->pagesize() for smaps to report MMUPageSize


 arch/powerpc/mm/hugetlbpage.c |5 +
 drivers/dax/device.c  |   10 ++
 include/linux/mm.h|1 +
 mm/hugetlb.c  |   23 ---
 4 files changed, 24 insertions(+), 15 deletions(-)

--
___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[PATCH v2 1/2] mm, hugetlbfs: introduce ->pagesize() to vm_operations_struct

2018-02-09 Thread Dave Jiang
From: Dan Williams 

When device-dax is operating in huge-page mode we want it to behave like
hugetlbfs and report the MMU page mapping size that is being enforced by
the vma. Similar to commit 31383c6865a5 "mm, hugetlbfs: introduce
->split() to vm_operations_struct" it would be messy to teach
vma_mmu_pagesize() about device-dax page mapping sizes in the same
(hstate) way that hugetlbfs communicates this attribute.  Instead, these
patches introduce a new ->pagesize() vm operation.

Cc: Benjamin Herrenschmidt 
Cc: Paul Mackerras 
Cc: Michael Ellerman 
Reported-by: Jane Chu 
Signed-off-by: Dan Williams 
Signed-off-by: Dave Jiang 
---
 arch/powerpc/mm/hugetlbpage.c |5 +
 include/linux/mm.h|1 +
 mm/hugetlb.c  |   23 ---
 3 files changed, 14 insertions(+), 15 deletions(-)

diff --git a/arch/powerpc/mm/hugetlbpage.c b/arch/powerpc/mm/hugetlbpage.c
index a9b9083c5e49..c6a2e577e842 100644
--- a/arch/powerpc/mm/hugetlbpage.c
+++ b/arch/powerpc/mm/hugetlbpage.c
@@ -568,10 +568,7 @@ unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
if (!radix_enabled())
return 1UL << mmu_psize_to_shift(psize);
 #endif
-   if (!is_vm_hugetlb_page(vma))
-   return PAGE_SIZE;
-
-   return huge_page_size(hstate_vma(vma));
+   return vma_kernel_pagesize(vma);
 }
 
 static inline bool is_power_of_4(unsigned long x)
diff --git a/include/linux/mm.h b/include/linux/mm.h
index ea818ff739cd..37b9aef91ec7 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -383,6 +383,7 @@ struct vm_operations_struct {
int (*huge_fault)(struct vm_fault *vmf, enum page_entry_size pe_size);
void (*map_pages)(struct vm_fault *vmf,
pgoff_t start_pgoff, pgoff_t end_pgoff);
+   unsigned long (*pagesize)(struct vm_area_struct * area);
 
/* notification that a previously read-only page is about to become
 * writable, if an error is returned it will cause a SIGBUS */
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 9a334f5fb730..8fa069b5cb4d 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -637,14 +637,9 @@ EXPORT_SYMBOL_GPL(linear_hugepage_index);
  */
 unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
 {
-   struct hstate *hstate;
-
-   if (!is_vm_hugetlb_page(vma))
-   return PAGE_SIZE;
-
-   hstate = hstate_vma(vma);
-
-   return 1UL << huge_page_shift(hstate);
+   if (vma->vm_ops && vma->vm_ops->pagesize)
+   return vma->vm_ops->pagesize(vma);
+   return PAGE_SIZE;
 }
 EXPORT_SYMBOL_GPL(vma_kernel_pagesize);
 
@@ -654,12 +649,10 @@ EXPORT_SYMBOL_GPL(vma_kernel_pagesize);
  * architectures where it differs, an architecture-specific version of this
  * function is required.
  */
-#ifndef vma_mmu_pagesize
-unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
+__weak unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
 {
return vma_kernel_pagesize(vma);
 }
-#endif
 
 /*
  * Flags for MAP_PRIVATE reservations.  These are stored in the bottom
@@ -3132,6 +3125,13 @@ static int hugetlb_vm_op_split(struct vm_area_struct 
*vma, unsigned long addr)
return 0;
 }
 
+static unsigned long hugetlb_vm_op_pagesize(struct vm_area_struct *vma)
+{
+   struct hstate *hstate = hstate_vma(vma);
+
+   return 1UL << huge_page_shift(hstate);
+}
+
 /*
  * We cannot handle pagefaults against hugetlb pages at all.  They cause
  * handle_mm_fault() to try to instantiate regular-sized pages in the
@@ -3149,6 +3149,7 @@ const struct vm_operations_struct hugetlb_vm_ops = {
.open = hugetlb_vm_op_open,
.close = hugetlb_vm_op_close,
.split = hugetlb_vm_op_split,
+   .pagesize = hugetlb_vm_op_pagesize,
 };
 
 static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,

___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


Re: [RFC PATCH v3 0/5] ndctl: monitor: monitor the smart events of

2018-02-09 Thread Verma, Vishal L

On Fri, 2018-02-09 at 17:02 +0900, QI Fuli wrote:
> This is a patch set of ndctl monitor, a tiny daemon to monitor the
> smart
> events of nvdimm dimms. When a smart event fires, monitor will output
> the notification which including dimm health status to syslog or a
> special file according to users' configuration. The output
> notification
> follows json format and can be consumed by log collectors like
> Fluentd.
> 
> Currently, I implemeted the following four commands to control
> monitor daemon.
> $ndctl create-monitor
> $ndctl list-monitor
> $ndctl show-monitor
> $ndclt destroy-monitor
> 
> I will appreciate if you could give some comments.
> 
> Change log since v2:
>  - Changing the interface of daemon to the ndctl command line
>  - Changing the name of daemon form "nvdimmd" to "monitor"
>  - Removing the config file, unit_file, nvdimmd dir
>  - Removing nvdimmd_test program
>  - Adding ndctl/monitor.c
> 
> Change log since v1:
>  - Adding a config file(/etc/nvdimmd/nvdimmd.conf)
>  - Using struct log_ctx instead of syslog()
> - Using log_syslog() to save the notify messages to syslog
> - Using log_file() to save the notify messages to special file
>  - Adding LOG_NOTICE level to log_priority
>  - Using automake instead of Makefile
>  - Adding a new util file(nvdimmd/util.c) including helper functions
>needed for nvdimm daemon
>  - Adding nvdimmd_test program
> 
> QI Fuli (5):
>   ndctl: monitor: add LOG_NOTICE level to log_priority
>   ndctl: monitor: add ndclt create-monitor command
>   ndctl: monitor: add ndclt list-monitor command
>   ndctl: monitor: add ndclt show-monitor command
>   ndctl: monitor: add ndclt destroy-monitor command
^
I haven't had a chance to look at the rest of the series, but spotted a
quick typo - this is in the subject line of each commit as well:
s/ndclt/ndctl/

> 
>  builtin.h |   4 +
>  configure.ac  |   3 +
>  ndctl/Makefile.am |   3 +-
>  ndctl/monitor.c   | 463
> ++
>  ndctl/ndctl.c |   4 +
>  util/log.c|   2 +
>  util/log.h|   3 +
>  7 files changed, 481 insertions(+), 1 deletion(-)
>  create mode 100644 ndctl/monitor.c
> 
___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[RFC PATCH v3 1/5] ndctl: nvdimmd: add LOG_NOTICE level to

2018-02-09 Thread QI Fuli
This patch adds LOG_NOTICE level to log_priority for the notifications
of nvdimm dimm over threshold event. Currently, the LOG_INFO level
is too low, and the notifications are not up to LOG_ERROR level.

Signed-off-by: QI Fuli 
---
 util/log.c | 2 ++
 util/log.h | 3 +++
 2 files changed, 5 insertions(+)

diff --git a/util/log.c b/util/log.c
index cc4d7d8..c60ca33 100644
--- a/util/log.c
+++ b/util/log.c
@@ -50,6 +50,8 @@ static int log_priority(const char *priority)
return LOG_INFO;
if (strncmp(priority, "debug", 5) == 0)
return LOG_DEBUG;
+   if (strncmp(priority, "notice", 6) == 0)
+   return LOG_NOTICE;
return 0;
 }
 
diff --git a/util/log.h b/util/log.h
index 5fc56e8..495e0d3 100644
--- a/util/log.h
+++ b/util/log.h
@@ -48,15 +48,18 @@ do { \
 #  endif
 #  define log_info(ctx, arg...) log_cond(ctx, LOG_INFO, ## arg)
 #  define log_err(ctx, arg...) log_cond(ctx, LOG_ERR, ## arg)
+#  define log_notice(ctx, arg...) log_cond(ctx, LOG_NOTICE, ## arg)
 #else
 #  define log_dbg(ctx, arg...) log_null(ctx, ## arg)
 #  define log_info(ctx, arg...) log_null(ctx, ## arg)
 #  define log_err(ctx, arg...) log_null(ctx, ## arg)
+#  define log_notice(ctx, arg...) log_null(ctx, ## arg)
 #endif
 
 #define dbg(x, arg...) log_dbg(&(x)->ctx, ## arg)
 #define info(x, arg...) log_info(&(x)->ctx, ## arg)
 #define err(x, arg...) log_err(&(x)->ctx, ## arg)
+#define notice(x, arg...) log_notice(&(x)->ctx, ## arg)
 
 #ifndef HAVE_SECURE_GETENV
 #  ifdef HAVE___SECURE_GETENV
-- 
2.9.5


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[RFC PATCH v3 4/5] ndctl: monitor: add ndclt show-monitor command

2018-02-09 Thread QI Fuli
This patch is used to add $ndctl show-monitor command, by which users
can check the specific of a monitor.
Example: $ndctl show-monitor --monitor m_nmem1

Signed-off-by: QI Fuli 
---
 builtin.h   |  1 +
 ndctl/monitor.c | 35 +++
 ndctl/ndctl.c   |  1 +
 3 files changed, 37 insertions(+)

diff --git a/builtin.h b/builtin.h
index eda5c7a..b5a006d 100644
--- a/builtin.h
+++ b/builtin.h
@@ -38,6 +38,7 @@ int cmd_check_labels(int argc, const char **argv, void *ctx);
 int cmd_inject_error(int argc, const char **argv, void *ctx);
 int cmd_create_monitor(int argc, const char **argv, void *ctx);
 int cmd_list_monitor(int argc, const char **argv, void *ctx);
+int cmd_show_monitor(int argc, const char **argv, void *ctx);
 int cmd_list(int argc, const char **argv, void *ctx);
 #ifdef ENABLE_TEST
 int cmd_test(int argc, const char **argv, void *ctx);
diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index 53b7c67..1cff2d8 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -372,3 +372,38 @@ int cmd_list_monitor(int argc, const char **argv, void 
*ctx)
 out:
return 1;
 }
+
+int cmd_show_monitor(int argc, const char **argv, void *ctx)
+{
+   const struct option options[] = {
+   OPT_STRING('m', "monitor", , "monitor name",
+   "monitor name")
+   };
+   const char * const u[] = {
+   "ndctl show-monitor ",
+   NULL
+   };
+   argc = parse_options(argc, argv, options, u, 0);
+   for (int i = 0; i < argc; i++) {
+   error("unknown parameter \"%s\"\n", argv[i]);
+   goto out;
+   }
+   if (!param.monitor) {
+   error("monitor name --monitor is required\n");
+   goto out;
+   }
+
+   char *filename;
+   filename = get_full_path_filename(proc_path, param.monitor);
+   struct json_object *jmonitors = json_object_from_file(filename);
+   if (jmonitors == NULL) {
+   error("monitor %s is not exist\n", param.monitor);
+   goto out;
+   }
+
+   printf("%s\n", json_object_to_json_string_ext(jmonitors,
+   JSON_C_TO_STRING_PRETTY));
+   return 0;
+out:
+   return 1;
+}
diff --git a/ndctl/ndctl.c b/ndctl/ndctl.c
index 7eae794..460cf76 100644
--- a/ndctl/ndctl.c
+++ b/ndctl/ndctl.c
@@ -86,6 +86,7 @@ static struct cmd_struct commands[] = {
{ "inject-error", cmd_inject_error },
{ "create-monitor", cmd_create_monitor },
{ "list-monitor", cmd_list_monitor },
+   { "show-monitor", cmd_show_monitor },
{ "list", cmd_list },
{ "help", cmd_help },
#ifdef ENABLE_TEST
-- 
2.9.5


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[RfC PATCH v3 5/5] ndctl: monitor: add ndclt destroy-monitor command

2018-02-09 Thread QI Fuli
This patch is used to add $ndctl destroy-monitor command, by which users
can destroy a monitor. After the monitor is destroyed, the file with
same name under /var/ndctl/monitor will also be deleted.
Example: #ndctl destroy-monitor --monitor m_nmem1

Signed-off-by: QI Fuli 
---
 builtin.h   |  1 +
 ndctl/monitor.c | 54 ++
 ndctl/ndctl.c   |  1 +
 3 files changed, 56 insertions(+)

diff --git a/builtin.h b/builtin.h
index b5a006d..2143666 100644
--- a/builtin.h
+++ b/builtin.h
@@ -39,6 +39,7 @@ int cmd_inject_error(int argc, const char **argv, void *ctx);
 int cmd_create_monitor(int argc, const char **argv, void *ctx);
 int cmd_list_monitor(int argc, const char **argv, void *ctx);
 int cmd_show_monitor(int argc, const char **argv, void *ctx);
+int cmd_destroy_monitor(int argc, const char **argv, void *ctx);
 int cmd_list(int argc, const char **argv, void *ctx);
 #ifdef ENABLE_TEST
 int cmd_test(int argc, const char **argv, void *ctx);
diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index 1cff2d8..563a726 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -407,3 +407,57 @@ int cmd_show_monitor(int argc, const char **argv, void 
*ctx)
 out:
return 1;
 }
+
+int cmd_destroy_monitor(int argc, const char **argv, void *ctx)
+{
+   const struct option options[] = {
+   OPT_STRING('m', "monitor", , "monitor name",
+   "monitor name")
+   };
+   const char * const u[] = {
+   "ndctl destroy-monitor ",
+   NULL
+   };
+   argc = parse_options(argc, argv, options, u, 0);
+   for (int i = 0; i < argc; i++) {
+   error("unknown parameter \"%s\"\n", argv[i]);
+   goto out;
+   }
+   if (!param.monitor) {
+   error("monitor name --monitor is required\n");
+   goto out;
+   }
+
+   char *filename;
+   struct json_object *jmonitors, *jpid;
+   int pid;
+   filename = get_full_path_filename(proc_path, param.monitor);
+   jmonitors = json_object_from_file(filename);
+   if (jmonitors == NULL) {
+   error("monitor %s is not exist\n", param.monitor);
+   goto out;
+   }
+
+   if (!json_object_object_get_ex(jmonitors, "pid", )) {
+   error("cannot find pid in %s\n", filename);
+   goto out;
+   }
+   pid = json_object_get_int(jpid);
+   if (!pid) {
+   error("cannot find pid in %s\n", filename);
+   goto out;
+   }
+   if (kill(pid, SIGUSR1) != 0) {
+   error("failed to kill pid(%d)\n", pid);
+   goto out;
+   }
+   if (destroy_monitor_proc() != 0) {
+   error("failed to delete %s\n", filename);
+   goto out;
+   }
+   printf("destroy monitor %s successed\n", param.monitor);
+
+   return 0;
+out:
+   return 1;
+}
diff --git a/ndctl/ndctl.c b/ndctl/ndctl.c
index 460cf76..ad69222 100644
--- a/ndctl/ndctl.c
+++ b/ndctl/ndctl.c
@@ -87,6 +87,7 @@ static struct cmd_struct commands[] = {
{ "create-monitor", cmd_create_monitor },
{ "list-monitor", cmd_list_monitor },
{ "show-monitor", cmd_show_monitor },
+   { "destroy-monitor", cmd_destroy_monitor },
{ "list", cmd_list },
{ "help", cmd_help },
#ifdef ENABLE_TEST
-- 
2.9.5


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[RFC PATCH v3 2/5] ndctl: monitor: add ndctl create-monitor command

2018-02-09 Thread QI Fuli
This patch is used to add $ndctl create-monitor command, by which users can
create a new monitor. Users can select the DIMMS to be monitored by using
[--dimm] [--bus] [--region] [--namespace] options. The notifications can
be outputed to a special file or syslog by using [--output] option, the
special file will be placed under /var/log/ndctl. A name is also required for
a monitor,so users can destroy the monitor by the name. When a monitor is
created successfully, a file with same name will be created under
/var/ndctl/monitor.
Example: 
#ndctl create-monitor --monitor m_nmem1 --dimm nmem1 --output m_nmem.log

Signed-off-by: QI Fuli 
---
 builtin.h |   1 +
 configure.ac  |   3 +
 ndctl/Makefile.am |   3 +-
 ndctl/monitor.c   | 342 ++
 ndctl/ndctl.c |   1 +
 5 files changed, 349 insertions(+), 1 deletion(-)
 create mode 100644 ndctl/monitor.c

diff --git a/builtin.h b/builtin.h
index 5e1b7ef..850f6a8 100644
--- a/builtin.h
+++ b/builtin.h
@@ -36,6 +36,7 @@ int cmd_write_labels(int argc, const char **argv, void *ctx);
 int cmd_init_labels(int argc, const char **argv, void *ctx);
 int cmd_check_labels(int argc, const char **argv, void *ctx);
 int cmd_inject_error(int argc, const char **argv, void *ctx);
+int cmd_create_monitor(int argc, const char **argv, void *ctx);
 int cmd_list(int argc, const char **argv, void *ctx);
 #ifdef ENABLE_TEST
 int cmd_test(int argc, const char **argv, void *ctx);
diff --git a/configure.ac b/configure.ac
index 70ba360..e859e04 100644
--- a/configure.ac
+++ b/configure.ac
@@ -160,6 +160,9 @@ AC_CONFIG_FILES([
 Documentation/daxctl/Makefile
 ])
 
+AC_CONFIG_COMMANDS([monitorlogdir], [$MKDIR_P /var/log/ndctl])
+AC_CONFIG_COMMANDS([monitorprocdir], [$MKDIR_P /var/ndctl/monitor])
+
 AC_OUTPUT
 AC_MSG_RESULT([
 $PACKAGE $VERSION
diff --git a/ndctl/Makefile.am b/ndctl/Makefile.am
index 6677607..d9a484d 100644
--- a/ndctl/Makefile.am
+++ b/ndctl/Makefile.am
@@ -13,7 +13,8 @@ ndctl_SOURCES = ndctl.c \
test.c \
../util/json.c \
util/json-smart.c \
-   inject-error.c
+   inject-error.c \
+   monitor.c
 
 if ENABLE_DESTRUCTIVE
 ndctl_SOURCES += ../test/blk_namespaces.c \
diff --git a/ndctl/monitor.c b/ndctl/monitor.c
new file mode 100644
index 000..cf1cd6e
--- /dev/null
+++ b/ndctl/monitor.c
@@ -0,0 +1,342 @@
+/*
+ * Copyright (c) 2018, FUJITSU LIMITED. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
+ * more details.
+ */
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#define NUM_MAX_DIMM 1024
+#define BUF_SIZE 4096
+
+struct monitor_dimm {
+   struct ndctl_dimm *dimm;
+   const char *devname;
+   int health_eventfd;
+};
+
+static struct parameter {
+   const char *bus;
+   const char *region;
+   const char *dimm;
+   const char *namespace;
+   const char *event;
+   const char *output;
+   const char *monitor;
+   bool all;
+} param;
+
+static const char *proc_path = "/var/ndctl/monitor/";
+
+static char *get_full_path_filename(const char *path, const char *name)
+{
+   char *filename;
+   int len = strlen(path) + strlen (name) +1;
+   filename = (char *) malloc(len);
+   if (!filename)
+   return NULL;
+   filename[0] = '\0';
+   strcpy(filename, path);
+   strcat(filename, name);
+   return filename;
+}
+
+static void log_syslog(struct ndctl_ctx *ctx, int priority, const char *file,
+   int line, const char *fn, const char *format, va_list args)
+{
+   char *buf;
+   buf = (char *)malloc(BUF_SIZE);
+   if (!buf) {
+   syslog(LOG_ERR, "could not get memory for log_syslog\n");
+   exit(EXIT_FAILURE);
+   }
+   vsnprintf(buf, BUF_SIZE, format, args);
+   syslog(priority, "%s", buf);
+   free(buf);
+}
+
+static void log_output(struct ndctl_ctx *ctx, int priority, const char *file,
+   int line, const char *fn, const char *format, va_list args)
+{
+   FILE *f;
+   char *filename, *buf;
+   const char *log_path = "/var/log/ndctl/";
+   filename = get_full_path_filename(log_path, param.output);
+
+   f = fopen(filename, "a+");
+   if (!f) {
+   syslog(LOG_ERR, "open %s failed\n", filename);
+   exit(EXIT_FAILURE);
+   }
+
+   buf = (char *)malloc(BUF_SIZE);
+   if (!buf) {
+   

[RFC PATCH v3 3/5] ndctl: monitor: add ndclt list-monitor command

2018-02-09 Thread QI Fuli
This patch adds $ndctl list-monitor command, by which users can list all
currently running monitors.
Example: $ndctl list-monitor --all

Signed-off-by: QI Fuli 
---
 builtin.h   |  1 +
 ndctl/monitor.c | 32 
 ndctl/ndctl.c   |  1 +
 3 files changed, 34 insertions(+)

diff --git a/builtin.h b/builtin.h
index 850f6a8..eda5c7a 100644
--- a/builtin.h
+++ b/builtin.h
@@ -37,6 +37,7 @@ int cmd_init_labels(int argc, const char **argv, void *ctx);
 int cmd_check_labels(int argc, const char **argv, void *ctx);
 int cmd_inject_error(int argc, const char **argv, void *ctx);
 int cmd_create_monitor(int argc, const char **argv, void *ctx);
+int cmd_list_monitor(int argc, const char **argv, void *ctx);
 int cmd_list(int argc, const char **argv, void *ctx);
 #ifdef ENABLE_TEST
 int cmd_test(int argc, const char **argv, void *ctx);
diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index cf1cd6e..53b7c67 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -340,3 +340,35 @@ int cmd_create_monitor(int argc, const char **argv, void 
*ctx)
 out:
return 1;
 }
+
+int cmd_list_monitor(int argc, const char **argv, void *ctx)
+{
+   const struct option options[] = {
+   OPT_BOOLEAN('a', "all", , "list all monitors")
+   };
+   const char * const u[] = {
+   "ndctl list-monitor []",
+   NULL
+   };
+   argc = parse_options(argc, argv, options, u, 0);
+   for (int i = 0; i < argc; i++) {
+   error("unknown parameter \"%s\"\n", argv[i]);
+   goto out;
+   }
+   DIR *dir;
+   struct dirent *ent;
+   dir = opendir("/var/ndctl/monitor/");
+   if (!dir)
+   return -1;
+   while ((ent = readdir(dir)) != NULL)
+   {
+   if (strcmp(ent->d_name, ".") == 0
+   || strcmp(ent->d_name, "..") == 0)
+   continue;
+   printf(" %s\n", ent->d_name);
+   }
+   return 0;
+
+out:
+   return 1;
+}
diff --git a/ndctl/ndctl.c b/ndctl/ndctl.c
index 6c63d79..7eae794 100644
--- a/ndctl/ndctl.c
+++ b/ndctl/ndctl.c
@@ -85,6 +85,7 @@ static struct cmd_struct commands[] = {
{ "check-labels", cmd_check_labels },
{ "inject-error", cmd_inject_error },
{ "create-monitor", cmd_create_monitor },
+   { "list-monitor", cmd_list_monitor },
{ "list", cmd_list },
{ "help", cmd_help },
#ifdef ENABLE_TEST
-- 
2.9.5


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm


[RFC PATCH v3 0/5] ndctl: monitor: monitor the smart events of

2018-02-09 Thread QI Fuli
This is a patch set of ndctl monitor, a tiny daemon to monitor the smart
events of nvdimm dimms. When a smart event fires, monitor will output
the notification which including dimm health status to syslog or a
special file according to users' configuration. The output notification
follows json format and can be consumed by log collectors like Fluentd.

Currently, I implemeted the following four commands to control monitor daemon.
$ndctl create-monitor
$ndctl list-monitor
$ndctl show-monitor
$ndclt destroy-monitor

I will appreciate if you could give some comments.

Change log since v2:
 - Changing the interface of daemon to the ndctl command line
 - Changing the name of daemon form "nvdimmd" to "monitor"
 - Removing the config file, unit_file, nvdimmd dir
 - Removing nvdimmd_test program
 - Adding ndctl/monitor.c

Change log since v1:
 - Adding a config file(/etc/nvdimmd/nvdimmd.conf)
 - Using struct log_ctx instead of syslog()
- Using log_syslog() to save the notify messages to syslog
- Using log_file() to save the notify messages to special file
 - Adding LOG_NOTICE level to log_priority
 - Using automake instead of Makefile
 - Adding a new util file(nvdimmd/util.c) including helper functions
   needed for nvdimm daemon
 - Adding nvdimmd_test program

QI Fuli (5):
  ndctl: monitor: add LOG_NOTICE level to log_priority
  ndctl: monitor: add ndclt create-monitor command
  ndctl: monitor: add ndclt list-monitor command
  ndctl: monitor: add ndclt show-monitor command
  ndctl: monitor: add ndclt destroy-monitor command

 builtin.h |   4 +
 configure.ac  |   3 +
 ndctl/Makefile.am |   3 +-
 ndctl/monitor.c   | 463 ++
 ndctl/ndctl.c |   4 +
 util/log.c|   2 +
 util/log.h|   3 +
 7 files changed, 481 insertions(+), 1 deletion(-)
 create mode 100644 ndctl/monitor.c

-- 
2.9.5


___
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm