staging: zcache: cleanup the code between tmem_obj_init and tmem_obj_find

tmem_obj_find and insertion tmem-obj have the some logic, we can integrate
the code

Signed-off-by: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com>
Acked-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Xiao Guangrong 2012-06-26 16:52:50 +08:00 committed by Greg Kroah-Hartman
parent 08b0b50048
commit c666e636ac
1 changed files with 32 additions and 29 deletions

View File

@ -72,33 +72,49 @@ void tmem_register_pamops(struct tmem_pamops *m)
* the hashbucket lock must be held. * the hashbucket lock must be held.
*/ */
/* searches for object==oid in pool, returns locked object if found */ static struct tmem_obj
static struct tmem_obj *tmem_obj_find(struct tmem_hashbucket *hb, *__tmem_obj_find(struct tmem_hashbucket*hb, struct tmem_oid *oidp,
struct tmem_oid *oidp) struct rb_node **parent, struct rb_node ***link)
{ {
struct rb_node *rbnode; struct rb_node *_parent = NULL, **rbnode;
struct tmem_obj *obj; struct tmem_obj *obj = NULL;
rbnode = hb->obj_rb_root.rb_node; rbnode = &hb->obj_rb_root.rb_node;
while (rbnode) { while (*rbnode) {
BUG_ON(RB_EMPTY_NODE(rbnode)); BUG_ON(RB_EMPTY_NODE(*rbnode));
obj = rb_entry(rbnode, struct tmem_obj, rb_tree_node); _parent = *rbnode;
obj = rb_entry(*rbnode, struct tmem_obj,
rb_tree_node);
switch (tmem_oid_compare(oidp, &obj->oid)) { switch (tmem_oid_compare(oidp, &obj->oid)) {
case 0: /* equal */ case 0: /* equal */
goto out; goto out;
case -1: case -1:
rbnode = rbnode->rb_left; rbnode = &(*rbnode)->rb_left;
break; break;
case 1: case 1:
rbnode = rbnode->rb_right; rbnode = &(*rbnode)->rb_right;
break; break;
} }
} }
if (parent)
*parent = _parent;
if (link)
*link = rbnode;
obj = NULL; obj = NULL;
out: out:
return obj; return obj;
} }
/* searches for object==oid in pool, returns locked object if found */
static struct tmem_obj *tmem_obj_find(struct tmem_hashbucket *hb,
struct tmem_oid *oidp)
{
return __tmem_obj_find(hb, oidp, NULL, NULL);
}
static void tmem_pampd_destroy_all_in_obj(struct tmem_obj *); static void tmem_pampd_destroy_all_in_obj(struct tmem_obj *);
/* free an object that has no more pampds in it */ /* free an object that has no more pampds in it */
@ -131,8 +147,7 @@ static void tmem_obj_init(struct tmem_obj *obj, struct tmem_hashbucket *hb,
struct tmem_oid *oidp) struct tmem_oid *oidp)
{ {
struct rb_root *root = &hb->obj_rb_root; struct rb_root *root = &hb->obj_rb_root;
struct rb_node **new = &(root->rb_node), *parent = NULL; struct rb_node **new = NULL, *parent = NULL;
struct tmem_obj *this;
BUG_ON(pool == NULL); BUG_ON(pool == NULL);
atomic_inc(&pool->obj_count); atomic_inc(&pool->obj_count);
@ -144,22 +159,10 @@ static void tmem_obj_init(struct tmem_obj *obj, struct tmem_hashbucket *hb,
obj->pampd_count = 0; obj->pampd_count = 0;
(*tmem_pamops.new_obj)(obj); (*tmem_pamops.new_obj)(obj);
SET_SENTINEL(obj, OBJ); SET_SENTINEL(obj, OBJ);
while (*new) {
BUG_ON(RB_EMPTY_NODE(*new)); if (__tmem_obj_find(hb, oidp, &parent, &new))
this = rb_entry(*new, struct tmem_obj, rb_tree_node); BUG();
parent = *new;
switch (tmem_oid_compare(oidp, &this->oid)) {
case 0:
BUG(); /* already present; should never happen! */
break;
case -1:
new = &(*new)->rb_left;
break;
case 1:
new = &(*new)->rb_right;
break;
}
}
rb_link_node(&obj->rb_tree_node, parent, new); rb_link_node(&obj->rb_tree_node, parent, new);
rb_insert_color(&obj->rb_tree_node, root); rb_insert_color(&obj->rb_tree_node, root);
} }