Linux 3.4.102
[linux/fpc-iii.git] / drivers / md / dm-path-selector.c
blobfa0ccc585cb4cb5ff4551d9bafd8dfd23b6f063c
1 /*
2 * Copyright (C) 2003 Sistina Software.
3 * Copyright (C) 2004 Red Hat, Inc. All rights reserved.
5 * Module Author: Heinz Mauelshagen
7 * This file is released under the GPL.
9 * Path selector registration.
12 #include <linux/device-mapper.h>
13 #include <linux/module.h>
15 #include "dm-path-selector.h"
17 #include <linux/slab.h>
19 struct ps_internal {
20 struct path_selector_type pst;
21 struct list_head list;
24 #define pst_to_psi(__pst) container_of((__pst), struct ps_internal, pst)
26 static LIST_HEAD(_path_selectors);
27 static DECLARE_RWSEM(_ps_lock);
29 static struct ps_internal *__find_path_selector_type(const char *name)
31 struct ps_internal *psi;
33 list_for_each_entry(psi, &_path_selectors, list) {
34 if (!strcmp(name, psi->pst.name))
35 return psi;
38 return NULL;
41 static struct ps_internal *get_path_selector(const char *name)
43 struct ps_internal *psi;
45 down_read(&_ps_lock);
46 psi = __find_path_selector_type(name);
47 if (psi && !try_module_get(psi->pst.module))
48 psi = NULL;
49 up_read(&_ps_lock);
51 return psi;
54 struct path_selector_type *dm_get_path_selector(const char *name)
56 struct ps_internal *psi;
58 if (!name)
59 return NULL;
61 psi = get_path_selector(name);
62 if (!psi) {
63 request_module("dm-%s", name);
64 psi = get_path_selector(name);
67 return psi ? &psi->pst : NULL;
70 void dm_put_path_selector(struct path_selector_type *pst)
72 struct ps_internal *psi;
74 if (!pst)
75 return;
77 down_read(&_ps_lock);
78 psi = __find_path_selector_type(pst->name);
79 if (!psi)
80 goto out;
82 module_put(psi->pst.module);
83 out:
84 up_read(&_ps_lock);
87 static struct ps_internal *_alloc_path_selector(struct path_selector_type *pst)
89 struct ps_internal *psi = kzalloc(sizeof(*psi), GFP_KERNEL);
91 if (psi)
92 psi->pst = *pst;
94 return psi;
97 int dm_register_path_selector(struct path_selector_type *pst)
99 int r = 0;
100 struct ps_internal *psi = _alloc_path_selector(pst);
102 if (!psi)
103 return -ENOMEM;
105 down_write(&_ps_lock);
107 if (__find_path_selector_type(pst->name)) {
108 kfree(psi);
109 r = -EEXIST;
110 } else
111 list_add(&psi->list, &_path_selectors);
113 up_write(&_ps_lock);
115 return r;
118 int dm_unregister_path_selector(struct path_selector_type *pst)
120 struct ps_internal *psi;
122 down_write(&_ps_lock);
124 psi = __find_path_selector_type(pst->name);
125 if (!psi) {
126 up_write(&_ps_lock);
127 return -EINVAL;
130 list_del(&psi->list);
132 up_write(&_ps_lock);
134 kfree(psi);
136 return 0;
139 EXPORT_SYMBOL_GPL(dm_register_path_selector);
140 EXPORT_SYMBOL_GPL(dm_unregister_path_selector);