1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Red Hat. All rights reserved.
5 * This file is released under the GPL.
8 #include "dm-cache-policy-internal.h"
11 #include <linux/module.h>
12 #include <linux/slab.h>
14 /*----------------------------------------------------------------*/
16 #define DM_MSG_PREFIX "cache-policy"
18 static DEFINE_SPINLOCK(register_lock
);
19 static LIST_HEAD(register_list
);
21 static struct dm_cache_policy_type
*__find_policy(const char *name
)
23 struct dm_cache_policy_type
*t
;
25 list_for_each_entry(t
, ®ister_list
, list
)
26 if (!strcmp(t
->name
, name
))
32 static struct dm_cache_policy_type
*__get_policy_once(const char *name
)
34 struct dm_cache_policy_type
*t
= __find_policy(name
);
36 if (t
&& !try_module_get(t
->owner
)) {
37 DMWARN("couldn't get module %s", name
);
44 static struct dm_cache_policy_type
*get_policy_once(const char *name
)
46 struct dm_cache_policy_type
*t
;
48 spin_lock(®ister_lock
);
49 t
= __get_policy_once(name
);
50 spin_unlock(®ister_lock
);
55 static struct dm_cache_policy_type
*get_policy(const char *name
)
57 struct dm_cache_policy_type
*t
;
59 t
= get_policy_once(name
);
66 request_module("dm-cache-%s", name
);
68 t
= get_policy_once(name
);
75 static void put_policy(struct dm_cache_policy_type
*t
)
80 int dm_cache_policy_register(struct dm_cache_policy_type
*type
)
84 /* One size fits all for now */
85 if (type
->hint_size
!= 0 && type
->hint_size
!= 4) {
86 DMWARN("hint size must be 0 or 4 but %llu supplied.", (unsigned long long) type
->hint_size
);
90 spin_lock(®ister_lock
);
91 if (__find_policy(type
->name
)) {
92 DMWARN("attempt to register policy under duplicate name %s", type
->name
);
95 list_add(&type
->list
, ®ister_list
);
98 spin_unlock(®ister_lock
);
102 EXPORT_SYMBOL_GPL(dm_cache_policy_register
);
104 void dm_cache_policy_unregister(struct dm_cache_policy_type
*type
)
106 spin_lock(®ister_lock
);
107 list_del_init(&type
->list
);
108 spin_unlock(®ister_lock
);
110 EXPORT_SYMBOL_GPL(dm_cache_policy_unregister
);
112 struct dm_cache_policy
*dm_cache_policy_create(const char *name
,
113 dm_cblock_t cache_size
,
114 sector_t origin_size
,
115 sector_t cache_block_size
)
117 struct dm_cache_policy
*p
= NULL
;
118 struct dm_cache_policy_type
*type
;
120 type
= get_policy(name
);
122 DMWARN("unknown policy type");
123 return ERR_PTR(-EINVAL
);
126 p
= type
->create(cache_size
, origin_size
, cache_block_size
);
129 return ERR_PTR(-ENOMEM
);
135 EXPORT_SYMBOL_GPL(dm_cache_policy_create
);
137 void dm_cache_policy_destroy(struct dm_cache_policy
*p
)
139 struct dm_cache_policy_type
*t
= p
->private;
144 EXPORT_SYMBOL_GPL(dm_cache_policy_destroy
);
146 const char *dm_cache_policy_get_name(struct dm_cache_policy
*p
)
148 struct dm_cache_policy_type
*t
= p
->private;
150 /* if t->real is set then an alias was used (e.g. "default") */
152 return t
->real
->name
;
156 EXPORT_SYMBOL_GPL(dm_cache_policy_get_name
);
158 const unsigned int *dm_cache_policy_get_version(struct dm_cache_policy
*p
)
160 struct dm_cache_policy_type
*t
= p
->private;
164 EXPORT_SYMBOL_GPL(dm_cache_policy_get_version
);
166 size_t dm_cache_policy_get_hint_size(struct dm_cache_policy
*p
)
168 struct dm_cache_policy_type
*t
= p
->private;
172 EXPORT_SYMBOL_GPL(dm_cache_policy_get_hint_size
);
174 /*----------------------------------------------------------------*/