From mboxrd@z Thu Jan 1 00:00:00 1970 From: Matthew Wilcox Subject: [PATCH v11 11/63] xarray: Add xa_extract Date: Sat, 14 Apr 2018 07:12:24 -0700 Message-ID: <20180414141316.7167-12-willy@infradead.org> References: <20180414141316.7167-1-willy@infradead.org> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; 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=Wq8SnqK0kN1GjxP5l2/9eSugIZT7zGoWlq8KGznm/68=; b=NCEUi/K9RpUZSG6gJPavadykbP r2MmKxemUcU0T8hLD5CnqR8XheQb2uq24wXXTMS/54c7ZoLdj+pTvECtaWh77tz/frFrnKUGBNgOq NsL7AXYBxtz3McsKgbTuB4D6pyu96omVD7B/hIaazg7N+qRN4iviIyEuGmIAcDFXK40o=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; 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=Wq8SnqK0kN1GjxP5l2/9eSugIZT7zGoWlq8KGznm/68=; b=IVZiyQdb1UUgnsOUrFm9z1Krru NHMeiop2MhpLGDmeue3xlIgFa3o4Nrp9/8s8GTenYJGBmDB9lNYuzl+GuR2cbXOcArzgRrem4RCDp twAM7gZQ2xQYnRXWATf+oyy9SftEX0be02jJOIaH7qyCUSE6F0x6uM4g4EwWdCFZz1dw=; 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=Wq8SnqK0kN1GjxP5l2/9eSugIZT7zGoWlq8KGznm/68=; b=Af+HX7DoTqtSHbaKdqZJAv1Cu DCATU6LRkmj5RD/shi9QjXcrXg4njaKLb00u6X77ZIEW2W5XkVQ4UAwmq6Apw3FILntf1aHz3B5GG 2uFM7tv4VcNW9Wcg7LxPybEGCC2zJU1BhVWPNJ/HrZRKmyROojQIiNdexGcQdXP5XvN0sl++Ut3iD QnZbMntF7pCjVssZfRnXTgLjgocDt3Scu7vOkwsX8d1qMarh7WV2jDlJzF5Jz7OS8qC89z8P4evZG RcqRdYLAHcuxVmlhgpRT5QvsHtoZ6Uhd2USQ9dnKl82Hb1z3NjfLBYIIulEF3V6khOhxq/I5CUSOU nqm69USzg==; In-Reply-To: <20180414141316.7167-1-willy@infradead.org> List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: linux-f2fs-devel-bounces@lists.sourceforge.net To: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org Cc: linux-nilfs@vger.kernel.org, Jan Kara , Jeff Layton , Matthew Wilcox , James Simmons , Jaegeuk Kim , Andreas Dilger , Nicholas Piggin , linux-f2fs-devel@lists.sourceforge.net, Oleg Drokin , Ryusuke Konishi , Lukas Czerner , Ross Zwisler , Christoph Hellwig , Goldwyn Rodrigues , Mike Kravetz From: Matthew Wilcox This function combines the functionality of radix_tree_gang_lookup() and radix_tree_gang_lookup_tagged(). It extracts entries matching the specified filter into a normal array. Signed-off-by: Matthew Wilcox --- include/linux/xarray.h | 2 ++ lib/xarray.c | 80 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/include/linux/xarray.h b/include/linux/xarray.h index 4162dadef9b2..4ee606957019 100644 --- a/include/linux/xarray.h +++ b/include/linux/xarray.h @@ -227,6 +227,8 @@ void *xa_find(struct xarray *xa, unsigned long *index, unsigned long max, xa_tag_t) __attribute__((nonnull(2))); void *xa_find_after(struct xarray *xa, unsigned long *index, unsigned long max, xa_tag_t) __attribute__((nonnull(2))); +unsigned int xa_extract(struct xarray *, void **dst, unsigned long start, + unsigned long max, unsigned int n, xa_tag_t); /** * xa_init() - Initialise an empty XArray. diff --git a/lib/xarray.c b/lib/xarray.c index 73cf4c984c2d..77133bc5419f 100644 --- a/lib/xarray.c +++ b/lib/xarray.c @@ -1402,6 +1402,86 @@ void *xa_find_after(struct xarray *xa, unsigned long *indexp, } EXPORT_SYMBOL(xa_find_after); +static unsigned int xas_extract_present(struct xa_state *xas, void **dst, + unsigned long max, unsigned int n) +{ + void *entry; + unsigned int i = 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; +} + +static unsigned int xas_extract_tag(struct xa_state *xas, void **dst, + unsigned long max, unsigned int n, xa_tag_t tag) +{ + void *entry; + unsigned int i = 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; +} + +/** + * xa_extract() - Copy selected entries from the XArray into a normal array. + * @xa: The source XArray to copy from. + * @dst: The buffer to copy entries into. + * @start: The first index in the XArray eligible to be selected. + * @max: The last index in the XArray eligible to be selected. + * @n: The maximum number of entries to copy. + * @filter: Selection criterion. + * + * Copies up to @n entries that match @filter from the XArray. The + * copied entries will have indices between @start and @max, inclusive. + * + * The @filter may be an XArray tag value, in which case entries which are + * tagged with that tag will be copied. It may also be %XA_PRESENT, in + * which case non-NULL entries will be copied. + * + * The entries returned may not represent a snapshot of the XArray at a + * moment in time. For example, if another thread stores to index 5, then + * index 10, calling xa_extract() 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. + * + * Context: Any context. Takes and releases the RCU lock. + * Return: The number of entries copied. + */ +unsigned int xa_extract(struct xarray *xa, void **dst, unsigned long start, + unsigned long max, unsigned int n, xa_tag_t filter) +{ + XA_STATE(xas, xa, start); + + if (!n) + return 0; + + if ((__force unsigned int)filter < XA_MAX_TAGS) + return xas_extract_tag(&xas, dst, max, n, filter); + return xas_extract_present(&xas, dst, max, n); +} +EXPORT_SYMBOL(xa_extract); + #ifdef XA_DEBUG void xa_dump_node(const struct xa_node *node) { -- 2.17.0 ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot