nvml-sys 0.0.6

A low-level FFI wrapper around the Persistent Memory Development Kit, PMDK (formerly NVML) and its libraries, including libpmem, libpmemobj and others. Currently tracks master after version 1.3.1.
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
706
707
708
709
710
711
712
713
714
715
716
717
/*
 * Copyright 2016-2017, Intel Corporation
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *
 *     * Neither the name of the copyright holder nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * ctl.c -- implementation of the interface for examination and modification of
 *	the library's internal state
 */
#include "out.h"
#include "obj.h"
#include "ctl.h"

#include "os.h"

#define CTL_MAX_ENTRIES 100

#define MAX_CONFIG_FILE_LEN (1 << 20) /* 1 megabyte */

#define CTL_STRING_QUERY_SEPARATOR ";"
#define CTL_NAME_VALUE_SEPARATOR "="
#define CTL_QUERY_NODE_SEPARATOR "."
#define CTL_VALUE_ARG_SEPARATOR ","

static int ctl_global_first_free = 0;
static struct ctl_node CTL_NODE(global)[CTL_MAX_ENTRIES];

/*
 * This is the top level node of the ctl tree structure. Each node can contain
 * children and leaf nodes.
 *
 * Internal nodes simply create a new path in the tree whereas child nodes are
 * the ones providing the read/write functionality by the means of callbacks.
 *
 * Each tree node must be NULL-terminated, CTL_NODE_END macro is provided for
 * convience.
 */
struct ctl {
	struct ctl_node root[CTL_MAX_ENTRIES];
	int first_free;
};

/*
 * ctl_find_node -- (internal) searches for a matching entry point in the
 *	provided nodes
 *
 * The caller is responsible for freeing all of the allocated indexes,
 * regardless of the return value.
 */
static struct ctl_node *
ctl_find_node(struct ctl_node *nodes, const char *name,
	struct ctl_indexes *indexes)
{
	LOG(3, "nodes %p name %s indexes %p", nodes, name, indexes);

	struct ctl_node *n = NULL;
	char *sptr = NULL;
	char *parse_str = Strdup(name);
	if (parse_str == NULL)
		return NULL;

	char *node_name = strtok_r(parse_str, CTL_QUERY_NODE_SEPARATOR, &sptr);

	/*
	 * Go through the string and separate tokens that correspond to nodes
	 * in the main ctl tree.
	 */
	while (node_name != NULL) {
		char *endptr;
		/*
		 * Ignore errno from strtol: FreeBSD returns EINVAL if no
		 * conversion is performed. Linux does not, but endptr
		 * check is valid in both cases.
		 */
		int tmp_errno = errno;
		long index_value = strtol(node_name, &endptr, 0);
		errno = tmp_errno;
		struct ctl_index *index_entry = NULL;
		if (endptr != node_name) { /* a valid index */
			index_entry = Malloc(sizeof(*index_entry));
			if (index_entry == NULL)
				goto error;
			index_entry->value = index_value;
			SLIST_INSERT_HEAD(indexes, index_entry, entry);
		}

		for (n = &nodes[0]; n->name != NULL; ++n) {
			if (index_entry && n->type == CTL_NODE_INDEXED)
				break;
			else if (strcmp(n->name, node_name) == 0)
				break;
		}
		if (n->name == NULL)
			goto error;

		if (index_entry)
			index_entry->name = n->name;

		nodes = n->children;
		node_name = strtok_r(NULL, CTL_QUERY_NODE_SEPARATOR, &sptr);
	}

	Free(parse_str);
	return n;

error:
	Free(parse_str);
	return NULL;
}

/*
 * ctl_delete_indexes --
 *	(internal) removes and frees all entires on the index list
 */
static void
ctl_delete_indexes(struct ctl_indexes *indexes)
{
	while (!SLIST_EMPTY(indexes)) {
		struct ctl_index *index = SLIST_FIRST(indexes);
		SLIST_REMOVE_HEAD(indexes, entry);
		Free(index);
	}
}

/*
 * ctl_parse_args -- (internal) parses a string argument based on the node
 *	structure
 */
static void *
ctl_parse_args(struct ctl_argument *arg_proto, char *arg)
{
	ASSERTne(arg, NULL);

	char *dest_arg = Malloc(arg_proto->dest_size);
	if (dest_arg == NULL)
		return NULL;

	char *sptr = NULL;
	char *arg_sep = strtok_r(arg, CTL_VALUE_ARG_SEPARATOR, &sptr);
	for (struct ctl_argument_parser *p = arg_proto->parsers;
			p->parser != NULL; ++p) {
		ASSERT(p->dest_offset + p->dest_size <= arg_proto->dest_size);
		if (arg_sep == NULL)
			goto error_parsing;

		if (p->parser(arg_sep, dest_arg + p->dest_offset,
			p->dest_size) != 0)
			goto error_parsing;

		arg_sep = strtok_r(NULL, CTL_VALUE_ARG_SEPARATOR, &sptr);
	}

	return dest_arg;

error_parsing:
	Free(dest_arg);
	return NULL;
}

/*
 * ctl_query_get_real_args -- (internal) returns a pointer with actual argument
 *	structure as required by the node callback
 */
static void *
ctl_query_get_real_args(struct ctl_node *n, void *write_arg,
	enum ctl_query_source source)
{
	void *real_arg = NULL;
	switch (source) {
		case CTL_QUERY_CONFIG_INPUT:
			real_arg = ctl_parse_args(n->arg, write_arg);
			break;
		case CTL_QUERY_PROGRAMMATIC:
			real_arg = write_arg;
			break;
		default:
			ASSERT(0);
			break;
	}

	return real_arg;
}

/*
 * ctl_query_cleanup_real_args -- (internal) cleanups relevant argument
 *	structures allocated as a result of the get_real_args call
 */
static void
ctl_query_cleanup_real_args(struct ctl_node *n, void *real_arg,
	enum ctl_query_source source)
{
	switch (source) {
		case CTL_QUERY_CONFIG_INPUT:
			Free(real_arg);
			break;
		case CTL_QUERY_PROGRAMMATIC:
			break;
		default:
			ASSERT(0);
			break;
	}
}

/*
 * ctl_exec_query_read -- (internal) calls the read callback of a node
 */
static int
ctl_exec_query_read(PMEMobjpool *pop, struct ctl_node *n,
	enum ctl_query_source source, void *arg, struct ctl_indexes *indexes)
{
	if (arg == NULL) {
		ERR("read queries require non-NULL argument");
		errno = EINVAL;
		return -1;
	}

	return n->cb[CTL_QUERY_READ](pop, source, arg, indexes);
}

/*
 * ctl_exec_query_write -- (internal) calls the write callback of a node
 */
static int
ctl_exec_query_write(PMEMobjpool *pop, struct ctl_node *n,
	enum ctl_query_source source, void *arg, struct ctl_indexes *indexes)
{
	if (arg == NULL) {
		ERR("write queries require non-NULL argument");
		errno = EINVAL;
		return -1;
	}

	void *real_arg = ctl_query_get_real_args(n, arg, source);
	if (real_arg == NULL) {
		errno = EINVAL;
		ERR("invalid arguments");
		return -1;
	}

	int ret = n->cb[CTL_QUERY_WRITE](pop, source, real_arg, indexes);
	ctl_query_cleanup_real_args(n, real_arg, source);

	return ret;
}

/*
 * ctl_exec_query_runnable -- (internal) calls the run callback of a node
 */
static int
ctl_exec_query_runnable(PMEMobjpool *pop, struct ctl_node *n,
	enum ctl_query_source source, void *arg, struct ctl_indexes *indexes)
{
	return n->cb[CTL_QUERY_RUNNABLE](pop, source, arg, indexes);
}

static int (*ctl_exec_query[MAX_CTL_QUERY_TYPE])(PMEMobjpool *pop,
	struct ctl_node *n, enum ctl_query_source source, void *arg,
	struct ctl_indexes *indexes) = {
	ctl_exec_query_read,
	ctl_exec_query_write,
	ctl_exec_query_runnable,
};

/*
 * ctl_query -- (internal) parses the name and calls the appropriate methods
 *	from the ctl tree
 */
static int
ctl_query(PMEMobjpool *pop, enum ctl_query_source source,
	const char *name, enum ctl_query_type type, void *arg)
{
	LOG(3, "pop %p source %d name %s", pop, source, name);

	if (name == NULL) {
		ERR("invalid query");
		errno = EINVAL;
		return -1;
	}

	/*
	 * All of the indexes are put on this list so that the handlers can
	 * easily retrieve the index values. The list is cleared once the ctl
	 * query has been handled.
	 */
	struct ctl_indexes indexes;
	SLIST_INIT(&indexes);

	int ret = -1;

	struct ctl_node *n = ctl_find_node(CTL_NODE(global),
		name, &indexes);

	if (n == NULL && pop) {
		ctl_delete_indexes(&indexes);
		n = ctl_find_node(pop->ctl->root, name, &indexes);
	}

	if (n == NULL || n->type != CTL_NODE_LEAF || n->cb[type] == NULL) {
		ERR("invalid query entry point %s", name);
		errno = EINVAL;
		goto out;
	}

	ret = ctl_exec_query[type](pop, n, source, arg, &indexes);

out:
	ctl_delete_indexes(&indexes);

	return ret;
}

/*
 * pmemobj_ctl_getU -- programmatically executes a read ctl query
 */
#ifndef _WIN32
static inline
#endif
int
pmemobj_ctl_getU(PMEMobjpool *pop, const char *name, void *arg)
{
	LOG(3, "pop %p name %s arg %p", pop, name, arg);
	return ctl_query(pop, CTL_QUERY_PROGRAMMATIC,
		name, CTL_QUERY_READ, arg);
}

/*
 * pmemobj_ctl_setU -- programmatically executes a write ctl query
 */
#ifndef _WIN32
static inline
#endif
int
pmemobj_ctl_setU(PMEMobjpool *pop, const char *name, void *arg)
{
	LOG(3, "pop %p name %s arg %p", pop, name, arg);
	return ctl_query(pop, CTL_QUERY_PROGRAMMATIC,
		name, CTL_QUERY_WRITE, arg);
}

/*
 * pmemobj_ctl_execU -- programmatically executes a runnable ctl query
 */
#ifndef _WIN32
static inline
#endif
int
pmemobj_ctl_execU(PMEMobjpool *pop, const char *name, void *arg)
{
	LOG(3, "pop %p name %s arg %p", pop, name, arg);
	return ctl_query(pop, CTL_QUERY_PROGRAMMATIC,
		name, CTL_QUERY_RUNNABLE, arg);
}

#ifndef _WIN32
/*
 * pmemobj_ctl_get -- programmatically executes a read ctl query
 */
int
pmemobj_ctl_get(PMEMobjpool *pop, const char *name, void *arg)
{
	return pmemobj_ctl_getU(pop, name, arg);
}

/*
 * pmemobj_ctl_set -- programmatically executes a write ctl query
 */
int
pmemobj_ctl_set(PMEMobjpool *pop, const char *name, void *arg)
{
	return pmemobj_ctl_setU(pop, name, arg);
}

/*
 * pmemobj_ctl_exec -- programmatically executes a runnable ctl query
 */
int
pmemobj_ctl_exec(PMEMobjpool *pop, const char *name, void *arg)
{
	return pmemobj_ctl_execU(pop, name, arg);
}
#else
/*
 * pmemobj_ctl_getW -- programmatically executes a read ctl query
 */
int
pmemobj_ctl_getW(PMEMobjpool *pop, const wchar_t *name, void *arg)
{
	char *uname = util_toUTF8(name);
	if (uname == NULL)
		return -1;

	int ret = pmemobj_ctl_getU(pop, uname, arg);
	util_free_UTF8(uname);

	return ret;
}

/*
 * pmemobj_ctl_setW -- programmatically executes a write ctl query
 */
int
pmemobj_ctl_setW(PMEMobjpool *pop, const wchar_t *name, void *arg)
{
	char *uname = util_toUTF8(name);
	if (uname == NULL)
		return -1;

	int ret = pmemobj_ctl_setU(pop, uname, arg);
	util_free_UTF8(uname);

	return ret;
}

/*
 * pmemobj_ctl_execW -- programmatically executes a runnable ctl query
 */
int
pmemobj_ctl_execW(PMEMobjpool *pop, const wchar_t *name, void *arg)
{
	char *uname = util_toUTF8(name);
	if (uname == NULL)
		return -1;

	int ret = pmemobj_ctl_execU(pop, uname, arg);
	util_free_UTF8(uname);

	return ret;
}
#endif

/*
 * ctl_register_module_node -- adds a new node to the CTL tree root.
 */
void
ctl_register_module_node(struct ctl *c, const char *name, struct ctl_node *n)
{
	struct ctl_node *nnode = c == NULL ?
		&CTL_NODE(global)[ctl_global_first_free++] :
		&c->root[c->first_free++];

	nnode->children = n;
	nnode->type = CTL_NODE_NAMED;
	nnode->name = name;
}

/*
 * ctl_parse_query -- (internal) splits an entire query string
 *	into name and value
 */
static int
ctl_parse_query(char *qbuf, char **name, char **value)
{
	if (qbuf == NULL)
		return 1;

	char *sptr;
	*name = strtok_r(qbuf, CTL_NAME_VALUE_SEPARATOR, &sptr);
	if (*name == NULL)
		return -1;

	*value = strtok_r(NULL, CTL_NAME_VALUE_SEPARATOR, &sptr);
	if (*value == NULL)
		return -1;

	/* the value itself mustn't include CTL_NAME_VALUE_SEPARATOR */
	char *extra = strtok_r(NULL, CTL_NAME_VALUE_SEPARATOR, &sptr);
	if (extra != NULL)
		return -1;

	return 0;
}

/*
 * ctl_load_config -- executes the entire query collection from a provider
 */
static int
ctl_load_config(PMEMobjpool *pop, char *buf)
{
	int r = 0;
	char *sptr = NULL; /* for internal use of strtok */
	char *name;
	char *value;

	ASSERTne(buf, NULL);

	char *qbuf = strtok_r(buf, CTL_STRING_QUERY_SEPARATOR, &sptr);
	do {
		r = ctl_parse_query(qbuf, &name, &value);
		if (r == 0)
			r = ctl_query(pop, CTL_QUERY_CONFIG_INPUT,
				name, CTL_QUERY_WRITE, value);

		if (r == -1) {
			ERR("failed to parse query %s", qbuf);
			return -1;
		}

		qbuf = strtok_r(NULL, CTL_STRING_QUERY_SEPARATOR, &sptr);
	} while (r == 0 && qbuf != NULL);

	/* ctl_parse_query() returns 1 to indicate end */
	return r >= 0 ? 0 : -1;
}

/*
 * ctl_load_config_from_string -- loads obj configuration from string
 */
int
ctl_load_config_from_string(PMEMobjpool *pop, const char *cfg_string)
{
	LOG(3, "pop %p cfg_string \"%s\"", pop, cfg_string);

	char *buf = Strdup(cfg_string);
	if (buf == NULL) {
		ERR("!Strdup");
		return -1;
	}

	int ret = ctl_load_config(pop, buf);

	Free(buf);
	return ret;
}

/*
 * ctl_load_config_from_file -- loads obj configuration from file
 *
 * This function opens up the config file, allocates a buffer of size equal to
 * the size of the file, reads its content and sanitizes it for ctl_load_config.
 */
int
ctl_load_config_from_file(PMEMobjpool *pop, const char *cfg_file)
{
	LOG(3, "pop %p cfg_file \"%s\"", pop, cfg_file);

	int ret = -1;

	FILE *fp = os_fopen(cfg_file, "r");
	if (fp == NULL)
		return ret;

	int err;
	if ((err = fseek(fp, 0, SEEK_END)) != 0)
		goto error_file_parse;

	long fsize = ftell(fp);
	if (fsize == -1)
		goto error_file_parse;

	if (fsize > MAX_CONFIG_FILE_LEN) {
		ERR("Config file too large");
		goto error_file_parse;
	}

	if ((err = fseek(fp, 0, SEEK_SET)) != 0)
		goto error_file_parse;

	char *buf = Zalloc((size_t)fsize + 1); /* +1 for NULL-termination */
	if (buf == NULL) {
		ERR("!Zalloc");
		goto error_file_parse;
	}

	size_t bufpos = 0;

	int c;
	int is_comment_section = 0;
	while ((c = fgetc(fp)) != EOF) {
		if (c == '#')
			is_comment_section = 1;
		else if (c == '\n')
			is_comment_section = 0;
		else if (!is_comment_section && !isspace(c))
			buf[bufpos++] = (char)c;
	}

	ret = ctl_load_config(pop, buf);

	Free(buf);

error_file_parse:
	(void) fclose(fp);
	return ret;
}

/*
 * ctl_new -- allocates and initalizes ctl data structures
 */
struct ctl *
ctl_new(void)
{
	struct ctl *c = Zalloc(sizeof(struct ctl));
	if (c == NULL)
		return NULL;

	c->first_free = 0;
	return c;
}

/*
 * ctl_delete -- deletes ctl
 */
void
ctl_delete(struct ctl *c)
{
	Free(c);
}

/*
 * ctl_parse_ll -- (internal) parses and returns a long long signed integer
 */
static long long
ctl_parse_ll(const char *str)
{
	char *endptr;
	int olderrno = errno;
	errno = 0;
	long long val = strtoll(str, &endptr, 0);
	if (endptr == str || errno != 0)
		return LLONG_MIN;
	errno = olderrno;

	return val;
}

/*
 * ctl_arg_boolean -- checks whether the provided argument contains
 *	either a 1 or y or Y.
 */
int
ctl_arg_boolean(const void *arg, void *dest, size_t dest_size)
{
	int *intp = dest;
	char in = ((char *)arg)[0];

	if (tolower(in) == 'y' || in == '1') {
		*intp = 1;
		return 0;
	} else if (tolower(in) == 'n' || in == '0') {
		*intp = 0;
		return 0;
	}

	return -1;
}

/*
 * ctl_arg_integer -- parses signed integer argument
 */
int
ctl_arg_integer(const void *arg, void *dest, size_t dest_size)
{
	long long val = ctl_parse_ll(arg);
	if (val == LLONG_MIN)
		return -1;

	switch (dest_size) {
		case sizeof(int):
			if (val > INT_MAX || val < INT_MIN)
				return -1;
			*(int *)dest = (int)val;
			break;
		case sizeof(long long):
			*(long long *)dest = val;
			break;
		case sizeof(uint8_t):
			if (val > UINT8_MAX || val < 0)
				return -1;
			*(uint8_t *)dest = (uint8_t)val;
			break;
	}

	return 0;
}

/*
 * ctl_arg_string -- verifies length and copies a string argument into a zeroed
 *	buffer
 */
int
ctl_arg_string(const void *arg, void *dest, size_t dest_size)
{
	/* check if the incoming string is longer or equal to dest_size */
	if (strnlen(arg, dest_size) == dest_size)
		return -1;

	strncpy(dest, arg, dest_size);

	return 0;
}