Remove remaining references to paraslash.
[osl.git] / osl.c
1 /*
2 * Copyright (C) 2007-2008 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file osl.c Object storage layer functions. */
8 #include <dirent.h> /* readdir() */
9 #include <assert.h>
10
11 #include "log.h"
12 #include "osl.h"
13 #include "util.h"
14 #include "osl_core.h"
15
16 /* Taken from Drepper: How to write shared libraries, Appendix B. */
17 #include <stddef.h>
18 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
19 #define MSGSTRFIELD1(line) str##line
20 static const union msgstr_t {
21 struct {
22 #define OSL_ERROR(n, s) char MSGSTRFIELD(__LINE__)[sizeof(s)];
23 #include "errtab.h"
24 #undef OSL_ERROR
25 };
26 char str[0];
27 } msgstr = { {
28 #define OSL_ERROR(n, s) s,
29 #include "errtab.h"
30 #undef OSL_ERROR
31 } };
32 static const unsigned int errmsgidx[] = {
33 #define OSL_ERROR(n, s) [n] = offsetof(union msgstr_t, MSGSTRFIELD(__LINE__)),
34 #include "errtab.h"
35 #undef OSL_ERROR
36 };
37
38 __export const char *osl_strerror(int num)
39 {
40 return msgstr.str + errmsgidx[num];
41 }
42
43 int loglevel = 0;
44
45 static void __attribute ((constructor)) init_loglevel(void)
46 {
47 char *p = getenv("OSL_LOGLEVEL");
48
49 /* don't log anything if unset */
50 loglevel = p? atoi(p) : EMERG + 1;
51 }
52
53 /**
54 * The log function.
55 *
56 * \param ll Loglevel.
57 * \param fmt Usual format string.
58 *
59 * All XXX_LOG() macros use this function.
60 */
61 __printf_2_3 void __log(int ll, const char* fmt,...)
62 {
63 va_list argp;
64 FILE *outfd;
65 struct tm *tm;
66 time_t t1;
67 char str[255] = "";
68
69 if (ll < loglevel)
70 return;
71 outfd = stderr;
72 time(&t1);
73 tm = localtime(&t1);
74 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
75 fprintf(outfd, "%s ", str);
76 va_start(argp, fmt);
77 vfprintf(outfd, fmt, argp);
78 va_end(argp);
79 }
80
81 /**
82 * A wrapper for lseek(2).
83 *
84 * \param fd The file descriptor whose offset is to be to repositioned.
85 * \param offset A value-result parameter.
86 * \param whence Usual repositioning directive.
87 *
88 * Reposition the offset of the file descriptor \a fd to the argument \a offset
89 * according to the directive \a whence. Upon successful return, \a offset
90 * contains the resulting offset location as measured in bytes from the
91 * beginning of the file.
92 *
93 * \return Positive on success. Otherwise, the function returns \p -E_OSL_LSEEK.
94 *
95 * \sa lseek(2).
96 */
97 static int __lseek(int fd, off_t *offset, int whence)
98 {
99 *offset = lseek(fd, *offset, whence);
100 int ret = -E_OSL_LSEEK;
101 if (*offset == -1)
102 return ret;
103 return 1;
104 }
105
106 static int append_file(const char *filename, char *data, size_t data_size,
107 uint32_t *new_pos)
108 {
109 int ret, fd;
110
111 // DEBUG_LOG("appending %zu + %zu bytes\n", header_size, data_size);
112 ret = osl_open(filename, O_WRONLY | O_CREAT | O_APPEND, 0644);
113 if (ret < 0)
114 return ret;
115 fd = ret;
116 ret = write_all(fd, data, &data_size);
117 if (ret < 0)
118 goto out;
119 if (new_pos) {
120 off_t offset = 0;
121 ret = __lseek(fd, &offset, SEEK_END);
122 if (ret < 0)
123 goto out;
124 // DEBUG_LOG("new file size: " FMT_OFF_T "\n", offset);
125 *new_pos = offset;
126 }
127 ret = 1;
128 out:
129 close(fd);
130 return ret;
131 }
132
133 static int verify_name(const char *name)
134 {
135 if (!name)
136 return -E_OSL_BAD_NAME;
137 if (!*name)
138 return -E_OSL_BAD_NAME;
139 if (strchr(name, '/'))
140 return -E_OSL_BAD_NAME;
141 if (!strcmp(name, ".."))
142 return -E_OSL_BAD_NAME;
143 if (!strcmp(name, "."))
144 return -E_OSL_BAD_NAME;
145 return 1;
146 }
147
148 static char *disk_storage_dirname(const struct osl_table *t, unsigned col_num,
149 const char *ds_name)
150 {
151 char *dirname, *column_name = column_filename(t, col_num);
152
153 if (!column_name)
154 return NULL;
155 if (!(t->desc->flags & OSL_LARGE_TABLE))
156 return column_name;
157 dirname = make_message("%s/%.2s", column_name, ds_name);
158 free(column_name);
159 return dirname;
160 }
161
162 static char *disk_storage_name_of_object(const struct osl_table *t,
163 const struct osl_object *obj)
164 {
165 HASH_TYPE hash[HASH_SIZE];
166 hash_object(obj, hash);
167 return disk_storage_name_of_hash(t, hash);
168 }
169
170 static int disk_storage_name_of_row(const struct osl_table *t,
171 const struct osl_row *row, char **name)
172 {
173 struct osl_object obj;
174 int ret = osl_get_object(t, row, t->disk_storage_name_column, &obj);
175
176 if (ret < 0)
177 return ret;
178 *name = disk_storage_name_of_object(t, &obj);
179 if (*name)
180 return 1;
181 return -E_OSL_NOMEM;
182 }
183
184 static void column_name_hash(const char *col_name, HASH_TYPE *hash)
185 {
186 hash_function(col_name, strlen(col_name), hash);
187 }
188
189 static int init_column_descriptions(struct osl_table *t)
190 {
191 int i, j, ret;
192 const struct osl_column_description *cd;
193
194 ret = verify_name(t->desc->name);
195 if (ret < 0)
196 goto err;
197 ret = -E_OSL_BAD_DB_DIR;
198 if (!t->desc->dir && (t->num_disk_storage_columns || t->num_mapped_columns))
199 goto err;
200 /* the size of the index header without column descriptions */
201 t->index_header_size = IDX_COLUMN_DESCRIPTIONS;
202 FOR_EACH_COLUMN(i, t->desc, cd) {
203 struct osl_column *col = t->columns + i;
204 if (cd->storage_flags & OSL_RBTREE) {
205 if (!cd->compare_function)
206 return -E_OSL_NO_COMPARE_FUNC;
207 }
208 if (cd->storage_type == OSL_NO_STORAGE)
209 continue;
210 ret = -E_OSL_NO_COLUMN_NAME;
211 if (!cd->name || !cd->name[0])
212 goto err;
213 ret = verify_name(cd->name);
214 if (ret < 0)
215 goto err;
216 t->index_header_size += index_column_description_size(cd->name);
217 column_name_hash(cd->name, col->name_hash);
218 ret = -E_OSL_DUPLICATE_COL_NAME;
219 for (j = i + 1; j < t->desc->num_columns; j++) {
220 const char *name2 = get_column_description(t->desc,
221 j)->name;
222 if (cd->name && name2 && !strcmp(cd->name, name2))
223 goto err;
224 }
225 }
226 return 1;
227 err:
228 return ret;
229 }
230
231 /**
232 * Initialize a struct table from given table description.
233 *
234 * \param desc The description of the osl table.
235 * \param table_ptr Result is returned here.
236 *
237 * This function performs several sanity checks on \p desc and returns if any
238 * of these tests fail. On success, a struct \p osl_table is allocated and
239 * initialized with data derived from \p desc.
240 *
241 * \return Standard.
242 *
243 * \sa struct osl_table.
244 */
245 int init_table_structure(const struct osl_table_description *desc,
246 struct osl_table **table_ptr)
247 {
248 const struct osl_column_description *cd;
249 struct osl_table *t = calloc(1, sizeof(*t));
250 int i, ret = -E_OSL_NOMEM, have_disk_storage_name_column = 0;
251
252 if (!t)
253 return ret;
254 ret = -E_OSL_BAD_TABLE_DESC;
255 if (!desc)
256 goto err;
257 DEBUG_LOG("creating table structure for '%s' from table "
258 "description\n", desc->name);
259 ret = -E_OSL_NO_COLUMN_DESC;
260 if (!desc->column_descriptions)
261 goto err;
262 ret = -E_OSL_NO_COLUMNS;
263 if (!desc->num_columns)
264 goto err;
265 ret = -E_OSL_NOMEM;
266 t->columns = calloc(desc->num_columns, sizeof(struct osl_column));
267 if (!t->columns)
268 goto err;
269 t->desc = desc;
270 FOR_EACH_COLUMN(i, t->desc, cd) {
271 enum osl_storage_type st = cd->storage_type;
272 enum osl_storage_flags sf = cd->storage_flags;
273 struct osl_column *col = &t->columns[i];
274
275 ret = -E_OSL_BAD_STORAGE_TYPE;
276 if (st != OSL_MAPPED_STORAGE && st != OSL_DISK_STORAGE
277 && st != OSL_NO_STORAGE)
278 goto err;
279 ret = -E_OSL_BAD_STORAGE_FLAGS;
280 if (st == OSL_DISK_STORAGE && sf & OSL_RBTREE)
281 goto err;
282 ret = -E_OSL_BAD_STORAGE_SIZE;
283 if (sf & OSL_FIXED_SIZE && !cd->data_size)
284 goto err;
285 switch (st) {
286 case OSL_DISK_STORAGE:
287 t->num_disk_storage_columns++;
288 break;
289 case OSL_MAPPED_STORAGE:
290 t->num_mapped_columns++;
291 col->index_offset = t->row_index_size;
292 t->row_index_size += 8;
293 break;
294 case OSL_NO_STORAGE:
295 col->volatile_num = t->num_volatile_columns;
296 t->num_volatile_columns++;
297 break;
298 }
299 if (sf & OSL_RBTREE) {
300 col->rbtree_num = t->num_rbtrees;
301 t->num_rbtrees++;
302 if ((sf & OSL_UNIQUE) && (st == OSL_MAPPED_STORAGE)) {
303 if (!have_disk_storage_name_column)
304 t->disk_storage_name_column = i;
305 have_disk_storage_name_column = 1;
306 }
307 }
308 }
309 ret = -E_OSL_NO_UNIQUE_RBTREE_COLUMN;
310 if (t->num_disk_storage_columns && !have_disk_storage_name_column)
311 goto err;
312 ret = -E_OSL_NO_RBTREE_COL;
313 if (!t->num_rbtrees)
314 goto err;
315 /* success */
316 DEBUG_LOG("OK. Index entry size: %u\n", t->row_index_size);
317 ret = init_column_descriptions(t);
318 if (ret < 0)
319 goto err;
320 *table_ptr = t;
321 return 1;
322 err:
323 free(t->columns);
324 free(t);
325 return ret;
326 }
327
328 /**
329 * Read the table description from index header.
330 *
331 * \param map The memory mapping of the index file.
332 * \param desc The values found in the index header are returned here.
333 *
334 * Read the index header, check for the osl magic string and the table version
335 * number. Read all information stored in the index header into \a desc.
336 *
337 * \return Standard.
338 *
339 * \sa struct osl_table_description, osl_create_table.
340 */
341 int read_table_desc(struct osl_object *map, struct osl_table_description *desc)
342 {
343 char *buf = map->data;
344 uint8_t version;
345 uint16_t header_size;
346 int ret, i;
347 unsigned offset;
348 struct osl_column_description *cd;
349
350 if (map->size < MIN_INDEX_HEADER_SIZE(1))
351 return -E_OSL_SHORT_TABLE;
352 if (strncmp(buf + IDX_OSL_MAGIC, OSL_MAGIC, strlen(OSL_MAGIC)))
353 return -E_OSL_NO_MAGIC;
354 version = read_u8(buf + IDX_VERSION);
355 if (version < MIN_TABLE_VERSION || version > MAX_TABLE_VERSION)
356 return -E_OSL_VERSION_MISMATCH;
357 desc->flags = read_u8(buf + IDX_TABLE_FLAGS);
358 desc->num_columns = read_u16(buf + IDX_NUM_COLUMNS);
359 INFO_LOG("%u columns\n", desc->num_columns);
360 if (!desc->num_columns)
361 return -E_OSL_NO_COLUMNS;
362 header_size = read_u16(buf + IDX_HEADER_SIZE);
363 if (map->size < header_size)
364 return -E_OSL_BAD_SIZE;
365 desc->column_descriptions = calloc(desc->num_columns,
366 sizeof(struct osl_column_description));
367 if (!desc->column_descriptions)
368 return -E_OSL_NOMEM;
369 offset = IDX_COLUMN_DESCRIPTIONS;
370 FOR_EACH_COLUMN(i, desc, cd) {
371 char *null_byte;
372
373 ret = -E_OSL_SHORT_TABLE;
374 if (map->size < offset + MIN_IDX_COLUMN_DESCRIPTION_SIZE) {
375 ERROR_LOG("map size = %zu < %u = offset + min desc size\n",
376 map->size, offset + MIN_IDX_COLUMN_DESCRIPTION_SIZE);
377 goto err;
378 }
379 cd->storage_type = read_u16(buf + offset + IDX_CD_STORAGE_TYPE);
380 cd->storage_flags = read_u16(buf + offset +
381 IDX_CD_STORAGE_FLAGS);
382 cd->data_size = read_u32(buf + offset + IDX_CD_DATA_SIZE);
383 null_byte = memchr(buf + offset + IDX_CD_NAME, '\0',
384 map->size - offset - IDX_CD_NAME);
385 ret = -E_OSL_INDEX_CORRUPTION;
386 if (!null_byte)
387 goto err;
388 ret = -E_OSL_NOMEM;
389 cd->name = strdup(buf + offset + IDX_CD_NAME);
390 if (!cd->name)
391 goto err;
392 offset += index_column_description_size(cd->name);
393 }
394 if (offset != header_size) {
395 ret = -E_OSL_INDEX_CORRUPTION;
396 ERROR_LOG("real header size = %u != %u = stored header size\n",
397 offset, header_size);
398 goto err;
399 }
400 return 1;
401 err:
402 FOR_EACH_COLUMN(i, desc, cd)
403 free(cd->name);
404 return ret;
405 }
406
407 /*
408 * check whether the table description given by \p t->desc matches the on-disk
409 * table structure stored in the index of \a t.
410 */
411 static int compare_table_descriptions(struct osl_table *t)
412 {
413 int i, ret;
414 struct osl_table_description desc;
415 const struct osl_column_description *cd1, *cd2;
416
417 /* read the on-disk structure into desc */
418 ret = read_table_desc(&t->index_map, &desc);
419 if (ret < 0)
420 return ret;
421 ret = -E_OSL_BAD_TABLE_FLAGS;
422 if (desc.flags != t->desc->flags)
423 goto out;
424 ret = -E_OSL_BAD_COLUMN_NUM;
425 if (desc.num_columns > t->desc->num_columns)
426 goto out;
427 if (desc.num_columns < t->desc->num_columns) {
428 struct osl_column_description *cd;
429 unsigned diff = t->desc->num_columns - desc.num_columns;
430 INFO_LOG("extending table by %u volatile columns\n", diff);
431 ret = -E_OSL_NOMEM;
432 desc.column_descriptions = realloc(desc.column_descriptions,
433 t->desc->num_columns * sizeof(struct osl_column_description));
434 if (!desc.column_descriptions)
435 goto out;
436 for (i = desc.num_columns; i < t->desc->num_columns; i++) {
437 cd = get_column_description(&desc, i);
438 cd->storage_type = OSL_NO_STORAGE;
439 cd->name = NULL;
440 }
441 desc.num_columns += diff;
442 }
443 FOR_EACH_COLUMN(i, t->desc, cd1) {
444 cd2 = get_column_description(&desc, i);
445 ret = -E_OSL_BAD_STORAGE_TYPE;
446 if (cd1->storage_type != cd2->storage_type)
447 goto out;
448 if (cd1->storage_type == OSL_NO_STORAGE)
449 continue;
450 ret = -E_OSL_BAD_STORAGE_FLAGS;
451 if (cd1->storage_flags != cd2->storage_flags) {
452 ERROR_LOG("sf1 = %u != %u = sf2\n",
453 cd1->storage_flags, cd2->storage_flags);
454 goto out;
455 }
456 ret = -E_OSL_BAD_DATA_SIZE;
457 if (cd1->storage_flags & OSL_FIXED_SIZE)
458 if (cd1->data_size != cd2->data_size)
459 goto out;
460 ret = -E_OSL_BAD_COLUMN_NAME;
461 if (strcmp(cd1->name, cd2->name))
462 goto out;
463 }
464 INFO_LOG("table description of '%s' matches on-disk data, good\n",
465 t->desc->name);
466 ret = 1;
467 out:
468 FOR_EACH_COLUMN(i, &desc, cd1)
469 free(cd1->name);
470 free(desc.column_descriptions);
471 return ret;
472 }
473
474 static int create_table_index(struct osl_table *t)
475 {
476 char *buf, *filename;
477 int i, ret;
478 size_t size = t->index_header_size;
479 const struct osl_column_description *cd;
480 unsigned offset;
481
482 INFO_LOG("creating %zu byte index for table %s\n", size,
483 t->desc->name);
484 buf = calloc(1, size);
485 if (!buf)
486 return -E_OSL_NOMEM;
487 sprintf(buf + IDX_OSL_MAGIC, "%s", OSL_MAGIC);
488 write_u8(buf + IDX_TABLE_FLAGS, t->desc->flags);
489 write_u8(buf + IDX_DIRTY_FLAG, 0);
490 write_u8(buf + IDX_VERSION, CURRENT_TABLE_VERSION);
491 write_u16(buf + IDX_NUM_COLUMNS, t->num_mapped_columns + t->num_disk_storage_columns);
492 write_u16(buf + IDX_HEADER_SIZE, t->index_header_size);
493 offset = IDX_COLUMN_DESCRIPTIONS;
494 FOR_EACH_COLUMN(i, t->desc, cd) {
495 /* no need to store info about volatile storage */
496 if (cd->storage_type == OSL_NO_STORAGE)
497 continue;
498 write_u16(buf + offset + IDX_CD_STORAGE_TYPE,
499 cd->storage_type);
500 write_u16(buf + offset + IDX_CD_STORAGE_FLAGS,
501 cd->storage_flags);
502 if (cd->storage_flags & OSL_FIXED_SIZE)
503 write_u32(buf + offset + IDX_CD_DATA_SIZE,
504 cd->data_size);
505 strcpy(buf + offset + IDX_CD_NAME, cd->name);
506 offset += index_column_description_size(cd->name);
507 }
508 assert(offset = size);
509 filename = index_filename(t->desc);
510 if (filename)
511 ret = write_file(filename, buf, size);
512 else
513 ret = -E_OSL_NOMEM;
514 free(buf);
515 free(filename);
516 return ret;
517 }
518
519 __export int osl_create_table(const struct osl_table_description *desc)
520 {
521 const struct osl_column_description *cd;
522 char *table_dir = NULL, *filename;
523 struct osl_table *t;
524 int i, ret = init_table_structure(desc, &t);
525
526 if (ret < 0)
527 return ret;
528 INFO_LOG("creating %s\n", desc->name);
529 FOR_EACH_COLUMN(i, t->desc, cd) {
530 if (cd->storage_type == OSL_NO_STORAGE)
531 continue;
532 if (!table_dir) {
533 ret = osl_mkdir(desc->dir, 0777);
534 if (ret < 0 && ret != -E_OSL_DIR_EXISTS)
535 goto out;
536 table_dir = make_message("%s/%s", desc->dir,
537 desc->name);
538 ret = -E_OSL_NOMEM;
539 if (!table_dir)
540 goto out;
541 ret = osl_mkdir(table_dir, 0777);
542 if (ret < 0)
543 goto out;
544 }
545 ret = -E_OSL_NOMEM;
546 filename = column_filename(t, i);
547 if (!filename)
548 goto out;
549 INFO_LOG("filename: %s\n", filename);
550 if (cd->storage_type == OSL_MAPPED_STORAGE) {
551 ret = osl_open(filename, O_RDWR | O_CREAT | O_EXCL,
552 0644);
553 free(filename);
554 if (ret < 0)
555 goto out;
556 close(ret);
557 continue;
558 }
559 /* DISK STORAGE */
560 ret = osl_mkdir(filename, 0777);
561 free(filename);
562 if (ret < 0)
563 goto out;
564 }
565 if (t->num_mapped_columns) {
566 ret = create_table_index(t);
567 if (ret < 0)
568 goto out;
569 }
570 ret = 1;
571 out:
572 free(table_dir);
573 free(t->columns);
574 free(t);
575 return ret;
576 }
577
578 static int table_is_dirty(struct osl_table *t)
579 {
580 char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
581 uint8_t dirty = read_u8(buf) & 0x1;
582 return !!dirty;
583 }
584
585 static void mark_table_dirty(struct osl_table *t)
586 {
587 char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
588 write_u8(buf, read_u8(buf) | 1);
589 }
590
591 static void mark_table_clean(struct osl_table *t)
592 {
593 char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
594 write_u8(buf, read_u8(buf) & 0xfe);
595 }
596
597 static void unmap_column(struct osl_table *t, unsigned col_num)
598 {
599 struct osl_object map = t->columns[col_num].data_map;
600 int ret;
601 if (!map.data)
602 return;
603 ret = osl_munmap(map.data, map.size);
604 assert(ret > 0);
605 map.data = NULL;
606 }
607
608 /**
609 * Unmap all mapped files of an osl table.
610 *
611 * \param t Pointer to a mapped table.
612 * \param flags Options for unmapping.
613 *
614 * \return Positive on success, negative on errors.
615 *
616 * \sa map_table(), enum osl_close_flags, osl_munmap().
617 */
618 int unmap_table(struct osl_table *t, enum osl_close_flags flags)
619 {
620 unsigned i;
621 const struct osl_column_description *cd;
622 int ret;
623
624 if (!t->num_mapped_columns) /* can this ever happen? */
625 return 1;
626 INFO_LOG("unmapping table '%s'\n", t->desc->name);
627 if (!t->index_map.data)
628 return -E_OSL_NOT_MAPPED;
629 if (flags & OSL_MARK_CLEAN)
630 mark_table_clean(t);
631 ret = osl_munmap(t->index_map.data, t->index_map.size);
632 if (ret < 0)
633 return ret;
634 t->index_map.data = NULL;
635 if (!t->num_rows)
636 return 1;
637 FOR_EACH_MAPPED_COLUMN(i, t, cd)
638 unmap_column(t, i);
639 return 1;
640 }
641
642 static int map_column(struct osl_table *t, unsigned col_num)
643 {
644 struct stat statbuf;
645 char *filename = column_filename(t, col_num);
646 int ret;
647
648 if (!filename)
649 return -E_OSL_NOMEM;
650 ret = osl_stat(filename, &statbuf);
651 if (ret < 0) {
652 free(filename);
653 return ret;
654 }
655 if (!(S_IFREG & statbuf.st_mode)) {
656 free(filename);
657 return ret;
658 }
659 ret = mmap_full_file(filename, O_RDWR,
660 &t->columns[col_num].data_map.data,
661 &t->columns[col_num].data_map.size,
662 NULL);
663 free(filename);
664 return ret;
665 }
666
667 /**
668 * Map the index file and all columns of type \p OSL_MAPPED_STORAGE into memory.
669 *
670 * \param t Pointer to an initialized table structure.
671 * \param flags Mapping options.
672 *
673 * \return Negative return value on errors; on success the number of rows
674 * (including invalid rows) is returned.
675 *
676 * \sa unmap_table(), enum map_table_flags, osl_open_table(), mmap(2).
677 */
678 int map_table(struct osl_table *t, enum map_table_flags flags)
679 {
680 char *filename;
681 const struct osl_column_description *cd;
682 int i = 0, ret, num_rows = 0;
683
684 if (!t->num_mapped_columns)
685 return 0;
686 if (t->index_map.data)
687 return -E_OSL_ALREADY_MAPPED;
688 filename = index_filename(t->desc);
689 if (!filename)
690 return -E_OSL_NOMEM;
691 INFO_LOG("mapping table '%s' (index: %s)\n", t->desc->name, filename);
692 ret = mmap_full_file(filename, flags & MAP_TBL_FL_MAP_RDONLY?
693 O_RDONLY : O_RDWR, &t->index_map.data, &t->index_map.size, NULL);
694 free(filename);
695 if (ret < 0)
696 return ret;
697 if (flags & MAP_TBL_FL_VERIFY_INDEX) {
698 ret = compare_table_descriptions(t);
699 if (ret < 0)
700 goto err;
701 }
702 ret = -E_OSL_BUSY;
703 if (!(flags & MAP_TBL_FL_IGNORE_DIRTY)) {
704 if (table_is_dirty(t)) {
705 ERROR_LOG("%s is dirty\n", t->desc->name);
706 goto err;
707 }
708 }
709 mark_table_dirty(t);
710 num_rows = table_num_rows(t);
711 if (!num_rows)
712 return num_rows;
713 /* map data files */
714 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
715 ret = map_column(t, i);
716 if (ret < 0)
717 goto err;
718 }
719 return num_rows;
720 err: /* unmap what is already mapped */
721 for (i--; i >= 0; i--) {
722 struct osl_object map = t->columns[i].data_map;
723 osl_munmap(map.data, map.size);
724 map.data = NULL;
725 }
726 osl_munmap(t->index_map.data, t->index_map.size);
727 t->index_map.data = NULL;
728 return ret;
729 }
730
731 /**
732 * Retrieve a mapped object by row and column number.
733 *
734 * \param t Pointer to an open osl table.
735 * \param col_num Number of the mapped column containing the object to retrieve.
736 * \param row_num Number of the row containing the object to retrieve.
737 * \param obj The result is returned here.
738 *
739 * It is considered an error if \a col_num does not refer to a column
740 * of storage type \p OSL_MAPPED_STORAGE.
741 *
742 * \return Standard.
743 *
744 * \sa osl_storage_type.
745 */
746 int get_mapped_object(const struct osl_table *t, unsigned col_num,
747 uint32_t row_num, struct osl_object *obj)
748 {
749 struct osl_column *col = &t->columns[col_num];
750 uint32_t offset;
751 char *cell_index;
752 int ret;
753
754 if (t->num_rows <= row_num)
755 return -E_OSL_BAD_ROW_NUM;
756 ret = get_cell_index(t, row_num, col_num, &cell_index);
757 if (ret < 0)
758 return ret;
759 offset = read_u32(cell_index);
760 obj->size = read_u32(cell_index + 4);
761 obj->data = col->data_map.data + offset;
762 return 1;
763 }
764
765 /*
766 * It's OK to call this with result = rb_node = NULL. If result is not NULL,
767 * and rb key was not found, result points to the parent node.
768 */
769 static int search_rbtree(const struct osl_object *obj,
770 const struct osl_table *t, unsigned col_num,
771 struct rb_node **result, struct rb_node ***rb_link)
772 {
773 struct osl_column *col = &t->columns[col_num];
774 struct rb_node **new = &col->rbtree.rb_node, *parent = NULL;
775 const struct osl_column_description *cd =
776 get_column_description(t->desc, col_num);
777 enum osl_storage_type st = cd->storage_type;
778 while (*new) {
779 struct osl_row *this_row = get_row_pointer(*new,
780 col->rbtree_num);
781 int ret;
782 struct osl_object this_obj;
783 parent = *new;
784 if (st == OSL_MAPPED_STORAGE) {
785 ret = get_mapped_object(t, col_num, this_row->num,
786 &this_obj);
787 if (ret < 0)
788 return ret;
789 } else
790 this_obj = this_row->volatile_objects[col->volatile_num];
791 ret = cd->compare_function(obj, &this_obj);
792 if (!ret) {
793 if (result)
794 *result = get_rb_node_pointer(this_row,
795 col->rbtree_num);
796 return 1;
797 }
798 if (ret < 0)
799 new = &((*new)->rb_left);
800 else
801 new = &((*new)->rb_right);
802 }
803 if (result)
804 *result = parent;
805 if (rb_link)
806 *rb_link = new;
807 return -E_OSL_RB_KEY_NOT_FOUND;
808 }
809
810 static int insert_rbtree(struct osl_table *t, unsigned col_num,
811 const struct osl_row *row, const struct osl_object *obj)
812 {
813 struct rb_node *parent, **rb_link;
814 unsigned rbtree_num;
815 struct rb_node *n;
816 int ret = search_rbtree(obj, t, col_num, &parent, &rb_link);
817
818 if (ret > 0)
819 return -E_OSL_RB_KEY_EXISTS;
820 rbtree_num = t->columns[col_num].rbtree_num;
821 n = get_rb_node_pointer(row, rbtree_num);
822 rb_link_node(n, parent, rb_link);
823 rb_insert_color(n, &t->columns[col_num].rbtree);
824 return 1;
825 }
826
827 static void remove_rb_node(struct osl_table *t, unsigned col_num,
828 const struct osl_row *row)
829 {
830 struct osl_column *col = &t->columns[col_num];
831 const struct osl_column_description *cd =
832 get_column_description(t->desc, col_num);
833 enum osl_storage_flags sf = cd->storage_flags;
834 struct rb_node *victim, *splice_out_node, *tmp;
835 if (!(sf & OSL_RBTREE))
836 return;
837 /*
838 * Which node is removed/spliced out actually depends on how many
839 * children the victim node has: If it has no children, it gets
840 * deleted. If it has one child, it gets spliced out. If it has two
841 * children, its successor (which has at most a right child) gets
842 * spliced out.
843 */
844 victim = get_rb_node_pointer(row, col->rbtree_num);
845 if (victim->rb_left && victim->rb_right)
846 splice_out_node = rb_next(victim);
847 else
848 splice_out_node = victim;
849 /* Go up to the root and decrement the size of each node in the path. */
850 for (tmp = splice_out_node; tmp; tmp = rb_parent(tmp))
851 tmp->size--;
852 rb_erase(victim, &col->rbtree);
853 }
854
855 static int add_row_to_rbtrees(struct osl_table *t, uint32_t row_num,
856 struct osl_object *volatile_objs, struct osl_row **row_ptr)
857 {
858 unsigned i;
859 int ret;
860 struct osl_row *row = allocate_row(t->num_rbtrees);
861 const struct osl_column_description *cd;
862
863 if (!row)
864 return -E_OSL_NOMEM;
865 row->num = row_num;
866 row->volatile_objects = volatile_objs;
867 FOR_EACH_RBTREE_COLUMN(i, t, cd) {
868 if (cd->storage_type == OSL_MAPPED_STORAGE) {
869 struct osl_object obj;
870 ret = get_mapped_object(t, i, row_num, &obj);
871 if (ret < 0)
872 goto err;
873 ret = insert_rbtree(t, i, row, &obj);
874 } else { /* volatile */
875 const struct osl_object *obj
876 = volatile_objs + t->columns[i].volatile_num;
877 ret = insert_rbtree(t, i, row, obj);
878 }
879 if (ret < 0)
880 goto err;
881 }
882 if (row_ptr)
883 *row_ptr = row;
884 return 1;
885 err: /* rollback changes, i.e. remove added entries from rbtrees */
886 while (i)
887 remove_rb_node(t, i--, row);
888 free(row);
889 return ret;
890 }
891
892 static void free_volatile_objects(const struct osl_table *t,
893 enum osl_close_flags flags)
894 {
895 int i, j;
896 struct rb_node *n;
897 struct osl_column *rb_col;
898 const struct osl_column_description *cd;
899
900 if (!t->num_volatile_columns)
901 return;
902 /* find the first rbtree column (any will do) */
903 FOR_EACH_RBTREE_COLUMN(i, t, cd)
904 break;
905 rb_col = t->columns + i;
906 /* walk that rbtree and free all volatile objects */
907 for (n = rb_first(&rb_col->rbtree); n; n = rb_next(n)) {
908 struct osl_row *r = get_row_pointer(n, rb_col->rbtree_num);
909 if (flags & OSL_FREE_VOLATILE)
910 FOR_EACH_VOLATILE_COLUMN(j, t, cd) {
911 if (cd->storage_flags & OSL_DONT_FREE)
912 continue;
913 free(r->volatile_objects[
914 t->columns[j].volatile_num].data);
915 }
916 // for (j = 0; j < t->num_volatile_columns; j++)
917 // free(r->volatile_objects[j].data);
918 free(r->volatile_objects);
919 }
920 }
921
922 /**
923 * Erase all rbtree nodes and free resources.
924 *
925 * \param t Pointer to an open osl table.
926 *
927 * This function is called by osl_close_table().
928 */
929 void clear_rbtrees(struct osl_table *t)
930 {
931 const struct osl_column_description *cd;
932 unsigned i, rbtrees_cleared = 0;
933
934 FOR_EACH_RBTREE_COLUMN(i, t, cd) {
935 struct osl_column *col = &t->columns[i];
936 struct rb_node *n;
937 rbtrees_cleared++;
938 for (n = rb_first(&col->rbtree); n;) {
939 struct osl_row *r;
940 rb_erase(n, &col->rbtree);
941 if (rbtrees_cleared == t->num_rbtrees) {
942 r = get_row_pointer(n, col->rbtree_num);
943 n = rb_next(n);
944 free(r);
945 } else
946 n = rb_next(n);
947 }
948 }
949
950 }
951
952 __export int osl_close_table(struct osl_table *t, enum osl_close_flags flags)
953 {
954 int ret;
955
956 if (!t)
957 return -E_OSL_BAD_TABLE;
958 NOTICE_LOG("closing table %s\n", t->desc->name);
959 free_volatile_objects(t, flags);
960 clear_rbtrees(t);
961 ret = unmap_table(t, flags);
962 if (ret < 0)
963 ERROR_LOG("unmap_table failed: %d\n", ret);
964 free(t->columns);
965 free(t);
966 return ret;
967 }
968
969 /**
970 * Find out whether the given row number corresponds to an invalid row.
971 *
972 * \param t Pointer to the osl table.
973 * \param row_num The number of the row in question.
974 *
975 * By definition, a row is considered invalid if all its index entries
976 * are invalid.
977 *
978 * \return Positive if \a row_num corresponds to an invalid row,
979 * zero if it corresponds to a valid row, negative on errors.
980 */
981 int row_is_invalid(struct osl_table *t, uint32_t row_num)
982 {
983 char *row_index;
984 int i, ret = get_row_index(t, row_num, &row_index);
985
986 if (ret < 0)
987 return ret;
988 for (i = 0; i < t->row_index_size; i++) {
989 if ((unsigned char)row_index[i] != 0xff)
990 return 0;
991 }
992 INFO_LOG("row %d is invalid\n", row_num);
993 return 1;
994 }
995
996 /**
997 * Invalidate a row of an osl table.
998 *
999 * \param t Pointer to an open osl table.
1000 * \param row_num Number of the row to mark as invalid.
1001 *
1002 * This function marks each mapped object in the index entry of \a row as
1003 * invalid.
1004 *
1005 * \return Standard.
1006 */
1007 int mark_row_invalid(struct osl_table *t, uint32_t row_num)
1008 {
1009 char *row_index;
1010 int ret = get_row_index(t, row_num, &row_index);
1011
1012 if (ret < 0)
1013 return ret;
1014 INFO_LOG("marking row %d as invalid\n", row_num);
1015 memset(row_index, 0xff, t->row_index_size);
1016 return 1;
1017 }
1018
1019 /**
1020 * Initialize all rbtrees and compute number of invalid rows.
1021 *
1022 * \param t The table containing the rbtrees to be initialized.
1023 *
1024 * \return Standard.
1025 */
1026 int init_rbtrees(struct osl_table *t)
1027 {
1028 int i, ret;
1029 const struct osl_column_description *cd;
1030
1031 /* create rbtrees */
1032 FOR_EACH_RBTREE_COLUMN(i, t, cd)
1033 t->columns[i].rbtree = RB_ROOT;
1034 /* add valid rows to rbtrees */
1035 t->num_invalid_rows = 0;
1036 for (i = 0; i < t->num_rows; i++) {
1037 ret = row_is_invalid(t, i);
1038 if (ret < 0)
1039 return ret;
1040 if (ret) {
1041 t->num_invalid_rows++;
1042 continue;
1043 }
1044 ret = add_row_to_rbtrees(t, i, NULL, NULL);
1045 if (ret < 0)
1046 return ret;
1047 }
1048 return 1;
1049 }
1050
1051 __export int osl_open_table(const struct osl_table_description *table_desc,
1052 struct osl_table **result)
1053 {
1054 int i, ret;
1055 struct osl_table *t;
1056 const struct osl_column_description *cd;
1057
1058 NOTICE_LOG("opening table %s\n", table_desc->name);
1059 ret = init_table_structure(table_desc, &t);
1060 if (ret < 0)
1061 return ret;
1062 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
1063 struct stat statbuf;
1064 char *dirname = column_filename(t, i);
1065
1066 ret = -E_OSL_NOMEM;
1067 if (!dirname)
1068 goto err;
1069 /* check if directory exists */
1070 ret = osl_stat(dirname, &statbuf);
1071 free(dirname);
1072 if (ret < 0)
1073 goto err;
1074 ret = -E_OSL_NOTDIR;
1075 if (!S_ISDIR(statbuf.st_mode))
1076 goto err;
1077 }
1078 ret = map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1079 if (ret < 0)
1080 goto err;
1081 t->num_rows = ret;
1082 DEBUG_LOG("num rows: %d\n", t->num_rows);
1083 ret = init_rbtrees(t);
1084 if (ret < 0) {
1085 osl_close_table(t, OSL_MARK_CLEAN); /* ignore further errors */
1086 return ret;
1087 }
1088 *result = t;
1089 return 1;
1090 err:
1091 free(t->columns);
1092 free(t);
1093 return ret;
1094 }
1095
1096 static int create_disk_storage_object_dir(const struct osl_table *t,
1097 unsigned col_num, const char *ds_name)
1098 {
1099 char *dirname;
1100 int ret;
1101
1102 if (!(t->desc->flags & OSL_LARGE_TABLE))
1103 return 1;
1104 dirname = disk_storage_dirname(t, col_num, ds_name);
1105 if (!dirname)
1106 return -E_OSL_NOMEM;
1107 ret = osl_mkdir(dirname, 0777);
1108 free(dirname);
1109 if (ret < 0 && ret != -E_OSL_DIR_EXISTS)
1110 return ret;
1111 return 1;
1112 }
1113
1114 static int write_disk_storage_file(const struct osl_table *t, unsigned col_num,
1115 const struct osl_object *obj, const char *ds_name)
1116 {
1117 int ret;
1118 char *filename;
1119
1120 ret = create_disk_storage_object_dir(t, col_num, ds_name);
1121 if (ret < 0)
1122 return ret;
1123 filename = disk_storage_path(t, col_num, ds_name);
1124 if (!filename)
1125 return -E_OSL_NOMEM;
1126 ret = write_file(filename, obj->data, obj->size);
1127 free(filename);
1128 return ret;
1129 }
1130
1131 static int append_map_file(const struct osl_table *t, unsigned col_num,
1132 const struct osl_object *obj, uint32_t *new_size)
1133 {
1134 char *filename = column_filename(t, col_num);
1135 int ret;
1136
1137 if (!filename)
1138 return -E_OSL_NOMEM;
1139 ret = append_file(filename, obj->data, obj->size, new_size);
1140 free(filename);
1141 return ret;
1142 }
1143
1144 static int append_row_index(const struct osl_table *t, char *row_index)
1145 {
1146 char *filename;
1147 int ret;
1148
1149 if (!t->num_mapped_columns)
1150 return 1;
1151 filename = index_filename(t->desc);
1152 if (!filename)
1153 return -E_OSL_NOMEM;
1154 ret = append_file(filename, row_index, t->row_index_size, NULL);
1155 free(filename);
1156 return ret;
1157 }
1158
1159 static int truncate_mapped_file(const struct osl_table *t, unsigned col_num,
1160 off_t size)
1161 {
1162 int ret;
1163 char *filename = column_filename(t, col_num);
1164
1165 if (!filename)
1166 return -E_OSL_NOMEM;
1167 ret = truncate_file(filename, size);
1168 free(filename);
1169 return ret;
1170 }
1171
1172 static int delete_disk_storage_file(const struct osl_table *t, unsigned col_num,
1173 const char *ds_name)
1174 {
1175 char *dirname, *filename = disk_storage_path(t, col_num, ds_name);
1176 int ret = 1;
1177
1178 if (!filename)
1179 return -E_OSL_NOMEM;
1180 if (unlink(filename) < 0)
1181 ret = errno == ENOENT? -E_OSL_NOENT : -E_OSL_UNLINK;
1182 free(filename);
1183 if (ret < 0)
1184 return ret;
1185 if (!(t->desc->flags & OSL_LARGE_TABLE))
1186 return 1;
1187 dirname = disk_storage_dirname(t, col_num, ds_name);
1188 if (!dirname)
1189 return -E_OSL_NOMEM;
1190 rmdir(dirname);
1191 free(dirname);
1192 return 1;
1193 }
1194
1195 __export int osl_add_and_get_row(struct osl_table *t, struct osl_object *objects,
1196 struct osl_row **row)
1197 {
1198 int i, ret;
1199 char *ds_name = NULL;
1200 struct rb_node **rb_parents = NULL, ***rb_links = NULL;
1201 char *new_row_index = NULL;
1202 struct osl_object *volatile_objs = NULL;
1203 const struct osl_column_description *cd;
1204
1205 if (!t)
1206 return -E_OSL_BAD_TABLE;
1207 rb_parents = malloc(t->num_rbtrees * sizeof(struct rn_node*));
1208 if (!rb_parents)
1209 return -E_OSL_NOMEM;
1210 rb_links = malloc(t->num_rbtrees * sizeof(struct rn_node**));
1211 if (!rb_links) {
1212 free(rb_parents);
1213 return -E_OSL_NOMEM;
1214 }
1215 if (t->num_mapped_columns) {
1216 new_row_index = malloc(t->row_index_size);
1217 if (!new_row_index) {
1218 free(rb_links);
1219 free(rb_parents);
1220 return -E_OSL_NOMEM;
1221 }
1222 }
1223 /* pass 1: sanity checks */
1224 // DEBUG_LOG("sanity tests: %p:%p\n", objects[0].data,
1225 // objects[1].data);
1226 FOR_EACH_COLUMN(i, t->desc, cd) {
1227 enum osl_storage_type st = cd->storage_type;
1228 enum osl_storage_flags sf = cd->storage_flags;
1229
1230 // ret = -E_OSL_NULL_OBJECT;
1231 // if (!objects[i])
1232 // goto out;
1233 if (st == OSL_DISK_STORAGE)
1234 continue;
1235 if (sf & OSL_RBTREE) {
1236 unsigned rbtree_num = t->columns[i].rbtree_num;
1237 ret = -E_OSL_RB_KEY_EXISTS;
1238 // DEBUG_LOG("checking whether %p exists\n",
1239 // objects[i].data);
1240 if (search_rbtree(objects + i, t, i,
1241 &rb_parents[rbtree_num],
1242 &rb_links[rbtree_num]) > 0)
1243 goto out;
1244 }
1245 if (sf & OSL_FIXED_SIZE) {
1246 // DEBUG_LOG("fixed size. need: %zu, have: %d\n",
1247 // objects[i].size, cd->data_size);
1248 ret = -E_OSL_BAD_DATA_SIZE;
1249 if (objects[i].size != cd->data_size)
1250 goto out;
1251 }
1252 }
1253 if (t->num_disk_storage_columns) {
1254 ds_name = disk_storage_name_of_object(t,
1255 &objects[t->disk_storage_name_column]);
1256 ret = -E_OSL_NOMEM;
1257 if (!ds_name)
1258 goto out;
1259 }
1260 ret = unmap_table(t, OSL_MARK_CLEAN);
1261 if (ret < 0)
1262 goto out;
1263 // DEBUG_LOG("sanity tests passed%s\n", "");
1264 /* pass 2: create data files, append map data */
1265 FOR_EACH_COLUMN(i, t->desc, cd) {
1266 enum osl_storage_type st = cd->storage_type;
1267 if (st == OSL_NO_STORAGE)
1268 continue;
1269 if (st == OSL_MAPPED_STORAGE) {
1270 uint32_t new_size;
1271 struct osl_column *col = &t->columns[i];
1272 // DEBUG_LOG("appending object of size %zu\n",
1273 // objects[i].size);
1274 ret = append_map_file(t, i, objects + i, &new_size);
1275 if (ret < 0)
1276 goto rollback;
1277 update_cell_index(new_row_index, col, new_size,
1278 objects[i].size);
1279 continue;
1280 }
1281 /* DISK_STORAGE */
1282 ret = write_disk_storage_file(t, i, objects + i, ds_name);
1283 if (ret < 0)
1284 goto rollback;
1285 }
1286 ret = append_row_index(t, new_row_index);
1287 if (ret < 0)
1288 goto rollback;
1289 ret = map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1290 if (ret < 0) { /* truncate index and rollback changes */
1291 char *filename = index_filename(t->desc);
1292 if (filename)
1293 truncate_file(filename, t->row_index_size);
1294 free(filename);
1295 goto rollback;
1296 }
1297 /* pass 3: add entry to rbtrees */
1298 if (t->num_volatile_columns) {
1299 ret = -E_OSL_NOMEM;
1300 volatile_objs = calloc(t->num_volatile_columns,
1301 sizeof(struct osl_object));
1302 if (!volatile_objs)
1303 goto out;
1304 FOR_EACH_VOLATILE_COLUMN(i, t, cd)
1305 volatile_objs[t->columns[i].volatile_num] = objects[i];
1306 }
1307 t->num_rows++;
1308 // DEBUG_LOG("adding new entry as row #%d\n", t->num_rows - 1);
1309 ret = add_row_to_rbtrees(t, t->num_rows - 1, volatile_objs, row);
1310 if (ret < 0)
1311 goto out;
1312 // DEBUG_LOG("added new entry as row #%d\n", t->num_rows - 1);
1313 ret = 1;
1314 goto out;
1315 rollback: /* rollback all changes made, ignore further errors */
1316 for (i--; i >= 0; i--) {
1317 cd = get_column_description(t->desc, i);
1318 enum osl_storage_type st = cd->storage_type;
1319 if (st == OSL_NO_STORAGE)
1320 continue;
1321
1322 if (st == OSL_MAPPED_STORAGE)
1323 truncate_mapped_file(t, i, objects[i].size);
1324 else /* disk storage */
1325 delete_disk_storage_file(t, i, ds_name);
1326 }
1327 /* ignore error and return previous error value */
1328 map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1329 out:
1330 free(new_row_index);
1331 free(ds_name);
1332 free(rb_parents);
1333 free(rb_links);
1334 return ret;
1335 }
1336
1337 __export int osl_add_row(struct osl_table *t, struct osl_object *objects)
1338 {
1339 return osl_add_and_get_row(t, objects, NULL);
1340 }
1341
1342 __export int osl_get_object(const struct osl_table *t, const struct osl_row *r,
1343 unsigned col_num, struct osl_object *object)
1344 {
1345 const struct osl_column_description *cd;
1346
1347 if (!t)
1348 return -E_OSL_BAD_TABLE;
1349 cd = get_column_description(t->desc, col_num);
1350 /* col must not be disk storage */
1351 if (cd->storage_type == OSL_DISK_STORAGE)
1352 return -E_OSL_BAD_STORAGE_TYPE;
1353 if (cd->storage_type == OSL_MAPPED_STORAGE)
1354 return get_mapped_object(t, col_num, r->num, object);
1355 /* volatile */
1356 *object = r->volatile_objects[t->columns[col_num].volatile_num];
1357 return 1;
1358 }
1359
1360 __export int osl_del_row(struct osl_table *t, struct osl_row *row)
1361 {
1362 struct osl_row *r = row;
1363 int i, ret;
1364 const struct osl_column_description *cd;
1365
1366 if (!t)
1367 return -E_OSL_BAD_TABLE;
1368 INFO_LOG("deleting row %p\n", row);
1369
1370 if (t->num_disk_storage_columns) {
1371 char *ds_name;
1372 ret = disk_storage_name_of_row(t, r, &ds_name);
1373 if (ret < 0)
1374 goto out;
1375 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd)
1376 delete_disk_storage_file(t, i, ds_name);
1377 free(ds_name);
1378 }
1379 FOR_EACH_COLUMN(i, t->desc, cd) {
1380 struct osl_column *col = t->columns + i;
1381 enum osl_storage_type st = cd->storage_type;
1382 remove_rb_node(t, i, r);
1383 if (st == OSL_MAPPED_STORAGE)
1384 continue;
1385 if (st == OSL_NO_STORAGE && !(cd->storage_flags & OSL_DONT_FREE))
1386 free(r->volatile_objects[col->volatile_num].data);
1387 }
1388 if (t->num_mapped_columns) {
1389 ret = mark_row_invalid(t, r->num);
1390 if (ret < 0)
1391 goto out;
1392 t->num_invalid_rows++;
1393 } else
1394 t->num_rows--;
1395 ret = 1;
1396 out:
1397 free(r->volatile_objects);
1398 free(r);
1399 return ret;
1400 }
1401
1402 /* test if column has an rbtree */
1403 static int check_rbtree_col(const struct osl_table *t, unsigned col_num,
1404 struct osl_column **col)
1405 {
1406 if (!t)
1407 return -E_OSL_BAD_TABLE;
1408 if (!(get_column_description(t->desc, col_num)->storage_flags & OSL_RBTREE))
1409 return -E_OSL_BAD_STORAGE_FLAGS;
1410 *col = t->columns + col_num;
1411 return 1;
1412 }
1413
1414 __export int osl_get_row(const struct osl_table *t, unsigned col_num,
1415 const struct osl_object *obj, struct osl_row **result)
1416 {
1417 int ret;
1418 struct rb_node *node;
1419 struct osl_row *row;
1420 struct osl_column *col;
1421
1422 *result = NULL;
1423 ret = check_rbtree_col(t, col_num, &col);
1424 if (ret < 0)
1425 return ret;
1426 ret = search_rbtree(obj, t, col_num, &node, NULL);
1427 if (ret < 0)
1428 return ret;
1429 row = get_row_pointer(node, t->columns[col_num].rbtree_num);
1430 *result = row;
1431 return 1;
1432 }
1433
1434 static int rbtree_loop(struct osl_column *col, void *private_data,
1435 osl_rbtree_loop_func *func)
1436 {
1437 struct rb_node *n, *tmp;
1438
1439 /* this for-loop is safe against removal of an entry */
1440 for (n = rb_first(&col->rbtree), tmp = n? rb_next(n) : NULL;
1441 n;
1442 n = tmp, tmp = tmp? rb_next(tmp) : NULL) {
1443 struct osl_row *r = get_row_pointer(n, col->rbtree_num);
1444 if (func(r, private_data) < 0)
1445 return -E_OSL_LOOP;
1446 }
1447 return 1;
1448 }
1449
1450 static int rbtree_loop_reverse(struct osl_column *col, void *private_data,
1451 osl_rbtree_loop_func *func)
1452 {
1453 struct rb_node *n, *tmp;
1454
1455 /* safe against removal of an entry */
1456 for (n = rb_last(&col->rbtree), tmp = n? rb_prev(n) : NULL;
1457 n;
1458 n = tmp, tmp = tmp? rb_prev(tmp) : NULL) {
1459 struct osl_row *r = get_row_pointer(n, col->rbtree_num);
1460 if (func(r, private_data) < 0)
1461 return -E_OSL_LOOP;
1462 }
1463 return 1;
1464 }
1465
1466 __export int osl_rbtree_loop(const struct osl_table *t, unsigned col_num,
1467 void *private_data, osl_rbtree_loop_func *func)
1468 {
1469 struct osl_column *col;
1470
1471 int ret = check_rbtree_col(t, col_num, &col);
1472 if (ret < 0)
1473 return ret;
1474 return rbtree_loop(col, private_data, func);
1475 }
1476
1477 __export int osl_rbtree_loop_reverse(const struct osl_table *t, unsigned col_num,
1478 void *private_data, osl_rbtree_loop_func *func)
1479 {
1480 struct osl_column *col;
1481
1482 int ret = check_rbtree_col(t, col_num, &col);
1483 if (ret < 0)
1484 return ret;
1485 return rbtree_loop_reverse(col, private_data, func);
1486 }
1487
1488 /* TODO: Rollback changes on errors */
1489 static int rename_disk_storage_objects(struct osl_table *t,
1490 struct osl_object *old_obj, struct osl_object *new_obj)
1491 {
1492 int i, ret;
1493 const struct osl_column_description *cd;
1494 char *old_ds_name, *new_ds_name;
1495
1496 if (!t->num_disk_storage_columns)
1497 return 1; /* nothing to do */
1498 if (old_obj->size == new_obj->size && !memcmp(new_obj->data,
1499 old_obj->data, new_obj->size))
1500 return 1; /* object did not change */
1501 old_ds_name = disk_storage_name_of_object(t, old_obj);
1502 new_ds_name = disk_storage_name_of_object(t, new_obj);
1503 ret = -E_OSL_NOMEM;
1504 if (!old_ds_name || ! new_ds_name)
1505 goto out;
1506
1507 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
1508 char *old_filename, *new_filename;
1509 ret = create_disk_storage_object_dir(t, i, new_ds_name);
1510 if (ret < 0)
1511 goto out;
1512 old_filename = disk_storage_path(t, i, old_ds_name);
1513 new_filename = disk_storage_path(t, i, new_ds_name);
1514 if (!old_filename || !new_filename)
1515 ret = -E_OSL_NOMEM;
1516 else
1517 ret = osl_rename(old_filename, new_filename);
1518 free(old_filename);
1519 free(new_filename);
1520 if (ret < 0)
1521 goto out;
1522 }
1523 ret = 1;
1524 out:
1525 free(old_ds_name);
1526 free(new_ds_name);
1527 return ret;
1528
1529 }
1530
1531 __export int osl_update_object(struct osl_table *t, const struct osl_row *r,
1532 unsigned col_num, struct osl_object *obj)
1533 {
1534 struct osl_column *col;
1535 const struct osl_column_description *cd;
1536 int ret;
1537
1538 if (!t)
1539 return -E_OSL_BAD_TABLE;
1540 col = &t->columns[col_num];
1541 cd = get_column_description(t->desc, col_num);
1542 DEBUG_LOG("updating column %u of %s\n", col_num, t->desc->name);
1543 if (cd->storage_flags & OSL_RBTREE) {
1544 if (search_rbtree(obj, t, col_num, NULL, NULL) > 0)
1545 return -E_OSL_RB_KEY_EXISTS;
1546 }
1547 if (cd->storage_flags & OSL_FIXED_SIZE) {
1548 if (obj->size != cd->data_size)
1549 return -E_OSL_BAD_DATA_SIZE;
1550 }
1551 remove_rb_node(t, col_num, r);
1552 if (cd->storage_type == OSL_NO_STORAGE) { /* TODO: If fixed size, reuse object? */
1553 free(r->volatile_objects[col->volatile_num].data);
1554 r->volatile_objects[col->volatile_num] = *obj;
1555 } else if (cd->storage_type == OSL_DISK_STORAGE) {
1556 char *ds_name;
1557 ret = disk_storage_name_of_row(t, r, &ds_name);
1558 if (ret < 0)
1559 return ret;
1560 ret = delete_disk_storage_file(t, col_num, ds_name);
1561 if (ret < 0 && ret != -E_OSL_NOENT) {
1562 free(ds_name);
1563 return ret;
1564 }
1565 ret = write_disk_storage_file(t, col_num, obj, ds_name);
1566 free(ds_name);
1567 if (ret < 0)
1568 return ret;
1569 } else { /* mapped storage */
1570 struct osl_object old_obj;
1571 ret = get_mapped_object(t, col_num, r->num, &old_obj);
1572 if (ret < 0)
1573 return ret;
1574 /*
1575 * If the updated column is the disk storage name column, the
1576 * disk storage name changes, so we have to rename all disk
1577 * storage objects accordingly.
1578 */
1579 if (col_num == t->disk_storage_name_column) {
1580 ret = rename_disk_storage_objects(t, &old_obj, obj);
1581 if (ret < 0)
1582 return ret;
1583 }
1584 if (cd->storage_flags & OSL_FIXED_SIZE)
1585 memcpy(old_obj.data, obj->data, cd->data_size);
1586 else { /* TODO: if the size doesn't change, use old space */
1587 uint32_t new_data_map_size;
1588 char *row_index;
1589 ret = get_row_index(t, r->num, &row_index);
1590 if (ret < 0)
1591 return ret;
1592 unmap_column(t, col_num);
1593 ret = append_map_file(t, col_num, obj,
1594 &new_data_map_size);
1595 if (ret < 0)
1596 return ret;
1597 ret = map_column(t, col_num);
1598 if (ret < 0)
1599 return ret;
1600 update_cell_index(row_index, col, new_data_map_size,
1601 obj->size);
1602 }
1603 }
1604 if (cd->storage_flags & OSL_RBTREE) {
1605 ret = insert_rbtree(t, col_num, r, obj);
1606 if (ret < 0)
1607 return ret;
1608 }
1609 return 1;
1610 }
1611
1612 __export int osl_open_disk_object(const struct osl_table *t, const struct osl_row *r,
1613 unsigned col_num, struct osl_object *obj)
1614 {
1615 const struct osl_column_description *cd;
1616 char *ds_name, *filename;
1617 int ret;
1618
1619 if (!t)
1620 return -E_OSL_BAD_TABLE;
1621 cd = get_column_description(t->desc, col_num);
1622 if (cd->storage_type != OSL_DISK_STORAGE)
1623 return -E_OSL_BAD_STORAGE_TYPE;
1624
1625 ret = disk_storage_name_of_row(t, r, &ds_name);
1626 if (ret < 0)
1627 return ret;
1628 filename = disk_storage_path(t, col_num, ds_name);
1629 free(ds_name);
1630 if (!filename)
1631 return -E_OSL_NOMEM;
1632 DEBUG_LOG("filename: %s\n", filename);
1633 ret = mmap_full_file(filename, O_RDONLY, &obj->data, &obj->size, NULL);
1634 free(filename);
1635 return ret;
1636 }
1637
1638 __export int osl_close_disk_object(struct osl_object *obj)
1639 {
1640 return osl_munmap(obj->data, obj->size);
1641 }
1642
1643 __export int osl_get_num_rows(const struct osl_table *t, unsigned *num_rows)
1644 {
1645 if (!t)
1646 return -E_OSL_BAD_TABLE;
1647 assert(t->num_rows >= t->num_invalid_rows);
1648 *num_rows = t->num_rows - t->num_invalid_rows;
1649 return 1;
1650 }
1651
1652 __export int osl_get_rank(const struct osl_table *t, struct osl_row *r,
1653 unsigned col_num, unsigned *rank)
1654 {
1655 struct osl_object obj;
1656 struct osl_column *col;
1657 struct rb_node *node;
1658 int ret = check_rbtree_col(t, col_num, &col);
1659
1660 if (ret < 0)
1661 return ret;
1662 ret = osl_get_object(t, r, col_num, &obj);
1663 if (ret < 0)
1664 return ret;
1665 ret = search_rbtree(&obj, t, col_num, &node, NULL);
1666 if (ret < 0)
1667 return ret;
1668 ret = rb_rank(node, rank);
1669 if (ret < 0)
1670 return -E_OSL_BAD_ROW;
1671 return 1;
1672 }
1673
1674 __export int osl_get_nth_row(const struct osl_table *t, unsigned col_num,
1675 unsigned n, struct osl_row **result)
1676 {
1677 struct osl_column *col;
1678 struct rb_node *node;
1679 unsigned num_rows;
1680 int ret;
1681
1682 if (n == 0)
1683 return -E_OSL_RB_KEY_NOT_FOUND;
1684 ret = osl_get_num_rows(t, &num_rows);
1685 if (ret < 0)
1686 return ret;
1687 if (n > num_rows)
1688 return -E_OSL_RB_KEY_NOT_FOUND;
1689 ret = check_rbtree_col(t, col_num, &col);
1690 if (ret < 0)
1691 return ret;
1692 node = rb_nth(col->rbtree.rb_node, n);
1693 if (!node)
1694 return -E_OSL_RB_KEY_NOT_FOUND;
1695 *result = get_row_pointer(node, col->rbtree_num);
1696 return 1;
1697 }
1698
1699 __export int osl_rbtree_first_row(const struct osl_table *t, unsigned col_num,
1700 struct osl_row **result)
1701 {
1702 return osl_get_nth_row(t, col_num, 1, result);
1703 }
1704
1705 __export int osl_rbtree_last_row(const struct osl_table *t, unsigned col_num,
1706 struct osl_row **result)
1707 {
1708 unsigned num_rows;
1709 int ret = osl_get_num_rows(t, &num_rows);
1710
1711 if (ret < 0)
1712 return ret;
1713 return osl_get_nth_row(t, col_num, num_rows, result);
1714 }