From patchwork Sat Jul 23 00:23:53 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Smart X-Patchwork-Id: 9244453 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 29C4E60459 for ; Sat, 23 Jul 2016 00:24:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1A7F3209CD for ; Sat, 23 Jul 2016 00:24:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0E02D22380; Sat, 23 Jul 2016 00:24:34 +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=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AD04B209CD for ; Sat, 23 Jul 2016 00:24:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752278AbcGWAX6 (ORCPT ); Fri, 22 Jul 2016 20:23:58 -0400 Received: from mail-pf0-f181.google.com ([209.85.192.181]:34569 "EHLO mail-pf0-f181.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751575AbcGWAXz (ORCPT ); Fri, 22 Jul 2016 20:23:55 -0400 Received: by mail-pf0-f181.google.com with SMTP id p64so45836892pfb.1 for ; Fri, 22 Jul 2016 17:23:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=date:from:to:subject:message-id:user-agent:mime-version :content-transfer-encoding; bh=8+vPY6Dx3eO2pxYzBkgBn6xkSGKcVCtqRSE/vvWKKho=; b=CHzgXdZNxSc7ZTlngCAqGDQQf3dWO84A4mEvuDiLqkYieBT9ilNMgQsQu4c7hso91U 0aHus5WHtC5uaj4fC8OxPVeqTcEOArgYlv+i21APYCHnFIC3jtBT/C6ISQBfRmHYOO/c ztW2Xb4Qc4XGbVQ3mbpYCGefbytuq8c/6+yyE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:subject:message-id:user-agent :mime-version:content-transfer-encoding; bh=8+vPY6Dx3eO2pxYzBkgBn6xkSGKcVCtqRSE/vvWKKho=; b=c804BwT3JX4uEK39R+LfI90nAT4f6E+vN8nsrsG0h4yljjTugR+Bqy4d88yLj3Z2n3 IHttJjLPiSZyFKsKSik8/9DGC754dmJthamicxqNj5uXm8IsEpE3vEMyBOD1oAvld2Qs SGcoaTHWX5nztZEr1P25JWnIQZXcGY2k/soUz9UBIvuxxiwapqPvfuNQbT//lA1Yu9qB YgwL9KZn+Tr3A6D0/aU/UcMPxLOMSVz94eqci2ZF3lfvLfLwDZo2weIkKm366Qw28YkT JRhhimcuvoExEzkBhlBUdNHE5v9v0e4RqIZdGHLJZjFykiOzCCDZO22+X8dlvF/09NEJ adow== X-Gm-Message-State: AEkoousMx4j9cbslRCHysfn707dfB/QPD1oIPlOt/Ly1rxP8ALjOG5p6DzhrZZOZ+TLfgiwu X-Received: by 10.98.74.201 with SMTP id c70mr11054420pfj.113.1469233434855; Fri, 22 Jul 2016 17:23:54 -0700 (PDT) Received: from flash ([192.19.224.250]) by smtp.gmail.com with ESMTPSA id x126sm16063058pfx.61.2016.07.22.17.23.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 22 Jul 2016 17:23:54 -0700 (PDT) Date: Fri, 22 Jul 2016 17:23:53 -0700 From: james.smart@broadcom.com (James Smart) To: linux-kernel@vger.kernel.org, linux-nvme@lists.infradead.org, linux-scsi@vger.kernel.org Subject: [PATCH] add u64 number parser Message-ID: <5792b919.0C1gyf+dF4XKu6Zj%james.smart@broadcom.com> User-Agent: Heirloom mailx 12.5 6/20/10 MIME-Version: 1.0 Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP add u64 number parser Will be used by the nvme-fabrics FC transport in parsing options Signed-off-by: James Smart Reviewed-by: Christoph Hellwig --- include/linux/parser.h | 1 + lib/parser.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/include/linux/parser.h b/include/linux/parser.h index 39d5b79..884c1e6 100644 --- a/include/linux/parser.h +++ b/include/linux/parser.h @@ -27,6 +27,7 @@ typedef struct { int match_token(char *, const match_table_t table, substring_t args[]); int match_int(substring_t *, int *result); +int match_u64(substring_t *, u64 *result); int match_octal(substring_t *, int *result); int match_hex(substring_t *, int *result); bool match_wildcard(const char *pattern, const char *str); diff --git a/lib/parser.c b/lib/parser.c index b6d1163..3278958 100644 --- a/lib/parser.c +++ b/lib/parser.c @@ -152,6 +152,36 @@ static int match_number(substring_t *s, int *result, int base) } /** + * match_u64int: scan a number in the given base from a substring_t + * @s: substring to be scanned + * @result: resulting u64 on success + * @base: base to use when converting string + * + * Description: Given a &substring_t and a base, attempts to parse the substring + * as a number in that base. On success, sets @result to the integer represented + * by the string and returns 0. Returns -ENOMEM, -EINVAL, or -ERANGE on failure. + */ +static int match_u64int(substring_t *s, u64 *result, int base) +{ + char *buf; + int ret; + u64 val; + size_t len = s->to - s->from; + + buf = kmalloc(len + 1, GFP_KERNEL); + if (!buf) + return -ENOMEM; + memcpy(buf, s->from, len); + buf[len] = '\0'; + + ret = kstrtoull(buf, base, &val); + if (!ret) + *result = val; + kfree(buf); + return ret; +} + +/** * match_int: - scan a decimal representation of an integer from a substring_t * @s: substring_t to be scanned * @result: resulting integer on success @@ -167,6 +197,23 @@ int match_int(substring_t *s, int *result) EXPORT_SYMBOL(match_int); /** + * match_u64: - scan a decimal representation of a u64 from + * a substring_t + * @s: substring_t to be scanned + * @result: resulting unsigned long long on success + * + * Description: Attempts to parse the &substring_t @s as a long decimal + * integer. On success, sets @result to the integer represented by the + * string and returns 0. + * Returns -ENOMEM, -EINVAL, or -ERANGE on failure. + */ +int match_u64(substring_t *s, u64 *result) +{ + return match_u64int(s, result, 0); +} +EXPORT_SYMBOL(match_u64); + +/** * match_octal: - scan an octal representation of an integer from a substring_t * @s: substring_t to be scanned * @result: resulting integer on success