2 * Copyright (C) 2007-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file osl.c Object storage layer functions. */
8 #include <dirent.h> /* readdir() */
17 /* Taken from Drepper: How to write shared libraries, Appendix B. */
19 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
20 #define MSGSTRFIELD1(line) str##line
21 static const union msgstr_t {
23 #define OSL_ERROR(n, s) char MSGSTRFIELD(__LINE__)[sizeof(s)];
29 #define OSL_ERROR(n, s) s,
33 static const unsigned int errmsgidx[] = {
34 #define OSL_ERROR(n, s) [n] = offsetof(union msgstr_t, MSGSTRFIELD(__LINE__)),
39 __export const char *osl_strerror(int num)
41 if (IS_SYSTEM_ERROR(num))
42 return strerror((num) & ((1 << SYSTEM_ERROR_BIT) - 1));
43 return msgstr.str + errmsgidx[num];
48 static void __attribute ((constructor)) init_loglevel(void)
50 char *p = getenv("OSL_LOGLEVEL");
52 /* don't log anything if unset */
53 loglevel = p? atoi(p) : EMERG + 1;
60 * \param fmt Usual format string.
62 * All XXX_LOG() macros use this function.
64 __printf_2_3 void __log(int ll, const char* fmt,...)
77 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
78 fprintf(outfd, "%s ", str);
80 vfprintf(outfd, fmt, argp);
85 * A wrapper for lseek(2).
87 * \param fd The file descriptor whose offset is to be to repositioned.
88 * \param offset A value-result parameter.
89 * \param whence Usual repositioning directive.
91 * Reposition the offset of the file descriptor \a fd to the argument \a offset
92 * according to the directive \a whence. Upon successful return, \a offset
93 * contains the resulting offset location as measured in bytes from the
94 * beginning of the file.
96 * \return Positive on success. Otherwise, the function returns \p -E_OSL_LSEEK.
100 static int __lseek(int fd, off_t *offset, int whence)
102 *offset = lseek(fd, *offset, whence);
103 int ret = -E_OSL_LSEEK;
109 static int append_file(const char *filename, char *data, size_t data_size,
114 // DEBUG_LOG("appending %zu + %zu bytes\n", header_size, data_size);
115 ret = osl_open(filename, O_WRONLY | O_CREAT | O_APPEND, 0644);
119 ret = write_all(fd, data, &data_size);
124 ret = __lseek(fd, &offset, SEEK_END);
127 // DEBUG_LOG("new file size: " FMT_OFF_T "\n", offset);
136 static int verify_name(const char *name)
139 return -E_OSL_BAD_NAME;
141 return -E_OSL_BAD_NAME;
142 if (strchr(name, '/'))
143 return -E_OSL_BAD_NAME;
144 if (!strcmp(name, ".."))
145 return -E_OSL_BAD_NAME;
146 if (!strcmp(name, "."))
147 return -E_OSL_BAD_NAME;
151 int osl_hash_compare(const struct osl_object *obj1, const struct osl_object *obj2)
153 return hash_compare((HASH_TYPE *)obj1->data, (HASH_TYPE *)obj2->data);
156 static char *disk_storage_dirname(const struct osl_table *t, unsigned col_num,
159 char *dirname, *column_name = column_filename(t, col_num);
163 if (!(t->desc->flags & OSL_LARGE_TABLE))
165 dirname = make_message("%s/%.2s", column_name, ds_name);
170 static char *disk_storage_name_of_object(const struct osl_table *t,
171 const struct osl_object *obj)
173 HASH_TYPE hash[HASH_SIZE];
174 hash_object(obj, hash);
175 return disk_storage_name_of_hash(t, hash);
178 static int disk_storage_name_of_row(const struct osl_table *t,
179 const struct osl_row *row, char **name)
181 struct osl_object obj;
182 int ret = osl_get_object(t, row, t->disk_storage_name_column, &obj);
186 *name = disk_storage_name_of_object(t, &obj);
189 return -ERRNO_TO_ERROR(ENOMEM);
192 static void column_name_hash(const char *col_name, HASH_TYPE *hash)
194 hash_function(col_name, strlen(col_name), hash);
197 static int init_column_descriptions(struct osl_table *t)
200 const struct osl_column_description *cd;
202 ret = -E_OSL_BAD_TABLE_DESC;
203 ret = verify_name(t->desc->name);
206 ret = -E_OSL_BAD_DB_DIR;
207 if (!t->desc->dir && (t->num_disk_storage_columns || t->num_mapped_columns))
209 /* the size of the index header without column descriptions */
210 t->index_header_size = IDX_COLUMN_DESCRIPTIONS;
211 FOR_EACH_COLUMN(i, t->desc, cd) {
212 struct osl_column *col = t->columns + i;
213 if (cd->storage_flags & OSL_RBTREE) {
214 if (!cd->compare_function)
215 return -E_OSL_NO_COMPARE_FUNC;
217 if (cd->storage_type == OSL_NO_STORAGE)
219 ret = -E_OSL_NO_COLUMN_NAME;
220 if (!cd->name || !cd->name[0])
222 ret = verify_name(cd->name);
225 t->index_header_size += index_column_description_size(cd->name);
226 column_name_hash(cd->name, col->name_hash);
227 ret = -E_OSL_DUPLICATE_COL_NAME;
228 for (j = i + 1; j < t->desc->num_columns; j++) {
229 const char *name2 = get_column_description(t->desc,
231 if (cd->name && name2 && !strcmp(cd->name, name2))
241 * Initialize a struct table from given table description.
243 * \param desc The description of the osl table.
244 * \param table_ptr Result is returned here.
246 * This function performs several sanity checks on \p desc and returns if any
247 * of these tests fail. On success, a struct \p osl_table is allocated and
248 * initialized with data derived from \p desc.
252 * \sa struct osl_table.
254 int init_table_structure(const struct osl_table_description *desc,
255 struct osl_table **table_ptr)
257 const struct osl_column_description *cd;
258 struct osl_table *t = calloc(1, sizeof(*t));
259 int i, ret = -ERRNO_TO_ERROR(ENOMEM), have_disk_storage_name_column = 0;
263 ret = -E_OSL_BAD_TABLE_DESC;
266 DEBUG_LOG("creating table structure for '%s' from table "
267 "description\n", desc->name);
268 ret = -E_OSL_NO_COLUMN_DESC;
269 if (!desc->column_descriptions)
271 ret = -E_OSL_NO_COLUMNS;
272 if (!desc->num_columns)
274 ret = -ERRNO_TO_ERROR(ENOMEM);
275 t->columns = calloc(desc->num_columns, sizeof(struct osl_column));
279 FOR_EACH_COLUMN(i, t->desc, cd) {
280 enum osl_storage_type st = cd->storage_type;
281 enum osl_storage_flags sf = cd->storage_flags;
282 struct osl_column *col = &t->columns[i];
284 ret = -E_OSL_BAD_STORAGE_TYPE;
285 if (st != OSL_MAPPED_STORAGE && st != OSL_DISK_STORAGE
286 && st != OSL_NO_STORAGE)
288 ret = -E_OSL_BAD_STORAGE_FLAGS;
289 if (st == OSL_DISK_STORAGE && sf & OSL_RBTREE)
291 ret = -E_OSL_BAD_STORAGE_SIZE;
292 if (sf & OSL_FIXED_SIZE && !cd->data_size)
295 case OSL_DISK_STORAGE:
296 t->num_disk_storage_columns++;
298 case OSL_MAPPED_STORAGE:
299 t->num_mapped_columns++;
300 col->index_offset = t->row_index_size;
301 t->row_index_size += 8;
304 col->volatile_num = t->num_volatile_columns;
305 t->num_volatile_columns++;
308 if (sf & OSL_RBTREE) {
309 col->rbtree_num = t->num_rbtrees;
311 if ((sf & OSL_UNIQUE) && (st == OSL_MAPPED_STORAGE)) {
312 if (!have_disk_storage_name_column)
313 t->disk_storage_name_column = i;
314 have_disk_storage_name_column = 1;
318 ret = -E_OSL_NO_UNIQUE_RBTREE_COLUMN;
319 if (t->num_disk_storage_columns && !have_disk_storage_name_column)
321 ret = -E_OSL_NO_RBTREE_COL;
325 DEBUG_LOG("OK. Index entry size: %u\n", t->row_index_size);
326 ret = init_column_descriptions(t);
338 * Read the table description from index header.
340 * \param map The memory mapping of the index file.
341 * \param desc The values found in the index header are returned here.
343 * Read the index header, check for the paraslash magic string and the table version number.
344 * Read all information stored in the index header into \a desc.
348 * \sa struct osl_table_description, osl_create_table.
350 int read_table_desc(struct osl_object *map, struct osl_table_description *desc)
352 char *buf = map->data;
354 uint16_t header_size;
357 struct osl_column_description *cd;
359 if (map->size < MIN_INDEX_HEADER_SIZE(1))
360 return -E_OSL_SHORT_TABLE;
361 if (strncmp(buf + IDX_PARA_MAGIC, PARA_MAGIC, strlen(PARA_MAGIC)))
362 return -E_OSL_NO_MAGIC;
363 version = read_u8(buf + IDX_VERSION);
364 if (version < MIN_TABLE_VERSION || version > MAX_TABLE_VERSION)
365 return -E_OSL_VERSION_MISMATCH;
366 desc->flags = read_u8(buf + IDX_TABLE_FLAGS);
367 desc->num_columns = read_u16(buf + IDX_NUM_COLUMNS);
368 INFO_LOG("%u columns\n", desc->num_columns);
369 if (!desc->num_columns)
370 return -E_OSL_NO_COLUMNS;
371 header_size = read_u16(buf + IDX_HEADER_SIZE);
372 if (map->size < header_size)
373 return -E_OSL_BAD_SIZE;
374 desc->column_descriptions = calloc(desc->num_columns,
375 sizeof(struct osl_column_description));
376 if (!desc->column_descriptions)
377 return -ERRNO_TO_ERROR(ENOMEM);
378 offset = IDX_COLUMN_DESCRIPTIONS;
379 FOR_EACH_COLUMN(i, desc, cd) {
382 ret = -E_OSL_SHORT_TABLE;
383 if (map->size < offset + MIN_IDX_COLUMN_DESCRIPTION_SIZE) {
384 ERROR_LOG("map size = %zu < %u = offset + min desc size\n",
385 map->size, offset + MIN_IDX_COLUMN_DESCRIPTION_SIZE);
388 cd->storage_type = read_u16(buf + offset + IDX_CD_STORAGE_TYPE);
389 cd->storage_flags = read_u16(buf + offset +
390 IDX_CD_STORAGE_FLAGS);
391 cd->data_size = read_u32(buf + offset + IDX_CD_DATA_SIZE);
392 null_byte = memchr(buf + offset + IDX_CD_NAME, '\0',
393 map->size - offset - IDX_CD_NAME);
394 ret = -E_OSL_INDEX_CORRUPTION;
397 ret = -ERRNO_TO_ERROR(ENOMEM);
398 cd->name = strdup(buf + offset + IDX_CD_NAME);
401 offset += index_column_description_size(cd->name);
403 if (offset != header_size) {
404 ret = -E_OSL_INDEX_CORRUPTION;
405 ERROR_LOG("real header size = %u != %u = stored header size\n",
406 offset, header_size);
411 FOR_EACH_COLUMN(i, desc, cd)
417 * check whether the table description given by \p t->desc matches the on-disk
418 * table structure stored in the index of \a t.
420 static int compare_table_descriptions(struct osl_table *t)
423 struct osl_table_description desc;
424 const struct osl_column_description *cd1, *cd2;
426 /* read the on-disk structure into desc */
427 ret = read_table_desc(&t->index_map, &desc);
430 ret = -E_OSL_BAD_TABLE_FLAGS;
431 if (desc.flags != t->desc->flags)
433 ret = -E_OSL_BAD_COLUMN_NUM;
434 if (desc.num_columns > t->desc->num_columns)
436 if (desc.num_columns < t->desc->num_columns) {
437 struct osl_column_description *cd;
438 unsigned diff = t->desc->num_columns - desc.num_columns;
439 INFO_LOG("extending table by %u volatile columns\n", diff);
440 ret = -ERRNO_TO_ERROR(ENOMEM);
441 desc.column_descriptions = realloc(desc.column_descriptions,
442 t->desc->num_columns * sizeof(struct osl_column_description));
443 if (!desc.column_descriptions)
445 for (i = desc.num_columns; i < t->desc->num_columns; i++) {
446 cd = get_column_description(&desc, i);
447 cd->storage_type = OSL_NO_STORAGE;
450 desc.num_columns += diff;
452 FOR_EACH_COLUMN(i, t->desc, cd1) {
453 cd2 = get_column_description(&desc, i);
454 ret = -E_OSL_BAD_STORAGE_TYPE;
455 if (cd1->storage_type != cd2->storage_type)
457 if (cd1->storage_type == OSL_NO_STORAGE)
459 ret = -E_OSL_BAD_STORAGE_FLAGS;
460 if (cd1->storage_flags != cd2->storage_flags) {
461 ERROR_LOG("sf1 = %u != %u = sf2\n",
462 cd1->storage_flags, cd2->storage_flags);
465 ret = -E_OSL_BAD_DATA_SIZE;
466 if (cd1->storage_flags & OSL_FIXED_SIZE)
467 if (cd1->data_size != cd2->data_size)
469 ret = -E_OSL_BAD_COLUMN_NAME;
470 if (strcmp(cd1->name, cd2->name))
473 INFO_LOG("table description of '%s' matches on-disk data, good\n",
477 FOR_EACH_COLUMN(i, &desc, cd1)
479 free(desc.column_descriptions);
483 static int create_table_index(struct osl_table *t)
485 char *buf, *filename;
487 size_t size = t->index_header_size;
488 const struct osl_column_description *cd;
491 INFO_LOG("creating %zu byte index for table %s\n", size,
493 buf = calloc(1, size);
495 return -ERRNO_TO_ERROR(ENOMEM);
496 sprintf(buf + IDX_PARA_MAGIC, "%s", PARA_MAGIC);
497 write_u8(buf + IDX_TABLE_FLAGS, t->desc->flags);
498 write_u8(buf + IDX_DIRTY_FLAG, 0);
499 write_u8(buf + IDX_VERSION, CURRENT_TABLE_VERSION);
500 write_u16(buf + IDX_NUM_COLUMNS, t->num_mapped_columns + t->num_disk_storage_columns);
501 write_u16(buf + IDX_HEADER_SIZE, t->index_header_size);
502 offset = IDX_COLUMN_DESCRIPTIONS;
503 FOR_EACH_COLUMN(i, t->desc, cd) {
504 /* no need to store info about volatile storage */
505 if (cd->storage_type == OSL_NO_STORAGE)
507 write_u16(buf + offset + IDX_CD_STORAGE_TYPE,
509 write_u16(buf + offset + IDX_CD_STORAGE_FLAGS,
511 if (cd->storage_flags & OSL_FIXED_SIZE)
512 write_u32(buf + offset + IDX_CD_DATA_SIZE,
514 strcpy(buf + offset + IDX_CD_NAME, cd->name);
515 offset += index_column_description_size(cd->name);
517 assert(offset = size);
518 filename = index_filename(t->desc);
520 ret = write_file(filename, buf, size);
522 ret = -ERRNO_TO_ERROR(ENOMEM);
528 __export int osl_create_table(const struct osl_table_description *desc)
530 const struct osl_column_description *cd;
531 char *table_dir = NULL, *filename;
533 int i, ret = init_table_structure(desc, &t);
537 INFO_LOG("creating %s\n", desc->name);
538 FOR_EACH_COLUMN(i, t->desc, cd) {
539 if (cd->storage_type == OSL_NO_STORAGE)
542 ret = osl_mkdir(desc->dir, 0777);
543 if (ret < 0 && !is_errno(-ret, EEXIST))
545 table_dir = make_message("%s/%s", desc->dir,
547 ret = -ERRNO_TO_ERROR(ENOMEM);
550 ret = osl_mkdir(table_dir, 0777);
554 ret = -ERRNO_TO_ERROR(ENOMEM);
555 filename = column_filename(t, i);
558 INFO_LOG("filename: %s\n", filename);
559 if (cd->storage_type == OSL_MAPPED_STORAGE) {
560 ret = osl_open(filename, O_RDWR | O_CREAT | O_EXCL,
569 ret = osl_mkdir(filename, 0777);
574 if (t->num_mapped_columns) {
575 ret = create_table_index(t);
587 static int table_is_dirty(struct osl_table *t)
589 char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
590 uint8_t dirty = read_u8(buf) & 0x1;
594 static void mark_table_dirty(struct osl_table *t)
596 char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
597 write_u8(buf, read_u8(buf) | 1);
600 static void mark_table_clean(struct osl_table *t)
602 char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
603 write_u8(buf, read_u8(buf) & 0xfe);
606 static void unmap_column(struct osl_table *t, unsigned col_num)
608 struct osl_object map = t->columns[col_num].data_map;
612 ret = osl_munmap(map.data, map.size);
618 * Unmap all mapped files of an osl table.
620 * \param t Pointer to a mapped table.
621 * \param flags Options for unmapping.
623 * \return Positive on success, negative on errors.
625 * \sa map_table(), enum osl_close_flags, osl_munmap().
627 int unmap_table(struct osl_table *t, enum osl_close_flags flags)
630 const struct osl_column_description *cd;
633 if (!t->num_mapped_columns) /* can this ever happen? */
635 INFO_LOG("unmapping table '%s'\n", t->desc->name);
636 if (!t->index_map.data)
637 return -E_OSL_NOT_MAPPED;
638 if (flags & OSL_MARK_CLEAN)
640 ret = osl_munmap(t->index_map.data, t->index_map.size);
643 t->index_map.data = NULL;
646 FOR_EACH_MAPPED_COLUMN(i, t, cd)
651 static int map_column(struct osl_table *t, unsigned col_num)
654 char *filename = column_filename(t, col_num);
658 return -ERRNO_TO_ERROR(ENOMEM);
659 ret = osl_stat(filename, &statbuf);
664 if (!(S_IFREG & statbuf.st_mode)) {
668 ret = mmap_full_file(filename, O_RDWR,
669 &t->columns[col_num].data_map.data,
670 &t->columns[col_num].data_map.size,
677 * Map the index file and all columns of type \p OSL_MAPPED_STORAGE into memory.
679 * \param t Pointer to an initialized table structure.
680 * \param flags Mapping options.
682 * \return Negative return value on errors; on success the number of rows
683 * (including invalid rows) is returned.
685 * \sa unmap_table(), enum map_table_flags, osl_open_table(), mmap(2).
687 int map_table(struct osl_table *t, enum map_table_flags flags)
690 const struct osl_column_description *cd;
691 int i = 0, ret, num_rows = 0;
693 if (!t->num_mapped_columns)
695 if (t->index_map.data)
696 return -E_OSL_ALREADY_MAPPED;
697 filename = index_filename(t->desc);
699 return -ERRNO_TO_ERROR(ENOMEM);
700 INFO_LOG("mapping table '%s' (index: %s)\n", t->desc->name, filename);
701 ret = mmap_full_file(filename, flags & MAP_TBL_FL_MAP_RDONLY?
702 O_RDONLY : O_RDWR, &t->index_map.data, &t->index_map.size, NULL);
706 if (flags & MAP_TBL_FL_VERIFY_INDEX) {
707 ret = compare_table_descriptions(t);
712 if (!(flags & MAP_TBL_FL_IGNORE_DIRTY)) {
713 if (table_is_dirty(t)) {
714 ERROR_LOG("%s is dirty\n", t->desc->name);
719 num_rows = table_num_rows(t);
723 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
724 ret = map_column(t, i);
729 err: /* unmap what is already mapped */
730 for (i--; i >= 0; i--) {
731 struct osl_object map = t->columns[i].data_map;
732 osl_munmap(map.data, map.size);
735 osl_munmap(t->index_map.data, t->index_map.size);
736 t->index_map.data = NULL;
741 * Retrieve a mapped object by row and column number.
743 * \param t Pointer to an open osl table.
744 * \param col_num Number of the mapped column containing the object to retrieve.
745 * \param row_num Number of the row containing the object to retrieve.
746 * \param obj The result is returned here.
748 * It is considered an error if \a col_num does not refer to a column
749 * of storage type \p OSL_MAPPED_STORAGE.
753 * \sa osl_storage_type.
755 int get_mapped_object(const struct osl_table *t, unsigned col_num,
756 uint32_t row_num, struct osl_object *obj)
758 struct osl_column *col = &t->columns[col_num];
763 if (t->num_rows <= row_num)
764 return -E_OSL_BAD_ROW_NUM;
765 ret = get_cell_index(t, row_num, col_num, &cell_index);
768 offset = read_u32(cell_index);
769 obj->size = read_u32(cell_index + 4);
770 obj->data = col->data_map.data + offset;
775 * It's OK to call this with result = rb_node = NULL. If result is not NULL,
776 * and rb key was not found, result points to the parent node.
778 static int search_rbtree(const struct osl_object *obj,
779 const struct osl_table *t, unsigned col_num,
780 struct rb_node **result, struct rb_node ***rb_link)
782 struct osl_column *col = &t->columns[col_num];
783 struct rb_node **new = &col->rbtree.rb_node, *parent = NULL;
784 const struct osl_column_description *cd =
785 get_column_description(t->desc, col_num);
786 enum osl_storage_type st = cd->storage_type;
788 struct osl_row *this_row = get_row_pointer(*new,
791 struct osl_object this_obj;
793 if (st == OSL_MAPPED_STORAGE) {
794 ret = get_mapped_object(t, col_num, this_row->num,
799 this_obj = this_row->volatile_objects[col->volatile_num];
800 ret = cd->compare_function(obj, &this_obj);
803 *result = get_rb_node_pointer(this_row,
808 new = &((*new)->rb_left);
810 new = &((*new)->rb_right);
816 return -E_OSL_RB_KEY_NOT_FOUND;
819 static int insert_rbtree(struct osl_table *t, unsigned col_num,
820 const struct osl_row *row, const struct osl_object *obj)
822 struct rb_node *parent, **rb_link;
825 int ret = search_rbtree(obj, t, col_num, &parent, &rb_link);
828 return -E_OSL_RB_KEY_EXISTS;
829 rbtree_num = t->columns[col_num].rbtree_num;
830 n = get_rb_node_pointer(row, rbtree_num);
831 rb_link_node(n, parent, rb_link);
832 rb_insert_color(n, &t->columns[col_num].rbtree);
836 static void remove_rb_node(struct osl_table *t, unsigned col_num,
837 const struct osl_row *row)
839 struct osl_column *col = &t->columns[col_num];
840 const struct osl_column_description *cd =
841 get_column_description(t->desc, col_num);
842 enum osl_storage_flags sf = cd->storage_flags;
843 struct rb_node *victim, *splice_out_node, *tmp;
844 if (!(sf & OSL_RBTREE))
847 * Which node is removed/spliced out actually depends on how many
848 * children the victim node has: If it has no children, it gets
849 * deleted. If it has one child, it gets spliced out. If it has two
850 * children, its successor (which has at most a right child) gets
853 victim = get_rb_node_pointer(row, col->rbtree_num);
854 if (victim->rb_left && victim->rb_right)
855 splice_out_node = rb_next(victim);
857 splice_out_node = victim;
858 /* Go up to the root and decrement the size of each node in the path. */
859 for (tmp = splice_out_node; tmp; tmp = rb_parent(tmp))
861 rb_erase(victim, &col->rbtree);
864 static int add_row_to_rbtrees(struct osl_table *t, uint32_t row_num,
865 struct osl_object *volatile_objs, struct osl_row **row_ptr)
869 struct osl_row *row = allocate_row(t->num_rbtrees);
870 const struct osl_column_description *cd;
873 return -ERRNO_TO_ERROR(ENOMEM);
875 row->volatile_objects = volatile_objs;
876 FOR_EACH_RBTREE_COLUMN(i, t, cd) {
877 if (cd->storage_type == OSL_MAPPED_STORAGE) {
878 struct osl_object obj;
879 ret = get_mapped_object(t, i, row_num, &obj);
882 ret = insert_rbtree(t, i, row, &obj);
883 } else { /* volatile */
884 const struct osl_object *obj
885 = volatile_objs + t->columns[i].volatile_num;
886 ret = insert_rbtree(t, i, row, obj);
894 err: /* rollback changes, i.e. remove added entries from rbtrees */
896 remove_rb_node(t, i--, row);
901 static void free_volatile_objects(const struct osl_table *t,
902 enum osl_close_flags flags)
906 struct osl_column *rb_col;
907 const struct osl_column_description *cd;
909 if (!t->num_volatile_columns)
911 /* find the first rbtree column (any will do) */
912 FOR_EACH_RBTREE_COLUMN(i, t, cd)
914 rb_col = t->columns + i;
915 /* walk that rbtree and free all volatile objects */
916 for (n = rb_first(&rb_col->rbtree); n; n = rb_next(n)) {
917 struct osl_row *r = get_row_pointer(n, rb_col->rbtree_num);
918 if (flags & OSL_FREE_VOLATILE)
919 FOR_EACH_VOLATILE_COLUMN(j, t, cd) {
920 if (cd->storage_flags & OSL_DONT_FREE)
922 free(r->volatile_objects[
923 t->columns[j].volatile_num].data);
925 // for (j = 0; j < t->num_volatile_columns; j++)
926 // free(r->volatile_objects[j].data);
927 free(r->volatile_objects);
932 * Erase all rbtree nodes and free resources.
934 * \param t Pointer to an open osl table.
936 * This function is called by osl_close_table().
938 void clear_rbtrees(struct osl_table *t)
940 const struct osl_column_description *cd;
941 unsigned i, rbtrees_cleared = 0;
943 FOR_EACH_RBTREE_COLUMN(i, t, cd) {
944 struct osl_column *col = &t->columns[i];
947 for (n = rb_first(&col->rbtree); n;) {
949 rb_erase(n, &col->rbtree);
950 if (rbtrees_cleared == t->num_rbtrees) {
951 r = get_row_pointer(n, col->rbtree_num);
961 __export int osl_close_table(struct osl_table *t, enum osl_close_flags flags)
966 return -E_OSL_BAD_TABLE;
967 NOTICE_LOG("closing table %s\n", t->desc->name);
968 free_volatile_objects(t, flags);
970 ret = unmap_table(t, flags);
972 ERROR_LOG("unmap_table failed: %d\n", ret);
979 * Find out whether the given row number corresponds to an invalid row.
981 * \param t Pointer to the osl table.
982 * \param row_num The number of the row in question.
984 * By definition, a row is considered invalid if all its index entries
987 * \return Positive if \a row_num corresponds to an invalid row,
988 * zero if it corresponds to a valid row, negative on errors.
990 int row_is_invalid(struct osl_table *t, uint32_t row_num)
993 int i, ret = get_row_index(t, row_num, &row_index);
997 for (i = 0; i < t->row_index_size; i++) {
998 if ((unsigned char)row_index[i] != 0xff)
1001 INFO_LOG("row %d is invalid\n", row_num);
1006 * Invalidate a row of an osl table.
1008 * \param t Pointer to an open osl table.
1009 * \param row_num Number of the row to mark as invalid.
1011 * This function marks each mapped object in the index entry of \a row as
1016 int mark_row_invalid(struct osl_table *t, uint32_t row_num)
1019 int ret = get_row_index(t, row_num, &row_index);
1023 INFO_LOG("marking row %d as invalid\n", row_num);
1024 memset(row_index, 0xff, t->row_index_size);
1029 * Initialize all rbtrees and compute number of invalid rows.
1031 * \param t The table containing the rbtrees to be initialized.
1035 int init_rbtrees(struct osl_table *t)
1038 const struct osl_column_description *cd;
1040 /* create rbtrees */
1041 FOR_EACH_RBTREE_COLUMN(i, t, cd)
1042 t->columns[i].rbtree = RB_ROOT;
1043 /* add valid rows to rbtrees */
1044 t->num_invalid_rows = 0;
1045 for (i = 0; i < t->num_rows; i++) {
1046 ret = row_is_invalid(t, i);
1050 t->num_invalid_rows++;
1053 ret = add_row_to_rbtrees(t, i, NULL, NULL);
1060 __export int osl_open_table(const struct osl_table_description *table_desc,
1061 struct osl_table **result)
1064 struct osl_table *t;
1065 const struct osl_column_description *cd;
1067 NOTICE_LOG("opening table %s\n", table_desc->name);
1068 ret = init_table_structure(table_desc, &t);
1071 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
1072 struct stat statbuf;
1073 char *dirname = column_filename(t, i);
1075 ret = -ERRNO_TO_ERROR(ENOMEM);
1078 /* check if directory exists */
1079 ret = stat(dirname, &statbuf);
1082 ret = -ERRNO_TO_ERROR(errno);
1085 ret = -ERRNO_TO_ERROR(ENOTDIR);
1086 if (!S_ISDIR(statbuf.st_mode))
1089 ret = map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1093 DEBUG_LOG("num rows: %d\n", t->num_rows);
1094 ret = init_rbtrees(t);
1096 osl_close_table(t, OSL_MARK_CLEAN); /* ignore further errors */
1107 static int create_disk_storage_object_dir(const struct osl_table *t,
1108 unsigned col_num, const char *ds_name)
1113 if (!(t->desc->flags & OSL_LARGE_TABLE))
1115 dirname = disk_storage_dirname(t, col_num, ds_name);
1117 return -ERRNO_TO_ERROR(ENOMEM);
1118 ret = osl_mkdir(dirname, 0777);
1120 if (ret < 0 && !is_errno(-ret, EEXIST))
1125 static int write_disk_storage_file(const struct osl_table *t, unsigned col_num,
1126 const struct osl_object *obj, const char *ds_name)
1131 ret = create_disk_storage_object_dir(t, col_num, ds_name);
1134 filename = disk_storage_path(t, col_num, ds_name);
1136 return -ERRNO_TO_ERROR(ENOMEM);
1137 ret = write_file(filename, obj->data, obj->size);
1142 static int append_map_file(const struct osl_table *t, unsigned col_num,
1143 const struct osl_object *obj, uint32_t *new_size)
1145 char *filename = column_filename(t, col_num);
1149 return -ERRNO_TO_ERROR(ENOMEM);
1150 ret = append_file(filename, obj->data, obj->size, new_size);
1155 static int append_row_index(const struct osl_table *t, char *row_index)
1160 if (!t->num_mapped_columns)
1162 filename = index_filename(t->desc);
1164 return -ERRNO_TO_ERROR(ENOMEM);
1165 ret = append_file(filename, row_index, t->row_index_size, NULL);
1170 static int truncate_mapped_file(const struct osl_table *t, unsigned col_num,
1174 char *filename = column_filename(t, col_num);
1177 return -ERRNO_TO_ERROR(ENOMEM);
1178 ret = truncate_file(filename, size);
1183 static int delete_disk_storage_file(const struct osl_table *t, unsigned col_num,
1184 const char *ds_name)
1186 char *dirname, *filename = disk_storage_path(t, col_num, ds_name);
1190 return -ERRNO_TO_ERROR(ENOMEM);
1191 ret = unlink(filename);
1195 return -ERRNO_TO_ERROR(err);
1196 if (!(t->desc->flags & OSL_LARGE_TABLE))
1198 dirname = disk_storage_dirname(t, col_num, ds_name);
1200 return -ERRNO_TO_ERROR(ENOMEM);
1206 __export int osl_add_and_get_row(struct osl_table *t, struct osl_object *objects,
1207 struct osl_row **row)
1210 char *ds_name = NULL;
1211 struct rb_node **rb_parents = NULL, ***rb_links = NULL;
1212 char *new_row_index = NULL;
1213 struct osl_object *volatile_objs = NULL;
1214 const struct osl_column_description *cd;
1217 return -E_OSL_BAD_TABLE;
1218 rb_parents = malloc(t->num_rbtrees * sizeof(struct rn_node*));
1220 return -ERRNO_TO_ERROR(ENOMEM);
1221 rb_links = malloc(t->num_rbtrees * sizeof(struct rn_node**));
1224 return -ERRNO_TO_ERROR(ENOMEM);
1226 if (t->num_mapped_columns) {
1227 new_row_index = malloc(t->row_index_size);
1228 if (!new_row_index) {
1231 return -ERRNO_TO_ERROR(ENOMEM);
1234 /* pass 1: sanity checks */
1235 // DEBUG_LOG("sanity tests: %p:%p\n", objects[0].data,
1236 // objects[1].data);
1237 FOR_EACH_COLUMN(i, t->desc, cd) {
1238 enum osl_storage_type st = cd->storage_type;
1239 enum osl_storage_flags sf = cd->storage_flags;
1241 // ret = -E_OSL_NULL_OBJECT;
1244 if (st == OSL_DISK_STORAGE)
1246 if (sf & OSL_RBTREE) {
1247 unsigned rbtree_num = t->columns[i].rbtree_num;
1248 ret = -E_OSL_RB_KEY_EXISTS;
1249 // DEBUG_LOG("checking whether %p exists\n",
1250 // objects[i].data);
1251 if (search_rbtree(objects + i, t, i,
1252 &rb_parents[rbtree_num],
1253 &rb_links[rbtree_num]) > 0)
1256 if (sf & OSL_FIXED_SIZE) {
1257 // DEBUG_LOG("fixed size. need: %zu, have: %d\n",
1258 // objects[i].size, cd->data_size);
1259 ret = -E_OSL_BAD_DATA_SIZE;
1260 if (objects[i].size != cd->data_size)
1264 if (t->num_disk_storage_columns) {
1265 ds_name = disk_storage_name_of_object(t,
1266 &objects[t->disk_storage_name_column]);
1267 ret = -ERRNO_TO_ERROR(ENOMEM);
1271 ret = unmap_table(t, OSL_MARK_CLEAN);
1274 // DEBUG_LOG("sanity tests passed%s\n", "");
1275 /* pass 2: create data files, append map data */
1276 FOR_EACH_COLUMN(i, t->desc, cd) {
1277 enum osl_storage_type st = cd->storage_type;
1278 if (st == OSL_NO_STORAGE)
1280 if (st == OSL_MAPPED_STORAGE) {
1282 struct osl_column *col = &t->columns[i];
1283 // DEBUG_LOG("appending object of size %zu\n",
1284 // objects[i].size);
1285 ret = append_map_file(t, i, objects + i, &new_size);
1288 update_cell_index(new_row_index, col, new_size,
1293 ret = write_disk_storage_file(t, i, objects + i, ds_name);
1297 ret = append_row_index(t, new_row_index);
1300 ret = map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1301 if (ret < 0) { /* truncate index and rollback changes */
1302 char *filename = index_filename(t->desc);
1304 truncate_file(filename, t->row_index_size);
1308 /* pass 3: add entry to rbtrees */
1309 if (t->num_volatile_columns) {
1310 ret = -ERRNO_TO_ERROR(ENOMEM);
1311 volatile_objs = calloc(t->num_volatile_columns,
1312 sizeof(struct osl_object));
1315 FOR_EACH_VOLATILE_COLUMN(i, t, cd)
1316 volatile_objs[t->columns[i].volatile_num] = objects[i];
1319 // DEBUG_LOG("adding new entry as row #%d\n", t->num_rows - 1);
1320 ret = add_row_to_rbtrees(t, t->num_rows - 1, volatile_objs, row);
1323 // DEBUG_LOG("added new entry as row #%d\n", t->num_rows - 1);
1326 rollback: /* rollback all changes made, ignore further errors */
1327 for (i--; i >= 0; i--) {
1328 cd = get_column_description(t->desc, i);
1329 enum osl_storage_type st = cd->storage_type;
1330 if (st == OSL_NO_STORAGE)
1333 if (st == OSL_MAPPED_STORAGE)
1334 truncate_mapped_file(t, i, objects[i].size);
1335 else /* disk storage */
1336 delete_disk_storage_file(t, i, ds_name);
1338 /* ignore error and return previous error value */
1339 map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1341 free(new_row_index);
1348 __export int osl_add_row(struct osl_table *t, struct osl_object *objects)
1350 return osl_add_and_get_row(t, objects, NULL);
1353 __export int osl_get_object(const struct osl_table *t, const struct osl_row *r,
1354 unsigned col_num, struct osl_object *object)
1356 const struct osl_column_description *cd;
1359 return -E_OSL_BAD_TABLE;
1360 cd = get_column_description(t->desc, col_num);
1361 /* col must not be disk storage */
1362 if (cd->storage_type == OSL_DISK_STORAGE)
1363 return -E_OSL_BAD_STORAGE_TYPE;
1364 if (cd->storage_type == OSL_MAPPED_STORAGE)
1365 return get_mapped_object(t, col_num, r->num, object);
1367 *object = r->volatile_objects[t->columns[col_num].volatile_num];
1371 __export int osl_del_row(struct osl_table *t, struct osl_row *row)
1373 struct osl_row *r = row;
1375 const struct osl_column_description *cd;
1378 return -E_OSL_BAD_TABLE;
1379 INFO_LOG("deleting row %p\n", row);
1381 if (t->num_disk_storage_columns) {
1383 ret = disk_storage_name_of_row(t, r, &ds_name);
1386 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd)
1387 delete_disk_storage_file(t, i, ds_name);
1390 FOR_EACH_COLUMN(i, t->desc, cd) {
1391 struct osl_column *col = t->columns + i;
1392 enum osl_storage_type st = cd->storage_type;
1393 remove_rb_node(t, i, r);
1394 if (st == OSL_MAPPED_STORAGE)
1396 if (st == OSL_NO_STORAGE && !(cd->storage_flags & OSL_DONT_FREE))
1397 free(r->volatile_objects[col->volatile_num].data);
1399 if (t->num_mapped_columns) {
1400 ret = mark_row_invalid(t, r->num);
1403 t->num_invalid_rows++;
1408 free(r->volatile_objects);
1413 /* test if column has an rbtree */
1414 static int check_rbtree_col(const struct osl_table *t, unsigned col_num,
1415 struct osl_column **col)
1418 return -E_OSL_BAD_TABLE;
1419 if (!(get_column_description(t->desc, col_num)->storage_flags & OSL_RBTREE))
1420 return -E_OSL_BAD_STORAGE_FLAGS;
1421 *col = t->columns + col_num;
1425 __export int osl_get_row(const struct osl_table *t, unsigned col_num,
1426 const struct osl_object *obj, struct osl_row **result)
1429 struct rb_node *node;
1430 struct osl_row *row;
1431 struct osl_column *col;
1434 ret = check_rbtree_col(t, col_num, &col);
1437 ret = search_rbtree(obj, t, col_num, &node, NULL);
1440 row = get_row_pointer(node, t->columns[col_num].rbtree_num);
1445 static int rbtree_loop(struct osl_column *col, void *private_data,
1446 osl_rbtree_loop_func *func)
1448 struct rb_node *n, *tmp;
1450 /* this for-loop is safe against removal of an entry */
1451 for (n = rb_first(&col->rbtree), tmp = n? rb_next(n) : NULL;
1453 n = tmp, tmp = tmp? rb_next(tmp) : NULL) {
1454 struct osl_row *r = get_row_pointer(n, col->rbtree_num);
1455 if (func(r, private_data) < 0)
1461 static int rbtree_loop_reverse(struct osl_column *col, void *private_data,
1462 osl_rbtree_loop_func *func)
1464 struct rb_node *n, *tmp;
1466 /* safe against removal of an entry */
1467 for (n = rb_last(&col->rbtree), tmp = n? rb_prev(n) : NULL;
1469 n = tmp, tmp = tmp? rb_prev(tmp) : NULL) {
1470 struct osl_row *r = get_row_pointer(n, col->rbtree_num);
1471 if (func(r, private_data) < 0)
1477 __export int osl_rbtree_loop(const struct osl_table *t, unsigned col_num,
1478 void *private_data, osl_rbtree_loop_func *func)
1480 struct osl_column *col;
1482 int ret = check_rbtree_col(t, col_num, &col);
1485 return rbtree_loop(col, private_data, func);
1488 __export int osl_rbtree_loop_reverse(const struct osl_table *t, unsigned col_num,
1489 void *private_data, osl_rbtree_loop_func *func)
1491 struct osl_column *col;
1493 int ret = check_rbtree_col(t, col_num, &col);
1496 return rbtree_loop_reverse(col, private_data, func);
1499 /* TODO: Rollback changes on errors */
1500 static int rename_disk_storage_objects(struct osl_table *t,
1501 struct osl_object *old_obj, struct osl_object *new_obj)
1504 const struct osl_column_description *cd;
1505 char *old_ds_name, *new_ds_name;
1507 if (!t->num_disk_storage_columns)
1508 return 1; /* nothing to do */
1509 if (old_obj->size == new_obj->size && !memcmp(new_obj->data,
1510 old_obj->data, new_obj->size))
1511 return 1; /* object did not change */
1512 old_ds_name = disk_storage_name_of_object(t, old_obj);
1513 new_ds_name = disk_storage_name_of_object(t, new_obj);
1514 ret = -ERRNO_TO_ERROR(ENOMEM);
1515 if (!old_ds_name || ! new_ds_name)
1518 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
1519 char *old_filename, *new_filename;
1520 ret = create_disk_storage_object_dir(t, i, new_ds_name);
1523 old_filename = disk_storage_path(t, i, old_ds_name);
1524 new_filename = disk_storage_path(t, i, new_ds_name);
1525 if (!old_filename || !new_filename)
1526 ret = -ERRNO_TO_ERROR(ENOMEM);
1528 ret = osl_rename(old_filename, new_filename);
1542 __export int osl_update_object(struct osl_table *t, const struct osl_row *r,
1543 unsigned col_num, struct osl_object *obj)
1545 struct osl_column *col;
1546 const struct osl_column_description *cd;
1550 return -E_OSL_BAD_TABLE;
1551 col = &t->columns[col_num];
1552 cd = get_column_description(t->desc, col_num);
1553 DEBUG_LOG("updating column %u of %s\n", col_num, t->desc->name);
1554 if (cd->storage_flags & OSL_RBTREE) {
1555 if (search_rbtree(obj, t, col_num, NULL, NULL) > 0)
1556 return -E_OSL_RB_KEY_EXISTS;
1558 if (cd->storage_flags & OSL_FIXED_SIZE) {
1559 if (obj->size != cd->data_size)
1560 return -E_OSL_BAD_DATA_SIZE;
1562 remove_rb_node(t, col_num, r);
1563 if (cd->storage_type == OSL_NO_STORAGE) { /* TODO: If fixed size, reuse object? */
1564 free(r->volatile_objects[col->volatile_num].data);
1565 r->volatile_objects[col->volatile_num] = *obj;
1566 } else if (cd->storage_type == OSL_DISK_STORAGE) {
1568 ret = disk_storage_name_of_row(t, r, &ds_name);
1571 ret = delete_disk_storage_file(t, col_num, ds_name);
1572 if (ret < 0 && !is_errno(-ret, ENOENT)) {
1576 ret = write_disk_storage_file(t, col_num, obj, ds_name);
1580 } else { /* mapped storage */
1581 struct osl_object old_obj;
1582 ret = get_mapped_object(t, col_num, r->num, &old_obj);
1586 * If the updated column is the disk storage name column, the
1587 * disk storage name changes, so we have to rename all disk
1588 * storage objects accordingly.
1590 if (col_num == t->disk_storage_name_column) {
1591 ret = rename_disk_storage_objects(t, &old_obj, obj);
1595 if (cd->storage_flags & OSL_FIXED_SIZE)
1596 memcpy(old_obj.data, obj->data, cd->data_size);
1597 else { /* TODO: if the size doesn't change, use old space */
1598 uint32_t new_data_map_size;
1600 ret = get_row_index(t, r->num, &row_index);
1603 unmap_column(t, col_num);
1604 ret = append_map_file(t, col_num, obj,
1605 &new_data_map_size);
1608 ret = map_column(t, col_num);
1611 update_cell_index(row_index, col, new_data_map_size,
1615 if (cd->storage_flags & OSL_RBTREE) {
1616 ret = insert_rbtree(t, col_num, r, obj);
1623 __export int osl_open_disk_object(const struct osl_table *t, const struct osl_row *r,
1624 unsigned col_num, struct osl_object *obj)
1626 const struct osl_column_description *cd;
1627 char *ds_name, *filename;
1631 return -E_OSL_BAD_TABLE;
1632 cd = get_column_description(t->desc, col_num);
1633 if (cd->storage_type != OSL_DISK_STORAGE)
1634 return -E_OSL_BAD_STORAGE_TYPE;
1636 ret = disk_storage_name_of_row(t, r, &ds_name);
1639 filename = disk_storage_path(t, col_num, ds_name);
1642 return -ERRNO_TO_ERROR(ENOMEM);
1643 DEBUG_LOG("filename: %s\n", filename);
1644 ret = mmap_full_file(filename, O_RDONLY, &obj->data, &obj->size, NULL);
1649 __export int osl_close_disk_object(struct osl_object *obj)
1651 return osl_munmap(obj->data, obj->size);
1654 __export int osl_get_num_rows(const struct osl_table *t, unsigned *num_rows)
1657 return -E_OSL_BAD_TABLE;
1658 assert(t->num_rows >= t->num_invalid_rows);
1659 *num_rows = t->num_rows - t->num_invalid_rows;
1663 __export int osl_get_rank(const struct osl_table *t, struct osl_row *r,
1664 unsigned col_num, unsigned *rank)
1666 struct osl_object obj;
1667 struct osl_column *col;
1668 struct rb_node *node;
1669 int ret = check_rbtree_col(t, col_num, &col);
1673 ret = osl_get_object(t, r, col_num, &obj);
1676 ret = search_rbtree(&obj, t, col_num, &node, NULL);
1679 ret = rb_rank(node, rank);
1681 return -E_OSL_BAD_ROW;
1685 __export int osl_get_nth_row(const struct osl_table *t, unsigned col_num,
1686 unsigned n, struct osl_row **result)
1688 struct osl_column *col;
1689 struct rb_node *node;
1694 return -E_OSL_RB_KEY_NOT_FOUND;
1695 ret = osl_get_num_rows(t, &num_rows);
1699 return -E_OSL_RB_KEY_NOT_FOUND;
1700 ret = check_rbtree_col(t, col_num, &col);
1703 node = rb_nth(col->rbtree.rb_node, n);
1705 return -E_OSL_RB_KEY_NOT_FOUND;
1706 *result = get_row_pointer(node, col->rbtree_num);
1710 __export int osl_rbtree_first_row(const struct osl_table *t, unsigned col_num,
1711 struct osl_row **result)
1713 return osl_get_nth_row(t, col_num, 1, result);
1716 __export int osl_rbtree_last_row(const struct osl_table *t, unsigned col_num,
1717 struct osl_row **result)
1720 int ret = osl_get_num_rows(t, &num_rows);
1724 return osl_get_nth_row(t, col_num, num_rows, result);