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