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
|
// -*- mode:C; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include <errno.h>
#include "objclass/objclass.h"
#include "cls_cas_ops.h"
#include "include/compat.h"
#include "osd/osd_types.h"
CLS_VER(1,0)
CLS_NAME(cas)
struct chunk_obj_refcount;
static int chunk_read_refcount(cls_method_context_t hctx, chunk_obj_refcount *objr)
{
bufferlist bl;
objr->refs.clear();
int ret = cls_cxx_getxattr(hctx, CHUNK_REFCOUNT_ATTR, &bl);
if (ret == -ENODATA) {
return 0;
}
if (ret < 0)
return ret;
try {
auto iter = bl.cbegin();
decode(*objr, iter);
} catch (buffer::error& err) {
CLS_LOG(0, "ERROR: chunk_read_refcount(): failed to decode refcount entry\n");
return -EIO;
}
return 0;
}
static int chunk_set_refcount(cls_method_context_t hctx, const struct chunk_obj_refcount& objr)
{
bufferlist bl;
encode(objr, bl);
int ret = cls_cxx_setxattr(hctx, CHUNK_REFCOUNT_ATTR, &bl);
if (ret < 0)
return ret;
return 0;
}
static int cls_rc_chunk_refcount_get(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
auto in_iter = in->cbegin();
cls_chunk_refcount_get_op op;
try {
decode(op, in_iter);
} catch (buffer::error& err) {
CLS_LOG(1, "ERROR: cls_rc_refcount_get(): failed to decode entry\n");
return -EINVAL;
}
chunk_obj_refcount objr;
int ret = chunk_read_refcount(hctx, &objr);
if (ret < 0)
return ret;
CLS_LOG(10, "cls_rc_chunk_refcount_get() oid=%s\n", op.source.oid.name.c_str());
objr.refs.insert(op.source);
ret = chunk_set_refcount(hctx, objr);
if (ret < 0)
return ret;
return 0;
}
static int cls_rc_chunk_refcount_put(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
auto in_iter = in->cbegin();
cls_chunk_refcount_put_op op;
try {
decode(op, in_iter);
} catch (buffer::error& err) {
CLS_LOG(1, "ERROR: cls_rc_chunk_refcount_put(): failed to decode entry\n");
return -EINVAL;
}
chunk_obj_refcount objr;
int ret = chunk_read_refcount(hctx, &objr);
if (ret < 0)
return ret;
if (objr.refs.empty()) {// shouldn't happen!
CLS_LOG(0, "ERROR: cls_rc_chunk_refcount_put() was called without any references!\n");
return -EINVAL;
}
CLS_LOG(10, "cls_rc_chunk_refcount_put() oid=%s\n", op.source.oid.name.c_str());
bool found = false;
for (auto &p : objr.refs) {
if (p == op.source) {
found = true;
break;
}
}
if (!found) {
return 0;
}
auto p = objr.refs.find(op.source);
objr.refs.erase(p);
if (objr.refs.empty()) {
return cls_cxx_remove(hctx);
}
ret = chunk_set_refcount(hctx, objr);
if (ret < 0)
return ret;
return 0;
}
static int cls_rc_chunk_refcount_set(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
auto in_iter = in->cbegin();
cls_chunk_refcount_set_op op;
try {
decode(op, in_iter);
} catch (buffer::error& err) {
CLS_LOG(1, "ERROR: cls_chunk_refcount_set(): failed to decode entry\n");
return -EINVAL;
}
if (!op.refs.size()) {
return cls_cxx_remove(hctx);
}
chunk_obj_refcount objr;
objr.refs = op.refs;
int ret = chunk_set_refcount(hctx, objr);
if (ret < 0)
return ret;
return 0;
}
static int cls_rc_chunk_refcount_read(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
chunk_obj_refcount objr;
cls_chunk_refcount_read_ret read_ret;
int ret = chunk_read_refcount(hctx, &objr);
if (ret < 0)
return ret;
for (auto &p : objr.refs) {
read_ret.refs.insert(p);
}
encode(read_ret, *out);
return 0;
}
static int cls_rc_write_or_get(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
auto in_iter = in->cbegin();
hobject_t src_obj;
bufferlist indata, outdata;
ceph_osd_op op;
try {
decode (op, in_iter);
decode(src_obj, in_iter);
in_iter.copy(op.extent.length, indata);
}
catch (buffer::error& e) {
return -EINVAL;
}
CLS_LOG(10, " offset: %llu length: %llu \n",
static_cast<long long unsigned>(op.extent.offset),
static_cast<long long unsigned>(op.extent.length));
chunk_obj_refcount objr;
int ret = chunk_read_refcount(hctx, &objr);
if (ret == -ENOENT) {
objr.refs.insert(src_obj);
bufferlist set_bl;
encode(objr, set_bl);
ret = cls_cxx_chunk_write_and_set(hctx, op.extent.offset, op.extent.length, &indata, op.flags,
&set_bl, set_bl.length());
if (ret < 0)
return ret;
return 0;
}
objr.refs.insert(src_obj);
ret = chunk_set_refcount(hctx, objr);
if (ret < 0)
return ret;
return 0;
}
static int cls_rc_has_chunk(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
auto in_iter = in->cbegin();
string fp_oid;
bufferlist indata, outdata;
try {
decode (fp_oid, in_iter);
}
catch (buffer::error& e) {
return -EINVAL;
}
CLS_LOG(10, " fp_oid: %s \n", fp_oid.c_str());
bool ret = cls_has_chunk(hctx, fp_oid);
if (ret) {
return 0;
}
return -ENOENT;
}
CLS_INIT(cas)
{
CLS_LOG(1, "Loaded cas class!");
cls_handle_t h_class;
cls_method_handle_t h_cas_write_or_get;
cls_method_handle_t h_chunk_refcount_get;
cls_method_handle_t h_chunk_refcount_put;
cls_method_handle_t h_chunk_refcount_set;
cls_method_handle_t h_chunk_refcount_read;
cls_method_handle_t h_chunk_has_chunk;
cls_register("cas", &h_class);
/* chunk refcount */
cls_register_cxx_method(h_class, "chunk_get", CLS_METHOD_RD | CLS_METHOD_WR, cls_rc_chunk_refcount_get,
&h_chunk_refcount_get);
cls_register_cxx_method(h_class, "chunk_put", CLS_METHOD_RD | CLS_METHOD_WR, cls_rc_chunk_refcount_put,
&h_chunk_refcount_put);
cls_register_cxx_method(h_class, "chunk_set", CLS_METHOD_RD | CLS_METHOD_WR, cls_rc_chunk_refcount_set,
&h_chunk_refcount_set);
cls_register_cxx_method(h_class, "chunk_read", CLS_METHOD_RD, cls_rc_chunk_refcount_read,
&h_chunk_refcount_read);
cls_register_cxx_method(h_class, "cas_write_or_get", CLS_METHOD_RD | CLS_METHOD_WR, cls_rc_write_or_get,
&h_cas_write_or_get);
cls_register_cxx_method(h_class, "has_chunk", CLS_METHOD_RD, cls_rc_has_chunk,
&h_chunk_has_chunk);
return;
}
|