forked from DiamondLightSource/DosNa
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclovis_functions.c
418 lines (329 loc) · 9.39 KB
/
clovis_functions.c
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/time.h>
#include <assert.h>
#include <string.h>
#include "clovis/clovis.h"
#include "clovis/clovis_idx.h"
#include "clovis_functions.h"
static struct m0_clovis *clovis_instance = NULL;
static struct m0_clovis_container clovis_container;
static struct m0_clovis_realm clovis_uber_realm;
static struct m0_clovis_config clovis_conf;
static struct m0_idx_dix_config dix_conf;
static size_t clovis_block_size = 4096;
static unsigned int tier_selection = 0;
extern struct m0_addb_ctx m0_clovis_addb_ctx;
static struct m0_fid TIER2 = (struct m0_fid) { // Tier 2 is SSD Pool
.f_container = 0x6f00000000000001,
.f_key = 0x3f8
};
static struct m0_fid TIER3 = (struct m0_fid) { // Tier 3 is HDD Pool
.f_container = 0x6f00000000000001,
.f_key = 0x426
};
static struct m0_fid *const TIERS[] = {
NULL, // Tier 0 and 1 are
NULL, // the same as the default one
&TIER2,
&TIER3
};
static const size_t N_TIER = sizeof(TIERS) / sizeof(TIERS[0]);
int
init_clovis(char *laddr, char *ha_addr, char *prof_id, char *proc_fid,
size_t block_size, unsigned int tier)
{
int rc;
dix_conf = (struct m0_idx_dix_config) {
.kc_create_meta = false
};
clovis_conf = (struct m0_clovis_config) {
.cc_local_addr = laddr,
.cc_ha_addr = ha_addr,
.cc_profile = prof_id,
.cc_process_fid = proc_fid,
.cc_is_oostore = true,
.cc_is_read_verify = false,
.cc_max_rpc_msg_size = M0_RPC_DEF_MAX_RPC_MSG_SIZE,
.cc_tm_recv_queue_min_len = M0_NET_TM_RECV_QUEUE_DEF_LEN,
.cc_idx_service_id = M0_CLOVIS_IDX_DIX,
.cc_idx_service_conf = &dix_conf
};
clovis_block_size = block_size;
if (tier >= N_TIER) {
return EPERM;
}
tier_selection = tier;
rc = m0_clovis_init(&clovis_instance, &clovis_conf, true);
if (rc)
return rc;
m0_clovis_container_init(&clovis_container, NULL, &M0_CLOVIS_UBER_REALM,
clovis_instance);
rc = clovis_container.co_realm.re_entity.en_sm.sm_rc;
if (rc != 0)
goto init_error1;
clovis_uber_realm = clovis_container.co_realm;
return 0;
init_error1:
m0_clovis_fini(clovis_instance, true);
return rc;
}
void
fini_clovis()
{
m0_clovis_fini(clovis_instance, true);
}
static int
open_entity(struct m0_clovis_entity *entity)
{
int rc = 0;
struct m0_clovis_op *ops[1] = { NULL };
m0_clovis_entity_open(entity, &ops[0]);
m0_clovis_op_launch(ops, 1);
m0_clovis_op_wait(ops[0],
M0_BITS(M0_CLOVIS_OS_FAILED, M0_CLOVIS_OS_STABLE),
M0_TIME_NEVER);
// this return code is not the state machine return code
rc = m0_clovis_rc(ops[0]);
m0_clovis_op_fini(ops[0]);
m0_clovis_op_free(ops[0]);
return rc;
}
static int
delete_entity(struct m0_clovis_entity *entity)
{
int rc = 0;
struct m0_clovis_op *ops[1] = { NULL };
m0_clovis_entity_delete(entity, &ops[0]);
m0_clovis_op_launch(ops, 1);
rc = m0_clovis_op_wait(ops[0], M0_BITS(M0_CLOVIS_OS_FAILED,
M0_CLOVIS_OS_STABLE), M0_TIME_NEVER);
m0_clovis_op_fini(ops[0]);
m0_clovis_op_free(ops[0]);
return rc;
}
static int
write_data_to_object(struct m0_uint128 id, struct m0_indexvec *ext,
struct m0_bufvec *data, struct m0_bufvec *attr)
{
int rc = 0;
struct m0_clovis_obj obj;
struct m0_clovis_op *ops[1] = { NULL };
memset(&obj, 0, sizeof(struct m0_clovis_obj));
m0_clovis_obj_init(&obj, &clovis_uber_realm, &id,
m0_clovis_layout_id(clovis_instance));
open_entity(&obj.ob_entity);
m0_clovis_obj_op(&obj, M0_CLOVIS_OC_WRITE, ext, data, attr, 0, &ops[0]);
m0_clovis_op_launch(ops, 1);
rc = m0_clovis_op_wait(ops[0], M0_BITS(M0_CLOVIS_OS_FAILED,
M0_CLOVIS_OS_STABLE), M0_TIME_NEVER);
if (ops[0]->op_sm.sm_state != M0_CLOVIS_OS_STABLE
|| ops[0]->op_sm.sm_rc != 0)
rc = EPERM;
m0_clovis_op_fini(ops[0]);
m0_clovis_op_free(ops[0]);
m0_clovis_entity_fini(&obj.ob_entity);
return rc;
}
static int
read_data_from_object(struct m0_uint128 id, struct m0_indexvec *ext,
struct m0_bufvec *data, struct m0_bufvec *attr)
{
int rc = 0;
struct m0_clovis_obj obj;
struct m0_clovis_op *ops[1] = { NULL };
memset(&obj, 0, sizeof(struct m0_clovis_obj));
m0_clovis_obj_init(&obj, &clovis_uber_realm, &id,
m0_clovis_layout_id(clovis_instance));
rc = open_entity(&obj.ob_entity);
if (rc < 0) // object not found
return rc;
m0_clovis_obj_op(&obj, M0_CLOVIS_OC_READ, ext, data, attr, 0, &ops[0]);
if (ops[0] == NULL || ops[0]->op_sm.sm_rc != 0) {
rc = EPERM;
goto read_exit1;
}
m0_clovis_op_launch(ops, 1);
rc = m0_clovis_op_wait(ops[0],
M0_BITS(M0_CLOVIS_OS_FAILED, M0_CLOVIS_OS_STABLE),
M0_TIME_NEVER);
if (ops[0]->op_sm.sm_state != M0_CLOVIS_OS_STABLE
|| ops[0]->op_sm.sm_rc != 0)
rc = -EPERM;
m0_clovis_op_fini(ops[0]);
m0_clovis_op_free(ops[0]);
read_exit1:
m0_clovis_entity_fini(&obj.ob_entity);
return rc;
}
int
create_object(uint64_t high_id, uint64_t low_id)
{
int rc = 0;
struct m0_clovis_obj obj;
struct m0_clovis_op *ops[1] = { NULL };
struct m0_uint128 id = {
.u_hi = high_id,
.u_lo = low_id
};
memset(&obj, 0, sizeof(struct m0_clovis_obj));
m0_clovis_obj_init(&obj, &clovis_uber_realm, &id,
m0_clovis_layout_id(clovis_instance));
rc = open_entity(&obj.ob_entity);
if (rc >= 0) // object already exists
return 1;
m0_clovis_entity_create(TIERS[tier_selection], &obj.ob_entity, &ops[0]);
m0_clovis_op_launch(ops, ARRAY_SIZE(ops));
rc = m0_clovis_op_wait(ops[0],
M0_BITS(M0_CLOVIS_OS_FAILED, M0_CLOVIS_OS_STABLE),
M0_TIME_NEVER);
m0_clovis_op_fini(ops[0]);
m0_clovis_op_free(ops[0]);
m0_clovis_entity_fini(&obj.ob_entity);
return rc;
}
int
delete_object(uint64_t high_id, uint64_t low_id)
{
int rc = 0;
struct m0_clovis_obj obj;
struct m0_uint128 id = {
.u_hi = high_id,
.u_lo = low_id
};
memset(&obj, 0, sizeof(struct m0_clovis_obj));
m0_clovis_obj_init(&obj, &clovis_uber_realm, &id,
m0_clovis_layout_id(clovis_instance));
rc = open_entity(&obj.ob_entity);
if (rc < 0) // object not found
return rc;
rc = delete_entity(&obj.ob_entity);
m0_clovis_entity_fini(&obj.ob_entity);
return rc;
}
/*
* read the data contained in the object with the id specified and
* write it to buffer.
* it returns 0 if the operation was correct
*/
int
read_object(uint64_t high_id, uint64_t low_id, char *buffer, size_t length)
{
int rc = 0;
int n_full_blocks = length / clovis_block_size;
int n_blocks = n_full_blocks + (length % clovis_block_size != 0);
struct m0_indexvec ext;
struct m0_bufvec data;
struct m0_bufvec attr;
int i;
struct m0_uint128 id = {
.u_hi = high_id,
.u_lo = low_id
};
rc = m0_indexvec_alloc(&ext, n_blocks);
if (rc)
return rc;
rc = m0_bufvec_alloc(&data, n_blocks, clovis_block_size);
if (rc)
goto read_exit2;
rc = m0_bufvec_alloc(&attr, n_blocks, 1);
if (rc)
goto read_exit1;
int byte_count = 0;
for (i = 0; i < n_blocks; i++) {
ext.iv_index[i] = byte_count;
ext.iv_vec.v_count[i] = clovis_block_size;
byte_count += clovis_block_size;
attr.ov_vec.v_count[i] = 0;
}
rc = read_data_from_object(id, &ext, &data, &attr);
if (rc)
goto read_exit;
for (i = 0; i < n_full_blocks; i++) {
memcpy(buffer + ext.iv_index[i],
data.ov_buf[i],
clovis_block_size);
}
if (n_blocks > n_full_blocks) {
memcpy(buffer + n_full_blocks * clovis_block_size,
data.ov_buf[n_full_blocks],
length % clovis_block_size);
}
read_exit:
m0_bufvec_free(&attr);
read_exit1:
m0_bufvec_free(&data);
read_exit2:
m0_indexvec_free(&ext);
return rc;
}
/*
* write data from buffer to object specified by the id.
* It returns 0 if the operation was correct.
*/
int
write_object(uint64_t high_id, uint64_t low_id, char *buffer, size_t length)
{
int rc = 0;
int n_full_blocks = length / clovis_block_size;
int n_blocks = n_full_blocks + (length % clovis_block_size != 0 ? 1 : 0);
struct m0_indexvec ext;
struct m0_bufvec data;
struct m0_bufvec attr;
int i;
struct m0_uint128 id = {
.u_hi = high_id,
.u_lo = low_id
};
rc = m0_indexvec_alloc(&ext, n_blocks);
if (rc)
return rc;
rc = m0_bufvec_alloc(&data, n_blocks, clovis_block_size);
if (rc)
goto write_exit2;
rc = m0_bufvec_alloc(&attr, n_blocks, 1);
if (rc)
goto write_exit1;
int byte_count = 0;
for (i = 0; i < n_full_blocks; i++) {
ext.iv_index[i] = byte_count;
ext.iv_vec.v_count[i] = clovis_block_size;
byte_count += clovis_block_size;
attr.ov_vec.v_count[i] = 0;
memcpy(data.ov_buf[i],
buffer + i * clovis_block_size,
clovis_block_size);
}
if (n_blocks > n_full_blocks) {
ext.iv_index[n_full_blocks] = byte_count;
ext.iv_vec.v_count[n_full_blocks] = clovis_block_size;
attr.ov_vec.v_count[n_full_blocks] = 0;
memcpy(data.ov_buf[n_full_blocks],
buffer + n_full_blocks * clovis_block_size,
length % clovis_block_size);
}
rc = write_data_to_object(id, &ext, &data, &attr);
m0_bufvec_free(&attr);
write_exit1:
m0_bufvec_free(&data);
write_exit2:
m0_indexvec_free(&ext);
return rc;
}
int
exist_object(uint64_t high_id, uint64_t low_id)
{
int rc;
struct m0_clovis_obj obj;
struct m0_uint128 id = {
.u_hi = high_id,
.u_lo = low_id
};
memset(&obj, 0, sizeof(struct m0_clovis_obj));
m0_clovis_obj_init(&obj, &clovis_uber_realm, &id,
m0_clovis_layout_id(clovis_instance));
rc = open_entity(&obj.ob_entity);
m0_clovis_entity_fini(&obj.ob_entity);
return rc >= 0;
}