summaryrefslogtreecommitdiffstats
path: root/src/storage-mmap.c
blob: 0cc04199257c6189a8fb7ffb0e0ba8f0b974deb7 (plain)
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

#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <asm/errno.h>
#include <errno.h>

#include "storage.h"
#include "intf.h"
#include "debug.h"

static int unmap_chunk(struct tsnif_storage_handle *h,
		       struct tsnif_storage_chunk *chunk);

static int write_header(int fd, struct tsnif_storage_opts *opts)
{
	int err;

	struct tsnif_storage_header_mmap header = {
		.common = {
			.magic         = TSNIF_HEADER_MAGIC,
			.version       = TSNIF_STORAGE_VERSION,
			.storage_type  = TSNIF_STORAGE_TYPE_MMAP,
			.term_type     = opts->term_type,
		},
		.size_max = opts->size_max,
	};

	err = write(fd, &header, sizeof(header));
	if (err < 0)
		return err;

	return ftruncate(fd, sysconf(_SC_PAGESIZE));
}

static int map_header(struct tsnif_storage_handle *h)
{
	void *ptr;
	int flags = PROT_READ;

	if (h->opts->flags & TSNIF_STORAGE_OPT_WRITE)
		flags = PROT_WRITE;

	ptr = mmap(NULL, sysconf(_SC_PAGESIZE),
			flags,
			MAP_SHARED, h->fd, 0);
	if (MAP_FAILED == ptr)
		return -1;

	h->header = ptr;
	return 0;
}

static int check_header(struct tsnif_storage_handle *h)
{
	struct tsnif_storage_header_mmap *header = h->header;
	struct tsnif_storage_header *common = &header->common;

	if (common->magic != TSNIF_HEADER_MAGIC) {
		TSNIF_DEBUG(STORAGE, "failed: wrong header magic %x\n", common->magic);
		return -1;
	}

	if (common->version != TSNIF_STORAGE_VERSION) {
		TSNIF_DEBUG(STORAGE, "failed: wrong storage version %x\n", common->version);
		return -1;
	}

	if (common->storage_type != TSNIF_STORAGE_TYPE_MMAP) {
		TSNIF_DEBUG(STORAGE, "failed: wrong storage type %x\n", common->storage_type);
		return -1;
	}

	TSNIF_DEBUG(STORAGE, "got terminal type %d\n", common->term_type);
	return 0;
}

int tsnif_storage_init(struct tsnif_storage_handle *h,
		       struct tsnif_storage_opts *opts, char *name)
{
	int fd, create, err;
	int flags = O_RDONLY;
	int mode = S_IRUSR | S_IWUSR;

	if (!opts)
		return -1;

	memset(h, 0, sizeof(*h));
	h->opts = opts;

	/* mmap page sized chunks as default */
	if (!opts->chunk_size)
		opts->chunk_size = sysconf(_SC_PAGESIZE);

	create = (opts->flags & TSNIF_STORAGE_OPT_WRITE);
	if (create)
		flags |= O_CREAT | O_TRUNC | O_RDWR;

	fd = open(name, flags, mode);
	if (fd < 0) {
		TSNIF_DEBUG(STORAGE, "open failed for '%s': %s\n",
			    name, strerror(errno));
		return -1;
	}

	TSNIF_DEBUG(STORAGE, "opened '%s'\n", name);

	if (create &&
	    (err = write_header(fd, opts))) {
		TSNIF_DEBUG(STORAGE, "write failed for '%s': %s\n",
			    name, strerror(errno));
		close(fd);
		return err;
	}

	h->fd = fd;

	err = map_header(h);
	if (err) {
		close(fd);
		return err;
	}

	TSNIF_DEBUG(STORAGE, "records count %d\n", h->header->cnt);

	if (opts->flags & TSNIF_STORAGE_OPT_READ) {
		struct stat st;

		err = fstat(fd, &st);
		if (err)
			return err;

		h->file_size = st.st_size;

		return check_header(h);
	}

	return 0;
}

int tsnif_storage_close(struct tsnif_storage_handle *h)
{
	if (h->header) {
		TSNIF_DEBUG(STORAGE, "records count %d\n", h->header->cnt);
		munmap(h->header, sysconf(_SC_PAGESIZE));
	}

	unmap_chunk(h, &h->active_chunk);

	close(h->fd);
	return 0;
}

static int get_open_offset(struct tsnif_storage_handle *h)
{
	return h->header->offset_start;
}

static int unmap_chunk(struct tsnif_storage_handle *h,
		       struct tsnif_storage_chunk *chunk)
{
	if (!chunk->header)
		return -1;

	return munmap(chunk->header, h->opts->chunk_size);
}

enum {
	STORAGE_MMAP_FIRST,
	STORAGE_MMAP_NEXT,
	STORAGE_MMAP_PREV,
};

static int file_trunc(struct tsnif_storage_handle *h, off_t new_size)
{
	off_t old_size;

	TSNIF_DEBUG(STORAGE, "new size %d\n", new_size);

	old_size = lseek(h->fd, 0, SEEK_END);
	if (old_size < 0)
		return -1;

	TSNIF_DEBUG(STORAGE, "old size %d\n", old_size);

	if (old_size >= new_size)
		return 0;

	TSNIF_DEBUG(STORAGE, "truncating to %d\n", new_size);

	return ftruncate(h->fd, new_size);
}

static int map_chunk(struct tsnif_storage_handle *h,
		     struct tsnif_storage_chunk *chunk, int what)
{
	void *ptr;
	off_t offset = 0;
	int err, flags = PROT_READ;


	TSNIF_DEBUG(STORAGE, "what %d\n", what);

	if (what == STORAGE_MMAP_FIRST) {
		if (h->opts->flags & TSNIF_STORAGE_OPT_READ)
			offset = get_open_offset(h);
		else {
			offset = sysconf(_SC_PAGESIZE);
			h->header->offset_start = offset;
		}
	}

	if (what == STORAGE_MMAP_NEXT) {

		offset = chunk->offset + h->opts->chunk_size;

		if (h->opts->flags & TSNIF_STORAGE_OPT_READ) {

			TSNIF_DEBUG(STORAGE, "file size       0x%x\n", h->file_size);
			TSNIF_DEBUG(STORAGE, "offset          0x%x\n", offset);

			if (h->file_size == offset)
				offset = sysconf(_SC_PAGESIZE);

			TSNIF_DEBUG(STORAGE, "offset          0x%x\n", offset);
			TSNIF_DEBUG(STORAGE, "start offset    0x%x\n", h->header->offset_start);

			if (h->header->offset_start == offset)
				return TSNIF_STORAGE_READ_EOF;
		} else {
			/* check the max size only in write mode */
			if (offset > h->opts->size_max)
				offset = sysconf(_SC_PAGESIZE);
		}

		TSNIF_DEBUG(STORAGE, "chunk offset 0x%x\n", chunk->offset);
		TSNIF_DEBUG(STORAGE, "chunk size   0x%x\n", chunk->offset);
		TSNIF_DEBUG(STORAGE, "max size     0x%x\n", h->opts->size_max);
	}

	if (what == STORAGE_MMAP_PREV) {

		if (h->header->offset_start == chunk->offset)
			return TSNIF_STORAGE_READ_EOF;

		if (chunk->offset == sysconf(_SC_PAGESIZE))
			offset = h->file_size - h->opts->chunk_size;
		else
			offset = chunk->offset - h->opts->chunk_size;

		TSNIF_DEBUG(STORAGE, "chunk offset 0x%x\n", chunk->offset);
		TSNIF_DEBUG(STORAGE, "chunk size   0x%x\n", chunk->offset);
		TSNIF_DEBUG(STORAGE, "max size     0x%x\n", h->opts->size_max);
	}

	TSNIF_DEBUG(STORAGE, "new offset 0x%x, old offset 0x%x\n",
		    offset, chunk->offset);

	if (chunk->header)
		unmap_chunk(h, chunk);

	if (h->opts->flags & TSNIF_STORAGE_OPT_WRITE) {
		err = file_trunc(h, offset + h->opts->chunk_size);
		if (err)
			return err;

		flags = PROT_WRITE;
	}

	ptr = mmap(NULL, h->opts->chunk_size,
			flags,
			MAP_SHARED, h->fd, offset);
	if (MAP_FAILED == ptr)
		return -1;

	TSNIF_DEBUG(STORAGE, "mmap-ed offset %ld, size %d\n",
		    offset, h->opts->chunk_size);

	chunk->offset = offset;
	chunk->header = ptr;
	chunk->current_data  = chunk->header->rec0;
	chunk->current_index = (void*) (chunk->header) + h->opts->chunk_size
				- sizeof(uint32_t);
	return 0;
}

static int clear_chunk(struct tsnif_storage_handle *h,
		       struct tsnif_storage_chunk *chunk)
{
	struct tsnif_storage_chunk_header *header = chunk->header;

	memset(header, 0x0, sizeof(*header));
	header->free = h->opts->chunk_size -
		    sizeof(struct tsnif_storage_chunk_header);

	chunk->current_data  = header->rec0;
	chunk->current_index = (void*) (header) + h->opts->chunk_size
				- sizeof(uint32_t);

	TSNIF_DEBUG(STORAGE, "chunk header %p\n", chunk->header);
	TSNIF_DEBUG(STORAGE, "chunk data   %p\n", chunk->current_data);
	TSNIF_DEBUG(STORAGE, "chunk index  %p\n", chunk->current_index);
	TSNIF_DEBUG(STORAGE, "chunk size   0x%x\n", h->opts->chunk_size);
	TSNIF_DEBUG(STORAGE, "chunk free   %d\n", header->free);
	return 0;
}

static int map_write_chunk(struct tsnif_storage_handle *h,
			   struct tsnif_storage_chunk *chunk, int what)
{
	return (map_chunk(h, chunk, what) ||
		clear_chunk(h, chunk));
}

static int map_read_chunk(struct tsnif_storage_handle *h,
			  struct tsnif_storage_chunk *chunk, int what)
{
	int err;

	err = map_chunk(h, chunk, what);
	if ((err < 0 ) ||
	    (err == TSNIF_STORAGE_READ_EOF))
		return err;

	chunk->read_index = -1;
	return 0;
}

static int get_chunk_write_rec(struct tsnif_storage_handle *h, int size)
{
	struct tsnif_storage_chunk *chunk = &h->active_chunk;
	struct tsnif_storage_chunk_header *chunk_header = chunk->header;

	TSNIF_DEBUG(STORAGE, "chunk header %p\n", chunk_header);

	/* first time */
	if (!chunk_header)
		return map_write_chunk(h, chunk, STORAGE_MMAP_FIRST);

	/* we fit into current chunk */
#define INDEX_OVERHEAD (4*sizeof(uint32_t))
	if (chunk_header->free > (size +
			   sizeof(struct tsnif_storage_rec_mmap) +
			   INDEX_OVERHEAD))
		return 0;

	/* we dont fit into current chunk,
	 * let's go for another */
	return map_write_chunk(h, chunk, STORAGE_MMAP_NEXT);
}

static int get_chunk_read_rec(struct tsnif_storage_handle *h, int what)
{
	struct tsnif_storage_chunk *chunk = &h->active_chunk;
	struct tsnif_storage_chunk_header *chunk_header = chunk->header;
	int map_what = 0;

	TSNIF_DEBUG(STORAGE, "chunk header %p\n", chunk_header);

	/* first time */
	if (!chunk_header)
		return map_read_chunk(h, chunk, STORAGE_MMAP_FIRST);

	TSNIF_DEBUG(STORAGE, "chunk cnt   0x%x\n", chunk_header->cnt);
	TSNIF_DEBUG(STORAGE, "read index  0x%x\n", chunk->read_index);

	/* we want next record and we have it */
	if (TSNIF_STORAGE_READ_NEXT == what) {
		if ((chunk->read_index + 1) < chunk_header->cnt)
			return 0;
		map_what = STORAGE_MMAP_NEXT;
	}

	/* we want previous record and we have it */
	if (TSNIF_STORAGE_READ_PREV == what) {
		if (chunk->read_index)
			return 0;
		map_what = STORAGE_MMAP_PREV;
	}

	return map_read_chunk(h, chunk, map_what);
}

static int store_rec(struct tsnif_storage_handle *h,
		     struct tsnif_storage_rec *rec)
{
	struct tsnif_storage_rec_mmap *mrec;
	struct tsnif_storage_chunk *chunk = &h->active_chunk;
	struct tsnif_storage_chunk_header *header = chunk->header;

	TSNIF_DEBUG(STORAGE, "chunk data  %p\n", chunk->current_data);
	TSNIF_DEBUG(STORAGE, "chunk index %p\n", chunk->current_index);
	TSNIF_DEBUG(STORAGE, "time %ld\n", rec->time.tv_sec);

	mrec = chunk->current_data;
	mrec->len   = rec->len;
	mrec->flags = rec->flags;
	mrec->time  = rec->time;
	mrec->ws    = rec->ws;
	memcpy(mrec->data, rec->ptr, rec->len);

#define RECLEN (sizeof(struct tsnif_storage_rec_mmap) + rec->len)

	*(chunk->current_index) = (void*) chunk->current_data - (void*) header;
	TSNIF_DEBUG(STORAGE, "rec offset  0x%x\n", *(chunk->current_index));
	TSNIF_DEBUG(STORAGE, "rec size    0x%x\n", rec->len);

	chunk->current_index--;
	chunk->current_data += RECLEN;

	/* update file header */
	h->header->cnt++;

	/* update chunk header */
	header->cnt++;
	header->free -= (sizeof(uint32_t) + RECLEN);

	TSNIF_DEBUG(STORAGE, "chunk data  %p\n", chunk->current_data);
	TSNIF_DEBUG(STORAGE, "chunk index %p\n", chunk->current_index);
	TSNIF_DEBUG(STORAGE, "chunk free  %d\n", header->free);
	TSNIF_DEBUG(STORAGE, "chunk cnt   %d\n", header->cnt);
	return 0;
}

static int read_rec(struct tsnif_storage_handle *h,
		     struct tsnif_storage_rec *rec, int what)
{
	struct tsnif_storage_rec_mmap *mrec;
	struct tsnif_storage_chunk *chunk = &h->active_chunk;
	struct tsnif_storage_chunk_header *header = chunk->header;
	off_t offset;

	TSNIF_DEBUG(STORAGE, "chunk data  %p\n", chunk->current_data);
	TSNIF_DEBUG(STORAGE, "chunk index %p\n", chunk->current_index);
	TSNIF_DEBUG(STORAGE, "read index  0x%x\n", chunk->read_index);

	/* the chunk got loaded just now */
	if (chunk->read_index == -1) {
		if (what == TSNIF_STORAGE_READ_NEXT)
			chunk->read_index = 0;
		else
			chunk->read_index = chunk->header->cnt - 1;
	/* move within the chunk */
	} else {
		if (what == TSNIF_STORAGE_READ_NEXT)
			chunk->read_index++;
		else
			chunk->read_index--;
	}

	offset = *(chunk->current_index - chunk->read_index);
	mrec = (void*) header + offset;

	rec->ptr   = mrec->data;
	rec->len   = mrec->len;
	rec->flags = mrec->flags;
	rec->time  = mrec->time;
	rec->ws    = mrec->ws;
	return 0;
}

int tsnif_storage_write(struct tsnif_storage_handle *h,
			struct tsnif_storage_rec *rec)
{
	int err;

	TSNIF_DEBUG(STORAGE, "entry\n");

	err = get_chunk_write_rec(h, rec->len);
	if (err)
		return err;

	return store_rec(h, rec);
}

int tsnif_storage_read(struct tsnif_storage_handle *h, int what,
			struct tsnif_storage_rec *rec)
{
	int err;

	TSNIF_DEBUG(STORAGE, "entry\n");

	err = get_chunk_read_rec(h, what);
	if (err)
		return err;

	return read_rec(h, rec, what);
}

int tsnif_storage_term_type(struct tsnif_storage_handle *h)
{
	return h->header->common.term_type;
}