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() */
20 * Allocate a sufficiently large string and print into it.
22 * \param fmt A usual format string.
24 * Produce output according to \p fmt. No artificial bound on the length of the
25 * resulting string is imposed.
27 * \return This function either returns a pointer to a string that must be
28 * freed by the caller or \p NULL if memory allocation failed.
32 static __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
36 char *p = malloc(size);
43 /* Try to print in the allocated space. */
45 n = vsnprintf(p, size, fmt, ap);
47 /* If that worked, return the string. */
48 if (n > -1 && n < size)
50 /* Else try again with more space. */
51 if (n > -1) /* glibc 2.1 */
52 size = n + 1; /* precisely what is needed */
54 size *= 2; /* twice the old size */
64 /* Taken from Drepper: How to write shared libraries, Appendix B. */
66 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
67 #define MSGSTRFIELD1(line) str##line
68 static const union msgstr_t {
70 #define _S(n, s) char MSGSTRFIELD(__LINE__)[sizeof(s)];
80 static const unsigned int errmsgidx[] = {
81 #define _S(n, s) [n] = offsetof(union msgstr_t, MSGSTRFIELD(__LINE__)),
86 __export const char *osl_strerror(int num)
88 if (IS_SYSTEM_ERROR(num))
89 return strerror((num) & ((1 << SYSTEM_ERROR_BIT) - 1));
90 return msgstr.str + errmsgidx[num];
95 static void __attribute ((constructor)) init_loglevel(void)
97 char *p = getenv("OSL_LOGLEVEL");
99 /* don't log anything if unset */
100 loglevel = p? atoi(p) : EMERG + 1;
106 * \param ll Loglevel.
107 * \param fmt Usual format string.
109 * All XXX_LOG() macros use this function.
111 __printf_2_3 void __log(int ll, const char* fmt,...)
124 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
125 fprintf(outfd, "%s ", str);
127 vfprintf(outfd, fmt, argp);
132 * A wrapper for lseek(2).
134 * \param fd The file descriptor whose offset is to be to repositioned.
135 * \param offset A value-result parameter.
136 * \param whence Usual repositioning directive.
138 * Reposition the offset of the file descriptor \a fd to the argument \a offset
139 * according to the directive \a whence. Upon successful return, \a offset
140 * contains the resulting offset location as measured in bytes from the
141 * beginning of the file.
143 * \return Positive on success. Otherwise, the function returns \p -E_OSL_LSEEK.
147 static int __lseek(int fd, off_t *offset, int whence)
149 *offset = lseek(fd, *offset, whence);
150 int ret = -E_OSL_LSEEK;
157 * Wrapper for the write system call.
159 * \param fd The file descriptor to write to.
160 * \param buf The buffer to write.
161 * \param size The length of \a buf in bytes.
163 * This function writes out the given buffer and retries if an interrupt
164 * occurred during the write.
166 * \return On success, the number of bytes written is returned, otherwise, the
167 * function returns \p -E_OSL_WRITE.
171 static ssize_t __write(int fd, const void *buf, size_t size)
176 ret = write(fd, buf, size);
177 if ((ret < 0) && (errno == EAGAIN || errno == EINTR))
179 return ret >= 0? ret : -E_OSL_WRITE;
184 * Write the whole buffer to a file descriptor.
186 * \param fd The file descriptor to write to.
187 * \param buf The buffer to write.
188 * \param size The length of \a buf in bytes.
190 * This function writes the given buffer and continues on short writes and
191 * when interrupted by a signal.
193 * \return Positive on success, negative on errors. Possible errors: any
194 * errors returned by para_write().
198 static ssize_t write_all(int fd, const void *buf, size_t size)
200 // DEBUG_LOG("writing %zu bytes\n", size);
203 ssize_t ret = __write(fd, b, size);
204 // DEBUG_LOG("ret: %zd\n", ret);
213 * Open a file, write the given buffer and close the file.
215 * \param filename Full path to the file to open.
216 * \param buf The buffer to write to the file.
217 * \param size The size of \a buf.
221 static int write_file(const char *filename, const void *buf, size_t size)
225 ret = osl_open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
229 ret = write_all(fd, buf, size);
238 static int append_file(const char *filename, char *header, size_t header_size,
239 char *data, size_t data_size, uint32_t *new_pos)
243 // DEBUG_LOG("appending %zu + %zu bytes\n", header_size, data_size);
244 ret = osl_open(filename, O_WRONLY | O_CREAT | O_APPEND, 0644);
248 if (header && header_size) {
249 ret = write_all(fd, header, header_size);
253 ret = write_all(fd, data, data_size);
258 ret = __lseek(fd, &offset, SEEK_END);
261 // DEBUG_LOG("new file size: " FMT_OFF_T "\n", offset);
270 static int verify_name(const char *name)
273 return -E_OSL_BAD_NAME;
275 return -E_OSL_BAD_NAME;
276 if (strchr(name, '/'))
277 return -E_OSL_BAD_NAME;
278 if (!strcmp(name, ".."))
279 return -E_OSL_BAD_NAME;
280 if (!strcmp(name, "."))
281 return -E_OSL_BAD_NAME;
286 * Compare two osl objects pointing to hash values.
288 * \param obj1 Pointer to the first hash object.
289 * \param obj2 Pointer to the second hash object.
291 * \return The values required for an osl compare function.
293 * \sa osl_compare_func, uint32_compare().
295 int osl_hash_compare(const struct osl_object *obj1, const struct osl_object *obj2)
297 return hash_compare((HASH_TYPE *)obj1->data, (HASH_TYPE *)obj2->data);
300 static char *disk_storage_dirname(const struct osl_table *t, unsigned col_num,
303 char *dirname, *column_name = column_filename(t, col_num);
307 if (!(t->desc->flags & OSL_LARGE_TABLE))
309 dirname = make_message("%s/%.2s", column_name, ds_name);
314 static char *disk_storage_name_of_object(const struct osl_table *t,
315 const struct osl_object *obj)
317 HASH_TYPE hash[HASH_SIZE];
318 hash_object(obj, hash);
319 return disk_storage_name_of_hash(t, hash);
322 static int disk_storage_name_of_row(const struct osl_table *t,
323 const struct osl_row *row, char **name)
325 struct osl_object obj;
326 int ret = osl_get_object(t, row, t->disk_storage_name_column, &obj);
330 *name = disk_storage_name_of_object(t, &obj);
333 return -ERRNO_TO_ERROR(ENOMEM);
336 static void column_name_hash(const char *col_name, HASH_TYPE *hash)
338 hash_function(col_name, strlen(col_name), hash);
341 static int init_column_descriptions(struct osl_table *t)
344 const struct osl_column_description *cd;
346 ret = -E_OSL_BAD_TABLE_DESC;
347 ret = verify_name(t->desc->name);
350 ret = -E_OSL_BAD_DB_DIR;
351 if (!t->desc->dir && (t->num_disk_storage_columns || t->num_mapped_columns))
353 /* the size of the index header without column descriptions */
354 t->index_header_size = IDX_COLUMN_DESCRIPTIONS;
355 FOR_EACH_COLUMN(i, t->desc, cd) {
356 struct osl_column *col = t->columns + i;
357 if (cd->storage_flags & OSL_RBTREE) {
358 if (!cd->compare_function)
359 return -E_OSL_NO_COMPARE_FUNC;
361 if (cd->storage_type == OSL_NO_STORAGE)
363 ret = -E_OSL_NO_COLUMN_NAME;
364 if (!cd->name || !cd->name[0])
366 ret = verify_name(cd->name);
369 t->index_header_size += index_column_description_size(cd->name);
370 column_name_hash(cd->name, col->name_hash);
371 ret = -E_OSL_DUPLICATE_COL_NAME;
372 for (j = i + 1; j < t->desc->num_columns; j++) {
373 const char *name2 = get_column_description(t->desc,
375 if (cd->name && name2 && !strcmp(cd->name, name2))
385 * Initialize a struct table from given table description.
387 * \param desc The description of the osl table.
388 * \param table_ptr Result is returned here.
390 * This function performs several sanity checks on \p desc and returns if any
391 * of these tests fail. On success, a struct \p osl_table is allocated and
392 * initialized with data derived from \p desc.
396 * \sa struct osl_table.
398 int init_table_structure(const struct osl_table_description *desc,
399 struct osl_table **table_ptr)
401 const struct osl_column_description *cd;
402 struct osl_table *t = calloc(1, sizeof(*t));
403 int i, ret = -ERRNO_TO_ERROR(ENOMEM), have_disk_storage_name_column = 0;
407 ret = -E_OSL_BAD_TABLE_DESC;
410 DEBUG_LOG("creating table structure for '%s' from table "
411 "description\n", desc->name);
412 ret = -E_OSL_NO_COLUMN_DESC;
413 if (!desc->column_descriptions)
415 ret = -E_OSL_NO_COLUMNS;
416 if (!desc->num_columns)
418 ret = -ERRNO_TO_ERROR(ENOMEM);
419 t->columns = calloc(desc->num_columns, sizeof(struct osl_column));
423 FOR_EACH_COLUMN(i, t->desc, cd) {
424 enum osl_storage_type st = cd->storage_type;
425 enum osl_storage_flags sf = cd->storage_flags;
426 struct osl_column *col = &t->columns[i];
428 ret = -E_OSL_BAD_STORAGE_TYPE;
429 if (st != OSL_MAPPED_STORAGE && st != OSL_DISK_STORAGE
430 && st != OSL_NO_STORAGE)
432 ret = -E_OSL_BAD_STORAGE_FLAGS;
433 if (st == OSL_DISK_STORAGE && sf & OSL_RBTREE)
435 ret = -E_OSL_BAD_STORAGE_SIZE;
436 if (sf & OSL_FIXED_SIZE && !cd->data_size)
439 case OSL_DISK_STORAGE:
440 t->num_disk_storage_columns++;
442 case OSL_MAPPED_STORAGE:
443 t->num_mapped_columns++;
444 col->index_offset = t->row_index_size;
445 t->row_index_size += 8;
448 col->volatile_num = t->num_volatile_columns;
449 t->num_volatile_columns++;
452 if (sf & OSL_RBTREE) {
453 col->rbtree_num = t->num_rbtrees;
455 if ((sf & OSL_UNIQUE) && (st == OSL_MAPPED_STORAGE)) {
456 if (!have_disk_storage_name_column)
457 t->disk_storage_name_column = i;
458 have_disk_storage_name_column = 1;
462 ret = -E_OSL_NO_UNIQUE_RBTREE_COLUMN;
463 if (t->num_disk_storage_columns && !have_disk_storage_name_column)
465 ret = -E_OSL_NO_RBTREE_COL;
469 DEBUG_LOG("OK. Index entry size: %u\n", t->row_index_size);
470 ret = init_column_descriptions(t);
482 * Read the table description from index header.
484 * \param map The memory mapping of the index file.
485 * \param desc The values found in the index header are returned here.
487 * Read the index header, check for the paraslash magic string and the table version number.
488 * Read all information stored in the index header into \a desc.
492 * \sa struct osl_table_description, osl_create_table.
494 int read_table_desc(struct osl_object *map, struct osl_table_description *desc)
496 char *buf = map->data;
498 uint16_t header_size;
501 struct osl_column_description *cd;
503 if (map->size < MIN_INDEX_HEADER_SIZE(1))
504 return -E_OSL_SHORT_TABLE;
505 if (strncmp(buf + IDX_PARA_MAGIC, PARA_MAGIC, strlen(PARA_MAGIC)))
506 return -E_OSL_NO_MAGIC;
507 version = read_u8(buf + IDX_VERSION);
508 if (version < MIN_TABLE_VERSION || version > MAX_TABLE_VERSION)
509 return -E_OSL_VERSION_MISMATCH;
510 desc->flags = read_u8(buf + IDX_TABLE_FLAGS);
511 desc->num_columns = read_u16(buf + IDX_NUM_COLUMNS);
512 INFO_LOG("%u columns\n", desc->num_columns);
513 if (!desc->num_columns)
514 return -E_OSL_NO_COLUMNS;
515 header_size = read_u16(buf + IDX_HEADER_SIZE);
516 if (map->size < header_size)
517 return -E_OSL_BAD_SIZE;
518 desc->column_descriptions = calloc(desc->num_columns,
519 sizeof(struct osl_column_description));
520 if (!desc->column_descriptions)
521 return -ERRNO_TO_ERROR(ENOMEM);
522 offset = IDX_COLUMN_DESCRIPTIONS;
523 FOR_EACH_COLUMN(i, desc, cd) {
526 ret = -E_OSL_SHORT_TABLE;
527 if (map->size < offset + MIN_IDX_COLUMN_DESCRIPTION_SIZE) {
528 ERROR_LOG("map size = %zu < %u = offset + min desc size\n",
529 map->size, offset + MIN_IDX_COLUMN_DESCRIPTION_SIZE);
532 cd->storage_type = read_u16(buf + offset + IDX_CD_STORAGE_TYPE);
533 cd->storage_flags = read_u16(buf + offset +
534 IDX_CD_STORAGE_FLAGS);
535 cd->data_size = read_u32(buf + offset + IDX_CD_DATA_SIZE);
536 null_byte = memchr(buf + offset + IDX_CD_NAME, '\0',
537 map->size - offset - IDX_CD_NAME);
538 ret = -E_OSL_INDEX_CORRUPTION;
541 ret = -ERRNO_TO_ERROR(ENOMEM);
542 cd->name = strdup(buf + offset + IDX_CD_NAME);
545 offset += index_column_description_size(cd->name);
547 if (offset != header_size) {
548 ret = -E_OSL_INDEX_CORRUPTION;
549 ERROR_LOG("real header size = %u != %u = stored header size\n",
550 offset, header_size);
555 FOR_EACH_COLUMN(i, desc, cd)
561 * check whether the table description given by \p t->desc matches the on-disk
562 * table structure stored in the index of \a t.
564 static int compare_table_descriptions(struct osl_table *t)
567 struct osl_table_description desc;
568 const struct osl_column_description *cd1, *cd2;
570 /* read the on-disk structure into desc */
571 ret = read_table_desc(&t->index_map, &desc);
574 ret = -E_OSL_BAD_TABLE_FLAGS;
575 if (desc.flags != t->desc->flags)
577 ret = -E_OSL_BAD_COLUMN_NUM;
578 if (desc.num_columns > t->desc->num_columns)
580 if (desc.num_columns < t->desc->num_columns) {
581 struct osl_column_description *cd;
582 unsigned diff = t->desc->num_columns - desc.num_columns;
583 INFO_LOG("extending table by %u volatile columns\n", diff);
584 ret = -ERRNO_TO_ERROR(ENOMEM);
585 desc.column_descriptions = realloc(desc.column_descriptions,
586 t->desc->num_columns * sizeof(struct osl_column_description));
587 if (!desc.column_descriptions)
589 for (i = desc.num_columns; i < t->desc->num_columns; i++) {
590 cd = get_column_description(&desc, i);
591 cd->storage_type = OSL_NO_STORAGE;
594 desc.num_columns += diff;
596 FOR_EACH_COLUMN(i, t->desc, cd1) {
597 cd2 = get_column_description(&desc, i);
598 ret = -E_OSL_BAD_STORAGE_TYPE;
599 if (cd1->storage_type != cd2->storage_type)
601 if (cd1->storage_type == OSL_NO_STORAGE)
603 ret = -E_OSL_BAD_STORAGE_FLAGS;
604 if (cd1->storage_flags != cd2->storage_flags) {
605 ERROR_LOG("sf1 = %u != %u = sf2\n",
606 cd1->storage_flags, cd2->storage_flags);
609 ret = -E_OSL_BAD_DATA_SIZE;
610 if (cd1->storage_flags & OSL_FIXED_SIZE)
611 if (cd1->data_size != cd2->data_size)
613 ret = -E_OSL_BAD_COLUMN_NAME;
614 if (strcmp(cd1->name, cd2->name))
617 INFO_LOG("table description of '%s' matches on-disk data, good\n",
621 FOR_EACH_COLUMN(i, &desc, cd1)
623 free(desc.column_descriptions);
627 static int create_table_index(struct osl_table *t)
629 char *buf, *filename;
631 size_t size = t->index_header_size;
632 const struct osl_column_description *cd;
635 INFO_LOG("creating %zu byte index for table %s\n", size,
637 buf = calloc(1, size);
639 return -ERRNO_TO_ERROR(ENOMEM);
640 sprintf(buf + IDX_PARA_MAGIC, "%s", PARA_MAGIC);
641 write_u8(buf + IDX_TABLE_FLAGS, t->desc->flags);
642 write_u8(buf + IDX_DIRTY_FLAG, 0);
643 write_u8(buf + IDX_VERSION, CURRENT_TABLE_VERSION);
644 write_u16(buf + IDX_NUM_COLUMNS, t->num_mapped_columns + t->num_disk_storage_columns);
645 write_u16(buf + IDX_HEADER_SIZE, t->index_header_size);
646 offset = IDX_COLUMN_DESCRIPTIONS;
647 FOR_EACH_COLUMN(i, t->desc, cd) {
648 /* no need to store info about volatile storage */
649 if (cd->storage_type == OSL_NO_STORAGE)
651 write_u16(buf + offset + IDX_CD_STORAGE_TYPE,
653 write_u16(buf + offset + IDX_CD_STORAGE_FLAGS,
655 if (cd->storage_flags & OSL_FIXED_SIZE)
656 write_u32(buf + offset + IDX_CD_DATA_SIZE,
658 strcpy(buf + offset + IDX_CD_NAME, cd->name);
659 offset += index_column_description_size(cd->name);
661 assert(offset = size);
662 filename = index_filename(t->desc);
664 ret = write_file(filename, buf, size);
666 ret = -ERRNO_TO_ERROR(ENOMEM);
673 * Create a new osl table.
675 * \param desc Pointer to the table description.
679 __export int osl_create_table(const struct osl_table_description *desc)
681 const struct osl_column_description *cd;
682 char *table_dir = NULL, *filename;
684 int i, ret = init_table_structure(desc, &t);
688 INFO_LOG("creating %s\n", desc->name);
689 FOR_EACH_COLUMN(i, t->desc, cd) {
690 if (cd->storage_type == OSL_NO_STORAGE)
693 ret = para_mkdir(desc->dir, 0777);
694 if (ret < 0 && !is_errno(-ret, EEXIST))
696 table_dir = make_message("%s/%s", desc->dir,
698 ret = -ERRNO_TO_ERROR(ENOMEM);
701 ret = para_mkdir(table_dir, 0777);
705 ret = -ERRNO_TO_ERROR(ENOMEM);
706 filename = column_filename(t, i);
709 INFO_LOG("filename: %s\n", filename);
710 if (cd->storage_type == OSL_MAPPED_STORAGE) {
711 ret = osl_open(filename, O_RDWR | O_CREAT | O_EXCL,
720 ret = para_mkdir(filename, 0777);
725 if (t->num_mapped_columns) {
726 ret = create_table_index(t);
738 static int table_is_dirty(struct osl_table *t)
740 char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
741 uint8_t dirty = read_u8(buf) & 0x1;
745 static void mark_table_dirty(struct osl_table *t)
747 char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
748 write_u8(buf, read_u8(buf) | 1);
751 static void mark_table_clean(struct osl_table *t)
753 char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
754 write_u8(buf, read_u8(buf) & 0xfe);
757 static void unmap_column(struct osl_table *t, unsigned col_num)
759 struct osl_object map = t->columns[col_num].data_map;
763 ret = para_munmap(map.data, map.size);
769 * Unmap all mapped files of an osl table.
771 * \param t Pointer to a mapped table.
772 * \param flags Options for unmapping.
774 * \return Positive on success, negative on errors.
776 * \sa map_table(), enum osl_close_flags, para_munmap().
778 int unmap_table(struct osl_table *t, enum osl_close_flags flags)
781 const struct osl_column_description *cd;
784 if (!t->num_mapped_columns) /* can this ever happen? */
786 INFO_LOG("unmapping table '%s'\n", t->desc->name);
787 if (!t->index_map.data)
788 return -E_OSL_NOT_MAPPED;
789 if (flags & OSL_MARK_CLEAN)
791 ret = para_munmap(t->index_map.data, t->index_map.size);
794 t->index_map.data = NULL;
797 FOR_EACH_MAPPED_COLUMN(i, t, cd)
802 static int map_column(struct osl_table *t, unsigned col_num)
805 char *filename = column_filename(t, col_num);
806 int ret = -E_OSL_STAT;
809 return -ERRNO_TO_ERROR(ENOMEM);
810 if (stat(filename, &statbuf) < 0) {
814 if (!(S_IFREG & statbuf.st_mode)) {
818 ret = mmap_full_file(filename, O_RDWR,
819 &t->columns[col_num].data_map.data,
820 &t->columns[col_num].data_map.size,
827 * Map the index file and all columns of type \p OSL_MAPPED_STORAGE into memory.
829 * \param t Pointer to an initialized table structure.
830 * \param flags Mapping options.
832 * \return Negative return value on errors; on success the number of rows
833 * (including invalid rows) is returned.
835 * \sa unmap_table(), enum map_table_flags, osl_open_table(), mmap(2).
837 int map_table(struct osl_table *t, enum map_table_flags flags)
840 const struct osl_column_description *cd;
841 int i = 0, ret, num_rows = 0;
843 if (!t->num_mapped_columns)
845 if (t->index_map.data)
846 return -E_OSL_ALREADY_MAPPED;
847 filename = index_filename(t->desc);
849 return -ERRNO_TO_ERROR(ENOMEM);
850 INFO_LOG("mapping table '%s' (index: %s)\n", t->desc->name, filename);
851 ret = mmap_full_file(filename, flags & MAP_TBL_FL_MAP_RDONLY?
852 O_RDONLY : O_RDWR, &t->index_map.data, &t->index_map.size, NULL);
856 if (flags & MAP_TBL_FL_VERIFY_INDEX) {
857 ret = compare_table_descriptions(t);
862 if (!(flags & MAP_TBL_FL_IGNORE_DIRTY)) {
863 if (table_is_dirty(t)) {
864 ERROR_LOG("%s is dirty\n", t->desc->name);
869 num_rows = table_num_rows(t);
873 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
874 ret = map_column(t, i);
879 err: /* unmap what is already mapped */
880 for (i--; i >= 0; i--) {
881 struct osl_object map = t->columns[i].data_map;
882 para_munmap(map.data, map.size);
885 para_munmap(t->index_map.data, t->index_map.size);
886 t->index_map.data = NULL;
891 * Retrieve a mapped object by row and column number.
893 * \param t Pointer to an open osl table.
894 * \param col_num Number of the mapped column containing the object to retrieve.
895 * \param row_num Number of the row containing the object to retrieve.
896 * \param obj The result is returned here.
898 * It is considered an error if \a col_num does not refer to a column
899 * of storage type \p OSL_MAPPED_STORAGE.
903 * \sa osl_storage_type.
905 int get_mapped_object(const struct osl_table *t, unsigned col_num,
906 uint32_t row_num, struct osl_object *obj)
908 struct osl_column *col = &t->columns[col_num];
914 if (t->num_rows <= row_num)
915 return -E_OSL_BAD_ROW_NUM;
916 ret = get_cell_index(t, row_num, col_num, &cell_index);
919 offset = read_u32(cell_index);
920 obj->size = read_u32(cell_index + 4) - 1;
921 header = col->data_map.data + offset;
922 obj->data = header + 1;
923 if (read_u8(header) == 0xff) {
924 ERROR_LOG("col %u, size %zu, offset %u\n", col_num,
926 return -E_OSL_INVALID_OBJECT;
931 static int search_rbtree(const struct osl_object *obj,
932 const struct osl_table *t, unsigned col_num,
933 struct rb_node **result, struct rb_node ***rb_link)
935 struct osl_column *col = &t->columns[col_num];
936 struct rb_node **new = &col->rbtree.rb_node, *parent = NULL;
937 const struct osl_column_description *cd =
938 get_column_description(t->desc, col_num);
939 enum osl_storage_type st = cd->storage_type;
941 struct osl_row *this_row = get_row_pointer(*new,
944 struct osl_object this_obj;
946 if (st == OSL_MAPPED_STORAGE) {
947 ret = get_mapped_object(t, col_num, this_row->num,
952 this_obj = this_row->volatile_objects[col->volatile_num];
953 ret = cd->compare_function(obj, &this_obj);
956 *result = get_rb_node_pointer(this_row,
961 new = &((*new)->rb_left);
963 new = &((*new)->rb_right);
969 return -E_OSL_RB_KEY_NOT_FOUND;
972 static int insert_rbtree(struct osl_table *t, unsigned col_num,
973 const struct osl_row *row, const struct osl_object *obj)
975 struct rb_node *parent, **rb_link;
978 int ret = search_rbtree(obj, t, col_num, &parent, &rb_link);
981 return -E_OSL_RB_KEY_EXISTS;
982 rbtree_num = t->columns[col_num].rbtree_num;
983 n = get_rb_node_pointer(row, rbtree_num);
984 rb_link_node(n, parent, rb_link);
985 rb_insert_color(n, &t->columns[col_num].rbtree);
989 static void remove_rb_node(struct osl_table *t, unsigned col_num,
990 const struct osl_row *row)
992 struct osl_column *col = &t->columns[col_num];
993 const struct osl_column_description *cd =
994 get_column_description(t->desc, col_num);
995 enum osl_storage_flags sf = cd->storage_flags;
996 struct rb_node *victim, *splice_out_node, *tmp;
997 if (!(sf & OSL_RBTREE))
1000 * Which node is removed/spliced out actually depends on how many
1001 * children the victim node has: If it has no children, it gets
1002 * deleted. If it has one child, it gets spliced out. If it has two
1003 * children, its successor (which has at most a right child) gets
1006 victim = get_rb_node_pointer(row, col->rbtree_num);
1007 if (victim->rb_left && victim->rb_right)
1008 splice_out_node = rb_next(victim);
1010 splice_out_node = victim;
1011 /* Go up to the root and decrement the size of each node in the path. */
1012 for (tmp = splice_out_node; tmp; tmp = rb_parent(tmp))
1014 rb_erase(victim, &col->rbtree);
1017 static int add_row_to_rbtrees(struct osl_table *t, uint32_t row_num,
1018 struct osl_object *volatile_objs, struct osl_row **row_ptr)
1022 struct osl_row *row = allocate_row(t->num_rbtrees);
1023 const struct osl_column_description *cd;
1026 return -ERRNO_TO_ERROR(ENOMEM);
1028 row->volatile_objects = volatile_objs;
1029 FOR_EACH_RBTREE_COLUMN(i, t, cd) {
1030 if (cd->storage_type == OSL_MAPPED_STORAGE) {
1031 struct osl_object obj;
1032 ret = get_mapped_object(t, i, row_num, &obj);
1035 ret = insert_rbtree(t, i, row, &obj);
1036 } else { /* volatile */
1037 const struct osl_object *obj
1038 = volatile_objs + t->columns[i].volatile_num;
1039 ret = insert_rbtree(t, i, row, obj);
1047 err: /* rollback changes, i.e. remove added entries from rbtrees */
1049 remove_rb_node(t, i--, row);
1054 static void free_volatile_objects(const struct osl_table *t,
1055 enum osl_close_flags flags)
1059 struct osl_column *rb_col;
1060 const struct osl_column_description *cd;
1062 if (!t->num_volatile_columns)
1064 /* find the first rbtree column (any will do) */
1065 FOR_EACH_RBTREE_COLUMN(i, t, cd)
1067 rb_col = t->columns + i;
1068 /* walk that rbtree and free all volatile objects */
1069 for (n = rb_first(&rb_col->rbtree); n; n = rb_next(n)) {
1070 struct osl_row *r = get_row_pointer(n, rb_col->rbtree_num);
1071 if (flags & OSL_FREE_VOLATILE)
1072 FOR_EACH_VOLATILE_COLUMN(j, t, cd) {
1073 if (cd->storage_flags & OSL_DONT_FREE)
1075 free(r->volatile_objects[
1076 t->columns[j].volatile_num].data);
1078 // for (j = 0; j < t->num_volatile_columns; j++)
1079 // free(r->volatile_objects[j].data);
1080 free(r->volatile_objects);
1085 * Erase all rbtree nodes and free resources.
1087 * \param t Pointer to an open osl table.
1089 * This function is called by osl_close_table().
1091 void clear_rbtrees(struct osl_table *t)
1093 const struct osl_column_description *cd;
1094 unsigned i, rbtrees_cleared = 0;
1096 FOR_EACH_RBTREE_COLUMN(i, t, cd) {
1097 struct osl_column *col = &t->columns[i];
1100 for (n = rb_first(&col->rbtree); n;) {
1102 rb_erase(n, &col->rbtree);
1103 if (rbtrees_cleared == t->num_rbtrees) {
1104 r = get_row_pointer(n, col->rbtree_num);
1115 * Close an osl table.
1117 * \param t Pointer to the table to be closed.
1118 * \param flags Options for what should be cleaned up.
1120 * If osl_open_table() succeeds, the resulting table pointer must later be
1121 * passed to this function in order to flush all changes to the file system and
1122 * to free the resources that were allocated by osl_open_table().
1126 * \sa osl_open_table(), unmap_table().
1128 __export int osl_close_table(struct osl_table *t, enum osl_close_flags flags)
1133 return -E_OSL_BAD_TABLE;
1134 NOTICE_LOG("closing table %s\n", t->desc->name);
1135 free_volatile_objects(t, flags);
1137 ret = unmap_table(t, flags);
1139 ERROR_LOG("unmap_table failed: %d\n", ret);
1146 * Find out whether the given row number corresponds to an invalid row.
1148 * \param t Pointer to the osl table.
1149 * \param row_num The number of the row in question.
1151 * By definition, a row is considered invalid if all its index entries
1154 * \return Positive if \a row_num corresponds to an invalid row,
1155 * zero if it corresponds to a valid row, negative on errors.
1157 int row_is_invalid(struct osl_table *t, uint32_t row_num)
1160 int i, ret = get_row_index(t, row_num, &row_index);
1164 for (i = 0; i < t->row_index_size; i++) {
1165 if ((unsigned char)row_index[i] != 0xff)
1168 INFO_LOG("row %d is invalid\n", row_num);
1173 * Invalidate a row of an osl table.
1175 * \param t Pointer to an open osl table.
1176 * \param row_num Number of the row to mark as invalid.
1178 * This function marks each mapped object in the index entry of \a row as
1183 int mark_row_invalid(struct osl_table *t, uint32_t row_num)
1186 int ret = get_row_index(t, row_num, &row_index);
1190 INFO_LOG("marking row %d as invalid\n", row_num);
1191 memset(row_index, 0xff, t->row_index_size);
1196 * Initialize all rbtrees and compute number of invalid rows.
1198 * \param t The table containing the rbtrees to be initialized.
1202 int init_rbtrees(struct osl_table *t)
1205 const struct osl_column_description *cd;
1207 /* create rbtrees */
1208 FOR_EACH_RBTREE_COLUMN(i, t, cd)
1209 t->columns[i].rbtree = RB_ROOT;
1210 /* add valid rows to rbtrees */
1211 t->num_invalid_rows = 0;
1212 for (i = 0; i < t->num_rows; i++) {
1213 ret = row_is_invalid(t, i);
1217 t->num_invalid_rows++;
1220 ret = add_row_to_rbtrees(t, i, NULL, NULL);
1228 * Open an osl table.
1230 * Each osl table must be opened before its data can be accessed.
1232 * \param table_desc Describes the table to be opened.
1233 * \param result Contains a pointer to the open table on success.
1235 * The table description given by \a desc should coincide with the
1236 * description used at creation time.
1240 __export int osl_open_table(const struct osl_table_description *table_desc,
1241 struct osl_table **result)
1244 struct osl_table *t;
1245 const struct osl_column_description *cd;
1247 NOTICE_LOG("opening table %s\n", table_desc->name);
1248 ret = init_table_structure(table_desc, &t);
1251 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
1252 struct stat statbuf;
1253 char *dirname = column_filename(t, i);
1255 ret = -ERRNO_TO_ERROR(ENOMEM);
1258 /* check if directory exists */
1259 ret = stat(dirname, &statbuf);
1262 ret = -ERRNO_TO_ERROR(errno);
1265 ret = -ERRNO_TO_ERROR(ENOTDIR);
1266 if (!S_ISDIR(statbuf.st_mode))
1269 ret = map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1273 DEBUG_LOG("num rows: %d\n", t->num_rows);
1274 ret = init_rbtrees(t);
1276 osl_close_table(t, OSL_MARK_CLEAN); /* ignore further errors */
1287 static int create_disk_storage_object_dir(const struct osl_table *t,
1288 unsigned col_num, const char *ds_name)
1293 if (!(t->desc->flags & OSL_LARGE_TABLE))
1295 dirname = disk_storage_dirname(t, col_num, ds_name);
1297 return -ERRNO_TO_ERROR(ENOMEM);
1298 ret = para_mkdir(dirname, 0777);
1300 if (ret < 0 && !is_errno(-ret, EEXIST))
1305 static int write_disk_storage_file(const struct osl_table *t, unsigned col_num,
1306 const struct osl_object *obj, const char *ds_name)
1311 ret = create_disk_storage_object_dir(t, col_num, ds_name);
1314 filename = disk_storage_path(t, col_num, ds_name);
1316 return -ERRNO_TO_ERROR(ENOMEM);
1317 ret = write_file(filename, obj->data, obj->size);
1322 static int append_map_file(const struct osl_table *t, unsigned col_num,
1323 const struct osl_object *obj, uint32_t *new_size)
1325 char *filename = column_filename(t, col_num);
1327 char header = 0; /* zero means valid object */
1330 return -ERRNO_TO_ERROR(ENOMEM);
1331 ret = append_file(filename, &header, 1, obj->data, obj->size,
1337 static int append_row_index(const struct osl_table *t, char *row_index)
1342 if (!t->num_mapped_columns)
1344 filename = index_filename(t->desc);
1346 return -ERRNO_TO_ERROR(ENOMEM);
1347 ret = append_file(filename, NULL, 0, row_index,
1348 t->row_index_size, NULL);
1354 * A wrapper for truncate(2)
1356 * \param path Name of the regular file to truncate
1357 * \param size Number of bytes to \b shave \b off
1359 * Truncate the regular file named by \a path by \a size bytes.
1365 int para_truncate(const char *path, off_t size)
1368 struct stat statbuf;
1371 if (stat(path, &statbuf) < 0)
1373 ret = -E_OSL_BAD_SIZE;
1374 if (statbuf.st_size < size)
1377 if (truncate(path, statbuf.st_size - size) < 0)
1384 static int truncate_mapped_file(const struct osl_table *t, unsigned col_num,
1388 char *filename = column_filename(t, col_num);
1391 return -ERRNO_TO_ERROR(ENOMEM);
1392 ret = para_truncate(filename, size);
1397 static int delete_disk_storage_file(const struct osl_table *t, unsigned col_num,
1398 const char *ds_name)
1400 char *dirname, *filename = disk_storage_path(t, col_num, ds_name);
1404 return -ERRNO_TO_ERROR(ENOMEM);
1405 ret = unlink(filename);
1409 return -ERRNO_TO_ERROR(err);
1410 if (!(t->desc->flags & OSL_LARGE_TABLE))
1412 dirname = disk_storage_dirname(t, col_num, ds_name);
1414 return -ERRNO_TO_ERROR(ENOMEM);
1421 * Add a new row to an osl table and retrieve this row.
1423 * \param t Pointer to an open osl table.
1424 * \param objects Array of objects to be added.
1425 * \param row Result pointer.
1427 * The \a objects parameter must point to an array containing one object per
1428 * column. The order of the objects in the array is given by the table
1429 * description of \a table. Several sanity checks are performed during object
1430 * insertion and the function returns without modifying the table if any of
1431 * these tests fail. In fact, it is atomic in the sense that it either
1432 * succeeds or leaves the table unchanged (i.e. either all or none of the
1433 * objects are added to the table).
1435 * It is considered an error if an object is added to a column with associated
1436 * rbtree if this object is equal to an object already contained in that column
1437 * (i.e. the compare function for the column's rbtree returns zero).
1441 * \sa struct osl_table_description, osl_compare_func, osl_add_row().
1443 __export int osl_add_and_get_row(struct osl_table *t, struct osl_object *objects,
1444 struct osl_row **row)
1447 char *ds_name = NULL;
1448 struct rb_node **rb_parents = NULL, ***rb_links = NULL;
1449 char *new_row_index = NULL;
1450 struct osl_object *volatile_objs = NULL;
1451 const struct osl_column_description *cd;
1454 return -E_OSL_BAD_TABLE;
1455 rb_parents = malloc(t->num_rbtrees * sizeof(struct rn_node*));
1457 return -ERRNO_TO_ERROR(ENOMEM);
1458 rb_links = malloc(t->num_rbtrees * sizeof(struct rn_node**));
1461 return -ERRNO_TO_ERROR(ENOMEM);
1463 if (t->num_mapped_columns) {
1464 new_row_index = malloc(t->row_index_size);
1465 if (!new_row_index) {
1468 return -ERRNO_TO_ERROR(ENOMEM);
1471 /* pass 1: sanity checks */
1472 // DEBUG_LOG("sanity tests: %p:%p\n", objects[0].data,
1473 // objects[1].data);
1474 FOR_EACH_COLUMN(i, t->desc, cd) {
1475 enum osl_storage_type st = cd->storage_type;
1476 enum osl_storage_flags sf = cd->storage_flags;
1478 // ret = -E_OSL_NULL_OBJECT;
1481 if (st == OSL_DISK_STORAGE)
1483 if (sf & OSL_RBTREE) {
1484 unsigned rbtree_num = t->columns[i].rbtree_num;
1485 ret = -E_OSL_RB_KEY_EXISTS;
1486 // DEBUG_LOG("checking whether %p exists\n",
1487 // objects[i].data);
1488 if (search_rbtree(objects + i, t, i,
1489 &rb_parents[rbtree_num],
1490 &rb_links[rbtree_num]) > 0)
1493 if (sf & OSL_FIXED_SIZE) {
1494 // DEBUG_LOG("fixed size. need: %zu, have: %d\n",
1495 // objects[i].size, cd->data_size);
1496 ret = -E_OSL_BAD_DATA_SIZE;
1497 if (objects[i].size != cd->data_size)
1501 if (t->num_disk_storage_columns) {
1502 ds_name = disk_storage_name_of_object(t,
1503 &objects[t->disk_storage_name_column]);
1504 ret = -ERRNO_TO_ERROR(ENOMEM);
1508 ret = unmap_table(t, OSL_MARK_CLEAN);
1511 // DEBUG_LOG("sanity tests passed%s\n", "");
1512 /* pass 2: create data files, append map data */
1513 FOR_EACH_COLUMN(i, t->desc, cd) {
1514 enum osl_storage_type st = cd->storage_type;
1515 if (st == OSL_NO_STORAGE)
1517 if (st == OSL_MAPPED_STORAGE) {
1519 struct osl_column *col = &t->columns[i];
1520 // DEBUG_LOG("appending object of size %zu\n",
1521 // objects[i].size);
1522 ret = append_map_file(t, i, objects + i, &new_size);
1525 update_cell_index(new_row_index, col, new_size,
1530 ret = write_disk_storage_file(t, i, objects + i, ds_name);
1534 ret = append_row_index(t, new_row_index);
1537 ret = map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1538 if (ret < 0) { /* truncate index and rollback changes */
1539 char *filename = index_filename(t->desc);
1541 para_truncate(filename, t->row_index_size);
1545 /* pass 3: add entry to rbtrees */
1546 if (t->num_volatile_columns) {
1547 ret = -ERRNO_TO_ERROR(ENOMEM);
1548 volatile_objs = calloc(t->num_volatile_columns,
1549 sizeof(struct osl_object));
1552 FOR_EACH_VOLATILE_COLUMN(i, t, cd)
1553 volatile_objs[t->columns[i].volatile_num] = objects[i];
1556 // DEBUG_LOG("adding new entry as row #%d\n", t->num_rows - 1);
1557 ret = add_row_to_rbtrees(t, t->num_rows - 1, volatile_objs, row);
1560 // DEBUG_LOG("added new entry as row #%d\n", t->num_rows - 1);
1563 rollback: /* rollback all changes made, ignore further errors */
1564 for (i--; i >= 0; i--) {
1565 cd = get_column_description(t->desc, i);
1566 enum osl_storage_type st = cd->storage_type;
1567 if (st == OSL_NO_STORAGE)
1570 if (st == OSL_MAPPED_STORAGE)
1571 truncate_mapped_file(t, i, objects[i].size);
1572 else /* disk storage */
1573 delete_disk_storage_file(t, i, ds_name);
1575 /* ignore error and return previous error value */
1576 map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1578 free(new_row_index);
1586 * Add a new row to an osl table.
1588 * \param t Same meaning as osl_add_and_get_row().
1589 * \param objects Same meaning as osl_add_and_get_row().
1591 * \return The return value of the underlying call to osl_add_and_get_row().
1593 * This is equivalent to osl_add_and_get_row(t, objects, NULL).
1595 __export int osl_add_row(struct osl_table *t, struct osl_object *objects)
1597 return osl_add_and_get_row(t, objects, NULL);
1601 * Retrieve an object identified by row and column
1603 * \param t Pointer to an open osl table.
1604 * \param r Pointer to the row.
1605 * \param col_num The column number.
1606 * \param object The result pointer.
1608 * The column determined by \a col_num must be of type \p OSL_MAPPED_STORAGE
1609 * or \p OSL_NO_STORAGE, i.e. no disk storage objects may be retrieved by this
1614 * \sa osl_storage_type, osl_open_disk_object().
1616 __export int osl_get_object(const struct osl_table *t, const struct osl_row *r,
1617 unsigned col_num, struct osl_object *object)
1619 const struct osl_column_description *cd;
1622 return -E_OSL_BAD_TABLE;
1623 cd = get_column_description(t->desc, col_num);
1624 /* col must not be disk storage */
1625 if (cd->storage_type == OSL_DISK_STORAGE)
1626 return -E_OSL_BAD_STORAGE_TYPE;
1627 if (cd->storage_type == OSL_MAPPED_STORAGE)
1628 return get_mapped_object(t, col_num, r->num, object);
1630 *object = r->volatile_objects[t->columns[col_num].volatile_num];
1634 static int mark_mapped_object_invalid(const struct osl_table *t,
1635 uint32_t row_num, unsigned col_num)
1637 struct osl_object obj;
1639 int ret = get_mapped_object(t, col_num, row_num, &obj);
1650 * Delete a row from an osl table.
1652 * \param t Pointer to an open osl table.
1653 * \param row Pointer to the row to delete.
1655 * This removes all disk storage objects, removes all rbtree nodes, and frees
1656 * all volatile objects belonging to the given row. For mapped columns, the
1657 * data is merely marked invalid and may be pruned from time to time by
1662 __export int osl_del_row(struct osl_table *t, struct osl_row *row)
1664 struct osl_row *r = row;
1666 const struct osl_column_description *cd;
1669 return -E_OSL_BAD_TABLE;
1670 INFO_LOG("deleting row %p\n", row);
1672 if (t->num_disk_storage_columns) {
1674 ret = disk_storage_name_of_row(t, r, &ds_name);
1677 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd)
1678 delete_disk_storage_file(t, i, ds_name);
1681 FOR_EACH_COLUMN(i, t->desc, cd) {
1682 struct osl_column *col = t->columns + i;
1683 enum osl_storage_type st = cd->storage_type;
1684 remove_rb_node(t, i, r);
1685 if (st == OSL_MAPPED_STORAGE) {
1686 mark_mapped_object_invalid(t, r->num, i);
1689 if (st == OSL_NO_STORAGE && !(cd->storage_flags & OSL_DONT_FREE))
1690 free(r->volatile_objects[col->volatile_num].data);
1692 if (t->num_mapped_columns) {
1693 ret = mark_row_invalid(t, r->num);
1696 t->num_invalid_rows++;
1701 free(r->volatile_objects);
1706 /* test if column has an rbtree */
1707 static int check_rbtree_col(const struct osl_table *t, unsigned col_num,
1708 struct osl_column **col)
1711 return -E_OSL_BAD_TABLE;
1712 if (!(get_column_description(t->desc, col_num)->storage_flags & OSL_RBTREE))
1713 return -E_OSL_BAD_STORAGE_FLAGS;
1714 *col = t->columns + col_num;
1719 * Get the row that contains the given object.
1721 * \param t Pointer to an open osl table.
1722 * \param col_num The number of the column to be searched.
1723 * \param obj The object to be looked up.
1724 * \param result Points to the row containing \a obj.
1726 * Lookup \a obj in \a t and return the row containing \a obj. The column
1727 * specified by \a col_num must have an associated rbtree.
1731 * \sa osl_storage_flags
1733 __export int osl_get_row(const struct osl_table *t, unsigned col_num,
1734 const struct osl_object *obj, struct osl_row **result)
1737 struct rb_node *node;
1738 struct osl_row *row;
1739 struct osl_column *col;
1742 ret = check_rbtree_col(t, col_num, &col);
1745 ret = search_rbtree(obj, t, col_num, &node, NULL);
1748 row = get_row_pointer(node, t->columns[col_num].rbtree_num);
1753 static int rbtree_loop(struct osl_column *col, void *private_data,
1754 osl_rbtree_loop_func *func)
1756 struct rb_node *n, *tmp;
1758 /* this for-loop is safe against removal of an entry */
1759 for (n = rb_first(&col->rbtree), tmp = n? rb_next(n) : NULL;
1761 n = tmp, tmp = tmp? rb_next(tmp) : NULL) {
1762 struct osl_row *r = get_row_pointer(n, col->rbtree_num);
1763 if (func(r, private_data) < 0)
1769 static int rbtree_loop_reverse(struct osl_column *col, void *private_data,
1770 osl_rbtree_loop_func *func)
1772 struct rb_node *n, *tmp;
1774 /* safe against removal of an entry */
1775 for (n = rb_last(&col->rbtree), tmp = n? rb_prev(n) : NULL;
1777 n = tmp, tmp = tmp? rb_prev(tmp) : NULL) {
1778 struct osl_row *r = get_row_pointer(n, col->rbtree_num);
1779 if (func(r, private_data) < 0)
1786 * Loop over all nodes in an rbtree.
1788 * \param t Pointer to an open osl table.
1789 * \param col_num The column to use for iterating over the elements.
1790 * \param private_data Pointer that gets passed to \a func.
1791 * \param func The function to be called for each node in the rbtree.
1793 * This function does an in-order walk of the rbtree associated with \a
1794 * col_num. It is an error if the \p OSL_RBTREE flag is not set for this
1795 * column. For each node in the rbtree, the given function \a func is called
1796 * with two pointers as arguments: The first osl_row* argument points to the
1797 * row that contains the object corresponding to the rbtree node currently
1798 * traversed, and the \a private_data pointer is passed verbatim to \a func as the
1799 * second argument. The loop terminates either if \a func returns a negative
1800 * value, or if all nodes of the tree have been visited.
1803 * \return Standard. If the termination of the loop was caused by \a func
1804 * returning a negative value, \p -E_OSL_LOOP is returned.
1806 * \sa osl_storage_flags, osl_rbtree_loop_reverse(), osl_compare_func.
1808 __export int osl_rbtree_loop(const struct osl_table *t, unsigned col_num,
1809 void *private_data, osl_rbtree_loop_func *func)
1811 struct osl_column *col;
1813 int ret = check_rbtree_col(t, col_num, &col);
1816 return rbtree_loop(col, private_data, func);
1820 * Loop over all nodes in an rbtree in reverse order.
1822 * \param t Identical meaning as in \p osl_rbtree_loop().
1823 * \param col_num Identical meaning as in \p osl_rbtree_loop().
1824 * \param private_data Identical meaning as in \p osl_rbtree_loop().
1825 * \param func Identical meaning as in \p osl_rbtree_loop().
1827 * This function is identical to \p osl_rbtree_loop(), the only difference
1828 * is that the tree is walked in reverse order.
1830 * \return The same return value as \p osl_rbtree_loop().
1832 * \sa osl_rbtree_loop().
1834 __export int osl_rbtree_loop_reverse(const struct osl_table *t, unsigned col_num,
1835 void *private_data, osl_rbtree_loop_func *func)
1837 struct osl_column *col;
1839 int ret = check_rbtree_col(t, col_num, &col);
1842 return rbtree_loop_reverse(col, private_data, func);
1845 /* TODO: Rollback changes on errors */
1846 static int rename_disk_storage_objects(struct osl_table *t,
1847 struct osl_object *old_obj, struct osl_object *new_obj)
1850 const struct osl_column_description *cd;
1851 char *old_ds_name, *new_ds_name;
1853 if (!t->num_disk_storage_columns)
1854 return 1; /* nothing to do */
1855 if (old_obj->size == new_obj->size && !memcmp(new_obj->data,
1856 old_obj->data, new_obj->size))
1857 return 1; /* object did not change */
1858 old_ds_name = disk_storage_name_of_object(t, old_obj);
1859 new_ds_name = disk_storage_name_of_object(t, new_obj);
1860 ret = -ERRNO_TO_ERROR(ENOMEM);
1861 if (!old_ds_name || ! new_ds_name)
1864 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
1865 char *old_filename, *new_filename;
1866 ret = create_disk_storage_object_dir(t, i, new_ds_name);
1869 old_filename = disk_storage_path(t, i, old_ds_name);
1870 new_filename = disk_storage_path(t, i, new_ds_name);
1871 if (!old_filename || !new_filename)
1872 ret = -ERRNO_TO_ERROR(ENOMEM);
1874 ret = para_rename(old_filename, new_filename);
1889 * Change an object in an osl table.
1891 * \param t Pointer to an open osl table.
1892 * \param r Pointer to the row containing the object to be updated.
1893 * \param col_num Number of the column containing the object to be updated.
1894 * \param obj Pointer to the replacement object.
1896 * This function gets rid of all references to the old object. This includes
1897 * removal of the rbtree node in case there is an rbtree associated with \a
1898 * col_num. It then inserts \a obj into the table and the rbtree if necessary.
1900 * If the \p OSL_RBTREE flag is set for \a col_num, you \b MUST call this
1901 * function in order to change the contents of an object, even for volatile or
1902 * mapped columns of constant size (which may be updated directly if \p
1903 * OSL_RBTREE is not set). Otherwise the rbtree might become corrupted.
1907 __export int osl_update_object(struct osl_table *t, const struct osl_row *r,
1908 unsigned col_num, struct osl_object *obj)
1910 struct osl_column *col;
1911 const struct osl_column_description *cd;
1915 return -E_OSL_BAD_TABLE;
1916 col = &t->columns[col_num];
1917 cd = get_column_description(t->desc, col_num);
1918 DEBUG_LOG("updating column %u of %s\n", col_num, t->desc->name);
1919 if (cd->storage_flags & OSL_RBTREE) {
1920 if (search_rbtree(obj, t, col_num, NULL, NULL) > 0)
1921 return -E_OSL_RB_KEY_EXISTS;
1923 if (cd->storage_flags & OSL_FIXED_SIZE) {
1924 if (obj->size != cd->data_size)
1925 return -E_OSL_BAD_DATA_SIZE;
1927 remove_rb_node(t, col_num, r);
1928 if (cd->storage_type == OSL_NO_STORAGE) { /* TODO: If fixed size, reuse object? */
1929 free(r->volatile_objects[col->volatile_num].data);
1930 r->volatile_objects[col->volatile_num] = *obj;
1931 } else if (cd->storage_type == OSL_DISK_STORAGE) {
1933 ret = disk_storage_name_of_row(t, r, &ds_name);
1936 ret = delete_disk_storage_file(t, col_num, ds_name);
1937 if (ret < 0 && !is_errno(-ret, ENOENT)) {
1941 ret = write_disk_storage_file(t, col_num, obj, ds_name);
1945 } else { /* mapped storage */
1946 struct osl_object old_obj;
1947 ret = get_mapped_object(t, col_num, r->num, &old_obj);
1951 * If the updated column is the disk storage name column, the
1952 * disk storage name changes, so we have to rename all disk
1953 * storage objects accordingly.
1955 if (col_num == t->disk_storage_name_column) {
1956 ret = rename_disk_storage_objects(t, &old_obj, obj);
1960 if (cd->storage_flags & OSL_FIXED_SIZE)
1961 memcpy(old_obj.data, obj->data, cd->data_size);
1962 else { /* TODO: if the size doesn't change, use old space */
1963 uint32_t new_data_map_size;
1965 ret = get_row_index(t, r->num, &row_index);
1968 ret = mark_mapped_object_invalid(t, r->num, col_num);
1971 unmap_column(t, col_num);
1972 ret = append_map_file(t, col_num, obj,
1973 &new_data_map_size);
1976 ret = map_column(t, col_num);
1979 update_cell_index(row_index, col, new_data_map_size,
1983 if (cd->storage_flags & OSL_RBTREE) {
1984 ret = insert_rbtree(t, col_num, r, obj);
1992 * Retrieve an object of type \p OSL_DISK_STORAGE by row and column.
1994 * \param t Pointer to an open osl table.
1995 * \param r Pointer to the row containing the object.
1996 * \param col_num The column number.
1997 * \param obj Points to the result upon successful return.
1999 * For columns of type \p OSL_DISK_STORAGE, this function must be used to
2000 * retrieve one of its containing objects. Afterwards, osl_close_disk_object()
2001 * must be called in order to deallocate the resources.
2005 * \sa osl_get_object(), osl_storage_type, osl_close_disk_object().
2007 __export int osl_open_disk_object(const struct osl_table *t, const struct osl_row *r,
2008 unsigned col_num, struct osl_object *obj)
2010 const struct osl_column_description *cd;
2011 char *ds_name, *filename;
2015 return -E_OSL_BAD_TABLE;
2016 cd = get_column_description(t->desc, col_num);
2017 if (cd->storage_type != OSL_DISK_STORAGE)
2018 return -E_OSL_BAD_STORAGE_TYPE;
2020 ret = disk_storage_name_of_row(t, r, &ds_name);
2023 filename = disk_storage_path(t, col_num, ds_name);
2026 return -ERRNO_TO_ERROR(ENOMEM);
2027 DEBUG_LOG("filename: %s\n", filename);
2028 ret = mmap_full_file(filename, O_RDONLY, &obj->data, &obj->size, NULL);
2034 * Free resources that were allocated during osl_open_disk_object().
2036 * \param obj Pointer to the object previously returned by open_disk_object().
2038 * \return The return value of the underlying call to para_munmap().
2040 * \sa para_munmap().
2042 __export int osl_close_disk_object(struct osl_object *obj)
2044 return para_munmap(obj->data, obj->size);
2048 * Get the number of rows of the given table.
2050 * \param t Pointer to an open osl table.
2051 * \param num_rows Result is returned here.
2053 * The number of rows returned via \a num_rows excluding any invalid rows.
2055 * \return Positive on success, \p -E_OSL_BAD_TABLE if \a t is \p NULL.
2057 __export int osl_get_num_rows(const struct osl_table *t, unsigned *num_rows)
2060 return -E_OSL_BAD_TABLE;
2061 assert(t->num_rows >= t->num_invalid_rows);
2062 *num_rows = t->num_rows - t->num_invalid_rows;
2067 * Get the rank of a row.
2069 * \param t An open osl table.
2070 * \param r The row to get the rank of.
2071 * \param col_num The number of an rbtree column.
2072 * \param rank Result pointer.
2074 * The rank is, by definition, the position of the row in the linear order
2075 * determined by an in-order tree walk of the rbtree associated with column
2076 * number \a col_num of \a table.
2080 * \sa osl_get_nth_row().
2082 __export int osl_get_rank(const struct osl_table *t, struct osl_row *r,
2083 unsigned col_num, unsigned *rank)
2085 struct osl_object obj;
2086 struct osl_column *col;
2087 struct rb_node *node;
2088 int ret = check_rbtree_col(t, col_num, &col);
2092 ret = osl_get_object(t, r, col_num, &obj);
2095 ret = search_rbtree(&obj, t, col_num, &node, NULL);
2098 ret = rb_rank(node, rank);
2100 return -E_OSL_BAD_ROW;
2105 * Get the row with n-th greatest value.
2107 * \param t Pointer to an open osl table.
2108 * \param col_num The column number.
2109 * \param n The rank of the desired row.
2110 * \param result Row is returned here.
2112 * Retrieve the n-th order statistic with respect to the compare function
2113 * of the rbtree column \a col_num. In other words, get that row with
2114 * \a n th greatest value in column \a col_num. It's an error if
2115 * \a col_num is not a rbtree column, or if \a n is larger than the
2116 * number of rows in the table.
2120 * \sa osl_storage_flags, osl_compare_func, osl_get_row(),
2121 * osl_rbtree_last_row(), osl_rbtree_first_row(), osl_get_rank().
2123 __export int osl_get_nth_row(const struct osl_table *t, unsigned col_num,
2124 unsigned n, struct osl_row **result)
2126 struct osl_column *col;
2127 struct rb_node *node;
2132 return -E_OSL_RB_KEY_NOT_FOUND;
2133 ret = osl_get_num_rows(t, &num_rows);
2137 return -E_OSL_RB_KEY_NOT_FOUND;
2138 ret = check_rbtree_col(t, col_num, &col);
2141 node = rb_nth(col->rbtree.rb_node, n);
2143 return -E_OSL_RB_KEY_NOT_FOUND;
2144 *result = get_row_pointer(node, col->rbtree_num);
2149 * Get the row corresponding to the smallest rbtree node of a column.
2151 * \param t An open rbtree table.
2152 * \param col_num The number of the rbtree column.
2153 * \param result A pointer to the first row is returned here.
2155 * The rbtree node of the smallest object (with respect to the corresponding
2156 * compare function) is selected and the row containing this object is
2157 * returned. It is an error if \a col_num refers to a column without an
2158 * associated rbtree.
2162 * \sa osl_get_nth_row(), osl_rbtree_last_row().
2164 __export int osl_rbtree_first_row(const struct osl_table *t, unsigned col_num,
2165 struct osl_row **result)
2167 return osl_get_nth_row(t, col_num, 1, result);
2171 * Get the row corresponding to the greatest rbtree node of a column.
2173 * \param t The same meaning as in \p osl_rbtree_first_row().
2174 * \param col_num The same meaning as in \p osl_rbtree_first_row().
2175 * \param result The same meaning as in \p osl_rbtree_first_row().
2177 * This function works just like osl_rbtree_first_row(), the only difference
2178 * is that the row containing the greatest rather than the smallest object is
2183 * \sa osl_get_nth_row(), osl_rbtree_first_row().
2185 __export int osl_rbtree_last_row(const struct osl_table *t, unsigned col_num,
2186 struct osl_row **result)
2189 int ret = osl_get_num_rows(t, &num_rows);
2193 return osl_get_nth_row(t, col_num, num_rows, result);