David Sterba | 9888c34 | 2018-04-03 19:16:55 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Arne Jansen | da5c813 | 2011-09-13 12:29:12 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011 STRATO AG |
| 4 | * written by Arne Jansen <sensille@gmx.net> |
Arne Jansen | da5c813 | 2011-09-13 12:29:12 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
David Sterba | 9888c34 | 2018-04-03 19:16:55 +0200 | [diff] [blame] | 7 | #ifndef BTRFS_ULIST_H |
| 8 | #define BTRFS_ULIST_H |
Arne Jansen | da5c813 | 2011-09-13 12:29:12 +0200 | [diff] [blame] | 9 | |
Wang Shilong | f7f82b8 | 2013-04-12 12:12:17 +0000 | [diff] [blame] | 10 | #include <linux/list.h> |
| 11 | #include <linux/rbtree.h> |
| 12 | |
Arne Jansen | da5c813 | 2011-09-13 12:29:12 +0200 | [diff] [blame] | 13 | /* |
| 14 | * ulist is a generic data structure to hold a collection of unique u64 |
| 15 | * values. The only operations it supports is adding to the list and |
| 16 | * enumerating it. |
| 17 | * It is possible to store an auxiliary value along with the key. |
| 18 | * |
Arne Jansen | da5c813 | 2011-09-13 12:29:12 +0200 | [diff] [blame] | 19 | */ |
Jan Schmidt | cd1b413 | 2012-05-22 14:56:50 +0200 | [diff] [blame] | 20 | struct ulist_iterator { |
Wang Shilong | 4c7a6f7 | 2014-01-29 00:25:34 +0800 | [diff] [blame] | 21 | struct list_head *cur_list; /* hint to start search */ |
Jan Schmidt | cd1b413 | 2012-05-22 14:56:50 +0200 | [diff] [blame] | 22 | }; |
| 23 | |
Arne Jansen | da5c813 | 2011-09-13 12:29:12 +0200 | [diff] [blame] | 24 | /* |
| 25 | * element of the list |
| 26 | */ |
| 27 | struct ulist_node { |
| 28 | u64 val; /* value to store */ |
Alexander Block | 34d73f5 | 2012-07-28 16:18:58 +0200 | [diff] [blame] | 29 | u64 aux; /* auxiliary value saved along with the val */ |
Wang Shilong | 4c7a6f7 | 2014-01-29 00:25:34 +0800 | [diff] [blame] | 30 | |
Wang Shilong | 4c7a6f7 | 2014-01-29 00:25:34 +0800 | [diff] [blame] | 31 | struct list_head list; /* used to link node */ |
Wang Shilong | f7f82b8 | 2013-04-12 12:12:17 +0000 | [diff] [blame] | 32 | struct rb_node rb_node; /* used to speed up search */ |
Arne Jansen | da5c813 | 2011-09-13 12:29:12 +0200 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | struct ulist { |
| 36 | /* |
| 37 | * number of elements stored in list |
| 38 | */ |
| 39 | unsigned long nnodes; |
| 40 | |
Wang Shilong | 4c7a6f7 | 2014-01-29 00:25:34 +0800 | [diff] [blame] | 41 | struct list_head nodes; |
Wang Shilong | f7f82b8 | 2013-04-12 12:12:17 +0000 | [diff] [blame] | 42 | struct rb_root root; |
Arne Jansen | da5c813 | 2011-09-13 12:29:12 +0200 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | void ulist_init(struct ulist *ulist); |
David Sterba | 6655bc3 | 2017-02-15 16:47:36 +0100 | [diff] [blame] | 46 | void ulist_release(struct ulist *ulist); |
Arne Jansen | da5c813 | 2011-09-13 12:29:12 +0200 | [diff] [blame] | 47 | void ulist_reinit(struct ulist *ulist); |
Daniel J Blueman | 2eec6c8 | 2012-04-26 00:37:14 +0800 | [diff] [blame] | 48 | struct ulist *ulist_alloc(gfp_t gfp_mask); |
Arne Jansen | da5c813 | 2011-09-13 12:29:12 +0200 | [diff] [blame] | 49 | void ulist_free(struct ulist *ulist); |
Alexander Block | 34d73f5 | 2012-07-28 16:18:58 +0200 | [diff] [blame] | 50 | int ulist_add(struct ulist *ulist, u64 val, u64 aux, gfp_t gfp_mask); |
| 51 | int ulist_add_merge(struct ulist *ulist, u64 val, u64 aux, |
| 52 | u64 *old_aux, gfp_t gfp_mask); |
Qu Wenruo | d4b8040 | 2015-04-20 09:26:02 +0800 | [diff] [blame] | 53 | int ulist_del(struct ulist *ulist, u64 val, u64 aux); |
Takashi Iwai | 4eb1f66 | 2014-07-28 10:57:04 +0200 | [diff] [blame] | 54 | |
| 55 | /* just like ulist_add_merge() but take a pointer for the aux data */ |
| 56 | static inline int ulist_add_merge_ptr(struct ulist *ulist, u64 val, void *aux, |
| 57 | void **old_aux, gfp_t gfp_mask) |
| 58 | { |
| 59 | #if BITS_PER_LONG == 32 |
| 60 | u64 old64 = (uintptr_t)*old_aux; |
| 61 | int ret = ulist_add_merge(ulist, val, (uintptr_t)aux, &old64, gfp_mask); |
| 62 | *old_aux = (void *)((uintptr_t)old64); |
| 63 | return ret; |
| 64 | #else |
| 65 | return ulist_add_merge(ulist, val, (u64)aux, (u64 *)old_aux, gfp_mask); |
| 66 | #endif |
| 67 | } |
| 68 | |
Jan Schmidt | cd1b413 | 2012-05-22 14:56:50 +0200 | [diff] [blame] | 69 | struct ulist_node *ulist_next(struct ulist *ulist, |
| 70 | struct ulist_iterator *uiter); |
| 71 | |
Wang Shilong | 4c7a6f7 | 2014-01-29 00:25:34 +0800 | [diff] [blame] | 72 | #define ULIST_ITER_INIT(uiter) ((uiter)->cur_list = NULL) |
Arne Jansen | da5c813 | 2011-09-13 12:29:12 +0200 | [diff] [blame] | 73 | |
| 74 | #endif |