2 * Copyright (C) 2007-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file fsck.c The program used to check an osl table. */
10 #include <sys/types.h>
20 #include "fsck.cmdline.h"
22 /** version text used by various commands if -V switch was given */
23 #define VERSION_TEXT(prefix) "osl_" prefix " " VERSION " " "\n" \
24 "Copyright (C) 2008 Andre Noll\n" \
25 "This is free software with ABSOLUTELY NO WARRANTY." \
26 " See COPYING for details.\n" \
27 "Written by Andre Noll.\n" \
28 "Report bugs to <maan@systemlinux.org>.\n"
30 /** print out \p VERSION_TEXT and exit if version flag was given */
31 #define HANDLE_VERSION_FLAG(_prefix, _args_info_struct) \
32 if (_args_info_struct.version_given) { \
33 printf("%s", VERSION_TEXT(_prefix)); \
37 static struct fsck_args_info conf;
40 FSCK_ERROR(RANGE_VIOLATION, "range violation detected, very bad"), \
41 FSCK_ERROR(NOT_A_REGULAR_FILE, "not a regular file"), \
42 FSCK_ERROR(SYNTAX, "fsck syntax error"),
44 #define FSCK_ERROR(num, txt) E_FSCK_ ## num
46 FSCK_DUMMY = (1 << FSCK_ERROR_BIT) - 1,
50 #define FSCK_ERROR(num, txt) txt
51 const char const *fsck_errors[] = {
55 static const char *fsck_strerror(int num)
57 if (num & (1 << FSCK_ERROR_BIT))
58 return fsck_errors[num & ((1 << FSCK_ERROR_BIT) - 1)];
59 return osl_strerror(num);
65 signed char hexval_table[256] = {
66 -1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */
67 -1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */
68 -1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */
69 -1, -1, -1, -1, -1, -1, -1, -1, /* 18-1f */
70 -1, -1, -1, -1, -1, -1, -1, -1, /* 20-27 */
71 -1, -1, -1, -1, -1, -1, -1, -1, /* 28-2f */
72 0, 1, 2, 3, 4, 5, 6, 7, /* 30-37 */
73 8, 9, -1, -1, -1, -1, -1, -1, /* 38-3f */
74 -1, 10, 11, 12, 13, 14, 15, -1, /* 40-47 */
75 -1, -1, -1, -1, -1, -1, -1, -1, /* 48-4f */
76 -1, -1, -1, -1, -1, -1, -1, -1, /* 50-57 */
77 -1, -1, -1, -1, -1, -1, -1, -1, /* 58-5f */
78 -1, 10, 11, 12, 13, 14, 15, -1, /* 60-67 */
79 -1, -1, -1, -1, -1, -1, -1, -1, /* 68-67 */
80 -1, -1, -1, -1, -1, -1, -1, -1, /* 70-77 */
81 -1, -1, -1, -1, -1, -1, -1, -1, /* 78-7f */
82 -1, -1, -1, -1, -1, -1, -1, -1, /* 80-87 */
83 -1, -1, -1, -1, -1, -1, -1, -1, /* 88-8f */
84 -1, -1, -1, -1, -1, -1, -1, -1, /* 90-97 */
85 -1, -1, -1, -1, -1, -1, -1, -1, /* 98-9f */
86 -1, -1, -1, -1, -1, -1, -1, -1, /* a0-a7 */
87 -1, -1, -1, -1, -1, -1, -1, -1, /* a8-af */
88 -1, -1, -1, -1, -1, -1, -1, -1, /* b0-b7 */
89 -1, -1, -1, -1, -1, -1, -1, -1, /* b8-bf */
90 -1, -1, -1, -1, -1, -1, -1, -1, /* c0-c7 */
91 -1, -1, -1, -1, -1, -1, -1, -1, /* c8-cf */
92 -1, -1, -1, -1, -1, -1, -1, -1, /* d0-d7 */
93 -1, -1, -1, -1, -1, -1, -1, -1, /* d8-df */
94 -1, -1, -1, -1, -1, -1, -1, -1, /* e0-e7 */
95 -1, -1, -1, -1, -1, -1, -1, -1, /* e8-ef */
96 -1, -1, -1, -1, -1, -1, -1, -1, /* f0-f7 */
97 -1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */
100 int asc_to_hash(const char *asc_hash, int len, HASH_TYPE *hash)
103 const unsigned char *asc = (const unsigned char *) asc_hash;
105 while (*asc && i++ < len) {
106 unsigned int val = (hexval_table[asc[0]] << 4) | hexval_table[asc[1]];
116 static int _write_all(int fd, const char *buf, size_t len)
118 return write_all(fd, buf, &len);
122 * Paraslash's version of malloc().
124 * \param size The desired new size.
126 * A wrapper for malloc(3) which exits on errors.
128 * \return A pointer to the allocated memory, which is suitably aligned for any
133 __must_check __malloc static void *fsck_malloc(size_t size)
136 void *p = malloc(size);
139 EMERG_LOG("malloc failed (size = %zu), aborting\n",
147 * Paraslash's version of calloc().
149 * \param size The desired new size.
151 * A wrapper for calloc(3) which exits on errors.
153 * \return A pointer to the allocated and zeroed-out memory, which is suitably
154 * aligned for any kind of variable.
158 __must_check __malloc static void *fsck_calloc(size_t size)
160 void *ret = fsck_malloc(size);
162 memset(ret, 0, size);
167 * Paraslash's version of strdup().
169 * \param s The string to be duplicated.
171 * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
172 * there is no need to check the return value in the caller.
174 * \return A pointer to the duplicated string. If \p s was the NULL pointer,
175 * an pointer to an empty string is returned.
179 __must_check __malloc static char *fsck_strdup(const char *s)
183 if ((ret = strdup(s? s: "")))
185 EMERG_LOG("strdup failed, aborting\n");
190 * Compare two osl objects pointing to unsigned integers of 32 bit size.
192 * \param obj1 Pointer to the first integer.
193 * \param obj2 Pointer to the second integer.
195 * \return The values required for an osl compare function.
197 * \sa osl_compare_func, osl_hash_compare().
199 static int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2)
201 uint32_t d1 = read_u32((const char *)obj1->data);
202 uint32_t d2 = read_u32((const char *)obj2->data);
212 * A wrapper for fchdir().
214 * \param fd An open file descriptor.
218 static inline int __fchdir(int fd)
221 return -ERRNO_TO_ERROR(errno);
226 * Wrapper for chdir(2).
228 * \param path The specified directory.
232 _static_inline_ int __chdir(const char *path)
234 if (chdir(path) >= 0)
236 return -ERRNO_TO_ERROR(errno);
240 * Save the cwd and open a given directory.
242 * \param dirname Path to the directory to open.
243 * \param dir Result pointer.
244 * \param cwd File descriptor of the current working directory.
248 * Opening the current directory (".") and calling fchdir() to return is
249 * usually faster and more reliable than saving cwd in some buffer and calling
250 * chdir() afterwards.
252 * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
253 * stored in \a cwd. If the function returns success, and \a cwd is not \p
254 * NULL, the caller must close this file descriptor (probably after calling
257 * On errors, the function undos everything, so the caller needs neither close
258 * any files, nor change back to the original working directory.
263 static int fsck_opendir(const char *dirname, DIR **dir, int *cwd)
268 ret = osl_open(".", O_RDONLY, 0);
273 ret = __chdir(dirname);
279 ret = -ERRNO_TO_ERROR(errno);
280 /* Ignore return value of fchdir() and close(). We're busted anyway. */
292 * Traverse the given directory recursively.
294 * \param dirname The directory to traverse.
295 * \param func The function to call for each entry.
296 * \param private_data Pointer to an arbitrary data structure.
298 * For each regular file under \a dirname, the supplied function \a func is
299 * called. The full path of the regular file and the \a private_data pointer
300 * are passed to \a func. Directories for which the calling process has no
301 * permissions to change to are silently ignored.
305 static int for_each_file_in_dir(const char *dirname,
306 int (*func)(const char *, void *), void *private_data)
309 struct dirent *entry;
310 int cwd_fd, ret2, ret = fsck_opendir(dirname, &dir, &cwd_fd);
313 return ret == -ERRNO_TO_ERROR(EACCES)? 1 : ret;
314 /* scan cwd recursively */
315 while ((entry = readdir(dir))) {
320 if (!strcmp(entry->d_name, "."))
322 if (!strcmp(entry->d_name, ".."))
324 if (lstat(entry->d_name, &s) == -1)
327 if (!S_ISREG(m) && !S_ISDIR(m))
329 tmp = make_message("%s/%s", dirname, entry->d_name);
331 ret = func(tmp, private_data);
338 ret = for_each_file_in_dir(tmp, func, private_data);
346 ret2 = __fchdir(cwd_fd);
347 if (ret2 < 0 && ret >= 0)
354 * check for object boundary violations
356 * test whether the range pointed to by the index entry for a given cell is
357 * contained in mapped data file. This should always be the case. Otherwise
358 * we are in real trouble.
360 static int check_range(struct osl_table *t, uint32_t row_num, uint32_t col_num)
363 struct osl_object obj;
364 struct osl_column *col;
366 char *map_start, *obj_start;
368 ret = get_cell_index(t, row_num, col_num, &index_entry);
371 ret = get_mapped_object(t, col_num, row_num, &obj);
374 col = t->columns + col_num;
375 obj_start = obj.data;
376 map_start = col->data_map.data;
377 // INFO_LOG("obj: %p..%p\n", obj_start, obj_start + obj.size);
378 // INFO_LOG("map: %p..%p\n", map_start, map_start + col->data_map.size);
379 if (obj_start < map_start || obj_start + obj.size > map_start + col->data_map.size) {
380 CRIT_LOG("range violation in row %u, col %u\n", row_num,
382 return -E_FSCK_RANGE_VIOLATION;
384 DEBUG_LOG("col %u: ok\n", col_num);
389 * check all cells of the given table for boundary violations
391 static int check_index_ranges(struct osl_table *t)
395 INFO_LOG("checking for range violations in index\n");
396 //DEBUG_LOG("%d rows. %d columns\n", t->num_rows, t->desc->num_columns);
397 t->num_invalid_rows = 0;
398 for (i = 0; i < t->num_rows; i++) {
399 if (row_is_invalid(t, i)) {
400 t->num_invalid_rows++;
403 for (j = 0; j < t->desc->num_columns; j++) { /* FXIME */
404 const struct osl_column_description *cd =
405 get_column_description(t->desc, j);
406 if (cd->storage_type != OSL_MAPPED_STORAGE)
408 ret = check_range(t, i, j);
410 if (ret != -E_FSCK_RANGE_VIOLATION)
412 ret = mark_row_invalid(t, i);
415 t->num_invalid_rows++;
421 if (t->num_invalid_rows)
422 NOTICE_LOG("ranges OK. %d invalid row(s) detected\n",
423 t->num_invalid_rows);
425 INFO_LOG("no invalid rows, no range violations, good\n");
431 static int move_index_entry(struct osl_table *t, uint32_t dest, uint32_t src)
433 char *dest_ie, *src_ie;
434 int ret = get_row_index(t, dest, &dest_ie);
438 ret = get_row_index(t, src, &src_ie);
441 INFO_LOG("moving entry #%u to position %u\n", src, dest);
442 memcpy(dest_ie, src_ie, t->row_index_size);
446 static int map_index(const struct osl_table_description *desc, struct osl_object *map)
448 char *filename = index_filename(desc);
451 ret = mmap_full_file(filename, O_RDWR, &map->data, &map->size, NULL);
452 DEBUG_LOG("mapping index %s: ret: %d, size: %zu\n", filename, ret, map->size);
457 static int prune_invalid_rows_from_index(struct osl_table *t)
459 uint32_t top = 0, bottom;
463 if (!t->num_invalid_rows) {
464 INFO_LOG("all rows are valid, good\n");
467 NOTICE_LOG("deleting %u invalid row(s) (%d bytes) from index\n",
468 t->num_invalid_rows, t->row_index_size * t->num_invalid_rows);
469 bottom = t->num_rows - 1;
470 while (top < bottom) {
471 if (!row_is_invalid(t, top)) {
475 while (bottom > top) {
476 if (row_is_invalid(t, bottom)) {
480 /* move bottom index entry to top */
481 move_index_entry(t, top, bottom);
487 DEBUG_LOG("unmapping index\n");
488 osl_munmap(t->index_map.data, t->index_map.size);
489 filename = index_filename(t->desc);
490 ret = truncate_file(filename, t->row_index_size
491 * t->num_invalid_rows);
495 ret = map_index(t->desc, &t->index_map);
498 t->num_rows = table_num_rows(t);
502 static int check_for_invalid_objects(struct osl_table *t, uint32_t **lost_bytes)
505 const struct osl_column_description *cd;
506 uint32_t *loss = fsck_malloc(sizeof(uint32_t) * t->desc->num_columns);
508 INFO_LOG("looking for mapped objects not contained in index\n");
509 /* first count used bytes */
510 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
511 loss[i] = t->columns[i].data_map.size;
512 for (j = 0; j < t->num_rows; j++) {
513 struct osl_object obj;
514 ret = get_mapped_object(t, i, j, &obj);
521 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
523 NOTICE_LOG("column %u contains %u lost bytes\n",
529 INFO_LOG("all mapped objects are valid, good\n");
537 /* prune_invalid_rows() must be run on the table before calling this */
538 static int prune_mapped_column(struct osl_table *t, uint32_t col_num, int fd)
541 uint32_t written = 0;
542 struct osl_column *col = t->columns + col_num;
544 INFO_LOG("pruning col %u\n", col_num);
545 for (i = 0; i < t->num_rows; i++) {
546 struct osl_object obj;
549 DEBUG_LOG("checking row %u/%u\n", i, t->num_rows);
550 ret = get_mapped_object(t, col_num, i, &obj);
553 ret = _write_all(fd, (char *)(obj.data) - 1, obj.size + 1);
556 written += obj.size + 1;
557 ret = get_row_index(t, i, &index_entry);
560 update_cell_index(index_entry, col, written, obj.size);
565 static int prune_objects(struct osl_table *t, uint32_t *lost_bytes)
568 const struct osl_column_description *cd;
569 char **col_filenames = fsck_calloc(t->desc->num_columns * sizeof(char *));
570 char **new_col_filenames = fsck_calloc(t->desc->num_columns * sizeof(char *));
571 char *idx_filename = index_filename(t->desc);
572 char *old_idx_filename = make_message("%s.bak", idx_filename);
575 NOTICE_LOG("removing unreferenced objects from data files\n");
576 /* first make a copy of the index */
577 ret = osl_open(old_idx_filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
581 ret = _write_all(fd, t->index_map.data, t->index_map.size);
585 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
588 col_filenames[i] = column_filename(t, i);
589 new_col_filenames[i] = make_message("%s.fsck", col_filenames[i]);
590 ret = osl_open(new_col_filenames[i], O_WRONLY | O_CREAT | O_EXCL, 0644);
592 goto out_unlink_data;
594 ret = prune_mapped_column(t, i, fd);
597 goto out_unlink_data;
599 ret = unmap_table(t, OSL_MARK_CLEAN);
601 goto out_unlink_data;
602 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
605 ret = osl_rename(new_col_filenames[i], col_filenames[i]);
606 if (ret < 0) { /* we're kinda screwed here */
607 CRIT_LOG("rename of col %i failed: %s\n", i,
608 osl_strerror(errno));
612 unlink(old_idx_filename);
613 ret = map_table(t, 0);
616 FOR_EACH_MAPPED_COLUMN(i, t, cd)
617 unlink(new_col_filenames[i]);
619 free(old_idx_filename);
621 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
622 free(col_filenames[i]);
623 free(new_col_filenames[i]);
626 free(new_col_filenames);
630 static struct osl_column_description hash_tree_table_cols[] = {
632 .storage_type = OSL_NO_STORAGE,
633 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
635 .compare_function = uint32_compare,
636 .data_size = HASH_SIZE
640 static const struct osl_table_description hash_tree_table_desc = {
641 .dir = "/", /* irrelevant */
645 .column_descriptions = hash_tree_table_cols
649 * The hash_tree table contains all hashes of the disk storage name column.
650 * of each row. It is used for checking if a disk storage file has a reference
653 static struct osl_table *hash_tree_table;
654 static HASH_TYPE *hashes;
656 static int check_disk_storage_column(struct osl_table *t, int row_num,
657 int col_num, char *ds_name, unsigned *num_missing_objects)
661 char *path = disk_storage_path(t, col_num, ds_name);
662 unsigned dsnc = t->disk_storage_name_column;
663 struct osl_object obj;
665 DEBUG_LOG("checking if %s is a regular file\n", path);
666 ret = stat(path, &statbuf);
667 if (ret < 0 && errno == ENOENT) {
669 (*num_missing_objects)++;
670 ERROR_LOG("row %d: object %s is missing\n", row_num, path);
671 NOTICE_LOG("trying to delete row %d\n", row_num);
672 ret = osl_get_row(t, dsnc, &obj, &row);
674 CRIT_LOG("unable to get row %d\n", row_num);
675 mark_row_invalid(t, row_num);
676 CRIT_LOG("Please re-run fsck\n");
679 ret = osl_del_row(t, row);
687 ret = -E_FSCK_NOT_A_REGULAR_FILE;
688 if (!(S_IFREG & statbuf.st_mode))
693 static int check_disk_storage_presence(struct osl_table *t)
696 struct osl_object obj, hash_obj = {.size = HASH_SIZE};
698 const struct osl_column_description *cd;
699 unsigned dsnc = t->disk_storage_name_column, missing_objects = 0;
703 hashes = fsck_malloc(t->num_rows * HASH_SIZE);
704 INFO_LOG("looking for missing disk storage objects\n");
705 for (i = 0; i < t->num_rows; i++) {
706 if (row_is_invalid(t, i))
708 ret = get_mapped_object(t, dsnc, i, &obj);
711 hash_object(&obj, hashes + i * HASH_SIZE);
712 hash_obj.data = hashes + i * HASH_SIZE;
713 osl_add_row(hash_tree_table, &hash_obj);
714 ds_name = disk_storage_name_of_hash(t, hashes + i * HASH_SIZE);
715 FOR_EACH_DISK_STORAGE_COLUMN(j, t, cd) {
716 ret = check_disk_storage_column(t, i, j, ds_name,
723 if (!missing_objects)
724 INFO_LOG("all referenced disk storage objects exist, good\n");
726 NOTICE_LOG("%d missing object(s)\n", missing_objects);
727 return missing_objects;
733 static int dummy_compare(const struct osl_object *obj1, const struct osl_object *obj2)
742 static unsigned files_pruned;
744 int prune_disk_storage_file(const char *path, void *private_data)
746 HASH_TYPE hash[HASH_SIZE];
747 uint8_t flags = *(uint8_t *)private_data;
748 struct osl_object obj = {.data = hash, .size = HASH_SIZE};
751 size_t len = strlen(path);
754 DEBUG_LOG("path: %s\n", path);
755 if (flags & OSL_LARGE_TABLE) {
756 if (len < HASH_SIZE * 2 + 2)
758 // NOTICE_LOG("p: %s\n", path + len - 2 * HASH_SIZE - 1);
759 ret = asc_to_hash(path + len - 2 * HASH_SIZE - 1, 1, hash);
762 ret = asc_to_hash(path + len - 2 * HASH_SIZE + 2, HASH_SIZE - 1,
766 // INFO_LOG("high: %x, low: %x, hash: %x\n", high, low, hash);
768 if (len < 2 * HASH_SIZE + 1)
770 ret = asc_to_hash(path + len - 2 * HASH_SIZE, 2 * HASH_SIZE, hash);
773 // INFO_LOG("hash: %x\n", hash);
777 char asc[2 * HASH_SIZE + 1];
778 hash_to_asc(hash, asc);
779 NOTICE_LOG("before: %s\nafter: %s\n", path, asc);
782 ret = osl_get_row(hash_tree_table, 0, &obj, &row);
785 NOTICE_LOG("unreferenced file in hash dir: %s\n", path);
788 ERROR_LOG("could not read hash value of %s\n", path);
790 NOTICE_LOG("removing %s\n", path);
796 static int prune_disk_storage_files(struct osl_table *t)
799 const struct osl_column_description *cd;
801 INFO_LOG("looking for unreferenced disk storage files\n");
802 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
803 char *dirname = column_filename(t, i);
804 uint8_t flags = t->desc->flags;
805 ret = for_each_file_in_dir(dirname, prune_disk_storage_file,
810 NOTICE_LOG("%u disk storage files deleted\n",
813 INFO_LOG("all files are are referenced, good\n");
817 static int check_disk_storage_columns(struct osl_table *t)
820 const struct osl_column_description *cd;
822 if (!t->num_disk_storage_columns) {
823 INFO_LOG("no disk storage columns in table '%s', "
824 "skipping checks\n", t->desc->name);
827 FOR_EACH_COLUMN(i, t->desc, cd)
828 t->desc->column_descriptions[i].compare_function = dummy_compare;
829 ret = init_rbtrees(t);
832 INFO_LOG("creating rbtree for disk storage hash values\n");
833 ret = osl_open_table(&hash_tree_table_desc, &hash_tree_table);
836 ret = check_disk_storage_presence(t);
838 goto out_close_hash_tree;
839 ret = prune_disk_storage_files(t);
841 osl_close_table(hash_tree_table, 0);
845 clear_rbtrees(t); /* TODO why are we doing that here? Seems odd */
849 static void set_dummy_contents(struct osl_table_description *desc)
852 struct osl_column_description *cd;
854 for (i = 0; i < desc->num_columns; i++) {
855 cd = get_column_description(desc, i);
856 cd->compare_function = dummy_compare;
860 static int fsck_init(struct osl_table_description *desc, struct osl_table **t)
862 struct osl_object map;
863 int ret = map_index(desc, &map);
867 ret = read_table_desc(&map, desc);
869 osl_munmap(map.data, map.size);
872 set_dummy_contents(desc);
873 ret = init_table_structure(desc, t);
875 osl_munmap(map.data, map.size);
878 DEBUG_LOG("unmapping index\n");
879 osl_munmap(map.data, map.size);
880 if (conf.force_given)
881 ret = map_table(*t, (MAP_TBL_FL_IGNORE_DIRTY));
883 ret = map_table(*t, 0);
885 (*t)->num_rows = table_num_rows(*t);
890 static void fsck_cleanup(struct osl_table *t)
896 if (t->desc->column_descriptions) {
897 struct osl_column_description *cd;
898 for (i = 0; i < t->desc->num_columns; i++) {
899 cd = get_column_description(t->desc, i);
900 free((char*)cd->name);
902 free(t->desc->column_descriptions);
909 #define ST_CASE(st) case st: return #st
911 const char *get_asc_storage_type(enum osl_storage_type st)
914 ST_CASE(OSL_MAPPED_STORAGE);
915 ST_CASE(OSL_DISK_STORAGE);
916 ST_CASE(OSL_NO_STORAGE);
921 #define APPEND_ASC_SF(sf, flag, str) do { if (sf & flag) { \
922 if (str) str = make_message("%s%s", str, " | " # flag); \
923 else str = fsck_strdup(#flag); }} while (0)
926 char *get_asc_storage_flags(enum osl_storage_type sf)
930 APPEND_ASC_SF(sf, OSL_RBTREE, asc_sf);
931 APPEND_ASC_SF(sf, OSL_FIXED_SIZE, asc_sf);
932 APPEND_ASC_SF(sf, OSL_UNIQUE, asc_sf);
936 static int dump_table_desc(struct osl_table *t, int fd)
938 const struct osl_table_description *desc = t->desc;
940 struct osl_column_description *cd;
941 char *msg = make_message("static struct osl_column_description cols[] = {\n");
942 ret = _write_all(fd, msg, strlen(msg));
946 FOR_EACH_COLUMN(i, desc, cd) {
948 msg = make_message("\t[%d] = {\n", i);
949 ret = _write_all(fd, msg, strlen(msg));
953 asc_st = get_asc_storage_type(cd->storage_type);
954 msg = make_message("\t\t.storage_type = %s,\n", asc_st);
955 ret = _write_all(fd, msg, strlen(msg));
959 if (cd->storage_flags) {
960 char *asc_sf = get_asc_storage_flags(cd->storage_flags);
961 msg = make_message("\t\t,storage_flags = %s,\n", asc_sf);
963 ret = _write_all(fd, msg, strlen(msg));
968 if (cd->storage_flags & OSL_FIXED_SIZE) {
969 msg = make_message("\t\t.data_size = %u,\n", cd->data_size);
970 ret = _write_all(fd, msg, strlen(msg));
975 msg = make_message("\t\t.name = \"%s\",\n", cd->name);
976 ret = _write_all(fd, msg, strlen(msg));
980 if (cd->storage_flags & OSL_RBTREE) {
981 msg = make_message("\t\t.compare_function = compare_func,\n");
982 ret = _write_all(fd, msg, strlen(msg));
987 msg = make_message("\t},\n");
988 ret = _write_all(fd, msg, strlen(msg));
993 msg = make_message("};\n");
994 ret = _write_all(fd, msg, strlen(msg));
1001 static int dump_row(struct osl_table *t, unsigned row_num, const char *row_dir)
1004 const struct osl_column_description *cd;
1006 struct osl_object obj;
1008 HASH_TYPE hash[HASH_SIZE];
1011 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
1012 ret = get_mapped_object(t, i, row_num, &obj);
1015 filename = make_message("%s/col_%03u", row_dir, i);
1016 ret = write_file(filename, obj.data, obj.size);
1021 if (!t->num_disk_storage_columns)
1023 dsnc = t->disk_storage_name_column;
1024 ret = get_mapped_object(t, dsnc, row_num, &obj);
1027 hash_object(&obj, hash);
1028 ds_name = disk_storage_name_of_hash(t, hash);
1029 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
1030 filename = disk_storage_path(t, i, ds_name);
1031 ret = mmap_full_file(filename, O_RDONLY, &obj.data, &obj.size, NULL);
1035 filename = make_message("%s/col_%03u", row_dir, i);
1036 ret = write_file(filename, obj.data, obj.size);
1047 static int dump_rows(char *dump_dir, struct osl_table *t)
1050 char *current_dir = NULL;
1053 for (i = 0; i < t->num_rows; i++) {
1055 if (row_is_invalid(t, i))
1059 current_dir = make_message("%s/rows_%u-%u", dump_dir, i, i + 999);
1060 NOTICE_LOG("dumping rows %u - %u\n", i, i + 999);
1061 ret = osl_mkdir(current_dir, 0777);
1062 if (ret < 0 && !is_errno(-ret, EEXIST))
1065 row_dir = make_message("%s/row_%03u", current_dir, i);
1066 ret = osl_mkdir(row_dir, 0777);
1067 if (ret < 0 && !is_errno(-ret, EEXIST)) {
1071 ret = dump_row(t, i, row_dir);
1081 static int dump_table(char *dump_dir, struct osl_table_description *desc)
1083 struct osl_table *t = NULL;
1084 int fd, ret = fsck_init(desc, &t);
1086 char *table_dump_dir = NULL;
1090 ret = osl_mkdir(dump_dir, 0777);
1091 if (ret < 0 && !is_errno(-ret, EEXIST))
1093 table_dump_dir = make_message("%s/%s", dump_dir, desc->name);
1094 ret = osl_mkdir(table_dump_dir, 0777);
1095 if (ret < 0 && !is_errno(-ret, EEXIST))
1097 desc_file = make_message("%s/table_description.c", table_dump_dir);
1098 ret = osl_open(desc_file, O_WRONLY | O_CREAT | O_EXCL, 0644);
1103 ret = dump_table_desc(t, fd);
1107 ret = dump_rows(table_dump_dir, t);
1109 free(table_dump_dir);
1114 static int fsck(struct osl_table_description *desc)
1117 struct osl_table *t = NULL;
1118 uint32_t *lost_bytes = NULL;
1120 ret = fsck_init(desc, &t);
1123 ret = check_index_ranges(t);
1126 ret = check_disk_storage_columns(t);
1129 ret = prune_invalid_rows_from_index(t);
1132 ret = check_for_invalid_objects(t, &lost_bytes);
1135 if (ret > 0) { /* at least one mapped data file needs pruning */
1136 ret = prune_objects(t, lost_bytes);
1142 unmap_table(t, OSL_MARK_CLEAN);
1148 static int check_table(char *db_dir, char *table_name)
1150 struct osl_table_description desc = {
1151 .column_descriptions = NULL,
1157 INFO_LOG("checking table %s\n", table_name);
1158 if (!conf.no_fsck_given) {
1164 if (!conf.dump_dir_given || !*conf.dump_dir_arg)
1166 ret = dump_table(conf.dump_dir_arg, &desc);
1169 ERROR_LOG("failed to check table %s\n", table_name);
1171 NOTICE_LOG("successfully checked table %s\n", table_name);
1175 static int check_all_tables(char *db_dir)
1178 struct dirent *entry;
1179 int cwd_fd, ret2, ret = fsck_opendir(db_dir, &dir, &cwd_fd);
1183 while ((entry = readdir(dir))) {
1186 if (!strcmp(entry->d_name, "."))
1188 if (!strcmp(entry->d_name, ".."))
1190 if (lstat(entry->d_name, &s) == -1)
1195 ret = check_table(db_dir, entry->d_name);
1200 ret2 = __fchdir(cwd_fd);
1201 if (ret2 < 0 && ret >= 0)
1207 int main(int argc, char **argv)
1210 struct fsck_cmdline_parser_params params = {
1213 .check_required = 1,
1214 .check_ambiguity = 1,
1217 ret = fsck_cmdline_parser_ext(argc, argv, &conf, ¶ms);
1220 ret = -E_FSCK_SYNTAX;
1223 loglevel = conf.loglevel_arg;
1224 HANDLE_VERSION_FLAG("fsck", conf);
1225 INFO_LOG("database dir: %s\n", conf.database_dir_arg);
1226 if (!conf.inputs_num) {
1227 ret = check_all_tables(conf.database_dir_arg);
1230 for (i = 0; i < conf.inputs_num; i++) {
1231 ret = check_table(conf.database_dir_arg, conf.inputs[i]);
1237 ERROR_LOG("%s\n", fsck_strerror(-ret));
1238 if (conf.loglevel_arg > 1)
1239 EMERG_LOG("re-run with \"--loglevel %d\" to increase verbosity\n",
1240 conf.loglevel_arg - 1);
1242 NOTICE_LOG("success\n");
1243 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;