blob: a01a2c45825f5c8cfa569218850acd91c3cf3e1c [file] [log] [blame]
Arne Jansenda5c8132011-09-13 12:29:12 +02001/*
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 Shilongf7f82b82013-04-12 12:12:17 +000011#include <linux/list.h>
12#include <linux/rbtree.h>
13
Arne Jansenda5c8132011-09-13 12:29:12 +020014/*
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 Jansenda5c8132011-09-13 12:29:12 +020020 */
Jan Schmidtcd1b4132012-05-22 14:56:50 +020021struct ulist_iterator {
Wang Shilong4c7a6f72014-01-29 00:25:34 +080022#ifdef CONFIG_BTRFS_DEBUG
Jan Schmidtcd1b4132012-05-22 14:56:50 +020023 int i;
Wang Shilong4c7a6f72014-01-29 00:25:34 +080024#endif
25 struct list_head *cur_list; /* hint to start search */
Jan Schmidtcd1b4132012-05-22 14:56:50 +020026};
27
Arne Jansenda5c8132011-09-13 12:29:12 +020028/*
29 * element of the list
30 */
31struct ulist_node {
32 u64 val; /* value to store */
Alexander Block34d73f52012-07-28 16:18:58 +020033 u64 aux; /* auxiliary value saved along with the val */
Wang Shilong4c7a6f72014-01-29 00:25:34 +080034
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 Shilongf7f82b82013-04-12 12:12:17 +000040 struct rb_node rb_node; /* used to speed up search */
Arne Jansenda5c8132011-09-13 12:29:12 +020041};
42
43struct ulist {
44 /*
45 * number of elements stored in list
46 */
47 unsigned long nnodes;
48
Wang Shilong4c7a6f72014-01-29 00:25:34 +080049 struct list_head nodes;
Wang Shilongf7f82b82013-04-12 12:12:17 +000050 struct rb_root root;
Arne Jansenda5c8132011-09-13 12:29:12 +020051};
52
53void ulist_init(struct ulist *ulist);
Arne Jansenda5c8132011-09-13 12:29:12 +020054void ulist_reinit(struct ulist *ulist);
Daniel J Blueman2eec6c82012-04-26 00:37:14 +080055struct ulist *ulist_alloc(gfp_t gfp_mask);
Arne Jansenda5c8132011-09-13 12:29:12 +020056void ulist_free(struct ulist *ulist);
Alexander Block34d73f52012-07-28 16:18:58 +020057int ulist_add(struct ulist *ulist, u64 val, u64 aux, gfp_t gfp_mask);
58int ulist_add_merge(struct ulist *ulist, u64 val, u64 aux,
59 u64 *old_aux, gfp_t gfp_mask);
Qu Wenruod4b80402015-04-20 09:26:02 +080060int ulist_del(struct ulist *ulist, u64 val, u64 aux);
Takashi Iwai4eb1f662014-07-28 10:57:04 +020061
62/* just like ulist_add_merge() but take a pointer for the aux data */
63static 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 Schmidtcd1b4132012-05-22 14:56:50 +020076struct ulist_node *ulist_next(struct ulist *ulist,
77 struct ulist_iterator *uiter);
78
Wang Shilong4c7a6f72014-01-29 00:25:34 +080079#define ULIST_ITER_INIT(uiter) ((uiter)->cur_list = NULL)
Arne Jansenda5c8132011-09-13 12:29:12 +020080
81#endif