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