2 * Copyright (C) 2007-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file osl_core.h Object storage layer details, not visible to users. */
13 static __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...);
15 /** Internal representation of a column of an osl table. */
17 /** The memory mapping of this comumn (only used for mapped columns). */
18 struct osl_object data_map;
19 /** The root of the rbtree (only used for columns with rbtrees). */
20 struct rb_root rbtree;
21 /** The index in the array of rb nodes (only used for columns with rbtrees). */
23 /** Index for volatile_objects of struct osl_row. */
24 unsigned volatile_num;
26 * Starting point of the data for this column within the index
27 * (only used for mapped columns).
29 uint16_t index_offset;
31 * The hash value of the name of this column.
33 * This is only used for mapped and disk storage columns).
35 HASH_TYPE name_hash[HASH_SIZE];
38 /** Internal representation of an osl table */
40 /** Pointer to the table description */
41 const struct osl_table_description *desc;
42 /** The size of the index header of this table. */
43 uint16_t index_header_size;
44 /** Contains the mapping of the table's index file */
45 struct osl_object index_map;
46 /** Total number of rows, including invalid rows. */
48 /** Keeps track of the number of invalid rows in the table. */
49 uint32_t num_invalid_rows;
50 /** Number of columns of type \p OSL_MAPPED_STORAGE. */
51 unsigned num_mapped_columns;
52 /** Number of columns of type \p OSL_NO_STORAGE. */
53 unsigned num_volatile_columns;
54 /** Number of columns of type \p OSL_DISK_STORAGE. */
55 unsigned num_disk_storage_columns;
56 /** Number of columns with associated rbtree. */
59 * The number of the column that determines the name of the disk
62 unsigned disk_storage_name_column;
63 /** The number of bytes of an index entry of a row. */
64 unsigned row_index_size;
65 /** Pointer to the internal representation of the columns. */
66 struct osl_column *columns;
69 /** Internal representation of a row of an osl table */
74 * It is only used if there is at least one mapped column.
77 /** Array of size \a num_volatile_columns. */
78 struct osl_object *volatile_objects;
81 int read_table_desc(struct osl_object *map, struct osl_table_description *desc);
82 int init_table_structure(const struct osl_table_description *desc,
83 struct osl_table **table_ptr);
84 int row_is_invalid(struct osl_table *t, uint32_t row_num);
85 int get_mapped_object(const struct osl_table *t, unsigned col_num,
86 uint32_t row_num, struct osl_object *obj);
87 int para_truncate(const char *filename, off_t size);
88 int unmap_table(struct osl_table *t, enum osl_close_flags flags);
89 int init_rbtrees(struct osl_table *t);
92 * Flags to be specified for map_table().
96 enum map_table_flags {
98 * Check whether the entries in the table index match the entries in
99 * the table description.
101 MAP_TBL_FL_VERIFY_INDEX = 1,
102 /** Do not complain even if the dirty flag is set. */
103 MAP_TBL_FL_IGNORE_DIRTY = 2,
104 /** Use read-only mappings. */
105 MAP_TBL_FL_MAP_RDONLY = 4
108 int map_table(struct osl_table *t, enum map_table_flags flags);
109 void clear_rbtrees(struct osl_table *t);
110 int mark_row_invalid(struct osl_table *t, uint32_t row_num);
114 * Get the description of a column by column number
116 * \param d Pointer to the table description.
117 * \param col_num The number of the column to get the description for.
119 * \return The table description.
121 * \sa struct osl_column_description.
123 _static_inline_ struct osl_column_description *get_column_description(
124 const struct osl_table_description *d, unsigned col_num)
126 return &d->column_descriptions[col_num];
130 * Format of the header of the index file of an osl table.
132 * Bytes 16-31 are currently unused.
134 * \sa enum index_column_desc_offsets, HASH_SIZE, osl_table_flags.
136 enum index_header_offsets {
137 /** Bytes 0-8: PARASLASH. */
139 /** Byte 9: Dirty flag (nonzero if table is mapped). */
141 /** Byte 10: osl table version number. */
143 /** Byte 11: Table flags.*/
144 IDX_TABLE_FLAGS = 11,
145 /** Byte 12,13: Number of columns. */
147 /** Byte 14,15 Size of the index header. */
148 IDX_HEADER_SIZE = 14,
149 /** Column descriptions start here. */
150 IDX_COLUMN_DESCRIPTIONS = 32
154 * Format of the column description in the index header.
156 * \sa index_header_offsets.
158 enum index_column_desc_offsets {
159 /** Bytes 0,1: Storage_type. */
160 IDX_CD_STORAGE_TYPE = 0,
161 /** Bytes 2,3: Storage_flags. */
162 IDX_CD_STORAGE_FLAGS = 2,
163 /** Bytes 4 - 7: Data_size (only used for fixed size columns). */
164 IDX_CD_DATA_SIZE = 4,
165 /** Bytes 8 - ... Name of the column. */
169 /** Magic string contained in the header of the index file of each osl table. */
170 #define PARA_MAGIC "PARASLASH"
173 * The minimal number of bytes for a column in the index header.
175 * The column name starts at byte IDX_CD_NAME and must at least contain one
176 * character, plus the terminating NULL byte.
178 #define MIN_IDX_COLUMN_DESCRIPTION_SIZE (IDX_CD_NAME + 2)
181 * Get the number of bytes used for a column in the index header.
183 * \param name The name of the column.
185 * The amount of space used for a column in the index header of a table depends
186 * on the (length of the) name of the column.
188 * \return The total number of bytes needed to store one column of this name.
190 _static_inline_ size_t index_column_description_size(const char *name)
192 return MIN_IDX_COLUMN_DESCRIPTION_SIZE + strlen(name) - 1;
195 #define CURRENT_TABLE_VERSION 1
196 #define MIN_TABLE_VERSION 1
197 #define MAX_TABLE_VERSION 1
198 /** An index header must be at least that many bytes long. */
199 #define MIN_INDEX_HEADER_SIZE(num_cols) (MIN_IDX_COLUMN_DESCRIPTION_SIZE \
200 * num_cols + IDX_COLUMN_DESCRIPTIONS)
203 * Get the number of rows from the size of the memory mapping.
205 * \param t Pointer to an open table.
207 * \return The number of rows, including invalid rows.
209 _static_inline_ unsigned table_num_rows(const struct osl_table *t)
211 return (t->index_map.size - t->index_header_size)
216 * Get the path of the index file from a table description.
218 * \param d The table description.
220 * \return The full path of the index file. Result must be freed by
223 _static_inline_ char *index_filename(const struct osl_table_description *d)
225 return make_message("%s/%s/index", d->dir, d->name);
229 * Get the path of storage of a column.
231 * \param t Pointer to an initialized table.
232 * \param col_num The number of the column to get the path for.
234 * \return The full path of the mapped data file (mapped storage) or the
235 * data directory (disk storage). Result must be freed by the caller.
237 * \sa osl_storage_type.
239 _static_inline_ char *column_filename(const struct osl_table *t, unsigned col_num)
241 char asc[2 * HASH_SIZE + 1];
242 hash_to_asc(t->columns[col_num].name_hash, asc);
243 return make_message("%s/%s/%s", t->desc->dir, t->desc->name, asc);
247 * Get the start of an index entry
249 * \param t Pointer to a table which has been mapped.
250 * \param row_num The number of the row whose index entry should be retrieved.
251 * \param row_index Result pointer.
253 * \return Positive on success, \p -E_INDEX_CORRUPTION otherwise.
255 * \sa get_cell_index().
257 _static_inline_ int get_row_index(const struct osl_table *t, uint32_t row_num,
260 uint32_t index_offset;
261 index_offset = t->index_header_size + t->row_index_size * row_num;
262 if (index_offset + 8 > t->index_map.size) {
264 return -E_INDEX_CORRUPTION;
266 *row_index = (char *)(t->index_map.data) + index_offset;
271 * Get the index entry of a row/column pair.
273 * \param t Pointer to a table which has been mapped.
274 * \param row_num The number of the row whose index entry should be retrieved.
275 * \param col_num The number of the column whose index entry should be retrieved.
276 * \param cell_index Result pointer.
278 * \return Positive on success, \p -E_INDEX_CORRUPTION otherwise.
280 * \sa get_row_index().
282 _static_inline_ int get_cell_index(const struct osl_table *t, uint32_t row_num,
283 uint32_t col_num, char **cell_index)
285 int ret = get_row_index(t, row_num, cell_index);
288 *cell_index += t->columns[col_num].index_offset;
293 * Change an index entry of a column after object was added.
295 * \param row_index Pointer to the index of the row to update.
296 * \param col Pointer to the column.
297 * \param map_size The new size of the data file.
298 * \param object_size The size of the object just appended to the data file.
300 * This is called right after an object was appended to the data file for a
303 * \sa get_row_index().
305 _static_inline_ void update_cell_index(char *row_index, struct osl_column *col,
306 uint32_t map_size, uint32_t object_size)
308 write_u32(row_index + col->index_offset, map_size - object_size - 1);
309 write_u32(row_index + col->index_offset + 4, object_size + 1);
313 * Get the full path of a disk storage object
315 * \param t Pointer to an initialized table.
316 * \param col_num The number of the column the disk storage object belongs to.
317 * \param ds_name The disk storage name of the object.
319 * \return Pointer to the full path which must be freed by the caller.
321 * \sa column_filename(), disk_storage_name_of_row().
323 _static_inline_ char *disk_storage_path(const struct osl_table *t,
324 unsigned col_num, const char *ds_name)
326 char *filename, *dirname = column_filename(t, col_num);
330 filename = make_message("%s/%s", dirname, ds_name);
336 * Get the column description of the next column of a given type.
338 * \param type the desired storage type.
339 * \param col_num the column to start the search.
340 * \param t the osl table.
341 * \param cd result is returned here.
343 * \return On success, \a cd contains the column description of the next column
344 * of type \a type, and the number of that column is returned. Otherwise, \a
345 * cd is set to \p NULL and the function returns t->num_columns + 1.
347 * \sa FOR_EACH_COLUMN_OF_TYPE, FOR_EACH_MAPPED_COLUMN, FOR_EACH_RBTREE_COLUMN,
348 * FOR_EACH_DISK_STORAGE_COLUMN, FOR_EACH_VOLATILE_COLUMN, osl_storage_type.
350 _static_inline_ int next_column_of_type(enum osl_storage_type type, int col_num,
351 const struct osl_table *t,
352 const struct osl_column_description **cd)
355 while (col_num < t->desc->num_columns) {
356 *cd = get_column_description(t->desc, col_num);
357 if ((*cd)->storage_type == type)
365 * Find the next column with an associated rbtree.
367 * \param col_num The column to start the search.
368 * \param t The osl table.
369 * \param cd Result is returned here.
371 * \return On success, \a cd contains the column description of the next column
372 * with associated rbtree, and the number of that column is returned.
373 * Otherwise, \a cd is set to \p NULL and the function returns t->num_columns +
376 * \sa FOR_EACH_COLUMN_OF_TYPE, FOR_EACH_MAPPED_COLUMN, FOR_EACH_RBTREE_COLUMN,
377 * FOR_EACH_DISK_STORAGE_COLUMN, FOR_EACH_VOLATILE_COLUMN, osl_storage_flags.
379 _static_inline_ int next_rbtree_column(int col_num, const struct osl_table *t,
380 const struct osl_column_description **cd)
383 while (col_num < t->desc->num_columns) {
384 *cd = get_column_description(t->desc, col_num);
385 if ((*cd)->storage_flags & OSL_RBTREE)
393 /* quite some dirty hacks */
395 /** Round up size of struct osl_row to multiple of 8 */
396 #define RB_NODES_OFFSET ((sizeof(struct osl_row) + 7) / 8 * 8)
399 * Allocate a new osl row.
401 * \param num_rbtrees The number of rbtrees for this row.
403 * \return A pointer to a zeroed-out area suitable for holding an osl row with
404 * \a num_rbtrees rbtree columns or \p NULL if no memory could be allocated.
406 _static_inline_ struct osl_row *allocate_row(unsigned num_rbtrees)
408 size_t s = RB_NODES_OFFSET + num_rbtrees * sizeof(struct rb_node);
413 * Compute the pointer to a rbtree node embedded in a osl row.
415 * \param row The row to extract the rbtree pointer from.
416 * \param rbtree_num The number of the rbtree node to extract.
418 * \return A pointer to the \a rbtree_num th node contained in \a row.
420 _static_inline_ struct rb_node *get_rb_node_pointer(const struct osl_row *row, uint32_t rbtree_num)
422 return ((struct rb_node *)(((char *)row) + RB_NODES_OFFSET)) + rbtree_num;
426 * Get a pointer to the struct containing the given rbtree node.
428 * \param node Pointer to the rbtree node.
429 * \param rbtree_num Number of \a node in the array of rbtree nodes.
431 * \return A pointer to the row containing \a node.
433 _static_inline_ struct osl_row *get_row_pointer(const struct rb_node *node,
437 return (struct osl_row *)(((char *)node) - RB_NODES_OFFSET);
441 * Compute a cryptographic hash of an osl object.
443 * \param obj the Object to compute the hash value from.
444 * \param hash Result is returned here.
446 _static_inline_ void hash_object(const struct osl_object *obj, HASH_TYPE *hash)
448 hash_function(obj->data, obj->size, hash);
452 * Get the relative path of an object, given the hash value.
454 * \param t Pointer to an initialized osl table.
455 * \param hash An arbitrary hash value.
457 * This function is typically called with \a hash being the hash of the object
458 * stored in the disk storage name column of a row. If the OSL_LARGE_TABLE
459 * flag is set, the first two hex digits get separated with a slash from the
462 * \return The relative path for all disk storage objects corresponding to \a
465 * \sa struct osl_table:disk_storage_name_column.
467 _static_inline_ char *disk_storage_name_of_hash(const struct osl_table *t, HASH_TYPE *hash)
469 char asc[2 * HASH_SIZE + 2];
471 hash_to_asc(hash, asc);
472 if (t->desc->flags & OSL_LARGE_TABLE)
473 return make_message("%.2s/%s", asc, asc + 2);
478 * A wrapper for rename(2).
480 * \param old_path The source path.
481 * \param new_path The destination path.
487 _static_inline_ int para_rename(const char *old_path, const char *new_path)
489 if (rename(old_path, new_path) < 0)
490 return -ERRNO_TO_ERROR(errno);
495 * Iterate over each column of an initialized table.
497 * \param col A pointer to a struct osl_column.
498 * \param desc Pointer to the table description.
499 * \param cd Pointer to struct osl_column_description.
501 * On each iteration, \a col will point to the next column of the table and \a
502 * cd will point to the column description of this column.
504 * \sa struct osl_column FOR_EACH_RBTREE_COLUMN, FOR_EACH_COLUMN_OF_TYPE,
505 * FOR_EACH_MAPPED_COLUMN, FOR_EACH_DISK_STORAGE_COLUMN,
506 * FOR_EACH_VOLATILE_COLUMN.
508 #define FOR_EACH_COLUMN(col, desc, cd) \
509 for (col = 0; col < (desc)->num_columns && \
510 (cd = get_column_description(desc, col)); col++)
513 * Iterate over each column with associated rbtree.
515 * \param col Same meaning as in FOR_EACH_COLUMN().
516 * \param table Same meaning as in FOR_EACH_COLUMN().
517 * \param cd Same meaning as in FOR_EACH_COLUMN().
519 * \sa osl_storage_flags::OSL_RBTREE, FOR_EACH_COLUMN, FOR_EACH_COLUMN_OF_TYPE,
520 * FOR_EACH_MAPPED_COLUMN, FOR_EACH_DISK_STORAGE_COLUMN,
521 * FOR_EACH_VOLATILE_COLUMN.
523 #define FOR_EACH_RBTREE_COLUMN(col, table, cd) \
524 for (col = next_rbtree_column(0, table, &cd); \
525 col < table->desc->num_columns; \
526 col = next_rbtree_column(++col, table, &cd))
529 * Iterate over each column of given storage type.
531 * \param type The osl_storage_type to iterate over.
532 * \param col Same meaning as in FOR_EACH_COLUMN().
533 * \param table Same meaning as in FOR_EACH_COLUMN().
534 * \param cd Same meaning as in FOR_EACH_COLUMN().
536 * \sa osl_storage_type, FOR_EACH_COLUMN, FOR_EACH_RBTREE_COLUMN,
537 * FOR_EACH_MAPPED_COLUMN, FOR_EACH_DISK_STORAGE_COLUMN,
538 * FOR_EACH_VOLATILE_COLUMN.
540 #define FOR_EACH_COLUMN_OF_TYPE(type, col, table, cd) \
541 for (col = next_column_of_type(type, 0, table, &cd); \
542 col < table->desc->num_columns; \
543 col = next_column_of_type(type, ++col, table, &cd))
546 * Iterate over each mapped column.
548 * \param col Same meaning as in FOR_EACH_COLUMN().
549 * \param table Same meaning as in FOR_EACH_COLUMN().
550 * \param cd Same meaning as in FOR_EACH_COLUMN().
552 * Just like FOR_EACH_COLUMN(), but skip columns which are
553 * not of type \p OSL_MAPPED_STORAGE.
555 * \sa osl_storage_flags::OSL_MAPPED_STORAGE, FOR_EACH_COLUMN,
556 * FOR_EACH_RBTREE_COLUMN, FOR_EACH_COLUMN_OF_TYPE,
557 * FOR_EACH_DISK_STORAGE_COLUMN, FOR_EACH_VOLATILE_COLUMN.
559 #define FOR_EACH_MAPPED_COLUMN(col, table, cd) \
560 FOR_EACH_COLUMN_OF_TYPE(OSL_MAPPED_STORAGE, col, table, cd)
563 * Iterate over each disk storage column.
565 * \param col Same meaning as in FOR_EACH_COLUMN().
566 * \param table Same meaning as in FOR_EACH_COLUMN().
567 * \param cd Same meaning as in FOR_EACH_COLUMN().
569 * Just like FOR_EACH_COLUMN(), but skip columns which are
570 * not of type \p OSL_DISK_STORAGE.
572 * \sa osl_storage_flags::OSL_DISK_STORAGE, FOR_EACH_COLUMN,
573 * FOR_EACH_RBTREE_COLUMN, FOR_EACH_COLUMN_OF_TYPE, FOR_EACH_MAPPED_COLUMN,
574 * FOR_EACH_VOLATILE_COLUMN.
576 #define FOR_EACH_DISK_STORAGE_COLUMN(col, table, cd) \
577 FOR_EACH_COLUMN_OF_TYPE(OSL_DISK_STORAGE, col, table, cd)
580 * Iterate over each volatile column.
582 * \param col Same meaning as in FOR_EACH_COLUMN().
583 * \param table Same meaning as in FOR_EACH_COLUMN().
584 * \param cd Same meaning as in FOR_EACH_COLUMN().
586 * Just like FOR_EACH_COLUMN(), but skip columns which are
587 * not of type \p OSL_NO_STORAGE.
589 * \sa osl_storage_flags::OSL_NO_STORAGE, FOR_EACH_COLUMN,
590 * FOR_EACH_RBTREE_COLUMN, FOR_EACH_COLUMN_OF_TYPE, FOR_EACH_MAPPED_COLUMN,
591 * FOR_EACH_DISK_STORAGE_COLUMN.
593 #define FOR_EACH_VOLATILE_COLUMN(col, table, cd) \
594 FOR_EACH_COLUMN_OF_TYPE(OSL_NO_STORAGE, col, table, cd)