aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/md/dm-path-selector.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/md/dm-path-selector.c
Linux-2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'drivers/md/dm-path-selector.c')
-rw-r--r--drivers/md/dm-path-selector.c156
1 files changed, 156 insertions, 0 deletions
diff --git a/drivers/md/dm-path-selector.c b/drivers/md/dm-path-selector.c
new file mode 100644
index 00000000000..ac5c4bbec6c
--- /dev/null
+++ b/drivers/md/dm-path-selector.c
@@ -0,0 +1,156 @@
1/*
2 * Copyright (C) 2003 Sistina Software.
3 * Copyright (C) 2004 Red Hat, Inc. All rights reserved.
4 *
5 * Module Author: Heinz Mauelshagen
6 *
7 * This file is released under the GPL.
8 *
9 * Path selector registration.
10 */
11
12#include "dm.h"
13#include "dm-path-selector.h"
14
15#include <linux/slab.h>
16
17struct ps_internal {
18 struct path_selector_type pst;
19
20 struct list_head list;
21 long use;
22};
23
24#define pst_to_psi(__pst) container_of((__pst), struct ps_internal, pst)
25
26static LIST_HEAD(_path_selectors);
27static DECLARE_RWSEM(_ps_lock);
28
29struct ps_internal *__find_path_selector_type(const char *name)
30{
31 struct ps_internal *psi;
32
33 list_for_each_entry(psi, &_path_selectors, list) {
34 if (!strcmp(name, psi->pst.name))
35 return psi;
36 }
37
38 return NULL;
39}
40
41static struct ps_internal *get_path_selector(const char *name)
42{
43 struct ps_internal *psi;
44
45 down_read(&_ps_lock);
46 psi = __find_path_selector_type(name);
47 if (psi) {
48 if ((psi->use == 0) && !try_module_get(psi->pst.module))
49 psi = NULL;
50 else
51 psi->use++;
52 }
53 up_read(&_ps_lock);
54
55 return psi;
56}
57
58struct path_selector_type *dm_get_path_selector(const char *name)
59{
60 struct ps_internal *psi;
61
62 if (!name)
63 return NULL;
64
65 psi = get_path_selector(name);
66 if (!psi) {
67 request_module("dm-%s", name);
68 psi = get_path_selector(name);
69 }
70
71 return psi ? &psi->pst : NULL;
72}
73
74void dm_put_path_selector(struct path_selector_type *pst)
75{
76 struct ps_internal *psi;
77
78 if (!pst)
79 return;
80
81 down_read(&_ps_lock);
82 psi = __find_path_selector_type(pst->name);
83 if (!psi)
84 goto out;
85
86 if (--psi->use == 0)
87 module_put(psi->pst.module);
88
89 if (psi->use < 0)
90 BUG();
91
92out:
93 up_read(&_ps_lock);
94}
95
96static struct ps_internal *_alloc_path_selector(struct path_selector_type *pst)
97{
98 struct ps_internal *psi = kmalloc(sizeof(*psi), GFP_KERNEL);
99
100 if (psi) {
101 memset(psi, 0, sizeof(*psi));
102 psi->pst = *pst;
103 }
104
105 return psi;
106}
107
108int dm_register_path_selector(struct path_selector_type *pst)
109{
110 int r = 0;
111 struct ps_internal *psi = _alloc_path_selector(pst);
112
113 if (!psi)
114 return -ENOMEM;
115
116 down_write(&_ps_lock);
117
118 if (__find_path_selector_type(pst->name)) {
119 kfree(psi);
120 r = -EEXIST;
121 } else
122 list_add(&psi->list, &_path_selectors);
123
124 up_write(&_ps_lock);
125
126 return r;
127}
128
129int dm_unregister_path_selector(struct path_selector_type *pst)
130{
131 struct ps_internal *psi;
132
133 down_write(&_ps_lock);
134
135 psi = __find_path_selector_type(pst->name);
136 if (!psi) {
137 up_write(&_ps_lock);
138 return -EINVAL;
139 }
140
141 if (psi->use) {
142 up_write(&_ps_lock);
143 return -ETXTBSY;
144 }
145
146 list_del(&psi->list);
147
148 up_write(&_ps_lock);
149
150 kfree(psi);
151
152 return 0;
153}
154
155EXPORT_SYMBOL_GPL(dm_register_path_selector);
156EXPORT_SYMBOL_GPL(dm_unregister_path_selector);