]> err.no Git - linux-2.6/blobdiff - lib/klist.c
klist: implement klist_add_{after|before}()
[linux-2.6] / lib / klist.c
index 9c94f0b163a11a713299f97876012296f63d51d7..ebba9488046ea7b8ed7ad01d66242c0efc703653 100644 (file)
@@ -120,15 +120,46 @@ void klist_add_tail(struct klist_node * n, struct klist * k)
 EXPORT_SYMBOL_GPL(klist_add_tail);
 
 
+/**
+ * klist_add_after - Init a klist_node and add it after an existing node
+ * @n: node we're adding.
+ * @pos: node to put @n after
+ */
+void klist_add_after(struct klist_node *n, struct klist_node *pos)
+{
+       struct klist *k = pos->n_klist;
+
+       klist_node_init(k, n);
+       spin_lock(&k->k_lock);
+       list_add(&n->n_node, &pos->n_node);
+       spin_unlock(&k->k_lock);
+}
+EXPORT_SYMBOL_GPL(klist_add_after);
+
+/**
+ * klist_add_before - Init a klist_node and add it before an existing node
+ * @n: node we're adding.
+ * @pos: node to put @n after
+ */
+void klist_add_before(struct klist_node *n, struct klist_node *pos)
+{
+       struct klist *k = pos->n_klist;
+
+       klist_node_init(k, n);
+       spin_lock(&k->k_lock);
+       list_add_tail(&n->n_node, &pos->n_node);
+       spin_unlock(&k->k_lock);
+}
+EXPORT_SYMBOL_GPL(klist_add_before);
+
+
 static void klist_release(struct kref * kref)
 {
        struct klist_node * n = container_of(kref, struct klist_node, n_ref);
-       void (*put)(struct klist_node *) = n->n_klist->put;
+
        list_del(&n->n_node);
        complete(&n->n_removed);
        n->n_klist = NULL;
-       if (put)
-               put(n);
 }
 
 static int klist_dec_and_del(struct klist_node * n)
@@ -145,10 +176,14 @@ static int klist_dec_and_del(struct klist_node * n)
 void klist_del(struct klist_node * n)
 {
        struct klist * k = n->n_klist;
+       void (*put)(struct klist_node *) = k->put;
 
        spin_lock(&k->k_lock);
-       klist_dec_and_del(n);
+       if (!klist_dec_and_del(n))
+               put = NULL;
        spin_unlock(&k->k_lock);
+       if (put)
+               put(n);
 }
 
 EXPORT_SYMBOL_GPL(klist_del);
@@ -161,10 +196,7 @@ EXPORT_SYMBOL_GPL(klist_del);
 
 void klist_remove(struct klist_node * n)
 {
-       struct klist * k = n->n_klist;
-       spin_lock(&k->k_lock);
-       klist_dec_and_del(n);
-       spin_unlock(&k->k_lock);
+       klist_del(n);
        wait_for_completion(&n->n_removed);
 }
 
@@ -260,12 +292,15 @@ static struct klist_node * to_klist_node(struct list_head * n)
 struct klist_node * klist_next(struct klist_iter * i)
 {
        struct list_head * next;
+       struct klist_node * lnode = i->i_cur;
        struct klist_node * knode = NULL;
+       void (*put)(struct klist_node *) = i->i_klist->put;
 
        spin_lock(&i->i_klist->k_lock);
-       if (i->i_cur) {
-               next = i->i_cur->n_node.next;
-               klist_dec_and_del(i->i_cur);
+       if (lnode) {
+               next = lnode->n_node.next;
+               if (!klist_dec_and_del(lnode))
+                       put = NULL;
        } else
                next = i->i_head->next;
 
@@ -275,6 +310,8 @@ struct klist_node * klist_next(struct klist_iter * i)
        }
        i->i_cur = knode;
        spin_unlock(&i->i_klist->k_lock);
+       if (put && lnode)
+               put(lnode);
        return knode;
 }