]> git.tuebingen.mpg.de Git - osl.git/blob - osl.c
e1b535c563b7ae99570950f7c0152be2f3412883
[osl.git] / osl.c
1 /*
2  * Copyright (C) 2007-2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file osl.c Object storage layer functions. */
8 #include <dirent.h> /* readdir() */
9 #include <assert.h>
10
11
12 #include "log.h"
13 #include "osl.h"
14 #include "error.h"
15 #include "fd.h"
16 #include "list.h"
17 #include "osl_core.h"
18
19 /**
20  * Allocate a sufficiently large string and print into it.
21  *
22  * \param fmt A usual format string.
23  *
24  * Produce output according to \p fmt. No artificial bound on the length of the
25  * resulting string is imposed.
26  *
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.
29  *
30  * \sa printf(3).
31  */
32 static __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
33 {
34         int n;
35         size_t size = 100;
36         char *p = malloc(size);
37
38         if (!p)
39                 return NULL;
40         while (1) {
41                 char *q;
42                 va_list ap;
43                 /* Try to print in the allocated space. */
44                 va_start(ap, fmt);
45                 n = vsnprintf(p, size, fmt, ap);
46                 va_end(ap);
47                 /* If that worked, return the string. */
48                 if (n > -1 && n < size)
49                         break;
50                 /* Else try again with more space. */
51                 if (n > -1) /* glibc 2.1 */
52                         size = n + 1; /* precisely what is needed */
53                 else /* glibc 2.0 */
54                         size *= 2; /* twice the old size */
55                 q = realloc(p, size);
56                 if (!q) {
57                         free(p);
58                         return NULL;
59                 }
60         }
61         return p;
62 }
63
64 /* Taken from Drepper: How to write shared libraries, Appendix B. */
65 #include <stddef.h>
66 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
67 #define MSGSTRFIELD1(line) str##line
68 static const union msgstr_t {
69         struct {
70 #define _S(n, s) char MSGSTRFIELD(__LINE__)[sizeof(s)];
71 #include "errtab.h"
72 #undef _S
73         };
74         char str[0];
75 } msgstr = { {
76 #define _S(n, s) s,
77 #include "errtab.h"
78 #undef _S
79 } };
80 static const unsigned int errmsgidx[] = {
81 #define _S(n, s) [n] = offsetof(union msgstr_t, MSGSTRFIELD(__LINE__)),
82 #include "errtab.h"
83 #undef _S
84 };
85
86 __export const char *osl_strerror(int num)
87 {
88         if (IS_SYSTEM_ERROR(num))
89                 return strerror((num) & ((1 << SYSTEM_ERROR_BIT) - 1));
90         return msgstr.str + errmsgidx[num];
91 }
92
93 /**
94  * The log function.
95  *
96  * \param ll Loglevel.
97  * \param fmt Usual format string.
98  *
99  * All XXX_LOG() macros use this function.
100  */
101 __printf_2_3 void __log(int ll, const char* fmt,...)
102 {
103         va_list argp;
104         FILE *outfd;
105         struct tm *tm;
106         time_t t1;
107         char str[255] = "";
108
109         if (ll < 2)
110                 return;
111         outfd = stderr;
112         time(&t1);
113         tm = localtime(&t1);
114         strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
115         fprintf(outfd, "%s ", str);
116         va_start(argp, fmt);
117         vfprintf(outfd, fmt, argp);
118         va_end(argp);
119 }
120
121 /**
122  * A wrapper for lseek(2).
123  *
124  * \param fd The file descriptor whose offset is to be to repositioned.
125  * \param offset A value-result parameter.
126  * \param whence Usual repositioning directive.
127  *
128  * Reposition the offset of the file descriptor \a fd to the argument \a offset
129  * according to the directive \a whence. Upon successful return, \a offset
130  * contains the resulting offset location as measured in bytes from the
131  * beginning of the file.
132  *
133  * \return Positive on success. Otherwise, the function returns \p -E_OSL_LSEEK.
134  *
135  * \sa lseek(2).
136  */
137 static int __lseek(int fd, off_t *offset, int whence)
138 {
139         *offset = lseek(fd, *offset, whence);
140         int ret = -E_OSL_LSEEK;
141         if (*offset == -1)
142                 return ret;
143         return 1;
144 }
145
146 /**
147  * Wrapper for the write system call.
148  *
149  * \param fd The file descriptor to write to.
150  * \param buf The buffer to write.
151  * \param size The length of \a buf in bytes.
152  *
153  * This function writes out the given buffer and retries if an interrupt
154  * occurred during the write.
155  *
156  * \return On success, the number of bytes written is returned, otherwise, the
157  * function returns \p -E_OSL_WRITE.
158  *
159  * \sa write(2).
160  */
161 static ssize_t __write(int fd, const void *buf, size_t size)
162 {
163         ssize_t ret;
164
165         for (;;) {
166                 ret = write(fd, buf, size);
167                 if ((ret < 0) && (errno == EAGAIN || errno == EINTR))
168                         continue;
169                 return ret >= 0? ret : -E_OSL_WRITE;
170         }
171 }
172
173 /**
174  * Write the whole buffer to a file descriptor.
175  *
176  * \param fd The file descriptor to write to.
177  * \param buf The buffer to write.
178  * \param size The length of \a buf in bytes.
179  *
180  * This function writes the given buffer and continues on short writes and
181  * when interrupted by a signal.
182  *
183  * \return Positive on success, negative on errors. Possible errors: any
184  * errors returned by para_write().
185  *
186  * \sa para_write().
187  */
188 static ssize_t write_all(int fd, const void *buf, size_t size)
189 {
190 //      DEBUG_LOG("writing %zu bytes\n", size);
191         const char *b = buf;
192         while (size) {
193                 ssize_t ret = __write(fd, b, size);
194 //              DEBUG_LOG("ret: %zd\n", ret);
195                 if (ret < 0)
196                         return ret;
197                 b += ret;
198                 size -= ret;
199         }
200         return 1;
201 }
202 /**
203  * Open a file, write the given buffer and close the file.
204  *
205  * \param filename Full path to the file to open.
206  * \param buf The buffer to write to the file.
207  * \param size The size of \a buf.
208  *
209  * \return Standard.
210  */
211 static int write_file(const char *filename, const void *buf, size_t size)
212 {
213         int ret, fd;
214
215         ret = osl_open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
216         if (ret < 0)
217                 return ret;
218         fd = ret;
219         ret = write_all(fd, buf, size);
220         if (ret < 0)
221                 goto out;
222         ret = 1;
223 out:
224         close(fd);
225         return ret;
226 }
227
228 static int append_file(const char *filename, char *header, size_t header_size,
229         char *data, size_t data_size, uint32_t *new_pos)
230 {
231         int ret, fd;
232
233 //      DEBUG_LOG("appending %zu  + %zu bytes\n", header_size, data_size);
234         ret = osl_open(filename, O_WRONLY | O_CREAT | O_APPEND, 0644);
235         if (ret < 0)
236                 return ret;
237         fd = ret;
238         if (header && header_size) {
239                 ret = write_all(fd, header, header_size);
240                 if (ret < 0)
241                         goto out;
242         }
243         ret = write_all(fd, data, data_size);
244         if (ret < 0)
245                 goto out;
246         if (new_pos) {
247                 off_t offset = 0;
248                 ret = __lseek(fd, &offset, SEEK_END);
249                 if (ret < 0)
250                         goto out;
251 //              DEBUG_LOG("new file size: " FMT_OFF_T "\n", offset);
252                 *new_pos = offset;
253         }
254         ret = 1;
255 out:
256         close(fd);
257         return ret;
258 }
259
260 static int verify_name(const char *name)
261 {
262         if (!name)
263                 return -E_OSL_BAD_NAME;
264         if (!*name)
265                 return -E_OSL_BAD_NAME;
266         if (strchr(name, '/'))
267                 return -E_OSL_BAD_NAME;
268         if (!strcmp(name, ".."))
269                 return -E_OSL_BAD_NAME;
270         if (!strcmp(name, "."))
271                 return -E_OSL_BAD_NAME;
272         return 1;
273 }
274
275 /**
276  * Compare two osl objects pointing to hash values.
277  *
278  * \param obj1 Pointer to the first hash object.
279  * \param obj2 Pointer to the second hash object.
280  *
281  * \return The values required for an osl compare function.
282  *
283  * \sa osl_compare_func, uint32_compare().
284  */
285 int osl_hash_compare(const struct osl_object *obj1, const struct osl_object *obj2)
286 {
287         return hash_compare((HASH_TYPE *)obj1->data, (HASH_TYPE *)obj2->data);
288 }
289
290 static char *disk_storage_dirname(const struct osl_table *t, unsigned col_num,
291                 const char *ds_name)
292 {
293         char *dirname, *column_name = column_filename(t, col_num);
294
295         if (!column_name)
296                 return NULL;
297         if (!(t->desc->flags & OSL_LARGE_TABLE))
298                 return column_name;
299         dirname = make_message("%s/%.2s", column_name, ds_name);
300         free(column_name);
301         return dirname;
302 }
303
304 static char *disk_storage_name_of_object(const struct osl_table *t,
305         const struct osl_object *obj)
306 {
307         HASH_TYPE hash[HASH_SIZE];
308         hash_object(obj, hash);
309         return disk_storage_name_of_hash(t, hash);
310 }
311
312 static int disk_storage_name_of_row(const struct osl_table *t,
313                 const struct osl_row *row, char **name)
314 {
315         struct osl_object obj;
316         int ret = osl_get_object(t, row, t->disk_storage_name_column, &obj);
317
318         if (ret < 0)
319                 return ret;
320         *name = disk_storage_name_of_object(t, &obj);
321         if (*name)
322                 return 1;
323         return -ERRNO_TO_ERROR(ENOMEM);
324 }
325
326 static void column_name_hash(const char *col_name, HASH_TYPE *hash)
327 {
328         hash_function(col_name, strlen(col_name), hash);
329 }
330
331 static int init_column_descriptions(struct osl_table *t)
332 {
333         int i, j, ret;
334         const struct osl_column_description *cd;
335
336         ret = -E_OSL_BAD_TABLE_DESC;
337         ret = verify_name(t->desc->name);
338         if (ret < 0)
339                 goto err;
340         ret = -E_OSL_BAD_DB_DIR;
341         if (!t->desc->dir && (t->num_disk_storage_columns || t->num_mapped_columns))
342                 goto err;
343         /* the size of the index header without column descriptions */
344         t->index_header_size = IDX_COLUMN_DESCRIPTIONS;
345         FOR_EACH_COLUMN(i, t->desc, cd) {
346                 struct osl_column *col = t->columns + i;
347                 if (cd->storage_flags & OSL_RBTREE) {
348                         if (!cd->compare_function)
349                                 return -E_OSL_NO_COMPARE_FUNC;
350                 }
351                 if (cd->storage_type == OSL_NO_STORAGE)
352                         continue;
353                 ret = -E_OSL_NO_COLUMN_NAME;
354                 if (!cd->name || !cd->name[0])
355                         goto err;
356                 ret = verify_name(cd->name);
357                 if (ret < 0)
358                         goto err;
359                 t->index_header_size += index_column_description_size(cd->name);
360                 column_name_hash(cd->name, col->name_hash);
361                 ret = -E_OSL_DUPLICATE_COL_NAME;
362                 for (j = i + 1; j < t->desc->num_columns; j++) {
363                         const char *name2 = get_column_description(t->desc,
364                                 j)->name;
365                         if (cd->name && name2 && !strcmp(cd->name, name2))
366                                 goto err;
367                 }
368         }
369         return 1;
370 err:
371         return ret;
372 }
373
374 /**
375  * Initialize a struct table from given table description.
376  *
377  * \param desc The description of the osl table.
378  * \param table_ptr Result is returned here.
379  *
380  * This function performs several sanity checks on \p desc and returns if any
381  * of these tests fail. On success, a struct \p osl_table is allocated and
382  * initialized with data derived from \p desc.
383  *
384  * \return Standard.
385  *
386  * \sa struct osl_table.
387  */
388 int init_table_structure(const struct osl_table_description *desc,
389                 struct osl_table **table_ptr)
390 {
391         const struct osl_column_description *cd;
392         struct osl_table *t = calloc(1, sizeof(*t));
393         int i, ret = -ERRNO_TO_ERROR(ENOMEM), have_disk_storage_name_column = 0;
394
395         if (!t)
396                 return ret;
397         ret = -E_OSL_BAD_TABLE_DESC;
398         if (!desc)
399                 goto err;
400         DEBUG_LOG("creating table structure for '%s' from table "
401                 "description\n", desc->name);
402         ret = -E_OSL_NO_COLUMN_DESC;
403         if (!desc->column_descriptions)
404                 goto err;
405         ret = -E_OSL_NO_COLUMNS;
406         if (!desc->num_columns)
407                 goto err;
408         ret = -ERRNO_TO_ERROR(ENOMEM);
409         t->columns = calloc(desc->num_columns, sizeof(struct osl_column));
410         if (!t->columns)
411                 goto err;
412         t->desc = desc;
413         FOR_EACH_COLUMN(i, t->desc, cd) {
414                 enum osl_storage_type st = cd->storage_type;
415                 enum osl_storage_flags sf = cd->storage_flags;
416                 struct osl_column *col = &t->columns[i];
417
418                 ret = -E_OSL_BAD_STORAGE_TYPE;
419                 if (st != OSL_MAPPED_STORAGE && st != OSL_DISK_STORAGE
420                                 && st != OSL_NO_STORAGE)
421                         goto err;
422                 ret = -E_OSL_BAD_STORAGE_FLAGS;
423                 if (st == OSL_DISK_STORAGE && sf & OSL_RBTREE)
424                         goto err;
425                 ret = -E_OSL_BAD_STORAGE_SIZE;
426                 if (sf & OSL_FIXED_SIZE && !cd->data_size)
427                         goto err;
428                 switch (st) {
429                 case OSL_DISK_STORAGE:
430                         t->num_disk_storage_columns++;
431                         break;
432                 case OSL_MAPPED_STORAGE:
433                         t->num_mapped_columns++;
434                         col->index_offset = t->row_index_size;
435                         t->row_index_size += 8;
436                         break;
437                 case OSL_NO_STORAGE:
438                         col->volatile_num = t->num_volatile_columns;
439                         t->num_volatile_columns++;
440                         break;
441                 }
442                 if (sf & OSL_RBTREE) {
443                         col->rbtree_num = t->num_rbtrees;
444                         t->num_rbtrees++;
445                         if ((sf & OSL_UNIQUE) && (st == OSL_MAPPED_STORAGE)) {
446                                 if (!have_disk_storage_name_column)
447                                         t->disk_storage_name_column = i;
448                                 have_disk_storage_name_column = 1;
449                         }
450                 }
451         }
452         ret = -E_OSL_NO_UNIQUE_RBTREE_COLUMN;
453         if (t->num_disk_storage_columns && !have_disk_storage_name_column)
454                 goto err;
455         ret = -E_OSL_NO_RBTREE_COL;
456         if (!t->num_rbtrees)
457                 goto err;
458         /* success */
459         DEBUG_LOG("OK. Index entry size: %u\n", t->row_index_size);
460         ret = init_column_descriptions(t);
461         if (ret < 0)
462                 goto err;
463         *table_ptr = t;
464         return 1;
465 err:
466         free(t->columns);
467         free(t);
468         return ret;
469 }
470
471 /**
472  * Read the table description from index header.
473  *
474  * \param map The memory mapping of the index file.
475  * \param desc The values found in the index header are returned here.
476  *
477  * Read the index header, check for the paraslash magic string and the table version number.
478  * Read all information stored in the index header into \a desc.
479  *
480  * \return Standard.
481  *
482  * \sa struct osl_table_description, osl_create_table.
483  */
484 int read_table_desc(struct osl_object *map, struct osl_table_description *desc)
485 {
486         char *buf = map->data;
487         uint8_t version;
488         uint16_t header_size;
489         int ret, i;
490         unsigned offset;
491         struct osl_column_description *cd;
492
493         if (map->size < MIN_INDEX_HEADER_SIZE(1))
494                 return -E_OSL_SHORT_TABLE;
495         if (strncmp(buf + IDX_PARA_MAGIC, PARA_MAGIC, strlen(PARA_MAGIC)))
496                 return -E_OSL_NO_MAGIC;
497         version = read_u8(buf + IDX_VERSION);
498         if (version < MIN_TABLE_VERSION || version > MAX_TABLE_VERSION)
499                 return -E_OSL_VERSION_MISMATCH;
500         desc->flags = read_u8(buf + IDX_TABLE_FLAGS);
501         desc->num_columns = read_u16(buf + IDX_NUM_COLUMNS);
502         DEBUG_LOG("%u columns\n", desc->num_columns);
503         if (!desc->num_columns)
504                 return -E_OSL_NO_COLUMNS;
505         header_size = read_u16(buf + IDX_HEADER_SIZE);
506         if (map->size < header_size)
507                 return -E_OSL_BAD_SIZE;
508         desc->column_descriptions = calloc(desc->num_columns,
509                 sizeof(struct osl_column_description));
510         if (!desc->column_descriptions)
511                 return -ERRNO_TO_ERROR(ENOMEM);
512         offset = IDX_COLUMN_DESCRIPTIONS;
513         FOR_EACH_COLUMN(i, desc, cd) {
514                 char *null_byte;
515
516                 ret = -E_OSL_SHORT_TABLE;
517                 if (map->size < offset + MIN_IDX_COLUMN_DESCRIPTION_SIZE) {
518                         ERROR_LOG("map size = %zu < %u = offset + min desc size\n",
519                                 map->size, offset + MIN_IDX_COLUMN_DESCRIPTION_SIZE);
520                         goto err;
521                 }
522                 cd->storage_type = read_u16(buf + offset + IDX_CD_STORAGE_TYPE);
523                 cd->storage_flags = read_u16(buf + offset +
524                         IDX_CD_STORAGE_FLAGS);
525                 cd->data_size = read_u32(buf + offset + IDX_CD_DATA_SIZE);
526                 null_byte = memchr(buf + offset + IDX_CD_NAME, '\0',
527                         map->size - offset - IDX_CD_NAME);
528                 ret = -E_OSL_INDEX_CORRUPTION;
529                 if (!null_byte)
530                         goto err;
531                 ret = -ERRNO_TO_ERROR(ENOMEM);
532                 cd->name = strdup(buf + offset + IDX_CD_NAME);
533                 if (!cd->name)
534                         goto err;
535                 offset += index_column_description_size(cd->name);
536         }
537         if (offset != header_size) {
538                 ret = -E_OSL_INDEX_CORRUPTION;
539                 ERROR_LOG("real header size = %u != %u = stored header size\n",
540                         offset, header_size);
541                 goto err;
542         }
543         return 1;
544 err:
545         FOR_EACH_COLUMN(i, desc, cd)
546                 free(cd->name);
547         return ret;
548 }
549
550 /*
551  * check whether the table description given by \p t->desc matches the on-disk
552  * table structure stored in the index of \a t.
553  */
554 static int compare_table_descriptions(struct osl_table *t)
555 {
556         int i, ret;
557         struct osl_table_description desc;
558         const struct osl_column_description *cd1, *cd2;
559
560         /* read the on-disk structure into desc */
561         ret = read_table_desc(&t->index_map, &desc);
562         if (ret < 0)
563                 return ret;
564         ret = -E_OSL_BAD_TABLE_FLAGS;
565         if (desc.flags != t->desc->flags)
566                 goto out;
567         ret = -E_OSL_BAD_COLUMN_NUM;
568         if (desc.num_columns > t->desc->num_columns)
569                 goto out;
570         if (desc.num_columns < t->desc->num_columns) {
571                 struct osl_column_description *cd;
572                 unsigned diff = t->desc->num_columns - desc.num_columns;
573                 INFO_LOG("extending table by %u volatile columns\n", diff);
574                 ret = -ERRNO_TO_ERROR(ENOMEM);
575                 desc.column_descriptions = realloc(desc.column_descriptions,
576                         t->desc->num_columns * sizeof(struct osl_column_description));
577                 if (!desc.column_descriptions)
578                         goto out;
579                 for (i = desc.num_columns; i < t->desc->num_columns; i++) {
580                         cd = get_column_description(&desc, i);
581                         cd->storage_type = OSL_NO_STORAGE;
582                         cd->name = NULL;
583                 }
584                 desc.num_columns += diff;
585         }
586         FOR_EACH_COLUMN(i, t->desc, cd1) {
587                 cd2 = get_column_description(&desc, i);
588                 ret = -E_OSL_BAD_STORAGE_TYPE;
589                 if (cd1->storage_type != cd2->storage_type)
590                         goto out;
591                 if (cd1->storage_type == OSL_NO_STORAGE)
592                         continue;
593                 ret = -E_OSL_BAD_STORAGE_FLAGS;
594                 if (cd1->storage_flags != cd2->storage_flags) {
595                         ERROR_LOG("sf1 = %u != %u = sf2\n",
596                                 cd1->storage_flags, cd2->storage_flags);
597                         goto out;
598                 }
599                 ret = -E_OSL_BAD_DATA_SIZE;
600                 if (cd1->storage_flags & OSL_FIXED_SIZE)
601                         if (cd1->data_size != cd2->data_size)
602                                 goto out;
603                 ret = -E_OSL_BAD_COLUMN_NAME;
604                 if (strcmp(cd1->name, cd2->name))
605                         goto out;
606         }
607         DEBUG_LOG("table description of '%s' matches on-disk data, good\n",
608                 t->desc->name);
609         ret = 1;
610 out:
611         FOR_EACH_COLUMN(i, &desc, cd1)
612                 free(cd1->name);
613         free(desc.column_descriptions);
614         return ret;
615 }
616
617 static int create_table_index(struct osl_table *t)
618 {
619         char *buf, *filename;
620         int i, ret;
621         size_t size = t->index_header_size;
622         const struct osl_column_description *cd;
623         unsigned offset;
624
625         INFO_LOG("creating %zu byte index for table %s\n", size,
626                 t->desc->name);
627         buf = calloc(1, size);
628         if (!buf)
629                 return -ERRNO_TO_ERROR(ENOMEM);
630         sprintf(buf + IDX_PARA_MAGIC, "%s", PARA_MAGIC);
631         write_u8(buf + IDX_TABLE_FLAGS, t->desc->flags);
632         write_u8(buf + IDX_DIRTY_FLAG, 0);
633         write_u8(buf + IDX_VERSION, CURRENT_TABLE_VERSION);
634         write_u16(buf + IDX_NUM_COLUMNS, t->num_mapped_columns + t->num_disk_storage_columns);
635         write_u16(buf + IDX_HEADER_SIZE, t->index_header_size);
636         offset = IDX_COLUMN_DESCRIPTIONS;
637         FOR_EACH_COLUMN(i, t->desc, cd) {
638                 /* no need to store info about volatile storage */
639                 if (cd->storage_type == OSL_NO_STORAGE)
640                         continue;
641                 write_u16(buf + offset + IDX_CD_STORAGE_TYPE,
642                         cd->storage_type);
643                 write_u16(buf + offset + IDX_CD_STORAGE_FLAGS,
644                         cd->storage_flags);
645                 if (cd->storage_flags & OSL_FIXED_SIZE)
646                         write_u32(buf + offset + IDX_CD_DATA_SIZE,
647                                 cd->data_size);
648                 strcpy(buf + offset + IDX_CD_NAME, cd->name);
649                 offset += index_column_description_size(cd->name);
650         }
651         assert(offset = size);
652         filename = index_filename(t->desc);
653         if (filename)
654                 ret = write_file(filename, buf, size);
655         else
656                 ret = -ERRNO_TO_ERROR(ENOMEM);
657         free(buf);
658         free(filename);
659         return ret;
660 }
661
662 /**
663  * Create a new osl table.
664  *
665  * \param desc Pointer to the table description.
666  *
667  * \return Standard.
668  */
669 __export int osl_create_table(const struct osl_table_description *desc)
670 {
671         const struct osl_column_description *cd;
672         char *table_dir = NULL, *filename;
673         struct osl_table *t;
674         int i, ret = init_table_structure(desc, &t);
675
676         if (ret < 0)
677                 return ret;
678         INFO_LOG("creating %s\n", desc->name);
679         FOR_EACH_COLUMN(i, t->desc, cd) {
680                 if (cd->storage_type == OSL_NO_STORAGE)
681                         continue;
682                 if (!table_dir) {
683                         ret = para_mkdir(desc->dir, 0777);
684                         if (ret < 0 && !is_errno(-ret, EEXIST))
685                                 goto out;
686                         table_dir = make_message("%s/%s", desc->dir,
687                                 desc->name);
688                         ret = -ERRNO_TO_ERROR(ENOMEM);
689                         if (!table_dir)
690                                 goto out;
691                         ret = para_mkdir(table_dir, 0777);
692                         if (ret < 0)
693                                 goto out;
694                 }
695                 ret = -ERRNO_TO_ERROR(ENOMEM);
696                 filename = column_filename(t, i);
697                 if (!filename)
698                         goto out;
699                 INFO_LOG("filename: %s\n", filename);
700                 if (cd->storage_type == OSL_MAPPED_STORAGE) {
701                         ret = osl_open(filename, O_RDWR | O_CREAT | O_EXCL,
702                                 0644);
703                         free(filename);
704                         if (ret < 0)
705                                 goto out;
706                         close(ret);
707                         continue;
708                 }
709                 /* DISK STORAGE */
710                 ret = para_mkdir(filename, 0777);
711                 free(filename);
712                 if (ret < 0)
713                         goto out;
714         }
715         if (t->num_mapped_columns) {
716                 ret = create_table_index(t);
717                 if (ret < 0)
718                         goto out;
719         }
720         ret = 1;
721 out:
722         free(table_dir);
723         free(t->columns);
724         free(t);
725         return ret;
726 }
727
728 static int table_is_dirty(struct osl_table *t)
729 {
730         char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
731         uint8_t dirty = read_u8(buf) & 0x1;
732         return !!dirty;
733 }
734
735 static void mark_table_dirty(struct osl_table *t)
736 {
737         char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
738         write_u8(buf, read_u8(buf) | 1);
739 }
740
741 static void mark_table_clean(struct osl_table *t)
742 {
743         char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
744         write_u8(buf, read_u8(buf) & 0xfe);
745 }
746
747 static void unmap_column(struct osl_table *t, unsigned col_num)
748 {
749         struct osl_object map = t->columns[col_num].data_map;
750         int ret;
751         if (!map.data)
752                 return;
753         ret = para_munmap(map.data, map.size);
754         assert(ret > 0);
755         map.data = NULL;
756 }
757
758 /**
759  * Unmap all mapped files of an osl table.
760  *
761  * \param t Pointer to a mapped table.
762  * \param flags Options for unmapping.
763  *
764  * \return Positive on success, negative on errors.
765  *
766  * \sa map_table(), enum osl_close_flags, para_munmap().
767  */
768 int unmap_table(struct osl_table *t, enum osl_close_flags flags)
769 {
770         unsigned i;
771         const struct osl_column_description *cd;
772         int ret;
773
774         if (!t->num_mapped_columns) /* can this ever happen? */
775                 return 1;
776         DEBUG_LOG("unmapping table '%s'\n", t->desc->name);
777         if (!t->index_map.data)
778                 return -E_OSL_NOT_MAPPED;
779         if (flags & OSL_MARK_CLEAN)
780                 mark_table_clean(t);
781         ret = para_munmap(t->index_map.data, t->index_map.size);
782         if (ret < 0)
783                 return ret;
784         t->index_map.data = NULL;
785         if (!t->num_rows)
786                 return 1;
787         FOR_EACH_MAPPED_COLUMN(i, t, cd)
788                 unmap_column(t, i);
789         return 1;
790 }
791
792 static int map_column(struct osl_table *t, unsigned col_num)
793 {
794         struct stat statbuf;
795         char *filename = column_filename(t, col_num);
796         int ret = -E_OSL_STAT;
797
798         if (!filename)
799                 return -ERRNO_TO_ERROR(ENOMEM);
800         if (stat(filename, &statbuf) < 0) {
801                 free(filename);
802                 return ret;
803         }
804         if (!(S_IFREG & statbuf.st_mode)) {
805                 free(filename);
806                 return ret;
807         }
808         ret = mmap_full_file(filename, O_RDWR,
809                 &t->columns[col_num].data_map.data,
810                 &t->columns[col_num].data_map.size,
811                 NULL);
812         free(filename);
813         return ret;
814 }
815
816 /**
817  * Map the index file and all columns of type \p OSL_MAPPED_STORAGE into memory.
818  *
819  * \param t Pointer to an initialized table structure.
820  * \param flags Mapping options.
821  *
822  * \return Negative return value on errors; on success the number of rows
823  * (including invalid rows) is returned.
824  *
825  * \sa unmap_table(), enum map_table_flags, osl_open_table(), mmap(2).
826  */
827 int map_table(struct osl_table *t, enum map_table_flags flags)
828 {
829         char *filename;
830         const struct osl_column_description *cd;
831         int i = 0, ret, num_rows = 0;
832
833         if (!t->num_mapped_columns)
834                 return 0;
835         if (t->index_map.data)
836                 return -E_OSL_ALREADY_MAPPED;
837         filename = index_filename(t->desc);
838         if (!filename)
839                 return -ERRNO_TO_ERROR(ENOMEM);
840         DEBUG_LOG("mapping table '%s' (index: %s)\n", t->desc->name, filename);
841         ret = mmap_full_file(filename, flags & MAP_TBL_FL_MAP_RDONLY?
842                 O_RDONLY : O_RDWR, &t->index_map.data, &t->index_map.size, NULL);
843         free(filename);
844         if (ret < 0)
845                 return ret;
846         if (flags & MAP_TBL_FL_VERIFY_INDEX) {
847                 ret = compare_table_descriptions(t);
848                 if (ret < 0)
849                         goto err;
850         }
851         ret = -E_OSL_BUSY;
852         if (!(flags & MAP_TBL_FL_IGNORE_DIRTY)) {
853                 if (table_is_dirty(t)) {
854                         ERROR_LOG("%s is dirty\n", t->desc->name);
855                         goto err;
856                 }
857         }
858         mark_table_dirty(t);
859         num_rows = table_num_rows(t);
860         if (!num_rows)
861                 return num_rows;
862         /* map data files */
863         FOR_EACH_MAPPED_COLUMN(i, t, cd) {
864                 ret = map_column(t, i);
865                 if (ret < 0)
866                         goto err;
867         }
868         return num_rows;
869 err:    /* unmap what is already mapped */
870         for (i--; i >= 0; i--) {
871                 struct osl_object map = t->columns[i].data_map;
872                 para_munmap(map.data, map.size);
873                 map.data = NULL;
874         }
875         para_munmap(t->index_map.data, t->index_map.size);
876         t->index_map.data = NULL;
877         return ret;
878 }
879
880 /**
881  * Retrieve a mapped object by row and column number.
882  *
883  * \param t Pointer to an open osl table.
884  * \param col_num Number of the mapped column containing the object to retrieve.
885  * \param row_num Number of the row containing the object to retrieve.
886  * \param obj The result is returned here.
887  *
888  * It is considered an error if \a col_num does not refer to a column
889  * of storage type \p OSL_MAPPED_STORAGE.
890  *
891  * \return Standard.
892  *
893  * \sa osl_storage_type.
894  */
895 int get_mapped_object(const struct osl_table *t, unsigned col_num,
896         uint32_t row_num, struct osl_object *obj)
897 {
898         struct osl_column *col = &t->columns[col_num];
899         uint32_t offset;
900         char *header;
901         char *cell_index;
902         int ret;
903
904         if (t->num_rows <= row_num)
905                 return -E_OSL_BAD_ROW_NUM;
906         ret = get_cell_index(t, row_num, col_num, &cell_index);
907         if (ret < 0)
908                 return ret;
909         offset = read_u32(cell_index);
910         obj->size = read_u32(cell_index + 4) - 1;
911         header = col->data_map.data + offset;
912         obj->data = header + 1;
913         if (read_u8(header) == 0xff) {
914                 ERROR_LOG("col %u, size %zu, offset %u\n", col_num,
915                         obj->size, offset);
916                 return -E_OSL_INVALID_OBJECT;
917         }
918         return 1;
919 }
920
921 static int search_rbtree(const struct osl_object *obj,
922                 const struct osl_table *t, unsigned col_num,
923                 struct rb_node **result, struct rb_node ***rb_link)
924 {
925         struct osl_column *col = &t->columns[col_num];
926         struct rb_node **new = &col->rbtree.rb_node, *parent = NULL;
927         const struct osl_column_description *cd =
928                 get_column_description(t->desc, col_num);
929         enum osl_storage_type st = cd->storage_type;
930         while (*new) {
931                 struct osl_row *this_row = get_row_pointer(*new,
932                         col->rbtree_num);
933                 int ret;
934                 struct osl_object this_obj;
935                 parent = *new;
936                 if (st == OSL_MAPPED_STORAGE) {
937                         ret = get_mapped_object(t, col_num, this_row->num,
938                                 &this_obj);
939                         if (ret < 0)
940                                 return ret;
941                 } else
942                         this_obj = this_row->volatile_objects[col->volatile_num];
943                 ret = cd->compare_function(obj, &this_obj);
944                 if (!ret) {
945                         if (result)
946                                 *result = get_rb_node_pointer(this_row,
947                                         col->rbtree_num);
948                         return 1;
949                 }
950                 if (ret < 0)
951                         new = &((*new)->rb_left);
952                 else
953                         new = &((*new)->rb_right);
954         }
955         if (result)
956                 *result = parent;
957         if (rb_link)
958                 *rb_link = new;
959         return -E_OSL_RB_KEY_NOT_FOUND;
960 }
961
962 static int insert_rbtree(struct osl_table *t, unsigned col_num,
963         const struct osl_row *row, const struct osl_object *obj)
964 {
965         struct rb_node *parent, **rb_link;
966         unsigned rbtree_num;
967         struct rb_node *n;
968         int ret = search_rbtree(obj, t, col_num, &parent, &rb_link);
969
970         if (ret > 0)
971                 return -E_OSL_RB_KEY_EXISTS;
972         rbtree_num = t->columns[col_num].rbtree_num;
973         n = get_rb_node_pointer(row, rbtree_num);
974         rb_link_node(n, parent, rb_link);
975         rb_insert_color(n, &t->columns[col_num].rbtree);
976         return 1;
977 }
978
979 static void remove_rb_node(struct osl_table *t, unsigned col_num,
980                 const struct osl_row *row)
981 {
982         struct osl_column *col = &t->columns[col_num];
983         const struct osl_column_description *cd =
984                 get_column_description(t->desc, col_num);
985         enum osl_storage_flags sf = cd->storage_flags;
986         struct rb_node *victim, *splice_out_node, *tmp;
987         if (!(sf & OSL_RBTREE))
988                 return;
989         /*
990          * Which node is removed/spliced out actually depends on how many
991          * children the victim node has: If it has no children, it gets
992          * deleted. If it has one child, it gets spliced out. If it has two
993          * children, its successor (which has at most a right child) gets
994          * spliced out.
995          */
996         victim = get_rb_node_pointer(row, col->rbtree_num);
997         if (victim->rb_left && victim->rb_right)
998                 splice_out_node = rb_next(victim);
999         else
1000                 splice_out_node = victim;
1001         /* Go up to the root and decrement the size of each node in the path. */
1002         for (tmp = splice_out_node; tmp; tmp = rb_parent(tmp))
1003                 tmp->size--;
1004         rb_erase(victim, &col->rbtree);
1005 }
1006
1007 static int add_row_to_rbtrees(struct osl_table *t, uint32_t row_num,
1008                 struct osl_object *volatile_objs, struct osl_row **row_ptr)
1009 {
1010         unsigned i;
1011         int ret;
1012         struct osl_row *row = allocate_row(t->num_rbtrees);
1013         const struct osl_column_description *cd;
1014
1015         if (!row)
1016                 return -ERRNO_TO_ERROR(ENOMEM);
1017         row->num = row_num;
1018         row->volatile_objects = volatile_objs;
1019         FOR_EACH_RBTREE_COLUMN(i, t, cd) {
1020                 if (cd->storage_type == OSL_MAPPED_STORAGE) {
1021                         struct osl_object obj;
1022                         ret = get_mapped_object(t, i, row_num, &obj);
1023                         if (ret < 0)
1024                                 goto err;
1025                         ret = insert_rbtree(t, i, row, &obj);
1026                 } else { /* volatile */
1027                         const struct osl_object *obj
1028                                 = volatile_objs + t->columns[i].volatile_num;
1029                         ret = insert_rbtree(t, i, row, obj);
1030                 }
1031                 if (ret < 0)
1032                         goto err;
1033         }
1034         if (row_ptr)
1035                 *row_ptr = row;
1036         return 1;
1037 err: /* rollback changes, i.e. remove added entries from rbtrees */
1038         while (i)
1039                 remove_rb_node(t, i--, row);
1040         free(row);
1041         return ret;
1042 }
1043
1044 static void free_volatile_objects(const struct osl_table *t,
1045                 enum osl_close_flags flags)
1046 {
1047         int i, j;
1048         struct rb_node *n;
1049         struct osl_column *rb_col;
1050         const struct osl_column_description *cd;
1051
1052         if (!t->num_volatile_columns)
1053                 return;
1054         /* find the first rbtree column (any will do) */
1055         FOR_EACH_RBTREE_COLUMN(i, t, cd)
1056                 break;
1057         rb_col = t->columns + i;
1058         /* walk that rbtree and free all volatile objects */
1059         for (n = rb_first(&rb_col->rbtree); n; n = rb_next(n)) {
1060                 struct osl_row *r = get_row_pointer(n, rb_col->rbtree_num);
1061                 if (flags & OSL_FREE_VOLATILE)
1062                         FOR_EACH_VOLATILE_COLUMN(j, t, cd) {
1063                                 if (cd->storage_flags & OSL_DONT_FREE)
1064                                         continue;
1065                                 free(r->volatile_objects[
1066                                         t->columns[j].volatile_num].data);
1067                         }
1068 //                      for (j = 0; j < t->num_volatile_columns; j++)
1069 //                              free(r->volatile_objects[j].data);
1070                 free(r->volatile_objects);
1071         }
1072 }
1073
1074 /**
1075  * Erase all rbtree nodes and free resources.
1076  *
1077  * \param t Pointer to an open osl table.
1078  *
1079  * This function is called by osl_close_table().
1080  */
1081 void clear_rbtrees(struct osl_table *t)
1082 {
1083         const struct osl_column_description *cd;
1084         unsigned i, rbtrees_cleared = 0;
1085
1086         FOR_EACH_RBTREE_COLUMN(i, t, cd) {
1087                 struct osl_column *col = &t->columns[i];
1088                 struct rb_node *n;
1089                 rbtrees_cleared++;
1090                 for (n = rb_first(&col->rbtree); n;) {
1091                         struct osl_row *r;
1092                         rb_erase(n, &col->rbtree);
1093                         if (rbtrees_cleared == t->num_rbtrees) {
1094                                 r = get_row_pointer(n, col->rbtree_num);
1095                                 n = rb_next(n);
1096                                 free(r);
1097                         } else
1098                                 n = rb_next(n);
1099                 }
1100         }
1101
1102 }
1103
1104 /**
1105  * Close an osl table.
1106  *
1107  * \param t Pointer to the table to be closed.
1108  * \param flags Options for what should be cleaned up.
1109  *
1110  * If osl_open_table() succeeds, the resulting table pointer must later be
1111  * passed to this function in order to flush all changes to the file system and
1112  * to free the resources that were allocated by osl_open_table().
1113  *
1114  * \return Standard.
1115  *
1116  * \sa osl_open_table(), unmap_table().
1117  */
1118 __export int osl_close_table(struct osl_table *t, enum osl_close_flags flags)
1119 {
1120         int ret;
1121
1122         if (!t)
1123                 return -E_OSL_BAD_TABLE;
1124         free_volatile_objects(t, flags);
1125         clear_rbtrees(t);
1126         ret = unmap_table(t, flags);
1127         if (ret < 0)
1128                 ERROR_LOG("unmap_table failed: %d\n", ret);
1129         free(t->columns);
1130         free(t);
1131         return ret;
1132 }
1133
1134 /**
1135  * Find out whether the given row number corresponds to an invalid row.
1136  *
1137  * \param t Pointer to the osl table.
1138  * \param row_num The number of the row in question.
1139  *
1140  * By definition, a row is considered invalid if all its index entries
1141  * are invalid.
1142  *
1143  * \return Positive if \a row_num corresponds to an invalid row,
1144  * zero if it corresponds to a valid row, negative on errors.
1145  */
1146 int row_is_invalid(struct osl_table *t, uint32_t row_num)
1147 {
1148         char *row_index;
1149         int i, ret = get_row_index(t, row_num, &row_index);
1150
1151         if (ret < 0)
1152                 return ret;
1153         for (i = 0; i < t->row_index_size; i++) {
1154                 if ((unsigned char)row_index[i] != 0xff)
1155                         return 0;
1156         }
1157         INFO_LOG("row %d is invalid\n", row_num);
1158         return 1;
1159 }
1160
1161 /**
1162  * Invalidate a row of an osl table.
1163  *
1164  * \param t Pointer to an open osl table.
1165  * \param row_num Number of the row to mark as invalid.
1166  *
1167  * This function marks each mapped object in the index entry of \a row as
1168  * invalid.
1169  *
1170  * \return Standard.
1171  */
1172 int mark_row_invalid(struct osl_table *t, uint32_t row_num)
1173 {
1174         char *row_index;
1175         int ret = get_row_index(t, row_num, &row_index);
1176
1177         if (ret < 0)
1178                 return ret;
1179         INFO_LOG("marking row %d as invalid\n", row_num);
1180         memset(row_index, 0xff, t->row_index_size);
1181         return 1;
1182 }
1183
1184 /**
1185  * Initialize all rbtrees and compute number of invalid rows.
1186  *
1187  * \param t The table containing the rbtrees to be initialized.
1188  *
1189  * \return Standard.
1190  */
1191 int init_rbtrees(struct osl_table *t)
1192 {
1193         int i, ret;
1194         const struct osl_column_description *cd;
1195
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);
1203                 if (ret < 0)
1204                         return ret;
1205                 if (ret) {
1206                         t->num_invalid_rows++;
1207                         continue;
1208                 }
1209                 ret = add_row_to_rbtrees(t, i, NULL, NULL);
1210                 if (ret < 0)
1211                         return ret;
1212         }
1213         return 1;
1214 }
1215
1216 /**
1217  * Open an osl table.
1218  *
1219  * Each osl table must be opened before its data can be accessed.
1220  *
1221  * \param table_desc Describes the table to be opened.
1222  * \param result Contains a pointer to the open table on success.
1223  *
1224  * The table description given by \a desc should coincide with the
1225  * description used at creation time.
1226  *
1227  * \return Standard.
1228  */
1229 __export int osl_open_table(const struct osl_table_description *table_desc,
1230                 struct osl_table **result)
1231 {
1232         int i, ret;
1233         struct osl_table *t;
1234         const struct osl_column_description *cd;
1235
1236         INFO_LOG("opening table %s\n", table_desc->name);
1237         ret = init_table_structure(table_desc, &t);
1238         if (ret < 0)
1239                 return ret;
1240         FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
1241                 struct stat statbuf;
1242                 char *dirname = column_filename(t, i);
1243
1244                 ret = -ERRNO_TO_ERROR(ENOMEM);
1245                 if (!dirname)
1246                         goto err;
1247                 /* check if directory exists */
1248                 ret = stat(dirname, &statbuf);
1249                 free(dirname);
1250                 if (ret < 0) {
1251                         ret = -ERRNO_TO_ERROR(errno);
1252                         goto err;
1253                 }
1254                 ret = -ERRNO_TO_ERROR(ENOTDIR);
1255                 if (!S_ISDIR(statbuf.st_mode))
1256                         goto err;
1257         }
1258         ret = map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1259         if (ret < 0)
1260                 goto err;
1261         t->num_rows = ret;
1262         DEBUG_LOG("num rows: %d\n", t->num_rows);
1263         ret = init_rbtrees(t);
1264         if (ret < 0) {
1265                 osl_close_table(t, OSL_MARK_CLEAN); /* ignore further errors */
1266                 return ret;
1267         }
1268         *result = t;
1269         return 1;
1270 err:
1271         free(t->columns);
1272         free(t);
1273         return ret;
1274 }
1275
1276 static int create_disk_storage_object_dir(const struct osl_table *t,
1277                 unsigned col_num, const char *ds_name)
1278 {
1279         char *dirname;
1280         int ret;
1281
1282         if (!(t->desc->flags & OSL_LARGE_TABLE))
1283                 return 1;
1284         dirname = disk_storage_dirname(t, col_num, ds_name);
1285         if (!dirname)
1286                 return -ERRNO_TO_ERROR(ENOMEM);
1287         ret = para_mkdir(dirname, 0777);
1288         free(dirname);
1289         if (ret < 0 && !is_errno(-ret, EEXIST))
1290                 return ret;
1291         return 1;
1292 }
1293
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)
1296 {
1297         int ret;
1298         char *filename;
1299
1300         ret = create_disk_storage_object_dir(t, col_num, ds_name);
1301         if (ret < 0)
1302                 return ret;
1303         filename = disk_storage_path(t, col_num, ds_name);
1304         if (!filename)
1305                 return -ERRNO_TO_ERROR(ENOMEM);
1306         ret = write_file(filename, obj->data, obj->size);
1307         free(filename);
1308         return ret;
1309 }
1310
1311 static int append_map_file(const struct osl_table *t, unsigned col_num,
1312         const struct osl_object *obj, uint32_t *new_size)
1313 {
1314         char *filename = column_filename(t, col_num);
1315         int ret;
1316         char header = 0; /* zero means valid object */
1317
1318         if (!filename)
1319                 return -ERRNO_TO_ERROR(ENOMEM);
1320         ret = append_file(filename, &header, 1, obj->data, obj->size,
1321                 new_size);
1322         free(filename);
1323         return ret;
1324 }
1325
1326 static int append_row_index(const struct osl_table *t, char *row_index)
1327 {
1328         char *filename;
1329         int ret;
1330
1331         if (!t->num_mapped_columns)
1332                 return 1;
1333         filename = index_filename(t->desc);
1334         if (!filename)
1335                 return -ERRNO_TO_ERROR(ENOMEM);
1336         ret = append_file(filename, NULL, 0, row_index,
1337                 t->row_index_size, NULL);
1338         free(filename);
1339         return ret;
1340 }
1341
1342 /**
1343  * A wrapper for truncate(2)
1344  *
1345  * \param path Name of the regular file to truncate
1346  * \param size Number of bytes to \b shave \b off
1347  *
1348  * Truncate the regular file named by \a path by \a size bytes.
1349  *
1350  * \return Standard.
1351  *
1352  * \sa truncate(2)
1353  */
1354 int para_truncate(const char *path, off_t size)
1355 {
1356         int ret;
1357         struct stat statbuf;
1358
1359         ret = -E_OSL_STAT;
1360         if (stat(path, &statbuf) < 0)
1361                 goto out;
1362         ret = -E_OSL_BAD_SIZE;
1363         if (statbuf.st_size < size)
1364                 goto out;
1365         ret = -E_OSL_TRUNC;
1366         if (truncate(path, statbuf.st_size - size) < 0)
1367                 goto out;
1368         ret = 1;
1369 out:
1370         return ret;
1371 }
1372
1373 static int truncate_mapped_file(const struct osl_table *t, unsigned col_num,
1374                 off_t size)
1375 {
1376         int ret;
1377         char *filename = column_filename(t, col_num);
1378
1379         if (!filename)
1380                 return -ERRNO_TO_ERROR(ENOMEM);
1381         ret = para_truncate(filename, size);
1382         free(filename);
1383         return ret;
1384 }
1385
1386 static int delete_disk_storage_file(const struct osl_table *t, unsigned col_num,
1387                 const char *ds_name)
1388 {
1389         char *dirname, *filename = disk_storage_path(t, col_num, ds_name);
1390         int ret, err;
1391
1392         if (!filename)
1393                 return -ERRNO_TO_ERROR(ENOMEM);
1394         ret = unlink(filename);
1395         err = errno;
1396         free(filename);
1397         if (ret < 0)
1398                 return -ERRNO_TO_ERROR(err);
1399         if (!(t->desc->flags & OSL_LARGE_TABLE))
1400                 return 1;
1401         dirname = disk_storage_dirname(t, col_num, ds_name);
1402         if (!dirname)
1403                 return -ERRNO_TO_ERROR(ENOMEM);
1404         rmdir(dirname);
1405         free(dirname);
1406         return 1;
1407 }
1408
1409 /**
1410  * Add a new row to an osl table and retrieve this row.
1411  *
1412  * \param t Pointer to an open osl table.
1413  * \param objects Array of objects to be added.
1414  * \param row Result pointer.
1415  *
1416  * The \a objects parameter must point to an array containing one object per
1417  * column.  The order of the objects in the array is given by the table
1418  * description of \a table. Several sanity checks are performed during object
1419  * insertion and the function returns without modifying the table if any of
1420  * these tests fail.  In fact, it is atomic in the sense that it either
1421  * succeeds or leaves the table unchanged (i.e. either all or none of the
1422  * objects are added to the table).
1423  *
1424  * It is considered an error if an object is added to a column with associated
1425  * rbtree if this object is equal to an object already contained in that column
1426  * (i.e. the compare function for the column's rbtree returns zero).
1427  *
1428  * \return Standard.
1429  *
1430  * \sa struct osl_table_description, osl_compare_func, osl_add_row().
1431  */
1432 __export int osl_add_and_get_row(struct osl_table *t, struct osl_object *objects,
1433                 struct osl_row **row)
1434 {
1435         int i, ret;
1436         char *ds_name = NULL;
1437         struct rb_node **rb_parents = NULL, ***rb_links = NULL;
1438         char *new_row_index = NULL;
1439         struct osl_object *volatile_objs = NULL;
1440         const struct osl_column_description *cd;
1441
1442         if (!t)
1443                 return -E_OSL_BAD_TABLE;
1444         rb_parents = malloc(t->num_rbtrees * sizeof(struct rn_node*));
1445         if (!rb_parents)
1446                 return -ERRNO_TO_ERROR(ENOMEM);
1447         rb_links = malloc(t->num_rbtrees * sizeof(struct rn_node**));
1448         if (!rb_links) {
1449                 free(rb_parents);
1450                 return -ERRNO_TO_ERROR(ENOMEM);
1451         }
1452         if (t->num_mapped_columns) {
1453                 new_row_index = malloc(t->row_index_size);
1454                 if (!new_row_index) {
1455                         free(rb_links);
1456                         free(rb_parents);
1457                         return -ERRNO_TO_ERROR(ENOMEM);
1458                 }
1459         }
1460         /* pass 1: sanity checks */
1461 //      DEBUG_LOG("sanity tests: %p:%p\n", objects[0].data,
1462 //              objects[1].data);
1463         FOR_EACH_COLUMN(i, t->desc, cd) {
1464                 enum osl_storage_type st = cd->storage_type;
1465                 enum osl_storage_flags sf = cd->storage_flags;
1466
1467 //              ret = -E_OSL_NULL_OBJECT;
1468 //              if (!objects[i])
1469 //                      goto out;
1470                 if (st == OSL_DISK_STORAGE)
1471                         continue;
1472                 if (sf & OSL_RBTREE) {
1473                         unsigned rbtree_num = t->columns[i].rbtree_num;
1474                         ret = -E_OSL_RB_KEY_EXISTS;
1475 //                      DEBUG_LOG("checking whether %p exists\n",
1476 //                              objects[i].data);
1477                         if (search_rbtree(objects + i, t, i,
1478                                         &rb_parents[rbtree_num],
1479                                         &rb_links[rbtree_num]) > 0)
1480                                 goto out;
1481                 }
1482                 if (sf & OSL_FIXED_SIZE) {
1483 //                      DEBUG_LOG("fixed size. need: %zu, have: %d\n",
1484 //                              objects[i].size, cd->data_size);
1485                         ret = -E_OSL_BAD_DATA_SIZE;
1486                         if (objects[i].size != cd->data_size)
1487                                 goto out;
1488                 }
1489         }
1490         if (t->num_disk_storage_columns) {
1491                 ds_name = disk_storage_name_of_object(t,
1492                         &objects[t->disk_storage_name_column]);
1493                 ret = -ERRNO_TO_ERROR(ENOMEM);
1494                 if (!ds_name)
1495                         goto out;
1496         }
1497         ret = unmap_table(t, OSL_MARK_CLEAN);
1498         if (ret < 0)
1499                 goto out;
1500 //      DEBUG_LOG("sanity tests passed%s\n", "");
1501         /* pass 2: create data files, append map data */
1502         FOR_EACH_COLUMN(i, t->desc, cd) {
1503                 enum osl_storage_type st = cd->storage_type;
1504                 if (st == OSL_NO_STORAGE)
1505                         continue;
1506                 if (st == OSL_MAPPED_STORAGE) {
1507                         uint32_t new_size;
1508                         struct osl_column *col = &t->columns[i];
1509 //                      DEBUG_LOG("appending object of size %zu\n",
1510 //                              objects[i].size);
1511                         ret = append_map_file(t, i, objects + i, &new_size);
1512                         if (ret < 0)
1513                                 goto rollback;
1514                         update_cell_index(new_row_index, col, new_size,
1515                                 objects[i].size);
1516                         continue;
1517                 }
1518                 /* DISK_STORAGE */
1519                 ret = write_disk_storage_file(t, i, objects + i, ds_name);
1520                 if (ret < 0)
1521                         goto rollback;
1522         }
1523         ret = append_row_index(t, new_row_index);
1524         if (ret < 0)
1525                 goto rollback;
1526         ret = map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1527         if (ret < 0) { /* truncate index and rollback changes */
1528                 char *filename = index_filename(t->desc);
1529                 if (filename)
1530                         para_truncate(filename, t->row_index_size);
1531                 free(filename);
1532                 goto rollback;
1533         }
1534         /* pass 3: add entry to rbtrees */
1535         if (t->num_volatile_columns) {
1536                 ret = -ERRNO_TO_ERROR(ENOMEM);
1537                 volatile_objs = calloc(t->num_volatile_columns,
1538                         sizeof(struct osl_object));
1539                 if (!volatile_objs)
1540                         goto out;
1541                 FOR_EACH_VOLATILE_COLUMN(i, t, cd)
1542                         volatile_objs[t->columns[i].volatile_num] = objects[i];
1543         }
1544         t->num_rows++;
1545 //      DEBUG_LOG("adding new entry as row #%d\n", t->num_rows - 1);
1546         ret = add_row_to_rbtrees(t, t->num_rows - 1, volatile_objs, row);
1547         if (ret < 0)
1548                 goto out;
1549 //      DEBUG_LOG("added new entry as row #%d\n", t->num_rows - 1);
1550         ret = 1;
1551         goto out;
1552 rollback: /* rollback all changes made, ignore further errors */
1553         for (i--; i >= 0; i--) {
1554                 cd = get_column_description(t->desc, i);
1555                 enum osl_storage_type st = cd->storage_type;
1556                 if (st == OSL_NO_STORAGE)
1557                         continue;
1558
1559                 if (st == OSL_MAPPED_STORAGE)
1560                         truncate_mapped_file(t, i, objects[i].size);
1561                 else /* disk storage */
1562                         delete_disk_storage_file(t, i, ds_name);
1563         }
1564         /* ignore error and return previous error value */
1565         map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1566 out:
1567         free(new_row_index);
1568         free(ds_name);
1569         free(rb_parents);
1570         free(rb_links);
1571         return ret;
1572 }
1573
1574 /**
1575  * Add a new row to an osl table.
1576  *
1577  * \param t Same meaning as osl_add_and_get_row().
1578  * \param objects Same meaning as osl_add_and_get_row().
1579  *
1580  * \return The return value of the underlying call to osl_add_and_get_row().
1581  *
1582  * This is equivalent to osl_add_and_get_row(t, objects, NULL).
1583  */
1584 __export int osl_add_row(struct osl_table *t, struct osl_object *objects)
1585 {
1586         return osl_add_and_get_row(t, objects, NULL);
1587 }
1588
1589 /**
1590  * Retrieve an object identified by row and column
1591  *
1592  * \param t Pointer to an open osl table.
1593  * \param r Pointer to the row.
1594  * \param col_num The column number.
1595  * \param object The result pointer.
1596  *
1597  * The column determined by \a col_num must be of type \p OSL_MAPPED_STORAGE
1598  * or \p OSL_NO_STORAGE, i.e. no disk storage objects may be retrieved by this
1599  * function.
1600  *
1601  * \return Standard.
1602  *
1603  * \sa osl_storage_type, osl_open_disk_object().
1604  */
1605 __export int osl_get_object(const struct osl_table *t, const struct osl_row *r,
1606         unsigned col_num, struct osl_object *object)
1607 {
1608         const struct osl_column_description *cd;
1609
1610         if (!t)
1611                 return -E_OSL_BAD_TABLE;
1612         cd = get_column_description(t->desc, col_num);
1613         /* col must not be disk storage */
1614         if (cd->storage_type == OSL_DISK_STORAGE)
1615                 return -E_OSL_BAD_STORAGE_TYPE;
1616         if (cd->storage_type == OSL_MAPPED_STORAGE)
1617                 return get_mapped_object(t, col_num, r->num, object);
1618         /* volatile */
1619         *object = r->volatile_objects[t->columns[col_num].volatile_num];
1620         return 1;
1621 }
1622
1623 static int mark_mapped_object_invalid(const struct osl_table *t,
1624                 uint32_t row_num, unsigned col_num)
1625 {
1626         struct osl_object obj;
1627         char *p;
1628         int ret = get_mapped_object(t, col_num, row_num, &obj);
1629
1630         if (ret < 0)
1631                 return ret;
1632         p = obj.data;
1633         p--;
1634         *p = 0xff;
1635         return 1;
1636 }
1637
1638 /**
1639  * Delete a row from an osl table.
1640  *
1641  * \param t Pointer to an open osl table.
1642  * \param row Pointer to the row to delete.
1643  *
1644  * This removes all disk storage objects, removes all rbtree nodes,  and frees
1645  * all volatile objects belonging to the given row. For mapped columns, the
1646  * data is merely marked invalid and may be pruned from time to time by
1647  * para_fsck.
1648  *
1649  * \return Standard.
1650  */
1651 __export int osl_del_row(struct osl_table *t, struct osl_row *row)
1652 {
1653         struct osl_row *r = row;
1654         int i, ret;
1655         const struct osl_column_description *cd;
1656
1657         if (!t)
1658                 return -E_OSL_BAD_TABLE;
1659         INFO_LOG("deleting row %p\n", row);
1660
1661         if (t->num_disk_storage_columns) {
1662                 char *ds_name;
1663                 ret = disk_storage_name_of_row(t, r, &ds_name);
1664                 if (ret < 0)
1665                         goto out;
1666                 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd)
1667                         delete_disk_storage_file(t, i, ds_name);
1668                 free(ds_name);
1669         }
1670         FOR_EACH_COLUMN(i, t->desc, cd) {
1671                 struct osl_column *col = t->columns + i;
1672                 enum osl_storage_type st = cd->storage_type;
1673                 remove_rb_node(t, i, r);
1674                 if (st == OSL_MAPPED_STORAGE) {
1675                         mark_mapped_object_invalid(t, r->num, i);
1676                         continue;
1677                 }
1678                 if (st == OSL_NO_STORAGE && !(cd->storage_flags & OSL_DONT_FREE))
1679                         free(r->volatile_objects[col->volatile_num].data);
1680         }
1681         if (t->num_mapped_columns) {
1682                 ret = mark_row_invalid(t, r->num);
1683                 if (ret < 0)
1684                         goto out;
1685                 t->num_invalid_rows++;
1686         } else
1687                 t->num_rows--;
1688         ret = 1;
1689 out:
1690         free(r->volatile_objects);
1691         free(r);
1692         return ret;
1693 }
1694
1695 /* test if column has an rbtree */
1696 static int check_rbtree_col(const struct osl_table *t, unsigned col_num,
1697                 struct osl_column **col)
1698 {
1699         if (!t)
1700                 return -E_OSL_BAD_TABLE;
1701         if (!(get_column_description(t->desc, col_num)->storage_flags & OSL_RBTREE))
1702                 return -E_OSL_BAD_STORAGE_FLAGS;
1703         *col = t->columns + col_num;
1704         return 1;
1705 }
1706
1707 /**
1708  * Get the row that contains the given object.
1709  *
1710  * \param t Pointer to an open osl table.
1711  * \param col_num The number of the column to be searched.
1712  * \param obj The object to be looked up.
1713  * \param result Points to the row containing \a obj.
1714  *
1715  * Lookup \a obj in \a t and return the row containing \a obj. The column
1716  * specified by \a col_num must have an associated rbtree.
1717  *
1718  * \return Standard.
1719  *
1720  * \sa osl_storage_flags
1721  */
1722 __export int osl_get_row(const struct osl_table *t, unsigned col_num,
1723                 const struct osl_object *obj, struct osl_row **result)
1724 {
1725         int ret;
1726         struct rb_node *node;
1727         struct osl_row *row;
1728         struct osl_column *col;
1729
1730         *result = NULL;
1731         ret = check_rbtree_col(t, col_num, &col);
1732         if (ret < 0)
1733                 return ret;
1734         ret = search_rbtree(obj, t, col_num, &node, NULL);
1735         if (ret < 0)
1736                 return ret;
1737         row = get_row_pointer(node, t->columns[col_num].rbtree_num);
1738         *result = row;
1739         return 1;
1740 }
1741
1742 static int rbtree_loop(struct osl_column *col,  void *private_data,
1743                 osl_rbtree_loop_func *func)
1744 {
1745         struct rb_node *n, *tmp;
1746
1747         /* this for-loop is safe against removal of an entry */
1748         for (n = rb_first(&col->rbtree), tmp = n? rb_next(n) : NULL;
1749                         n;
1750                         n = tmp, tmp = tmp? rb_next(tmp) : NULL) {
1751                 struct osl_row *r = get_row_pointer(n, col->rbtree_num);
1752                 int ret = func(r, private_data);
1753                 if (ret < 0)
1754                         return ret;
1755         }
1756         return 1;
1757 }
1758
1759 static int rbtree_loop_reverse(struct osl_column *col,  void *private_data,
1760                 osl_rbtree_loop_func *func)
1761 {
1762         struct rb_node *n, *tmp;
1763
1764         /* safe against removal of an entry */
1765         for (n = rb_last(&col->rbtree), tmp = n? rb_prev(n) : NULL;
1766                         n;
1767                         n = tmp, tmp = tmp? rb_prev(tmp) : NULL) {
1768                 struct osl_row *r = get_row_pointer(n, col->rbtree_num);
1769                 int ret = func(r, private_data);
1770                 if (ret < 0)
1771                         return ret;
1772         }
1773         return 1;
1774 }
1775
1776 /**
1777  * Loop over all nodes in an rbtree.
1778  *
1779  * \param t Pointer to an open osl table.
1780  * \param col_num The column to use for iterating over the elements.
1781  * \param private_data Pointer that gets passed to \a func.
1782  * \param func The function to be called for each node in the rbtree.
1783  *
1784  * This function does an in-order walk of the rbtree associated with \a
1785  * col_num. It is an error if the \p OSL_RBTREE flag is not set for this
1786  * column. For each node in the rbtree, the given function \a func is called
1787  * with two pointers as arguments: The first osl_row* argument points to the
1788  * row that contains the object corresponding to the rbtree node currently
1789  * traversed, and the \a private_data pointer is passed verbatim to \a func as the
1790  * second argument. The loop terminates either if \a func returns a negative
1791  * value, or if all nodes of the tree have been visited.
1792  *
1793  *
1794  * \return Standard. If the termination of the loop was caused by \a func
1795  * returning a negative value, this value is returned.
1796  *
1797  * \sa osl_storage_flags, osl_rbtree_loop_reverse(), osl_compare_func.
1798  */
1799 __export int osl_rbtree_loop(const struct osl_table *t, unsigned col_num,
1800         void *private_data, osl_rbtree_loop_func *func)
1801 {
1802         struct osl_column *col;
1803
1804         int ret = check_rbtree_col(t, col_num, &col);
1805         if (ret < 0)
1806                 return ret;
1807         return rbtree_loop(col, private_data, func);
1808 }
1809
1810 /**
1811  * Loop over all nodes in an rbtree in reverse order.
1812  *
1813  * \param t Identical meaning as in \p osl_rbtree_loop().
1814  * \param col_num Identical meaning as in \p osl_rbtree_loop().
1815  * \param private_data Identical meaning as in \p osl_rbtree_loop().
1816  * \param func Identical meaning as in \p osl_rbtree_loop().
1817  *
1818  * This function is identical to \p osl_rbtree_loop(), the only difference
1819  * is that the tree is walked in reverse order.
1820  *
1821  * \return The same return value as \p osl_rbtree_loop().
1822  *
1823  * \sa osl_rbtree_loop().
1824  */
1825 __export int osl_rbtree_loop_reverse(const struct osl_table *t, unsigned col_num,
1826         void *private_data, osl_rbtree_loop_func *func)
1827 {
1828         struct osl_column *col;
1829
1830         int ret = check_rbtree_col(t, col_num, &col);
1831         if (ret < 0)
1832                 return ret;
1833         return rbtree_loop_reverse(col, private_data, func);
1834 }
1835
1836 /* TODO: Rollback changes on errors */
1837 static int rename_disk_storage_objects(struct osl_table *t,
1838                 struct osl_object *old_obj, struct osl_object *new_obj)
1839 {
1840         int i, ret;
1841         const struct osl_column_description *cd;
1842         char *old_ds_name, *new_ds_name;
1843
1844         if (!t->num_disk_storage_columns)
1845                 return 1; /* nothing to do */
1846         if (old_obj->size == new_obj->size && !memcmp(new_obj->data,
1847                         old_obj->data, new_obj->size))
1848                 return 1; /* object did not change */
1849         old_ds_name = disk_storage_name_of_object(t, old_obj);
1850         new_ds_name = disk_storage_name_of_object(t, new_obj);
1851         ret = -ERRNO_TO_ERROR(ENOMEM);
1852         if (!old_ds_name || ! new_ds_name)
1853                 goto out;
1854
1855         FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
1856                 char *old_filename, *new_filename;
1857                 ret = create_disk_storage_object_dir(t, i, new_ds_name);
1858                 if (ret < 0)
1859                         goto out;
1860                 old_filename = disk_storage_path(t, i, old_ds_name);
1861                 new_filename = disk_storage_path(t, i, new_ds_name);
1862                 if (!old_filename || !new_filename)
1863                         ret = -ERRNO_TO_ERROR(ENOMEM);
1864                 else
1865                         ret = para_rename(old_filename, new_filename);
1866                 free(old_filename);
1867                 free(new_filename);
1868                 if (ret < 0)
1869                         goto out;
1870         }
1871         ret = 1;
1872 out:
1873         free(old_ds_name);
1874         free(new_ds_name);
1875         return ret;
1876
1877 }
1878
1879 /**
1880  * Change an object in an osl table.
1881  *
1882  * \param t Pointer to an open osl table.
1883  * \param r Pointer to the row containing the object to be updated.
1884  * \param col_num Number of the column containing the object to be updated.
1885  * \param obj Pointer to the replacement object.
1886  *
1887  * This function  gets rid of all references to the old object. This includes
1888  * removal of the rbtree node in case there is an rbtree associated with \a
1889  * col_num. It then inserts \a obj into the table and the rbtree if necessary.
1890  *
1891  * If the \p OSL_RBTREE flag is set for \a col_num, you \b MUST call this
1892  * function in order to change the contents of an object, even for volatile or
1893  * mapped columns of constant size (which may be updated directly if \p
1894  * OSL_RBTREE is not set).  Otherwise the rbtree might become corrupted.
1895  *
1896  * \return Standard
1897  */
1898 __export int osl_update_object(struct osl_table *t, const struct osl_row *r,
1899                 unsigned col_num, struct osl_object *obj)
1900 {
1901         struct osl_column *col;
1902         const struct osl_column_description *cd;
1903         int ret;
1904
1905         if (!t)
1906                 return -E_OSL_BAD_TABLE;
1907         col = &t->columns[col_num];
1908         cd = get_column_description(t->desc, col_num);
1909         DEBUG_LOG("updating column %u of %s\n", col_num, t->desc->name);
1910         if (cd->storage_flags & OSL_RBTREE) {
1911                 if (search_rbtree(obj, t, col_num, NULL, NULL) > 0)
1912                         return -E_OSL_RB_KEY_EXISTS;
1913         }
1914         if (cd->storage_flags & OSL_FIXED_SIZE) {
1915                 if (obj->size != cd->data_size)
1916                         return -E_OSL_BAD_DATA_SIZE;
1917         }
1918         remove_rb_node(t, col_num, r);
1919         if (cd->storage_type == OSL_NO_STORAGE) { /* TODO: If fixed size, reuse object? */
1920                 free(r->volatile_objects[col->volatile_num].data);
1921                 r->volatile_objects[col->volatile_num] = *obj;
1922         } else if (cd->storage_type == OSL_DISK_STORAGE) {
1923                 char *ds_name;
1924                 ret = disk_storage_name_of_row(t, r, &ds_name);
1925                 if (ret < 0)
1926                         return ret;
1927                 ret = delete_disk_storage_file(t, col_num, ds_name);
1928                 if (ret < 0 && !is_errno(-ret, ENOENT)) {
1929                         free(ds_name);
1930                         return ret;
1931                 }
1932                 ret = write_disk_storage_file(t, col_num, obj, ds_name);
1933                 free(ds_name);
1934                 if (ret < 0)
1935                         return ret;
1936         } else { /* mapped storage */
1937                 struct osl_object old_obj;
1938                 ret = get_mapped_object(t, col_num, r->num, &old_obj);
1939                 if (ret < 0)
1940                         return ret;
1941                 /*
1942                  * If the updated column is the disk storage name column, the
1943                  * disk storage name changes, so we have to rename all disk
1944                  * storage objects accordingly.
1945                  */
1946                 if (col_num == t->disk_storage_name_column) {
1947                         ret = rename_disk_storage_objects(t, &old_obj, obj);
1948                         if (ret < 0)
1949                                 return ret;
1950                 }
1951                 if (cd->storage_flags & OSL_FIXED_SIZE)
1952                         memcpy(old_obj.data, obj->data, cd->data_size);
1953                 else { /* TODO: if the size doesn't change, use old space */
1954                         uint32_t new_data_map_size;
1955                         char *row_index;
1956                         ret = get_row_index(t, r->num, &row_index);
1957                         if (ret < 0)
1958                                 return ret;
1959                         ret = mark_mapped_object_invalid(t, r->num, col_num);
1960                         if (ret < 0)
1961                                 return ret;
1962                         unmap_column(t, col_num);
1963                         ret = append_map_file(t, col_num, obj,
1964                                 &new_data_map_size);
1965                         if (ret < 0)
1966                                 return ret;
1967                         ret = map_column(t, col_num);
1968                         if (ret < 0)
1969                                 return ret;
1970                         update_cell_index(row_index, col, new_data_map_size,
1971                                 obj->size);
1972                 }
1973         }
1974         if (cd->storage_flags & OSL_RBTREE) {
1975                 ret = insert_rbtree(t, col_num, r, obj);
1976                 if (ret < 0)
1977                         return ret;
1978         }
1979         return 1;
1980 }
1981
1982 /**
1983  * Retrieve an object of type \p OSL_DISK_STORAGE by row and column.
1984  *
1985  * \param t Pointer to an open osl table.
1986  * \param r Pointer to the row containing the object.
1987  * \param col_num The column number.
1988  * \param obj Points to the result upon successful return.
1989  *
1990  * For columns of type \p OSL_DISK_STORAGE, this function must be used to
1991  * retrieve one of its containing objects. Afterwards, osl_close_disk_object()
1992  * must be called in order to deallocate the resources.
1993  *
1994  * \return Standard.
1995  *
1996  * \sa osl_get_object(), osl_storage_type, osl_close_disk_object().
1997  */
1998 __export int osl_open_disk_object(const struct osl_table *t, const struct osl_row *r,
1999                 unsigned col_num, struct osl_object *obj)
2000 {
2001         const struct osl_column_description *cd;
2002         char *ds_name, *filename;
2003         int ret;
2004
2005         if (!t)
2006                 return -E_OSL_BAD_TABLE;
2007         cd = get_column_description(t->desc, col_num);
2008         if (cd->storage_type != OSL_DISK_STORAGE)
2009                 return -E_OSL_BAD_STORAGE_TYPE;
2010
2011         ret = disk_storage_name_of_row(t, r, &ds_name);
2012         if (ret < 0)
2013                 return ret;
2014         filename = disk_storage_path(t, col_num, ds_name);
2015         free(ds_name);
2016         if (!filename)
2017                 return -ERRNO_TO_ERROR(ENOMEM);
2018         DEBUG_LOG("filename: %s\n", filename);
2019         ret = mmap_full_file(filename, O_RDONLY, &obj->data, &obj->size, NULL);
2020         free(filename);
2021         return ret;
2022 }
2023
2024 /**
2025  * Free resources that were allocated during osl_open_disk_object().
2026  *
2027  * \param obj Pointer to the object previously returned by open_disk_object().
2028  *
2029  * \return The return value of the underlying call to para_munmap().
2030  *
2031  * \sa para_munmap().
2032  */
2033 __export int osl_close_disk_object(struct osl_object *obj)
2034 {
2035         return para_munmap(obj->data, obj->size);
2036 }
2037
2038 /**
2039  * Get the number of rows of the given table.
2040  *
2041  * \param t Pointer to an open osl table.
2042  * \param num_rows Result is returned here.
2043  *
2044  * The number of rows returned via \a num_rows excluding any invalid rows.
2045  *
2046  * \return Positive on success, \p -E_OSL_BAD_TABLE if \a t is \p NULL.
2047  */
2048 __export int osl_get_num_rows(const struct osl_table *t, unsigned *num_rows)
2049 {
2050         if (!t)
2051                 return -E_OSL_BAD_TABLE;
2052         assert(t->num_rows >= t->num_invalid_rows);
2053         *num_rows = t->num_rows - t->num_invalid_rows;
2054         return 1;
2055 }
2056
2057 /**
2058  * Get the rank of a row.
2059  *
2060  * \param t An open osl table.
2061  * \param r The row to get the rank of.
2062  * \param col_num The number of an rbtree column.
2063  * \param rank Result pointer.
2064  *
2065  * The rank is, by definition, the position of the row in the linear order
2066  * determined by an in-order tree walk of the rbtree associated with column
2067  * number \a col_num of \a table.
2068  *
2069  * \return Standard.
2070  *
2071  * \sa osl_get_nth_row().
2072  */
2073 __export int osl_get_rank(const struct osl_table *t, struct osl_row *r,
2074                 unsigned col_num, unsigned *rank)
2075 {
2076         struct osl_object obj;
2077         struct osl_column *col;
2078         struct rb_node *node;
2079         int ret = check_rbtree_col(t, col_num, &col);
2080
2081         if (ret < 0)
2082                 return ret;
2083         ret = osl_get_object(t, r, col_num, &obj);
2084         if (ret < 0)
2085                 return ret;
2086         ret = search_rbtree(&obj, t, col_num, &node, NULL);
2087         if (ret < 0)
2088                 return ret;
2089         ret = rb_rank(node, rank);
2090         if (ret < 0)
2091                 return -E_OSL_BAD_ROW;
2092         return 1;
2093 }
2094
2095 /**
2096  * Get the row with n-th greatest value.
2097  *
2098  * \param t Pointer to an open osl table.
2099  * \param col_num The column number.
2100  * \param n The rank of the desired row.
2101  * \param result Row is returned here.
2102  *
2103  * Retrieve the n-th order statistic with respect to the compare function
2104  * of the rbtree column \a col_num. In other words, get that row with
2105  * \a n th greatest value in column \a col_num. It's an error if
2106  * \a col_num is not a rbtree column, or if \a n is larger than the
2107  * number of rows in the table.
2108  *
2109  * \return Standard.
2110  *
2111  * \sa osl_storage_flags, osl_compare_func, osl_get_row(),
2112  * osl_rbtree_last_row(), osl_rbtree_first_row(), osl_get_rank().
2113  */
2114 __export int osl_get_nth_row(const struct osl_table *t, unsigned col_num,
2115                 unsigned n, struct osl_row **result)
2116 {
2117         struct osl_column *col;
2118         struct rb_node *node;
2119         unsigned num_rows;
2120         int ret;
2121
2122         if (n == 0)
2123                 return -E_OSL_RB_KEY_NOT_FOUND;
2124         ret = osl_get_num_rows(t, &num_rows);
2125         if (ret < 0)
2126                 return ret;
2127         if (n > num_rows)
2128                 return -E_OSL_RB_KEY_NOT_FOUND;
2129         ret = check_rbtree_col(t, col_num, &col);
2130         if (ret < 0)
2131                 return ret;
2132         node = rb_nth(col->rbtree.rb_node, n);
2133         if (!node)
2134                 return -E_OSL_RB_KEY_NOT_FOUND;
2135         *result = get_row_pointer(node, col->rbtree_num);
2136         return 1;
2137 }
2138
2139 /**
2140  * Get the row corresponding to the smallest rbtree node of a column.
2141  *
2142  * \param t An open rbtree table.
2143  * \param col_num The number of the rbtree column.
2144  * \param result A pointer to the first row is returned here.
2145  *
2146  * The rbtree node of the smallest object (with respect to the corresponding
2147  * compare function) is selected and the row containing this object is
2148  * returned. It is an error if \a col_num refers to a column without an
2149  * associated rbtree.
2150  *
2151  * \return Standard.
2152  *
2153  * \sa osl_get_nth_row(), osl_rbtree_last_row().
2154  */
2155 __export int osl_rbtree_first_row(const struct osl_table *t, unsigned col_num,
2156                 struct osl_row **result)
2157 {
2158         return osl_get_nth_row(t, col_num, 1, result);
2159 }
2160
2161 /**
2162  * Get the row corresponding to the greatest rbtree node of a column.
2163  *
2164  * \param t The same meaning as in \p osl_rbtree_first_row().
2165  * \param col_num The same meaning as in \p osl_rbtree_first_row().
2166  * \param result The same meaning as in \p osl_rbtree_first_row().
2167  *
2168  * This function works just like osl_rbtree_first_row(), the only difference
2169  * is that the row containing the greatest rather than the smallest object is
2170  * returned.
2171  *
2172  * \return Standard.
2173  *
2174  * \sa osl_get_nth_row(), osl_rbtree_first_row().
2175  */
2176 __export int osl_rbtree_last_row(const struct osl_table *t, unsigned col_num,
2177                 struct osl_row **result)
2178 {
2179         unsigned num_rows;
2180         int ret = osl_get_num_rows(t, &num_rows);
2181
2182         if (ret < 0)
2183                 return ret;
2184         return osl_get_nth_row(t, col_num, num_rows, result);
2185 }