From patchwork Thu May 11 17:37:08 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 9722621 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 12E3560364 for ; Thu, 11 May 2017 17:37:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EB6831FFAD for ; Thu, 11 May 2017 17:37:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DD24320683; Thu, 11 May 2017 17:37:12 +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 B873B28681 for ; Thu, 11 May 2017 17:37:11 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 1C72621A13493; Thu, 11 May 2017 10:37:11 -0700 (PDT) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 034D921A16E36 for ; Thu, 11 May 2017 10:37:09 -0700 (PDT) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 11 May 2017 10:37:09 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.38,325,1491289200"; d="scan'208";a="85687860" Received: from djiang5-desk3.ch.intel.com ([143.182.137.38]) by orsmga002.jf.intel.com with ESMTP; 11 May 2017 10:37:08 -0700 Subject: [PATCH v5 1/2] ndctl: add foreach helper for region badblocks in libndctl From: Dave Jiang To: dan.j.williams@intel.com Date: Thu, 11 May 2017 10:37:08 -0700 Message-ID: <149452421176.25868.9410889126303443053.stgit@djiang5-desk3.ch.intel.com> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-nvdimm@lists.01.org Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" X-Virus-Scanned: ClamAV using ClamSMTP The helper function allow iteration through the badblocks file that's part of the region sysfs attributes. This will support the region list badblocks code that's coming. Signed-off-by: Dave Jiang --- v2: added cleanup of opened badblocks file, from Dan's comments. v3: fixed inconsistent return code, from Andy Rudoff's comments. v4: removed unnecessary code comment, from Vishal's comments. v5: no change. ndctl/lib/libndctl.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ ndctl/lib/libndctl.sym | 2 + ndctl/libndctl.h.in | 10 +++++++ 3 files changed, 85 insertions(+) diff --git a/ndctl/lib/libndctl.c b/ndctl/lib/libndctl.c index ac1fc63..4acebc0 100644 --- a/ndctl/lib/libndctl.c +++ b/ndctl/lib/libndctl.c @@ -229,6 +229,8 @@ struct ndctl_region { int state; unsigned long long cookie; } iset; + FILE *badblocks; + struct badblock bb; }; /** @@ -594,6 +596,8 @@ static void free_region(struct ndctl_region *region) kmod_module_unref(region->module); free(region->region_buf); free(region->region_path); + if (region->badblocks) + fclose(region->badblocks); free(region); } @@ -1867,6 +1871,75 @@ NDCTL_EXPORT struct ndctl_dimm *ndctl_region_get_next_dimm(struct ndctl_region * return NULL; } +static int regions_badblocks_init(struct ndctl_region *region) +{ + struct ndctl_ctx *ctx = ndctl_region_get_ctx(region); + char *bb_path; + int rc = 0; + + /* if the file is already open */ + if (region->badblocks) { + fclose(region->badblocks); + region->badblocks = NULL; + } + + if (asprintf(&bb_path, "%s/badblocks", + region->region_path) < 0) { + rc = -errno; + err(ctx, "region badblocks path allocation failure\n"); + return rc; + } + + region->badblocks = fopen(bb_path, "re"); + if (!region->badblocks) { + rc = -errno; + free(bb_path); + return rc; + } + + free(bb_path); + return rc; +} + +NDCTL_EXPORT struct badblock *ndctl_region_get_next_badblock(struct ndctl_region *region) +{ + int rc; + char *buf = NULL; + size_t rlen = 0; + + if (!region->badblocks) + return NULL; + + rc = getline(&buf, &rlen, region->badblocks); + if (rc == -1) { + free(buf); + return NULL; + } + + rc = sscanf(buf, "%llu %u", ®ion->bb.offset, ®ion->bb.len); + free(buf); + if (rc != 2) { + fclose(region->badblocks); + region->badblocks = NULL; + region->bb.offset = 0; + region->bb.len = 0; + return NULL; + } + + return ®ion->bb; +} + +NDCTL_EXPORT struct badblock *ndctl_region_get_first_badblock(struct ndctl_region *region) +{ + int rc; + + rc = regions_badblocks_init(region); + if (rc < 0) + return NULL; + + return ndctl_region_get_next_badblock(region); +} + static struct nd_cmd_vendor_tail *to_vendor_tail(struct ndctl_cmd *cmd) { struct nd_cmd_vendor_tail *tail = (struct nd_cmd_vendor_tail *) diff --git a/ndctl/lib/libndctl.sym b/ndctl/lib/libndctl.sym index b5a085c..9bc36a3 100644 --- a/ndctl/lib/libndctl.sym +++ b/ndctl/lib/libndctl.sym @@ -140,6 +140,8 @@ global: ndctl_region_get_ro; ndctl_region_set_ro; ndctl_region_get_resource; + ndctl_region_get_first_badblock; + ndctl_region_get_next_badblock; ndctl_interleave_set_get_first; ndctl_interleave_set_get_next; ndctl_interleave_set_is_active; diff --git a/ndctl/libndctl.h.in b/ndctl/libndctl.h.in index 6ee8a35..2c45d2d 100644 --- a/ndctl/libndctl.h.in +++ b/ndctl/libndctl.h.in @@ -372,6 +372,10 @@ int ndctl_cmd_get_status(struct ndctl_cmd *cmd); unsigned int ndctl_cmd_get_firmware_status(struct ndctl_cmd *cmd); int ndctl_cmd_submit(struct ndctl_cmd *cmd); +struct badblock { + unsigned long long offset; + unsigned int len; +}; struct ndctl_region; struct ndctl_region *ndctl_region_get_first(struct ndctl_bus *bus); struct ndctl_region *ndctl_region_get_next(struct ndctl_region *region); @@ -379,6 +383,12 @@ struct ndctl_region *ndctl_region_get_next(struct ndctl_region *region); for (region = ndctl_region_get_first(bus); \ region != NULL; \ region = ndctl_region_get_next(region)) +struct badblock *ndctl_region_get_first_badblock(struct ndctl_region *region); +struct badblock *ndctl_region_get_next_badblock(struct ndctl_region *region); +#define ndctl_region_badblock_foreach(region, badblock) \ + for (badblock = ndctl_region_get_first_badblock(region); \ + badblock != NULL; \ + badblock = ndctl_region_get_next_badblock(region)) unsigned int ndctl_region_get_id(struct ndctl_region *region); const char *ndctl_region_get_devname(struct ndctl_region *region); unsigned int ndctl_region_get_interleave_ways(struct ndctl_region *region);