From patchwork Tue Aug 7 22:24:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Keith Busch X-Patchwork-Id: 10559287 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 24FF296FA for ; Tue, 7 Aug 2018 22:24:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 148C72A505 for ; Tue, 7 Aug 2018 22:24:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 08E1E2A508; Tue, 7 Aug 2018 22:24:16 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, 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 678A52A52B for ; Tue, 7 Aug 2018 22:24:15 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 5FB6B210DF5D3; Tue, 7 Aug 2018 15:24:15 -0700 (PDT) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.93; helo=mga11.intel.com; envelope-from=keith.busch@intel.com; receiver=linux-nvdimm@lists.01.org Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) (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 9C31B210DF5C7 for ; Tue, 7 Aug 2018 15:24:12 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 07 Aug 2018 15:24:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,456,1526367600"; d="scan'208";a="74588757" Received: from unknown (HELO localhost.lm.intel.com) ([10.232.112.44]) by fmsmga002.fm.intel.com with ESMTP; 07 Aug 2018 15:24:11 -0700 From: Keith Busch To: Vishal Verma , Dave Jiang , linux-nvdimm@lists.01.org Subject: [PATCH 2/2] ndctl, intel: Pre-initialize smart shutdown_count Date: Tue, 7 Aug 2018 16:24:41 -0600 Message-Id: <20180807222441.14246-2-keith.busch@intel.com> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20180807222441.14246-1-keith.busch@intel.com> References: <20180807222441.14246-1-keith.busch@intel.com> X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" X-Virus-Scanned: ClamAV using ClamSMTP A user space rule saves the unsafe shutdown count to a fixed location for Intel dimms so that non-root users can read it. This patch uses that saved value if it is available in case the command can't be executed due to user permissions, allowing an application to detect if an unsafe shutdown occured during a write. Signed-off-by: Keith Busch --- ndctl/lib/intel.c | 42 +++++++++++++++++++++++++++++++++++++----- ndctl/util/json-smart.c | 4 ---- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/ndctl/lib/intel.c b/ndctl/lib/intel.c index 0abea1e..6df5aff 100644 --- a/ndctl/lib/intel.c +++ b/ndctl/lib/intel.c @@ -56,6 +56,35 @@ static struct ndctl_cmd *alloc_intel_cmd(struct ndctl_dimm *dimm, return cmd; } +/* + * If provided, prime the shutdown count with the saved value in case the user + * does not have access rights to run the smart command. + */ +static void intel_prime_shutdown_count(struct ndctl_dimm *dimm, + struct ndctl_cmd *cmd) +{ + char *path = NULL, shutdown_count[16] = {}; + int fd; + + if (asprintf(&path, DEF_TMPFS_DIR "/%s/usc", + ndctl_dimm_get_unique_id(dimm)) < 0) + return; + + fd = open(path, O_RDONLY); + if (fd < 0) + goto free_path; + + if (read(fd, shutdown_count, sizeof(shutdown_count)) < 0) + goto close; + + cmd->intel->smart.flags |= ND_INTEL_SMART_SHUTDOWN_COUNT_VALID; + cmd->intel->smart.shutdown_count = strtoull(shutdown_count, NULL, 0); + close: + close (fd); + free_path: + free(path); +} + static struct ndctl_cmd *intel_dimm_cmd_new_smart(struct ndctl_dimm *dimm) { struct ndctl_cmd *cmd; @@ -67,7 +96,7 @@ static struct ndctl_cmd *intel_dimm_cmd_new_smart(struct ndctl_dimm *dimm) if (!cmd) return NULL; cmd->firmware_status = &cmd->intel->smart.status; - + intel_prime_shutdown_count(dimm, cmd); return cmd; } @@ -95,9 +124,6 @@ static unsigned int intel_cmd_smart_get_flags(struct ndctl_cmd *cmd) unsigned int flags = 0; unsigned int intel_flags; - if (intel_smart_valid(cmd) < 0) - return 0; - /* translate intel specific flags to libndctl api smart flags */ intel_flags = cmd->intel->smart.flags; if (intel_flags & ND_INTEL_SMART_HEALTH_VALID) @@ -142,13 +168,19 @@ static unsigned int intel_cmd_smart_get_health(struct ndctl_cmd *cmd) return health; } +static unsigned int intel_cmd_smart_get_shutdown_count(struct ndctl_cmd *cmd) +{ + if (cmd->intel->smart.flags & ND_INTEL_SMART_SHUTDOWN_COUNT_VALID) + return cmd->intel->smart.shutdown_count; + return UINT_MAX; +} + intel_smart_get_field(cmd, media_temperature) intel_smart_get_field(cmd, ctrl_temperature) intel_smart_get_field(cmd, spares) intel_smart_get_field(cmd, alarm_flags) intel_smart_get_field(cmd, life_used) intel_smart_get_field(cmd, shutdown_state) -intel_smart_get_field(cmd, shutdown_count) intel_smart_get_field(cmd, vendor_size) static unsigned char *intel_cmd_smart_get_vendor_data(struct ndctl_cmd *cmd) diff --git a/ndctl/util/json-smart.c b/ndctl/util/json-smart.c index 6a1f294..5f43982 100644 --- a/ndctl/util/json-smart.c +++ b/ndctl/util/json-smart.c @@ -93,7 +93,6 @@ struct json_object *util_dimm_health_to_json(struct ndctl_dimm *dimm) jobj = json_object_new_string("unknown"); if (jobj) json_object_object_add(jhealth, "health_state", jobj); - goto out; } flags = ndctl_cmd_smart_get_flags(cmd); @@ -189,8 +188,5 @@ struct json_object *util_dimm_health_to_json(struct ndctl_dimm *dimm) err: json_object_put(jhealth); jhealth = NULL; - out: - if (cmd) - ndctl_cmd_unref(cmd); return jhealth; }