Commit 4c7a6f74 authored by Wang Shilong's avatar Wang Shilong Committed by Chris Mason

Btrfs: rework ulist with list+rb_tree

We are really suffering from now ulist's implementation, some developers
gave their try, and i just gave some of my ideas for things:

 1. use list+rb_tree instead of arrary+rb_tree

 2. add cur_list to iterator rather than ulist structure.

 3. add seqnum into every node when they are added, this is
 used to do selfcheck when iterating node.

I noticed Zach Brown's comments before, long term is to kick off
ulist implementation, however, for now, we need at least avoid
arrary from ulist.

Cc: Liu Bo <bo.li.liu@oracle.com>
Cc: Josef Bacik <jbacik@fb.com>
Cc: Zach Brown <zab@redhat.com>
Signed-off-by: default avatarWang Shilong <wangsl.fnst@cn.fujitsu.com>
Signed-off-by: default avatarJosef Bacik <jbacik@fb.com>
Signed-off-by: default avatarChris Mason <clm@fb.com>
parent f05c4746
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/export.h> #include <linux/export.h>
#include "ulist.h" #include "ulist.h"
#include "ctree.h"
/* /*
* ulist is a generic data structure to hold a collection of unique u64 * ulist is a generic data structure to hold a collection of unique u64
...@@ -14,10 +15,6 @@ ...@@ -14,10 +15,6 @@
* enumerating it. * enumerating it.
* It is possible to store an auxiliary value along with the key. * It is possible to store an auxiliary value along with the key.
* *
* The implementation is preliminary and can probably be sped up
* significantly. A first step would be to store the values in an rbtree
* as soon as ULIST_SIZE is exceeded.
*
* A sample usage for ulists is the enumeration of directed graphs without * A sample usage for ulists is the enumeration of directed graphs without
* visiting a node twice. The pseudo-code could look like this: * visiting a node twice. The pseudo-code could look like this:
* *
...@@ -50,10 +47,9 @@ ...@@ -50,10 +47,9 @@
*/ */
void ulist_init(struct ulist *ulist) void ulist_init(struct ulist *ulist)
{ {
ulist->nnodes = 0; INIT_LIST_HEAD(&ulist->nodes);
ulist->nodes = ulist->int_nodes;
ulist->nodes_alloced = ULIST_SIZE;
ulist->root = RB_ROOT; ulist->root = RB_ROOT;
ulist->nnodes = 0;
} }
EXPORT_SYMBOL(ulist_init); EXPORT_SYMBOL(ulist_init);
...@@ -66,14 +62,14 @@ EXPORT_SYMBOL(ulist_init); ...@@ -66,14 +62,14 @@ EXPORT_SYMBOL(ulist_init);
*/ */
void ulist_fini(struct ulist *ulist) void ulist_fini(struct ulist *ulist)
{ {
/* struct ulist_node *node;
* The first ULIST_SIZE elements are stored inline in struct ulist. struct ulist_node *next;
* Only if more elements are alocated they need to be freed.
*/ list_for_each_entry_safe(node, next, &ulist->nodes, list) {
if (ulist->nodes_alloced > ULIST_SIZE) kfree(node);
kfree(ulist->nodes); }
ulist->nodes_alloced = 0; /* in case ulist_fini is called twice */
ulist->root = RB_ROOT; ulist->root = RB_ROOT;
INIT_LIST_HEAD(&ulist->nodes);
} }
EXPORT_SYMBOL(ulist_fini); EXPORT_SYMBOL(ulist_fini);
...@@ -192,57 +188,29 @@ int ulist_add(struct ulist *ulist, u64 val, u64 aux, gfp_t gfp_mask) ...@@ -192,57 +188,29 @@ int ulist_add(struct ulist *ulist, u64 val, u64 aux, gfp_t gfp_mask)
int ulist_add_merge(struct ulist *ulist, u64 val, u64 aux, int ulist_add_merge(struct ulist *ulist, u64 val, u64 aux,
u64 *old_aux, gfp_t gfp_mask) u64 *old_aux, gfp_t gfp_mask)
{ {
int ret = 0; int ret;
struct ulist_node *node = NULL; struct ulist_node *node;
node = ulist_rbtree_search(ulist, val); node = ulist_rbtree_search(ulist, val);
if (node) { if (node) {
if (old_aux) if (old_aux)
*old_aux = node->aux; *old_aux = node->aux;
return 0; return 0;
} }
node = kmalloc(sizeof(*node), gfp_mask);
if (!node)
return -ENOMEM;
if (ulist->nnodes >= ulist->nodes_alloced) { node->val = val;
u64 new_alloced = ulist->nodes_alloced + 128; node->aux = aux;
struct ulist_node *new_nodes; #ifdef CONFIG_BTRFS_DEBUG
void *old = NULL; node->seqnum = ulist->nnodes;
int i; #endif
/*
* if nodes_alloced == ULIST_SIZE no memory has been allocated
* yet, so pass NULL to krealloc
*/
if (ulist->nodes_alloced > ULIST_SIZE)
old = ulist->nodes;
new_nodes = krealloc(old, sizeof(*new_nodes) * new_alloced, ret = ulist_rbtree_insert(ulist, node);
gfp_mask); ASSERT(!ret);
if (!new_nodes) list_add_tail(&node->list, &ulist->nodes);
return -ENOMEM; ulist->nnodes++;
if (!old)
memcpy(new_nodes, ulist->int_nodes,
sizeof(ulist->int_nodes));
ulist->nodes = new_nodes;
ulist->nodes_alloced = new_alloced;
/*
* krealloc actually uses memcpy, which does not copy rb_node
* pointers, so we have to do it ourselves. Otherwise we may
* be bitten by crashes.
*/
ulist->root = RB_ROOT;
for (i = 0; i < ulist->nnodes; i++) {
ret = ulist_rbtree_insert(ulist, &ulist->nodes[i]);
if (ret < 0)
return ret;
}
}
ulist->nodes[ulist->nnodes].val = val;
ulist->nodes[ulist->nnodes].aux = aux;
ret = ulist_rbtree_insert(ulist, &ulist->nodes[ulist->nnodes]);
BUG_ON(ret);
++ulist->nnodes;
return 1; return 1;
} }
...@@ -266,11 +234,26 @@ EXPORT_SYMBOL(ulist_add); ...@@ -266,11 +234,26 @@ EXPORT_SYMBOL(ulist_add);
*/ */
struct ulist_node *ulist_next(struct ulist *ulist, struct ulist_iterator *uiter) struct ulist_node *ulist_next(struct ulist *ulist, struct ulist_iterator *uiter)
{ {
if (ulist->nnodes == 0) struct ulist_node *node;
if (list_empty(&ulist->nodes))
return NULL; return NULL;
if (uiter->i < 0 || uiter->i >= ulist->nnodes) if (uiter->cur_list && uiter->cur_list->next == &ulist->nodes)
return NULL; return NULL;
if (uiter->cur_list) {
return &ulist->nodes[uiter->i++]; uiter->cur_list = uiter->cur_list->next;
} else {
uiter->cur_list = ulist->nodes.next;
#ifdef CONFIG_BTRFS_DEBUG
uiter->i = 0;
#endif
}
node = list_entry(uiter->cur_list, struct ulist_node, list);
#ifdef CONFIG_BTRFS_DEBUG
ASSERT(node->seqnum == uiter->i);
ASSERT(uiter->i >= 0 && uiter->i < ulist->nnodes);
uiter->i++;
#endif
return node;
} }
EXPORT_SYMBOL(ulist_next); EXPORT_SYMBOL(ulist_next);
...@@ -17,18 +17,12 @@ ...@@ -17,18 +17,12 @@
* enumerating it. * enumerating it.
* It is possible to store an auxiliary value along with the key. * It is possible to store an auxiliary value along with the key.
* *
* The implementation is preliminary and can probably be sped up
* significantly. A first step would be to store the values in an rbtree
* as soon as ULIST_SIZE is exceeded.
*/ */
/*
* number of elements statically allocated inside struct ulist
*/
#define ULIST_SIZE 16
struct ulist_iterator { struct ulist_iterator {
#ifdef CONFIG_BTRFS_DEBUG
int i; int i;
#endif
struct list_head *cur_list; /* hint to start search */
}; };
/* /*
...@@ -37,6 +31,12 @@ struct ulist_iterator { ...@@ -37,6 +31,12 @@ struct ulist_iterator {
struct ulist_node { struct ulist_node {
u64 val; /* value to store */ u64 val; /* value to store */
u64 aux; /* auxiliary value saved along with the val */ u64 aux; /* auxiliary value saved along with the val */
#ifdef CONFIG_BTRFS_DEBUG
int seqnum; /* sequence number this node is added */
#endif
struct list_head list; /* used to link node */
struct rb_node rb_node; /* used to speed up search */ struct rb_node rb_node; /* used to speed up search */
}; };
...@@ -46,24 +46,8 @@ struct ulist { ...@@ -46,24 +46,8 @@ struct ulist {
*/ */
unsigned long nnodes; unsigned long nnodes;
/* struct list_head nodes;
* number of nodes we already have room for
*/
unsigned long nodes_alloced;
/*
* pointer to the array storing the elements. The first ULIST_SIZE
* elements are stored inline. In this case the it points to int_nodes.
* After exceeding ULIST_SIZE, dynamic memory is allocated.
*/
struct ulist_node *nodes;
struct rb_root root; struct rb_root root;
/*
* inline storage space for the first ULIST_SIZE entries
*/
struct ulist_node int_nodes[ULIST_SIZE];
}; };
void ulist_init(struct ulist *ulist); void ulist_init(struct ulist *ulist);
...@@ -77,6 +61,6 @@ int ulist_add_merge(struct ulist *ulist, u64 val, u64 aux, ...@@ -77,6 +61,6 @@ int ulist_add_merge(struct ulist *ulist, u64 val, u64 aux,
struct ulist_node *ulist_next(struct ulist *ulist, struct ulist_node *ulist_next(struct ulist *ulist,
struct ulist_iterator *uiter); struct ulist_iterator *uiter);
#define ULIST_ITER_INIT(uiter) ((uiter)->i = 0) #define ULIST_ITER_INIT(uiter) ((uiter)->cur_list = NULL)
#endif #endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment