-
Notifications
You must be signed in to change notification settings - Fork 7
/
lua_rados.cc
705 lines (588 loc) · 16.1 KB
/
lua_rados.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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/**
@module lua-rados
*/
#include <errno.h>
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
#include <string>
#include <map>
#include <rados/librados.hpp>
using std::string;
using std::map;
using librados::bufferlist;
using librados::Rados;
using librados::IoCtx;
#define LRAD_TRADOS_T "Rados.RadosT"
#define LRAD_TIOCTX_T "Rados.IoctxT"
#define LRAD_BL_T "Rados.Bufferlist"
#define LRAD_SBM_T "Rados.StrBLMap"
static char reg_key_rados_refs;
typedef enum {
CREATED,
CONNECTED,
SHUTDOWN
} cluster_state_t;
struct lrad_cluster {
Rados rados;
cluster_state_t state;
};
/*
* Get cluster object at stack index.
*/
static inline struct lrad_cluster *__lrad_checkcluster(lua_State *L, int pos)
{
struct lrad_cluster *cluster = (struct lrad_cluster *)luaL_checkudata(L, pos, LRAD_TRADOS_T);
return cluster;
}
/*
* Get non-shutdown cluster object at stack index. Error if had shutdown.
*/
static inline struct lrad_cluster *lrad_checkcluster(lua_State *L, int pos)
{
struct lrad_cluster *cluster = __lrad_checkcluster(L, pos);
if (cluster->state == SHUTDOWN)
luaL_argerror(L, pos, "cannot reuse shutdown cluster handle");
return cluster;
}
/*
* Get connected cluster object at stack index. Error if not connected.
*/
static inline struct lrad_cluster *lrad_checkcluster_conn(lua_State *L, int pos)
{
struct lrad_cluster *cluster = lrad_checkcluster(L, pos);
if (cluster->state != CONNECTED)
luaL_argerror(L, pos, "not connected to cluster");
return cluster;
}
/*
* Get Ioctx object.
*/
static inline IoCtx *lrad_checkioctx(lua_State *L, int pos)
{
return (IoCtx *)luaL_checkudata(L, pos, LRAD_TIOCTX_T);
}
/*
* Push nil-error protocol values
*/
static int lrad_pusherror(lua_State *L, int ret)
{
lua_pushnil(L);
lua_pushfstring(L, "%s", strerror(ret));
lua_pushinteger(L, ret);
return 3;
}
/*
* Push return value or error if not ok
*/
static int lrad_pushresult(lua_State *L, int ok, int ret)
{
if (!ok)
return lrad_pusherror(L, ret);
lua_pushinteger(L, ret);
return 1;
}
/*
* Garbage collect a bufferlist in the heap
*/
static int lrad_bl_gc(lua_State *L)
{
bufferlist **pbl = (bufferlist **)luaL_checkudata(L, 1, LRAD_BL_T);
if (pbl && *pbl)
delete *pbl;
return 0;
}
/*
* Allocate a new bufferlist in the heap
*/
static bufferlist *lrad_newbufferlist(lua_State *L)
{
bufferlist *bl = new bufferlist();
bufferlist **pbl = (bufferlist **)lua_newuserdata(L, sizeof(*pbl));
*pbl = bl;
luaL_getmetatable(L, LRAD_BL_T);
lua_setmetatable(L, -2);
return bl;
}
/*
* Garbage collect a string buffer list map in the heap
*/
static int lrad_sbm_gc(lua_State *L)
{
map<string, bufferlist> **sbmp =
(map<string, bufferlist>**)luaL_checkudata(L, 1, LRAD_SBM_T);
if (sbmp && *sbmp)
delete *sbmp;
return 0;
}
/*
* Allocate a new string buffer list map in the heap
*/
static map<string, bufferlist> *lrad_newsbm(lua_State *L)
{
map<string, bufferlist> *pairs = new map<string, bufferlist>;
map<string, bufferlist> **sbmp =
(map<string, bufferlist>**)lua_newuserdata(L, sizeof(*sbmp));
*sbmp = pairs;
luaL_getmetatable(L, LRAD_SBM_T);
lua_setmetatable(L, -2);
return pairs;
}
/**
Get the version of librados.
@function version
@return major version
@return minor version
@return extra version
@usage major, minor, extra = rados.version()
*/
static int lrad_version(lua_State *L)
{
int major, minor, extra;
rados_version(&major, &minor, &extra);
lua_pushinteger(L, major);
lua_pushinteger(L, minor);
lua_pushinteger(L, extra);
return 3;
}
/**
Create handle for communicating with RADOS cluster.
@function create
@string id the user to connect as (optional, or nil)
@return cluster handle object on success, nil otherwise
@return error message and retval if failed
@usage cluster = rados.create()
@usage cluster = rados.create('admin')
*/
static int lrad_create(lua_State *L)
{
const char *id = NULL;
struct lrad_cluster *cluster;
int ret;
if (lua_gettop(L) > 0 && lua_type(L, 1) != LUA_TNIL) {
luaL_checktype(L, 1, LUA_TSTRING);
id = lua_tostring(L, 1);
}
cluster = (struct lrad_cluster *)lua_newuserdata(L, sizeof(*cluster));
cluster->state = CREATED;
luaL_getmetatable(L, LRAD_TRADOS_T);
lua_setmetatable(L, -2);
ret = cluster->rados.init(id);
if (ret)
return lrad_pusherror(L, ret);
/* return the userdata */
return 1;
}
/**
@type Cluster
*/
/**
Configure the cluster handle using a Ceph config file.
@function conf_read_file
@string file path to configuration file (optional, or nil)
@return 0 on success, nil otherwise
@return error message and retval if failed
@usage cluster:conf_read_file()
@usage cluster:conf_read_file('/path/to/ceph.conf')
*/
static int lrad_conf_read_file(lua_State *L)
{
struct lrad_cluster *cluster = lrad_checkcluster(L, 1);
const char *conf_file = NULL;
int ret;
if (lua_gettop(L) > 1 && lua_type(L, 2) != LUA_TNIL) {
luaL_checktype(L, 2, LUA_TSTRING);
conf_file = lua_tostring(L, 2);
}
ret = cluster->rados.conf_read_file(conf_file);
return lrad_pushresult(L, (ret == 0), ret);
}
/**
Connect to the cluster.
@function connect
@return 0 on success, nil otherwise
@return error message and retval if failed
@usage cluster:connect()
@usage status, errstr, ret = cluster:connect()
*/
static int lrad_connect(lua_State *L)
{
struct lrad_cluster *cluster = lrad_checkcluster(L, 1);
int ret;
if (cluster->state == CONNECTED)
luaL_argerror(L, 1, "already connected to cluster");
ret = cluster->rados.connect();
if (!ret)
cluster->state = CONNECTED;
return lrad_pushresult(L, (ret == 0), ret);
}
/**
Disconnect from the cluster.
@function shutdown
@usage cluster:shutdown()
*/
static int lrad_shutdown(lua_State *L)
{
struct lrad_cluster *cluster = lrad_checkcluster_conn(L, 1);
cluster->rados.shutdown();
cluster->state = SHUTDOWN;
return 0;
}
/**
Create an I/O context.
@function open_ioctx
@string name of pool
@return I/O context object or nil on failure
@return errstr, ret if failed
@usage ioctx = cluster:open_ioctx('my_pool')
*/
static int lrad_open_ioctx(lua_State *L)
{
struct lrad_cluster *cluster = lrad_checkcluster_conn(L, 1);
const char *pool_name = luaL_checkstring(L, 2);
IoCtx *ioctx;
int ret;
ioctx = (IoCtx *)lua_newuserdata(L, sizeof(*ioctx));
luaL_getmetatable(L, LRAD_TIOCTX_T);
lua_setmetatable(L, -2);
ret = cluster->rados.ioctx_create(pool_name, *ioctx);
if (ret)
return lrad_pusherror(L, ret);
/* record IoCtx -> Rados reference in weak key table */
lua_pushlightuserdata(L, ®_key_rados_refs);
lua_gettable(L, LUA_REGISTRYINDEX);
assert(!lua_isnil(L, -1));
assert(lua_type(L, -1) == LUA_TTABLE);
lua_pushvalue(L, -2); /* key = ioctx */
lua_pushvalue(L, 1); /* value = cluster */
lua_settable(L, -3);
lua_pop(L, 1);
/* return the userdata */
return 1;
}
/*
* Garbage collect the cluster object, and shutdown only if connected.
*/
static int lrad_cluster_gc(lua_State *L)
{
struct lrad_cluster *cluster = __lrad_checkcluster(L, 1);
if (cluster->state == CONNECTED)
cluster->rados.shutdown();
cluster->state = SHUTDOWN;
return 0;
}
/**
@type Ioctx
*/
/**
Close the I/O context.
@function close
@usage ioctx:close()
*/
static int lrad_ioctx_close(lua_State *L)
{
IoCtx *ioctx = lrad_checkioctx(L, 1);
ioctx->close();
return 0;
}
/**
Get object stat info (size/mtime)
@function stat
@string oid object name
@return len, mtime, or nil on failure
@return errstr and retval if failed
@usage size, mtime = ioctx:stat('obj3')
*/
static int lrad_ioctx_stat(lua_State *L)
{
IoCtx *ioctx = lrad_checkioctx(L, 1);
const char *oid = luaL_checkstring(L, 2);
uint64_t len;
time_t mtime;
int ret;
ret = ioctx->stat(oid, &len, &mtime);
if (ret)
return lrad_pusherror(L, ret);
lua_pushinteger(L, len);
lua_pushinteger(L, mtime);
return 2;
}
/**
Write data to an object.
@function write
@string oid object name
@string buf buffer containing bytes
@int length number of bytes to write
@int offset offset in object from which to write
@return 0 on success, or nil on error
@return errstr and retval if failed
@usage ioctx:write('obj3', 'data', #'data', 0)
*/
static int lrad_ioctx_write(lua_State *L)
{
IoCtx *ioctx = lrad_checkioctx(L, 1);
const char *oid = luaL_checkstring(L, 2);
const char *buf = luaL_checkstring(L, 3);
size_t len = luaL_checkint(L, 4);
uint64_t off = luaL_checkint(L, 5);
bufferlist *bl = lrad_newbufferlist(L);
int ret;
bl->append(buf, len);
ret = ioctx->write(oid, *bl, len, off);
bl->clear();
return lrad_pushresult(L, (ret == 0), ret);
}
/**
Read data from an object.
@function read
@string oid object name
@int length number of bytes read
@int offset offset in object from which to read
@return string with at most `length` bytes, or nil on error
@return errstr and retval if failed
@usage data = ioctx:read('obj3', 1000, 0)
*/
static int lrad_ioctx_read(lua_State *L)
{
IoCtx *ioctx = lrad_checkioctx(L, 1);
const char *oid = luaL_checkstring(L, 2);
size_t len = luaL_checkint(L, 3);
uint64_t off = luaL_checkint(L, 4);
bufferlist *bl = lrad_newbufferlist(L);
int ret;
ret = ioctx->read(oid, *bl, len, off);
if (ret < 0)
return lrad_pusherror(L, ret);
if (bl->length() > len)
return lrad_pusherror(L, -ERANGE);
lua_pushlstring(L, bl->c_str(), bl->length());
bl->clear();
return 1;
}
/**
Set xattr name on object
@function setxattr
@string oid object name
@string name xattr name
@string buf value to set xattr name to
@int size of buf
@return 0 on success, or nil on error
@return errstr and retval if failed
@usage ioctx:setxattr('obj3', 'name', 'data', #'data')
*/
static int lrad_ioctx_setxattr(lua_State *L)
{
IoCtx *ioctx = lrad_checkioctx(L, 1);
const char *oid = luaL_checkstring(L, 2);
const char *name = luaL_checkstring(L, 3);
size_t buflen, len = luaL_checkint(L, 5);
const char* buf = lua_tolstring(L, 4, &buflen);
bufferlist *bl = lrad_newbufferlist(L);
int ret;
if( !buf ){
return luaL_argerror(L, 4, "expected string");
}
luaL_argcheck(L, len >= 0, 5, "invalid length");
luaL_argcheck(L, buflen >= len, 5, "length longer than buffer");
bl->append(buf, len);
ret = ioctx->setxattr(oid, name, *bl);
bl->clear();
return lrad_pushresult(L, (ret >= 0), ret);
}
/**
Get xattr name from object.
@function getxattr
@string oid object name
@string name xattr name
@return string containing xattr name
@return errstr and retval if failed
@usage data = ioctx:getxattr('obj3', 'name')
*/
static int lrad_ioctx_getxattr(lua_State *L)
{
IoCtx *ioctx = lrad_checkioctx(L, 1);
const char *oid = luaL_checkstring(L, 2);
const char *name = luaL_checkstring(L, 3);
bufferlist *bl = lrad_newbufferlist(L);
int ret;
ret = ioctx->getxattr(oid, name, *bl );
if (ret < 0)
return lrad_pusherror(L, ret);
lua_pushlstring(L, bl->c_str(), bl->length());
bl->clear();
return 1;
}
/**
Set omap values for oid
@function omapset
@string oid object name
@table pairs [key,value] pairs to be set
@return 0 on success, or nil on error
@return errstr and retval if failed
@usage ioctx:omapset('obj3', 'pairs')
*/
static int lrad_ioctx_omapset(lua_State *L)
{
IoCtx *ioctx = lrad_checkioctx(L, 1);
const char *oid = luaL_checkstring(L, 2);
map<string, bufferlist> *pairs;
int ret;
if (lua_type(L, 3) != LUA_TTABLE)
return luaL_error(L, "omapset expects a table for the second argument");
pairs = lrad_newsbm(L);
lua_pushnil(L);
while (lua_next(L, 3) != 0){
size_t valLength;
const char *keyTemp = luaL_checkstring(L, -2);
const char *valTemp = luaL_checklstring(L, -1, &valLength);
bufferlist bl;
bl.append(valTemp, valLength);
(*pairs)[keyTemp] = bl;
lua_pop(L, 1);
}
ret = ioctx->omap_set(oid, *pairs);
return lrad_pushresult(L, (ret >= 0), ret);
}
/**
Get omap values for oid
@function omapget
@string oid object name
@string after list no keys smaller than after
@int maxret maximum number of key/val pairs to retrieve
@return map, numpairs or nil on failure
@return errstr and retval if failed
@usage map, numpairs = ioctx:omapget('obj3', 'after', 'maxret')
*/
static int lrad_ioctx_omapget(lua_State *L)
{
IoCtx *ioctx = lrad_checkioctx(L, 1);
const char *oid = luaL_checkstring(L, 2);
const char *after = luaL_checkstring(L, 3);
size_t maxret = luaL_checkint(L, 4);
map<string, bufferlist> *pairs = lrad_newsbm(L);
int ret;
ret = ioctx->omap_get_vals(oid, after, maxret, pairs);
if (ret < 0)
return lrad_pusherror(L, ret);
std::map<std::string, bufferlist>::iterator iter;
lua_newtable(L);
size_t pairsProcessed = 0;
for(iter = pairs->begin(); iter != pairs->end(); iter++){
const std::string *key = &(iter->first);
bufferlist *bl = &(iter->second);
lua_pushlstring(L, key->c_str(), key->length());
lua_pushlstring(L, bl->c_str(), bl->length());
lua_settable(L,-3);
pairsProcessed++;
}
lua_pushinteger(L, pairsProcessed);
return 2;
}
/**
Execute an OSD class method on an object.
@function exec
@string oid the name of the object
@string class the name of the class
@string method the name of the method
@string buf buffer containing method input
@int length length of input buffer
@return retval, output on success, else nil
@return strerr, errval on failure
@usage ret, reply = ioctx:exec('oid', 'cls', 'func', input, #input)
*/
static int lrad_ioctx_exec(lua_State *L)
{
IoCtx *ioctx = lrad_checkioctx(L, 1);
const char *oid = luaL_checkstring(L, 2);
const char *cls = luaL_checkstring(L, 3);
const char *method = luaL_checkstring(L, 4);
size_t buflen, len = luaL_checkint(L, 6);
const char *buf = lua_tolstring(L, 5, &buflen);
bufferlist *inbl = lrad_newbufferlist(L);
bufferlist *outbl = lrad_newbufferlist(L);
int ret;
if (!buf) {
if (lua_type(L, 5) != LUA_TNIL)
return luaL_argerror(L, 5, "expected string or nil");
buflen = 0;
}
luaL_argcheck(L, len >= 0, 6, "invalid length");
luaL_argcheck(L, buflen >= len, 6, "length longer than buffer");
if (buf)
inbl->append(buf, len);
ret = ioctx->exec(oid, cls, method, *inbl, *outbl);
if (ret < 0) {
inbl->clear();
outbl->clear();
return lrad_pusherror(L, ret);
}
lua_pushinteger(L, ret);
lua_pushlstring(L, outbl->c_str(), outbl->length());
inbl->clear();
outbl->clear();
return 2;
}
static const luaL_Reg clusterlib_m[] = {
{"conf_read_file", lrad_conf_read_file},
{"connect", lrad_connect},
{"shutdown", lrad_shutdown},
{"open_ioctx", lrad_open_ioctx},
{"__gc", lrad_cluster_gc},
{NULL, NULL}
};
static const luaL_Reg ioctxlib_m[] = {
{"close", lrad_ioctx_close},
{"write", lrad_ioctx_write},
{"read", lrad_ioctx_read},
{"stat", lrad_ioctx_stat},
{"setxattr", lrad_ioctx_setxattr},
{"getxattr", lrad_ioctx_getxattr},
{"omapset", lrad_ioctx_omapset},
{"omapget", lrad_ioctx_omapget},
{"exec", lrad_ioctx_exec},
{NULL, NULL}
};
static const luaL_Reg radoslib_f[] = {
{"version", lrad_version},
{"create", lrad_create},
{NULL, NULL}
};
extern "C" {
LUALIB_API int luaopen_rados(lua_State *L)
{
/* setup rados_t userdata type */
luaL_newmetatable(L, LRAD_TRADOS_T);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_register(L, NULL, clusterlib_m);
lua_pop(L, 1);
/* setup rados_ioctx_t userdata type */
luaL_newmetatable(L, LRAD_TIOCTX_T);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_register(L, NULL, ioctxlib_m);
lua_pop(L, 1);
/* setup bufferlist userdata type */
luaL_newmetatable(L, LRAD_BL_T);
lua_pushcfunction(L, lrad_bl_gc);
lua_setfield(L, -2, "__gc");
lua_pop(L, 1);
/* setup string bufferlist map userdata type */
luaL_newmetatable(L, LRAD_SBM_T);
lua_pushcfunction(L, lrad_sbm_gc);
lua_setfield(L, -2, "__gc");
lua_pop(L, 1);
/* weak table to protect IoCtx -> Rados refs */
lua_pushlightuserdata(L, ®_key_rados_refs);
lua_newtable(L);
lua_pushstring(L, "k");
lua_setfield(L, -2, "__mode");
lua_pushvalue(L, -1);
lua_setmetatable(L, -2);
lua_settable(L, LUA_REGISTRYINDEX);
luaL_register(L, "rados", radoslib_f);
return 0;
}
}