blob: 11dea11dc0b649e16594e071163f79647c274293 [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{
116 return 0;
117}
118
Cheng Renquan45194e42009-04-02 19:55:28 +0100119static void io_err_dtr(struct dm_target *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 /* empty */
122}
123
Cheng Renquan45194e42009-04-02 19:55:28 +0100124static int io_err_map(struct dm_target *tt, struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 union map_info *map_context)
126{
127 return -EIO;
128}
129
130static struct target_type error_target = {
131 .name = "error",
132 .version = {1, 0, 1},
133 .ctr = io_err_ctr,
134 .dtr = io_err_dtr,
135 .map = io_err_map,
136};
137
138int __init dm_target_init(void)
139{
140 return dm_register_target(&error_target);
141}
142
143void dm_target_exit(void)
144{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +0000145 dm_unregister_target(&error_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148EXPORT_SYMBOL(dm_register_target);
149EXPORT_SYMBOL(dm_unregister_target);