Blame view

include/linux/radix-tree.h 19.5 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
  /*
   * Copyright (C) 2001 Momchil Velikov
   * Portions Copyright (C) 2001 Christoph Hellwig
7cf9c2c76   Nick Piggin   [PATCH] radix-tre...
4
   * Copyright (C) 2006 Nick Piggin
78c1d7848   Konstantin Khlebnikov   radix-tree: intro...
5
   * Copyright (C) 2012 Konstantin Khlebnikov
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License as
   * published by the Free Software Foundation; either version 2, or (at
   * your option) any later version.
   * 
   * This program is distributed in the hope that it will be useful, but
   * WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * General Public License for more details.
   * 
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   */
  #ifndef _LINUX_RADIX_TREE_H
  #define _LINUX_RADIX_TREE_H
f67c07f07   Matthew Wilcox   radix-tree: add a...
23
  #include <linux/bitops.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
25
  #include <linux/preempt.h>
  #include <linux/types.h>
187f1882b   Paul Gortmaker   BUG: headers with...
26
  #include <linux/bug.h>
7cf9c2c76   Nick Piggin   [PATCH] radix-tre...
27
28
29
30
  #include <linux/kernel.h>
  #include <linux/rcupdate.h>
  
  /*
3bcadd6fa   Matthew Wilcox   radix-tree: free ...
31
32
   * The bottom two bits of the slot determine how the remaining bits in the
   * slot are interpreted:
7cf9c2c76   Nick Piggin   [PATCH] radix-tre...
33
   *
3bcadd6fa   Matthew Wilcox   radix-tree: free ...
34
35
36
   * 00 - data pointer
   * 01 - internal entry
   * 10 - exceptional entry
a23216a2f   Ross Zwisler   radix-tree: fix c...
37
   * 11 - this bit combination is currently unused/reserved
3bcadd6fa   Matthew Wilcox   radix-tree: free ...
38
39
40
41
42
43
44
   *
   * The internal entry may be a pointer to the next level in the tree, a
   * sibling entry, or an indicator that the entry in this slot has been moved
   * to another location in the tree and the lookup should be restarted.  While
   * NULL fits the 'data pointer' pattern, it means that there is no entry in
   * the tree for this index (no matter what level of the tree it is found at).
   * This means that you cannot store NULL in the tree as a value for the index.
7cf9c2c76   Nick Piggin   [PATCH] radix-tre...
45
   */
3bcadd6fa   Matthew Wilcox   radix-tree: free ...
46
47
  #define RADIX_TREE_ENTRY_MASK		3UL
  #define RADIX_TREE_INTERNAL_NODE	1UL
30ff46ccb   Matthew Wilcox   radix-tree: renam...
48

6328650bb   Hugh Dickins   radix_tree: excep...
49
  /*
3bcadd6fa   Matthew Wilcox   radix-tree: free ...
50
51
52
   * Most users of the radix tree store pointers but shmem/tmpfs stores swap
   * entries in the same tree.  They are marked as exceptional entries to
   * distinguish them from pointers to struct page.
6328650bb   Hugh Dickins   radix_tree: excep...
53
54
55
56
   * EXCEPTIONAL_ENTRY tests the bit, EXCEPTIONAL_SHIFT shifts content past it.
   */
  #define RADIX_TREE_EXCEPTIONAL_ENTRY	2
  #define RADIX_TREE_EXCEPTIONAL_SHIFT	2
7cf9c2c76   Nick Piggin   [PATCH] radix-tre...
57

3bcadd6fa   Matthew Wilcox   radix-tree: free ...
58
  static inline bool radix_tree_is_internal_node(void *ptr)
7cf9c2c76   Nick Piggin   [PATCH] radix-tre...
59
  {
3bcadd6fa   Matthew Wilcox   radix-tree: free ...
60
61
  	return ((unsigned long)ptr & RADIX_TREE_ENTRY_MASK) ==
  				RADIX_TREE_INTERNAL_NODE;
7cf9c2c76   Nick Piggin   [PATCH] radix-tre...
62
63
64
  }
  
  /*** radix-tree API starts here ***/
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
65

f446daaea   Jan Kara   mm: implement wri...
66
  #define RADIX_TREE_MAX_TAGS 3
612d6c19d   Nick Piggin   [PATCH] radix-tre...
67

97d778b2d   Ross Zwisler   radix tree test s...
68
  #ifndef RADIX_TREE_MAP_SHIFT
139e56166   Johannes Weiner   lib: radix_tree: ...
69
  #define RADIX_TREE_MAP_SHIFT	(CONFIG_BASE_SMALL ? 4 : 6)
139e56166   Johannes Weiner   lib: radix_tree: ...
70
71
72
73
74
75
76
  #endif
  
  #define RADIX_TREE_MAP_SIZE	(1UL << RADIX_TREE_MAP_SHIFT)
  #define RADIX_TREE_MAP_MASK	(RADIX_TREE_MAP_SIZE-1)
  
  #define RADIX_TREE_TAG_LONGS	\
  	((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG)
449dd6984   Johannes Weiner   mm: keep page cac...
77
78
79
  #define RADIX_TREE_INDEX_BITS  (8 /* CHAR_BIT */ * sizeof(unsigned long))
  #define RADIX_TREE_MAX_PATH (DIV_ROUND_UP(RADIX_TREE_INDEX_BITS, \
  					  RADIX_TREE_MAP_SHIFT))
449dd6984   Johannes Weiner   mm: keep page cac...
80
81
82
  /* Internally used bits of node->count */
  #define RADIX_TREE_COUNT_SHIFT	(RADIX_TREE_MAP_SHIFT + 1)
  #define RADIX_TREE_COUNT_MASK	((1UL << RADIX_TREE_COUNT_SHIFT) - 1)
139e56166   Johannes Weiner   lib: radix_tree: ...
83
  struct radix_tree_node {
c12e51b07   Matthew Wilcox   radix-tree: repla...
84
  	unsigned char	shift;	/* Bits remaining in each slot */
0c7fa0a84   Matthew Wilcox   radix-tree: split...
85
  	unsigned char	offset;	/* Slot offset in parent */
139e56166   Johannes Weiner   lib: radix_tree: ...
86
87
  	unsigned int	count;
  	union {
449dd6984   Johannes Weiner   mm: keep page cac...
88
89
90
91
92
93
94
95
  		struct {
  			/* Used when ascending tree */
  			struct radix_tree_node *parent;
  			/* For tree user */
  			void *private_data;
  		};
  		/* Used when freeing node */
  		struct rcu_head	rcu_head;
139e56166   Johannes Weiner   lib: radix_tree: ...
96
  	};
449dd6984   Johannes Weiner   mm: keep page cac...
97
98
  	/* For tree user */
  	struct list_head private_list;
139e56166   Johannes Weiner   lib: radix_tree: ...
99
100
101
  	void __rcu	*slots[RADIX_TREE_MAP_SIZE];
  	unsigned long	tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS];
  };
612d6c19d   Nick Piggin   [PATCH] radix-tre...
102
  /* root tags are stored in gfp_mask, shifted by __GFP_BITS_SHIFT */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
103
  struct radix_tree_root {
fd4f2df24   Al Viro   [PATCH] gfp_t: lib/*
104
  	gfp_t			gfp_mask;
a1115570b   Arnd Bergmann   radix-tree: __rcu...
105
  	struct radix_tree_node	__rcu *rnode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106
107
108
  };
  
  #define RADIX_TREE_INIT(mask)	{					\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
111
112
113
114
115
116
117
  	.gfp_mask = (mask),						\
  	.rnode = NULL,							\
  }
  
  #define RADIX_TREE(name, mask) \
  	struct radix_tree_root name = RADIX_TREE_INIT(mask)
  
  #define INIT_RADIX_TREE(root, mask)					\
  do {									\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118
119
120
  	(root)->gfp_mask = (mask);					\
  	(root)->rnode = NULL;						\
  } while (0)
e9256efcc   Matthew Wilcox   radix-tree: intro...
121
122
123
124
  static inline bool radix_tree_empty(struct radix_tree_root *root)
  {
  	return root->rnode == NULL;
  }
7cf9c2c76   Nick Piggin   [PATCH] radix-tre...
125
126
127
128
129
130
131
132
133
134
  /**
   * Radix-tree synchronization
   *
   * The radix-tree API requires that users provide all synchronisation (with
   * specific exceptions, noted below).
   *
   * Synchronization of access to the data items being stored in the tree, and
   * management of their lifetimes must be completely managed by API users.
   *
   * For API usage, in general,
59c51591a   Michael Opdenacker   Fix occurrences o...
135
   * - any function _modifying_ the tree or tags (inserting or deleting
eb8dc5e7b   Tim Pepper   radix_tree.h triv...
136
   *   items, setting or clearing tags) must exclude other modifications, and
7cf9c2c76   Nick Piggin   [PATCH] radix-tre...
137
   *   exclude any functions reading the tree.
59c51591a   Michael Opdenacker   Fix occurrences o...
138
   * - any function _reading_ the tree or tags (looking up items or tags,
7cf9c2c76   Nick Piggin   [PATCH] radix-tre...
139
140
141
142
   *   gang lookups) must exclude modifications to the tree, but may occur
   *   concurrently with other readers.
   *
   * The notable exceptions to this rule are the following functions:
139e56166   Johannes Weiner   lib: radix_tree: ...
143
   * __radix_tree_lookup
7cf9c2c76   Nick Piggin   [PATCH] radix-tre...
144
   * radix_tree_lookup
47feff2c8   Nick Piggin   radix-tree: add g...
145
   * radix_tree_lookup_slot
7cf9c2c76   Nick Piggin   [PATCH] radix-tre...
146
147
   * radix_tree_tag_get
   * radix_tree_gang_lookup
47feff2c8   Nick Piggin   radix-tree: add g...
148
   * radix_tree_gang_lookup_slot
7cf9c2c76   Nick Piggin   [PATCH] radix-tre...
149
   * radix_tree_gang_lookup_tag
47feff2c8   Nick Piggin   radix-tree: add g...
150
   * radix_tree_gang_lookup_tag_slot
7cf9c2c76   Nick Piggin   [PATCH] radix-tre...
151
152
   * radix_tree_tagged
   *
243c2137c   Adam Barth   include/linux/rad...
153
   * The first 8 functions are able to be called locklessly, using RCU. The
7cf9c2c76   Nick Piggin   [PATCH] radix-tre...
154
155
156
157
158
159
160
161
162
163
164
165
166
   * caller must ensure calls to these functions are made within rcu_read_lock()
   * regions. Other readers (lock-free or otherwise) and modifications may be
   * running concurrently.
   *
   * It is still required that the caller manage the synchronization and lifetimes
   * of the items. So if RCU lock-free lookups are used, typically this would mean
   * that the items have their own locks, or are amenable to lock-free access; and
   * that the items are freed by RCU (or only freed after having been deleted from
   * the radix tree *and* a synchronize_rcu() grace period).
   *
   * (Note, rcu_assign_pointer and rcu_dereference are not needed to control
   * access to data items when inserting into or looking up from the radix tree)
   *
ce82653d6   David Howells   radix_tree_tag_ge...
167
168
169
170
171
172
173
   * Note that the value returned by radix_tree_tag_get() may not be relied upon
   * if only the RCU read lock is held.  Functions to set/clear tags and to
   * delete nodes running concurrently with it may affect its result such that
   * two consecutive reads in the same locked section may return different
   * values.  If reliability is required, modification functions must also be
   * excluded from concurrency.
   *
7cf9c2c76   Nick Piggin   [PATCH] radix-tre...
174
175
176
177
178
179
180
181
182
183
   * radix_tree_tagged is able to be called without locking or RCU.
   */
  
  /**
   * radix_tree_deref_slot	- dereference a slot
   * @pslot:	pointer to slot, returned by radix_tree_lookup_slot
   * Returns:	item that was stored in that slot with any direct pointer flag
   *		removed.
   *
   * For use with radix_tree_lookup_slot().  Caller must hold tree at least read
27d20fddc   Nick Piggin   radix-tree: fix R...
184
185
186
187
188
   * locked across slot lookup and dereference. Not required if write lock is
   * held (ie. items cannot be concurrently inserted).
   *
   * radix_tree_deref_retry must be used to confirm validity of the pointer if
   * only the read lock is held.
7cf9c2c76   Nick Piggin   [PATCH] radix-tre...
189
190
191
   */
  static inline void *radix_tree_deref_slot(void **pslot)
  {
27d20fddc   Nick Piggin   radix-tree: fix R...
192
  	return rcu_dereference(*pslot);
7cf9c2c76   Nick Piggin   [PATCH] radix-tre...
193
  }
27d20fddc   Nick Piggin   radix-tree: fix R...
194
195
  
  /**
29c1f677d   Mel Gorman   mm: migration: us...
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
   * radix_tree_deref_slot_protected	- dereference a slot without RCU lock but with tree lock held
   * @pslot:	pointer to slot, returned by radix_tree_lookup_slot
   * Returns:	item that was stored in that slot with any direct pointer flag
   *		removed.
   *
   * Similar to radix_tree_deref_slot but only used during migration when a pages
   * mapping is being moved. The caller does not hold the RCU read lock but it
   * must hold the tree lock to prevent parallel updates.
   */
  static inline void *radix_tree_deref_slot_protected(void **pslot,
  							spinlock_t *treelock)
  {
  	return rcu_dereference_protected(*pslot, lockdep_is_held(treelock));
  }
  
  /**
27d20fddc   Nick Piggin   radix-tree: fix R...
212
213
214
215
216
217
218
219
   * radix_tree_deref_retry	- check radix_tree_deref_slot
   * @arg:	pointer returned by radix_tree_deref_slot
   * Returns:	0 if retry is not required, otherwise retry is required
   *
   * radix_tree_deref_retry must be used with radix_tree_deref_slot.
   */
  static inline int radix_tree_deref_retry(void *arg)
  {
b194d16c2   Matthew Wilcox   radix-tree: renam...
220
  	return unlikely(radix_tree_is_internal_node(arg));
27d20fddc   Nick Piggin   radix-tree: fix R...
221
  }
7cf9c2c76   Nick Piggin   [PATCH] radix-tre...
222
  /**
6328650bb   Hugh Dickins   radix_tree: excep...
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
   * radix_tree_exceptional_entry	- radix_tree_deref_slot gave exceptional entry?
   * @arg:	value returned by radix_tree_deref_slot
   * Returns:	0 if well-aligned pointer, non-0 if exceptional entry.
   */
  static inline int radix_tree_exceptional_entry(void *arg)
  {
  	/* Not unlikely because radix_tree_exception often tested first */
  	return (unsigned long)arg & RADIX_TREE_EXCEPTIONAL_ENTRY;
  }
  
  /**
   * radix_tree_exception	- radix_tree_deref_slot returned either exception?
   * @arg:	value returned by radix_tree_deref_slot
   * Returns:	0 if well-aligned pointer, non-0 if either kind of exception.
   */
  static inline int radix_tree_exception(void *arg)
  {
3bcadd6fa   Matthew Wilcox   radix-tree: free ...
240
  	return unlikely((unsigned long)arg & RADIX_TREE_ENTRY_MASK);
6328650bb   Hugh Dickins   radix_tree: excep...
241
242
243
  }
  
  /**
7cf9c2c76   Nick Piggin   [PATCH] radix-tre...
244
245
246
247
248
249
250
251
252
   * radix_tree_replace_slot	- replace item in a slot
   * @pslot:	pointer to slot, returned by radix_tree_lookup_slot
   * @item:	new item to store in the slot.
   *
   * For use with radix_tree_lookup_slot().  Caller must hold tree write locked
   * across slot lookup and replacement.
   */
  static inline void radix_tree_replace_slot(void **pslot, void *item)
  {
b194d16c2   Matthew Wilcox   radix-tree: renam...
253
  	BUG_ON(radix_tree_is_internal_node(item));
c0bc9875b   Nick Piggin   radix-tree: use i...
254
  	rcu_assign_pointer(*pslot, item);
7cf9c2c76   Nick Piggin   [PATCH] radix-tre...
255
  }
139e56166   Johannes Weiner   lib: radix_tree: ...
256
  int __radix_tree_create(struct radix_tree_root *root, unsigned long index,
e61452365   Matthew Wilcox   radix_tree: add s...
257
258
259
260
261
262
263
264
265
  			unsigned order, struct radix_tree_node **nodep,
  			void ***slotp);
  int __radix_tree_insert(struct radix_tree_root *, unsigned long index,
  			unsigned order, void *);
  static inline int radix_tree_insert(struct radix_tree_root *root,
  			unsigned long index, void *entry)
  {
  	return __radix_tree_insert(root, index, 0, entry);
  }
139e56166   Johannes Weiner   lib: radix_tree: ...
266
267
  void *__radix_tree_lookup(struct radix_tree_root *root, unsigned long index,
  			  struct radix_tree_node **nodep, void ***slotp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
268
  void *radix_tree_lookup(struct radix_tree_root *, unsigned long);
a43313668   Hans Reiser   [PATCH] reiser4: ...
269
  void **radix_tree_lookup_slot(struct radix_tree_root *, unsigned long);
449dd6984   Johannes Weiner   mm: keep page cac...
270
  bool __radix_tree_delete_node(struct radix_tree_root *root,
139e56166   Johannes Weiner   lib: radix_tree: ...
271
  			      struct radix_tree_node *node);
53c59f262   Johannes Weiner   lib: radix-tree: ...
272
  void *radix_tree_delete_item(struct radix_tree_root *, unsigned long, void *);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273
  void *radix_tree_delete(struct radix_tree_root *, unsigned long);
d3798ae8c   Johannes Weiner   mm: filemap: don'...
274
275
276
  void radix_tree_clear_tags(struct radix_tree_root *root,
  			   struct radix_tree_node *node,
  			   void **slot);
d604c3245   Matthew Wilcox   radix-tree: intro...
277
278
279
  unsigned int radix_tree_gang_lookup(struct radix_tree_root *root,
  			void **results, unsigned long first_index,
  			unsigned int max_items);
6328650bb   Hugh Dickins   radix_tree: excep...
280
281
  unsigned int radix_tree_gang_lookup_slot(struct radix_tree_root *root,
  			void ***results, unsigned long *indices,
47feff2c8   Nick Piggin   radix-tree: add g...
282
  			unsigned long first_index, unsigned int max_items);
dd0fc66fb   Al Viro   [PATCH] gfp flags...
283
  int radix_tree_preload(gfp_t gfp_mask);
5e4c0d974   Jan Kara   lib/radix-tree.c:...
284
  int radix_tree_maybe_preload(gfp_t gfp_mask);
c78c66d1d   Kirill A. Shutemov   radix-tree: imple...
285
  int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
286
287
  void radix_tree_init(void);
  void *radix_tree_tag_set(struct radix_tree_root *root,
daff89f32   Jonathan Corbet   [PATCH] radix-tre...
288
  			unsigned long index, unsigned int tag);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
289
  void *radix_tree_tag_clear(struct radix_tree_root *root,
daff89f32   Jonathan Corbet   [PATCH] radix-tre...
290
  			unsigned long index, unsigned int tag);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
291
  int radix_tree_tag_get(struct radix_tree_root *root,
daff89f32   Jonathan Corbet   [PATCH] radix-tre...
292
  			unsigned long index, unsigned int tag);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
293
294
  unsigned int
  radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
daff89f32   Jonathan Corbet   [PATCH] radix-tre...
295
296
  		unsigned long first_index, unsigned int max_items,
  		unsigned int tag);
47feff2c8   Nick Piggin   radix-tree: add g...
297
298
299
300
  unsigned int
  radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
  		unsigned long first_index, unsigned int max_items,
  		unsigned int tag);
ebf8aa44b   Jan Kara   radix-tree: omple...
301
302
303
304
  unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
  		unsigned long *first_indexp, unsigned long last_index,
  		unsigned long nr_to_tag,
  		unsigned int fromtag, unsigned int totag);
daff89f32   Jonathan Corbet   [PATCH] radix-tre...
305
  int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag);
e504f3fdd   Hugh Dickins   tmpfs radix_tree:...
306
  unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
308
309
310
311
  
  static inline void radix_tree_preload_end(void)
  {
  	preempt_enable();
  }
78c1d7848   Konstantin Khlebnikov   radix-tree: intro...
312
313
314
315
  /**
   * struct radix_tree_iter - radix tree iterator state
   *
   * @index:	index of current slot
21ef53393   Ross Zwisler   radix-tree: add s...
316
   * @next_index:	one beyond the last index for this chunk
78c1d7848   Konstantin Khlebnikov   radix-tree: intro...
317
   * @tags:	bit-mask for tag-iterating
21ef53393   Ross Zwisler   radix-tree: add s...
318
   * @shift:	shift for the node that holds our slots
78c1d7848   Konstantin Khlebnikov   radix-tree: intro...
319
320
321
322
323
324
325
326
327
328
329
330
   *
   * This radix tree iterator works in terms of "chunks" of slots.  A chunk is a
   * subinterval of slots contained within one radix tree leaf node.  It is
   * described by a pointer to its first slot and a struct radix_tree_iter
   * which holds the chunk's position in the tree and its size.  For tagged
   * iteration radix_tree_iter also holds the slots' bit-mask for one chosen
   * radix tree tag.
   */
  struct radix_tree_iter {
  	unsigned long	index;
  	unsigned long	next_index;
  	unsigned long	tags;
21ef53393   Ross Zwisler   radix-tree: add s...
331
332
333
  #ifdef CONFIG_RADIX_TREE_MULTIORDER
  	unsigned int	shift;
  #endif
78c1d7848   Konstantin Khlebnikov   radix-tree: intro...
334
  };
21ef53393   Ross Zwisler   radix-tree: add s...
335
336
337
338
339
340
341
342
  static inline unsigned int iter_shift(struct radix_tree_iter *iter)
  {
  #ifdef CONFIG_RADIX_TREE_MULTIORDER
  	return iter->shift;
  #else
  	return 0;
  #endif
  }
78c1d7848   Konstantin Khlebnikov   radix-tree: intro...
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
  #define RADIX_TREE_ITER_TAG_MASK	0x00FF	/* tag index in lower byte */
  #define RADIX_TREE_ITER_TAGGED		0x0100	/* lookup tagged slots */
  #define RADIX_TREE_ITER_CONTIG		0x0200	/* stop at first hole */
  
  /**
   * radix_tree_iter_init - initialize radix tree iterator
   *
   * @iter:	pointer to iterator state
   * @start:	iteration starting index
   * Returns:	NULL
   */
  static __always_inline void **
  radix_tree_iter_init(struct radix_tree_iter *iter, unsigned long start)
  {
  	/*
  	 * Leave iter->tags uninitialized. radix_tree_next_chunk() will fill it
  	 * in the case of a successful tagged chunk lookup.  If the lookup was
  	 * unsuccessful or non-tagged then nobody cares about ->tags.
  	 *
  	 * Set index to zero to bypass next_index overflow protection.
  	 * See the comment in radix_tree_next_chunk() for details.
  	 */
  	iter->index = 0;
  	iter->next_index = start;
  	return NULL;
  }
  
  /**
   * radix_tree_next_chunk - find next chunk of slots for iteration
   *
   * @root:	radix tree root
   * @iter:	iterator state
   * @flags:	RADIX_TREE_ITER_* flags and tag index
   * Returns:	pointer to chunk first slot, or NULL if there no more left
   *
   * This function looks up the next chunk in the radix tree starting from
   * @iter->next_index.  It returns a pointer to the chunk's first slot.
   * Also it fills @iter with data about chunk: position in the tree (index),
   * its end (next_index), and constructs a bit mask for tagged iterating (tags).
   */
  void **radix_tree_next_chunk(struct radix_tree_root *root,
  			     struct radix_tree_iter *iter, unsigned flags);
  
  /**
46437f9a5   Matthew Wilcox   radix-tree: fix r...
387
388
389
390
391
392
393
394
395
396
397
398
   * radix_tree_iter_retry - retry this chunk of the iteration
   * @iter:	iterator state
   *
   * If we iterate over a tree protected only by the RCU lock, a race
   * against deletion or creation may result in seeing a slot for which
   * radix_tree_deref_retry() returns true.  If so, call this function
   * and continue the iteration.
   */
  static inline __must_check
  void **radix_tree_iter_retry(struct radix_tree_iter *iter)
  {
  	iter->next_index = iter->index;
3cb9185c6   Andrey Ryabinin   radix-tree: fix r...
399
  	iter->tags = 0;
46437f9a5   Matthew Wilcox   radix-tree: fix r...
400
401
  	return NULL;
  }
21ef53393   Ross Zwisler   radix-tree: add s...
402
403
404
405
406
  static inline unsigned long
  __radix_tree_iter_add(struct radix_tree_iter *iter, unsigned long slots)
  {
  	return iter->index + (slots << iter_shift(iter));
  }
46437f9a5   Matthew Wilcox   radix-tree: fix r...
407
  /**
7165092fe   Matthew Wilcox   radix-tree,shmem:...
408
409
410
411
412
413
414
415
416
417
   * radix_tree_iter_next - resume iterating when the chunk may be invalid
   * @iter:	iterator state
   *
   * If the iterator needs to release then reacquire a lock, the chunk may
   * have been invalidated by an insertion or deletion.  Call this function
   * to continue the iteration from the next index.
   */
  static inline __must_check
  void **radix_tree_iter_next(struct radix_tree_iter *iter)
  {
21ef53393   Ross Zwisler   radix-tree: add s...
418
  	iter->next_index = __radix_tree_iter_add(iter, 1);
7165092fe   Matthew Wilcox   radix-tree,shmem:...
419
420
421
422
423
  	iter->tags = 0;
  	return NULL;
  }
  
  /**
78c1d7848   Konstantin Khlebnikov   radix-tree: intro...
424
425
426
427
428
   * radix_tree_chunk_size - get current chunk size
   *
   * @iter:	pointer to radix tree iterator
   * Returns:	current chunk size
   */
732042821   Konstantin Khlebnikov   radix-tree: fix o...
429
  static __always_inline long
78c1d7848   Konstantin Khlebnikov   radix-tree: intro...
430
431
  radix_tree_chunk_size(struct radix_tree_iter *iter)
  {
21ef53393   Ross Zwisler   radix-tree: add s...
432
433
  	return (iter->next_index - iter->index) >> iter_shift(iter);
  }
4dd6c0987   Matthew Wilcox   radix-tree: renam...
434
  static inline struct radix_tree_node *entry_to_node(void *ptr)
21ef53393   Ross Zwisler   radix-tree: add s...
435
  {
30ff46ccb   Matthew Wilcox   radix-tree: renam...
436
  	return (void *)((unsigned long)ptr & ~RADIX_TREE_INTERNAL_NODE);
78c1d7848   Konstantin Khlebnikov   radix-tree: intro...
437
438
439
440
441
442
443
444
445
446
447
448
  }
  
  /**
   * radix_tree_next_slot - find next slot in chunk
   *
   * @slot:	pointer to current slot
   * @iter:	pointer to interator state
   * @flags:	RADIX_TREE_ITER_*, should be constant
   * Returns:	pointer to next slot, or NULL if there no more left
   *
   * This function updates @iter->index in the case of a successful lookup.
   * For tagged lookup it also eats @iter->tags.
915045fe1   Ross Zwisler   radix-tree: 'slot...
449
450
451
452
453
454
455
456
   *
   * There are several cases where 'slot' can be passed in as NULL to this
   * function.  These cases result from the use of radix_tree_iter_next() or
   * radix_tree_iter_retry().  In these cases we don't end up dereferencing
   * 'slot' because either:
   * a) we are doing tagged iteration and iter->tags has been set to 0, or
   * b) we are doing non-tagged iteration, and iter->index and iter->next_index
   *    have been set up so that radix_tree_chunk_size() returns 1 or 0.
78c1d7848   Konstantin Khlebnikov   radix-tree: intro...
457
458
459
460
461
   */
  static __always_inline void **
  radix_tree_next_slot(void **slot, struct radix_tree_iter *iter, unsigned flags)
  {
  	if (flags & RADIX_TREE_ITER_TAGGED) {
21ef53393   Ross Zwisler   radix-tree: add s...
462
  		void *canon = slot;
78c1d7848   Konstantin Khlebnikov   radix-tree: intro...
463
  		iter->tags >>= 1;
21ef53393   Ross Zwisler   radix-tree: add s...
464
465
466
  		if (unlikely(!iter->tags))
  			return NULL;
  		while (IS_ENABLED(CONFIG_RADIX_TREE_MULTIORDER) &&
b194d16c2   Matthew Wilcox   radix-tree: renam...
467
  					radix_tree_is_internal_node(slot[1])) {
4dd6c0987   Matthew Wilcox   radix-tree: renam...
468
  			if (entry_to_node(slot[1]) == canon) {
21ef53393   Ross Zwisler   radix-tree: add s...
469
470
471
472
473
474
475
476
  				iter->tags >>= 1;
  				iter->index = __radix_tree_iter_add(iter, 1);
  				slot++;
  				continue;
  			}
  			iter->next_index = __radix_tree_iter_add(iter, 1);
  			return NULL;
  		}
78c1d7848   Konstantin Khlebnikov   radix-tree: intro...
477
  		if (likely(iter->tags & 1ul)) {
21ef53393   Ross Zwisler   radix-tree: add s...
478
  			iter->index = __radix_tree_iter_add(iter, 1);
78c1d7848   Konstantin Khlebnikov   radix-tree: intro...
479
480
  			return slot + 1;
  		}
21ef53393   Ross Zwisler   radix-tree: add s...
481
  		if (!(flags & RADIX_TREE_ITER_CONTIG)) {
78c1d7848   Konstantin Khlebnikov   radix-tree: intro...
482
483
484
  			unsigned offset = __ffs(iter->tags);
  
  			iter->tags >>= offset;
21ef53393   Ross Zwisler   radix-tree: add s...
485
  			iter->index = __radix_tree_iter_add(iter, offset + 1);
78c1d7848   Konstantin Khlebnikov   radix-tree: intro...
486
487
488
  			return slot + offset + 1;
  		}
  	} else {
21ef53393   Ross Zwisler   radix-tree: add s...
489
490
  		long count = radix_tree_chunk_size(iter);
  		void *canon = slot;
78c1d7848   Konstantin Khlebnikov   radix-tree: intro...
491

21ef53393   Ross Zwisler   radix-tree: add s...
492
  		while (--count > 0) {
78c1d7848   Konstantin Khlebnikov   radix-tree: intro...
493
  			slot++;
21ef53393   Ross Zwisler   radix-tree: add s...
494
495
496
  			iter->index = __radix_tree_iter_add(iter, 1);
  
  			if (IS_ENABLED(CONFIG_RADIX_TREE_MULTIORDER) &&
b194d16c2   Matthew Wilcox   radix-tree: renam...
497
  			    radix_tree_is_internal_node(*slot)) {
4dd6c0987   Matthew Wilcox   radix-tree: renam...
498
  				if (entry_to_node(*slot) == canon)
21ef53393   Ross Zwisler   radix-tree: add s...
499
  					continue;
4dd6c0987   Matthew Wilcox   radix-tree: renam...
500
501
  				iter->next_index = iter->index;
  				break;
21ef53393   Ross Zwisler   radix-tree: add s...
502
  			}
78c1d7848   Konstantin Khlebnikov   radix-tree: intro...
503
504
  			if (likely(*slot))
  				return slot;
fffaee365   Konstantin Khlebnikov   radix-tree: fix c...
505
506
507
  			if (flags & RADIX_TREE_ITER_CONTIG) {
  				/* forbid switching to the next chunk */
  				iter->next_index = 0;
78c1d7848   Konstantin Khlebnikov   radix-tree: intro...
508
  				break;
fffaee365   Konstantin Khlebnikov   radix-tree: fix c...
509
  			}
78c1d7848   Konstantin Khlebnikov   radix-tree: intro...
510
511
512
513
514
515
  		}
  	}
  	return NULL;
  }
  
  /**
78c1d7848   Konstantin Khlebnikov   radix-tree: intro...
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
   * radix_tree_for_each_slot - iterate over non-empty slots
   *
   * @slot:	the void** variable for pointer to slot
   * @root:	the struct radix_tree_root pointer
   * @iter:	the struct radix_tree_iter pointer
   * @start:	iteration starting index
   *
   * @slot points to radix tree slot, @iter->index contains its index.
   */
  #define radix_tree_for_each_slot(slot, root, iter, start)		\
  	for (slot = radix_tree_iter_init(iter, start) ;			\
  	     slot || (slot = radix_tree_next_chunk(root, iter, 0)) ;	\
  	     slot = radix_tree_next_slot(slot, iter, 0))
  
  /**
   * radix_tree_for_each_contig - iterate over contiguous slots
   *
   * @slot:	the void** variable for pointer to slot
   * @root:	the struct radix_tree_root pointer
   * @iter:	the struct radix_tree_iter pointer
   * @start:	iteration starting index
   *
   * @slot points to radix tree slot, @iter->index contains its index.
   */
  #define radix_tree_for_each_contig(slot, root, iter, start)		\
  	for (slot = radix_tree_iter_init(iter, start) ;			\
  	     slot || (slot = radix_tree_next_chunk(root, iter,		\
  				RADIX_TREE_ITER_CONTIG)) ;		\
  	     slot = radix_tree_next_slot(slot, iter,			\
  				RADIX_TREE_ITER_CONTIG))
  
  /**
   * radix_tree_for_each_tagged - iterate over tagged slots
   *
   * @slot:	the void** variable for pointer to slot
   * @root:	the struct radix_tree_root pointer
   * @iter:	the struct radix_tree_iter pointer
   * @start:	iteration starting index
   * @tag:	tag index
   *
   * @slot points to radix tree slot, @iter->index contains its index.
   */
  #define radix_tree_for_each_tagged(slot, root, iter, start, tag)	\
  	for (slot = radix_tree_iter_init(iter, start) ;			\
  	     slot || (slot = radix_tree_next_chunk(root, iter,		\
  			      RADIX_TREE_ITER_TAGGED | tag)) ;		\
  	     slot = radix_tree_next_slot(slot, iter,			\
  				RADIX_TREE_ITER_TAGGED))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
564
  #endif /* _LINUX_RADIX_TREE_H */