2 * Copyright (C) 2007-2009 Andre Noll <maan@tuebingen.mpg.de>
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 "oslfsck.lsg.h"
22 static struct lls_parse_result *lpr;
23 #define CMD_PTR (lls_cmd(0, oslfsck_suite))
24 #define OPT_RESULT(_name) \
25 (lls_opt_result(LSG_OSLFSCK_OSLFSCK_OPT_ ## _name, lpr))
26 #define OPT_GIVEN(_name) (lls_opt_given(OPT_RESULT(_name)))
27 #define OPT_STRING_VAL(_name) (lls_string_val(0, OPT_RESULT(_name)))
28 #define OPT_UINT32_VAL(_name) (lls_uint32_val(0, OPT_RESULT(_name)))
31 FSCK_ERROR(RANGE_VIOLATION, "range violation detected, very bad"), \
32 FSCK_ERROR(NOT_A_REGULAR_FILE, "not a regular file"), \
33 FSCK_ERROR(SYNTAX, "fsck syntax error"), \
34 FSCK_ERROR(ACCESS, "permission denied"), \
35 FSCK_ERROR(CHDIR, "could not change directory"), \
36 FSCK_ERROR(OPENDIR, "could not open directory"),
38 #define FSCK_ERROR_BIT 29
39 #define FSCK_ERROR(num, txt) E_FSCK_ ## num
41 FSCK_DUMMY = (1 << FSCK_ERROR_BIT) - 1,
45 #define FSCK_ERROR(num, txt) txt
46 static const char *fsck_errors[] = {
50 static const char *fsck_strerror(int num)
52 if (num & (1 << FSCK_ERROR_BIT))
53 return fsck_errors[num & ((1 << FSCK_ERROR_BIT) - 1)];
54 return osl_strerror(num);
60 static signed char hexval_table[256] = {
61 -1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */
62 -1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */
63 -1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */
64 -1, -1, -1, -1, -1, -1, -1, -1, /* 18-1f */
65 -1, -1, -1, -1, -1, -1, -1, -1, /* 20-27 */
66 -1, -1, -1, -1, -1, -1, -1, -1, /* 28-2f */
67 0, 1, 2, 3, 4, 5, 6, 7, /* 30-37 */
68 8, 9, -1, -1, -1, -1, -1, -1, /* 38-3f */
69 -1, 10, 11, 12, 13, 14, 15, -1, /* 40-47 */
70 -1, -1, -1, -1, -1, -1, -1, -1, /* 48-4f */
71 -1, -1, -1, -1, -1, -1, -1, -1, /* 50-57 */
72 -1, -1, -1, -1, -1, -1, -1, -1, /* 58-5f */
73 -1, 10, 11, 12, 13, 14, 15, -1, /* 60-67 */
74 -1, -1, -1, -1, -1, -1, -1, -1, /* 68-67 */
75 -1, -1, -1, -1, -1, -1, -1, -1, /* 70-77 */
76 -1, -1, -1, -1, -1, -1, -1, -1, /* 78-7f */
77 -1, -1, -1, -1, -1, -1, -1, -1, /* 80-87 */
78 -1, -1, -1, -1, -1, -1, -1, -1, /* 88-8f */
79 -1, -1, -1, -1, -1, -1, -1, -1, /* 90-97 */
80 -1, -1, -1, -1, -1, -1, -1, -1, /* 98-9f */
81 -1, -1, -1, -1, -1, -1, -1, -1, /* a0-a7 */
82 -1, -1, -1, -1, -1, -1, -1, -1, /* a8-af */
83 -1, -1, -1, -1, -1, -1, -1, -1, /* b0-b7 */
84 -1, -1, -1, -1, -1, -1, -1, -1, /* b8-bf */
85 -1, -1, -1, -1, -1, -1, -1, -1, /* c0-c7 */
86 -1, -1, -1, -1, -1, -1, -1, -1, /* c8-cf */
87 -1, -1, -1, -1, -1, -1, -1, -1, /* d0-d7 */
88 -1, -1, -1, -1, -1, -1, -1, -1, /* d8-df */
89 -1, -1, -1, -1, -1, -1, -1, -1, /* e0-e7 */
90 -1, -1, -1, -1, -1, -1, -1, -1, /* e8-ef */
91 -1, -1, -1, -1, -1, -1, -1, -1, /* f0-f7 */
92 -1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */
95 static int asc_to_hash(const char *asc_hash, int len, HASH_TYPE *hash)
98 const unsigned char *asc = (const unsigned char *) asc_hash;
100 while (*asc && i++ < len) {
101 unsigned int val = (hexval_table[asc[0]] << 4) | hexval_table[asc[1]];
111 static int _write_all(int fd, const char *buf, size_t len)
113 return write_all(fd, buf, &len);
117 * Wrapper for malloc().
119 * \param size The desired new size.
121 * A wrapper for malloc(3) which exits on errors.
123 * \return A pointer to the allocated memory, which is suitably aligned for any
128 __must_check __malloc static void *fsck_malloc(size_t size)
131 void *p = malloc(size);
134 EMERG_LOG("malloc failed (size = %zu), aborting\n",
142 * Allocate memory and fill with zeros.
144 * \param size The desired new size.
146 * A wrapper for calloc(3) which exits on errors.
148 * \return A pointer to the allocated and zeroed-out memory, which is suitably
149 * aligned for any kind of variable.
153 __must_check __malloc static void *fsck_calloc(size_t size)
155 void *ret = fsck_malloc(size);
157 memset(ret, 0, size);
162 * Save version of strdup().
164 * \param s The string to be duplicated.
166 * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
167 * there is no need to check the return value in the caller.
169 * \return A pointer to the duplicated string. If \p s was the NULL pointer,
170 * an pointer to an empty string is returned.
174 __must_check __malloc static char *fsck_strdup(const char *s)
178 if ((ret = strdup(s? s: "")))
180 EMERG_LOG("strdup failed, aborting\n");
185 * Compare two osl objects pointing to unsigned integers of 32 bit size.
187 * \param obj1 Pointer to the first integer.
188 * \param obj2 Pointer to the second integer.
190 * \return The values required for an osl compare function.
192 * \sa osl_compare_func, osl_hash_compare().
194 static int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2)
196 uint32_t d1 = read_u32((const char *)obj1->data);
197 uint32_t d2 = read_u32((const char *)obj2->data);
207 * A wrapper for fchdir().
209 * \param fd An open file descriptor.
213 static inline int __fchdir(int fd)
217 return errno == EACCES? -E_FSCK_ACCESS : -E_FSCK_CHDIR;
221 * Wrapper for chdir(2).
223 * \param path The specified directory.
227 _static_inline_ int __chdir(const char *path)
229 if (chdir(path) >= 0)
231 return errno == EACCES? -E_FSCK_ACCESS : -E_FSCK_CHDIR;
235 * Save the cwd and open a given directory.
237 * \param dirname Path to the directory to open.
238 * \param dir Result pointer.
239 * \param cwd File descriptor of the current working directory.
243 * Opening the current directory (".") and calling fchdir() to return is
244 * usually faster and more reliable than saving cwd in some buffer and calling
245 * chdir() afterwards.
247 * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
248 * stored in \a cwd. If the function returns success, and \a cwd is not \p
249 * NULL, the caller must close this file descriptor (probably after calling
252 * On errors, the function undos everything, so the caller needs neither close
253 * any files, nor change back to the original working directory.
258 static int fsck_opendir(const char *dirname, DIR **dir, int *cwd)
263 ret = osl_open(".", O_RDONLY, 0);
268 ret = __chdir(dirname);
274 ret = errno == EACCES? -E_FSCK_ACCESS : -E_FSCK_OPENDIR;
275 /* Ignore return value of fchdir() and close(). We're busted anyway. */
277 int __a_unused ret2 = fchdir(*cwd); /* STFU, gcc */
288 * Traverse the given directory recursively.
290 * \param dirname The directory to traverse.
291 * \param func The function to call for each entry.
292 * \param private_data Pointer to an arbitrary data structure.
294 * For each regular file under \a dirname, the supplied function \a func is
295 * called. The full path of the regular file and the \a private_data pointer
296 * are passed to \a func. Directories for which the calling process has no
297 * permissions to change to are silently ignored.
301 static int for_each_file_in_dir(const char *dirname,
302 int (*func)(const char *, void *), void *private_data)
305 struct dirent *entry;
306 int cwd_fd, ret2, ret = fsck_opendir(dirname, &dir, &cwd_fd);
309 return ret == -E_FSCK_ACCESS? 1 : ret;
310 /* scan cwd recursively */
311 while ((entry = readdir(dir))) {
316 if (!strcmp(entry->d_name, "."))
318 if (!strcmp(entry->d_name, ".."))
320 if (lstat(entry->d_name, &s) == -1)
323 if (!S_ISREG(m) && !S_ISDIR(m))
325 tmp = make_message("%s/%s", dirname, entry->d_name);
327 ret = func(tmp, private_data);
334 ret = for_each_file_in_dir(tmp, func, private_data);
342 ret2 = __fchdir(cwd_fd);
343 if (ret2 < 0 && ret >= 0)
350 * check for object boundary violations
352 * test whether the range pointed to by the index entry for a given cell is
353 * contained in mapped data file. This should always be the case. Otherwise
354 * we are in real trouble.
356 static int check_range(struct osl_table *t, uint32_t row_num, uint32_t col_num)
359 struct osl_object obj;
360 struct osl_column *col;
362 char *map_start, *obj_start;
364 ret = get_cell_index(t, row_num, col_num, &index_entry);
367 ret = get_mapped_object(t, col_num, row_num, &obj);
370 col = t->columns + col_num;
371 obj_start = obj.data;
372 map_start = col->data_map.data;
373 // INFO_LOG("obj: %p..%p\n", obj_start, obj_start + obj.size);
374 // INFO_LOG("map: %p..%p\n", map_start, map_start + col->data_map.size);
375 if (obj_start < map_start || obj_start + obj.size > map_start + col->data_map.size) {
376 CRIT_LOG("range violation in row %u, col %u\n", row_num,
378 return -E_FSCK_RANGE_VIOLATION;
380 DEBUG_LOG("col %u: ok\n", col_num);
384 static int fsck_mark_row_invalid(struct osl_table *t, int i)
386 if (OPT_GIVEN(DRY_RUN))
388 return mark_row_invalid(t, i);
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 const struct osl_column_description *cd;
404 if (row_is_invalid(t, i)) {
405 t->num_invalid_rows++;
408 FOR_EACH_MAPPED_COLUMN(j, t, cd) {
409 ret = check_range(t, i, j);
411 if (ret != -E_FSCK_RANGE_VIOLATION)
413 ret = fsck_mark_row_invalid(t, i);
416 t->num_invalid_rows++;
422 if (t->num_invalid_rows)
423 NOTICE_LOG("ranges OK. %d invalid row(s) detected\n",
424 t->num_invalid_rows);
426 INFO_LOG("no invalid rows, no range violations, good\n");
432 static int move_index_entry(struct osl_table *t, uint32_t dest, uint32_t src)
434 char *dest_ie, *src_ie;
435 int ret = get_row_index(t, dest, &dest_ie);
439 ret = get_row_index(t, src, &src_ie);
442 INFO_LOG("moving entry #%u to position %u\n", src, dest);
443 memcpy(dest_ie, src_ie, t->row_index_size);
447 static int map_index(const struct osl_table_description *desc, struct osl_object *map)
449 char *filename = index_filename(desc);
452 ret = mmap_full_file(filename, O_RDWR, &map->data, &map->size, NULL);
453 DEBUG_LOG("mapping index %s: ret: %d, size: %zu\n", filename, ret, map->size);
458 static int prune_invalid_rows_from_index(struct osl_table *t)
460 uint32_t top = 0, bottom;
464 if (!t->num_invalid_rows) {
465 INFO_LOG("all rows are valid, good\n");
468 NOTICE_LOG("index contains %u invalid row(s) (%d bytes)\n",
469 t->num_invalid_rows, t->row_index_size * t->num_invalid_rows);
470 if (OPT_GIVEN(DRY_RUN))
472 NOTICE_LOG("removing invalid rows from index\n");
473 bottom = t->num_rows - 1;
474 while (top < bottom) {
475 if (!row_is_invalid(t, top)) {
479 while (bottom > top) {
480 if (row_is_invalid(t, bottom)) {
484 /* move bottom index entry to top */
485 move_index_entry(t, top, bottom);
491 DEBUG_LOG("unmapping index\n");
492 osl_munmap(t->index_map.data, t->index_map.size);
493 filename = index_filename(t->desc);
494 ret = truncate_file(filename, t->row_index_size
495 * t->num_invalid_rows);
499 ret = map_index(t->desc, &t->index_map);
502 t->num_rows = table_num_rows(t);
506 static int check_for_invalid_objects(struct osl_table *t, uint32_t **lost_bytes)
509 const struct osl_column_description *cd;
510 uint32_t *loss = fsck_malloc(sizeof(uint32_t) * t->desc->num_columns);
512 INFO_LOG("looking for mapped objects not contained in index\n");
513 /* first count used bytes */
514 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
515 loss[i] = t->columns[i].data_map.size;
516 DEBUG_LOG("column %i data map: %zu bytes\n", i,
517 t->columns[i].data_map.size);
518 for (j = 0; j < t->num_rows; j++) {
519 struct osl_object obj;
520 ret = get_mapped_object(t, i, j, &obj);
527 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
529 NOTICE_LOG("column %u contains %u lost bytes\n",
535 INFO_LOG("all mapped objects are valid, good\n");
543 /* prune_invalid_rows() must be run on the table before calling this */
544 static int prune_mapped_column(struct osl_table *t, uint32_t col_num, int fd)
547 uint32_t written = 0;
548 struct osl_column *col = t->columns + col_num;
550 INFO_LOG("pruning col %u\n", col_num);
551 for (i = 0; i < t->num_rows; i++) {
552 struct osl_object obj;
555 DEBUG_LOG("checking row %u/%u\n", i, t->num_rows);
556 ret = get_mapped_object(t, col_num, i, &obj);
559 ret = _write_all(fd, (char *)(obj.data), obj.size);
563 ret = get_row_index(t, i, &index_entry);
566 update_cell_index(index_entry, col, written, obj.size);
571 static int prune_objects(struct osl_table *t, uint32_t *lost_bytes)
574 const struct osl_column_description *cd;
575 char **col_filenames = fsck_calloc(t->desc->num_columns * sizeof(char *));
576 char **new_col_filenames = fsck_calloc(t->desc->num_columns * sizeof(char *));
577 char *idx_filename = index_filename(t->desc);
578 char *old_idx_filename = make_message("%s.bak", idx_filename);
581 NOTICE_LOG("removing unreferenced objects from data files\n");
582 /* first make a copy of the index */
583 DEBUG_LOG("opening %s\n", old_idx_filename);
584 ret = osl_open(old_idx_filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
588 ret = _write_all(fd, t->index_map.data, t->index_map.size);
592 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
595 col_filenames[i] = column_filename(t, i);
596 new_col_filenames[i] = make_message("%s.fsck", col_filenames[i]);
597 ret = osl_open(new_col_filenames[i], O_WRONLY | O_CREAT | O_EXCL, 0644);
599 goto out_unlink_data;
601 ret = prune_mapped_column(t, i, fd);
604 goto out_unlink_data;
606 ret = unmap_table(t, OSL_MARK_CLEAN);
608 goto out_unlink_data;
609 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
612 ret = osl_rename(new_col_filenames[i], col_filenames[i]);
613 if (ret < 0) { /* we're kinda screwed here */
614 CRIT_LOG("rename of col %i failed: %s\n", i,
615 osl_strerror(errno));
619 unlink(old_idx_filename);
620 ret = map_table(t, 0);
623 FOR_EACH_MAPPED_COLUMN(i, t, cd)
624 unlink(new_col_filenames[i]);
626 free(old_idx_filename);
628 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
629 free(col_filenames[i]);
630 free(new_col_filenames[i]);
633 free(new_col_filenames);
637 static struct osl_column_description hash_tree_table_cols[] = {
639 .storage_type = OSL_NO_STORAGE,
640 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
642 .compare_function = uint32_compare,
643 .data_size = HASH_SIZE
647 static const struct osl_table_description hash_tree_table_desc = {
648 .dir = "/", /* irrelevant */
652 .column_descriptions = hash_tree_table_cols
656 * The hash_tree table contains all hashes of the disk storage name column.
657 * of each row. It is used for checking if a disk storage file has a reference
660 static struct osl_table *hash_tree_table;
661 static HASH_TYPE *hashes;
663 static int check_disk_storage_column(struct osl_table *t, int row_num,
664 int col_num, char *ds_name, unsigned *num_missing_objects)
668 char *path = disk_storage_path(t, col_num, ds_name);
669 unsigned dsnc = t->disk_storage_name_column;
670 struct osl_object obj;
672 DEBUG_LOG("checking if %s is a regular file\n", path);
673 ret = stat(path, &statbuf);
674 if (ret < 0 && errno == ENOENT) {
676 (*num_missing_objects)++;
677 ERROR_LOG("row %d: object %s is missing\n", row_num, path);
679 if (OPT_GIVEN(DRY_RUN))
681 NOTICE_LOG("trying to delete row %d\n", row_num);
682 ret = osl_get_row(t, dsnc, &obj, &row);
684 CRIT_LOG("unable to get row %d\n", row_num);
685 fsck_mark_row_invalid(t, row_num);
686 CRIT_LOG("Please re-run fsck\n");
689 ret = osl_del_row(t, row);
697 ret = -E_FSCK_NOT_A_REGULAR_FILE;
698 if (!(S_IFREG & statbuf.st_mode))
703 static int check_disk_storage_presence(struct osl_table *t)
706 struct osl_object obj, hash_obj = {.size = HASH_SIZE};
708 const struct osl_column_description *cd;
709 unsigned dsnc = t->disk_storage_name_column, missing_objects = 0;
713 hashes = fsck_malloc(t->num_rows * HASH_SIZE);
714 INFO_LOG("looking for missing disk storage objects\n");
715 for (i = 0; i < t->num_rows; i++) {
716 if (row_is_invalid(t, i))
718 ret = get_mapped_object(t, dsnc, i, &obj);
721 hash_object(&obj, hashes + i * HASH_SIZE);
722 hash_obj.data = hashes + i * HASH_SIZE;
723 osl_add_row(hash_tree_table, &hash_obj);
724 ds_name = disk_storage_name_of_hash(t, hashes + i * HASH_SIZE);
725 FOR_EACH_DISK_STORAGE_COLUMN(j, t, cd) {
726 ret = check_disk_storage_column(t, i, j, ds_name,
733 if (!missing_objects)
734 INFO_LOG("all referenced disk storage objects exist, good\n");
736 NOTICE_LOG("%d missing object(s)\n", missing_objects);
737 return missing_objects;
743 static int dummy_compare(const struct osl_object *obj1, const struct osl_object *obj2)
752 static unsigned files_pruned;
754 static int prune_disk_storage_file(const char *path, void *private_data)
756 HASH_TYPE hash[HASH_SIZE];
757 uint8_t flags = *(uint8_t *)private_data;
758 struct osl_object obj = {.data = hash, .size = HASH_SIZE};
761 size_t len = strlen(path);
764 DEBUG_LOG("path: %s\n", path);
765 if (flags & OSL_LARGE_TABLE) {
766 if (len < HASH_SIZE * 2 + 2)
768 // NOTICE_LOG("p: %s\n", path + len - 2 * HASH_SIZE - 1);
769 ret = asc_to_hash(path + len - 2 * HASH_SIZE - 1, 1, hash);
772 ret = asc_to_hash(path + len - 2 * HASH_SIZE + 2, HASH_SIZE - 1,
776 // INFO_LOG("high: %x, low: %x, hash: %x\n", high, low, hash);
778 if (len < 2 * HASH_SIZE + 1)
780 ret = asc_to_hash(path + len - 2 * HASH_SIZE, 2 * HASH_SIZE, hash);
783 // INFO_LOG("hash: %x\n", hash);
787 char asc[2 * HASH_SIZE + 1];
788 hash_to_asc(hash, asc);
789 NOTICE_LOG("before: %s\nafter: %s\n", path, asc);
792 ret = osl_get_row(hash_tree_table, 0, &obj, &row);
795 NOTICE_LOG("unreferenced file in hash dir: %s\n", path);
798 ERROR_LOG("could not read hash value of %s\n", path);
800 if (OPT_GIVEN(DRY_RUN))
802 NOTICE_LOG("removing %s\n", path);
808 static int prune_disk_storage_files(struct osl_table *t)
811 const struct osl_column_description *cd;
813 INFO_LOG("looking for unreferenced disk storage files\n");
814 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
815 char *dirname = column_filename(t, i);
816 uint8_t flags = t->desc->flags;
817 ret = for_each_file_in_dir(dirname, prune_disk_storage_file,
822 NOTICE_LOG("%u disk storage files deleted\n",
825 INFO_LOG("all files are are referenced, good\n");
829 static int check_disk_storage_columns(struct osl_table *t)
832 const struct osl_column_description *cd;
834 if (!t->num_disk_storage_columns) {
835 INFO_LOG("no disk storage columns in table '%s', "
836 "skipping checks\n", t->desc->name);
839 FOR_EACH_COLUMN(i, t->desc, cd)
840 t->desc->column_descriptions[i].compare_function = dummy_compare;
841 ret = init_rbtrees(t);
844 INFO_LOG("creating rbtree for disk storage hash values\n");
845 ret = osl_open_table(&hash_tree_table_desc, &hash_tree_table);
848 ret = check_disk_storage_presence(t);
850 goto out_close_hash_tree;
851 ret = prune_disk_storage_files(t);
853 osl_close_table(hash_tree_table, 0);
857 clear_rbtrees(t); /* TODO why are we doing that here? Seems odd */
861 static void set_dummy_contents(struct osl_table_description *desc)
864 struct osl_column_description *cd;
866 for (i = 0; i < desc->num_columns; i++) {
867 cd = get_column_description(desc, i);
868 cd->compare_function = dummy_compare;
872 static int fsck_init(struct osl_table_description *desc, struct osl_table **t)
874 struct osl_object map;
875 int ret = map_index(desc, &map);
879 ret = read_table_desc(&map, desc);
881 osl_munmap(map.data, map.size);
884 set_dummy_contents(desc);
885 ret = init_table_structure(desc, t);
887 osl_munmap(map.data, map.size);
890 DEBUG_LOG("unmapping index\n");
891 osl_munmap(map.data, map.size);
892 if (OPT_GIVEN(FORCE))
893 ret = map_table(*t, (MAP_TBL_FL_IGNORE_DIRTY));
895 ret = map_table(*t, 0);
897 (*t)->num_rows = table_num_rows(*t);
898 DEBUG_LOG("index header size: %d\n", (*t)->index_header_size);
899 DEBUG_LOG("row index size: %d\n", (*t)->row_index_size);
905 static void fsck_cleanup(struct osl_table *t)
911 if (t->desc->column_descriptions) {
912 struct osl_column_description *cd;
913 for (i = 0; i < t->desc->num_columns; i++) {
914 cd = get_column_description(t->desc, i);
915 free((char*)cd->name);
917 free(t->desc->column_descriptions);
924 #define ST_CASE(st) case st: return #st
926 static const char *get_asc_storage_type(enum osl_storage_type st)
929 ST_CASE(OSL_MAPPED_STORAGE);
930 ST_CASE(OSL_DISK_STORAGE);
931 ST_CASE(OSL_NO_STORAGE);
936 #define APPEND_ASC_SF(sf, flag, str) do { if (sf & flag) { \
937 if (str) str = make_message("%s%s", str, " | " # flag); \
938 else str = fsck_strdup(#flag); }} while (0)
941 static char *get_asc_storage_flags(enum osl_storage_type sf)
945 APPEND_ASC_SF(sf, OSL_RBTREE, asc_sf);
946 APPEND_ASC_SF(sf, OSL_FIXED_SIZE, asc_sf);
947 APPEND_ASC_SF(sf, OSL_UNIQUE, asc_sf);
951 static int dump_table_desc(struct osl_table *t, int fd)
953 const struct osl_table_description *desc = t->desc;
955 struct osl_column_description *cd;
956 char *msg = make_message("static struct osl_column_description cols[] = {\n");
957 ret = _write_all(fd, msg, strlen(msg));
961 FOR_EACH_COLUMN(i, desc, cd) {
963 msg = make_message("\t[%d] = {\n", i);
964 ret = _write_all(fd, msg, strlen(msg));
968 asc_st = get_asc_storage_type(cd->storage_type);
969 msg = make_message("\t\t.storage_type = %s,\n", asc_st);
970 ret = _write_all(fd, msg, strlen(msg));
974 if (cd->storage_flags) {
975 char *asc_sf = get_asc_storage_flags(cd->storage_flags);
976 msg = make_message("\t\t,storage_flags = %s,\n", asc_sf);
978 ret = _write_all(fd, msg, strlen(msg));
983 if (cd->storage_flags & OSL_FIXED_SIZE) {
984 msg = make_message("\t\t.data_size = %u,\n", cd->data_size);
985 ret = _write_all(fd, msg, strlen(msg));
990 msg = make_message("\t\t.name = \"%s\",\n", cd->name);
991 ret = _write_all(fd, msg, strlen(msg));
995 if (cd->storage_flags & OSL_RBTREE) {
996 msg = make_message("\t\t.compare_function = compare_func,\n");
997 ret = _write_all(fd, msg, strlen(msg));
1002 msg = make_message("\t},\n");
1003 ret = _write_all(fd, msg, strlen(msg));
1008 msg = make_message("};\n");
1009 ret = _write_all(fd, msg, strlen(msg));
1016 static int dump_row(struct osl_table *t, unsigned row_num, const char *row_dir)
1019 const struct osl_column_description *cd;
1021 struct osl_object obj;
1023 HASH_TYPE hash[HASH_SIZE];
1026 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
1027 ret = get_mapped_object(t, i, row_num, &obj);
1030 filename = make_message("%s/col_%03u", row_dir, i);
1031 ret = write_file(filename, obj.data, obj.size);
1036 if (!t->num_disk_storage_columns)
1038 dsnc = t->disk_storage_name_column;
1039 ret = get_mapped_object(t, dsnc, row_num, &obj);
1042 hash_object(&obj, hash);
1043 ds_name = disk_storage_name_of_hash(t, hash);
1044 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
1045 filename = disk_storage_path(t, i, ds_name);
1046 ret = mmap_full_file(filename, O_RDONLY, &obj.data, &obj.size, NULL);
1050 filename = make_message("%s/col_%03u", row_dir, i);
1051 ret = write_file(filename, obj.data, obj.size);
1062 static int dump_rows(char *dump_dir, struct osl_table *t)
1065 char *current_dir = NULL;
1068 for (i = 0; i < t->num_rows; i++) {
1070 if (row_is_invalid(t, i))
1074 current_dir = make_message("%s/rows_%u-%u", dump_dir, i, i + 999);
1075 NOTICE_LOG("dumping rows %u - %u\n", i, i + 999);
1076 ret = osl_mkdir(current_dir, 0777);
1077 if (ret < 0 && ret != -E_OSL_DIR_EXISTS)
1080 row_dir = make_message("%s/row_%03u", current_dir, i);
1081 ret = osl_mkdir(row_dir, 0777);
1082 if (ret < 0 && ret != -E_OSL_DIR_EXISTS) {
1086 ret = dump_row(t, i, row_dir);
1096 static int dump_table(const char *dump_dir, struct osl_table_description *desc)
1098 struct osl_table *t = NULL;
1099 int fd, ret = fsck_init(desc, &t);
1101 char *table_dump_dir = NULL;
1105 ret = osl_mkdir(dump_dir, 0777);
1106 if (ret < 0 && ret != -E_OSL_DIR_EXISTS)
1108 table_dump_dir = make_message("%s/%s", dump_dir, desc->name);
1109 ret = osl_mkdir(table_dump_dir, 0777);
1110 if (ret < 0 && ret != -E_OSL_DIR_EXISTS)
1112 desc_file = make_message("%s/table_description.c", table_dump_dir);
1113 ret = osl_open(desc_file, O_WRONLY | O_CREAT | O_EXCL, 0644);
1118 ret = dump_table_desc(t, fd);
1122 ret = dump_rows(table_dump_dir, t);
1124 free(table_dump_dir);
1129 static int fsck(struct osl_table_description *desc)
1132 struct osl_table *t = NULL;
1133 uint32_t *lost_bytes = NULL;
1135 ret = fsck_init(desc, &t);
1138 ret = check_index_ranges(t);
1141 ret = check_disk_storage_columns(t);
1144 ret = prune_invalid_rows_from_index(t);
1147 ret = check_for_invalid_objects(t, &lost_bytes);
1150 if (ret > 0 && !OPT_GIVEN(DRY_RUN)) {
1151 /* at least one mapped data file needs pruning */
1152 ret = prune_objects(t, lost_bytes);
1158 unmap_table(t, OSL_MARK_CLEAN);
1164 static int check_table(const char *db_dir, const char *table_name)
1166 struct osl_table_description desc = {
1167 .column_descriptions = NULL,
1173 INFO_LOG("checking table %s\n", table_name);
1174 if (!OPT_GIVEN(NO_FSCK)) {
1180 if (!OPT_GIVEN(DUMP_DIR) || !*OPT_STRING_VAL(DUMP_DIR))
1182 ret = dump_table(OPT_STRING_VAL(DUMP_DIR), &desc);
1185 ERROR_LOG("failed to check table %s: %s\n", table_name,
1186 fsck_strerror(-ret));
1188 NOTICE_LOG("successfully checked table %s\n", table_name);
1192 static int check_all_tables(const char *db_dir)
1195 struct dirent *entry;
1196 int cwd_fd, ret2, ret = fsck_opendir(db_dir, &dir, &cwd_fd);
1200 while ((entry = readdir(dir))) {
1203 if (!strcmp(entry->d_name, "."))
1205 if (!strcmp(entry->d_name, ".."))
1207 if (lstat(entry->d_name, &s) == -1)
1212 ret = check_table(db_dir, entry->d_name);
1217 ret2 = __fchdir(cwd_fd);
1218 if (ret2 < 0 && ret >= 0)
1224 int main(int argc, char **argv)
1227 char *errctx = NULL;
1231 ret = lls_parse(argc, argv, CMD_PTR, &lpr, &errctx);
1234 ERROR_LOG("%s\n", errctx);
1235 EMERG_LOG("%s\n", lls_strerror(-ret));
1238 loglevel = OPT_UINT32_VAL(LOGLEVEL);
1239 if (OPT_GIVEN(DETAILED_HELP)) {
1240 printf("%s\n", lls_long_help(CMD_PTR));
1243 if (OPT_GIVEN(HELP)) {
1244 printf("%s\n", lls_short_help(CMD_PTR));
1247 if (OPT_GIVEN(VERSION)) {
1249 "oslfsck " OSL_VERSION "\n"
1250 "Copyright (C) 2008-2009 Andre Noll\n"
1251 "This is free software with ABSOLUTELY NO WARRANTY."
1252 " See COPYING for details.\n"
1253 "Written by Andre Noll.\n"
1254 "Report bugs to <maan@tuebingen.mpg.de>.\n"
1258 dd = OPT_STRING_VAL(DATABASE_DIR);
1260 EMERG_LOG("--database-dir: mandatory option not given\n");
1263 INFO_LOG("database dir: %s\n", dd);
1264 if (lls_num_inputs(lpr) == 0) {
1265 ret = check_all_tables(dd);
1268 for (i = 0; i < lls_num_inputs(lpr); i++) {
1269 ret = check_table(dd, lls_input(i, lpr));
1275 ERROR_LOG("%s\n", fsck_strerror(-ret));
1277 EMERG_LOG("re-run with \"--loglevel %d\" to increase verbosity\n",
1280 NOTICE_LOG("success\n");
1281 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;