1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
5 #include "common/ceph_context.h"
6 #include "common/ceph_releases.h"
7 #include "common/config.h"
8 #include "common/debug.h"
10 #include "objclass/objclass.h"
11 #include "osd/osd_internal_types.h"
13 #include "osd/ClassHandler.h"
15 #include "auth/Crypto.h"
16 #include "common/armor.h"
18 #define dout_context ClassHandler::get_instance().cct
20 void *cls_alloc(size_t size
)
25 void cls_free(void *p
)
30 int cls_register(const char *name
, cls_handle_t
*handle
)
32 ClassHandler::ClassData
*cls
= \
33 ClassHandler::get_instance().register_class(name
);
34 *handle
= (cls_handle_t
)cls
;
38 int cls_unregister(cls_handle_t handle
)
40 ClassHandler::ClassData
*cls
= (ClassHandler::ClassData
*)handle
;
41 ClassHandler::get_instance().unregister_class(cls
);
45 int cls_register_method(cls_handle_t hclass
, const char *method
,
47 cls_method_call_t class_call
, cls_method_handle_t
*handle
)
49 if (!(flags
& (CLS_METHOD_RD
| CLS_METHOD_WR
)))
51 ClassHandler::ClassData
*cls
= (ClassHandler::ClassData
*)hclass
;
52 cls_method_handle_t hmethod
=(cls_method_handle_t
)cls
->register_method(method
, flags
, class_call
);
55 return (hmethod
!= NULL
);
58 int cls_register_cxx_method(cls_handle_t hclass
, const char *method
,
60 cls_method_cxx_call_t class_call
, cls_method_handle_t
*handle
)
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
);
66 return (hmethod
!= NULL
);
69 int cls_unregister_method(cls_method_handle_t handle
)
71 ClassHandler::ClassMethod
*method
= (ClassHandler::ClassMethod
*)handle
;
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
)
81 ClassHandler::ClassData
*cls
= (ClassHandler::ClassData
*)hclass
;
82 cls_filter_handle_t hfilter
= (cls_filter_handle_t
)cls
->register_cxx_filter(filter_name
, fn
);
86 return (hfilter
!= NULL
);
89 void cls_unregister_filter(cls_filter_handle_t handle
)
91 ClassHandler::ClassFilter
*filter
= (ClassHandler::ClassFilter
*)handle
;
95 int cls_cxx_read(cls_method_context_t hctx
, int ofs
, int len
,
96 ceph::buffer::list
*outbl
)
98 return cls_cxx_read2(hctx
, ofs
, len
, outbl
, 0);
101 int cls_cxx_write(cls_method_context_t hctx
, int ofs
, int len
,
102 ceph::buffer::list
*inbl
)
104 return cls_cxx_write2(hctx
, ofs
, len
, inbl
, 0);
107 int cls_gen_random_bytes(char *buf
, int size
)
109 ClassHandler::get_instance().cct
->random()->get_bytes(buf
, size
);
113 int cls_gen_rand_base64(char *dest
, int size
) /* size should be the required string size + 1 */
116 char tmp_dest
[size
+ 4]; /* so that there's space for the extra '=' characters, and some */
119 ret
= cls_gen_random_bytes(buf
, sizeof(buf
));
121 derr
<< "cannot get random bytes: " << ret
<< dendl
;
125 ret
= ceph_armor(tmp_dest
, &tmp_dest
[sizeof(tmp_dest
)],
126 (const char *)buf
, ((const char *)buf
) + ((size
- 1) * 3 + 4 - 1) / 4);
128 derr
<< "ceph_armor failed" << dendl
;
131 tmp_dest
[ret
] = '\0';
132 memcpy(dest
, tmp_dest
, size
);
138 void cls_cxx_subop_version(cls_method_context_t hctx
, std::string
*s
)
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
);