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