Merge branch 'pci/aspm'
[linux-2.6-block.git] / drivers / md / dm-target.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (C) 2001 Sistina Software (UK) Limited
3 *
4 * This file is released under the GPL.
5 */
6
4cc96131 7#include "dm-core.h"
1da177e4
LT
8
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/kmod.h>
12#include <linux/bio.h>
1da177e4 13
72d94861
AK
14#define DM_MSG_PREFIX "target"
15
1da177e4
LT
16static LIST_HEAD(_targets);
17static DECLARE_RWSEM(_lock);
18
45194e4f 19static inline struct target_type *__find_target_type(const char *name)
1da177e4 20{
45194e4f 21 struct target_type *tt;
1da177e4 22
45194e4f
CR
23 list_for_each_entry(tt, &_targets, list)
24 if (!strcmp(name, tt->name))
25 return tt;
1da177e4
LT
26
27 return NULL;
28}
29
45194e4f 30static struct target_type *get_target_type(const char *name)
1da177e4 31{
45194e4f 32 struct target_type *tt;
1da177e4
LT
33
34 down_read(&_lock);
35
45194e4f
CR
36 tt = __find_target_type(name);
37 if (tt && !try_module_get(tt->module))
38 tt = NULL;
1da177e4
LT
39
40 up_read(&_lock);
45194e4f 41 return tt;
1da177e4
LT
42}
43
44static void load_module(const char *name)
45{
46 request_module("dm-%s", name);
47}
48
49struct target_type *dm_get_target_type(const char *name)
50{
45194e4f 51 struct target_type *tt = get_target_type(name);
1da177e4 52
45194e4f 53 if (!tt) {
1da177e4 54 load_module(name);
45194e4f 55 tt = get_target_type(name);
1da177e4
LT
56 }
57
45194e4f 58 return tt;
1da177e4
LT
59}
60
45194e4f 61void dm_put_target_type(struct target_type *tt)
1da177e4 62{
1da177e4 63 down_read(&_lock);
45194e4f 64 module_put(tt->module);
1da177e4 65 up_read(&_lock);
1da177e4
LT
66}
67
1da177e4
LT
68int dm_target_iterate(void (*iter_func)(struct target_type *tt,
69 void *param), void *param)
70{
45194e4f 71 struct target_type *tt;
1da177e4
LT
72
73 down_read(&_lock);
45194e4f
CR
74 list_for_each_entry(tt, &_targets, list)
75 iter_func(tt, param);
1da177e4
LT
76 up_read(&_lock);
77
78 return 0;
79}
80
45194e4f 81int dm_register_target(struct target_type *tt)
1da177e4
LT
82{
83 int rv = 0;
1da177e4
LT
84
85 down_write(&_lock);
45194e4f 86 if (__find_target_type(tt->name))
1da177e4
LT
87 rv = -EEXIST;
88 else
45194e4f 89 list_add(&tt->list, &_targets);
1da177e4
LT
90
91 up_write(&_lock);
1da177e4
LT
92 return rv;
93}
94
45194e4f 95void dm_unregister_target(struct target_type *tt)
1da177e4 96{
1da177e4 97 down_write(&_lock);
45194e4f
CR
98 if (!__find_target_type(tt->name)) {
99 DMCRIT("Unregistering unrecognised target: %s", tt->name);
10d3bd09 100 BUG();
1da177e4
LT
101 }
102
45194e4f 103 list_del(&tt->list);
1da177e4
LT
104
105 up_write(&_lock);
1da177e4
LT
106}
107
108/*
109 * io-err: always fails an io, useful for bringing
110 * up LVs that have holes in them.
111 */
45194e4f 112static int io_err_ctr(struct dm_target *tt, unsigned int argc, char **args)
1da177e4 113{
38e1b257
MS
114 /*
115 * Return error for discards instead of -EOPNOTSUPP
116 */
55a62eef 117 tt->num_discard_bios = 1;
38e1b257 118
1da177e4
LT
119 return 0;
120}
121
45194e4f 122static void io_err_dtr(struct dm_target *tt)
1da177e4
LT
123{
124 /* empty */
125}
126
7de3ee57 127static int io_err_map(struct dm_target *tt, struct bio *bio)
1da177e4 128{
846785e6 129 return DM_MAPIO_KILL;
1da177e4
LT
130}
131
e5863d9a
MS
132static int io_err_clone_and_map_rq(struct dm_target *ti, struct request *rq,
133 union map_info *map_context,
134 struct request **clone)
135{
412445ac 136 return DM_MAPIO_KILL;
e5863d9a
MS
137}
138
5de719e3
YY
139static void io_err_release_clone_rq(struct request *clone,
140 union map_info *map_context)
e5863d9a
MS
141{
142}
143
817bf402
DW
144static long io_err_dax_direct_access(struct dm_target *ti, pgoff_t pgoff,
145 long nr_pages, void **kaddr, pfn_t *pfn)
f8df1fdf
MS
146{
147 return -EIO;
148}
149
1da177e4
LT
150static struct target_type error_target = {
151 .name = "error",
f8df1fdf 152 .version = {1, 5, 0},
f083b09b 153 .features = DM_TARGET_WILDCARD,
1da177e4
LT
154 .ctr = io_err_ctr,
155 .dtr = io_err_dtr,
156 .map = io_err_map,
e5863d9a
MS
157 .clone_and_map_rq = io_err_clone_and_map_rq,
158 .release_clone_rq = io_err_release_clone_rq,
817bf402 159 .direct_access = io_err_dax_direct_access,
1da177e4
LT
160};
161
162int __init dm_target_init(void)
163{
164 return dm_register_target(&error_target);
165}
166
167void dm_target_exit(void)
168{
10d3bd09 169 dm_unregister_target(&error_target);
1da177e4
LT
170}
171
172EXPORT_SYMBOL(dm_register_target);
173EXPORT_SYMBOL(dm_unregister_target);