xref: /openbmc/linux/drivers/md/dm-target.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
13bd94003SHeinz Mauelshagen // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Copyright (C) 2001 Sistina Software (UK) Limited
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * This file is released under the GPL.
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
84cc96131SMike Snitzer #include "dm-core.h"
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <linux/module.h>
111da177e4SLinus Torvalds #include <linux/init.h>
121da177e4SLinus Torvalds #include <linux/kmod.h>
131da177e4SLinus Torvalds #include <linux/bio.h>
14e511c4a3SJane Chu #include <linux/dax.h>
151da177e4SLinus Torvalds 
1672d94861SAlasdair G Kergon #define DM_MSG_PREFIX "target"
1772d94861SAlasdair G Kergon 
181da177e4SLinus Torvalds static LIST_HEAD(_targets);
191da177e4SLinus Torvalds static DECLARE_RWSEM(_lock);
201da177e4SLinus Torvalds 
__find_target_type(const char * name)2145194e4fSCheng Renquan static inline struct target_type *__find_target_type(const char *name)
221da177e4SLinus Torvalds {
2345194e4fSCheng Renquan 	struct target_type *tt;
241da177e4SLinus Torvalds 
2545194e4fSCheng Renquan 	list_for_each_entry(tt, &_targets, list)
2645194e4fSCheng Renquan 		if (!strcmp(name, tt->name))
2745194e4fSCheng Renquan 			return tt;
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds 	return NULL;
301da177e4SLinus Torvalds }
311da177e4SLinus Torvalds 
get_target_type(const char * name)3245194e4fSCheng Renquan static struct target_type *get_target_type(const char *name)
331da177e4SLinus Torvalds {
3445194e4fSCheng Renquan 	struct target_type *tt;
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds 	down_read(&_lock);
371da177e4SLinus Torvalds 
3845194e4fSCheng Renquan 	tt = __find_target_type(name);
3945194e4fSCheng Renquan 	if (tt && !try_module_get(tt->module))
4045194e4fSCheng Renquan 		tt = NULL;
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds 	up_read(&_lock);
4345194e4fSCheng Renquan 	return tt;
441da177e4SLinus Torvalds }
451da177e4SLinus Torvalds 
load_module(const char * name)461da177e4SLinus Torvalds static void load_module(const char *name)
471da177e4SLinus Torvalds {
481da177e4SLinus Torvalds 	request_module("dm-%s", name);
491da177e4SLinus Torvalds }
501da177e4SLinus Torvalds 
dm_get_target_type(const char * name)511da177e4SLinus Torvalds struct target_type *dm_get_target_type(const char *name)
521da177e4SLinus Torvalds {
5345194e4fSCheng Renquan 	struct target_type *tt = get_target_type(name);
541da177e4SLinus Torvalds 
5545194e4fSCheng Renquan 	if (!tt) {
561da177e4SLinus Torvalds 		load_module(name);
5745194e4fSCheng Renquan 		tt = get_target_type(name);
581da177e4SLinus Torvalds 	}
591da177e4SLinus Torvalds 
6045194e4fSCheng Renquan 	return tt;
611da177e4SLinus Torvalds }
621da177e4SLinus Torvalds 
dm_put_target_type(struct target_type * tt)6345194e4fSCheng Renquan void dm_put_target_type(struct target_type *tt)
641da177e4SLinus Torvalds {
651da177e4SLinus Torvalds 	down_read(&_lock);
6645194e4fSCheng Renquan 	module_put(tt->module);
671da177e4SLinus Torvalds 	up_read(&_lock);
681da177e4SLinus Torvalds }
691da177e4SLinus Torvalds 
dm_target_iterate(void (* iter_func)(struct target_type * tt,void * param),void * param)701da177e4SLinus Torvalds int dm_target_iterate(void (*iter_func)(struct target_type *tt,
711da177e4SLinus Torvalds 					void *param), void *param)
721da177e4SLinus Torvalds {
7345194e4fSCheng Renquan 	struct target_type *tt;
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 	down_read(&_lock);
7645194e4fSCheng Renquan 	list_for_each_entry(tt, &_targets, list)
7745194e4fSCheng Renquan 		iter_func(tt, param);
781da177e4SLinus Torvalds 	up_read(&_lock);
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	return 0;
811da177e4SLinus Torvalds }
821da177e4SLinus Torvalds 
dm_register_target(struct target_type * tt)8345194e4fSCheng Renquan int dm_register_target(struct target_type *tt)
841da177e4SLinus Torvalds {
851da177e4SLinus Torvalds 	int rv = 0;
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	down_write(&_lock);
88*b362c733SYangtao Li 	if (__find_target_type(tt->name)) {
89*b362c733SYangtao Li 		DMERR("%s: '%s' target already registered",
90*b362c733SYangtao Li 		      __func__, tt->name);
911da177e4SLinus Torvalds 		rv = -EEXIST;
92*b362c733SYangtao Li 	} else {
9345194e4fSCheng Renquan 		list_add(&tt->list, &_targets);
94*b362c733SYangtao Li 	}
951da177e4SLinus Torvalds 	up_write(&_lock);
96*b362c733SYangtao Li 
971da177e4SLinus Torvalds 	return rv;
981da177e4SLinus Torvalds }
99aa07f9d8SHeinz Mauelshagen EXPORT_SYMBOL(dm_register_target);
1001da177e4SLinus Torvalds 
dm_unregister_target(struct target_type * tt)10145194e4fSCheng Renquan void dm_unregister_target(struct target_type *tt)
1021da177e4SLinus Torvalds {
1031da177e4SLinus Torvalds 	down_write(&_lock);
10445194e4fSCheng Renquan 	if (!__find_target_type(tt->name)) {
10545194e4fSCheng Renquan 		DMCRIT("Unregistering unrecognised target: %s", tt->name);
10610d3bd09SMikulas Patocka 		BUG();
1071da177e4SLinus Torvalds 	}
1081da177e4SLinus Torvalds 
10945194e4fSCheng Renquan 	list_del(&tt->list);
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	up_write(&_lock);
1121da177e4SLinus Torvalds }
113aa07f9d8SHeinz Mauelshagen EXPORT_SYMBOL(dm_unregister_target);
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds /*
1161da177e4SLinus Torvalds  * io-err: always fails an io, useful for bringing
1171da177e4SLinus Torvalds  * up LVs that have holes in them.
1181da177e4SLinus Torvalds  */
io_err_ctr(struct dm_target * tt,unsigned int argc,char ** args)11945194e4fSCheng Renquan static int io_err_ctr(struct dm_target *tt, unsigned int argc, char **args)
1201da177e4SLinus Torvalds {
12138e1b257SMike Snitzer 	/*
12238e1b257SMike Snitzer 	 * Return error for discards instead of -EOPNOTSUPP
12338e1b257SMike Snitzer 	 */
12455a62eefSAlasdair G Kergon 	tt->num_discard_bios = 1;
125b6bcb844SMikulas Patocka 	tt->discards_supported = true;
12638e1b257SMike Snitzer 
1271da177e4SLinus Torvalds 	return 0;
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds 
io_err_dtr(struct dm_target * tt)13045194e4fSCheng Renquan static void io_err_dtr(struct dm_target *tt)
1311da177e4SLinus Torvalds {
1321da177e4SLinus Torvalds 	/* empty */
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds 
io_err_map(struct dm_target * tt,struct bio * bio)1357de3ee57SMikulas Patocka static int io_err_map(struct dm_target *tt, struct bio *bio)
1361da177e4SLinus Torvalds {
137846785e6SChristoph Hellwig 	return DM_MAPIO_KILL;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
io_err_clone_and_map_rq(struct dm_target * ti,struct request * rq,union map_info * map_context,struct request ** clone)140e5863d9aSMike Snitzer static int io_err_clone_and_map_rq(struct dm_target *ti, struct request *rq,
141e5863d9aSMike Snitzer 				   union map_info *map_context,
142e5863d9aSMike Snitzer 				   struct request **clone)
143e5863d9aSMike Snitzer {
144412445acSChristoph Hellwig 	return DM_MAPIO_KILL;
145e5863d9aSMike Snitzer }
146e5863d9aSMike Snitzer 
io_err_release_clone_rq(struct request * clone,union map_info * map_context)1475de719e3SYufen Yu static void io_err_release_clone_rq(struct request *clone,
1485de719e3SYufen Yu 				    union map_info *map_context)
149e5863d9aSMike Snitzer {
150e5863d9aSMike Snitzer }
151e5863d9aSMike Snitzer 
io_err_io_hints(struct dm_target * ti,struct queue_limits * limits)152b6bcb844SMikulas Patocka static void io_err_io_hints(struct dm_target *ti, struct queue_limits *limits)
153b6bcb844SMikulas Patocka {
154b6bcb844SMikulas Patocka 	limits->max_discard_sectors = UINT_MAX;
155b6bcb844SMikulas Patocka 	limits->max_hw_discard_sectors = UINT_MAX;
156b6bcb844SMikulas Patocka 	limits->discard_granularity = 512;
157b6bcb844SMikulas Patocka }
158b6bcb844SMikulas Patocka 
io_err_dax_direct_access(struct dm_target * ti,pgoff_t pgoff,long nr_pages,enum dax_access_mode mode,void ** kaddr,pfn_t * pfn)159817bf402SDan Williams static long io_err_dax_direct_access(struct dm_target *ti, pgoff_t pgoff,
160e511c4a3SJane Chu 		long nr_pages, enum dax_access_mode mode, void **kaddr,
161e511c4a3SJane Chu 		pfn_t *pfn)
162f8df1fdfSMike Snitzer {
163f8df1fdfSMike Snitzer 	return -EIO;
164f8df1fdfSMike Snitzer }
165f8df1fdfSMike Snitzer 
1661da177e4SLinus Torvalds static struct target_type error_target = {
1671da177e4SLinus Torvalds 	.name = "error",
168b6bcb844SMikulas Patocka 	.version = {1, 6, 0},
169f083b09bSMike Snitzer 	.features = DM_TARGET_WILDCARD,
1701da177e4SLinus Torvalds 	.ctr  = io_err_ctr,
1711da177e4SLinus Torvalds 	.dtr  = io_err_dtr,
1721da177e4SLinus Torvalds 	.map  = io_err_map,
173e5863d9aSMike Snitzer 	.clone_and_map_rq = io_err_clone_and_map_rq,
174e5863d9aSMike Snitzer 	.release_clone_rq = io_err_release_clone_rq,
175b6bcb844SMikulas Patocka 	.io_hints = io_err_io_hints,
176817bf402SDan Williams 	.direct_access = io_err_dax_direct_access,
1771da177e4SLinus Torvalds };
1781da177e4SLinus Torvalds 
dm_target_init(void)1791da177e4SLinus Torvalds int __init dm_target_init(void)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	return dm_register_target(&error_target);
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds 
dm_target_exit(void)1841da177e4SLinus Torvalds void dm_target_exit(void)
1851da177e4SLinus Torvalds {
18610d3bd09SMikulas Patocka 	dm_unregister_target(&error_target);
1871da177e4SLinus Torvalds }
188