| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2001 Sistina Software (UK) Limited | 
 | 3 |  * | 
 | 4 |  * This file is released under the GPL. | 
 | 5 |  */ | 
 | 6 |  | 
 | 7 | #include "dm.h" | 
 | 8 |  | 
 | 9 | #include <linux/module.h> | 
 | 10 | #include <linux/init.h> | 
 | 11 | #include <linux/kmod.h> | 
 | 12 | #include <linux/bio.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 |  | 
| Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 14 | #define DM_MSG_PREFIX "target" | 
 | 15 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | static LIST_HEAD(_targets); | 
 | 17 | static DECLARE_RWSEM(_lock); | 
 | 18 |  | 
 | 19 | #define DM_MOD_NAME_SIZE 32 | 
 | 20 |  | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 21 | static inline struct target_type *__find_target_type(const char *name) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | { | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 23 | 	struct target_type *tt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 |  | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 25 | 	list_for_each_entry(tt, &_targets, list) | 
 | 26 | 		if (!strcmp(name, tt->name)) | 
 | 27 | 			return tt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 |  | 
 | 29 | 	return NULL; | 
 | 30 | } | 
 | 31 |  | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 32 | static struct target_type *get_target_type(const char *name) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 34 | 	struct target_type *tt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 |  | 
 | 36 | 	down_read(&_lock); | 
 | 37 |  | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 38 | 	tt = __find_target_type(name); | 
 | 39 | 	if (tt && !try_module_get(tt->module)) | 
 | 40 | 		tt = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 |  | 
 | 42 | 	up_read(&_lock); | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 43 | 	return tt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 53 | 	struct target_type *tt = get_target_type(name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 |  | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 55 | 	if (!tt) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | 		load_module(name); | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 57 | 		tt = get_target_type(name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | 	} | 
 | 59 |  | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 60 | 	return tt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } | 
 | 62 |  | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 63 | void dm_put_target_type(struct target_type *tt) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | 	down_read(&_lock); | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 66 | 	module_put(tt->module); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | 	up_read(&_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } | 
 | 69 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | int dm_target_iterate(void (*iter_func)(struct target_type *tt, | 
 | 71 | 					void *param), void *param) | 
 | 72 | { | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 73 | 	struct target_type *tt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 |  | 
 | 75 | 	down_read(&_lock); | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 76 | 	list_for_each_entry(tt, &_targets, list) | 
 | 77 | 		iter_func(tt, param); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | 	up_read(&_lock); | 
 | 79 |  | 
 | 80 | 	return 0; | 
 | 81 | } | 
 | 82 |  | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 83 | int dm_register_target(struct target_type *tt) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { | 
 | 85 | 	int rv = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 |  | 
 | 87 | 	down_write(&_lock); | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 88 | 	if (__find_target_type(tt->name)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | 		rv = -EEXIST; | 
 | 90 | 	else | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 91 | 		list_add(&tt->list, &_targets); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 |  | 
 | 93 | 	up_write(&_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | 	return rv; | 
 | 95 | } | 
 | 96 |  | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 97 | void dm_unregister_target(struct target_type *tt) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | 	down_write(&_lock); | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 100 | 	if (!__find_target_type(tt->name)) { | 
 | 101 | 		DMCRIT("Unregistering unrecognised target: %s", tt->name); | 
| Mikulas Patocka | 10d3bd0 | 2009-01-06 03:04:58 +0000 | [diff] [blame] | 102 | 		BUG(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | 	} | 
 | 104 |  | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 105 | 	list_del(&tt->list); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 |  | 
 | 107 | 	up_write(&_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } | 
 | 109 |  | 
 | 110 | /* | 
 | 111 |  * io-err: always fails an io, useful for bringing | 
 | 112 |  * up LVs that have holes in them. | 
 | 113 |  */ | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 114 | static int io_err_ctr(struct dm_target *tt, unsigned int argc, char **args) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { | 
| Mike Snitzer | 38e1b25 | 2010-08-12 04:14:14 +0100 | [diff] [blame] | 116 | 	/* | 
 | 117 | 	 * Return error for discards instead of -EOPNOTSUPP | 
 | 118 | 	 */ | 
 | 119 | 	tt->num_discard_requests = 1; | 
 | 120 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | 	return 0; | 
 | 122 | } | 
 | 123 |  | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 124 | static void io_err_dtr(struct dm_target *tt) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { | 
 | 126 | 	/* empty */ | 
 | 127 | } | 
 | 128 |  | 
| Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 129 | static int io_err_map(struct dm_target *tt, struct bio *bio, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | 		      union map_info *map_context) | 
 | 131 | { | 
 | 132 | 	return -EIO; | 
 | 133 | } | 
 | 134 |  | 
 | 135 | static struct target_type error_target = { | 
 | 136 | 	.name = "error", | 
 | 137 | 	.version = {1, 0, 1}, | 
 | 138 | 	.ctr  = io_err_ctr, | 
 | 139 | 	.dtr  = io_err_dtr, | 
 | 140 | 	.map  = io_err_map, | 
 | 141 | }; | 
 | 142 |  | 
 | 143 | int __init dm_target_init(void) | 
 | 144 | { | 
 | 145 | 	return dm_register_target(&error_target); | 
 | 146 | } | 
 | 147 |  | 
 | 148 | void dm_target_exit(void) | 
 | 149 | { | 
| Mikulas Patocka | 10d3bd0 | 2009-01-06 03:04:58 +0000 | [diff] [blame] | 150 | 	dm_unregister_target(&error_target); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } | 
 | 152 |  | 
 | 153 | EXPORT_SYMBOL(dm_register_target); | 
 | 154 | EXPORT_SYMBOL(dm_unregister_target); |