blob: 925ec1b15e75ede24ea91c74667d426950af251e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070013
Alasdair G Kergon72d94862006-06-26 00:27:35 -070014#define DM_MSG_PREFIX "target"
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016static LIST_HEAD(_targets);
17static DECLARE_RWSEM(_lock);
18
19#define DM_MOD_NAME_SIZE 32
20
Cheng Renquan45194e42009-04-02 19:55:28 +010021static inline struct target_type *__find_target_type(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
Cheng Renquan45194e42009-04-02 19:55:28 +010023 struct target_type *tt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Cheng Renquan45194e42009-04-02 19:55:28 +010025 list_for_each_entry(tt, &_targets, list)
26 if (!strcmp(name, tt->name))
27 return tt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29 return NULL;
30}
31
Cheng Renquan45194e42009-04-02 19:55:28 +010032static struct target_type *get_target_type(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Cheng Renquan45194e42009-04-02 19:55:28 +010034 struct target_type *tt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36 down_read(&_lock);
37
Cheng Renquan45194e42009-04-02 19:55:28 +010038 tt = __find_target_type(name);
39 if (tt && !try_module_get(tt->module))
40 tt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42 up_read(&_lock);
Cheng Renquan45194e42009-04-02 19:55:28 +010043 return tt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
46static void load_module(const char *name)
47{
48 request_module("dm-%s", name);
49}
50
51struct target_type *dm_get_target_type(const char *name)
52{
Cheng Renquan45194e42009-04-02 19:55:28 +010053 struct target_type *tt = get_target_type(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Cheng Renquan45194e42009-04-02 19:55:28 +010055 if (!tt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 load_module(name);
Cheng Renquan45194e42009-04-02 19:55:28 +010057 tt = get_target_type(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
59
Cheng Renquan45194e42009-04-02 19:55:28 +010060 return tt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
Cheng Renquan45194e42009-04-02 19:55:28 +010063void dm_put_target_type(struct target_type *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 down_read(&_lock);
Cheng Renquan45194e42009-04-02 19:55:28 +010066 module_put(tt->module);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 up_read(&_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070int dm_target_iterate(void (*iter_func)(struct target_type *tt,
71 void *param), void *param)
72{
Cheng Renquan45194e42009-04-02 19:55:28 +010073 struct target_type *tt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 down_read(&_lock);
Cheng Renquan45194e42009-04-02 19:55:28 +010076 list_for_each_entry(tt, &_targets, list)
77 iter_func(tt, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 up_read(&_lock);
79
80 return 0;
81}
82
Cheng Renquan45194e42009-04-02 19:55:28 +010083int dm_register_target(struct target_type *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 int rv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 down_write(&_lock);
Cheng Renquan45194e42009-04-02 19:55:28 +010088 if (__find_target_type(tt->name))
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 rv = -EEXIST;
90 else
Cheng Renquan45194e42009-04-02 19:55:28 +010091 list_add(&tt->list, &_targets);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 up_write(&_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return rv;
95}
96
Cheng Renquan45194e42009-04-02 19:55:28 +010097void dm_unregister_target(struct target_type *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 down_write(&_lock);
Cheng Renquan45194e42009-04-02 19:55:28 +0100100 if (!__find_target_type(tt->name)) {
101 DMCRIT("Unregistering unrecognised target: %s", tt->name);
Mikulas Patocka10d3bd02009-01-06 03:04:58 +0000102 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104
Cheng Renquan45194e42009-04-02 19:55:28 +0100105 list_del(&tt->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 up_write(&_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
110/*
111 * io-err: always fails an io, useful for bringing
112 * up LVs that have holes in them.
113 */
Cheng Renquan45194e42009-04-02 19:55:28 +0100114static int io_err_ctr(struct dm_target *tt, unsigned int argc, char **args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Mike Snitzer38e1b252010-08-12 04:14:14 +0100116 /*
117 * Return error for discards instead of -EOPNOTSUPP
118 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +0000119 tt->num_discard_bios = 1;
Mike Snitzer38e1b252010-08-12 04:14:14 +0100120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return 0;
122}
123
Cheng Renquan45194e42009-04-02 19:55:28 +0100124static void io_err_dtr(struct dm_target *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 /* empty */
127}
128
Mikulas Patocka7de3ee52012-12-21 20:23:41 +0000129static int io_err_map(struct dm_target *tt, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 return -EIO;
132}
133
Mike Snitzer169e2cc2013-08-22 18:21:38 -0400134static int io_err_map_rq(struct dm_target *ti, struct request *clone,
135 union map_info *map_context)
136{
137 return -EIO;
138}
139
Mike Snitzere5863d92014-12-17 21:08:12 -0500140static int io_err_clone_and_map_rq(struct dm_target *ti, struct request *rq,
141 union map_info *map_context,
142 struct request **clone)
143{
144 return -EIO;
145}
146
147static void io_err_release_clone_rq(struct request *clone)
148{
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static struct target_type error_target = {
152 .name = "error",
Mike Snitzere5863d92014-12-17 21:08:12 -0500153 .version = {1, 3, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 .ctr = io_err_ctr,
155 .dtr = io_err_dtr,
156 .map = io_err_map,
Mike Snitzer169e2cc2013-08-22 18:21:38 -0400157 .map_rq = io_err_map_rq,
Mike Snitzere5863d92014-12-17 21:08:12 -0500158 .clone_and_map_rq = io_err_clone_and_map_rq,
159 .release_clone_rq = io_err_release_clone_rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
161
162int __init dm_target_init(void)
163{
164 return dm_register_target(&error_target);
165}
166
167void dm_target_exit(void)
168{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +0000169 dm_unregister_target(&error_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172EXPORT_SYMBOL(dm_register_target);
173EXPORT_SYMBOL(dm_unregister_target);