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>
19 #include "fsck.cmdline.h"
21 /** version text used by various commands if -V switch was given */
22 #define VERSION_TEXT(prefix) "osl_" prefix " " VERSION " " "\n" \
23 "Copyright (C) 2008 Andre Noll\n" \
24 "This is free software with ABSOLUTELY NO WARRANTY." \
25 " See COPYING for details.\n" \
26 "Written by Andre Noll.\n" \
27 "Report bugs to <maan@systemlinux.org>.\n"
29 /** print out \p VERSION_TEXT and exit if version flag was given */
30 #define HANDLE_VERSION_FLAG(_prefix, _args_info_struct) \
31 if (_args_info_struct.version_given) { \
32 printf("%s", VERSION_TEXT(_prefix)); \
36 static struct fsck_args_info conf;
39 FSCK_ERROR(RANGE_VIOLATION, "range violation detected, very bad"), \
40 FSCK_ERROR(NOT_A_REGULAR_FILE, "not a regular file"), \
41 FSCK_ERROR(SYNTAX, "fsck syntax error"), \
42 FSCK_ERROR(ACCESS, "permission denied"), \
43 FSCK_ERROR(CHDIR, "could not change directory"), \
44 FSCK_ERROR(OPENDIR, "could not open directory"),
46 #define FSCK_ERROR_BIT 29
47 #define FSCK_ERROR(num, txt) E_FSCK_ ## num
49 FSCK_DUMMY = (1 << FSCK_ERROR_BIT) - 1,
53 #define FSCK_ERROR(num, txt) txt
54 const char const *fsck_errors[] = {
58 static const char *fsck_strerror(int num)
60 if (num & (1 << FSCK_ERROR_BIT))
61 return fsck_errors[num & ((1 << FSCK_ERROR_BIT) - 1)];
62 return osl_strerror(num);
68 signed char hexval_table[256] = {
69 -1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */
70 -1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */
71 -1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */
72 -1, -1, -1, -1, -1, -1, -1, -1, /* 18-1f */
73 -1, -1, -1, -1, -1, -1, -1, -1, /* 20-27 */
74 -1, -1, -1, -1, -1, -1, -1, -1, /* 28-2f */
75 0, 1, 2, 3, 4, 5, 6, 7, /* 30-37 */
76 8, 9, -1, -1, -1, -1, -1, -1, /* 38-3f */
77 -1, 10, 11, 12, 13, 14, 15, -1, /* 40-47 */
78 -1, -1, -1, -1, -1, -1, -1, -1, /* 48-4f */
79 -1, -1, -1, -1, -1, -1, -1, -1, /* 50-57 */
80 -1, -1, -1, -1, -1, -1, -1, -1, /* 58-5f */
81 -1, 10, 11, 12, 13, 14, 15, -1, /* 60-67 */
82 -1, -1, -1, -1, -1, -1, -1, -1, /* 68-67 */
83 -1, -1, -1, -1, -1, -1, -1, -1, /* 70-77 */
84 -1, -1, -1, -1, -1, -1, -1, -1, /* 78-7f */
85 -1, -1, -1, -1, -1, -1, -1, -1, /* 80-87 */
86 -1, -1, -1, -1, -1, -1, -1, -1, /* 88-8f */
87 -1, -1, -1, -1, -1, -1, -1, -1, /* 90-97 */
88 -1, -1, -1, -1, -1, -1, -1, -1, /* 98-9f */
89 -1, -1, -1, -1, -1, -1, -1, -1, /* a0-a7 */
90 -1, -1, -1, -1, -1, -1, -1, -1, /* a8-af */
91 -1, -1, -1, -1, -1, -1, -1, -1, /* b0-b7 */
92 -1, -1, -1, -1, -1, -1, -1, -1, /* b8-bf */
93 -1, -1, -1, -1, -1, -1, -1, -1, /* c0-c7 */
94 -1, -1, -1, -1, -1, -1, -1, -1, /* c8-cf */
95 -1, -1, -1, -1, -1, -1, -1, -1, /* d0-d7 */
96 -1, -1, -1, -1, -1, -1, -1, -1, /* d8-df */
97 -1, -1, -1, -1, -1, -1, -1, -1, /* e0-e7 */
98 -1, -1, -1, -1, -1, -1, -1, -1, /* e8-ef */
99 -1, -1, -1, -1, -1, -1, -1, -1, /* f0-f7 */
100 -1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */
103 int asc_to_hash(const char *asc_hash, int len, HASH_TYPE *hash)
106 const unsigned char *asc = (const unsigned char *) asc_hash;
108 while (*asc && i++ < len) {
109 unsigned int val = (hexval_table[asc[0]] << 4) | hexval_table[asc[1]];
119 static int _write_all(int fd, const char *buf, size_t len)
121 return write_all(fd, buf, &len);
125 * Wrapper for malloc().
127 * \param size The desired new size.
129 * A wrapper for malloc(3) which exits on errors.
131 * \return A pointer to the allocated memory, which is suitably aligned for any
136 __must_check __malloc static void *fsck_malloc(size_t size)
139 void *p = malloc(size);
142 EMERG_LOG("malloc failed (size = %zu), aborting\n",
150 * Allocate memory and fill with zeros.
152 * \param size The desired new size.
154 * A wrapper for calloc(3) which exits on errors.
156 * \return A pointer to the allocated and zeroed-out memory, which is suitably
157 * aligned for any kind of variable.
161 __must_check __malloc static void *fsck_calloc(size_t size)
163 void *ret = fsck_malloc(size);
165 memset(ret, 0, size);
170 * Paraslash's version of strdup().
172 * \param s The string to be duplicated.
174 * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
175 * there is no need to check the return value in the caller.
177 * \return A pointer to the duplicated string. If \p s was the NULL pointer,
178 * an pointer to an empty string is returned.
182 __must_check __malloc static char *fsck_strdup(const char *s)
186 if ((ret = strdup(s? s: "")))
188 EMERG_LOG("strdup failed, aborting\n");
193 * Compare two osl objects pointing to unsigned integers of 32 bit size.
195 * \param obj1 Pointer to the first integer.
196 * \param obj2 Pointer to the second integer.
198 * \return The values required for an osl compare function.
200 * \sa osl_compare_func, osl_hash_compare().
202 static int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2)
204 uint32_t d1 = read_u32((const char *)obj1->data);
205 uint32_t d2 = read_u32((const char *)obj2->data);
215 * A wrapper for fchdir().
217 * \param fd An open file descriptor.
221 static inline int __fchdir(int fd)
225 return errno == EACCES? -E_FSCK_ACCESS : -E_FSCK_CHDIR;
229 * Wrapper for chdir(2).
231 * \param path The specified directory.
235 _static_inline_ int __chdir(const char *path)
237 if (chdir(path) >= 0)
239 return errno == EACCES? -E_FSCK_ACCESS : -E_FSCK_CHDIR;
243 * Save the cwd and open a given directory.
245 * \param dirname Path to the directory to open.
246 * \param dir Result pointer.
247 * \param cwd File descriptor of the current working directory.
251 * Opening the current directory (".") and calling fchdir() to return is
252 * usually faster and more reliable than saving cwd in some buffer and calling
253 * chdir() afterwards.
255 * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
256 * stored in \a cwd. If the function returns success, and \a cwd is not \p
257 * NULL, the caller must close this file descriptor (probably after calling
260 * On errors, the function undos everything, so the caller needs neither close
261 * any files, nor change back to the original working directory.
266 static int fsck_opendir(const char *dirname, DIR **dir, int *cwd)
271 ret = osl_open(".", O_RDONLY, 0);
276 ret = __chdir(dirname);
282 ret = errno == EACCES? -E_FSCK_ACCESS : -E_FSCK_OPENDIR;
283 /* Ignore return value of fchdir() and close(). We're busted anyway. */
295 * Traverse the given directory recursively.
297 * \param dirname The directory to traverse.
298 * \param func The function to call for each entry.
299 * \param private_data Pointer to an arbitrary data structure.
301 * For each regular file under \a dirname, the supplied function \a func is
302 * called. The full path of the regular file and the \a private_data pointer
303 * are passed to \a func. Directories for which the calling process has no
304 * permissions to change to are silently ignored.
308 static int for_each_file_in_dir(const char *dirname,
309 int (*func)(const char *, void *), void *private_data)
312 struct dirent *entry;
313 int cwd_fd, ret2, ret = fsck_opendir(dirname, &dir, &cwd_fd);
316 return ret == -E_FSCK_ACCESS? 1 : ret;
317 /* scan cwd recursively */
318 while ((entry = readdir(dir))) {
323 if (!strcmp(entry->d_name, "."))
325 if (!strcmp(entry->d_name, ".."))
327 if (lstat(entry->d_name, &s) == -1)
330 if (!S_ISREG(m) && !S_ISDIR(m))
332 tmp = make_message("%s/%s", dirname, entry->d_name);
334 ret = func(tmp, private_data);
341 ret = for_each_file_in_dir(tmp, func, private_data);
349 ret2 = __fchdir(cwd_fd);
350 if (ret2 < 0 && ret >= 0)
357 * check for object boundary violations
359 * test whether the range pointed to by the index entry for a given cell is
360 * contained in mapped data file. This should always be the case. Otherwise
361 * we are in real trouble.
363 static int check_range(struct osl_table *t, uint32_t row_num, uint32_t col_num)
366 struct osl_object obj;
367 struct osl_column *col;
369 char *map_start, *obj_start;
371 ret = get_cell_index(t, row_num, col_num, &index_entry);
374 ret = get_mapped_object(t, col_num, row_num, &obj);
377 col = t->columns + col_num;
378 obj_start = obj.data;
379 map_start = col->data_map.data;
380 // INFO_LOG("obj: %p..%p\n", obj_start, obj_start + obj.size);
381 // INFO_LOG("map: %p..%p\n", map_start, map_start + col->data_map.size);
382 if (obj_start < map_start || obj_start + obj.size > map_start + col->data_map.size) {
383 CRIT_LOG("range violation in row %u, col %u\n", row_num,
385 return -E_FSCK_RANGE_VIOLATION;
387 DEBUG_LOG("col %u: ok\n", col_num);
392 * check all cells of the given table for boundary violations
394 static int check_index_ranges(struct osl_table *t)
398 INFO_LOG("checking for range violations in index\n");
399 //DEBUG_LOG("%d rows. %d columns\n", t->num_rows, t->desc->num_columns);
400 t->num_invalid_rows = 0;
401 for (i = 0; i < t->num_rows; i++) {
402 if (row_is_invalid(t, i)) {
403 t->num_invalid_rows++;
406 for (j = 0; j < t->desc->num_columns; j++) { /* FXIME */
407 const struct osl_column_description *cd =
408 get_column_description(t->desc, j);
409 if (cd->storage_type != OSL_MAPPED_STORAGE)
411 ret = check_range(t, i, j);
413 if (ret != -E_FSCK_RANGE_VIOLATION)
415 ret = mark_row_invalid(t, i);
418 t->num_invalid_rows++;
424 if (t->num_invalid_rows)
425 NOTICE_LOG("ranges OK. %d invalid row(s) detected\n",
426 t->num_invalid_rows);
428 INFO_LOG("no invalid rows, no range violations, good\n");
434 static int move_index_entry(struct osl_table *t, uint32_t dest, uint32_t src)
436 char *dest_ie, *src_ie;
437 int ret = get_row_index(t, dest, &dest_ie);
441 ret = get_row_index(t, src, &src_ie);
444 INFO_LOG("moving entry #%u to position %u\n", src, dest);
445 memcpy(dest_ie, src_ie, t->row_index_size);
449 static int map_index(const struct osl_table_description *desc, struct osl_object *map)
451 char *filename = index_filename(desc);
454 ret = mmap_full_file(filename, O_RDWR, &map->data, &map->size, NULL);
455 DEBUG_LOG("mapping index %s: ret: %d, size: %zu\n", filename, ret, map->size);
460 static int prune_invalid_rows_from_index(struct osl_table *t)
462 uint32_t top = 0, bottom;
466 if (!t->num_invalid_rows) {
467 INFO_LOG("all rows are valid, good\n");
470 NOTICE_LOG("deleting %u invalid row(s) (%d bytes) from index\n",
471 t->num_invalid_rows, t->row_index_size * t->num_invalid_rows);
472 bottom = t->num_rows - 1;
473 while (top < bottom) {
474 if (!row_is_invalid(t, top)) {
478 while (bottom > top) {
479 if (row_is_invalid(t, bottom)) {
483 /* move bottom index entry to top */
484 move_index_entry(t, top, bottom);
490 DEBUG_LOG("unmapping index\n");
491 osl_munmap(t->index_map.data, t->index_map.size);
492 filename = index_filename(t->desc);
493 ret = truncate_file(filename, t->row_index_size
494 * t->num_invalid_rows);
498 ret = map_index(t->desc, &t->index_map);
501 t->num_rows = table_num_rows(t);
505 static int check_for_invalid_objects(struct osl_table *t, uint32_t **lost_bytes)
508 const struct osl_column_description *cd;
509 uint32_t *loss = fsck_malloc(sizeof(uint32_t) * t->desc->num_columns);
511 INFO_LOG("looking for mapped objects not contained in index\n");
512 /* first count used bytes */
513 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
514 loss[i] = t->columns[i].data_map.size;
515 for (j = 0; j < t->num_rows; j++) {
516 struct osl_object obj;
517 ret = get_mapped_object(t, i, j, &obj);
524 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
526 NOTICE_LOG("column %u contains %u lost bytes\n",
532 INFO_LOG("all mapped objects are valid, good\n");
540 /* prune_invalid_rows() must be run on the table before calling this */
541 static int prune_mapped_column(struct osl_table *t, uint32_t col_num, int fd)
544 uint32_t written = 0;
545 struct osl_column *col = t->columns + col_num;
547 INFO_LOG("pruning col %u\n", col_num);
548 for (i = 0; i < t->num_rows; i++) {
549 struct osl_object obj;
552 DEBUG_LOG("checking row %u/%u\n", i, t->num_rows);
553 ret = get_mapped_object(t, col_num, i, &obj);
556 ret = _write_all(fd, (char *)(obj.data) - 1, obj.size + 1);
559 written += obj.size + 1;
560 ret = get_row_index(t, i, &index_entry);
563 update_cell_index(index_entry, col, written, obj.size);
568 static int prune_objects(struct osl_table *t, uint32_t *lost_bytes)
571 const struct osl_column_description *cd;
572 char **col_filenames = fsck_calloc(t->desc->num_columns * sizeof(char *));
573 char **new_col_filenames = fsck_calloc(t->desc->num_columns * sizeof(char *));
574 char *idx_filename = index_filename(t->desc);
575 char *old_idx_filename = make_message("%s.bak", idx_filename);
578 NOTICE_LOG("removing unreferenced objects from data files\n");
579 /* first make a copy of the index */
580 ret = osl_open(old_idx_filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
584 ret = _write_all(fd, t->index_map.data, t->index_map.size);
588 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
591 col_filenames[i] = column_filename(t, i);
592 new_col_filenames[i] = make_message("%s.fsck", col_filenames[i]);
593 ret = osl_open(new_col_filenames[i], O_WRONLY | O_CREAT | O_EXCL, 0644);
595 goto out_unlink_data;
597 ret = prune_mapped_column(t, i, fd);
600 goto out_unlink_data;
602 ret = unmap_table(t, OSL_MARK_CLEAN);
604 goto out_unlink_data;
605 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
608 ret = osl_rename(new_col_filenames[i], col_filenames[i]);
609 if (ret < 0) { /* we're kinda screwed here */
610 CRIT_LOG("rename of col %i failed: %s\n", i,
611 osl_strerror(errno));
615 unlink(old_idx_filename);
616 ret = map_table(t, 0);
619 FOR_EACH_MAPPED_COLUMN(i, t, cd)
620 unlink(new_col_filenames[i]);
622 free(old_idx_filename);
624 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
625 free(col_filenames[i]);
626 free(new_col_filenames[i]);
629 free(new_col_filenames);
633 static struct osl_column_description hash_tree_table_cols[] = {
635 .storage_type = OSL_NO_STORAGE,
636 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
638 .compare_function = uint32_compare,
639 .data_size = HASH_SIZE
643 static const struct osl_table_description hash_tree_table_desc = {
644 .dir = "/", /* irrelevant */
648 .column_descriptions = hash_tree_table_cols
652 * The hash_tree table contains all hashes of the disk storage name column.
653 * of each row. It is used for checking if a disk storage file has a reference
656 static struct osl_table *hash_tree_table;
657 static HASH_TYPE *hashes;
659 static int check_disk_storage_column(struct osl_table *t, int row_num,
660 int col_num, char *ds_name, unsigned *num_missing_objects)
664 char *path = disk_storage_path(t, col_num, ds_name);
665 unsigned dsnc = t->disk_storage_name_column;
666 struct osl_object obj;
668 DEBUG_LOG("checking if %s is a regular file\n", path);
669 ret = stat(path, &statbuf);
670 if (ret < 0 && errno == ENOENT) {
672 (*num_missing_objects)++;
673 ERROR_LOG("row %d: object %s is missing\n", row_num, path);
674 NOTICE_LOG("trying to delete row %d\n", row_num);
675 ret = osl_get_row(t, dsnc, &obj, &row);
677 CRIT_LOG("unable to get row %d\n", row_num);
678 mark_row_invalid(t, row_num);
679 CRIT_LOG("Please re-run fsck\n");
682 ret = osl_del_row(t, row);
690 ret = -E_FSCK_NOT_A_REGULAR_FILE;
691 if (!(S_IFREG & statbuf.st_mode))
696 static int check_disk_storage_presence(struct osl_table *t)
699 struct osl_object obj, hash_obj = {.size = HASH_SIZE};
701 const struct osl_column_description *cd;
702 unsigned dsnc = t->disk_storage_name_column, missing_objects = 0;
706 hashes = fsck_malloc(t->num_rows * HASH_SIZE);
707 INFO_LOG("looking for missing disk storage objects\n");
708 for (i = 0; i < t->num_rows; i++) {
709 if (row_is_invalid(t, i))
711 ret = get_mapped_object(t, dsnc, i, &obj);
714 hash_object(&obj, hashes + i * HASH_SIZE);
715 hash_obj.data = hashes + i * HASH_SIZE;
716 osl_add_row(hash_tree_table, &hash_obj);
717 ds_name = disk_storage_name_of_hash(t, hashes + i * HASH_SIZE);
718 FOR_EACH_DISK_STORAGE_COLUMN(j, t, cd) {
719 ret = check_disk_storage_column(t, i, j, ds_name,
726 if (!missing_objects)
727 INFO_LOG("all referenced disk storage objects exist, good\n");
729 NOTICE_LOG("%d missing object(s)\n", missing_objects);
730 return missing_objects;
736 static int dummy_compare(const struct osl_object *obj1, const struct osl_object *obj2)
745 static unsigned files_pruned;
747 int prune_disk_storage_file(const char *path, void *private_data)
749 HASH_TYPE hash[HASH_SIZE];
750 uint8_t flags = *(uint8_t *)private_data;
751 struct osl_object obj = {.data = hash, .size = HASH_SIZE};
754 size_t len = strlen(path);
757 DEBUG_LOG("path: %s\n", path);
758 if (flags & OSL_LARGE_TABLE) {
759 if (len < HASH_SIZE * 2 + 2)
761 // NOTICE_LOG("p: %s\n", path + len - 2 * HASH_SIZE - 1);
762 ret = asc_to_hash(path + len - 2 * HASH_SIZE - 1, 1, hash);
765 ret = asc_to_hash(path + len - 2 * HASH_SIZE + 2, HASH_SIZE - 1,
769 // INFO_LOG("high: %x, low: %x, hash: %x\n", high, low, hash);
771 if (len < 2 * HASH_SIZE + 1)
773 ret = asc_to_hash(path + len - 2 * HASH_SIZE, 2 * HASH_SIZE, hash);
776 // INFO_LOG("hash: %x\n", hash);
780 char asc[2 * HASH_SIZE + 1];
781 hash_to_asc(hash, asc);
782 NOTICE_LOG("before: %s\nafter: %s\n", path, asc);
785 ret = osl_get_row(hash_tree_table, 0, &obj, &row);
788 NOTICE_LOG("unreferenced file in hash dir: %s\n", path);
791 ERROR_LOG("could not read hash value of %s\n", path);
793 NOTICE_LOG("removing %s\n", path);
799 static int prune_disk_storage_files(struct osl_table *t)
802 const struct osl_column_description *cd;
804 INFO_LOG("looking for unreferenced disk storage files\n");
805 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
806 char *dirname = column_filename(t, i);
807 uint8_t flags = t->desc->flags;
808 ret = for_each_file_in_dir(dirname, prune_disk_storage_file,
813 NOTICE_LOG("%u disk storage files deleted\n",
816 INFO_LOG("all files are are referenced, good\n");
820 static int check_disk_storage_columns(struct osl_table *t)
823 const struct osl_column_description *cd;
825 if (!t->num_disk_storage_columns) {
826 INFO_LOG("no disk storage columns in table '%s', "
827 "skipping checks\n", t->desc->name);
830 FOR_EACH_COLUMN(i, t->desc, cd)
831 t->desc->column_descriptions[i].compare_function = dummy_compare;
832 ret = init_rbtrees(t);
835 INFO_LOG("creating rbtree for disk storage hash values\n");
836 ret = osl_open_table(&hash_tree_table_desc, &hash_tree_table);
839 ret = check_disk_storage_presence(t);
841 goto out_close_hash_tree;
842 ret = prune_disk_storage_files(t);
844 osl_close_table(hash_tree_table, 0);
848 clear_rbtrees(t); /* TODO why are we doing that here? Seems odd */
852 static void set_dummy_contents(struct osl_table_description *desc)
855 struct osl_column_description *cd;
857 for (i = 0; i < desc->num_columns; i++) {
858 cd = get_column_description(desc, i);
859 cd->compare_function = dummy_compare;
863 static int fsck_init(struct osl_table_description *desc, struct osl_table **t)
865 struct osl_object map;
866 int ret = map_index(desc, &map);
870 ret = read_table_desc(&map, desc);
872 osl_munmap(map.data, map.size);
875 set_dummy_contents(desc);
876 ret = init_table_structure(desc, t);
878 osl_munmap(map.data, map.size);
881 DEBUG_LOG("unmapping index\n");
882 osl_munmap(map.data, map.size);
883 if (conf.force_given)
884 ret = map_table(*t, (MAP_TBL_FL_IGNORE_DIRTY));
886 ret = map_table(*t, 0);
888 (*t)->num_rows = table_num_rows(*t);
893 static void fsck_cleanup(struct osl_table *t)
899 if (t->desc->column_descriptions) {
900 struct osl_column_description *cd;
901 for (i = 0; i < t->desc->num_columns; i++) {
902 cd = get_column_description(t->desc, i);
903 free((char*)cd->name);
905 free(t->desc->column_descriptions);
912 #define ST_CASE(st) case st: return #st
914 const char *get_asc_storage_type(enum osl_storage_type st)
917 ST_CASE(OSL_MAPPED_STORAGE);
918 ST_CASE(OSL_DISK_STORAGE);
919 ST_CASE(OSL_NO_STORAGE);
924 #define APPEND_ASC_SF(sf, flag, str) do { if (sf & flag) { \
925 if (str) str = make_message("%s%s", str, " | " # flag); \
926 else str = fsck_strdup(#flag); }} while (0)
929 char *get_asc_storage_flags(enum osl_storage_type sf)
933 APPEND_ASC_SF(sf, OSL_RBTREE, asc_sf);
934 APPEND_ASC_SF(sf, OSL_FIXED_SIZE, asc_sf);
935 APPEND_ASC_SF(sf, OSL_UNIQUE, asc_sf);
939 static int dump_table_desc(struct osl_table *t, int fd)
941 const struct osl_table_description *desc = t->desc;
943 struct osl_column_description *cd;
944 char *msg = make_message("static struct osl_column_description cols[] = {\n");
945 ret = _write_all(fd, msg, strlen(msg));
949 FOR_EACH_COLUMN(i, desc, cd) {
951 msg = make_message("\t[%d] = {\n", i);
952 ret = _write_all(fd, msg, strlen(msg));
956 asc_st = get_asc_storage_type(cd->storage_type);
957 msg = make_message("\t\t.storage_type = %s,\n", asc_st);
958 ret = _write_all(fd, msg, strlen(msg));
962 if (cd->storage_flags) {
963 char *asc_sf = get_asc_storage_flags(cd->storage_flags);
964 msg = make_message("\t\t,storage_flags = %s,\n", asc_sf);
966 ret = _write_all(fd, msg, strlen(msg));
971 if (cd->storage_flags & OSL_FIXED_SIZE) {
972 msg = make_message("\t\t.data_size = %u,\n", cd->data_size);
973 ret = _write_all(fd, msg, strlen(msg));
978 msg = make_message("\t\t.name = \"%s\",\n", cd->name);
979 ret = _write_all(fd, msg, strlen(msg));
983 if (cd->storage_flags & OSL_RBTREE) {
984 msg = make_message("\t\t.compare_function = compare_func,\n");
985 ret = _write_all(fd, msg, strlen(msg));
990 msg = make_message("\t},\n");
991 ret = _write_all(fd, msg, strlen(msg));
996 msg = make_message("};\n");
997 ret = _write_all(fd, msg, strlen(msg));
1004 static int dump_row(struct osl_table *t, unsigned row_num, const char *row_dir)
1007 const struct osl_column_description *cd;
1009 struct osl_object obj;
1011 HASH_TYPE hash[HASH_SIZE];
1014 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
1015 ret = get_mapped_object(t, i, row_num, &obj);
1018 filename = make_message("%s/col_%03u", row_dir, i);
1019 ret = write_file(filename, obj.data, obj.size);
1024 if (!t->num_disk_storage_columns)
1026 dsnc = t->disk_storage_name_column;
1027 ret = get_mapped_object(t, dsnc, row_num, &obj);
1030 hash_object(&obj, hash);
1031 ds_name = disk_storage_name_of_hash(t, hash);
1032 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
1033 filename = disk_storage_path(t, i, ds_name);
1034 ret = mmap_full_file(filename, O_RDONLY, &obj.data, &obj.size, NULL);
1038 filename = make_message("%s/col_%03u", row_dir, i);
1039 ret = write_file(filename, obj.data, obj.size);
1050 static int dump_rows(char *dump_dir, struct osl_table *t)
1053 char *current_dir = NULL;
1056 for (i = 0; i < t->num_rows; i++) {
1058 if (row_is_invalid(t, i))
1062 current_dir = make_message("%s/rows_%u-%u", dump_dir, i, i + 999);
1063 NOTICE_LOG("dumping rows %u - %u\n", i, i + 999);
1064 ret = osl_mkdir(current_dir, 0777);
1065 if (ret < 0 && ret != -E_OSL_DIR_EXISTS)
1068 row_dir = make_message("%s/row_%03u", current_dir, i);
1069 ret = osl_mkdir(row_dir, 0777);
1070 if (ret < 0 && ret != -E_OSL_DIR_EXISTS) {
1074 ret = dump_row(t, i, row_dir);
1084 static int dump_table(char *dump_dir, struct osl_table_description *desc)
1086 struct osl_table *t = NULL;
1087 int fd, ret = fsck_init(desc, &t);
1089 char *table_dump_dir = NULL;
1093 ret = osl_mkdir(dump_dir, 0777);
1094 if (ret < 0 && ret != -E_OSL_DIR_EXISTS)
1096 table_dump_dir = make_message("%s/%s", dump_dir, desc->name);
1097 ret = osl_mkdir(table_dump_dir, 0777);
1098 if (ret < 0 && ret != -E_OSL_DIR_EXISTS)
1100 desc_file = make_message("%s/table_description.c", table_dump_dir);
1101 ret = osl_open(desc_file, O_WRONLY | O_CREAT | O_EXCL, 0644);
1106 ret = dump_table_desc(t, fd);
1110 ret = dump_rows(table_dump_dir, t);
1112 free(table_dump_dir);
1117 static int fsck(struct osl_table_description *desc)
1120 struct osl_table *t = NULL;
1121 uint32_t *lost_bytes = NULL;
1123 ret = fsck_init(desc, &t);
1126 ret = check_index_ranges(t);
1129 ret = check_disk_storage_columns(t);
1132 ret = prune_invalid_rows_from_index(t);
1135 ret = check_for_invalid_objects(t, &lost_bytes);
1138 if (ret > 0) { /* at least one mapped data file needs pruning */
1139 ret = prune_objects(t, lost_bytes);
1145 unmap_table(t, OSL_MARK_CLEAN);
1151 static int check_table(char *db_dir, char *table_name)
1153 struct osl_table_description desc = {
1154 .column_descriptions = NULL,
1160 INFO_LOG("checking table %s\n", table_name);
1161 if (!conf.no_fsck_given) {
1167 if (!conf.dump_dir_given || !*conf.dump_dir_arg)
1169 ret = dump_table(conf.dump_dir_arg, &desc);
1172 ERROR_LOG("failed to check table %s\n", table_name);
1174 NOTICE_LOG("successfully checked table %s\n", table_name);
1178 static int check_all_tables(char *db_dir)
1181 struct dirent *entry;
1182 int cwd_fd, ret2, ret = fsck_opendir(db_dir, &dir, &cwd_fd);
1186 while ((entry = readdir(dir))) {
1189 if (!strcmp(entry->d_name, "."))
1191 if (!strcmp(entry->d_name, ".."))
1193 if (lstat(entry->d_name, &s) == -1)
1198 ret = check_table(db_dir, entry->d_name);
1203 ret2 = __fchdir(cwd_fd);
1204 if (ret2 < 0 && ret >= 0)
1210 int main(int argc, char **argv)
1213 struct fsck_cmdline_parser_params params = {
1216 .check_required = 1,
1217 .check_ambiguity = 1,
1220 ret = fsck_cmdline_parser_ext(argc, argv, &conf, ¶ms);
1223 ret = -E_FSCK_SYNTAX;
1226 loglevel = conf.loglevel_arg;
1227 HANDLE_VERSION_FLAG("fsck", conf);
1228 INFO_LOG("database dir: %s\n", conf.database_dir_arg);
1229 if (!conf.inputs_num) {
1230 ret = check_all_tables(conf.database_dir_arg);
1233 for (i = 0; i < conf.inputs_num; i++) {
1234 ret = check_table(conf.database_dir_arg, conf.inputs[i]);
1240 ERROR_LOG("%s\n", fsck_strerror(-ret));
1241 if (conf.loglevel_arg > 1)
1242 EMERG_LOG("re-run with \"--loglevel %d\" to increase verbosity\n",
1243 conf.loglevel_arg - 1);
1245 NOTICE_LOG("success\n");
1246 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;