blob: 2de504ae9ba4067faef40ff8836aa3824ca3c2a1 [file] [log] [blame]
Matthew Wilcoxf6bb2a22018-04-10 16:36:52 -07001/* SPDX-License-Identifier: GPL-2.0+ */
2#ifndef _LINUX_XARRAY_H
3#define _LINUX_XARRAY_H
4/*
5 * eXtensible Arrays
6 * Copyright (c) 2017 Microsoft Corporation
Matthew Wilcox3d0186b2018-06-16 17:32:07 -04007 * Author: Matthew Wilcox <willy@infradead.org>
Matthew Wilcox3159f942017-11-03 13:30:42 -04008 *
9 * See Documentation/core-api/xarray.rst for how to use the XArray.
Matthew Wilcoxf6bb2a22018-04-10 16:36:52 -070010 */
11
Matthew Wilcox3159f942017-11-03 13:30:42 -040012#include <linux/bug.h>
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -050013#include <linux/compiler.h>
Matthew Wilcox9b89a032017-11-10 09:34:31 -050014#include <linux/gfp.h>
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -050015#include <linux/kconfig.h>
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -050016#include <linux/kernel.h>
17#include <linux/rcupdate.h>
Matthew Wilcoxf6bb2a22018-04-10 16:36:52 -070018#include <linux/spinlock.h>
Matthew Wilcox3159f942017-11-03 13:30:42 -040019#include <linux/types.h>
20
21/*
22 * The bottom two bits of the entry determine how the XArray interprets
23 * the contents:
24 *
25 * 00: Pointer entry
26 * 10: Internal entry
27 * x1: Value entry or tagged pointer
28 *
29 * Attempting to store internal entries in the XArray is a bug.
Matthew Wilcox02c02bf2017-11-03 23:09:45 -040030 *
31 * Most internal entries are pointers to the next node in the tree.
32 * The following internal entries have a special meaning:
33 *
34 * 0-62: Sibling entries
35 * 256: Retry entry
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -050036 *
37 * Errors are also represented as internal entries, but use the negative
38 * space (-4094 to -2). They're never stored in the slots array; only
39 * returned by the normal API.
Matthew Wilcox3159f942017-11-03 13:30:42 -040040 */
41
42#define BITS_PER_XA_VALUE (BITS_PER_LONG - 1)
43
44/**
45 * xa_mk_value() - Create an XArray entry from an integer.
46 * @v: Value to store in XArray.
47 *
48 * Context: Any context.
49 * Return: An entry suitable for storing in the XArray.
50 */
51static inline void *xa_mk_value(unsigned long v)
52{
53 WARN_ON((long)v < 0);
54 return (void *)((v << 1) | 1);
55}
56
57/**
58 * xa_to_value() - Get value stored in an XArray entry.
59 * @entry: XArray entry.
60 *
61 * Context: Any context.
62 * Return: The value stored in the XArray entry.
63 */
64static inline unsigned long xa_to_value(const void *entry)
65{
66 return (unsigned long)entry >> 1;
67}
68
69/**
70 * xa_is_value() - Determine if an entry is a value.
71 * @entry: XArray entry.
72 *
73 * Context: Any context.
74 * Return: True if the entry is a value, false if it is a pointer.
75 */
76static inline bool xa_is_value(const void *entry)
77{
78 return (unsigned long)entry & 1;
79}
80
81/**
82 * xa_tag_pointer() - Create an XArray entry for a tagged pointer.
83 * @p: Plain pointer.
84 * @tag: Tag value (0, 1 or 3).
85 *
86 * If the user of the XArray prefers, they can tag their pointers instead
87 * of storing value entries. Three tags are available (0, 1 and 3).
88 * These are distinct from the xa_mark_t as they are not replicated up
89 * through the array and cannot be searched for.
90 *
91 * Context: Any context.
92 * Return: An XArray entry.
93 */
94static inline void *xa_tag_pointer(void *p, unsigned long tag)
95{
96 return (void *)((unsigned long)p | tag);
97}
98
99/**
100 * xa_untag_pointer() - Turn an XArray entry into a plain pointer.
101 * @entry: XArray entry.
102 *
103 * If you have stored a tagged pointer in the XArray, call this function
104 * to get the untagged version of the pointer.
105 *
106 * Context: Any context.
107 * Return: A pointer.
108 */
109static inline void *xa_untag_pointer(void *entry)
110{
111 return (void *)((unsigned long)entry & ~3UL);
112}
113
114/**
115 * xa_pointer_tag() - Get the tag stored in an XArray entry.
116 * @entry: XArray entry.
117 *
118 * If you have stored a tagged pointer in the XArray, call this function
119 * to get the tag of that pointer.
120 *
121 * Context: Any context.
122 * Return: A tag.
123 */
124static inline unsigned int xa_pointer_tag(void *entry)
125{
126 return (unsigned long)entry & 3UL;
127}
Matthew Wilcoxf6bb2a22018-04-10 16:36:52 -0700128
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400129/*
130 * xa_mk_internal() - Create an internal entry.
131 * @v: Value to turn into an internal entry.
132 *
133 * Context: Any context.
134 * Return: An XArray internal entry corresponding to this value.
135 */
136static inline void *xa_mk_internal(unsigned long v)
137{
138 return (void *)((v << 2) | 2);
139}
140
141/*
142 * xa_to_internal() - Extract the value from an internal entry.
143 * @entry: XArray entry.
144 *
145 * Context: Any context.
146 * Return: The value which was stored in the internal entry.
147 */
148static inline unsigned long xa_to_internal(const void *entry)
149{
150 return (unsigned long)entry >> 2;
151}
152
153/*
154 * xa_is_internal() - Is the entry an internal entry?
155 * @entry: XArray entry.
156 *
157 * Context: Any context.
158 * Return: %true if the entry is an internal entry.
159 */
160static inline bool xa_is_internal(const void *entry)
161{
162 return ((unsigned long)entry & 3) == 2;
163}
164
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500165/**
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500166 * xa_is_err() - Report whether an XArray operation returned an error
167 * @entry: Result from calling an XArray function
168 *
169 * If an XArray operation cannot complete an operation, it will return
170 * a special value indicating an error. This function tells you
171 * whether an error occurred; xa_err() tells you which error occurred.
172 *
173 * Context: Any context.
174 * Return: %true if the entry indicates an error.
175 */
176static inline bool xa_is_err(const void *entry)
177{
178 return unlikely(xa_is_internal(entry));
179}
180
181/**
182 * xa_err() - Turn an XArray result into an errno.
183 * @entry: Result from calling an XArray function.
184 *
185 * If an XArray operation cannot complete an operation, it will return
186 * a special pointer value which encodes an errno. This function extracts
187 * the errno from the pointer value, or returns 0 if the pointer does not
188 * represent an errno.
189 *
190 * Context: Any context.
191 * Return: A negative errno or 0.
192 */
193static inline int xa_err(void *entry)
194{
195 /* xa_to_internal() would not do sign extension. */
196 if (xa_is_err(entry))
197 return (long)entry >> 2;
198 return 0;
199}
200
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500201typedef unsigned __bitwise xa_mark_t;
202#define XA_MARK_0 ((__force xa_mark_t)0U)
203#define XA_MARK_1 ((__force xa_mark_t)1U)
204#define XA_MARK_2 ((__force xa_mark_t)2U)
205#define XA_PRESENT ((__force xa_mark_t)8U)
206#define XA_MARK_MAX XA_MARK_2
207
208/*
209 * Values for xa_flags. The radix tree stores its GFP flags in the xa_flags,
210 * and we remain compatible with that.
211 */
212#define XA_FLAGS_MARK(mark) ((__force gfp_t)((1U << __GFP_BITS_SHIFT) << \
213 (__force unsigned)(mark)))
214
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500215/**
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500216 * struct xarray - The anchor of the XArray.
217 * @xa_lock: Lock that protects the contents of the XArray.
218 *
219 * To use the xarray, define it statically or embed it in your data structure.
220 * It is a very small data structure, so it does not usually make sense to
221 * allocate it separately and keep a pointer to it in your data structure.
222 *
223 * You may use the xa_lock to protect your own data structures as well.
224 */
225/*
226 * If all of the entries in the array are NULL, @xa_head is a NULL pointer.
227 * If the only non-NULL entry in the array is at index 0, @xa_head is that
228 * entry. If any other entry in the array is non-NULL, @xa_head points
229 * to an @xa_node.
230 */
231struct xarray {
232 spinlock_t xa_lock;
233/* private: The rest of the data structure is not to be used directly. */
234 gfp_t xa_flags;
235 void __rcu * xa_head;
236};
237
238#define XARRAY_INIT(name, flags) { \
239 .xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock), \
240 .xa_flags = flags, \
241 .xa_head = NULL, \
242}
243
244/**
245 * DEFINE_XARRAY_FLAGS() - Define an XArray with custom flags.
246 * @name: A string that names your XArray.
247 * @flags: XA_FLAG values.
248 *
249 * This is intended for file scope definitions of XArrays. It declares
250 * and initialises an empty XArray with the chosen name and flags. It is
251 * equivalent to calling xa_init_flags() on the array, but it does the
252 * initialisation at compiletime instead of runtime.
253 */
254#define DEFINE_XARRAY_FLAGS(name, flags) \
255 struct xarray name = XARRAY_INIT(name, flags)
256
257/**
258 * DEFINE_XARRAY() - Define an XArray.
259 * @name: A string that names your XArray.
260 *
261 * This is intended for file scope definitions of XArrays. It declares
262 * and initialises an empty XArray with the chosen name. It is equivalent
263 * to calling xa_init() on the array, but it does the initialisation at
264 * compiletime instead of runtime.
265 */
266#define DEFINE_XARRAY(name) DEFINE_XARRAY_FLAGS(name, 0)
267
268void xa_init_flags(struct xarray *, gfp_t flags);
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500269void *xa_load(struct xarray *, unsigned long index);
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500270bool xa_get_mark(struct xarray *, unsigned long index, xa_mark_t);
271void xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
272void xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500273
274/**
275 * xa_init() - Initialise an empty XArray.
276 * @xa: XArray.
277 *
278 * An empty XArray is full of NULL entries.
279 *
280 * Context: Any context.
281 */
282static inline void xa_init(struct xarray *xa)
283{
284 xa_init_flags(xa, 0);
285}
286
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500287/**
288 * xa_empty() - Determine if an array has any present entries.
289 * @xa: XArray.
290 *
291 * Context: Any context.
292 * Return: %true if the array contains only NULL pointers.
293 */
294static inline bool xa_empty(const struct xarray *xa)
295{
296 return xa->xa_head == NULL;
297}
298
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500299/**
300 * xa_marked() - Inquire whether any entry in this array has a mark set
301 * @xa: Array
302 * @mark: Mark value
303 *
304 * Context: Any context.
305 * Return: %true if any entry has this mark set.
306 */
307static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark)
308{
309 return xa->xa_flags & XA_FLAGS_MARK(mark);
310}
311
Matthew Wilcoxf6bb2a22018-04-10 16:36:52 -0700312#define xa_trylock(xa) spin_trylock(&(xa)->xa_lock)
313#define xa_lock(xa) spin_lock(&(xa)->xa_lock)
314#define xa_unlock(xa) spin_unlock(&(xa)->xa_lock)
315#define xa_lock_bh(xa) spin_lock_bh(&(xa)->xa_lock)
316#define xa_unlock_bh(xa) spin_unlock_bh(&(xa)->xa_lock)
317#define xa_lock_irq(xa) spin_lock_irq(&(xa)->xa_lock)
318#define xa_unlock_irq(xa) spin_unlock_irq(&(xa)->xa_lock)
319#define xa_lock_irqsave(xa, flags) \
320 spin_lock_irqsave(&(xa)->xa_lock, flags)
321#define xa_unlock_irqrestore(xa, flags) \
322 spin_unlock_irqrestore(&(xa)->xa_lock, flags)
323
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500324/*
325 * Versions of the normal API which require the caller to hold the xa_lock.
326 */
327void __xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
328void __xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
329
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400330/* Everything below here is the Advanced API. Proceed with caution. */
331
332/*
333 * The xarray is constructed out of a set of 'chunks' of pointers. Choosing
334 * the best chunk size requires some tradeoffs. A power of two recommends
335 * itself so that we can walk the tree based purely on shifts and masks.
336 * Generally, the larger the better; as the number of slots per level of the
337 * tree increases, the less tall the tree needs to be. But that needs to be
338 * balanced against the memory consumption of each node. On a 64-bit system,
339 * xa_node is currently 576 bytes, and we get 7 of them per 4kB page. If we
340 * doubled the number of slots per node, we'd get only 3 nodes per 4kB page.
341 */
342#ifndef XA_CHUNK_SHIFT
343#define XA_CHUNK_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
344#endif
345#define XA_CHUNK_SIZE (1UL << XA_CHUNK_SHIFT)
346#define XA_CHUNK_MASK (XA_CHUNK_SIZE - 1)
Matthew Wilcox01959df2017-11-09 09:23:56 -0500347#define XA_MAX_MARKS 3
348#define XA_MARK_LONGS DIV_ROUND_UP(XA_CHUNK_SIZE, BITS_PER_LONG)
349
350/*
351 * @count is the count of every non-NULL element in the ->slots array
352 * whether that is a value entry, a retry entry, a user pointer,
353 * a sibling entry or a pointer to the next level of the tree.
354 * @nr_values is the count of every element in ->slots which is
355 * either a value entry or a sibling of a value entry.
356 */
357struct xa_node {
358 unsigned char shift; /* Bits remaining in each slot */
359 unsigned char offset; /* Slot offset in parent */
360 unsigned char count; /* Total entry count */
361 unsigned char nr_values; /* Value entry count */
362 struct xa_node __rcu *parent; /* NULL at top of tree */
363 struct xarray *array; /* The array we belong to */
364 union {
365 struct list_head private_list; /* For tree user */
366 struct rcu_head rcu_head; /* Used when freeing node */
367 };
368 void __rcu *slots[XA_CHUNK_SIZE];
369 union {
370 unsigned long tags[XA_MAX_MARKS][XA_MARK_LONGS];
371 unsigned long marks[XA_MAX_MARKS][XA_MARK_LONGS];
372 };
373};
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400374
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500375void xa_dump(const struct xarray *);
376void xa_dump_node(const struct xa_node *);
377
378#ifdef XA_DEBUG
379#define XA_BUG_ON(xa, x) do { \
380 if (x) { \
381 xa_dump(xa); \
382 BUG(); \
383 } \
384 } while (0)
385#define XA_NODE_BUG_ON(node, x) do { \
386 if (x) { \
387 if (node) xa_dump_node(node); \
388 BUG(); \
389 } \
390 } while (0)
391#else
392#define XA_BUG_ON(xa, x) do { } while (0)
393#define XA_NODE_BUG_ON(node, x) do { } while (0)
394#endif
395
396/* Private */
397static inline void *xa_head(const struct xarray *xa)
398{
399 return rcu_dereference_check(xa->xa_head,
400 lockdep_is_held(&xa->xa_lock));
401}
402
403/* Private */
404static inline void *xa_head_locked(const struct xarray *xa)
405{
406 return rcu_dereference_protected(xa->xa_head,
407 lockdep_is_held(&xa->xa_lock));
408}
409
410/* Private */
411static inline void *xa_entry(const struct xarray *xa,
412 const struct xa_node *node, unsigned int offset)
413{
414 XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
415 return rcu_dereference_check(node->slots[offset],
416 lockdep_is_held(&xa->xa_lock));
417}
418
419/* Private */
420static inline void *xa_entry_locked(const struct xarray *xa,
421 const struct xa_node *node, unsigned int offset)
422{
423 XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
424 return rcu_dereference_protected(node->slots[offset],
425 lockdep_is_held(&xa->xa_lock));
426}
427
428/* Private */
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500429static inline struct xa_node *xa_parent(const struct xarray *xa,
430 const struct xa_node *node)
431{
432 return rcu_dereference_check(node->parent,
433 lockdep_is_held(&xa->xa_lock));
434}
435
436/* Private */
437static inline struct xa_node *xa_parent_locked(const struct xarray *xa,
438 const struct xa_node *node)
439{
440 return rcu_dereference_protected(node->parent,
441 lockdep_is_held(&xa->xa_lock));
442}
443
444/* Private */
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500445static inline struct xa_node *xa_to_node(const void *entry)
446{
447 return (struct xa_node *)((unsigned long)entry - 2);
448}
449
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400450/* Private */
451static inline bool xa_is_node(const void *entry)
452{
453 return xa_is_internal(entry) && (unsigned long)entry > 4096;
454}
455
456/* Private */
457static inline void *xa_mk_sibling(unsigned int offset)
458{
459 return xa_mk_internal(offset);
460}
461
462/* Private */
463static inline unsigned long xa_to_sibling(const void *entry)
464{
465 return xa_to_internal(entry);
466}
467
468/**
469 * xa_is_sibling() - Is the entry a sibling entry?
470 * @entry: Entry retrieved from the XArray
471 *
472 * Return: %true if the entry is a sibling entry.
473 */
474static inline bool xa_is_sibling(const void *entry)
475{
476 return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) &&
477 (entry < xa_mk_sibling(XA_CHUNK_SIZE - 1));
478}
479
480#define XA_RETRY_ENTRY xa_mk_internal(256)
481
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500482/**
483 * xa_is_retry() - Is the entry a retry entry?
484 * @entry: Entry retrieved from the XArray
485 *
486 * Return: %true if the entry is a retry entry.
487 */
488static inline bool xa_is_retry(const void *entry)
489{
490 return unlikely(entry == XA_RETRY_ENTRY);
491}
492
493/**
494 * typedef xa_update_node_t - A callback function from the XArray.
495 * @node: The node which is being processed
496 *
497 * This function is called every time the XArray updates the count of
498 * present and value entries in a node. It allows advanced users to
499 * maintain the private_list in the node.
500 *
501 * Context: The xa_lock is held and interrupts may be disabled.
502 * Implementations should not drop the xa_lock, nor re-enable
503 * interrupts.
504 */
505typedef void (*xa_update_node_t)(struct xa_node *node);
506
507/*
508 * The xa_state is opaque to its users. It contains various different pieces
509 * of state involved in the current operation on the XArray. It should be
510 * declared on the stack and passed between the various internal routines.
511 * The various elements in it should not be accessed directly, but only
512 * through the provided accessor functions. The below documentation is for
513 * the benefit of those working on the code, not for users of the XArray.
514 *
515 * @xa_node usually points to the xa_node containing the slot we're operating
516 * on (and @xa_offset is the offset in the slots array). If there is a
517 * single entry in the array at index 0, there are no allocated xa_nodes to
518 * point to, and so we store %NULL in @xa_node. @xa_node is set to
519 * the value %XAS_RESTART if the xa_state is not walked to the correct
520 * position in the tree of nodes for this operation. If an error occurs
521 * during an operation, it is set to an %XAS_ERROR value. If we run off the
522 * end of the allocated nodes, it is set to %XAS_BOUNDS.
523 */
524struct xa_state {
525 struct xarray *xa;
526 unsigned long xa_index;
527 unsigned char xa_shift;
528 unsigned char xa_sibs;
529 unsigned char xa_offset;
530 unsigned char xa_pad; /* Helps gcc generate better code */
531 struct xa_node *xa_node;
532 struct xa_node *xa_alloc;
533 xa_update_node_t xa_update;
534};
535
536/*
537 * We encode errnos in the xas->xa_node. If an error has happened, we need to
538 * drop the lock to fix it, and once we've done so the xa_state is invalid.
539 */
540#define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL))
541#define XAS_BOUNDS ((struct xa_node *)1UL)
542#define XAS_RESTART ((struct xa_node *)3UL)
543
544#define __XA_STATE(array, index, shift, sibs) { \
545 .xa = array, \
546 .xa_index = index, \
547 .xa_shift = shift, \
548 .xa_sibs = sibs, \
549 .xa_offset = 0, \
550 .xa_pad = 0, \
551 .xa_node = XAS_RESTART, \
552 .xa_alloc = NULL, \
553 .xa_update = NULL \
554}
555
556/**
557 * XA_STATE() - Declare an XArray operation state.
558 * @name: Name of this operation state (usually xas).
559 * @array: Array to operate on.
560 * @index: Initial index of interest.
561 *
562 * Declare and initialise an xa_state on the stack.
563 */
564#define XA_STATE(name, array, index) \
565 struct xa_state name = __XA_STATE(array, index, 0, 0)
566
567/**
568 * XA_STATE_ORDER() - Declare an XArray operation state.
569 * @name: Name of this operation state (usually xas).
570 * @array: Array to operate on.
571 * @index: Initial index of interest.
572 * @order: Order of entry.
573 *
574 * Declare and initialise an xa_state on the stack. This variant of
575 * XA_STATE() allows you to specify the 'order' of the element you
576 * want to operate on.`
577 */
578#define XA_STATE_ORDER(name, array, index, order) \
579 struct xa_state name = __XA_STATE(array, \
580 (index >> order) << order, \
581 order - (order % XA_CHUNK_SHIFT), \
582 (1U << (order % XA_CHUNK_SHIFT)) - 1)
583
584#define xas_marked(xas, mark) xa_marked((xas)->xa, (mark))
585#define xas_trylock(xas) xa_trylock((xas)->xa)
586#define xas_lock(xas) xa_lock((xas)->xa)
587#define xas_unlock(xas) xa_unlock((xas)->xa)
588#define xas_lock_bh(xas) xa_lock_bh((xas)->xa)
589#define xas_unlock_bh(xas) xa_unlock_bh((xas)->xa)
590#define xas_lock_irq(xas) xa_lock_irq((xas)->xa)
591#define xas_unlock_irq(xas) xa_unlock_irq((xas)->xa)
592#define xas_lock_irqsave(xas, flags) \
593 xa_lock_irqsave((xas)->xa, flags)
594#define xas_unlock_irqrestore(xas, flags) \
595 xa_unlock_irqrestore((xas)->xa, flags)
596
597/**
598 * xas_error() - Return an errno stored in the xa_state.
599 * @xas: XArray operation state.
600 *
601 * Return: 0 if no error has been noted. A negative errno if one has.
602 */
603static inline int xas_error(const struct xa_state *xas)
604{
605 return xa_err(xas->xa_node);
606}
607
608/**
609 * xas_set_err() - Note an error in the xa_state.
610 * @xas: XArray operation state.
611 * @err: Negative error number.
612 *
613 * Only call this function with a negative @err; zero or positive errors
614 * will probably not behave the way you think they should. If you want
615 * to clear the error from an xa_state, use xas_reset().
616 */
617static inline void xas_set_err(struct xa_state *xas, long err)
618{
619 xas->xa_node = XA_ERROR(err);
620}
621
622/**
623 * xas_invalid() - Is the xas in a retry or error state?
624 * @xas: XArray operation state.
625 *
626 * Return: %true if the xas cannot be used for operations.
627 */
628static inline bool xas_invalid(const struct xa_state *xas)
629{
630 return (unsigned long)xas->xa_node & 3;
631}
632
633/**
634 * xas_valid() - Is the xas a valid cursor into the array?
635 * @xas: XArray operation state.
636 *
637 * Return: %true if the xas can be used for operations.
638 */
639static inline bool xas_valid(const struct xa_state *xas)
640{
641 return !xas_invalid(xas);
642}
643
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500644/* True if the pointer is something other than a node */
645static inline bool xas_not_node(struct xa_node *node)
646{
647 return ((unsigned long)node & 3) || !node;
648}
649
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500650/**
651 * xas_reset() - Reset an XArray operation state.
652 * @xas: XArray operation state.
653 *
654 * Resets the error or walk state of the @xas so future walks of the
655 * array will start from the root. Use this if you have dropped the
656 * xarray lock and want to reuse the xa_state.
657 *
658 * Context: Any context.
659 */
660static inline void xas_reset(struct xa_state *xas)
661{
662 xas->xa_node = XAS_RESTART;
663}
664
665/**
666 * xas_retry() - Retry the operation if appropriate.
667 * @xas: XArray operation state.
668 * @entry: Entry from xarray.
669 *
670 * The advanced functions may sometimes return an internal entry, such as
671 * a retry entry or a zero entry. This function sets up the @xas to restart
672 * the walk from the head of the array if needed.
673 *
674 * Context: Any context.
675 * Return: true if the operation needs to be retried.
676 */
677static inline bool xas_retry(struct xa_state *xas, const void *entry)
678{
679 if (!xa_is_retry(entry))
680 return false;
681 xas_reset(xas);
682 return true;
683}
684
685void *xas_load(struct xa_state *);
686
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500687bool xas_get_mark(const struct xa_state *, xa_mark_t);
688void xas_set_mark(const struct xa_state *, xa_mark_t);
689void xas_clear_mark(const struct xa_state *, xa_mark_t);
690
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500691/**
692 * xas_reload() - Refetch an entry from the xarray.
693 * @xas: XArray operation state.
694 *
695 * Use this function to check that a previously loaded entry still has
696 * the same value. This is useful for the lockless pagecache lookup where
697 * we walk the array with only the RCU lock to protect us, lock the page,
698 * then check that the page hasn't moved since we looked it up.
699 *
700 * The caller guarantees that @xas is still valid. If it may be in an
701 * error or restart state, call xas_load() instead.
702 *
703 * Return: The entry at this location in the xarray.
704 */
705static inline void *xas_reload(struct xa_state *xas)
706{
707 struct xa_node *node = xas->xa_node;
708
709 if (node)
710 return xa_entry(xas->xa, node, xas->xa_offset);
711 return xa_head(xas->xa);
712}
713
Matthew Wilcoxf6bb2a22018-04-10 16:36:52 -0700714#endif /* _LINUX_XARRAY_H */