From patchwork Wed Dec 6 00:41:01 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 10094435 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 76CCC60329 for ; Wed, 6 Dec 2017 00:52:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 67D4E28A88 for ; Wed, 6 Dec 2017 00:52:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5CB9D28C30; Wed, 6 Dec 2017 00:52:31 +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 BCFDD29953 for ; Wed, 6 Dec 2017 00:52:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753789AbdLFAvW (ORCPT ); Tue, 5 Dec 2017 19:51:22 -0500 Received: from bombadil.infradead.org ([65.50.211.133]:36318 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753364AbdLFAmO (ORCPT ); Tue, 5 Dec 2017 19:42:14 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=References:In-Reply-To:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=chiftpdumpRGKsEX4yu+ZD4c4KgFddEHIQBrNvx8WxA=; b=N6eB0Db2QC8OtFFyBMyIooCUp +KDHgastxTWPJzAsrOLNJ5anltubrPN//KZdr4AXX8UTeq3zJVK8E9G54HZ6e+hZJxoflG9uZXJ1z ELJxEehz0+O+5CJN589sL3XrhshbHsfKC3eP/6yGugsOYMGR1KyhOYgdgXKDZVlf7e1oCfur/qcsU 2dMamvmVBRi3x/XiPAP9vIkPkee2rhgojcu9hoMrCi0TMwHIaillaQn3vZSv3DGhMoRZ/IB/sryFc iHL6lyRVszBlXd15ktDYVkKmw82vPBeKYCo+OaLsfYJ72viXX9ItOsXo13A8YNmlg01XXp5r9PmOr ao18QOBLw==; Received: from willy by bombadil.infradead.org with local (Exim 4.87 #1 (Red Hat Linux)) id 1eMNmp-000124-1x; Wed, 06 Dec 2017 00:42:07 +0000 From: Matthew Wilcox Cc: Matthew Wilcox , Ross Zwisler , Jens Axboe , Rehas Sachdeva , linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-nilfs@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-xfs@vger.kernel.org, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 15/73] xarray: Add xa_get_entries, xa_get_tagged and xa_get_maybe_tag Date: Tue, 5 Dec 2017 16:41:01 -0800 Message-Id: <20171206004159.3755-16-willy@infradead.org> X-Mailer: git-send-email 2.9.5 In-Reply-To: <20171206004159.3755-1-willy@infradead.org> References: <20171206004159.3755-1-willy@infradead.org> To: unlisted-recipients:; (no To-header on input) Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Matthew Wilcox These functions allow a range of xarray entries to be extracted into a compact normal array. Signed-off-by: Matthew Wilcox --- include/linux/xarray.h | 27 ++++++++++++++++ lib/xarray.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/include/linux/xarray.h b/include/linux/xarray.h index 4e61ebd406f5..c3efcc3432f7 100644 --- a/include/linux/xarray.h +++ b/include/linux/xarray.h @@ -135,6 +135,33 @@ void *xa_clear_tag(struct xarray *, unsigned long index, xa_tag_t); void *xa_find(struct xarray *xa, unsigned long *index, unsigned long max); void *xa_find_after(struct xarray *xa, unsigned long *index, unsigned long max); +int xa_get_entries(struct xarray *, void **dst, unsigned long start, + unsigned long max, unsigned int n); +int xa_get_tagged(struct xarray *, void **dst, unsigned long start, + unsigned long max, unsigned int n, xa_tag_t); + +/** + * xa_get_maybe_tag() - Copy entries from the XArray into a normal array. + * @xa: The source XArray to copy from. + * @dst: The buffer to copy pointers into. + * @start: The first index in the XArray eligible to be copied from. + * @max: The last index in the XArray eligible to be copied from. + * @n: The maximum number of entries to copy. + * @tag: Tag number. + * + * If you specify %XA_NO_TAG as the tag number, this is the same as + * xa_get_entries(). Otherwise, it is the same as xa_get_tagged(). + * + * Return: The number of entries copied. + */ +static inline int xa_get_maybe_tag(struct xarray *xa, void **dst, + unsigned long start, unsigned long max, + unsigned int n, xa_tag_t tag) +{ + if (tag == XA_NO_TAG) + return xa_get_entries(xa, dst, start, max, n); + return xa_get_tagged(xa, dst, start, max, n, tag); +} /** * xa_for_each() - Iterate over a portion of an XArray. diff --git a/lib/xarray.c b/lib/xarray.c index f9eaac2d85f9..251724f62b11 100644 --- a/lib/xarray.c +++ b/lib/xarray.c @@ -1253,6 +1253,94 @@ void *xa_find_after(struct xarray *xa, unsigned long *indexp, unsigned long max) } EXPORT_SYMBOL(xa_find_after); +/** + * xa_get_entries() - Copy entries from the XArray into a normal array. + * @xa: The source XArray to copy from. + * @dst: The buffer to copy pointers into. + * @start: The first index in the XArray eligible to be copied from. + * @max: The last index in the XArray eligible to be copied from. + * @n: The maximum number of entries to copy. + * + * Copies up to @n non-NULL entries from the XArray. The copied entries will + * have indices between @start and @max, inclusive. + * + * This function uses the RCU lock to protect itself. That means that the + * entries returned may not represent a snapshot of the XArray at a moment + * in time. For example, if index 5 is stored to, then index 10 is stored to, + * calling xa_get_entries() may return the old contents of index 5 and the + * new contents of index 10. Indices not modified while this function is + * running will not be skipped. + * + * If you need stronger guarantees, holding the xa_lock across calls to this + * function will prevent concurrent modification. + * + * Return: The number of entries copied. + */ +int xa_get_entries(struct xarray *xa, void **dst, unsigned long start, + unsigned long max, unsigned int n) +{ + XA_STATE(xas, xa, start); + void *entry; + unsigned int i = 0; + + if (!n) + return 0; + + rcu_read_lock(); + xas_for_each(&xas, entry, max) { + if (xas_retry(&xas, entry)) + continue; + dst[i++] = entry; + if (i == n) + break; + } + rcu_read_unlock(); + + return i; +} +EXPORT_SYMBOL(xa_get_entries); + +/** + * xa_get_tagged() - Copy tagged entries from the XArray into a normal array. + * @xa: The source XArray to copy from. + * @dst: The buffer to copy pointers into. + * @start: The first index in the XArray eligible to be copied from. + * @max: The last index in the XArray eligible to be copied from + * @n: The maximum number of entries to copy. + * @tag: Tag number. + * + * Copies up to @n non-NULL entries that have @tag set from the XArray. The + * copied entries will have indices between @start and @max, inclusive. + * + * See the xa_get_entries() documentation for the consistency guarantees + * provided. + * + * Return: The number of entries copied. + */ +int xa_get_tagged(struct xarray *xa, void **dst, unsigned long start, + unsigned long max, unsigned int n, xa_tag_t tag) +{ + XA_STATE(xas, xa, start); + void *entry; + unsigned int i = 0; + + if (!n) + return 0; + + rcu_read_lock(); + xas_for_each_tag(&xas, entry, max, tag) { + if (xas_retry(&xas, entry)) + continue; + dst[i++] = entry; + if (i == n) + break; + } + rcu_read_unlock(); + + return i; +} +EXPORT_SYMBOL(xa_get_tagged); + #ifdef XA_DEBUG void xa_dump_entry(void *entry, unsigned long index) {