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() */
18 /* Taken from Drepper: How to write shared libraries, Appendix B. */
20 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
21 #define MSGSTRFIELD1(line) str##line
22 static const union msgstr_t {
24 #define _S(n, s) char MSGSTRFIELD(__LINE__)[sizeof(s)];
34 static const unsigned int errmsgidx[] = {
35 #define _S(n, s) [n] = offsetof(union msgstr_t, MSGSTRFIELD(__LINE__)),
40 __export const char *osl_strerror(int num)
42 if (IS_SYSTEM_ERROR(num))
43 return strerror((num) & ((1 << SYSTEM_ERROR_BIT) - 1));
44 return msgstr.str + errmsgidx[num];
49 static void __attribute ((constructor)) init_loglevel(void)
51 char *p = getenv("OSL_LOGLEVEL");
53 /* don't log anything if unset */
54 loglevel = p? atoi(p) : EMERG + 1;
61 * \param fmt Usual format string.
63 * All XXX_LOG() macros use this function.
65 __printf_2_3 void __log(int ll, const char* fmt,...)
78 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
79 fprintf(outfd, "%s ", str);
81 vfprintf(outfd, fmt, argp);
86 * A wrapper for lseek(2).
88 * \param fd The file descriptor whose offset is to be to repositioned.
89 * \param offset A value-result parameter.
90 * \param whence Usual repositioning directive.
92 * Reposition the offset of the file descriptor \a fd to the argument \a offset
93 * according to the directive \a whence. Upon successful return, \a offset
94 * contains the resulting offset location as measured in bytes from the
95 * beginning of the file.
97 * \return Positive on success. Otherwise, the function returns \p -E_OSL_LSEEK.
101 static int __lseek(int fd, off_t *offset, int whence)
103 *offset = lseek(fd, *offset, whence);
104 int ret = -E_OSL_LSEEK;
110 static int append_file(const char *filename, char *header, size_t header_size,
111 char *data, size_t data_size, uint32_t *new_pos)
115 // DEBUG_LOG("appending %zu + %zu bytes\n", header_size, data_size);
116 ret = osl_open(filename, O_WRONLY | O_CREAT | O_APPEND, 0644);
120 if (header && header_size) {
121 ret = write_all(fd, header, &header_size);
125 ret = write_all(fd, data, &data_size);
130 ret = __lseek(fd, &offset, SEEK_END);
133 // DEBUG_LOG("new file size: " FMT_OFF_T "\n", offset);
142 static int verify_name(const char *name)
145 return -E_OSL_BAD_NAME;
147 return -E_OSL_BAD_NAME;
148 if (strchr(name, '/'))
149 return -E_OSL_BAD_NAME;
150 if (!strcmp(name, ".."))
151 return -E_OSL_BAD_NAME;
152 if (!strcmp(name, "."))
153 return -E_OSL_BAD_NAME;
157 int osl_hash_compare(const struct osl_object *obj1, const struct osl_object *obj2)
159 return hash_compare((HASH_TYPE *)obj1->data, (HASH_TYPE *)obj2->data);
162 static char *disk_storage_dirname(const struct osl_table *t, unsigned col_num,
165 char *dirname, *column_name = column_filename(t, col_num);
169 if (!(t->desc->flags & OSL_LARGE_TABLE))
171 dirname = make_message("%s/%.2s", column_name, ds_name);
176 static char *disk_storage_name_of_object(const struct osl_table *t,
177 const struct osl_object *obj)
179 HASH_TYPE hash[HASH_SIZE];
180 hash_object(obj, hash);
181 return disk_storage_name_of_hash(t, hash);
184 static int disk_storage_name_of_row(const struct osl_table *t,
185 const struct osl_row *row, char **name)
187 struct osl_object obj;
188 int ret = osl_get_object(t, row, t->disk_storage_name_column, &obj);
192 *name = disk_storage_name_of_object(t, &obj);
195 return -ERRNO_TO_ERROR(ENOMEM);
198 static void column_name_hash(const char *col_name, HASH_TYPE *hash)
200 hash_function(col_name, strlen(col_name), hash);
203 static int init_column_descriptions(struct osl_table *t)
206 const struct osl_column_description *cd;
208 ret = -E_OSL_BAD_TABLE_DESC;
209 ret = verify_name(t->desc->name);
212 ret = -E_OSL_BAD_DB_DIR;
213 if (!t->desc->dir && (t->num_disk_storage_columns || t->num_mapped_columns))
215 /* the size of the index header without column descriptions */
216 t->index_header_size = IDX_COLUMN_DESCRIPTIONS;
217 FOR_EACH_COLUMN(i, t->desc, cd) {
218 struct osl_column *col = t->columns + i;
219 if (cd->storage_flags & OSL_RBTREE) {
220 if (!cd->compare_function)
221 return -E_OSL_NO_COMPARE_FUNC;
223 if (cd->storage_type == OSL_NO_STORAGE)
225 ret = -E_OSL_NO_COLUMN_NAME;
226 if (!cd->name || !cd->name[0])
228 ret = verify_name(cd->name);
231 t->index_header_size += index_column_description_size(cd->name);
232 column_name_hash(cd->name, col->name_hash);
233 ret = -E_OSL_DUPLICATE_COL_NAME;
234 for (j = i + 1; j < t->desc->num_columns; j++) {
235 const char *name2 = get_column_description(t->desc,
237 if (cd->name && name2 && !strcmp(cd->name, name2))
247 * Initialize a struct table from given table description.
249 * \param desc The description of the osl table.
250 * \param table_ptr Result is returned here.
252 * This function performs several sanity checks on \p desc and returns if any
253 * of these tests fail. On success, a struct \p osl_table is allocated and
254 * initialized with data derived from \p desc.
258 * \sa struct osl_table.
260 int init_table_structure(const struct osl_table_description *desc,
261 struct osl_table **table_ptr)
263 const struct osl_column_description *cd;
264 struct osl_table *t = calloc(1, sizeof(*t));
265 int i, ret = -ERRNO_TO_ERROR(ENOMEM), have_disk_storage_name_column = 0;
269 ret = -E_OSL_BAD_TABLE_DESC;
272 DEBUG_LOG("creating table structure for '%s' from table "
273 "description\n", desc->name);
274 ret = -E_OSL_NO_COLUMN_DESC;
275 if (!desc->column_descriptions)
277 ret = -E_OSL_NO_COLUMNS;
278 if (!desc->num_columns)
280 ret = -ERRNO_TO_ERROR(ENOMEM);
281 t->columns = calloc(desc->num_columns, sizeof(struct osl_column));
285 FOR_EACH_COLUMN(i, t->desc, cd) {
286 enum osl_storage_type st = cd->storage_type;
287 enum osl_storage_flags sf = cd->storage_flags;
288 struct osl_column *col = &t->columns[i];
290 ret = -E_OSL_BAD_STORAGE_TYPE;
291 if (st != OSL_MAPPED_STORAGE && st != OSL_DISK_STORAGE
292 && st != OSL_NO_STORAGE)
294 ret = -E_OSL_BAD_STORAGE_FLAGS;
295 if (st == OSL_DISK_STORAGE && sf & OSL_RBTREE)
297 ret = -E_OSL_BAD_STORAGE_SIZE;
298 if (sf & OSL_FIXED_SIZE && !cd->data_size)
301 case OSL_DISK_STORAGE:
302 t->num_disk_storage_columns++;
304 case OSL_MAPPED_STORAGE:
305 t->num_mapped_columns++;
306 col->index_offset = t->row_index_size;
307 t->row_index_size += 8;
310 col->volatile_num = t->num_volatile_columns;
311 t->num_volatile_columns++;
314 if (sf & OSL_RBTREE) {
315 col->rbtree_num = t->num_rbtrees;
317 if ((sf & OSL_UNIQUE) && (st == OSL_MAPPED_STORAGE)) {
318 if (!have_disk_storage_name_column)
319 t->disk_storage_name_column = i;
320 have_disk_storage_name_column = 1;
324 ret = -E_OSL_NO_UNIQUE_RBTREE_COLUMN;
325 if (t->num_disk_storage_columns && !have_disk_storage_name_column)
327 ret = -E_OSL_NO_RBTREE_COL;
331 DEBUG_LOG("OK. Index entry size: %u\n", t->row_index_size);
332 ret = init_column_descriptions(t);
344 * Read the table description from index header.
346 * \param map The memory mapping of the index file.
347 * \param desc The values found in the index header are returned here.
349 * Read the index header, check for the paraslash magic string and the table version number.
350 * Read all information stored in the index header into \a desc.
354 * \sa struct osl_table_description, osl_create_table.
356 int read_table_desc(struct osl_object *map, struct osl_table_description *desc)
358 char *buf = map->data;
360 uint16_t header_size;
363 struct osl_column_description *cd;
365 if (map->size < MIN_INDEX_HEADER_SIZE(1))
366 return -E_OSL_SHORT_TABLE;
367 if (strncmp(buf + IDX_PARA_MAGIC, PARA_MAGIC, strlen(PARA_MAGIC)))
368 return -E_OSL_NO_MAGIC;
369 version = read_u8(buf + IDX_VERSION);
370 if (version < MIN_TABLE_VERSION || version > MAX_TABLE_VERSION)
371 return -E_OSL_VERSION_MISMATCH;
372 desc->flags = read_u8(buf + IDX_TABLE_FLAGS);
373 desc->num_columns = read_u16(buf + IDX_NUM_COLUMNS);
374 INFO_LOG("%u columns\n", desc->num_columns);
375 if (!desc->num_columns)
376 return -E_OSL_NO_COLUMNS;
377 header_size = read_u16(buf + IDX_HEADER_SIZE);
378 if (map->size < header_size)
379 return -E_OSL_BAD_SIZE;
380 desc->column_descriptions = calloc(desc->num_columns,
381 sizeof(struct osl_column_description));
382 if (!desc->column_descriptions)
383 return -ERRNO_TO_ERROR(ENOMEM);
384 offset = IDX_COLUMN_DESCRIPTIONS;
385 FOR_EACH_COLUMN(i, desc, cd) {
388 ret = -E_OSL_SHORT_TABLE;
389 if (map->size < offset + MIN_IDX_COLUMN_DESCRIPTION_SIZE) {
390 ERROR_LOG("map size = %zu < %u = offset + min desc size\n",
391 map->size, offset + MIN_IDX_COLUMN_DESCRIPTION_SIZE);
394 cd->storage_type = read_u16(buf + offset + IDX_CD_STORAGE_TYPE);
395 cd->storage_flags = read_u16(buf + offset +
396 IDX_CD_STORAGE_FLAGS);
397 cd->data_size = read_u32(buf + offset + IDX_CD_DATA_SIZE);
398 null_byte = memchr(buf + offset + IDX_CD_NAME, '\0',
399 map->size - offset - IDX_CD_NAME);
400 ret = -E_OSL_INDEX_CORRUPTION;
403 ret = -ERRNO_TO_ERROR(ENOMEM);
404 cd->name = strdup(buf + offset + IDX_CD_NAME);
407 offset += index_column_description_size(cd->name);
409 if (offset != header_size) {
410 ret = -E_OSL_INDEX_CORRUPTION;
411 ERROR_LOG("real header size = %u != %u = stored header size\n",
412 offset, header_size);
417 FOR_EACH_COLUMN(i, desc, cd)
423 * check whether the table description given by \p t->desc matches the on-disk
424 * table structure stored in the index of \a t.
426 static int compare_table_descriptions(struct osl_table *t)
429 struct osl_table_description desc;
430 const struct osl_column_description *cd1, *cd2;
432 /* read the on-disk structure into desc */
433 ret = read_table_desc(&t->index_map, &desc);
436 ret = -E_OSL_BAD_TABLE_FLAGS;
437 if (desc.flags != t->desc->flags)
439 ret = -E_OSL_BAD_COLUMN_NUM;
440 if (desc.num_columns > t->desc->num_columns)
442 if (desc.num_columns < t->desc->num_columns) {
443 struct osl_column_description *cd;
444 unsigned diff = t->desc->num_columns - desc.num_columns;
445 INFO_LOG("extending table by %u volatile columns\n", diff);
446 ret = -ERRNO_TO_ERROR(ENOMEM);
447 desc.column_descriptions = realloc(desc.column_descriptions,
448 t->desc->num_columns * sizeof(struct osl_column_description));
449 if (!desc.column_descriptions)
451 for (i = desc.num_columns; i < t->desc->num_columns; i++) {
452 cd = get_column_description(&desc, i);
453 cd->storage_type = OSL_NO_STORAGE;
456 desc.num_columns += diff;
458 FOR_EACH_COLUMN(i, t->desc, cd1) {
459 cd2 = get_column_description(&desc, i);
460 ret = -E_OSL_BAD_STORAGE_TYPE;
461 if (cd1->storage_type != cd2->storage_type)
463 if (cd1->storage_type == OSL_NO_STORAGE)
465 ret = -E_OSL_BAD_STORAGE_FLAGS;
466 if (cd1->storage_flags != cd2->storage_flags) {
467 ERROR_LOG("sf1 = %u != %u = sf2\n",
468 cd1->storage_flags, cd2->storage_flags);
471 ret = -E_OSL_BAD_DATA_SIZE;
472 if (cd1->storage_flags & OSL_FIXED_SIZE)
473 if (cd1->data_size != cd2->data_size)
475 ret = -E_OSL_BAD_COLUMN_NAME;
476 if (strcmp(cd1->name, cd2->name))
479 INFO_LOG("table description of '%s' matches on-disk data, good\n",
483 FOR_EACH_COLUMN(i, &desc, cd1)
485 free(desc.column_descriptions);
489 static int create_table_index(struct osl_table *t)
491 char *buf, *filename;
493 size_t size = t->index_header_size;
494 const struct osl_column_description *cd;
497 INFO_LOG("creating %zu byte index for table %s\n", size,
499 buf = calloc(1, size);
501 return -ERRNO_TO_ERROR(ENOMEM);
502 sprintf(buf + IDX_PARA_MAGIC, "%s", PARA_MAGIC);
503 write_u8(buf + IDX_TABLE_FLAGS, t->desc->flags);
504 write_u8(buf + IDX_DIRTY_FLAG, 0);
505 write_u8(buf + IDX_VERSION, CURRENT_TABLE_VERSION);
506 write_u16(buf + IDX_NUM_COLUMNS, t->num_mapped_columns + t->num_disk_storage_columns);
507 write_u16(buf + IDX_HEADER_SIZE, t->index_header_size);
508 offset = IDX_COLUMN_DESCRIPTIONS;
509 FOR_EACH_COLUMN(i, t->desc, cd) {
510 /* no need to store info about volatile storage */
511 if (cd->storage_type == OSL_NO_STORAGE)
513 write_u16(buf + offset + IDX_CD_STORAGE_TYPE,
515 write_u16(buf + offset + IDX_CD_STORAGE_FLAGS,
517 if (cd->storage_flags & OSL_FIXED_SIZE)
518 write_u32(buf + offset + IDX_CD_DATA_SIZE,
520 strcpy(buf + offset + IDX_CD_NAME, cd->name);
521 offset += index_column_description_size(cd->name);
523 assert(offset = size);
524 filename = index_filename(t->desc);
526 ret = write_file(filename, buf, size);
528 ret = -ERRNO_TO_ERROR(ENOMEM);
534 __export int osl_create_table(const struct osl_table_description *desc)
536 const struct osl_column_description *cd;
537 char *table_dir = NULL, *filename;
539 int i, ret = init_table_structure(desc, &t);
543 INFO_LOG("creating %s\n", desc->name);
544 FOR_EACH_COLUMN(i, t->desc, cd) {
545 if (cd->storage_type == OSL_NO_STORAGE)
548 ret = osl_mkdir(desc->dir, 0777);
549 if (ret < 0 && !is_errno(-ret, EEXIST))
551 table_dir = make_message("%s/%s", desc->dir,
553 ret = -ERRNO_TO_ERROR(ENOMEM);
556 ret = osl_mkdir(table_dir, 0777);
560 ret = -ERRNO_TO_ERROR(ENOMEM);
561 filename = column_filename(t, i);
564 INFO_LOG("filename: %s\n", filename);
565 if (cd->storage_type == OSL_MAPPED_STORAGE) {
566 ret = osl_open(filename, O_RDWR | O_CREAT | O_EXCL,
575 ret = osl_mkdir(filename, 0777);
580 if (t->num_mapped_columns) {
581 ret = create_table_index(t);
593 static int table_is_dirty(struct osl_table *t)
595 char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
596 uint8_t dirty = read_u8(buf) & 0x1;
600 static void mark_table_dirty(struct osl_table *t)
602 char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
603 write_u8(buf, read_u8(buf) | 1);
606 static void mark_table_clean(struct osl_table *t)
608 char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
609 write_u8(buf, read_u8(buf) & 0xfe);
612 static void unmap_column(struct osl_table *t, unsigned col_num)
614 struct osl_object map = t->columns[col_num].data_map;
618 ret = osl_munmap(map.data, map.size);
624 * Unmap all mapped files of an osl table.
626 * \param t Pointer to a mapped table.
627 * \param flags Options for unmapping.
629 * \return Positive on success, negative on errors.
631 * \sa map_table(), enum osl_close_flags, osl_munmap().
633 int unmap_table(struct osl_table *t, enum osl_close_flags flags)
636 const struct osl_column_description *cd;
639 if (!t->num_mapped_columns) /* can this ever happen? */
641 INFO_LOG("unmapping table '%s'\n", t->desc->name);
642 if (!t->index_map.data)
643 return -E_OSL_NOT_MAPPED;
644 if (flags & OSL_MARK_CLEAN)
646 ret = osl_munmap(t->index_map.data, t->index_map.size);
649 t->index_map.data = NULL;
652 FOR_EACH_MAPPED_COLUMN(i, t, cd)
657 static int map_column(struct osl_table *t, unsigned col_num)
660 char *filename = column_filename(t, col_num);
664 return -ERRNO_TO_ERROR(ENOMEM);
665 ret = osl_stat(filename, &statbuf);
670 if (!(S_IFREG & statbuf.st_mode)) {
674 ret = mmap_full_file(filename, O_RDWR,
675 &t->columns[col_num].data_map.data,
676 &t->columns[col_num].data_map.size,
683 * Map the index file and all columns of type \p OSL_MAPPED_STORAGE into memory.
685 * \param t Pointer to an initialized table structure.
686 * \param flags Mapping options.
688 * \return Negative return value on errors; on success the number of rows
689 * (including invalid rows) is returned.
691 * \sa unmap_table(), enum map_table_flags, osl_open_table(), mmap(2).
693 int map_table(struct osl_table *t, enum map_table_flags flags)
696 const struct osl_column_description *cd;
697 int i = 0, ret, num_rows = 0;
699 if (!t->num_mapped_columns)
701 if (t->index_map.data)
702 return -E_OSL_ALREADY_MAPPED;
703 filename = index_filename(t->desc);
705 return -ERRNO_TO_ERROR(ENOMEM);
706 INFO_LOG("mapping table '%s' (index: %s)\n", t->desc->name, filename);
707 ret = mmap_full_file(filename, flags & MAP_TBL_FL_MAP_RDONLY?
708 O_RDONLY : O_RDWR, &t->index_map.data, &t->index_map.size, NULL);
712 if (flags & MAP_TBL_FL_VERIFY_INDEX) {
713 ret = compare_table_descriptions(t);
718 if (!(flags & MAP_TBL_FL_IGNORE_DIRTY)) {
719 if (table_is_dirty(t)) {
720 ERROR_LOG("%s is dirty\n", t->desc->name);
725 num_rows = table_num_rows(t);
729 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
730 ret = map_column(t, i);
735 err: /* unmap what is already mapped */
736 for (i--; i >= 0; i--) {
737 struct osl_object map = t->columns[i].data_map;
738 osl_munmap(map.data, map.size);
741 osl_munmap(t->index_map.data, t->index_map.size);
742 t->index_map.data = NULL;
747 * Retrieve a mapped object by row and column number.
749 * \param t Pointer to an open osl table.
750 * \param col_num Number of the mapped column containing the object to retrieve.
751 * \param row_num Number of the row containing the object to retrieve.
752 * \param obj The result is returned here.
754 * It is considered an error if \a col_num does not refer to a column
755 * of storage type \p OSL_MAPPED_STORAGE.
759 * \sa osl_storage_type.
761 int get_mapped_object(const struct osl_table *t, unsigned col_num,
762 uint32_t row_num, struct osl_object *obj)
764 struct osl_column *col = &t->columns[col_num];
770 if (t->num_rows <= row_num)
771 return -E_OSL_BAD_ROW_NUM;
772 ret = get_cell_index(t, row_num, col_num, &cell_index);
775 offset = read_u32(cell_index);
776 obj->size = read_u32(cell_index + 4) - 1;
777 header = col->data_map.data + offset;
778 obj->data = header + 1;
779 if (read_u8(header) == 0xff) {
780 ERROR_LOG("col %u, size %zu, offset %u\n", col_num,
782 return -E_OSL_INVALID_OBJECT;
787 static int search_rbtree(const struct osl_object *obj,
788 const struct osl_table *t, unsigned col_num,
789 struct rb_node **result, struct rb_node ***rb_link)
791 struct osl_column *col = &t->columns[col_num];
792 struct rb_node **new = &col->rbtree.rb_node, *parent = NULL;
793 const struct osl_column_description *cd =
794 get_column_description(t->desc, col_num);
795 enum osl_storage_type st = cd->storage_type;
797 struct osl_row *this_row = get_row_pointer(*new,
800 struct osl_object this_obj;
802 if (st == OSL_MAPPED_STORAGE) {
803 ret = get_mapped_object(t, col_num, this_row->num,
808 this_obj = this_row->volatile_objects[col->volatile_num];
809 ret = cd->compare_function(obj, &this_obj);
812 *result = get_rb_node_pointer(this_row,
817 new = &((*new)->rb_left);
819 new = &((*new)->rb_right);
825 return -E_OSL_RB_KEY_NOT_FOUND;
828 static int insert_rbtree(struct osl_table *t, unsigned col_num,
829 const struct osl_row *row, const struct osl_object *obj)
831 struct rb_node *parent, **rb_link;
834 int ret = search_rbtree(obj, t, col_num, &parent, &rb_link);
837 return -E_OSL_RB_KEY_EXISTS;
838 rbtree_num = t->columns[col_num].rbtree_num;
839 n = get_rb_node_pointer(row, rbtree_num);
840 rb_link_node(n, parent, rb_link);
841 rb_insert_color(n, &t->columns[col_num].rbtree);
845 static void remove_rb_node(struct osl_table *t, unsigned col_num,
846 const struct osl_row *row)
848 struct osl_column *col = &t->columns[col_num];
849 const struct osl_column_description *cd =
850 get_column_description(t->desc, col_num);
851 enum osl_storage_flags sf = cd->storage_flags;
852 struct rb_node *victim, *splice_out_node, *tmp;
853 if (!(sf & OSL_RBTREE))
856 * Which node is removed/spliced out actually depends on how many
857 * children the victim node has: If it has no children, it gets
858 * deleted. If it has one child, it gets spliced out. If it has two
859 * children, its successor (which has at most a right child) gets
862 victim = get_rb_node_pointer(row, col->rbtree_num);
863 if (victim->rb_left && victim->rb_right)
864 splice_out_node = rb_next(victim);
866 splice_out_node = victim;
867 /* Go up to the root and decrement the size of each node in the path. */
868 for (tmp = splice_out_node; tmp; tmp = rb_parent(tmp))
870 rb_erase(victim, &col->rbtree);
873 static int add_row_to_rbtrees(struct osl_table *t, uint32_t row_num,
874 struct osl_object *volatile_objs, struct osl_row **row_ptr)
878 struct osl_row *row = allocate_row(t->num_rbtrees);
879 const struct osl_column_description *cd;
882 return -ERRNO_TO_ERROR(ENOMEM);
884 row->volatile_objects = volatile_objs;
885 FOR_EACH_RBTREE_COLUMN(i, t, cd) {
886 if (cd->storage_type == OSL_MAPPED_STORAGE) {
887 struct osl_object obj;
888 ret = get_mapped_object(t, i, row_num, &obj);
891 ret = insert_rbtree(t, i, row, &obj);
892 } else { /* volatile */
893 const struct osl_object *obj
894 = volatile_objs + t->columns[i].volatile_num;
895 ret = insert_rbtree(t, i, row, obj);
903 err: /* rollback changes, i.e. remove added entries from rbtrees */
905 remove_rb_node(t, i--, row);
910 static void free_volatile_objects(const struct osl_table *t,
911 enum osl_close_flags flags)
915 struct osl_column *rb_col;
916 const struct osl_column_description *cd;
918 if (!t->num_volatile_columns)
920 /* find the first rbtree column (any will do) */
921 FOR_EACH_RBTREE_COLUMN(i, t, cd)
923 rb_col = t->columns + i;
924 /* walk that rbtree and free all volatile objects */
925 for (n = rb_first(&rb_col->rbtree); n; n = rb_next(n)) {
926 struct osl_row *r = get_row_pointer(n, rb_col->rbtree_num);
927 if (flags & OSL_FREE_VOLATILE)
928 FOR_EACH_VOLATILE_COLUMN(j, t, cd) {
929 if (cd->storage_flags & OSL_DONT_FREE)
931 free(r->volatile_objects[
932 t->columns[j].volatile_num].data);
934 // for (j = 0; j < t->num_volatile_columns; j++)
935 // free(r->volatile_objects[j].data);
936 free(r->volatile_objects);
941 * Erase all rbtree nodes and free resources.
943 * \param t Pointer to an open osl table.
945 * This function is called by osl_close_table().
947 void clear_rbtrees(struct osl_table *t)
949 const struct osl_column_description *cd;
950 unsigned i, rbtrees_cleared = 0;
952 FOR_EACH_RBTREE_COLUMN(i, t, cd) {
953 struct osl_column *col = &t->columns[i];
956 for (n = rb_first(&col->rbtree); n;) {
958 rb_erase(n, &col->rbtree);
959 if (rbtrees_cleared == t->num_rbtrees) {
960 r = get_row_pointer(n, col->rbtree_num);
970 __export int osl_close_table(struct osl_table *t, enum osl_close_flags flags)
975 return -E_OSL_BAD_TABLE;
976 NOTICE_LOG("closing table %s\n", t->desc->name);
977 free_volatile_objects(t, flags);
979 ret = unmap_table(t, flags);
981 ERROR_LOG("unmap_table failed: %d\n", ret);
988 * Find out whether the given row number corresponds to an invalid row.
990 * \param t Pointer to the osl table.
991 * \param row_num The number of the row in question.
993 * By definition, a row is considered invalid if all its index entries
996 * \return Positive if \a row_num corresponds to an invalid row,
997 * zero if it corresponds to a valid row, negative on errors.
999 int row_is_invalid(struct osl_table *t, uint32_t row_num)
1002 int i, ret = get_row_index(t, row_num, &row_index);
1006 for (i = 0; i < t->row_index_size; i++) {
1007 if ((unsigned char)row_index[i] != 0xff)
1010 INFO_LOG("row %d is invalid\n", row_num);
1015 * Invalidate a row of an osl table.
1017 * \param t Pointer to an open osl table.
1018 * \param row_num Number of the row to mark as invalid.
1020 * This function marks each mapped object in the index entry of \a row as
1025 int mark_row_invalid(struct osl_table *t, uint32_t row_num)
1028 int ret = get_row_index(t, row_num, &row_index);
1032 INFO_LOG("marking row %d as invalid\n", row_num);
1033 memset(row_index, 0xff, t->row_index_size);
1038 * Initialize all rbtrees and compute number of invalid rows.
1040 * \param t The table containing the rbtrees to be initialized.
1044 int init_rbtrees(struct osl_table *t)
1047 const struct osl_column_description *cd;
1049 /* create rbtrees */
1050 FOR_EACH_RBTREE_COLUMN(i, t, cd)
1051 t->columns[i].rbtree = RB_ROOT;
1052 /* add valid rows to rbtrees */
1053 t->num_invalid_rows = 0;
1054 for (i = 0; i < t->num_rows; i++) {
1055 ret = row_is_invalid(t, i);
1059 t->num_invalid_rows++;
1062 ret = add_row_to_rbtrees(t, i, NULL, NULL);
1069 __export int osl_open_table(const struct osl_table_description *table_desc,
1070 struct osl_table **result)
1073 struct osl_table *t;
1074 const struct osl_column_description *cd;
1076 NOTICE_LOG("opening table %s\n", table_desc->name);
1077 ret = init_table_structure(table_desc, &t);
1080 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
1081 struct stat statbuf;
1082 char *dirname = column_filename(t, i);
1084 ret = -ERRNO_TO_ERROR(ENOMEM);
1087 /* check if directory exists */
1088 ret = stat(dirname, &statbuf);
1091 ret = -ERRNO_TO_ERROR(errno);
1094 ret = -ERRNO_TO_ERROR(ENOTDIR);
1095 if (!S_ISDIR(statbuf.st_mode))
1098 ret = map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1102 DEBUG_LOG("num rows: %d\n", t->num_rows);
1103 ret = init_rbtrees(t);
1105 osl_close_table(t, OSL_MARK_CLEAN); /* ignore further errors */
1116 static int create_disk_storage_object_dir(const struct osl_table *t,
1117 unsigned col_num, const char *ds_name)
1122 if (!(t->desc->flags & OSL_LARGE_TABLE))
1124 dirname = disk_storage_dirname(t, col_num, ds_name);
1126 return -ERRNO_TO_ERROR(ENOMEM);
1127 ret = osl_mkdir(dirname, 0777);
1129 if (ret < 0 && !is_errno(-ret, EEXIST))
1134 static int write_disk_storage_file(const struct osl_table *t, unsigned col_num,
1135 const struct osl_object *obj, const char *ds_name)
1140 ret = create_disk_storage_object_dir(t, col_num, ds_name);
1143 filename = disk_storage_path(t, col_num, ds_name);
1145 return -ERRNO_TO_ERROR(ENOMEM);
1146 ret = write_file(filename, obj->data, obj->size);
1151 static int append_map_file(const struct osl_table *t, unsigned col_num,
1152 const struct osl_object *obj, uint32_t *new_size)
1154 char *filename = column_filename(t, col_num);
1156 char header = 0; /* zero means valid object */
1159 return -ERRNO_TO_ERROR(ENOMEM);
1160 ret = append_file(filename, &header, 1, obj->data, obj->size,
1166 static int append_row_index(const struct osl_table *t, char *row_index)
1171 if (!t->num_mapped_columns)
1173 filename = index_filename(t->desc);
1175 return -ERRNO_TO_ERROR(ENOMEM);
1176 ret = append_file(filename, NULL, 0, row_index,
1177 t->row_index_size, NULL);
1182 static int truncate_mapped_file(const struct osl_table *t, unsigned col_num,
1186 char *filename = column_filename(t, col_num);
1189 return -ERRNO_TO_ERROR(ENOMEM);
1190 ret = truncate_file(filename, size);
1195 static int delete_disk_storage_file(const struct osl_table *t, unsigned col_num,
1196 const char *ds_name)
1198 char *dirname, *filename = disk_storage_path(t, col_num, ds_name);
1202 return -ERRNO_TO_ERROR(ENOMEM);
1203 ret = unlink(filename);
1207 return -ERRNO_TO_ERROR(err);
1208 if (!(t->desc->flags & OSL_LARGE_TABLE))
1210 dirname = disk_storage_dirname(t, col_num, ds_name);
1212 return -ERRNO_TO_ERROR(ENOMEM);
1218 __export int osl_add_and_get_row(struct osl_table *t, struct osl_object *objects,
1219 struct osl_row **row)
1222 char *ds_name = NULL;
1223 struct rb_node **rb_parents = NULL, ***rb_links = NULL;
1224 char *new_row_index = NULL;
1225 struct osl_object *volatile_objs = NULL;
1226 const struct osl_column_description *cd;
1229 return -E_OSL_BAD_TABLE;
1230 rb_parents = malloc(t->num_rbtrees * sizeof(struct rn_node*));
1232 return -ERRNO_TO_ERROR(ENOMEM);
1233 rb_links = malloc(t->num_rbtrees * sizeof(struct rn_node**));
1236 return -ERRNO_TO_ERROR(ENOMEM);
1238 if (t->num_mapped_columns) {
1239 new_row_index = malloc(t->row_index_size);
1240 if (!new_row_index) {
1243 return -ERRNO_TO_ERROR(ENOMEM);
1246 /* pass 1: sanity checks */
1247 // DEBUG_LOG("sanity tests: %p:%p\n", objects[0].data,
1248 // objects[1].data);
1249 FOR_EACH_COLUMN(i, t->desc, cd) {
1250 enum osl_storage_type st = cd->storage_type;
1251 enum osl_storage_flags sf = cd->storage_flags;
1253 // ret = -E_OSL_NULL_OBJECT;
1256 if (st == OSL_DISK_STORAGE)
1258 if (sf & OSL_RBTREE) {
1259 unsigned rbtree_num = t->columns[i].rbtree_num;
1260 ret = -E_OSL_RB_KEY_EXISTS;
1261 // DEBUG_LOG("checking whether %p exists\n",
1262 // objects[i].data);
1263 if (search_rbtree(objects + i, t, i,
1264 &rb_parents[rbtree_num],
1265 &rb_links[rbtree_num]) > 0)
1268 if (sf & OSL_FIXED_SIZE) {
1269 // DEBUG_LOG("fixed size. need: %zu, have: %d\n",
1270 // objects[i].size, cd->data_size);
1271 ret = -E_OSL_BAD_DATA_SIZE;
1272 if (objects[i].size != cd->data_size)
1276 if (t->num_disk_storage_columns) {
1277 ds_name = disk_storage_name_of_object(t,
1278 &objects[t->disk_storage_name_column]);
1279 ret = -ERRNO_TO_ERROR(ENOMEM);
1283 ret = unmap_table(t, OSL_MARK_CLEAN);
1286 // DEBUG_LOG("sanity tests passed%s\n", "");
1287 /* pass 2: create data files, append map data */
1288 FOR_EACH_COLUMN(i, t->desc, cd) {
1289 enum osl_storage_type st = cd->storage_type;
1290 if (st == OSL_NO_STORAGE)
1292 if (st == OSL_MAPPED_STORAGE) {
1294 struct osl_column *col = &t->columns[i];
1295 // DEBUG_LOG("appending object of size %zu\n",
1296 // objects[i].size);
1297 ret = append_map_file(t, i, objects + i, &new_size);
1300 update_cell_index(new_row_index, col, new_size,
1305 ret = write_disk_storage_file(t, i, objects + i, ds_name);
1309 ret = append_row_index(t, new_row_index);
1312 ret = map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1313 if (ret < 0) { /* truncate index and rollback changes */
1314 char *filename = index_filename(t->desc);
1316 truncate_file(filename, t->row_index_size);
1320 /* pass 3: add entry to rbtrees */
1321 if (t->num_volatile_columns) {
1322 ret = -ERRNO_TO_ERROR(ENOMEM);
1323 volatile_objs = calloc(t->num_volatile_columns,
1324 sizeof(struct osl_object));
1327 FOR_EACH_VOLATILE_COLUMN(i, t, cd)
1328 volatile_objs[t->columns[i].volatile_num] = objects[i];
1331 // DEBUG_LOG("adding new entry as row #%d\n", t->num_rows - 1);
1332 ret = add_row_to_rbtrees(t, t->num_rows - 1, volatile_objs, row);
1335 // DEBUG_LOG("added new entry as row #%d\n", t->num_rows - 1);
1338 rollback: /* rollback all changes made, ignore further errors */
1339 for (i--; i >= 0; i--) {
1340 cd = get_column_description(t->desc, i);
1341 enum osl_storage_type st = cd->storage_type;
1342 if (st == OSL_NO_STORAGE)
1345 if (st == OSL_MAPPED_STORAGE)
1346 truncate_mapped_file(t, i, objects[i].size);
1347 else /* disk storage */
1348 delete_disk_storage_file(t, i, ds_name);
1350 /* ignore error and return previous error value */
1351 map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1353 free(new_row_index);
1360 __export int osl_add_row(struct osl_table *t, struct osl_object *objects)
1362 return osl_add_and_get_row(t, objects, NULL);
1365 __export int osl_get_object(const struct osl_table *t, const struct osl_row *r,
1366 unsigned col_num, struct osl_object *object)
1368 const struct osl_column_description *cd;
1371 return -E_OSL_BAD_TABLE;
1372 cd = get_column_description(t->desc, col_num);
1373 /* col must not be disk storage */
1374 if (cd->storage_type == OSL_DISK_STORAGE)
1375 return -E_OSL_BAD_STORAGE_TYPE;
1376 if (cd->storage_type == OSL_MAPPED_STORAGE)
1377 return get_mapped_object(t, col_num, r->num, object);
1379 *object = r->volatile_objects[t->columns[col_num].volatile_num];
1383 static int mark_mapped_object_invalid(const struct osl_table *t,
1384 uint32_t row_num, unsigned col_num)
1386 struct osl_object obj;
1388 int ret = get_mapped_object(t, col_num, row_num, &obj);
1398 __export int osl_del_row(struct osl_table *t, struct osl_row *row)
1400 struct osl_row *r = row;
1402 const struct osl_column_description *cd;
1405 return -E_OSL_BAD_TABLE;
1406 INFO_LOG("deleting row %p\n", row);
1408 if (t->num_disk_storage_columns) {
1410 ret = disk_storage_name_of_row(t, r, &ds_name);
1413 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd)
1414 delete_disk_storage_file(t, i, ds_name);
1417 FOR_EACH_COLUMN(i, t->desc, cd) {
1418 struct osl_column *col = t->columns + i;
1419 enum osl_storage_type st = cd->storage_type;
1420 remove_rb_node(t, i, r);
1421 if (st == OSL_MAPPED_STORAGE) {
1422 mark_mapped_object_invalid(t, r->num, i);
1425 if (st == OSL_NO_STORAGE && !(cd->storage_flags & OSL_DONT_FREE))
1426 free(r->volatile_objects[col->volatile_num].data);
1428 if (t->num_mapped_columns) {
1429 ret = mark_row_invalid(t, r->num);
1432 t->num_invalid_rows++;
1437 free(r->volatile_objects);
1442 /* test if column has an rbtree */
1443 static int check_rbtree_col(const struct osl_table *t, unsigned col_num,
1444 struct osl_column **col)
1447 return -E_OSL_BAD_TABLE;
1448 if (!(get_column_description(t->desc, col_num)->storage_flags & OSL_RBTREE))
1449 return -E_OSL_BAD_STORAGE_FLAGS;
1450 *col = t->columns + col_num;
1454 __export int osl_get_row(const struct osl_table *t, unsigned col_num,
1455 const struct osl_object *obj, struct osl_row **result)
1458 struct rb_node *node;
1459 struct osl_row *row;
1460 struct osl_column *col;
1463 ret = check_rbtree_col(t, col_num, &col);
1466 ret = search_rbtree(obj, t, col_num, &node, NULL);
1469 row = get_row_pointer(node, t->columns[col_num].rbtree_num);
1474 static int rbtree_loop(struct osl_column *col, void *private_data,
1475 osl_rbtree_loop_func *func)
1477 struct rb_node *n, *tmp;
1479 /* this for-loop is safe against removal of an entry */
1480 for (n = rb_first(&col->rbtree), tmp = n? rb_next(n) : NULL;
1482 n = tmp, tmp = tmp? rb_next(tmp) : NULL) {
1483 struct osl_row *r = get_row_pointer(n, col->rbtree_num);
1484 if (func(r, private_data) < 0)
1490 static int rbtree_loop_reverse(struct osl_column *col, void *private_data,
1491 osl_rbtree_loop_func *func)
1493 struct rb_node *n, *tmp;
1495 /* safe against removal of an entry */
1496 for (n = rb_last(&col->rbtree), tmp = n? rb_prev(n) : NULL;
1498 n = tmp, tmp = tmp? rb_prev(tmp) : NULL) {
1499 struct osl_row *r = get_row_pointer(n, col->rbtree_num);
1500 if (func(r, private_data) < 0)
1506 __export int osl_rbtree_loop(const struct osl_table *t, unsigned col_num,
1507 void *private_data, osl_rbtree_loop_func *func)
1509 struct osl_column *col;
1511 int ret = check_rbtree_col(t, col_num, &col);
1514 return rbtree_loop(col, private_data, func);
1517 __export int osl_rbtree_loop_reverse(const struct osl_table *t, unsigned col_num,
1518 void *private_data, osl_rbtree_loop_func *func)
1520 struct osl_column *col;
1522 int ret = check_rbtree_col(t, col_num, &col);
1525 return rbtree_loop_reverse(col, private_data, func);
1528 /* TODO: Rollback changes on errors */
1529 static int rename_disk_storage_objects(struct osl_table *t,
1530 struct osl_object *old_obj, struct osl_object *new_obj)
1533 const struct osl_column_description *cd;
1534 char *old_ds_name, *new_ds_name;
1536 if (!t->num_disk_storage_columns)
1537 return 1; /* nothing to do */
1538 if (old_obj->size == new_obj->size && !memcmp(new_obj->data,
1539 old_obj->data, new_obj->size))
1540 return 1; /* object did not change */
1541 old_ds_name = disk_storage_name_of_object(t, old_obj);
1542 new_ds_name = disk_storage_name_of_object(t, new_obj);
1543 ret = -ERRNO_TO_ERROR(ENOMEM);
1544 if (!old_ds_name || ! new_ds_name)
1547 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
1548 char *old_filename, *new_filename;
1549 ret = create_disk_storage_object_dir(t, i, new_ds_name);
1552 old_filename = disk_storage_path(t, i, old_ds_name);
1553 new_filename = disk_storage_path(t, i, new_ds_name);
1554 if (!old_filename || !new_filename)
1555 ret = -ERRNO_TO_ERROR(ENOMEM);
1557 ret = osl_rename(old_filename, new_filename);
1571 __export int osl_update_object(struct osl_table *t, const struct osl_row *r,
1572 unsigned col_num, struct osl_object *obj)
1574 struct osl_column *col;
1575 const struct osl_column_description *cd;
1579 return -E_OSL_BAD_TABLE;
1580 col = &t->columns[col_num];
1581 cd = get_column_description(t->desc, col_num);
1582 DEBUG_LOG("updating column %u of %s\n", col_num, t->desc->name);
1583 if (cd->storage_flags & OSL_RBTREE) {
1584 if (search_rbtree(obj, t, col_num, NULL, NULL) > 0)
1585 return -E_OSL_RB_KEY_EXISTS;
1587 if (cd->storage_flags & OSL_FIXED_SIZE) {
1588 if (obj->size != cd->data_size)
1589 return -E_OSL_BAD_DATA_SIZE;
1591 remove_rb_node(t, col_num, r);
1592 if (cd->storage_type == OSL_NO_STORAGE) { /* TODO: If fixed size, reuse object? */
1593 free(r->volatile_objects[col->volatile_num].data);
1594 r->volatile_objects[col->volatile_num] = *obj;
1595 } else if (cd->storage_type == OSL_DISK_STORAGE) {
1597 ret = disk_storage_name_of_row(t, r, &ds_name);
1600 ret = delete_disk_storage_file(t, col_num, ds_name);
1601 if (ret < 0 && !is_errno(-ret, ENOENT)) {
1605 ret = write_disk_storage_file(t, col_num, obj, ds_name);
1609 } else { /* mapped storage */
1610 struct osl_object old_obj;
1611 ret = get_mapped_object(t, col_num, r->num, &old_obj);
1615 * If the updated column is the disk storage name column, the
1616 * disk storage name changes, so we have to rename all disk
1617 * storage objects accordingly.
1619 if (col_num == t->disk_storage_name_column) {
1620 ret = rename_disk_storage_objects(t, &old_obj, obj);
1624 if (cd->storage_flags & OSL_FIXED_SIZE)
1625 memcpy(old_obj.data, obj->data, cd->data_size);
1626 else { /* TODO: if the size doesn't change, use old space */
1627 uint32_t new_data_map_size;
1629 ret = get_row_index(t, r->num, &row_index);
1632 ret = mark_mapped_object_invalid(t, r->num, col_num);
1635 unmap_column(t, col_num);
1636 ret = append_map_file(t, col_num, obj,
1637 &new_data_map_size);
1640 ret = map_column(t, col_num);
1643 update_cell_index(row_index, col, new_data_map_size,
1647 if (cd->storage_flags & OSL_RBTREE) {
1648 ret = insert_rbtree(t, col_num, r, obj);
1655 __export int osl_open_disk_object(const struct osl_table *t, const struct osl_row *r,
1656 unsigned col_num, struct osl_object *obj)
1658 const struct osl_column_description *cd;
1659 char *ds_name, *filename;
1663 return -E_OSL_BAD_TABLE;
1664 cd = get_column_description(t->desc, col_num);
1665 if (cd->storage_type != OSL_DISK_STORAGE)
1666 return -E_OSL_BAD_STORAGE_TYPE;
1668 ret = disk_storage_name_of_row(t, r, &ds_name);
1671 filename = disk_storage_path(t, col_num, ds_name);
1674 return -ERRNO_TO_ERROR(ENOMEM);
1675 DEBUG_LOG("filename: %s\n", filename);
1676 ret = mmap_full_file(filename, O_RDONLY, &obj->data, &obj->size, NULL);
1681 __export int osl_close_disk_object(struct osl_object *obj)
1683 return osl_munmap(obj->data, obj->size);
1686 __export int osl_get_num_rows(const struct osl_table *t, unsigned *num_rows)
1689 return -E_OSL_BAD_TABLE;
1690 assert(t->num_rows >= t->num_invalid_rows);
1691 *num_rows = t->num_rows - t->num_invalid_rows;
1695 __export int osl_get_rank(const struct osl_table *t, struct osl_row *r,
1696 unsigned col_num, unsigned *rank)
1698 struct osl_object obj;
1699 struct osl_column *col;
1700 struct rb_node *node;
1701 int ret = check_rbtree_col(t, col_num, &col);
1705 ret = osl_get_object(t, r, col_num, &obj);
1708 ret = search_rbtree(&obj, t, col_num, &node, NULL);
1711 ret = rb_rank(node, rank);
1713 return -E_OSL_BAD_ROW;
1717 __export int osl_get_nth_row(const struct osl_table *t, unsigned col_num,
1718 unsigned n, struct osl_row **result)
1720 struct osl_column *col;
1721 struct rb_node *node;
1726 return -E_OSL_RB_KEY_NOT_FOUND;
1727 ret = osl_get_num_rows(t, &num_rows);
1731 return -E_OSL_RB_KEY_NOT_FOUND;
1732 ret = check_rbtree_col(t, col_num, &col);
1735 node = rb_nth(col->rbtree.rb_node, n);
1737 return -E_OSL_RB_KEY_NOT_FOUND;
1738 *result = get_row_pointer(node, col->rbtree_num);
1742 __export int osl_rbtree_first_row(const struct osl_table *t, unsigned col_num,
1743 struct osl_row **result)
1745 return osl_get_nth_row(t, col_num, 1, result);
1748 __export int osl_rbtree_last_row(const struct osl_table *t, unsigned col_num,
1749 struct osl_row **result)
1752 int ret = osl_get_num_rows(t, &num_rows);
1756 return osl_get_nth_row(t, col_num, num_rows, result);