]>
Commit | Line | Data |
---|---|---|
3bd94003 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * Copyright (C) 2001 Sistina Software (UK) Limited | |
4 | * | |
5 | * This file is released under the GPL. | |
6 | */ | |
7 | ||
4cc96131 | 8 | #include "dm-core.h" |
1da177e4 LT |
9 | |
10 | #include <linux/module.h> | |
11 | #include <linux/init.h> | |
12 | #include <linux/kmod.h> | |
13 | #include <linux/bio.h> | |
e511c4a3 | 14 | #include <linux/dax.h> |
1da177e4 | 15 | |
72d94861 AK |
16 | #define DM_MSG_PREFIX "target" |
17 | ||
1da177e4 LT |
18 | static LIST_HEAD(_targets); |
19 | static DECLARE_RWSEM(_lock); | |
20 | ||
45194e4f | 21 | static inline struct target_type *__find_target_type(const char *name) |
1da177e4 | 22 | { |
45194e4f | 23 | struct target_type *tt; |
1da177e4 | 24 | |
45194e4f CR |
25 | list_for_each_entry(tt, &_targets, list) |
26 | if (!strcmp(name, tt->name)) | |
27 | return tt; | |
1da177e4 LT |
28 | |
29 | return NULL; | |
30 | } | |
31 | ||
45194e4f | 32 | static struct target_type *get_target_type(const char *name) |
1da177e4 | 33 | { |
45194e4f | 34 | struct target_type *tt; |
1da177e4 LT |
35 | |
36 | down_read(&_lock); | |
37 | ||
45194e4f CR |
38 | tt = __find_target_type(name); |
39 | if (tt && !try_module_get(tt->module)) | |
40 | tt = NULL; | |
1da177e4 LT |
41 | |
42 | up_read(&_lock); | |
45194e4f | 43 | return tt; |
1da177e4 LT |
44 | } |
45 | ||
46 | static void load_module(const char *name) | |
47 | { | |
48 | request_module("dm-%s", name); | |
49 | } | |
50 | ||
51 | struct target_type *dm_get_target_type(const char *name) | |
52 | { | |
45194e4f | 53 | struct target_type *tt = get_target_type(name); |
1da177e4 | 54 | |
45194e4f | 55 | if (!tt) { |
1da177e4 | 56 | load_module(name); |
45194e4f | 57 | tt = get_target_type(name); |
1da177e4 LT |
58 | } |
59 | ||
45194e4f | 60 | return tt; |
1da177e4 LT |
61 | } |
62 | ||
45194e4f | 63 | void dm_put_target_type(struct target_type *tt) |
1da177e4 | 64 | { |
1da177e4 | 65 | down_read(&_lock); |
45194e4f | 66 | module_put(tt->module); |
1da177e4 | 67 | up_read(&_lock); |
1da177e4 LT |
68 | } |
69 | ||
1da177e4 LT |
70 | int dm_target_iterate(void (*iter_func)(struct target_type *tt, |
71 | void *param), void *param) | |
72 | { | |
45194e4f | 73 | struct target_type *tt; |
1da177e4 LT |
74 | |
75 | down_read(&_lock); | |
45194e4f CR |
76 | list_for_each_entry(tt, &_targets, list) |
77 | iter_func(tt, param); | |
1da177e4 LT |
78 | up_read(&_lock); |
79 | ||
80 | return 0; | |
81 | } | |
82 | ||
45194e4f | 83 | int dm_register_target(struct target_type *tt) |
1da177e4 LT |
84 | { |
85 | int rv = 0; | |
1da177e4 LT |
86 | |
87 | down_write(&_lock); | |
b362c733 YL |
88 | if (__find_target_type(tt->name)) { |
89 | DMERR("%s: '%s' target already registered", | |
90 | __func__, tt->name); | |
1da177e4 | 91 | rv = -EEXIST; |
b362c733 | 92 | } else { |
45194e4f | 93 | list_add(&tt->list, &_targets); |
b362c733 | 94 | } |
1da177e4 | 95 | up_write(&_lock); |
b362c733 | 96 | |
1da177e4 LT |
97 | return rv; |
98 | } | |
aa07f9d8 | 99 | EXPORT_SYMBOL(dm_register_target); |
1da177e4 | 100 | |
45194e4f | 101 | void dm_unregister_target(struct target_type *tt) |
1da177e4 | 102 | { |
1da177e4 | 103 | down_write(&_lock); |
45194e4f CR |
104 | if (!__find_target_type(tt->name)) { |
105 | DMCRIT("Unregistering unrecognised target: %s", tt->name); | |
10d3bd09 | 106 | BUG(); |
1da177e4 LT |
107 | } |
108 | ||
45194e4f | 109 | list_del(&tt->list); |
1da177e4 LT |
110 | |
111 | up_write(&_lock); | |
1da177e4 | 112 | } |
aa07f9d8 | 113 | EXPORT_SYMBOL(dm_unregister_target); |
1da177e4 LT |
114 | |
115 | /* | |
116 | * io-err: always fails an io, useful for bringing | |
117 | * up LVs that have holes in them. | |
118 | */ | |
a9511043 DLM |
119 | struct io_err_c { |
120 | struct dm_dev *dev; | |
121 | sector_t start; | |
122 | }; | |
123 | ||
124 | static int io_err_get_args(struct dm_target *tt, unsigned int argc, char **args) | |
125 | { | |
126 | unsigned long long start; | |
127 | struct io_err_c *ioec; | |
128 | char dummy; | |
129 | int ret; | |
130 | ||
131 | ioec = kmalloc(sizeof(*ioec), GFP_KERNEL); | |
132 | if (!ioec) { | |
133 | tt->error = "Cannot allocate io_err context"; | |
134 | return -ENOMEM; | |
135 | } | |
136 | ||
137 | ret = -EINVAL; | |
138 | if (sscanf(args[1], "%llu%c", &start, &dummy) != 1 || | |
139 | start != (sector_t)start) { | |
140 | tt->error = "Invalid device sector"; | |
141 | goto bad; | |
142 | } | |
143 | ioec->start = start; | |
144 | ||
145 | ret = dm_get_device(tt, args[0], dm_table_get_mode(tt->table), &ioec->dev); | |
146 | if (ret) { | |
147 | tt->error = "Device lookup failed"; | |
148 | goto bad; | |
149 | } | |
150 | ||
151 | tt->private = ioec; | |
152 | ||
153 | return 0; | |
154 | ||
155 | bad: | |
156 | kfree(ioec); | |
157 | ||
158 | return ret; | |
159 | } | |
160 | ||
45194e4f | 161 | static int io_err_ctr(struct dm_target *tt, unsigned int argc, char **args) |
1da177e4 | 162 | { |
a9511043 DLM |
163 | /* |
164 | * If we have arguments, assume it is the path to the backing | |
165 | * block device and its mapping start sector (same as dm-linear). | |
166 | * In this case, get the device so that we can get its limits. | |
167 | */ | |
168 | if (argc == 2) { | |
169 | int ret = io_err_get_args(tt, argc, args); | |
170 | ||
171 | if (ret) | |
172 | return ret; | |
173 | } | |
174 | ||
38e1b257 MS |
175 | /* |
176 | * Return error for discards instead of -EOPNOTSUPP | |
177 | */ | |
55a62eef | 178 | tt->num_discard_bios = 1; |
b6bcb844 | 179 | tt->discards_supported = true; |
38e1b257 | 180 | |
1da177e4 LT |
181 | return 0; |
182 | } | |
183 | ||
45194e4f | 184 | static void io_err_dtr(struct dm_target *tt) |
1da177e4 | 185 | { |
a9511043 DLM |
186 | struct io_err_c *ioec = tt->private; |
187 | ||
188 | if (ioec) { | |
189 | dm_put_device(tt, ioec->dev); | |
190 | kfree(ioec); | |
191 | } | |
1da177e4 LT |
192 | } |
193 | ||
7de3ee57 | 194 | static int io_err_map(struct dm_target *tt, struct bio *bio) |
1da177e4 | 195 | { |
846785e6 | 196 | return DM_MAPIO_KILL; |
1da177e4 LT |
197 | } |
198 | ||
e5863d9a MS |
199 | static int io_err_clone_and_map_rq(struct dm_target *ti, struct request *rq, |
200 | union map_info *map_context, | |
201 | struct request **clone) | |
202 | { | |
412445ac | 203 | return DM_MAPIO_KILL; |
e5863d9a MS |
204 | } |
205 | ||
5de719e3 YY |
206 | static void io_err_release_clone_rq(struct request *clone, |
207 | union map_info *map_context) | |
e5863d9a MS |
208 | { |
209 | } | |
210 | ||
a9511043 DLM |
211 | #ifdef CONFIG_BLK_DEV_ZONED |
212 | static sector_t io_err_map_sector(struct dm_target *ti, sector_t bi_sector) | |
213 | { | |
214 | struct io_err_c *ioec = ti->private; | |
215 | ||
216 | return ioec->start + dm_target_offset(ti, bi_sector); | |
217 | } | |
218 | ||
219 | static int io_err_report_zones(struct dm_target *ti, | |
220 | struct dm_report_zones_args *args, unsigned int nr_zones) | |
221 | { | |
222 | struct io_err_c *ioec = ti->private; | |
223 | ||
224 | /* | |
225 | * This should never be called when we do not have a backing device | |
226 | * as that mean the target is not a zoned one. | |
227 | */ | |
228 | if (WARN_ON_ONCE(!ioec)) | |
229 | return -EIO; | |
230 | ||
231 | return dm_report_zones(ioec->dev->bdev, ioec->start, | |
232 | io_err_map_sector(ti, args->next_sector), | |
233 | args, nr_zones); | |
234 | } | |
235 | #else | |
236 | #define io_err_report_zones NULL | |
237 | #endif | |
238 | ||
239 | static int io_err_iterate_devices(struct dm_target *ti, | |
240 | iterate_devices_callout_fn fn, void *data) | |
241 | { | |
242 | struct io_err_c *ioec = ti->private; | |
243 | ||
244 | if (!ioec) | |
245 | return 0; | |
246 | ||
247 | return fn(ti, ioec->dev, ioec->start, ti->len, data); | |
248 | } | |
249 | ||
b6bcb844 MP |
250 | static void io_err_io_hints(struct dm_target *ti, struct queue_limits *limits) |
251 | { | |
252 | limits->max_discard_sectors = UINT_MAX; | |
253 | limits->max_hw_discard_sectors = UINT_MAX; | |
254 | limits->discard_granularity = 512; | |
255 | } | |
256 | ||
817bf402 | 257 | static long io_err_dax_direct_access(struct dm_target *ti, pgoff_t pgoff, |
e511c4a3 JC |
258 | long nr_pages, enum dax_access_mode mode, void **kaddr, |
259 | pfn_t *pfn) | |
f8df1fdf MS |
260 | { |
261 | return -EIO; | |
262 | } | |
263 | ||
1da177e4 LT |
264 | static struct target_type error_target = { |
265 | .name = "error", | |
a9511043 DLM |
266 | .version = {1, 7, 0}, |
267 | .features = DM_TARGET_WILDCARD | DM_TARGET_ZONED_HM, | |
1da177e4 LT |
268 | .ctr = io_err_ctr, |
269 | .dtr = io_err_dtr, | |
270 | .map = io_err_map, | |
e5863d9a MS |
271 | .clone_and_map_rq = io_err_clone_and_map_rq, |
272 | .release_clone_rq = io_err_release_clone_rq, | |
a9511043 | 273 | .iterate_devices = io_err_iterate_devices, |
b6bcb844 | 274 | .io_hints = io_err_io_hints, |
817bf402 | 275 | .direct_access = io_err_dax_direct_access, |
a9511043 | 276 | .report_zones = io_err_report_zones, |
1da177e4 LT |
277 | }; |
278 | ||
279 | int __init dm_target_init(void) | |
280 | { | |
281 | return dm_register_target(&error_target); | |
282 | } | |
283 | ||
284 | void dm_target_exit(void) | |
285 | { | |
10d3bd09 | 286 | dm_unregister_target(&error_target); |
1da177e4 | 287 | } |