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