Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp4201876ioo; Wed, 25 May 2022 18:15:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzg8cTXE6D1Wa59E7d9rC4CUGn4uXqq0b8OOozDFiSjbLC5qieOQx4YOSi2gaR9///EYcLw X-Received: by 2002:a17:903:2304:b0:162:ed1:ed9c with SMTP id d4-20020a170903230400b001620ed1ed9cmr21664007plh.122.1653527730222; Wed, 25 May 2022 18:15:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653527730; cv=none; d=google.com; s=arc-20160816; b=cEUQa//IU8zhKHfNi8yGg9FtFOIPUQ8eqdlhzJ3PHJDaDvGJmoFzjxCY1TtLiiO56o dYMbAH/01J8m9bh9ZlSS/Z2Ggb63EcmwAC79jEVEvZVib4f8377RidyJFu2xlD4osmwk Y7bLmEsXbd+72tvv0iJBKXXjZMdMH1Wawy9KwipBW7Fqv++z4gjKuHvFvFi3dMUvKEQ6 IzNL5mUuj9C2xDq9viQgT39NXodzazFNXZAtGU5CsRkGwYzHlphtF2f4/8Z7g+KHgTGx aKgrXJVDilffHbK+pdmuqZ+HNKbisqvcCbR1OqJYFXkTjcWnnyscXCSBwyOAIehc0kif +woA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:cc:to:from :subject:organization:dkim-signature; bh=81g6TLubS0BUdbR5ajxUofL0J/LtNw5cIh2f43naGp0=; b=cWHSq3WeQtaTIw8X2SAyyvNqKIcbADVxAzRR+lmltQD4owx8wRoqPLR+DgSdrlfW1x HLA7NuB/fz1mPl9BgJlKLaEb9F0d5fNgqdEAT6BxWcw6bwGz5cYmDkxn5llNiZxw16lc 2isCmhxhSqk/1ogubwyAR9Ak5kKNTR0S0t8jPGXJJV+/yeget8KO7mEhLd3V9MTIdRkW FPbxEJy73UvU2MmqLkDkbRzKlTA3hLdWDfg+lPa6cfRmLsSb9IpLu+D5urzgKhViGQz4 vVJP7qgK1hQDboRxgcAq/QLuKxRHX7F8e97g90KdwObed7EAlMADG8exhZH4Z4fWoGCr hzqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="DXc/V64h"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 196-20020a6216cd000000b0050a96ba4e5esi47205pfw.290.2022.05.25.18.15.17; Wed, 25 May 2022 18:15:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="DXc/V64h"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244802AbiEYO0r (ORCPT + 99 others); Wed, 25 May 2022 10:26:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55118 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244807AbiEYO0d (ORCPT ); Wed, 25 May 2022 10:26:33 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0346911151 for ; Wed, 25 May 2022 07:26:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1653488788; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=81g6TLubS0BUdbR5ajxUofL0J/LtNw5cIh2f43naGp0=; b=DXc/V64hEJgvLVh4ZWREA7mkkC7k9M62WoBphcOqdhNmQ84NX5d4syJaz6z2E3vYvfYNLd jjScDDgc9V43LQ++M/OPz5RsbSx1/1h066Jb6YwfiqNPFwsCBYxyk4akBLTOBiUh9W0ssU 0QQ+kA2b0sk6MKF8F1ue52oZ8kxTAFg= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-178-bvT2x6thOt-o64zYp2NbJw-1; Wed, 25 May 2022 10:26:24 -0400 X-MC-Unique: bvT2x6thOt-o64zYp2NbJw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3A8033802B88; Wed, 25 May 2022 14:26:24 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.8]) by smtp.corp.redhat.com (Postfix) with ESMTP id 13BB140CFD0A; Wed, 25 May 2022 14:26:22 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 Subject: [PATCH 2/7] iov_iter: Add a general purpose iteration function From: David Howells To: Steve French Cc: dhowells@redhat.com, Shyam Prasad N , Rohith Surabattula , Jeff Layton , Al Viro , linux-cifs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Date: Wed, 25 May 2022 15:26:22 +0100 Message-ID: <165348878240.2106726.14850277156941003064.stgit@warthog.procyon.org.uk> In-Reply-To: <165348876794.2106726.9240233279581920208.stgit@warthog.procyon.org.uk> References: <165348876794.2106726.9240233279581920208.stgit@warthog.procyon.org.uk> User-Agent: StGit/1.4 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a function, iov_iter_scan(), to iterate over the buffers described by an I/O iterator, kmapping and passing each contiguous chunk the supplied scanner function in turn, up to the requested amount of data or until the scanner function returns an error. This can be used, for example, to hash all the data in an iterator by having the scanner function call the appropriate crypto update function. Signed-off-by: David Howells --- include/linux/uio.h | 4 ++++ lib/iov_iter.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/include/linux/uio.h b/include/linux/uio.h index 5a3c6f296b96..8d89203064e7 100644 --- a/include/linux/uio.h +++ b/include/linux/uio.h @@ -244,6 +244,10 @@ int iov_iter_npages(const struct iov_iter *i, int maxpages); void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state); const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags); +ssize_t iov_iter_scan(struct iov_iter *i, size_t bytes, + ssize_t (*scanner)(struct iov_iter *i, const void *p, + size_t len, size_t off, void *priv), + void *priv); static inline size_t iov_iter_count(const struct iov_iter *i) { diff --git a/lib/iov_iter.c b/lib/iov_iter.c index 8db34ddd23be..834e1e268eb6 100644 --- a/lib/iov_iter.c +++ b/lib/iov_iter.c @@ -1789,6 +1789,46 @@ ssize_t extract_iter_to_iter(struct iov_iter *orig, } EXPORT_SYMBOL(extract_iter_to_iter); +/** + * iov_iter_scan - Scan a source iter + * @i: The iterator to scan + * @bytes: The amount of buffer/data to scan + * @scanner: The function to call for each bit + * @priv: Private data to pass to the scanner function + * + * Scan an iterator, passing each segment to the scanner function. If the + * scanner returns an error at any time, scanning stops and the error is + * returned, otherwise the sum of the scanner results is returned. + */ +ssize_t iov_iter_scan(struct iov_iter *i, size_t bytes, + ssize_t (*scanner)(struct iov_iter *i, const void *p, + size_t len, size_t off, void *priv), + void *priv) +{ + ssize_t ret = 0, scanned = 0; + + if (!bytes) + return 0; + if (iter_is_iovec(i)) + might_fault(); + + iterate_and_advance( + i, bytes, base, len, off, ({ + ret = scanner(i, base, len, off, priv); + if (ret < 0) + break; + scanned += ret; + }), ({ + ret = scanner(i, base, len, off, priv); + if (ret < 0) + break; + scanned += ret; + }) + ); + return ret < 0 ? ret : scanned; +} +EXPORT_SYMBOL(iov_iter_scan); + size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i) {