1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "include/types.h"
#include "ClassHandler.h"
#include "common/errno.h"
#include "common/ceph_context.h"
#include "include/dlfcn_compat.h"
#include <map>
#if defined(__FreeBSD__)
#include <sys/param.h>
#endif
#include "common/config.h"
#include "common/debug.h"
#define dout_subsys ceph_subsys_osd
#undef dout_prefix
#define dout_prefix *_dout
#define CLS_PREFIX "libcls_"
#define CLS_SUFFIX SHARED_LIB_SUFFIX
using std::map;
using std::set;
using std::string;
using ceph::bufferlist;
int ClassHandler::open_class(const string& cname, ClassData **pcls)
{
std::lock_guard lock(mutex);
ClassData *cls = _get_class(cname, true);
if (!cls)
return -EPERM;
if (cls->status != ClassData::CLASS_OPEN) {
int r = _load_class(cls);
if (r)
return r;
}
*pcls = cls;
return 0;
}
int ClassHandler::open_all_classes()
{
ldout(cct, 10) << __func__ << dendl;
DIR *dir = ::opendir(cct->_conf->osd_class_dir.c_str());
if (!dir)
return -errno;
struct dirent *pde = nullptr;
int r = 0;
while ((pde = ::readdir(dir))) {
if (pde->d_name[0] == '.')
continue;
if (strlen(pde->d_name) > sizeof(CLS_PREFIX) - 1 + sizeof(CLS_SUFFIX) - 1 &&
strncmp(pde->d_name, CLS_PREFIX, sizeof(CLS_PREFIX) - 1) == 0 &&
strcmp(pde->d_name + strlen(pde->d_name) - (sizeof(CLS_SUFFIX) - 1), CLS_SUFFIX) == 0) {
char cname[PATH_MAX + 1];
strncpy(cname, pde->d_name + sizeof(CLS_PREFIX) - 1, sizeof(cname) -1);
cname[strlen(cname) - (sizeof(CLS_SUFFIX) - 1)] = '\0';
ldout(cct, 10) << __func__ << " found " << cname << dendl;
ClassData *cls;
// skip classes that aren't in 'osd class load list'
r = open_class(cname, &cls);
if (r < 0 && r != -EPERM)
goto out;
}
}
out:
closedir(dir);
return r;
}
void ClassHandler::shutdown()
{
for (auto& cls : classes) {
if (cls.second.handle) {
dlclose(cls.second.handle);
}
}
classes.clear();
}
/*
* Check if @cname is in the whitespace delimited list @list, or the @list
* contains the wildcard "*".
*
* This is expensive but doesn't consume memory for an index, and is performed
* only once when a class is loaded.
*/
bool ClassHandler::in_class_list(const std::string& cname,
const std::string& list)
{
std::istringstream ss(list);
std::istream_iterator<std::string> begin{ss};
std::istream_iterator<std::string> end{};
const std::vector<std::string> targets{cname, "*"};
auto it = std::find_first_of(begin, end,
targets.begin(), targets.end());
return it != end;
}
ClassHandler::ClassData *ClassHandler::_get_class(const string& cname,
bool check_allowed)
{
ClassData *cls;
map<string, ClassData>::iterator iter = classes.find(cname);
if (iter != classes.end()) {
cls = &iter->second;
} else {
if (check_allowed && !in_class_list(cname, cct->_conf->osd_class_load_list)) {
ldout(cct, 0) << "_get_class not permitted to load " << cname << dendl;
return NULL;
}
cls = &classes[cname];
ldout(cct, 10) << "_get_class adding new class name " << cname << " " << cls << dendl;
cls->name = cname;
cls->handler = this;
cls->allowed = in_class_list(cname, cct->_conf->osd_class_default_list);
}
return cls;
}
int ClassHandler::_load_class(ClassData *cls)
{
// already open
if (cls->status == ClassData::CLASS_OPEN)
return 0;
if (cls->status == ClassData::CLASS_UNKNOWN ||
cls->status == ClassData::CLASS_MISSING) {
char fname[PATH_MAX];
snprintf(fname, sizeof(fname), "%s/" CLS_PREFIX "%s" CLS_SUFFIX,
cct->_conf->osd_class_dir.c_str(),
cls->name.c_str());
ldout(cct, 10) << "_load_class " << cls->name << " from " << fname << dendl;
cls->handle = dlopen(fname, RTLD_NOW);
if (!cls->handle) {
struct stat st;
int r = ::stat(fname, &st);
if (r < 0) {
r = -errno;
ldout(cct, 0) << __func__ << " could not stat class " << fname
<< ": " << cpp_strerror(r) << dendl;
} else {
ldout(cct, 0) << "_load_class could not open class " << fname
<< " (dlopen failed): " << dlerror() << dendl;
r = -EIO;
}
cls->status = ClassData::CLASS_MISSING;
return r;
}
cls_deps_t *(*cls_deps)();
cls_deps = (cls_deps_t *(*)())dlsym(cls->handle, "class_deps");
if (cls_deps) {
cls_deps_t *deps = cls_deps();
while (deps) {
if (!deps->name)
break;
ClassData *cls_dep = _get_class(deps->name, false);
cls->dependencies.insert(cls_dep);
if (cls_dep->status != ClassData::CLASS_OPEN)
cls->missing_dependencies.insert(cls_dep);
deps++;
}
}
}
// resolve dependencies
set<ClassData*>::iterator p = cls->missing_dependencies.begin();
while (p != cls->missing_dependencies.end()) {
ClassData *dc = *p;
int r = _load_class(dc);
if (r < 0) {
cls->status = ClassData::CLASS_MISSING_DEPS;
return r;
}
ldout(cct, 10) << "_load_class " << cls->name << " satisfied dependency " << dc->name << dendl;
cls->missing_dependencies.erase(p++);
}
// initialize
void (*cls_init)() = (void (*)())dlsym(cls->handle, "__cls_init");
if (cls_init) {
cls->status = ClassData::CLASS_INITIALIZING;
cls_init();
}
ldout(cct, 10) << "_load_class " << cls->name << " success" << dendl;
cls->status = ClassData::CLASS_OPEN;
return 0;
}
ClassHandler::ClassData *ClassHandler::register_class(const char *cname)
{
ceph_assert(ceph_mutex_is_locked(mutex));
ClassData *cls = _get_class(cname, false);
ldout(cct, 10) << "register_class " << cname << " status " << cls->status << dendl;
if (cls->status != ClassData::CLASS_INITIALIZING) {
ldout(cct, 0) << "class " << cname << " isn't loaded; is the class registering under the wrong name?" << dendl;
return NULL;
}
return cls;
}
void ClassHandler::unregister_class(ClassHandler::ClassData *cls)
{
/* FIXME: do we really need this one? */
}
ClassHandler::ClassMethod *ClassHandler::ClassData::register_method(const char *mname,
int flags,
cls_method_call_t func)
{
/* no need for locking, called under the class_init mutex */
if (!flags) {
lderr(handler->cct) << "register_method " << name << "." << mname
<< " flags " << flags << " " << (void*)func
<< " FAILED -- flags must be non-zero" << dendl;
return NULL;
}
ldout(handler->cct, 10) << "register_method " << name << "." << mname << " flags " << flags << " " << (void*)func << dendl;
[[maybe_unused]] auto [method, added] = methods_map.try_emplace(mname, mname, func, flags, this);
return &method->second;
}
ClassHandler::ClassMethod *ClassHandler::ClassData::register_cxx_method(const char *mname,
int flags,
cls_method_cxx_call_t func)
{
/* no need for locking, called under the class_init mutex */
ldout(handler->cct, 10) << "register_cxx_method " << name << "." << mname << " flags " << flags << " " << (void*)func << dendl;
[[maybe_unused]] auto [method, added] = methods_map.try_emplace(mname, mname, func, flags, this);
return &method->second;
}
ClassHandler::ClassFilter *ClassHandler::ClassData::register_cxx_filter(
const std::string &filter_name,
cls_cxx_filter_factory_t fn)
{
ClassFilter &filter = filters_map[filter_name];
filter.fn = fn;
filter.name = filter_name;
filter.cls = this;
return &filter;
}
ClassHandler::ClassMethod *ClassHandler::ClassData::_get_method(
const std::string& mname)
{
if (auto iter = methods_map.find(mname); iter != methods_map.end()) {
return &(iter->second);
} else {
return nullptr;
}
}
int ClassHandler::ClassData::get_method_flags(const std::string& mname)
{
std::lock_guard l(handler->mutex);
ClassMethod *method = _get_method(mname);
if (!method)
return -ENOENT;
return method->flags;
}
void ClassHandler::ClassData::unregister_method(ClassHandler::ClassMethod *method)
{
/* no need for locking, called under the class_init mutex */
map<string, ClassMethod>::iterator iter = methods_map.find(method->name);
if (iter == methods_map.end())
return;
methods_map.erase(iter);
}
void ClassHandler::ClassMethod::unregister()
{
cls->unregister_method(this);
}
void ClassHandler::ClassData::unregister_filter(ClassHandler::ClassFilter *filter)
{
/* no need for locking, called under the class_init mutex */
map<string, ClassFilter>::iterator iter = filters_map.find(filter->name);
if (iter == filters_map.end())
return;
filters_map.erase(iter);
}
void ClassHandler::ClassFilter::unregister()
{
cls->unregister_filter(this);
}
int ClassHandler::ClassMethod::exec(cls_method_context_t ctx, bufferlist& indata, bufferlist& outdata)
{
int ret = 0;
std::visit([&](auto method) {
using method_t = decltype(method);
if constexpr (std::is_same_v<method_t, cls_method_cxx_call_t>) {
// C++ call version
ret = method(ctx, &indata, &outdata);
} else if constexpr (std::is_same_v<method_t, cls_method_call_t>) {
// C version
char *out = nullptr;
int olen = 0;
ret = method(ctx, indata.c_str(), indata.length(), &out, &olen);
if (out) {
// assume *out was allocated via cls_alloc (which calls malloc!)
ceph::buffer::ptr bp = ceph::buffer::claim_malloc(olen, out);
outdata.push_back(bp);
}
} else {
static_assert(std::is_same_v<method_t, void>);
}
}, func);
return ret;
}
ClassHandler& ClassHandler::get_instance()
{
#ifdef WITH_SEASTAR
// the context is being used solely for:
// 1. random number generation (cls_gen_random_bytes)
// 2. accessing the configuration
// 3. logging
static CephContext cct;
static ClassHandler single(&cct);
#else
static ClassHandler single(g_ceph_context);
#endif // WITH_SEASTAR
return single;
}
|