ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / md / dm-target.c
1 /*
2  * Copyright (C) 2001 Sistina Software (UK) Limited
3  *
4  * This file is released under the GPL.
5  */
6
7 #include "dm.h"
8
9 #include <linux/module.h>
10 #include <linux/kmod.h>
11 #include <linux/bio.h>
12 #include <linux/slab.h>
13
14 struct tt_internal {
15         struct target_type tt;
16
17         struct list_head list;
18         long use;
19 };
20
21 static LIST_HEAD(_targets);
22 static DECLARE_RWSEM(_lock);
23
24 #define DM_MOD_NAME_SIZE 32
25
26 static inline struct tt_internal *__find_target_type(const char *name)
27 {
28         struct tt_internal *ti;
29
30         list_for_each_entry (ti, &_targets, list)
31                 if (!strcmp(name, ti->tt.name))
32                         return ti;
33
34         return NULL;
35 }
36
37 static struct tt_internal *get_target_type(const char *name)
38 {
39         struct tt_internal *ti;
40
41         down_read(&_lock);
42
43         ti = __find_target_type(name);
44         if (ti) {
45                 if ((ti->use == 0) && !try_module_get(ti->tt.module))
46                         ti = NULL;
47                 else
48                         ti->use++;
49         }
50
51         up_read(&_lock);
52         return ti;
53 }
54
55 static void load_module(const char *name)
56 {
57         request_module("dm-%s", name);
58 }
59
60 struct target_type *dm_get_target_type(const char *name)
61 {
62         struct tt_internal *ti = get_target_type(name);
63
64         if (!ti) {
65                 load_module(name);
66                 ti = get_target_type(name);
67         }
68
69         return ti ? &ti->tt : NULL;
70 }
71
72 void dm_put_target_type(struct target_type *t)
73 {
74         struct tt_internal *ti = (struct tt_internal *) t;
75
76         down_read(&_lock);
77         if (--ti->use == 0)
78                 module_put(ti->tt.module);
79
80         if (ti->use < 0)
81                 BUG();
82         up_read(&_lock);
83
84         return;
85 }
86
87 static struct tt_internal *alloc_target(struct target_type *t)
88 {
89         struct tt_internal *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
90
91         if (ti) {
92                 memset(ti, 0, sizeof(*ti));
93                 ti->tt = *t;
94         }
95
96         return ti;
97 }
98
99
100 int dm_target_iterate(void (*iter_func)(struct target_type *tt,
101                                         void *param), void *param)
102 {
103         struct tt_internal *ti;
104
105         down_read(&_lock);
106         list_for_each_entry (ti, &_targets, list)
107                 iter_func(&ti->tt, param);
108         up_read(&_lock);
109
110         return 0;
111 }
112
113 int dm_register_target(struct target_type *t)
114 {
115         int rv = 0;
116         struct tt_internal *ti = alloc_target(t);
117
118         if (!ti)
119                 return -ENOMEM;
120
121         down_write(&_lock);
122         if (__find_target_type(t->name)) {
123                 kfree(ti);
124                 rv = -EEXIST;
125         } else
126                 list_add(&ti->list, &_targets);
127
128         up_write(&_lock);
129         if (rv)
130                 kfree(ti);
131         return rv;
132 }
133
134 int dm_unregister_target(struct target_type *t)
135 {
136         struct tt_internal *ti;
137
138         down_write(&_lock);
139         if (!(ti = __find_target_type(t->name))) {
140                 up_write(&_lock);
141                 return -EINVAL;
142         }
143
144         if (ti->use) {
145                 up_write(&_lock);
146                 return -ETXTBSY;
147         }
148
149         list_del(&ti->list);
150         kfree(ti);
151
152         up_write(&_lock);
153         return 0;
154 }
155
156 /*
157  * io-err: always fails an io, useful for bringing
158  * up LVs that have holes in them.
159  */
160 static int io_err_ctr(struct dm_target *ti, unsigned int argc, char **args)
161 {
162         return 0;
163 }
164
165 static void io_err_dtr(struct dm_target *ti)
166 {
167         /* empty */
168 }
169
170 static int io_err_map(struct dm_target *ti, struct bio *bio,
171                       union map_info *map_context)
172 {
173         return -EIO;
174 }
175
176 static struct target_type error_target = {
177         .name = "error",
178         .version = {1, 0, 1},
179         .ctr  = io_err_ctr,
180         .dtr  = io_err_dtr,
181         .map  = io_err_map,
182 };
183
184 int dm_target_init(void)
185 {
186         return dm_register_target(&error_target);
187 }
188
189 void dm_target_exit(void)
190 {
191         if (dm_unregister_target(&error_target))
192                 DMWARN("error target unregistration failed");
193 }
194
195 EXPORT_SYMBOL(dm_register_target);
196 EXPORT_SYMBOL(dm_unregister_target);