]> git.proxmox.com Git - ceph.git/blob - ceph/src/objclass/class_api.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / objclass / class_api.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include <cstdarg>
5 #include "common/ceph_context.h"
6 #include "common/ceph_releases.h"
7 #include "common/config.h"
8 #include "common/debug.h"
9
10 #include "objclass/objclass.h"
11 #include "osd/osd_internal_types.h"
12
13 #include "osd/ClassHandler.h"
14
15 #include "auth/Crypto.h"
16 #include "common/armor.h"
17
18 #define dout_context ClassHandler::get_instance().cct
19
20 void *cls_alloc(size_t size)
21 {
22 return malloc(size);
23 }
24
25 void cls_free(void *p)
26 {
27 free(p);
28 }
29
30 int cls_register(const char *name, cls_handle_t *handle)
31 {
32 ClassHandler::ClassData *cls = \
33 ClassHandler::get_instance().register_class(name);
34 *handle = (cls_handle_t)cls;
35 return (cls != NULL);
36 }
37
38 int cls_unregister(cls_handle_t handle)
39 {
40 ClassHandler::ClassData *cls = (ClassHandler::ClassData *)handle;
41 ClassHandler::get_instance().unregister_class(cls);
42 return 1;
43 }
44
45 int cls_register_method(cls_handle_t hclass, const char *method,
46 int flags,
47 cls_method_call_t class_call, cls_method_handle_t *handle)
48 {
49 if (!(flags & (CLS_METHOD_RD | CLS_METHOD_WR)))
50 return -EINVAL;
51 ClassHandler::ClassData *cls = (ClassHandler::ClassData *)hclass;
52 cls_method_handle_t hmethod =(cls_method_handle_t)cls->register_method(method, flags, class_call);
53 if (handle)
54 *handle = hmethod;
55 return (hmethod != NULL);
56 }
57
58 int cls_register_cxx_method(cls_handle_t hclass, const char *method,
59 int flags,
60 cls_method_cxx_call_t class_call, cls_method_handle_t *handle)
61 {
62 ClassHandler::ClassData *cls = (ClassHandler::ClassData *)hclass;
63 cls_method_handle_t hmethod = (cls_method_handle_t)cls->register_cxx_method(method, flags, class_call);
64 if (handle)
65 *handle = hmethod;
66 return (hmethod != NULL);
67 }
68
69 int cls_unregister_method(cls_method_handle_t handle)
70 {
71 ClassHandler::ClassMethod *method = (ClassHandler::ClassMethod *)handle;
72 method->unregister();
73 return 1;
74 }
75
76 int cls_register_cxx_filter(cls_handle_t hclass,
77 const std::string &filter_name,
78 cls_cxx_filter_factory_t fn,
79 cls_filter_handle_t *handle)
80 {
81 ClassHandler::ClassData *cls = (ClassHandler::ClassData *)hclass;
82 cls_filter_handle_t hfilter = (cls_filter_handle_t)cls->register_cxx_filter(filter_name, fn);
83 if (handle) {
84 *handle = hfilter;
85 }
86 return (hfilter != NULL);
87 }
88
89 void cls_unregister_filter(cls_filter_handle_t handle)
90 {
91 ClassHandler::ClassFilter *filter = (ClassHandler::ClassFilter *)handle;
92 filter->unregister();
93 }
94
95 int cls_cxx_read(cls_method_context_t hctx, int ofs, int len,
96 ceph::buffer::list *outbl)
97 {
98 return cls_cxx_read2(hctx, ofs, len, outbl, 0);
99 }
100
101 int cls_cxx_write(cls_method_context_t hctx, int ofs, int len,
102 ceph::buffer::list *inbl)
103 {
104 return cls_cxx_write2(hctx, ofs, len, inbl, 0);
105 }
106
107 int cls_gen_random_bytes(char *buf, int size)
108 {
109 ClassHandler::get_instance().cct->random()->get_bytes(buf, size);
110 return 0;
111 }
112
113 int cls_gen_rand_base64(char *dest, int size) /* size should be the required string size + 1 */
114 {
115 char buf[size];
116 char tmp_dest[size + 4]; /* so that there's space for the extra '=' characters, and some */
117 int ret;
118
119 ret = cls_gen_random_bytes(buf, sizeof(buf));
120 if (ret < 0) {
121 derr << "cannot get random bytes: " << ret << dendl;
122 return -1;
123 }
124
125 ret = ceph_armor(tmp_dest, &tmp_dest[sizeof(tmp_dest)],
126 (const char *)buf, ((const char *)buf) + ((size - 1) * 3 + 4 - 1) / 4);
127 if (ret < 0) {
128 derr << "ceph_armor failed" << dendl;
129 return -1;
130 }
131 tmp_dest[ret] = '\0';
132 memcpy(dest, tmp_dest, size);
133 dest[size-1] = '\0';
134
135 return 0;
136 }
137
138 void cls_cxx_subop_version(cls_method_context_t hctx, std::string *s)
139 {
140 if (!s)
141 return;
142
143 char buf[32];
144 uint64_t ver = cls_current_version(hctx);
145 int subop_num = cls_current_subop_num(hctx);
146 snprintf(buf, sizeof(buf), "%lld.%d", (long long)ver, subop_num);
147
148 *s = buf;
149 }