5 #define OSL_DUMP_DIR "/tmp/osldump"
15 INIT_STDERR_LOGGING(1);
18 signed char hexval_table[256] = {
19 -1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */
20 -1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */
21 -1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */
22 -1, -1, -1, -1, -1, -1, -1, -1, /* 18-1f */
23 -1, -1, -1, -1, -1, -1, -1, -1, /* 20-27 */
24 -1, -1, -1, -1, -1, -1, -1, -1, /* 28-2f */
25 0, 1, 2, 3, 4, 5, 6, 7, /* 30-37 */
26 8, 9, -1, -1, -1, -1, -1, -1, /* 38-3f */
27 -1, 10, 11, 12, 13, 14, 15, -1, /* 40-47 */
28 -1, -1, -1, -1, -1, -1, -1, -1, /* 48-4f */
29 -1, -1, -1, -1, -1, -1, -1, -1, /* 50-57 */
30 -1, -1, -1, -1, -1, -1, -1, -1, /* 58-5f */
31 -1, 10, 11, 12, 13, 14, 15, -1, /* 60-67 */
32 -1, -1, -1, -1, -1, -1, -1, -1, /* 68-67 */
33 -1, -1, -1, -1, -1, -1, -1, -1, /* 70-77 */
34 -1, -1, -1, -1, -1, -1, -1, -1, /* 78-7f */
35 -1, -1, -1, -1, -1, -1, -1, -1, /* 80-87 */
36 -1, -1, -1, -1, -1, -1, -1, -1, /* 88-8f */
37 -1, -1, -1, -1, -1, -1, -1, -1, /* 90-97 */
38 -1, -1, -1, -1, -1, -1, -1, -1, /* 98-9f */
39 -1, -1, -1, -1, -1, -1, -1, -1, /* a0-a7 */
40 -1, -1, -1, -1, -1, -1, -1, -1, /* a8-af */
41 -1, -1, -1, -1, -1, -1, -1, -1, /* b0-b7 */
42 -1, -1, -1, -1, -1, -1, -1, -1, /* b8-bf */
43 -1, -1, -1, -1, -1, -1, -1, -1, /* c0-c7 */
44 -1, -1, -1, -1, -1, -1, -1, -1, /* c8-cf */
45 -1, -1, -1, -1, -1, -1, -1, -1, /* d0-d7 */
46 -1, -1, -1, -1, -1, -1, -1, -1, /* d8-df */
47 -1, -1, -1, -1, -1, -1, -1, -1, /* e0-e7 */
48 -1, -1, -1, -1, -1, -1, -1, -1, /* e8-ef */
49 -1, -1, -1, -1, -1, -1, -1, -1, /* f0-f7 */
50 -1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */
53 int asc_to_hash(const char *asc_hash, int len, HASH_TYPE *hash)
56 const unsigned char *asc = (const unsigned char *) asc_hash;
58 while (*asc && i++ < len) {
59 unsigned int val = (hexval_table[asc[0]] << 4) | hexval_table[asc[1]];
70 * check for object boundary violations
72 * test whether the range pointed to by the index entry for a given cell is
73 * contained in mapped data file. This should always be the case. Otherwise
74 * we are in real trouble.
76 static int check_range(struct osl_table *t, uint32_t row_num, uint32_t col_num)
79 struct osl_object obj;
80 struct osl_column *col;
82 char *map_start, *obj_start;
84 ret = get_cell_index(t, row_num, col_num, &index_entry);
87 ret = get_mapped_object(t, col_num, row_num, &obj);
90 col = t->columns + col_num;
92 map_start = col->data_map.data;
93 // PARA_INFO_LOG("obj: %p..%p\n", obj_start, obj_start + obj.size);
94 // PARA_INFO_LOG("map: %p..%p\n", map_start, map_start + col->data_map.size);
95 if (obj_start < map_start || obj_start + obj.size > map_start + col->data_map.size) {
96 PARA_CRIT_LOG("row %u, col %u: range violation, very bad\n", row_num, col_num);
97 return -E_RANGE_VIOLATION;
99 PARA_DEBUG_LOG("col %u: ok\n", col_num);
104 * check all cells of the given table for boundary violations
106 static int check_index_ranges(struct osl_table *t)
110 PARA_NOTICE_LOG("checking for range violations in index\n");
111 //PARA_DEBUG_LOG("%d rows. %d columns\n", t->num_rows, t->desc->num_columns);
112 t->num_invalid_rows = 0;
113 for (i = 0; i < t->num_rows; i++) {
114 if (row_is_invalid(t, i)) {
115 t->num_invalid_rows++;
118 for (j = 0; j < t->desc->num_columns; j++) { /* FXIME */
119 const struct osl_column_description *cd =
120 get_column_description(t->desc, j);
121 if (cd->storage_type != OSL_MAPPED_STORAGE)
123 ret = check_range(t, i, j);
125 if (ret != -E_INVALID_OBJECT &&
126 ret != -E_RANGE_VIOLATION)
128 if (ret == -E_INVALID_OBJECT) {
129 PARA_CRIT_LOG("row %d, col %d maps to an "
130 "invalid object\n", i, j);
132 ret = mark_row_invalid(t, i);
135 t->num_invalid_rows++;
141 if (t->num_invalid_rows)
142 PARA_NOTICE_LOG("ranges OK. %d invalid row(s) detected\n",
143 t->num_invalid_rows);
145 PARA_INFO_LOG("no invalid rows, no range violations, good\n");
151 static int move_index_entry(struct osl_table *t, uint32_t dest, uint32_t src)
153 char *dest_ie, *src_ie;
154 int ret = get_row_index(t, dest, &dest_ie);
158 ret = get_row_index(t, src, &src_ie);
161 PARA_INFO_LOG("moving entry #%u to position %u\n", src, dest);
162 memcpy(dest_ie, src_ie, t->row_index_size);
166 static int map_index(const struct osl_table_description *desc, struct osl_object *map)
168 char *filename = index_filename(desc);
171 ret = mmap_full_file(filename, O_RDWR, map);
172 PARA_INFO_LOG("mapping index %s: ret: %d, size: %zu\n", filename, ret, map->size);
177 static int prune_invalid_rows_from_index(struct osl_table *t)
179 uint32_t top = 0, bottom;
183 if (!t->num_invalid_rows) {
184 PARA_INFO_LOG("all rows are valid, good\n");
187 PARA_NOTICE_LOG("deleting %u invalid row(s) (%d bytes) from index\n",
188 t->num_invalid_rows, t->row_index_size * t->num_invalid_rows);
189 bottom = t->num_rows - 1;
190 while (top < bottom) {
191 if (!row_is_invalid(t, top)) {
195 while (bottom > top) {
196 if (row_is_invalid(t, bottom)) {
200 /* move bottom index entry to top */
201 move_index_entry(t, top, bottom);
207 PARA_INFO_LOG("unmapping index\n");
208 para_munmap(t->index_map.data, t->index_map.size);
209 filename = index_filename(t->desc);
210 ret = para_truncate(filename, t->row_index_size
211 * t->num_invalid_rows);
215 ret = map_index(t->desc, &t->index_map);
218 t->num_rows = table_num_rows(t);
222 static int check_for_invalid_objects(struct osl_table *t, uint32_t **lost_bytes)
225 const struct osl_column_description *cd;
226 uint32_t *loss = para_malloc(sizeof(uint32_t) * t->desc->num_columns);
228 PARA_NOTICE_LOG("looking for mapped objects not contained in index\n");
229 /* first count used bytes */
230 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
231 loss[i] = t->columns[i].data_map.size;
232 for (j = 0; j < t->num_rows; j++) {
233 struct osl_object obj;
234 ret = get_mapped_object(t, i, j, &obj);
236 loss[i] -= obj.size + 1; /* add one for header byte */
239 if (ret != -E_INVALID_OBJECT)
241 PARA_CRIT_LOG("row %d, col %d points to an invalid "
242 "mapped object, bad\n", j, i);
246 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
248 PARA_NOTICE_LOG("column %u contains %u lost bytes\n",
254 PARA_INFO_LOG("all mapped objects are valid, good\n");
262 /* prune_invalid_rows() must be run on the table before calling this */
263 static int prune_mapped_column(struct osl_table *t, uint32_t col_num, int fd)
266 uint32_t written = 0;
267 struct osl_column *col = t->columns + col_num;
269 PARA_INFO_LOG("pruning col %u\n", col_num);
270 for (i = 0; i < t->num_rows; i++) {
271 struct osl_object obj;
274 PARA_DEBUG_LOG("checking row %u/%u\n", i, t->num_rows);
275 ret = get_mapped_object(t, col_num, i, &obj);
278 ret = para_write_all(fd, (char *)(obj.data) - 1, obj.size + 1);
281 written += obj.size + 1;
282 ret = get_row_index(t, i, &index_entry);
285 update_cell_index(index_entry, col, written, obj.size);
290 static int prune_objects(struct osl_table *t, uint32_t *lost_bytes)
293 const struct osl_column_description *cd;
294 char **col_filenames = para_calloc(t->desc->num_columns * sizeof(char *));
295 char **new_col_filenames = para_calloc(t->desc->num_columns * sizeof(char *));
296 char *idx_filename = index_filename(t->desc);
297 char *old_idx_filename = make_message("%s.bak", idx_filename);
300 PARA_NOTICE_LOG("removing unreferenced objects from data files\n");
301 /* first make a copy of the index */
302 ret = para_open(old_idx_filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
306 ret = para_write_all(fd, t->index_map.data, t->index_map.size);
310 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
313 col_filenames[i] = column_filename(t, i);
314 new_col_filenames[i] = make_message("%s.fsck", col_filenames[i]);
315 ret = para_open(new_col_filenames[i], O_WRONLY | O_CREAT | O_EXCL, 0644);
317 goto out_unlink_data;
319 ret = prune_mapped_column(t, i, fd);
322 goto out_unlink_data;
324 ret = unmap_table(t, OSL_MARK_CLEAN);
326 goto out_unlink_data;
327 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
330 ret = para_rename(new_col_filenames[i], col_filenames[i]);
331 if (ret < 0) { /* we're kinda screwed here */
332 PARA_CRIT_LOG("rename of col %i failed: %s\n", i,
337 unlink(old_idx_filename);
338 ret = map_table(t, 0);
341 FOR_EACH_MAPPED_COLUMN(i, t, cd)
342 unlink(new_col_filenames[i]);
344 free(old_idx_filename);
346 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
347 free(col_filenames[i]);
348 free(new_col_filenames[i]);
351 free(new_col_filenames);
355 static struct osl_column_description hash_tree_table_cols[] = {
357 .storage_type = OSL_NO_STORAGE,
358 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
360 .compare_function = uint32_compare,
361 .data_size = HASH_SIZE
365 static const struct osl_table_description hash_tree_table_desc = {
366 .dir = "/", /* irrelevant */
370 .column_descriptions = hash_tree_table_cols
374 * The hash_tree table contains all hashes of the disk storage name column.
375 * of each row. It is used for checking if a disk storage file has a reference
378 static struct osl_table *hash_tree_table;
379 static HASH_TYPE *hashes;
381 static int check_disk_storage_column(struct osl_table *t, int row_num,
382 int col_num, char *ds_name, unsigned *num_missing_objects)
386 char *path = disk_storage_path(t, col_num, ds_name);
387 unsigned dsnc = t->disk_storage_name_column;
388 struct osl_object obj;
390 PARA_DEBUG_LOG("checking if %s is a regular file\n", path);
391 ret = stat(path, &statbuf);
392 if (ret < 0 && errno == ENOENT) {
394 (*num_missing_objects)++;
395 PARA_ERROR_LOG("row %d: object %s is missing\n", row_num, path);
396 PARA_NOTICE_LOG("trying to delete row %d\n", row_num);
397 ret = osl_get_row(t, dsnc, &obj, &row);
399 PARA_CRIT_LOG("unable to get row %d\n", row_num);
400 mark_row_invalid(t, row_num);
401 PARA_CRIT_LOG("Please re-run fsck\n");
404 ret = osl_del_row(t, row);
412 ret = -E_NOT_A_REGULAR_FILE;
413 if (!(S_IFREG & statbuf.st_mode))
418 static int check_disk_storage_presence(struct osl_table *t)
421 struct osl_object obj, hash_obj = {.size = HASH_SIZE};
423 const struct osl_column_description *cd;
424 unsigned dsnc = t->disk_storage_name_column, missing_objects = 0;
428 hashes = para_malloc(t->num_rows * HASH_SIZE);
429 PARA_NOTICE_LOG("looking for missing disk storage objects\n");
430 for (i = 0; i < t->num_rows; i++) {
431 if (row_is_invalid(t, i))
433 ret = get_mapped_object(t, dsnc, i, &obj);
436 hash_object(&obj, hashes + i * HASH_SIZE);
437 hash_obj.data = hashes + i * HASH_SIZE;
438 osl_add_row(hash_tree_table, &hash_obj);
439 ds_name = disk_storage_name_of_hash(t, hashes + i * HASH_SIZE);
440 FOR_EACH_DISK_STORAGE_COLUMN(j, t, cd) {
441 ret = check_disk_storage_column(t, i, j, ds_name,
448 if (!missing_objects)
449 PARA_INFO_LOG("all referenced disk storage objects exist, good\n");
451 PARA_NOTICE_LOG("%d missing object(s)\n", missing_objects);
452 return missing_objects;
458 static int dummy_compare(const struct osl_object *obj1, const struct osl_object *obj2)
467 static unsigned files_pruned;
469 int prune_disk_storage_file(const char *path, const void *private_data)
471 HASH_TYPE hash[HASH_SIZE];
472 unsigned flags = *(unsigned *)private_data;
473 struct osl_object obj = {.data = hash, .size = HASH_SIZE};
476 size_t len = strlen(path);
479 PARA_DEBUG_LOG("path: %s\n", path);
480 if (flags & OSL_LARGE_TABLE) {
481 if (len < HASH_SIZE * 2 + 2)
483 // PARA_NOTICE_LOG("p: %s\n", path + len - 2 * HASH_SIZE - 1);
484 ret = asc_to_hash(path + len - 2 * HASH_SIZE - 1, 1, hash);
487 ret = asc_to_hash(path + len - 2 * HASH_SIZE + 2, HASH_SIZE - 1,
491 // PARA_INFO_LOG("high: %x, low: %x, hash: %x\n", high, low, hash);
493 if (len < 2 * HASH_SIZE + 1)
495 ret = asc_to_hash(path + len - 2 * HASH_SIZE, 2 * HASH_SIZE, hash);
498 // PARA_INFO_LOG("hash: %x\n", hash);
502 char asc[2 * HASH_SIZE + 1];
503 hash_to_asc(hash, asc);
504 PARA_NOTICE_LOG("before: %s\nafter: %s\n", path, asc);
507 ret = osl_get_row(hash_tree_table, 0, &obj, &row);
510 PARA_NOTICE_LOG("unreferenced file in hash dir: %s\n", path);
513 PARA_ERROR_LOG("could not read hash value of %s\n", path);
515 PARA_NOTICE_LOG("removing %s\n", path);
521 static int prune_disk_storage_files(struct osl_table *t)
524 const struct osl_column_description *cd;
526 PARA_NOTICE_LOG("looking for unreferenced disk storage files\n");
527 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
528 char *dirname = column_filename(t, i);
529 ret = for_each_file_in_dir(dirname, prune_disk_storage_file, &t->desc->flags);
533 PARA_NOTICE_LOG("%u disk storage files deleted\n",
536 PARA_INFO_LOG("all files are are referenced, good\n");
540 static int check_disk_storage_columns(struct osl_table *t)
543 const struct osl_column_description *cd;
545 if (!t->num_disk_storage_columns) {
546 PARA_NOTICE_LOG("no disk storage columns in table '%s', "
547 "skipping checks\n", t->desc->name);
550 FOR_EACH_COLUMN(i, t->desc, cd)
551 t->desc->column_descriptions[i].compare_function = dummy_compare;
552 ret = init_rbtrees(t);
555 PARA_NOTICE_LOG("creating rbtree for disk storage hash values\n");
556 ret = osl_open_table(&hash_tree_table_desc, &hash_tree_table);
559 ret = check_disk_storage_presence(t);
561 goto out_close_hash_tree;
562 ret = prune_disk_storage_files(t);
564 osl_close_table(hash_tree_table, 0);
567 clear_rbtrees(t); /* TODO why are we doing that here? Seems odd */
575 static void set_dummy_contents(struct osl_table_description *desc)
578 struct osl_column_description *cd;
580 for (i = 0; i < desc->num_columns; i++) {
581 cd = get_column_description(desc, i);
582 cd->compare_function = dummy_compare;
586 static int fsck_init(struct osl_table_description *desc, struct osl_table **t)
588 struct osl_object map;
589 int ret = map_index(desc, &map);
593 ret = read_table_desc(&map, desc);
595 para_munmap(map.data, map.size);
598 set_dummy_contents(desc);
599 ret = init_table_structure(desc, t);
601 para_munmap(map.data, map.size);
604 PARA_INFO_LOG("unmapping index\n");
605 para_munmap(map.data, map.size);
607 ret = map_table(*t, (MAP_TBL_FL_IGNORE_DIRTY));
609 ret = map_table(*t, 0);
611 (*t)->num_rows = table_num_rows(*t);
616 static void fsck_cleanup(struct osl_table *t)
619 if (t->desc->column_descriptions) {
620 struct osl_column_description *cd;
621 for (i = 0; i < t->desc->num_columns; i++) {
622 cd = get_column_description(t->desc, i);
623 free((char*)cd->name);
625 free(t->desc->column_descriptions);
634 #define ST_CASE(st) case st: return #st
636 const char *get_asc_storage_type(enum osl_storage_type st)
639 ST_CASE(OSL_MAPPED_STORAGE);
640 ST_CASE(OSL_DISK_STORAGE);
641 ST_CASE(OSL_NO_STORAGE);
646 #define APPEND_ASC_SF(sf, flag, str) do { if (sf & flag) { \
647 if (str) str = para_strcat(str, " | " # flag); \
648 else str = para_strdup(#flag); }} while (0)
651 char *get_asc_storage_flags(enum osl_storage_type sf)
655 APPEND_ASC_SF(sf, OSL_RBTREE, asc_sf);
656 APPEND_ASC_SF(sf, OSL_FIXED_SIZE, asc_sf);
657 APPEND_ASC_SF(sf, OSL_UNIQUE, asc_sf);
661 static int dump_table_desc(struct osl_table *t, int fd)
663 const struct osl_table_description *desc = t->desc;
665 struct osl_column_description *cd;
666 char *msg = make_message("static struct osl_column_description cols[] = {\n");
667 ret = para_write_all(fd, msg, strlen(msg));
671 FOR_EACH_COLUMN(i, desc, cd) {
673 msg = make_message("\t[%d] = {\n", i);
674 ret = para_write_all(fd, msg, strlen(msg));
678 asc_st = get_asc_storage_type(cd->storage_type);
679 msg = make_message("\t\t.storage_type = %s,\n", asc_st);
680 ret = para_write_all(fd, msg, strlen(msg));
684 if (cd->storage_flags) {
685 char *asc_sf = get_asc_storage_flags(cd->storage_flags);
686 msg = make_message("\t\t,storage_flags = %s,\n", asc_sf);
688 ret = para_write_all(fd, msg, strlen(msg));
693 if (cd->storage_flags & OSL_FIXED_SIZE) {
694 msg = make_message("\t\t.data_size = %u,\n", cd->data_size);
695 ret = para_write_all(fd, msg, strlen(msg));
700 msg = make_message("\t\t.name = \"%s\",\n", cd->name);
701 ret = para_write_all(fd, msg, strlen(msg));
705 if (cd->storage_flags & OSL_RBTREE) {
706 msg = make_message("\t\t.compare_function = compare_func,\n");
707 ret = para_write_all(fd, msg, strlen(msg));
712 msg = make_message("\t},\n");
713 ret = para_write_all(fd, msg, strlen(msg));
718 msg = make_message("};\n");
719 ret = para_write_all(fd, msg, strlen(msg));
726 static int dump_row(struct osl_table *t, unsigned row_num, const char *row_dir)
729 const struct osl_column_description *cd;
731 struct osl_object obj;
733 HASH_TYPE hash[HASH_SIZE];
736 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
737 ret = get_mapped_object(t, i, row_num, &obj);
740 filename = make_message("%s/col_%03u", row_dir, i);
741 ret = para_write_file(filename, obj.data, obj.size);
746 if (!t->num_disk_storage_columns)
748 dsnc = t->disk_storage_name_column;
749 ret = get_mapped_object(t, dsnc, i, &obj);
752 hash_object(&obj, hash);
753 ds_name = disk_storage_name_of_hash(t, hash);
754 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
755 filename = disk_storage_path(t, i, ds_name);
756 ret = mmap_full_file(filename, O_RDONLY, &obj);
760 filename = make_message("%s/col_%03u", row_dir, i);
761 ret = para_write_file(filename, obj.data, obj.size);
772 static int dump_rows(char *dump_dir, struct osl_table *t)
775 char *current_dir = NULL;
778 for (i = 0; i < t->num_rows; i++) {
780 if (row_is_invalid(t, i))
784 current_dir = make_message("%s/rows_%u-%u", dump_dir, i, i + 999);
785 PARA_NOTICE_LOG("dumping rows %u - %u\n", i, i + 999);
786 ret = para_mkdir(current_dir, 0777);
790 row_dir = make_message("%s/row_%03u", current_dir, i);
791 ret = para_mkdir(row_dir, 0777);
796 ret = dump_row(t, i, row_dir);
806 static int dump_table(char *dump_dir, struct osl_table_description *desc)
808 struct osl_table *t = NULL;
809 int fd, ret = fsck_init(desc, &t);
814 ret = para_mkdir(dump_dir, 0777);
817 desc_file = make_message("%s/table_description.c", dump_dir);
818 ret = para_open(desc_file, O_WRONLY | O_CREAT | O_EXCL, 0644);
823 ret = dump_table_desc(t, fd);
827 ret = dump_rows(dump_dir, t);
833 static int fsck(struct osl_table_description *desc)
836 struct osl_table *t = NULL;
837 uint32_t *lost_bytes = NULL;
839 ret = fsck_init(desc, &t);
842 ret = check_index_ranges(t);
845 ret = check_disk_storage_columns(t);
848 ret = prune_invalid_rows_from_index(t);
851 ret = check_for_invalid_objects(t, &lost_bytes);
854 if (ret > 0) { /* at least one mapped data file needs pruning */
855 ret = prune_objects(t, lost_bytes);
860 PARA_INFO_LOG("success\n");
862 unmap_table(t, OSL_MARK_CLEAN);
867 int main(__a_unused int argc, __a_unused char **argv)
870 struct osl_table_description desc = {.column_descriptions = NULL};
872 ret = -E_FSCK_SYNTAX;
883 ret = dump_table(OSL_DUMP_DIR, &desc);
886 PARA_ERROR_LOG("error %d\n", ret);
887 return ret < 0? EXIT_FAILURE: EXIT_SUCCESS;