From patchwork Wed Aug 8 15:59:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Keith Busch X-Patchwork-Id: 10560331 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 27A1C139A for ; Wed, 8 Aug 2018 15:59:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1401F2B195 for ; Wed, 8 Aug 2018 15:59:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0642D2B19B; Wed, 8 Aug 2018 15:59:17 +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 AF9522B18F for ; Wed, 8 Aug 2018 15:59:16 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 51043210E12B9; Wed, 8 Aug 2018 08:59:16 -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.151; helo=mga17.intel.com; envelope-from=keith.busch@intel.com; receiver=linux-nvdimm@lists.01.org Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) (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 7AC6A210E129F for ; Wed, 8 Aug 2018 08:59:14 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga107.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 08 Aug 2018 08:59:14 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.53,458,1531810800"; d="scan'208";a="80005222" Received: from unknown (HELO localhost.lm.intel.com) ([10.232.112.44]) by orsmga001.jf.intel.com with ESMTP; 08 Aug 2018 08:59:13 -0700 From: Keith Busch To: Vishal Verma , Dave Jiang , linux-nvdimm@lists.01.org Subject: [ndctl PATCHv2 2/2] ndctl, intel: Fallback to smart cached shutdown_count Date: Wed, 8 Aug 2018 09:59:34 -0600 Message-Id: <20180808155934.15976-2-keith.busch@intel.com> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20180808155934.15976-1-keith.busch@intel.com> References: <20180808155934.15976-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. If the smart command submission fails, this patch uses that saved value in a newly added ndctl_cmd "handle_error" callback. If the cached value is available, the error handler will override the command status to success, and set the valid flags for the shutdown count. Signed-off-by: Keith Busch --- v1 -> v2: Use nmem instead of dimm-id ndctl/lib/intel.c | 33 +++++++++++++++++++++++++++++++++ ndctl/lib/libndctl.c | 2 ++ ndctl/lib/private.h | 1 + 3 files changed, 36 insertions(+) diff --git a/ndctl/lib/intel.c b/ndctl/lib/intel.c index 0abea1e..00c65a5 100644 --- a/ndctl/lib/intel.c +++ b/ndctl/lib/intel.c @@ -56,6 +56,38 @@ static struct ndctl_cmd *alloc_intel_cmd(struct ndctl_dimm *dimm, return cmd; } +/* + * If provided, read the cached shutdown count in case the user does not have + * access rights to run the smart command, and pretend the command was + * successful with only the shutdown_count valid. + */ +static int intel_smart_handle_error(struct ndctl_cmd *cmd) +{ + struct ndctl_dimm *dimm = cmd->dimm; + char *path = NULL, shutdown_count[16] = {}; + int fd, rc = cmd->status; + + if (asprintf(&path, DEF_TMPFS_DIR "/%s/usc", + ndctl_dimm_get_devname(dimm)) < 0) + return rc; + + 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); + rc = cmd->status = 0; + close: + close (fd); + free_path: + free(path); + return rc; +} + static struct ndctl_cmd *intel_dimm_cmd_new_smart(struct ndctl_dimm *dimm) { struct ndctl_cmd *cmd; @@ -67,6 +99,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; + cmd->handle_error = intel_smart_handle_error; return cmd; } diff --git a/ndctl/lib/libndctl.c b/ndctl/lib/libndctl.c index e911ea2..ab47b27 100644 --- a/ndctl/lib/libndctl.c +++ b/ndctl/lib/libndctl.c @@ -2766,6 +2766,8 @@ NDCTL_EXPORT int ndctl_cmd_submit(struct ndctl_cmd *cmd) close(fd); out: cmd->status = rc; + if (rc && cmd->handle_error) + rc = cmd->handle_error(cmd); return rc; } diff --git a/ndctl/lib/private.h b/ndctl/lib/private.h index 5ddc682..c6eb39e 100644 --- a/ndctl/lib/private.h +++ b/ndctl/lib/private.h @@ -255,6 +255,7 @@ struct ndctl_cmd { int dir; } iter; struct ndctl_cmd *source; + int (*handle_error)(struct ndctl_cmd *cmd); union { struct nd_cmd_ars_cap ars_cap[0]; struct nd_cmd_ars_start ars_start[0];