Revert "ARM: tegra: tegratab: dummy change"
[linux-2.6.git] / crypto / algapi.c
index 6a98076..b4c046c 100644 (file)
 #include <linux/list.h>
 #include <linux/module.h>
 #include <linux/rtnetlink.h>
+#include <linux/slab.h>
 #include <linux/string.h>
 
 #include "internal.h"
 
-static void crypto_remove_final(struct list_head *list);
-
 static LIST_HEAD(crypto_template_list);
 
 void crypto_larval_error(const char *name, u32 type, u32 mask)
@@ -81,16 +80,35 @@ static void crypto_destroy_instance(struct crypto_alg *alg)
        crypto_tmpl_put(tmpl);
 }
 
+static struct list_head *crypto_more_spawns(struct crypto_alg *alg,
+                                           struct list_head *stack,
+                                           struct list_head *top,
+                                           struct list_head *secondary_spawns)
+{
+       struct crypto_spawn *spawn, *n;
+
+       if (list_empty(stack))
+               return NULL;
+
+       spawn = list_first_entry(stack, struct crypto_spawn, list);
+       n = list_entry(spawn->list.next, struct crypto_spawn, list);
+
+       if (spawn->alg && &n->list != stack && !n->alg)
+               n->alg = (n->list.next == stack) ? alg :
+                        &list_entry(n->list.next, struct crypto_spawn,
+                                    list)->inst->alg;
+
+       list_move(&spawn->list, secondary_spawns);
+
+       return &n->list == stack ? top : &n->inst->alg.cra_users;
+}
+
 static void crypto_remove_spawn(struct crypto_spawn *spawn,
-                               struct list_head *list,
-                               struct list_head *secondary_spawns)
+                               struct list_head *list)
 {
        struct crypto_instance *inst = spawn->inst;
        struct crypto_template *tmpl = inst->tmpl;
 
-       list_del_init(&spawn->list);
-       spawn->alg = NULL;
-
        if (crypto_is_dead(&inst->alg))
                return;
 
@@ -106,27 +124,57 @@ static void crypto_remove_spawn(struct crypto_spawn *spawn,
        hlist_del(&inst->list);
        inst->alg.cra_destroy = crypto_destroy_instance;
 
-       list_splice(&inst->alg.cra_users, secondary_spawns);
+       BUG_ON(!list_empty(&inst->alg.cra_users));
 }
 
-static void crypto_remove_spawns(struct list_head *spawns,
-                                struct list_head *list, u32 new_type)
+void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
+                         struct crypto_alg *nalg)
 {
+       u32 new_type = (nalg ?: alg)->cra_flags;
        struct crypto_spawn *spawn, *n;
        LIST_HEAD(secondary_spawns);
+       struct list_head *spawns;
+       LIST_HEAD(stack);
+       LIST_HEAD(top);
 
+       spawns = &alg->cra_users;
        list_for_each_entry_safe(spawn, n, spawns, list) {
                if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
                        continue;
 
-               crypto_remove_spawn(spawn, list, &secondary_spawns);
+               list_move(&spawn->list, &top);
        }
 
-       while (!list_empty(&secondary_spawns)) {
-               list_for_each_entry_safe(spawn, n, &secondary_spawns, list)
-                       crypto_remove_spawn(spawn, list, &secondary_spawns);
+       spawns = &top;
+       do {
+               while (!list_empty(spawns)) {
+                       struct crypto_instance *inst;
+
+                       spawn = list_first_entry(spawns, struct crypto_spawn,
+                                                list);
+                       inst = spawn->inst;
+
+                       BUG_ON(&inst->alg == alg);
+
+                       list_move(&spawn->list, &stack);
+
+                       if (&inst->alg == nalg)
+                               break;
+
+                       spawn->alg = NULL;
+                       spawns = &inst->alg.cra_users;
+               }
+       } while ((spawns = crypto_more_spawns(alg, &stack, &top,
+                                             &secondary_spawns)));
+
+       list_for_each_entry_safe(spawn, n, &secondary_spawns, list) {
+               if (spawn->alg)
+                       list_move(&spawn->list, &spawn->alg->cra_users);
+               else
+                       crypto_remove_spawn(spawn, list);
        }
 }
+EXPORT_SYMBOL_GPL(crypto_remove_spawns);
 
 static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg)
 {
@@ -181,7 +229,7 @@ static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg)
        list_add(&alg->cra_list, &crypto_alg_list);
        list_add(&larval->alg.cra_list, &crypto_alg_list);
 
-out:   
+out:
        return larval;
 
 free_larval:
@@ -258,7 +306,7 @@ found:
                    q->cra_priority > alg->cra_priority)
                        continue;
 
-               crypto_remove_spawns(&q->cra_users, &list, alg->cra_flags);
+               crypto_remove_spawns(q, &list, alg);
        }
 
 complete:
@@ -271,7 +319,7 @@ unlock:
 }
 EXPORT_SYMBOL_GPL(crypto_alg_tested);
 
-static void crypto_remove_final(struct list_head *list)
+void crypto_remove_final(struct list_head *list)
 {
        struct crypto_alg *alg;
        struct crypto_alg *n;
@@ -281,6 +329,7 @@ static void crypto_remove_final(struct list_head *list)
                crypto_alg_put(alg);
        }
 }
+EXPORT_SYMBOL_GPL(crypto_remove_final);
 
 static void crypto_wait_for_test(struct crypto_larval *larval)
 {
@@ -330,7 +379,7 @@ static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
 
        crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
        list_del_init(&alg->cra_list);
-       crypto_remove_spawns(&alg->cra_users, list, alg->cra_flags);
+       crypto_remove_spawns(alg, list, NULL);
 
        return 0;
 }
@@ -339,7 +388,7 @@ int crypto_unregister_alg(struct crypto_alg *alg)
 {
        int ret;
        LIST_HEAD(list);
-       
+
        down_write(&crypto_alg_sem);
        ret = crypto_remove_alg(alg, &list);
        up_write(&crypto_alg_sem);
@@ -356,6 +405,41 @@ int crypto_unregister_alg(struct crypto_alg *alg)
 }
 EXPORT_SYMBOL_GPL(crypto_unregister_alg);
 
+int crypto_register_algs(struct crypto_alg *algs, int count)
+{
+       int i, ret;
+
+       for (i = 0; i < count; i++) {
+               ret = crypto_register_alg(&algs[i]);
+               if (ret)
+                       goto err;
+       }
+
+       return 0;
+
+err:
+       for (--i; i >= 0; --i)
+               crypto_unregister_alg(&algs[i]);
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(crypto_register_algs);
+
+int crypto_unregister_algs(struct crypto_alg *algs, int count)
+{
+       int i, ret;
+
+       for (i = 0; i < count; i++) {
+               ret = crypto_unregister_alg(&algs[i]);
+               if (ret)
+                       pr_err("Failed to unregister %s %s: %d\n",
+                              algs[i].cra_driver_name, algs[i].cra_name, ret);
+       }
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(crypto_unregister_algs);
+
 int crypto_register_template(struct crypto_template *tmpl)
 {
        struct crypto_template *q;
@@ -428,7 +512,8 @@ static struct crypto_template *__crypto_lookup_template(const char *name)
 
 struct crypto_template *crypto_lookup_template(const char *name)
 {
-       return try_then_request_module(__crypto_lookup_template(name), name);
+       return try_then_request_module(__crypto_lookup_template(name), "%s",
+                                      name);
 }
 EXPORT_SYMBOL_GPL(crypto_lookup_template);
 
@@ -443,6 +528,7 @@ int crypto_register_instance(struct crypto_template *tmpl,
                goto err;
 
        inst->alg.cra_module = tmpl->module;
+       inst->alg.cra_flags |= CRYPTO_ALG_INSTANCE;
 
        down_write(&crypto_alg_sem);
 
@@ -468,6 +554,35 @@ err:
 }
 EXPORT_SYMBOL_GPL(crypto_register_instance);
 
+int crypto_unregister_instance(struct crypto_alg *alg)
+{
+       int err;
+       struct crypto_instance *inst = (void *)alg;
+       struct crypto_template *tmpl = inst->tmpl;
+       LIST_HEAD(users);
+
+       if (!(alg->cra_flags & CRYPTO_ALG_INSTANCE))
+               return -EINVAL;
+
+       BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
+
+       down_write(&crypto_alg_sem);
+
+       hlist_del_init(&inst->list);
+       err = crypto_remove_alg(alg, &users);
+
+       up_write(&crypto_alg_sem);
+
+       if (err)
+               return err;
+
+       tmpl->free(inst);
+       crypto_remove_final(&users);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(crypto_unregister_instance);
+
 int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
                      struct crypto_instance *inst, u32 mask)
 {
@@ -494,7 +609,7 @@ int crypto_init_spawn2(struct crypto_spawn *spawn, struct crypto_alg *alg,
 {
        int err = -EINVAL;
 
-       if (frontend && (alg->cra_flags ^ frontend->type) & frontend->maskset)
+       if ((alg->cra_flags ^ frontend->type) & frontend->maskset)
                goto out;
 
        spawn->frontend = frontend;
@@ -771,7 +886,7 @@ out:
 }
 EXPORT_SYMBOL_GPL(crypto_enqueue_request);
 
-struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
+void *__crypto_dequeue_request(struct crypto_queue *queue, unsigned int offset)
 {
        struct list_head *request;
 
@@ -786,7 +901,14 @@ struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
        request = queue->list.next;
        list_del(request);
 
-       return list_entry(request, struct crypto_async_request, list);
+       return (char *)list_entry(request, struct crypto_async_request, list) -
+              offset;
+}
+EXPORT_SYMBOL_GPL(__crypto_dequeue_request);
+
+struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
+{
+       return __crypto_dequeue_request(queue, 0);
 }
 EXPORT_SYMBOL_GPL(crypto_dequeue_request);