From patchwork Tue Aug 16 17:22:04 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 9284315 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9BCEF600CB for ; Tue, 16 Aug 2016 17:24:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8C0CD285E3 for ; Tue, 16 Aug 2016 17:24:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8121A287E0; Tue, 16 Aug 2016 17:24:29 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id E61E9285E3 for ; Tue, 16 Aug 2016 17:24:28 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id DE0461A1E1F; Tue, 16 Aug 2016 10:24:28 -0700 (PDT) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by ml01.01.org (Postfix) with ESMTP id 3EDB41A1DF6 for ; Tue, 16 Aug 2016 10:24:28 -0700 (PDT) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga101.jf.intel.com with ESMTP; 16 Aug 2016 10:24:28 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.28,529,1464678000"; d="scan'208";a="156679264" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.14]) by fmsmga004.fm.intel.com with ESMTP; 16 Aug 2016 10:24:28 -0700 Subject: [ndctl PATCH 7/8] test: convert device-dax test from shell to C From: Dan Williams To: linux-nvdimm@lists.01.org Date: Tue, 16 Aug 2016 10:22:04 -0700 Message-ID: <147136812464.27902.10272950614113633356.stgit@dwillia2-desk3.amr.corp.intel.com> In-Reply-To: <147136808681.27902.14029119240021638000.stgit@dwillia2-desk3.amr.corp.intel.com> References: <147136808681.27902.14029119240021638000.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: StGit/0.17.1-9-g687f MIME-Version: 1.0 X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" X-Virus-Scanned: ClamAV using ClamSMTP In preparation for expanded device-dax unit tests, convert the existing implementation to be entirely C-based. Signed-off-by: Dan Williams --- test/Makefile.am | 13 ++++- test/device-dax.c | 128 ++++++++++++++++++++++++++++++++++++++++++++++------ test/device-dax.sh | 40 ---------------- 3 files changed, 124 insertions(+), 57 deletions(-) delete mode 100755 test/device-dax.sh diff --git a/test/Makefile.am b/test/Makefile.am index ab566148e1f4..d13c138bb28a 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -20,7 +20,7 @@ TESTS +=\ pmem-ns \ dax-dev \ dax.sh \ - device-dax.sh \ + device-dax \ mmap.sh check_PROGRAMS +=\ @@ -57,4 +57,13 @@ dax_dev_LDADD = $(LIBNDCTL_LIB) dax_pmd_SOURCES = dax-pmd.c mmap_SOURCES = mmap.c dax_errors_SOURCES = dax-errors.c -device_dax_SOURCES = device-dax.c +device_dax_SOURCES = \ + device-dax.c \ + dax-dev.c \ + core.c \ + ../ndctl/builtin-xaction-namespace.c \ + ../ndctl/util/json.c +device_dax_LDADD = \ + $(LIBNDCTL_LIB) \ + $(JSON_LIBS) \ + ../libutil.a diff --git a/test/device-dax.c b/test/device-dax.c index addf93f59252..7b624ea5bc8f 100644 --- a/test/device-dax.c +++ b/test/device-dax.c @@ -1,32 +1,130 @@ -#include -#include -#include #include -#include #include -#include #include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +static int create_namespace(int argc, const char **argv, struct ndctl_ctx *ctx) +{ + builtin_xaction_namespace_reset(); + return cmd_create_namespace(argc, argv, ctx); +} + +static int reset_device_dax(struct ndctl_namespace *ndns) +{ + struct ndctl_ctx *ctx = ndctl_namespace_get_ctx(ndns); + const char *argv[] = { + "__func__", "-v", "-m", "raw", "-f", "-e", "", + }; + int argc = ARRAY_SIZE(argv); + + argv[argc - 1] = ndctl_namespace_get_devname(ndns); + return create_namespace(argc, argv, ctx); +} + +static int setup_device_dax(struct ndctl_namespace *ndns) +{ + struct ndctl_ctx *ctx = ndctl_namespace_get_ctx(ndns); + const char *argv[] = { + "__func__", "-v", "-m", "dax", "-M", "dev", "-f", "-e", "", + }; + int argc = ARRAY_SIZE(argv); + + argv[argc - 1] = ndctl_namespace_get_devname(ndns); + return create_namespace(argc, argv, ctx); +} -int main(int argc, char *argv[]) +static int test_device_dax(int loglevel, struct ndctl_test *test, + struct ndctl_ctx *ctx) { - char *buf; - int fd; + int fd, rc, *p; + char *buf, path[100]; + struct ndctl_dax *dax; + struct daxctl_dev *dev; + struct ndctl_namespace *ndns; + struct daxctl_region *dax_region; + + if (!ndctl_test_attempt(test, KERNEL_VERSION(4, 7, 0))) + return 77; + + ndctl_set_log_priority(ctx, loglevel); - if (argc < 2) { - perror("argc invalid"); - return -EINVAL; + ndns = ndctl_get_test_dev(ctx); + if (!ndns) { + fprintf(stderr, "%s: failed to find suitable namespace\n", + __func__); + return 77; } - fd = open(argv[1], O_RDWR); + rc = setup_device_dax(ndns); + if (rc < 0) { + fprintf(stderr, "%s: failed device-dax setup\n", + ndctl_namespace_get_devname(ndns)); + return rc; + } + + dax = ndctl_namespace_get_dax(ndns); + dax_region = ndctl_dax_get_daxctl_region(dax); + dev = daxctl_dev_get_first(dax_region); + if (!dev) { + fprintf(stderr, "%s: failed to find device-dax instance\n", + ndctl_namespace_get_devname(ndns)); + return -ENXIO; + } + + sprintf(path, "/dev/%s", daxctl_dev_get_devname(dev)); + + fd = open(path, O_RDWR); if (fd < 0) { - perror("fd"); - return 1; + fprintf(stderr, "%s: failed to open device-dax instance\n", + daxctl_dev_get_devname(dev)); + return -ENXIO; } buf = mmap(NULL, 2UL << 20, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); - *((int *) (buf + (1UL << 20))) = 0; + p = (int *) (buf + (1UL << 20)); + *p = 0; + + rc = reset_device_dax(ndns); + if (rc < 0) { + fprintf(stderr, "%s: failed to reset device-dax instance\n", + ndctl_namespace_get_devname(ndns)); + return rc; + } close(fd); return 0; } + +int __attribute__((weak)) main(int argc, char *argv[]) +{ + struct ndctl_test *test = ndctl_test_new(0); + struct ndctl_ctx *ctx; + int rc; + + if (!test) { + fprintf(stderr, "failed to initialize test\n"); + return EXIT_FAILURE; + } + + rc = ndctl_new(&ctx); + if (rc < 0) + return ndctl_test_result(test, rc); + + rc = test_device_dax(LOG_DEBUG, test, ctx); + ndctl_unref(ctx); + return ndctl_test_result(test, rc); +} diff --git a/test/device-dax.sh b/test/device-dax.sh deleted file mode 100755 index 6bc724fe7c95..000000000000 --- a/test/device-dax.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/bash -NDCTL="../ndctl/ndctl" -json2var="s/[{}\",]//g; s/:/=/g; s/\]//g" -rc=77 - -err() { - rc=$? - echo "device-dax: failed at line $1" - exit $rc -} - -eval $(uname -r | awk -F. '{print "maj="$1 ";" "min="$2}') -if [ $maj -lt 4 ]; then - echo "kernel $maj.$min lacks device-dax" - exit $rc -elif [ $maj -eq 4 -a $min -lt 7 ]; then - echo "kernel $maj.$min lacks device-dax" - exit $rc -fi - -set -e -x -trap 'err $LINENO' ERR - -dev=$(./dax-dev) -json=$($NDCTL list -N -n $dev) -eval $(echo $json | sed -e "$json2var") - -# setup a device-dax configuration -json=$($NDCTL create-namespace -v -m dax -M dev -f -e $dev) -eval $(echo $json | sed -e "$json2var") -[ $mode != "dax" ] && echo "fail: $LINENO" && exit 1 - -./device-dax /dev/$chardev - -# revert namespace to raw mode -json=$($NDCTL create-namespace -v -m raw -f -e $dev) -eval $(echo $json | sed -e "$json2var") -[ $mode != "memory" ] && echo "fail: $LINENO" && exit 1 - -exit 0