Merge git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux-2.6-for-linus
[wrt350n-kernel.git] / drivers / md / dm-path-selector.c
blobca1bb636a3e4ba808ae63d88dfbb62bc4ee28a04
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 "dm.h"
13 #include "dm-path-selector.h"
15 #include <linux/slab.h>
17 struct ps_internal {
18 struct path_selector_type pst;
20 struct list_head list;
21 long use;
24 #define pst_to_psi(__pst) container_of((__pst), struct ps_internal, pst)
26 static LIST_HEAD(_path_selectors);
27 static DECLARE_RWSEM(_ps_lock);
29 static struct ps_internal *__find_path_selector_type(const char *name)
31 struct ps_internal *psi;
33 list_for_each_entry(psi, &_path_selectors, list) {
34 if (!strcmp(name, psi->pst.name))
35 return psi;
38 return NULL;
41 static struct ps_internal *get_path_selector(const char *name)
43 struct ps_internal *psi;
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++;
53 up_read(&_ps_lock);
55 return psi;
58 struct path_selector_type *dm_get_path_selector(const char *name)
60 struct ps_internal *psi;
62 if (!name)
63 return NULL;
65 psi = get_path_selector(name);
66 if (!psi) {
67 request_module("dm-%s", name);
68 psi = get_path_selector(name);
71 return psi ? &psi->pst : NULL;
74 void dm_put_path_selector(struct path_selector_type *pst)
76 struct ps_internal *psi;
78 if (!pst)
79 return;
81 down_read(&_ps_lock);
82 psi = __find_path_selector_type(pst->name);
83 if (!psi)
84 goto out;
86 if (--psi->use == 0)
87 module_put(psi->pst.module);
89 BUG_ON(psi->use < 0);
91 out:
92 up_read(&_ps_lock);
95 static struct ps_internal *_alloc_path_selector(struct path_selector_type *pst)
97 struct ps_internal *psi = kzalloc(sizeof(*psi), GFP_KERNEL);
99 if (psi)
100 psi->pst = *pst;
102 return psi;
105 int dm_register_path_selector(struct path_selector_type *pst)
107 int r = 0;
108 struct ps_internal *psi = _alloc_path_selector(pst);
110 if (!psi)
111 return -ENOMEM;
113 down_write(&_ps_lock);
115 if (__find_path_selector_type(pst->name)) {
116 kfree(psi);
117 r = -EEXIST;
118 } else
119 list_add(&psi->list, &_path_selectors);
121 up_write(&_ps_lock);
123 return r;
126 int dm_unregister_path_selector(struct path_selector_type *pst)
128 struct ps_internal *psi;
130 down_write(&_ps_lock);
132 psi = __find_path_selector_type(pst->name);
133 if (!psi) {
134 up_write(&_ps_lock);
135 return -EINVAL;
138 if (psi->use) {
139 up_write(&_ps_lock);
140 return -ETXTBSY;
143 list_del(&psi->list);
145 up_write(&_ps_lock);
147 kfree(psi);
149 return 0;
152 EXPORT_SYMBOL_GPL(dm_register_path_selector);
153 EXPORT_SYMBOL_GPL(dm_unregister_path_selector);