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