blob: 617d21a772568dad424aa1b49cb0a53de980b775 [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 */
119 tt->num_discard_requests = 1;
120
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
134static struct target_type error_target = {
135 .name = "error",
Mikulas Patocka7de3ee52012-12-21 20:23:41 +0000136 .version = {1, 1, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .ctr = io_err_ctr,
138 .dtr = io_err_dtr,
139 .map = io_err_map,
140};
141
142int __init dm_target_init(void)
143{
144 return dm_register_target(&error_target);
145}
146
147void dm_target_exit(void)
148{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +0000149 dm_unregister_target(&error_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152EXPORT_SYMBOL(dm_register_target);
153EXPORT_SYMBOL(dm_unregister_target);