blob: 96ea226155b10e3dbb560b3faa8c2211ec11f41f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Mikulas Patocka586e80e2008-10-21 17:44:59 +010012#include <linux/device-mapper.h>
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "dm-path-selector.h"
15
16#include <linux/slab.h>
17
18struct ps_internal {
19 struct path_selector_type pst;
20
21 struct list_head list;
22 long use;
23};
24
25#define pst_to_psi(__pst) container_of((__pst), struct ps_internal, pst)
26
27static LIST_HEAD(_path_selectors);
28static DECLARE_RWSEM(_ps_lock);
29
Alasdair G Kergon5e198d92005-05-05 16:16:09 -070030static struct ps_internal *__find_path_selector_type(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
32 struct ps_internal *psi;
33
34 list_for_each_entry(psi, &_path_selectors, list) {
35 if (!strcmp(name, psi->pst.name))
36 return psi;
37 }
38
39 return NULL;
40}
41
42static struct ps_internal *get_path_selector(const char *name)
43{
44 struct ps_internal *psi;
45
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++;
53 }
54 up_read(&_ps_lock);
55
56 return psi;
57}
58
59struct path_selector_type *dm_get_path_selector(const char *name)
60{
61 struct ps_internal *psi;
62
63 if (!name)
64 return NULL;
65
66 psi = get_path_selector(name);
67 if (!psi) {
68 request_module("dm-%s", name);
69 psi = get_path_selector(name);
70 }
71
72 return psi ? &psi->pst : NULL;
73}
74
75void dm_put_path_selector(struct path_selector_type *pst)
76{
77 struct ps_internal *psi;
78
79 if (!pst)
80 return;
81
82 down_read(&_ps_lock);
83 psi = __find_path_selector_type(pst->name);
84 if (!psi)
85 goto out;
86
87 if (--psi->use == 0)
88 module_put(psi->pst.module);
89
Eric Sesterhennc163c722006-03-26 18:21:58 +020090 BUG_ON(psi->use < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92out:
93 up_read(&_ps_lock);
94}
95
96static struct ps_internal *_alloc_path_selector(struct path_selector_type *pst)
97{
Dmitry Monakhov094262d2007-10-19 22:38:51 +010098 struct ps_internal *psi = kzalloc(sizeof(*psi), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Dmitry Monakhov094262d2007-10-19 22:38:51 +0100100 if (psi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 psi->pst = *pst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 return psi;
104}
105
106int dm_register_path_selector(struct path_selector_type *pst)
107{
108 int r = 0;
109 struct ps_internal *psi = _alloc_path_selector(pst);
110
111 if (!psi)
112 return -ENOMEM;
113
114 down_write(&_ps_lock);
115
116 if (__find_path_selector_type(pst->name)) {
117 kfree(psi);
118 r = -EEXIST;
119 } else
120 list_add(&psi->list, &_path_selectors);
121
122 up_write(&_ps_lock);
123
124 return r;
125}
126
127int dm_unregister_path_selector(struct path_selector_type *pst)
128{
129 struct ps_internal *psi;
130
131 down_write(&_ps_lock);
132
133 psi = __find_path_selector_type(pst->name);
134 if (!psi) {
135 up_write(&_ps_lock);
136 return -EINVAL;
137 }
138
139 if (psi->use) {
140 up_write(&_ps_lock);
141 return -ETXTBSY;
142 }
143
144 list_del(&psi->list);
145
146 up_write(&_ps_lock);
147
148 kfree(psi);
149
150 return 0;
151}
152
153EXPORT_SYMBOL_GPL(dm_register_path_selector);
154EXPORT_SYMBOL_GPL(dm_unregister_path_selector);