Hopefully get the Kconfig PCI stuff right, finally.
[linux-2.6/linux-mips.git] / drivers / md / dm-target.c
blob20fd9f6074b79e7edc5676134de9fd0da6d2405b
1 /*
2 * Copyright (C) 2001 Sistina Software (UK) Limited
4 * This file is released under the GPL.
5 */
7 #include "dm.h"
9 #include <linux/module.h>
10 #include <linux/kmod.h>
11 #include <linux/bio.h>
12 #include <linux/slab.h>
14 struct tt_internal {
15 struct target_type tt;
17 struct list_head list;
18 long use;
21 static LIST_HEAD(_targets);
22 static DECLARE_RWSEM(_lock);
24 #define DM_MOD_NAME_SIZE 32
26 static inline struct tt_internal *__find_target_type(const char *name)
28 struct list_head *tih;
29 struct tt_internal *ti;
31 list_for_each(tih, &_targets) {
32 ti = list_entry(tih, struct tt_internal, list);
34 if (!strcmp(name, ti->tt.name))
35 return ti;
38 return NULL;
41 static struct tt_internal *get_target_type(const char *name)
43 struct tt_internal *ti;
45 down_read(&_lock);
47 ti = __find_target_type(name);
48 if (ti) {
49 if ((ti->use == 0) && !try_module_get(ti->tt.module))
50 ti = NULL;
51 else
52 ti->use++;
55 up_read(&_lock);
56 return ti;
59 static void load_module(const char *name)
61 request_module("dm-%s", name);
64 struct target_type *dm_get_target_type(const char *name)
66 struct tt_internal *ti = get_target_type(name);
68 if (!ti) {
69 load_module(name);
70 ti = get_target_type(name);
73 return ti ? &ti->tt : NULL;
76 void dm_put_target_type(struct target_type *t)
78 struct tt_internal *ti = (struct tt_internal *) t;
80 down_read(&_lock);
81 if (--ti->use == 0)
82 module_put(ti->tt.module);
84 if (ti->use < 0)
85 BUG();
86 up_read(&_lock);
88 return;
91 static struct tt_internal *alloc_target(struct target_type *t)
93 struct tt_internal *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
95 if (ti) {
96 memset(ti, 0, sizeof(*ti));
97 ti->tt = *t;
100 return ti;
103 int dm_register_target(struct target_type *t)
105 int rv = 0;
106 struct tt_internal *ti = alloc_target(t);
108 if (!ti)
109 return -ENOMEM;
111 down_write(&_lock);
112 if (__find_target_type(t->name))
113 rv = -EEXIST;
114 else
115 list_add(&ti->list, &_targets);
117 up_write(&_lock);
118 if (rv)
119 kfree(ti);
120 return rv;
123 int dm_unregister_target(struct target_type *t)
125 struct tt_internal *ti;
127 down_write(&_lock);
128 if (!(ti = __find_target_type(t->name))) {
129 up_write(&_lock);
130 return -EINVAL;
133 if (ti->use) {
134 up_write(&_lock);
135 return -ETXTBSY;
138 list_del(&ti->list);
139 kfree(ti);
141 up_write(&_lock);
142 return 0;
146 * io-err: always fails an io, useful for bringing
147 * up LVs that have holes in them.
149 static int io_err_ctr(struct dm_target *ti, int argc, char **args)
151 return 0;
154 static void io_err_dtr(struct dm_target *ti)
156 /* empty */
159 static int io_err_map(struct dm_target *ti, struct bio *bio)
161 return -EIO;
164 static struct target_type error_target = {
165 .name = "error",
166 .ctr = io_err_ctr,
167 .dtr = io_err_dtr,
168 .map = io_err_map,
171 int dm_target_init(void)
173 return dm_register_target(&error_target);
176 void dm_target_exit(void)
178 if (dm_unregister_target(&error_target))
179 DMWARN("error target unregistration failed");
182 EXPORT_SYMBOL(dm_register_target);
183 EXPORT_SYMBOL(dm_unregister_target);