From: Christoph Hellwig <hch@lst.de>
To: Chandan Babu R <chandan.babu@oracle.com>,
Matthew Wilcox <willy@infradead.org>
Cc: "Darrick J. Wong" <djwong@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
linux-xfs@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-fsdevel@vger.kernel.org
Subject: [PATCH 1/3] xarray: add xa_set
Date: Mon, 12 Aug 2024 08:31:00 +0200 [thread overview]
Message-ID: <20240812063143.3806677-2-hch@lst.de> (raw)
In-Reply-To: <20240812063143.3806677-1-hch@lst.de>
Add a convenience wrapper or xa_store that returns an error value when
there is an existing entry instead of the old entry. This simplifies
code that wants to check that it is never overwriting an existing
entry.
Signed-off-by: Christoph Hellwig <hch@lst.de>
---
include/linux/xarray.h | 1 +
lib/xarray.c | 33 +++++++++++++++++++++++++++++++++
2 files changed, 34 insertions(+)
diff --git a/include/linux/xarray.h b/include/linux/xarray.h
index 0b618ec04115fc..8dc4e575378ca5 100644
--- a/include/linux/xarray.h
+++ b/include/linux/xarray.h
@@ -354,6 +354,7 @@ struct xarray {
void *xa_load(struct xarray *, unsigned long index);
void *xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
+int xa_set(struct xarray *, unsigned long index, void *entry, gfp_t);
void *xa_erase(struct xarray *, unsigned long index);
void *xa_store_range(struct xarray *, unsigned long first, unsigned long last,
void *entry, gfp_t);
diff --git a/lib/xarray.c b/lib/xarray.c
index 32d4bac8c94ca1..500d3405c0c8c0 100644
--- a/lib/xarray.c
+++ b/lib/xarray.c
@@ -1600,6 +1600,39 @@ void *xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
}
EXPORT_SYMBOL(xa_store);
+/**
+ * xa_set() - Store this entry in the XArray.
+ * @xa: XArray.
+ * @index: Index into array.
+ * @entry: New entry.
+ * @gfp: Memory allocation flags.
+ *
+ * After this function returns, loads from this index will return @entry.
+ * Storing into an existing multi-index entry updates the entry of every index.
+ * The marks associated with @index are unaffected unless @entry is %NULL.
+ *
+ * Context: Any context. Takes and releases the xa_lock.
+ * May sleep if the @gfp flags permit.
+ * Return: 0 on success, -EEXIST if there already is an entry at @index, -EINVAL
+ * if @entry cannot be stored in an XArray, or -ENOMEM if memory allocation
+ * failed.
+ */
+int xa_set(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
+{
+ int error = 0;
+ void *curr;
+
+ curr = xa_store(xa, index, entry, gfp);
+ if (curr) {
+ error = xa_err(curr);
+ if (error == 0)
+ error = -ENOENT;
+ }
+
+ return error;
+}
+EXPORT_SYMBOL(xa_set);
+
/**
* __xa_cmpxchg() - Store this entry in the XArray.
* @xa: XArray.
--
2.43.0
next prev parent reply other threads:[~2024-08-12 6:31 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-12 6:30 conver XFS perag lookup to xarrays Christoph Hellwig
2024-08-12 6:31 ` Christoph Hellwig [this message]
2024-08-12 12:25 ` [PATCH 1/3] xarray: add xa_set Matthew Wilcox
2024-08-12 12:28 ` Christoph Hellwig
2024-08-12 6:31 ` [PATCH 2/3] xfs: convert perag lookup to xarray Christoph Hellwig
2024-08-12 14:39 ` Matthew Wilcox
2024-08-12 14:43 ` Christoph Hellwig
2024-08-12 16:39 ` Pankaj Raghav (Samsung)
2024-08-12 19:02 ` Matthew Wilcox
2024-08-13 8:30 ` Pankaj Raghav (Samsung)
2024-08-13 6:37 ` kernel test robot
2024-08-14 4:59 ` Dave Chinner
2024-08-14 5:21 ` Christoph Hellwig
2024-08-12 6:31 ` [PATCH 3/3] xfs: use kfree_rcu_mightsleep to free the perag structures Christoph Hellwig
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240812063143.3806677-2-hch@lst.de \
--to=hch@lst.de \
--cc=akpm@linux-foundation.org \
--cc=chandan.babu@oracle.com \
--cc=djwong@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-xfs@vger.kernel.org \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).