forked from ceph/ceph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcls_rgw.cc
380 lines (328 loc) · 10.6 KB
/
cls_rgw.cc
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "include/types.h"
#include "include/utime.h"
#include "objclass/objclass.h"
#include "rgw/rgw_cls_api.h"
#include "common/Clock.h"
#include "global/global_context.h"
CLS_VER(1,0)
CLS_NAME(rgw)
cls_handle_t h_class;
cls_method_handle_t h_rgw_bucket_init_index;
cls_method_handle_t h_rgw_bucket_list;
cls_method_handle_t h_rgw_bucket_prepare_op;
cls_method_handle_t h_rgw_bucket_complete_op;
cls_method_handle_t h_rgw_dir_suggest_changes;
#define ROUND_BLOCK_SIZE 4096
static uint64_t get_rounded_size(uint64_t size)
{
return (size + ROUND_BLOCK_SIZE - 1) & ~(ROUND_BLOCK_SIZE - 1);
}
static int read_bucket_dir(cls_method_context_t hctx, struct rgw_bucket_dir& dir)
{
bufferlist bl;
uint64_t size;
int rc = cls_cxx_stat(hctx, &size, NULL);
if (rc < 0)
return rc;
rc = cls_cxx_map_read_full(hctx, &bl);
if (rc < 0)
return rc;
try {
bufferlist::iterator iter = bl.begin();
bufferlist header_bl;
::decode(header_bl, iter);
bufferlist::iterator header_iter = header_bl.begin();
::decode(dir.header, header_iter);
__u32 nkeys = 0;
::decode(nkeys, iter);
while (nkeys) {
string key;
bufferlist value;
::decode(key, iter);
::decode(value, iter);
bufferlist::iterator val_iter = value.begin();
::decode(dir.m[key], val_iter);
--nkeys;
}
} catch (buffer::error& err) {
CLS_LOG("ERROR: read_bucket_dir(): failed to decode buffer\n");
return -EIO;
}
return 0;
}
int rgw_bucket_list(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
bufferlist bl;
struct rgw_bucket_dir dir;
int rc = read_bucket_dir(hctx, dir);
if (rc < 0)
return rc;
bufferlist::iterator iter = in->begin();
struct rgw_cls_list_op op;
try {
::decode(op, iter);
} catch (buffer::error& err) {
CLS_LOG("ERROR: rgw_bucket_list(): failed to decode request\n");
return -EINVAL;
}
struct rgw_cls_list_ret ret;
struct rgw_bucket_dir& new_dir = ret.dir;
new_dir.header = dir.header;
std::map<string, struct rgw_bucket_dir_entry>& m = new_dir.m;
std::map<string, struct rgw_bucket_dir_entry>::iterator miter = dir.m.upper_bound(op.start_obj);
uint32_t i;
for (i = 0; i != op.num_entries && miter != dir.m.end(); ++i, ++miter) {
m[miter->first] = miter->second;
}
ret.is_truncated = (miter != dir.m.end());
::encode(ret, *out);
return 0;
}
int rgw_bucket_init_index(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
bufferlist bl;
bufferlist::iterator iter;
uint64_t size;
int rc = cls_cxx_stat(hctx, &size, NULL);
if (rc < 0)
return rc;
if (size != 0) {
CLS_LOG("ERROR: index already initialized\n");
return -EINVAL;
}
rgw_bucket_dir dir;
bufferlist map_bl;
bufferlist header_bl;
::encode(dir.header, header_bl);
::encode(header_bl, map_bl);
__u32 num_keys = 0;
::encode(num_keys, map_bl);
rc = cls_cxx_map_write_full(hctx, &map_bl);
return rc;
}
int rgw_bucket_prepare_op(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
// decode request
rgw_cls_obj_prepare_op op;
bufferlist::iterator iter = in->begin();
try {
::decode(op, iter);
} catch (buffer::error& err) {
CLS_LOG("ERROR: rgw_bucket_prepare_op(): failed to decode request\n");
return -EINVAL;
}
if (op.tag.empty()) {
CLS_LOG("ERROR: tag is empty\n");
return -EINVAL;
}
CLS_LOG("rgw_bucket_prepare_op(): request: op=%d name=%s tag=%s\n", op.op, op.name.c_str(), op.tag.c_str());
// get on-disk state
bufferlist cur_value;
int rc = cls_cxx_map_read_key(hctx, op.name, &cur_value);
if (rc < 0 && rc != -ENOENT)
return rc;
struct rgw_bucket_dir_entry entry;
if (rc != -ENOENT) {
bufferlist::iterator biter = cur_value.begin();
::decode(entry, biter);
} else { // no entry, initialize fields
entry.name = op.name;
entry.epoch = 0;
entry.exists = false;
entry.locator = op.locator;
}
// fill in proper state
struct rgw_bucket_pending_info& info = entry.pending_map[op.tag];
info.timestamp = ceph_clock_now(g_ceph_context);
info.state = CLS_RGW_STATE_PENDING_MODIFY;
info.op = op.op;
// write out new key to disk
bufferlist info_bl;
::encode(entry, info_bl);
cls_cxx_map_write_key(hctx, op.name, &info_bl);
return rc;
}
int rgw_bucket_complete_op(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
// decode request
rgw_cls_obj_complete_op op;
bufferlist::iterator iter = in->begin();
try {
::decode(op, iter);
} catch (buffer::error& err) {
CLS_LOG("ERROR: rgw_bucket_complete_op(): failed to decode request\n");
return -EINVAL;
}
CLS_LOG("rgw_bucket_complete_op(): request: op=%d name=%s epoch=%lld tag=%s\n", op.op, op.name.c_str(), op.epoch, op.tag.c_str());
bufferlist header_bl;
struct rgw_bucket_dir_header header;
int rc = cls_cxx_map_read_header(hctx, &header_bl);
if (rc < 0)
return rc;
bufferlist::iterator header_iter = header_bl.begin();
::decode(header, header_iter);
bufferlist current_entry;
struct rgw_bucket_dir_entry entry;
bool ondisk = true;
rc = cls_cxx_map_read_key(hctx, op.name, ¤t_entry);
if (rc < 0) {
if (rc != -ENOENT) {
return rc;
} else {
entry.name = op.name;
entry.epoch = op.epoch;
entry.meta = op.meta;
entry.locator = op.locator;
ondisk = false;
}
} else {
bufferlist::iterator cur_iter = current_entry.begin();
::decode(entry, cur_iter);
CLS_LOG("rgw_bucket_complete_op(): existing entry: epoch=%lld\n", entry.epoch);
}
if (op.tag.size()) {
map<string, struct rgw_bucket_pending_info>::iterator pinter = entry.pending_map.find(op.tag);
if (pinter == entry.pending_map.end()) {
CLS_LOG("ERROR: couldn't find tag for pending operation\n");
return -EINVAL;
}
entry.pending_map.erase(pinter);
}
if (op.epoch <= entry.epoch) {
CLS_LOG("rgw_bucket_complete_op(): skipping request, old epoch\n");
return 0;
}
if (entry.exists) {
struct rgw_bucket_category_stats& stats = header.stats[entry.meta.category];
stats.num_entries--;
stats.total_size -= entry.meta.size;
stats.total_size_rounded -= get_rounded_size(entry.meta.size);
}
bufferlist op_bl;
switch (op.op) {
case CLS_RGW_OP_DEL:
if (ondisk) {
if (!entry.pending_map.size()) {
op_bl.append(CEPH_OSD_TMAP_RM);
::encode(op.name, op_bl);
} else
entry.exists = false;
} else {
return -ENOENT;
}
break;
case CLS_RGW_OP_ADD:
{
struct rgw_bucket_dir_entry_meta& meta = op.meta;
struct rgw_bucket_category_stats& stats = header.stats[meta.category];
entry.meta = meta;
entry.name = op.name;
entry.epoch = op.epoch;
entry.exists = true;
stats.num_entries++;
stats.total_size += meta.size;
stats.total_size_rounded += get_rounded_size(meta.size);
bufferlist new_key_bl;
::encode(entry, new_key_bl);
op_bl.append(CEPH_OSD_TMAP_SET);
::encode(op.name, op_bl);
::encode(new_key_bl, op_bl);
}
break;
}
bufferlist update_bl;
bufferlist new_header_bl;
::encode(header, new_header_bl);
update_bl.append(CEPH_OSD_TMAP_HDR);
::encode(new_header_bl, update_bl);
update_bl.claim_append(op_bl);
return cls_cxx_map_update(hctx, &update_bl);
}
int rgw_dir_suggest_changes(cls_method_context_t hctx, bufferlist *in, bufferlist *out)
{
CLS_LOG("rgw_dir_suggest_changes()");
bufferlist header_bl;
struct rgw_bucket_dir_header header;
bool header_changed = false;
int rc = cls_cxx_map_read_header(hctx, &header_bl);
if (rc < 0)
return rc;
bufferlist::iterator header_iter = header_bl.begin();
::decode(header, header_iter);
bufferlist::iterator in_iter = in->begin();
__u8 op;
rgw_bucket_dir_entry cur_change;
rgw_bucket_dir_entry cur_disk;
bufferlist cur_disk_bl;
bufferlist op_bl;
while (!in_iter.end()) {
try {
::decode(op, in_iter);
::decode(cur_change, in_iter);
} catch (buffer::error& err) {
CLS_LOG("ERROR: rgw_dir_suggest_changes(): failed to decode request\n");
return -EINVAL;
}
cls_cxx_map_read_key(hctx, cur_change.name, &cur_disk_bl);
bufferlist::iterator cur_disk_iter = cur_disk_bl.begin();
::decode(cur_disk, cur_disk_iter);
utime_t cur_time = ceph_clock_now(g_ceph_context);
map<string, struct rgw_bucket_pending_info>::iterator iter =
cur_disk.pending_map.begin();
while(iter != cur_disk.pending_map.end()) {
map<string, struct rgw_bucket_pending_info>::iterator cur_iter=iter++;
if (cur_time > (cur_iter->second.timestamp + CEPH_RGW_TAG_TIMEOUT)) {
cur_disk.pending_map.erase(cur_iter);
}
}
if (cur_disk.pending_map.empty()) {
struct rgw_bucket_category_stats& stats =
header.stats[cur_disk.meta.category];
if (cur_disk.exists) {
stats.num_entries--;
stats.total_size -= cur_disk.meta.size;
stats.total_size_rounded -= get_rounded_size(cur_disk.meta.size);
header_changed = true;
}
switch(op) {
case CEPH_RGW_REMOVE:
op_bl.append(CEPH_OSD_TMAP_RM);
::encode(cur_change.name, op_bl);
break;
case CEPH_RGW_UPDATE:
stats.num_entries++;
stats.total_size += cur_change.meta.size;
stats.total_size_rounded += get_rounded_size(cur_change.meta.size);
bufferlist cur_state_bl;
::encode(cur_change, cur_state_bl);
op_bl.append(CEPH_OSD_TMAP_SET);
::encode(cur_state_bl, op_bl);
break;
}
}
}
bufferlist update_bl;
if (header_changed) {
bufferlist new_header_bl;
::encode(header, new_header_bl);
update_bl.append(CEPH_OSD_TMAP_HDR);
::encode(new_header_bl, update_bl);
}
update_bl.claim_append(op_bl);
return cls_cxx_map_update(hctx, &update_bl);
}
void __cls_init()
{
CLS_LOG("Loaded rgw class!");
cls_register("rgw", &h_class);
cls_register_cxx_method(h_class, "bucket_init_index", CLS_METHOD_RD | CLS_METHOD_WR | CLS_METHOD_PUBLIC, rgw_bucket_init_index, &h_rgw_bucket_init_index);
cls_register_cxx_method(h_class, "bucket_list", CLS_METHOD_RD | CLS_METHOD_PUBLIC, rgw_bucket_list, &h_rgw_bucket_list);
cls_register_cxx_method(h_class, "bucket_prepare_op", CLS_METHOD_RD | CLS_METHOD_WR | CLS_METHOD_PUBLIC, rgw_bucket_prepare_op, &h_rgw_bucket_prepare_op);
cls_register_cxx_method(h_class, "bucket_complete_op", CLS_METHOD_RD | CLS_METHOD_WR | CLS_METHOD_PUBLIC, rgw_bucket_complete_op, &h_rgw_bucket_complete_op);
cls_register_cxx_method(h_class, "dir_suggest_changes", CLS_METHOD_RD | CLS_METHOD_WR | CLS_METHOD_PUBLIC, rgw_dir_suggest_changes, &h_rgw_dir_suggest_changes);
return;
}