net: Fix missing dev->neigh_setup in register_netdevice().
[linux-2.6/mini2440.git] / drivers / md / dm-path-selector.c
blob96ea226155b10e3dbb560b3faa8c2211ec11f41f
1 /*
2 * Copyright (C) 2003 Sistina Software.
3 * Copyright (C) 2004 Red Hat, Inc. All rights reserved.
5 * Module Author: Heinz Mauelshagen
7 * This file is released under the GPL.
9 * Path selector registration.
12 #include <linux/device-mapper.h>
14 #include "dm-path-selector.h"
16 #include <linux/slab.h>
18 struct ps_internal {
19 struct path_selector_type pst;
21 struct list_head list;
22 long use;
25 #define pst_to_psi(__pst) container_of((__pst), struct ps_internal, pst)
27 static LIST_HEAD(_path_selectors);
28 static DECLARE_RWSEM(_ps_lock);
30 static struct ps_internal *__find_path_selector_type(const char *name)
32 struct ps_internal *psi;
34 list_for_each_entry(psi, &_path_selectors, list) {
35 if (!strcmp(name, psi->pst.name))
36 return psi;
39 return NULL;
42 static struct ps_internal *get_path_selector(const char *name)
44 struct ps_internal *psi;
46 down_read(&_ps_lock);
47 psi = __find_path_selector_type(name);
48 if (psi) {
49 if ((psi->use == 0) && !try_module_get(psi->pst.module))
50 psi = NULL;
51 else
52 psi->use++;
54 up_read(&_ps_lock);
56 return psi;
59 struct path_selector_type *dm_get_path_selector(const char *name)
61 struct ps_internal *psi;
63 if (!name)
64 return NULL;
66 psi = get_path_selector(name);
67 if (!psi) {
68 request_module("dm-%s", name);
69 psi = get_path_selector(name);
72 return psi ? &psi->pst : NULL;
75 void dm_put_path_selector(struct path_selector_type *pst)
77 struct ps_internal *psi;
79 if (!pst)
80 return;
82 down_read(&_ps_lock);
83 psi = __find_path_selector_type(pst->name);
84 if (!psi)
85 goto out;
87 if (--psi->use == 0)
88 module_put(psi->pst.module);
90 BUG_ON(psi->use < 0);
92 out:
93 up_read(&_ps_lock);
96 static struct ps_internal *_alloc_path_selector(struct path_selector_type *pst)
98 struct ps_internal *psi = kzalloc(sizeof(*psi), GFP_KERNEL);
100 if (psi)
101 psi->pst = *pst;
103 return psi;
106 int dm_register_path_selector(struct path_selector_type *pst)
108 int r = 0;
109 struct ps_internal *psi = _alloc_path_selector(pst);
111 if (!psi)
112 return -ENOMEM;
114 down_write(&_ps_lock);
116 if (__find_path_selector_type(pst->name)) {
117 kfree(psi);
118 r = -EEXIST;
119 } else
120 list_add(&psi->list, &_path_selectors);
122 up_write(&_ps_lock);
124 return r;
127 int dm_unregister_path_selector(struct path_selector_type *pst)
129 struct ps_internal *psi;
131 down_write(&_ps_lock);
133 psi = __find_path_selector_type(pst->name);
134 if (!psi) {
135 up_write(&_ps_lock);
136 return -EINVAL;
139 if (psi->use) {
140 up_write(&_ps_lock);
141 return -ETXTBSY;
144 list_del(&psi->list);
146 up_write(&_ps_lock);
148 kfree(psi);
150 return 0;
153 EXPORT_SYMBOL_GPL(dm_register_path_selector);
154 EXPORT_SYMBOL_GPL(dm_unregister_path_selector);