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