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() */
19 * Allocate a sufficiently large string and print into it.
21 * \param fmt A usual format string.
23 * Produce output according to \p fmt. No artificial bound on the length of the
24 * resulting string is imposed.
26 * \return This function either returns a pointer to a string that must be
27 * freed by the caller or \p NULL if memory allocation failed.
31 static __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
35 char *p = malloc(size);
42 /* Try to print in the allocated space. */
44 n = vsnprintf(p, size, fmt, ap);
46 /* If that worked, return the string. */
47 if (n > -1 && n < size)
49 /* Else try again with more space. */
50 if (n > -1) /* glibc 2.1 */
51 size = n + 1; /* precisely what is needed */
53 size *= 2; /* twice the old size */
67 * \param fmt Usual format string.
69 * All XXX_LOG() macros use this function.
71 __printf_2_3 void __log(int ll, const char* fmt,...)
84 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
85 fprintf(outfd, "%s ", str);
87 vfprintf(outfd, fmt, argp);
92 * A wrapper for lseek(2).
94 * \param fd The file descriptor whose offset is to be to repositioned.
95 * \param offset A value-result parameter.
96 * \param whence Usual repositioning directive.
98 * Reposition the offset of the file descriptor \a fd to the argument \a offset
99 * according to the directive \a whence. Upon successful return, \a offset
100 * contains the resulting offset location as measured in bytes from the
101 * beginning of the file.
103 * \return Positive on success. Otherwise, the function returns \p -E_LSEEK.
107 static int __lseek(int fd, off_t *offset, int whence)
109 *offset = lseek(fd, *offset, whence);
117 * Wrapper for the write system call.
119 * \param fd The file descriptor to write to.
120 * \param buf The buffer to write.
121 * \param size The length of \a buf in bytes.
123 * This function writes out the given buffer and retries if an interrupt
124 * occurred during the write.
126 * \return On success, the number of bytes written is returned, otherwise, the
127 * function returns \p -E_WRITE.
131 static ssize_t __write(int fd, const void *buf, size_t size)
136 ret = write(fd, buf, size);
137 if ((ret < 0) && (errno == EAGAIN || errno == EINTR))
139 return ret >= 0? ret : -E_WRITE;
144 * Write the whole buffer to a file descriptor.
146 * \param fd The file descriptor to write to.
147 * \param buf The buffer to write.
148 * \param size The length of \a buf in bytes.
150 * This function writes the given buffer and continues on short writes and
151 * when interrupted by a signal.
153 * \return Positive on success, negative on errors. Possible errors: any
154 * errors returned by para_write().
158 static ssize_t write_all(int fd, const void *buf, size_t size)
160 // DEBUG_LOG("writing %zu bytes\n", size);
163 ssize_t ret = __write(fd, b, size);
164 // DEBUG_LOG("ret: %zd\n", ret);
173 * Open a file, write the given buffer and close the file.
175 * \param filename Full path to the file to open.
176 * \param buf The buffer to write to the file.
177 * \param size The size of \a buf.
179 * \return Positive on success, negative on errors. Possible errors include:
180 * any errors from para_open() or para_write().
182 * \sa para_open(), para_write().
184 static int write_file(const char *filename, const void *buf, size_t size)
188 ret = para_open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
192 ret = write_all(fd, buf, size);
201 static int append_file(const char *filename, char *header, size_t header_size,
202 char *data, size_t data_size, uint32_t *new_pos)
206 // DEBUG_LOG("appending %zu + %zu bytes\n", header_size, data_size);
207 ret = para_open(filename, O_WRONLY | O_CREAT | O_APPEND, 0644);
211 if (header && header_size) {
212 ret = write_all(fd, header, header_size);
216 ret = write_all(fd, data, data_size);
221 ret = __lseek(fd, &offset, SEEK_END);
224 // DEBUG_LOG("new file size: " FMT_OFF_T "\n", offset);
233 static int verify_name(const char *name)
239 if (strchr(name, '/'))
241 if (!strcmp(name, ".."))
243 if (!strcmp(name, "."))
249 * Compare two osl objects pointing to unsigned integers of 32 bit size.
251 * \param obj1 Pointer to the first integer.
252 * \param obj2 Pointer to the second integer.
254 * \return The values required for an osl compare function.
256 * \sa osl_compare_func, osl_hash_compare().
258 int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2)
260 uint32_t d1 = read_u32((const char *)obj1->data);
261 uint32_t d2 = read_u32((const char *)obj2->data);
271 * Compare two osl objects pointing to hash values.
273 * \param obj1 Pointer to the first hash object.
274 * \param obj2 Pointer to the second hash object.
276 * \return The values required for an osl compare function.
278 * \sa osl_compare_func, uint32_compare().
280 int osl_hash_compare(const struct osl_object *obj1, const struct osl_object *obj2)
282 return hash_compare((HASH_TYPE *)obj1->data, (HASH_TYPE *)obj2->data);
285 static char *disk_storage_dirname(const struct osl_table *t, unsigned col_num,
288 char *dirname, *column_name = column_filename(t, col_num);
292 if (!(t->desc->flags & OSL_LARGE_TABLE))
294 dirname = make_message("%s/%.2s", column_name, ds_name);
299 static char *disk_storage_name_of_object(const struct osl_table *t,
300 const struct osl_object *obj)
302 HASH_TYPE hash[HASH_SIZE];
303 hash_object(obj, hash);
304 return disk_storage_name_of_hash(t, hash);
307 static int disk_storage_name_of_row(const struct osl_table *t,
308 const struct osl_row *row, char **name)
310 struct osl_object obj;
311 int ret = osl_get_object(t, row, t->disk_storage_name_column, &obj);
315 *name = disk_storage_name_of_object(t, &obj);
318 return -ERRNO_TO_ERROR(ENOMEM);
321 static void column_name_hash(const char *col_name, HASH_TYPE *hash)
323 hash_function(col_name, strlen(col_name), hash);
326 static int init_column_descriptions(struct osl_table *t)
329 const struct osl_column_description *cd;
331 ret = -E_BAD_TABLE_DESC;
332 ret = verify_name(t->desc->name);
336 if (!t->desc->dir && (t->num_disk_storage_columns || t->num_mapped_columns))
338 /* the size of the index header without column descriptions */
339 t->index_header_size = IDX_COLUMN_DESCRIPTIONS;
340 FOR_EACH_COLUMN(i, t->desc, cd) {
341 struct osl_column *col = t->columns + i;
342 if (cd->storage_flags & OSL_RBTREE) {
343 if (!cd->compare_function)
344 return -E_NO_COMPARE_FUNC;
346 if (cd->storage_type == OSL_NO_STORAGE)
348 ret = -E_NO_COLUMN_NAME;
349 if (!cd->name || !cd->name[0])
351 ret = verify_name(cd->name);
354 t->index_header_size += index_column_description_size(cd->name);
355 column_name_hash(cd->name, col->name_hash);
356 ret = -E_DUPLICATE_COL_NAME;
357 for (j = i + 1; j < t->desc->num_columns; j++) {
358 const char *name2 = get_column_description(t->desc,
360 if (cd->name && name2 && !strcmp(cd->name, name2))
370 * Initialize a struct table from given table description.
372 * \param desc The description of the osl table.
373 * \param table_ptr Result is returned here.
375 * This function performs several sanity checks on \p desc and returns if any
376 * of these tests fail. On success, a struct \p osl_table is allocated and
377 * initialized with data derived from \p desc.
379 * \return Positive on success, negative on errors. Possible errors include: \p
380 * E_BAD_TABLE_DESC, \p E_NO_COLUMN_DESC, \p E_NO_COLUMNS, \p
381 * E_BAD_STORAGE_TYPE, \p E_BAD_STORAGE_FLAGS, \p E_BAD_STORAGE_SIZE, \p
382 * E_NO_UNIQUE_RBTREE_COLUMN, \p E_NO_RBTREE_COL.
384 * \sa struct osl_table.
386 int init_table_structure(const struct osl_table_description *desc,
387 struct osl_table **table_ptr)
389 const struct osl_column_description *cd;
390 struct osl_table *t = calloc(1, sizeof(*t));
391 int i, ret = -ERRNO_TO_ERROR(ENOMEM), have_disk_storage_name_column = 0;
395 ret = -E_BAD_TABLE_DESC;
398 DEBUG_LOG("creating table structure for '%s' from table "
399 "description\n", desc->name);
400 ret = -E_NO_COLUMN_DESC;
401 if (!desc->column_descriptions)
404 if (!desc->num_columns)
406 ret = -ERRNO_TO_ERROR(ENOMEM);
407 t->columns = calloc(desc->num_columns, sizeof(struct osl_column));
411 FOR_EACH_COLUMN(i, t->desc, cd) {
412 enum osl_storage_type st = cd->storage_type;
413 enum osl_storage_flags sf = cd->storage_flags;
414 struct osl_column *col = &t->columns[i];
416 ret = -E_BAD_STORAGE_TYPE;
417 if (st != OSL_MAPPED_STORAGE && st != OSL_DISK_STORAGE
418 && st != OSL_NO_STORAGE)
420 ret = -E_BAD_STORAGE_FLAGS;
421 if (st == OSL_DISK_STORAGE && sf & OSL_RBTREE)
423 ret = -E_BAD_STORAGE_SIZE;
424 if (sf & OSL_FIXED_SIZE && !cd->data_size)
427 case OSL_DISK_STORAGE:
428 t->num_disk_storage_columns++;
430 case OSL_MAPPED_STORAGE:
431 t->num_mapped_columns++;
432 col->index_offset = t->row_index_size;
433 t->row_index_size += 8;
436 col->volatile_num = t->num_volatile_columns;
437 t->num_volatile_columns++;
440 if (sf & OSL_RBTREE) {
441 col->rbtree_num = t->num_rbtrees;
443 if ((sf & OSL_UNIQUE) && (st == OSL_MAPPED_STORAGE)) {
444 if (!have_disk_storage_name_column)
445 t->disk_storage_name_column = i;
446 have_disk_storage_name_column = 1;
450 ret = -E_NO_UNIQUE_RBTREE_COLUMN;
451 if (t->num_disk_storage_columns && !have_disk_storage_name_column)
453 ret = -E_NO_RBTREE_COL;
457 DEBUG_LOG("OK. Index entry size: %u\n", t->row_index_size);
458 ret = init_column_descriptions(t);
470 * Read the table description from index header.
472 * \param map The memory mapping of the index file.
473 * \param desc The values found in the index header are returned here.
475 * Read the index header, check for the paraslash magic string and the table version number.
476 * Read all information stored in the index header into \a desc.
478 * \return Positive on success, negative on errors.
480 * \sa struct osl_table_description, osl_create_table.
482 int read_table_desc(struct osl_object *map, struct osl_table_description *desc)
484 char *buf = map->data;
486 uint16_t header_size;
489 struct osl_column_description *cd;
491 if (map->size < MIN_INDEX_HEADER_SIZE(1))
492 return -E_SHORT_TABLE;
493 if (strncmp(buf + IDX_PARA_MAGIC, PARA_MAGIC, strlen(PARA_MAGIC)))
495 version = read_u8(buf + IDX_VERSION);
496 if (version < MIN_TABLE_VERSION || version > MAX_TABLE_VERSION)
497 return -E_VERSION_MISMATCH;
498 desc->flags = read_u8(buf + IDX_TABLE_FLAGS);
499 desc->num_columns = read_u16(buf + IDX_NUM_COLUMNS);
500 DEBUG_LOG("%u columns\n", desc->num_columns);
501 if (!desc->num_columns)
502 return -E_NO_COLUMNS;
503 header_size = read_u16(buf + IDX_HEADER_SIZE);
504 if (map->size < header_size)
506 desc->column_descriptions = calloc(desc->num_columns,
507 sizeof(struct osl_column_description));
508 if (!desc->column_descriptions)
509 return -ERRNO_TO_ERROR(ENOMEM);
510 offset = IDX_COLUMN_DESCRIPTIONS;
511 FOR_EACH_COLUMN(i, desc, cd) {
514 ret = -E_SHORT_TABLE;
515 if (map->size < offset + MIN_IDX_COLUMN_DESCRIPTION_SIZE) {
516 ERROR_LOG("map size = %zu < %u = offset + min desc size\n",
517 map->size, offset + MIN_IDX_COLUMN_DESCRIPTION_SIZE);
520 cd->storage_type = read_u16(buf + offset + IDX_CD_STORAGE_TYPE);
521 cd->storage_flags = read_u16(buf + offset +
522 IDX_CD_STORAGE_FLAGS);
523 cd->data_size = read_u32(buf + offset + IDX_CD_DATA_SIZE);
524 null_byte = memchr(buf + offset + IDX_CD_NAME, '\0',
525 map->size - offset - IDX_CD_NAME);
526 ret = -E_INDEX_CORRUPTION;
529 ret = -ERRNO_TO_ERROR(ENOMEM);
530 cd->name = strdup(buf + offset + IDX_CD_NAME);
533 offset += index_column_description_size(cd->name);
535 if (offset != header_size) {
536 ret = -E_INDEX_CORRUPTION;
537 ERROR_LOG("real header size = %u != %u = stored header size\n",
538 offset, header_size);
543 FOR_EACH_COLUMN(i, desc, cd)
549 * check whether the table description given by \p t->desc matches the on-disk
550 * table structure stored in the index of \a t.
552 static int compare_table_descriptions(struct osl_table *t)
555 struct osl_table_description desc;
556 const struct osl_column_description *cd1, *cd2;
558 /* read the on-disk structure into desc */
559 ret = read_table_desc(&t->index_map, &desc);
562 ret = -E_BAD_TABLE_FLAGS;
563 if (desc.flags != t->desc->flags)
565 ret = -E_BAD_COLUMN_NUM;
566 if (desc.num_columns > t->desc->num_columns)
568 if (desc.num_columns < t->desc->num_columns) {
569 struct osl_column_description *cd;
570 unsigned diff = t->desc->num_columns - desc.num_columns;
571 INFO_LOG("extending table by %u volatile columns\n", diff);
572 ret = -ERRNO_TO_ERROR(ENOMEM);
573 desc.column_descriptions = realloc(desc.column_descriptions,
574 t->desc->num_columns * sizeof(struct osl_column_description));
575 if (!desc.column_descriptions)
577 for (i = desc.num_columns; i < t->desc->num_columns; i++) {
578 cd = get_column_description(&desc, i);
579 cd->storage_type = OSL_NO_STORAGE;
582 desc.num_columns += diff;
584 FOR_EACH_COLUMN(i, t->desc, cd1) {
585 cd2 = get_column_description(&desc, i);
586 ret = -E_BAD_STORAGE_TYPE;
587 if (cd1->storage_type != cd2->storage_type)
589 if (cd1->storage_type == OSL_NO_STORAGE)
591 ret = -E_BAD_STORAGE_FLAGS;
592 if (cd1->storage_flags != cd2->storage_flags) {
593 ERROR_LOG("sf1 = %u != %u = sf2\n",
594 cd1->storage_flags, cd2->storage_flags);
597 ret = -E_BAD_DATA_SIZE;
598 if (cd1->storage_flags & OSL_FIXED_SIZE)
599 if (cd1->data_size != cd2->data_size)
601 ret = -E_BAD_COLUMN_NAME;
602 if (strcmp(cd1->name, cd2->name))
605 DEBUG_LOG("table description of '%s' matches on-disk data, good\n",
609 FOR_EACH_COLUMN(i, &desc, cd1)
611 free(desc.column_descriptions);
615 static int create_table_index(struct osl_table *t)
617 char *buf, *filename;
619 size_t size = t->index_header_size;
620 const struct osl_column_description *cd;
623 INFO_LOG("creating %zu byte index for table %s\n", size,
625 buf = calloc(1, size);
627 return -ERRNO_TO_ERROR(ENOMEM);
628 sprintf(buf + IDX_PARA_MAGIC, "%s", PARA_MAGIC);
629 write_u8(buf + IDX_TABLE_FLAGS, t->desc->flags);
630 write_u8(buf + IDX_DIRTY_FLAG, 0);
631 write_u8(buf + IDX_VERSION, CURRENT_TABLE_VERSION);
632 write_u16(buf + IDX_NUM_COLUMNS, t->num_mapped_columns + t->num_disk_storage_columns);
633 write_u16(buf + IDX_HEADER_SIZE, t->index_header_size);
634 offset = IDX_COLUMN_DESCRIPTIONS;
635 FOR_EACH_COLUMN(i, t->desc, cd) {
636 /* no need to store info about volatile storage */
637 if (cd->storage_type == OSL_NO_STORAGE)
639 write_u16(buf + offset + IDX_CD_STORAGE_TYPE,
641 write_u16(buf + offset + IDX_CD_STORAGE_FLAGS,
643 if (cd->storage_flags & OSL_FIXED_SIZE)
644 write_u32(buf + offset + IDX_CD_DATA_SIZE,
646 strcpy(buf + offset + IDX_CD_NAME, cd->name);
647 offset += index_column_description_size(cd->name);
649 assert(offset = size);
650 filename = index_filename(t->desc);
652 ret = write_file(filename, buf, size);
654 ret = -ERRNO_TO_ERROR(ENOMEM);
661 * Create a new osl table.
663 * \param desc Pointer to the table description.
667 int osl_create_table(const struct osl_table_description *desc)
669 const struct osl_column_description *cd;
670 char *table_dir = NULL, *filename;
672 int i, ret = init_table_structure(desc, &t);
676 INFO_LOG("creating %s\n", desc->name);
677 FOR_EACH_COLUMN(i, t->desc, cd) {
678 if (cd->storage_type == OSL_NO_STORAGE)
681 ret = para_mkdir(desc->dir, 0777);
682 if (ret < 0 && !is_errno(-ret, EEXIST))
684 table_dir = make_message("%s/%s", desc->dir,
686 ret = -ERRNO_TO_ERROR(ENOMEM);
689 ret = para_mkdir(table_dir, 0777);
693 ret = -ERRNO_TO_ERROR(ENOMEM);
694 filename = column_filename(t, i);
697 INFO_LOG("filename: %s\n", filename);
698 if (cd->storage_type == OSL_MAPPED_STORAGE) {
699 ret = para_open(filename, O_RDWR | O_CREAT | O_EXCL,
708 ret = para_mkdir(filename, 0777);
713 if (t->num_mapped_columns) {
714 ret = create_table_index(t);
726 static int table_is_dirty(struct osl_table *t)
728 char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
729 uint8_t dirty = read_u8(buf) & 0x1;
733 static void mark_table_dirty(struct osl_table *t)
735 char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
736 write_u8(buf, read_u8(buf) | 1);
739 static void mark_table_clean(struct osl_table *t)
741 char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
742 write_u8(buf, read_u8(buf) & 0xfe);
745 static void unmap_column(struct osl_table *t, unsigned col_num)
747 struct osl_object map = t->columns[col_num].data_map;
751 ret = para_munmap(map.data, map.size);
757 * Unmap all mapped files of an osl table.
759 * \param t Pointer to a mapped table.
760 * \param flags Options for unmapping.
762 * \return Positive on success, negative on errors.
764 * \sa map_table(), enum osl_close_flags, para_munmap().
766 int unmap_table(struct osl_table *t, enum osl_close_flags flags)
769 const struct osl_column_description *cd;
772 if (!t->num_mapped_columns) /* can this ever happen? */
774 DEBUG_LOG("unmapping table '%s'\n", t->desc->name);
775 if (!t->index_map.data)
776 return -E_NOT_MAPPED;
777 if (flags & OSL_MARK_CLEAN)
779 ret = para_munmap(t->index_map.data, t->index_map.size);
782 t->index_map.data = NULL;
785 FOR_EACH_MAPPED_COLUMN(i, t, cd)
790 static int map_column(struct osl_table *t, unsigned col_num)
793 char *filename = column_filename(t, col_num);
797 return -ERRNO_TO_ERROR(ENOMEM);
798 if (stat(filename, &statbuf) < 0) {
802 if (!(S_IFREG & statbuf.st_mode)) {
806 ret = mmap_full_file(filename, O_RDWR,
807 &t->columns[col_num].data_map.data,
808 &t->columns[col_num].data_map.size,
815 * Map the index file and all columns of type \p OSL_MAPPED_STORAGE into memory.
817 * \param t Pointer to an initialized table structure.
818 * \param flags Mapping options.
820 * \return Negative return value on errors; on success the number of rows
821 * (including invalid rows) is returned.
823 * \sa unmap_table(), enum map_table_flags, osl_open_table(), mmap(2).
825 int map_table(struct osl_table *t, enum map_table_flags flags)
828 const struct osl_column_description *cd;
829 int i = 0, ret, num_rows = 0;
831 if (!t->num_mapped_columns)
833 if (t->index_map.data)
834 return -E_ALREADY_MAPPED;
835 filename = index_filename(t->desc);
837 return -ERRNO_TO_ERROR(ENOMEM);
838 DEBUG_LOG("mapping table '%s' (index: %s)\n", t->desc->name, filename);
839 ret = mmap_full_file(filename, flags & MAP_TBL_FL_MAP_RDONLY?
840 O_RDONLY : O_RDWR, &t->index_map.data, &t->index_map.size, NULL);
844 if (flags & MAP_TBL_FL_VERIFY_INDEX) {
845 ret = compare_table_descriptions(t);
850 if (!(flags & MAP_TBL_FL_IGNORE_DIRTY)) {
851 if (table_is_dirty(t)) {
852 ERROR_LOG("%s is dirty\n", t->desc->name);
857 num_rows = table_num_rows(t);
861 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
862 ret = map_column(t, i);
867 err: /* unmap what is already mapped */
868 for (i--; i >= 0; i--) {
869 struct osl_object map = t->columns[i].data_map;
870 para_munmap(map.data, map.size);
873 para_munmap(t->index_map.data, t->index_map.size);
874 t->index_map.data = NULL;
879 * Retrieve a mapped object by row and column number.
881 * \param t Pointer to an open osl table.
882 * \param col_num Number of the mapped column containing the object to retrieve.
883 * \param row_num Number of the row containing the object to retrieve.
884 * \param obj The result is returned here.
886 * It is considered an error if \a col_num does not refer to a column
887 * of storage type \p OSL_MAPPED_STORAGE.
889 * \return Positive on success, negative on errors. Possible errors include:
890 * \p E_BAD_ROW_NUM, \p E_INVALID_OBJECT.
892 * \sa osl_storage_type.
894 int get_mapped_object(const struct osl_table *t, unsigned col_num,
895 uint32_t row_num, struct osl_object *obj)
897 struct osl_column *col = &t->columns[col_num];
903 if (t->num_rows <= row_num)
904 return -E_BAD_ROW_NUM;
905 ret = get_cell_index(t, row_num, col_num, &cell_index);
908 offset = read_u32(cell_index);
909 obj->size = read_u32(cell_index + 4) - 1;
910 header = col->data_map.data + offset;
911 obj->data = header + 1;
912 if (read_u8(header) == 0xff) {
913 ERROR_LOG("col %u, size %zu, offset %u\n", col_num,
915 return -E_INVALID_OBJECT;
920 static int search_rbtree(const struct osl_object *obj,
921 const struct osl_table *t, unsigned col_num,
922 struct rb_node **result, struct rb_node ***rb_link)
924 struct osl_column *col = &t->columns[col_num];
925 struct rb_node **new = &col->rbtree.rb_node, *parent = NULL;
926 const struct osl_column_description *cd =
927 get_column_description(t->desc, col_num);
928 enum osl_storage_type st = cd->storage_type;
930 struct osl_row *this_row = get_row_pointer(*new,
933 struct osl_object this_obj;
935 if (st == OSL_MAPPED_STORAGE) {
936 ret = get_mapped_object(t, col_num, this_row->num,
941 this_obj = this_row->volatile_objects[col->volatile_num];
942 ret = cd->compare_function(obj, &this_obj);
945 *result = get_rb_node_pointer(this_row,
950 new = &((*new)->rb_left);
952 new = &((*new)->rb_right);
958 return -E_RB_KEY_NOT_FOUND;
961 static int insert_rbtree(struct osl_table *t, unsigned col_num,
962 const struct osl_row *row, const struct osl_object *obj)
964 struct rb_node *parent, **rb_link;
967 int ret = search_rbtree(obj, t, col_num, &parent, &rb_link);
970 return -E_RB_KEY_EXISTS;
971 rbtree_num = t->columns[col_num].rbtree_num;
972 n = get_rb_node_pointer(row, rbtree_num);
973 rb_link_node(n, parent, rb_link);
974 rb_insert_color(n, &t->columns[col_num].rbtree);
978 static void remove_rb_node(struct osl_table *t, unsigned col_num,
979 const struct osl_row *row)
981 struct osl_column *col = &t->columns[col_num];
982 const struct osl_column_description *cd =
983 get_column_description(t->desc, col_num);
984 enum osl_storage_flags sf = cd->storage_flags;
985 struct rb_node *victim, *splice_out_node, *tmp;
986 if (!(sf & OSL_RBTREE))
989 * Which node is removed/spliced out actually depends on how many
990 * children the victim node has: If it has no children, it gets
991 * deleted. If it has one child, it gets spliced out. If it has two
992 * children, its successor (which has at most a right child) gets
995 victim = get_rb_node_pointer(row, col->rbtree_num);
996 if (victim->rb_left && victim->rb_right)
997 splice_out_node = rb_next(victim);
999 splice_out_node = victim;
1000 /* Go up to the root and decrement the size of each node in the path. */
1001 for (tmp = splice_out_node; tmp; tmp = rb_parent(tmp))
1003 rb_erase(victim, &col->rbtree);
1006 static int add_row_to_rbtrees(struct osl_table *t, uint32_t row_num,
1007 struct osl_object *volatile_objs, struct osl_row **row_ptr)
1011 struct osl_row *row = allocate_row(t->num_rbtrees);
1012 const struct osl_column_description *cd;
1015 return -ERRNO_TO_ERROR(ENOMEM);
1017 row->volatile_objects = volatile_objs;
1018 FOR_EACH_RBTREE_COLUMN(i, t, cd) {
1019 if (cd->storage_type == OSL_MAPPED_STORAGE) {
1020 struct osl_object obj;
1021 ret = get_mapped_object(t, i, row_num, &obj);
1024 ret = insert_rbtree(t, i, row, &obj);
1025 } else { /* volatile */
1026 const struct osl_object *obj
1027 = volatile_objs + t->columns[i].volatile_num;
1028 ret = insert_rbtree(t, i, row, obj);
1036 err: /* rollback changes, i.e. remove added entries from rbtrees */
1038 remove_rb_node(t, i--, row);
1043 static void free_volatile_objects(const struct osl_table *t,
1044 enum osl_close_flags flags)
1048 struct osl_column *rb_col;
1049 const struct osl_column_description *cd;
1051 if (!t->num_volatile_columns)
1053 /* find the first rbtree column (any will do) */
1054 FOR_EACH_RBTREE_COLUMN(i, t, cd)
1056 rb_col = t->columns + i;
1057 /* walk that rbtree and free all volatile objects */
1058 for (n = rb_first(&rb_col->rbtree); n; n = rb_next(n)) {
1059 struct osl_row *r = get_row_pointer(n, rb_col->rbtree_num);
1060 if (flags & OSL_FREE_VOLATILE)
1061 FOR_EACH_VOLATILE_COLUMN(j, t, cd) {
1062 if (cd->storage_flags & OSL_DONT_FREE)
1064 free(r->volatile_objects[
1065 t->columns[j].volatile_num].data);
1067 // for (j = 0; j < t->num_volatile_columns; j++)
1068 // free(r->volatile_objects[j].data);
1069 free(r->volatile_objects);
1074 * Erase all rbtree nodes and free resources.
1076 * \param t Pointer to an open osl table.
1078 * This function is called by osl_close_table().
1080 void clear_rbtrees(struct osl_table *t)
1082 const struct osl_column_description *cd;
1083 unsigned i, rbtrees_cleared = 0;
1085 FOR_EACH_RBTREE_COLUMN(i, t, cd) {
1086 struct osl_column *col = &t->columns[i];
1089 for (n = rb_first(&col->rbtree); n;) {
1091 rb_erase(n, &col->rbtree);
1092 if (rbtrees_cleared == t->num_rbtrees) {
1093 r = get_row_pointer(n, col->rbtree_num);
1104 * Close an osl table.
1106 * \param t Pointer to the table to be closed.
1107 * \param flags Options for what should be cleaned up.
1109 * If osl_open_table() succeeds, the resulting table pointer must later be
1110 * passed to this function in order to flush all changes to the file system and
1111 * to free the resources that were allocated by osl_open_table().
1113 * \return Positive on success, negative on errors. Possible errors: \p E_BAD_TABLE,
1114 * errors returned by unmap_table().
1116 * \sa osl_open_table(), unmap_table().
1118 int osl_close_table(struct osl_table *t, enum osl_close_flags flags)
1123 return -E_BAD_TABLE;
1124 free_volatile_objects(t, flags);
1126 ret = unmap_table(t, flags);
1128 ERROR_LOG("unmap_table failed: %d\n", ret);
1135 * Find out whether the given row number corresponds to an invalid row.
1137 * \param t Pointer to the osl table.
1138 * \param row_num The number of the row in question.
1140 * By definition, a row is considered invalid if all its index entries
1143 * \return Positive if \a row_num corresponds to an invalid row,
1144 * zero if it corresponds to a valid row, negative on errors.
1146 int row_is_invalid(struct osl_table *t, uint32_t row_num)
1149 int i, ret = get_row_index(t, row_num, &row_index);
1153 for (i = 0; i < t->row_index_size; i++) {
1154 if ((unsigned char)row_index[i] != 0xff)
1157 INFO_LOG("row %d is invalid\n", row_num);
1162 * Invalidate a row of an osl table.
1164 * \param t Pointer to an open osl table.
1165 * \param row_num Number of the row to mark as invalid.
1167 * This function marks each mapped object in the index entry of \a row as
1170 * \return Positive on success, negative on errors.
1172 int mark_row_invalid(struct osl_table *t, uint32_t row_num)
1175 int ret = get_row_index(t, row_num, &row_index);
1179 INFO_LOG("marking row %d as invalid\n", row_num);
1180 memset(row_index, 0xff, t->row_index_size);
1185 * Initialize all rbtrees and compute number of invalid rows.
1187 * \param t The table containing the rbtrees to be initialized.
1189 * \return Positive on success, negative on errors.
1191 int init_rbtrees(struct osl_table *t)
1194 const struct osl_column_description *cd;
1196 /* create rbtrees */
1197 FOR_EACH_RBTREE_COLUMN(i, t, cd)
1198 t->columns[i].rbtree = RB_ROOT;
1199 /* add valid rows to rbtrees */
1200 t->num_invalid_rows = 0;
1201 for (i = 0; i < t->num_rows; i++) {
1202 ret = row_is_invalid(t, i);
1206 t->num_invalid_rows++;
1209 ret = add_row_to_rbtrees(t, i, NULL, NULL);
1217 * Open an osl table.
1219 * Each osl table must be opened before its data can be accessed.
1221 * \param table_desc Describes the table to be opened.
1222 * \param result Contains a pointer to the open table on success.
1224 * The table description given by \a desc should coincide with the
1225 * description used at creation time.
1229 int osl_open_table(const struct osl_table_description *table_desc,
1230 struct osl_table **result)
1233 struct osl_table *t;
1234 const struct osl_column_description *cd;
1236 INFO_LOG("opening table %s\n", table_desc->name);
1237 ret = init_table_structure(table_desc, &t);
1240 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
1241 struct stat statbuf;
1242 char *dirname = column_filename(t, i);
1244 ret = -ERRNO_TO_ERROR(ENOMEM);
1247 /* check if directory exists */
1248 ret = stat(dirname, &statbuf);
1251 ret = -ERRNO_TO_ERROR(errno);
1254 ret = -ERRNO_TO_ERROR(ENOTDIR);
1255 if (!S_ISDIR(statbuf.st_mode))
1258 ret = map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1262 DEBUG_LOG("num rows: %d\n", t->num_rows);
1263 ret = init_rbtrees(t);
1265 osl_close_table(t, OSL_MARK_CLEAN); /* ignore further errors */
1276 static int create_disk_storage_object_dir(const struct osl_table *t,
1277 unsigned col_num, const char *ds_name)
1282 if (!(t->desc->flags & OSL_LARGE_TABLE))
1284 dirname = disk_storage_dirname(t, col_num, ds_name);
1286 return -ERRNO_TO_ERROR(ENOMEM);
1287 ret = para_mkdir(dirname, 0777);
1289 if (ret < 0 && !is_errno(-ret, EEXIST))
1294 static int write_disk_storage_file(const struct osl_table *t, unsigned col_num,
1295 const struct osl_object *obj, const char *ds_name)
1300 ret = create_disk_storage_object_dir(t, col_num, ds_name);
1303 filename = disk_storage_path(t, col_num, ds_name);
1305 return -ERRNO_TO_ERROR(ENOMEM);
1306 ret = write_file(filename, obj->data, obj->size);
1311 static int append_map_file(const struct osl_table *t, unsigned col_num,
1312 const struct osl_object *obj, uint32_t *new_size)
1314 char *filename = column_filename(t, col_num);
1316 char header = 0; /* zero means valid object */
1319 return -ERRNO_TO_ERROR(ENOMEM);
1320 ret = append_file(filename, &header, 1, obj->data, obj->size,
1326 static int append_row_index(const struct osl_table *t, char *row_index)
1331 if (!t->num_mapped_columns)
1333 filename = index_filename(t->desc);
1335 return -ERRNO_TO_ERROR(ENOMEM);
1336 ret = append_file(filename, NULL, 0, row_index,
1337 t->row_index_size, NULL);
1343 * A wrapper for truncate(2)
1345 * \param path Name of the regular file to truncate
1346 * \param size Number of bytes to \b shave \b off
1348 * Truncate the regular file named by \a path by \a size bytes.
1350 * \return Positive on success, negative on errors. Possible errors include: \p
1351 * E_STAT, \p E_BAD_SIZE, \p E_TRUNC.
1355 int para_truncate(const char *path, off_t size)
1358 struct stat statbuf;
1361 if (stat(path, &statbuf) < 0)
1364 if (statbuf.st_size < size)
1367 if (truncate(path, statbuf.st_size - size) < 0)
1374 static int truncate_mapped_file(const struct osl_table *t, unsigned col_num,
1378 char *filename = column_filename(t, col_num);
1381 return -ERRNO_TO_ERROR(ENOMEM);
1382 ret = para_truncate(filename, size);
1387 static int delete_disk_storage_file(const struct osl_table *t, unsigned col_num,
1388 const char *ds_name)
1390 char *dirname, *filename = disk_storage_path(t, col_num, ds_name);
1394 return -ERRNO_TO_ERROR(ENOMEM);
1395 ret = unlink(filename);
1399 return -ERRNO_TO_ERROR(err);
1400 if (!(t->desc->flags & OSL_LARGE_TABLE))
1402 dirname = disk_storage_dirname(t, col_num, ds_name);
1404 return -ERRNO_TO_ERROR(ENOMEM);
1411 * Add a new row to an osl table and retrieve this row.
1413 * \param t Pointer to an open osl table.
1414 * \param objects Array of objects to be added.
1415 * \param row Result pointer.
1417 * The \a objects parameter must point to an array containing one object per
1418 * column. The order of the objects in the array is given by the table
1419 * description of \a table. Several sanity checks are performed during object
1420 * insertion and the function returns without modifying the table if any of
1421 * these tests fail. In fact, it is atomic in the sense that it either
1422 * succeeds or leaves the table unchanged (i.e. either all or none of the
1423 * objects are added to the table).
1425 * It is considered an error if an object is added to a column with associated
1426 * rbtree if this object is equal to an object already contained in that column
1427 * (i.e. the compare function for the column's rbtree returns zero).
1429 * Possible errors include: \p E_RB_KEY_EXISTS, \p E_BAD_DATA_SIZE.
1431 * \return Positive on success, negative on errors.
1433 * \sa struct osl_table_description, osl_compare_func, osl_add_row().
1435 int osl_add_and_get_row(struct osl_table *t, struct osl_object *objects,
1436 struct osl_row **row)
1439 char *ds_name = NULL;
1440 struct rb_node **rb_parents = NULL, ***rb_links = NULL;
1441 char *new_row_index = NULL;
1442 struct osl_object *volatile_objs = NULL;
1443 const struct osl_column_description *cd;
1446 return -E_BAD_TABLE;
1447 rb_parents = malloc(t->num_rbtrees * sizeof(struct rn_node*));
1449 return -ERRNO_TO_ERROR(ENOMEM);
1450 rb_links = malloc(t->num_rbtrees * sizeof(struct rn_node**));
1453 return -ERRNO_TO_ERROR(ENOMEM);
1455 if (t->num_mapped_columns) {
1456 new_row_index = malloc(t->row_index_size);
1457 if (!new_row_index) {
1460 return -ERRNO_TO_ERROR(ENOMEM);
1463 /* pass 1: sanity checks */
1464 // DEBUG_LOG("sanity tests: %p:%p\n", objects[0].data,
1465 // objects[1].data);
1466 FOR_EACH_COLUMN(i, t->desc, cd) {
1467 enum osl_storage_type st = cd->storage_type;
1468 enum osl_storage_flags sf = cd->storage_flags;
1470 // ret = -E_NULL_OBJECT;
1473 if (st == OSL_DISK_STORAGE)
1475 if (sf & OSL_RBTREE) {
1476 unsigned rbtree_num = t->columns[i].rbtree_num;
1477 ret = -E_RB_KEY_EXISTS;
1478 // DEBUG_LOG("checking whether %p exists\n",
1479 // objects[i].data);
1480 if (search_rbtree(objects + i, t, i,
1481 &rb_parents[rbtree_num],
1482 &rb_links[rbtree_num]) > 0)
1485 if (sf & OSL_FIXED_SIZE) {
1486 // DEBUG_LOG("fixed size. need: %zu, have: %d\n",
1487 // objects[i].size, cd->data_size);
1488 ret = -E_BAD_DATA_SIZE;
1489 if (objects[i].size != cd->data_size)
1493 if (t->num_disk_storage_columns) {
1494 ds_name = disk_storage_name_of_object(t,
1495 &objects[t->disk_storage_name_column]);
1496 ret = -ERRNO_TO_ERROR(ENOMEM);
1500 ret = unmap_table(t, OSL_MARK_CLEAN);
1503 // DEBUG_LOG("sanity tests passed%s\n", "");
1504 /* pass 2: create data files, append map data */
1505 FOR_EACH_COLUMN(i, t->desc, cd) {
1506 enum osl_storage_type st = cd->storage_type;
1507 if (st == OSL_NO_STORAGE)
1509 if (st == OSL_MAPPED_STORAGE) {
1511 struct osl_column *col = &t->columns[i];
1512 // DEBUG_LOG("appending object of size %zu\n",
1513 // objects[i].size);
1514 ret = append_map_file(t, i, objects + i, &new_size);
1517 update_cell_index(new_row_index, col, new_size,
1522 ret = write_disk_storage_file(t, i, objects + i, ds_name);
1526 ret = append_row_index(t, new_row_index);
1529 ret = map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1530 if (ret < 0) { /* truncate index and rollback changes */
1531 char *filename = index_filename(t->desc);
1533 para_truncate(filename, t->row_index_size);
1537 /* pass 3: add entry to rbtrees */
1538 if (t->num_volatile_columns) {
1539 ret = -ERRNO_TO_ERROR(ENOMEM);
1540 volatile_objs = calloc(t->num_volatile_columns,
1541 sizeof(struct osl_object));
1544 FOR_EACH_VOLATILE_COLUMN(i, t, cd)
1545 volatile_objs[t->columns[i].volatile_num] = objects[i];
1548 // DEBUG_LOG("adding new entry as row #%d\n", t->num_rows - 1);
1549 ret = add_row_to_rbtrees(t, t->num_rows - 1, volatile_objs, row);
1552 // DEBUG_LOG("added new entry as row #%d\n", t->num_rows - 1);
1555 rollback: /* rollback all changes made, ignore further errors */
1556 for (i--; i >= 0; i--) {
1557 cd = get_column_description(t->desc, i);
1558 enum osl_storage_type st = cd->storage_type;
1559 if (st == OSL_NO_STORAGE)
1562 if (st == OSL_MAPPED_STORAGE)
1563 truncate_mapped_file(t, i, objects[i].size);
1564 else /* disk storage */
1565 delete_disk_storage_file(t, i, ds_name);
1567 /* ignore error and return previous error value */
1568 map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1570 free(new_row_index);
1578 * Add a new row to an osl table.
1580 * \param t Same meaning as osl_add_and_get_row().
1581 * \param objects Same meaning as osl_add_and_get_row().
1583 * \return The return value of the underlying call to osl_add_and_get_row().
1585 * This is equivalent to osl_add_and_get_row(t, objects, NULL).
1587 int osl_add_row(struct osl_table *t, struct osl_object *objects)
1589 return osl_add_and_get_row(t, objects, NULL);
1593 * Retrieve an object identified by row and column
1595 * \param t Pointer to an open osl table.
1596 * \param r Pointer to the row.
1597 * \param col_num The column number.
1598 * \param object The result pointer.
1600 * The column determined by \a col_num must be of type \p OSL_MAPPED_STORAGE
1601 * or \p OSL_NO_STORAGE, i.e. no disk storage objects may be retrieved by this
1604 * \return Positive if object was found, negative on errors. Possible errors
1605 * include: \p E_BAD_TABLE, \p E_BAD_STORAGE_TYPE.
1607 * \sa osl_storage_type, osl_open_disk_object().
1609 int osl_get_object(const struct osl_table *t, const struct osl_row *r,
1610 unsigned col_num, struct osl_object *object)
1612 const struct osl_column_description *cd;
1615 return -E_BAD_TABLE;
1616 cd = get_column_description(t->desc, col_num);
1617 /* col must not be disk storage */
1618 if (cd->storage_type == OSL_DISK_STORAGE)
1619 return -E_BAD_STORAGE_TYPE;
1620 if (cd->storage_type == OSL_MAPPED_STORAGE)
1621 return get_mapped_object(t, col_num, r->num, object);
1623 *object = r->volatile_objects[t->columns[col_num].volatile_num];
1627 static int mark_mapped_object_invalid(const struct osl_table *t,
1628 uint32_t row_num, unsigned col_num)
1630 struct osl_object obj;
1632 int ret = get_mapped_object(t, col_num, row_num, &obj);
1643 * Delete a row from an osl table.
1645 * \param t Pointer to an open osl table.
1646 * \param row Pointer to the row to delete.
1648 * This removes all disk storage objects, removes all rbtree nodes, and frees
1649 * all volatile objects belonging to the given row. For mapped columns, the
1650 * data is merely marked invalid and may be pruned from time to time by
1653 * \return Positive on success, negative on errors. Possible errors include:
1654 * \p E_BAD_TABLE, errors returned by osl_get_object().
1656 int osl_del_row(struct osl_table *t, struct osl_row *row)
1658 struct osl_row *r = row;
1660 const struct osl_column_description *cd;
1663 return -E_BAD_TABLE;
1664 INFO_LOG("deleting row %p\n", row);
1666 if (t->num_disk_storage_columns) {
1668 ret = disk_storage_name_of_row(t, r, &ds_name);
1671 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd)
1672 delete_disk_storage_file(t, i, ds_name);
1675 FOR_EACH_COLUMN(i, t->desc, cd) {
1676 struct osl_column *col = t->columns + i;
1677 enum osl_storage_type st = cd->storage_type;
1678 remove_rb_node(t, i, r);
1679 if (st == OSL_MAPPED_STORAGE) {
1680 mark_mapped_object_invalid(t, r->num, i);
1683 if (st == OSL_NO_STORAGE && !(cd->storage_flags & OSL_DONT_FREE))
1684 free(r->volatile_objects[col->volatile_num].data);
1686 if (t->num_mapped_columns) {
1687 ret = mark_row_invalid(t, r->num);
1690 t->num_invalid_rows++;
1695 free(r->volatile_objects);
1700 /* test if column has an rbtree */
1701 static int check_rbtree_col(const struct osl_table *t, unsigned col_num,
1702 struct osl_column **col)
1705 return -E_BAD_TABLE;
1706 if (!(get_column_description(t->desc, col_num)->storage_flags & OSL_RBTREE))
1707 return -E_BAD_STORAGE_FLAGS;
1708 *col = t->columns + col_num;
1713 * Get the row that contains the given object.
1715 * \param t Pointer to an open osl table.
1716 * \param col_num The number of the column to be searched.
1717 * \param obj The object to be looked up.
1718 * \param result Points to the row containing \a obj.
1720 * Lookup \a obj in \a t and return the row containing \a obj. The column
1721 * specified by \a col_num must have an associated rbtree.
1723 * \return Positive on success, negative on errors. If an error occurred, \a
1724 * result is set to \p NULL. Possible errors include: \p E_BAD_TABLE, \p
1725 * E_BAD_STORAGE_FLAGS, errors returned by get_mapped_object(), \p
1726 * E_RB_KEY_NOT_FOUND.
1728 * \sa osl_storage_flags
1730 int osl_get_row(const struct osl_table *t, unsigned col_num,
1731 const struct osl_object *obj, struct osl_row **result)
1734 struct rb_node *node;
1735 struct osl_row *row;
1736 struct osl_column *col;
1739 ret = check_rbtree_col(t, col_num, &col);
1742 ret = search_rbtree(obj, t, col_num, &node, NULL);
1745 row = get_row_pointer(node, t->columns[col_num].rbtree_num);
1750 static int rbtree_loop(struct osl_column *col, void *private_data,
1751 osl_rbtree_loop_func *func)
1753 struct rb_node *n, *tmp;
1755 /* this for-loop is safe against removal of an entry */
1756 for (n = rb_first(&col->rbtree), tmp = n? rb_next(n) : NULL;
1758 n = tmp, tmp = tmp? rb_next(tmp) : NULL) {
1759 struct osl_row *r = get_row_pointer(n, col->rbtree_num);
1760 int ret = func(r, private_data);
1767 static int rbtree_loop_reverse(struct osl_column *col, void *private_data,
1768 osl_rbtree_loop_func *func)
1770 struct rb_node *n, *tmp;
1772 /* safe against removal of an entry */
1773 for (n = rb_last(&col->rbtree), tmp = n? rb_prev(n) : NULL;
1775 n = tmp, tmp = tmp? rb_prev(tmp) : NULL) {
1776 struct osl_row *r = get_row_pointer(n, col->rbtree_num);
1777 int ret = func(r, private_data);
1785 * Loop over all nodes in an rbtree.
1787 * \param t Pointer to an open osl table.
1788 * \param col_num The column to use for iterating over the elements.
1789 * \param private_data Pointer that gets passed to \a func.
1790 * \param func The function to be called for each node in the rbtree.
1792 * This function does an in-order walk of the rbtree associated with \a
1793 * col_num. It is an error if the \p OSL_RBTREE flag is not set for this
1794 * column. For each node in the rbtree, the given function \a func is called
1795 * with two pointers as arguments: The first osl_row* argument points to the
1796 * row that contains the object corresponding to the rbtree node currently
1797 * traversed, and the \a private_data pointer is passed verbatim to \a func as the
1798 * second argument. The loop terminates either if \a func returns a negative
1799 * value, or if all nodes of the tree have been visited.
1802 * \return Positive on success, negative on errors. If the termination of the
1803 * loop was caused by \a func returning a negative value, this value is
1806 * \sa osl_storage_flags, osl_rbtree_loop_reverse(), osl_compare_func.
1808 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 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 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_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_RB_KEY_EXISTS;
1923 if (cd->storage_flags & OSL_FIXED_SIZE) {
1924 if (obj->size != cd->data_size)
1925 return -E_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.
2003 * \return Positive on success, negative on errors. Possible errors include:
2004 * \p E_BAD_TABLE, \p E_BAD_STORAGE_TYPE, errors returned by osl_get_object().
2006 * \sa osl_get_object(), osl_storage_type, osl_close_disk_object().
2008 int osl_open_disk_object(const struct osl_table *t, const struct osl_row *r,
2009 unsigned col_num, struct osl_object *obj)
2011 const struct osl_column_description *cd;
2012 char *ds_name, *filename;
2016 return -E_BAD_TABLE;
2017 cd = get_column_description(t->desc, col_num);
2018 if (cd->storage_type != OSL_DISK_STORAGE)
2019 return -E_BAD_STORAGE_TYPE;
2021 ret = disk_storage_name_of_row(t, r, &ds_name);
2024 filename = disk_storage_path(t, col_num, ds_name);
2027 return -ERRNO_TO_ERROR(ENOMEM);
2028 DEBUG_LOG("filename: %s\n", filename);
2029 ret = mmap_full_file(filename, O_RDONLY, &obj->data, &obj->size, NULL);
2035 * Free resources that were allocated during osl_open_disk_object().
2037 * \param obj Pointer to the object previously returned by open_disk_object().
2039 * \return The return value of the underlying call to para_munmap().
2041 * \sa para_munmap().
2043 int osl_close_disk_object(struct osl_object *obj)
2045 return para_munmap(obj->data, obj->size);
2049 * Get the number of rows of the given table.
2051 * \param t Pointer to an open osl table.
2052 * \param num_rows Result is returned here.
2054 * The number of rows returned via \a num_rows excluding any invalid rows.
2056 * \return Positive on success, \p -E_BAD_TABLE if \a t is \p NULL.
2058 int osl_get_num_rows(const struct osl_table *t, unsigned *num_rows)
2061 return -E_BAD_TABLE;
2062 assert(t->num_rows >= t->num_invalid_rows);
2063 *num_rows = t->num_rows - t->num_invalid_rows;
2068 * Get the rank of a row.
2070 * \param t An open osl table.
2071 * \param r The row to get the rank of.
2072 * \param col_num The number of an rbtree column.
2073 * \param rank Result pointer.
2075 * The rank is, by definition, the position of the row in the linear order
2076 * determined by an in-order tree walk of the rbtree associated with column
2077 * number \a col_num of \a table.
2079 * \return Positive on success, negative on errors.
2081 * \sa osl_get_nth_row().
2083 int osl_get_rank(const struct osl_table *t, struct osl_row *r,
2084 unsigned col_num, unsigned *rank)
2086 struct osl_object obj;
2087 struct osl_column *col;
2088 struct rb_node *node;
2089 int ret = check_rbtree_col(t, col_num, &col);
2093 ret = osl_get_object(t, r, col_num, &obj);
2096 ret = search_rbtree(&obj, t, col_num, &node, NULL);
2099 ret = rb_rank(node, rank);
2106 * Get the row with n-th greatest value.
2108 * \param t Pointer to an open osl table.
2109 * \param col_num The column number.
2110 * \param n The rank of the desired row.
2111 * \param result Row is returned here.
2113 * Retrieve the n-th order statistic with respect to the compare function
2114 * of the rbtree column \a col_num. In other words, get that row with
2115 * \a n th greatest value in column \a col_num. It's an error if
2116 * \a col_num is not a rbtree column, or if \a n is larger than the
2117 * number of rows in the table.
2119 * \return Positive on success, negative on errors. Possible errors:
2120 * \p E_BAD_TABLE, \p E_BAD_STORAGE_FLAGS, \p E_RB_KEY_NOT_FOUND.
2122 * \sa osl_storage_flags, osl_compare_func, osl_get_row(),
2123 * osl_rbtree_last_row(), osl_rbtree_first_row(), osl_get_rank().
2125 int osl_get_nth_row(const struct osl_table *t, unsigned col_num,
2126 unsigned n, struct osl_row **result)
2128 struct osl_column *col;
2129 struct rb_node *node;
2134 return -E_RB_KEY_NOT_FOUND;
2135 ret = osl_get_num_rows(t, &num_rows);
2139 return -E_RB_KEY_NOT_FOUND;
2140 ret = check_rbtree_col(t, col_num, &col);
2143 node = rb_nth(col->rbtree.rb_node, n);
2145 return -E_RB_KEY_NOT_FOUND;
2146 *result = get_row_pointer(node, col->rbtree_num);
2151 * Get the row corresponding to the smallest rbtree node of a column.
2153 * \param t An open rbtree table.
2154 * \param col_num The number of the rbtree column.
2155 * \param result A pointer to the first row is returned here.
2157 * The rbtree node of the smallest object (with respect to the corresponding
2158 * compare function) is selected and the row containing this object is
2159 * returned. It is an error if \a col_num refers to a column without an
2160 * associated rbtree.
2162 * \return Positive on success, negative on errors.
2164 * \sa osl_get_nth_row(), osl_rbtree_last_row().
2166 int osl_rbtree_first_row(const struct osl_table *t, unsigned col_num,
2167 struct osl_row **result)
2169 return osl_get_nth_row(t, col_num, 1, result);
2173 * Get the row corresponding to the greatest rbtree node of a column.
2175 * \param t The same meaning as in \p osl_rbtree_first_row().
2176 * \param col_num The same meaning as in \p osl_rbtree_first_row().
2177 * \param result The same meaning as in \p osl_rbtree_first_row().
2179 * This function works just like osl_rbtree_first_row(), the only difference
2180 * is that the row containing the greatest rather than the smallest object is
2183 * \return Positive on success, negative on errors.
2185 * \sa osl_get_nth_row(), osl_rbtree_first_row().
2187 int osl_rbtree_last_row(const struct osl_table *t, unsigned col_num,
2188 struct osl_row **result)
2191 int ret = osl_get_num_rows(t, &num_rows);
2195 return osl_get_nth_row(t, col_num, num_rows, result);