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. */
9 /* FIXME: check error codes of make_message or write wrapper */
14 #include <sys/types.h>
24 #include "fsck.cmdline.h"
26 /** version text used by various commands if -V switch was given */
27 #define VERSION_TEXT(prefix) "osl_" prefix " " VERSION " " "\n" \
28 "Copyright (C) 2008 Andre Noll\n" \
29 "This is free software with ABSOLUTELY NO WARRANTY." \
30 " See COPYING for details.\n" \
31 "Written by Andre Noll.\n" \
32 "Report bugs to <maan@systemlinux.org>.\n"
34 /** print out \p VERSION_TEXT and exit if version flag was given */
35 #define HANDLE_VERSION_FLAG(_prefix, _args_info_struct) \
36 if (_args_info_struct.version_given) { \
37 printf("%s", VERSION_TEXT(_prefix)); \
41 static struct fsck_args_info conf;
44 FSCK_ERROR(RANGE_VIOLATION, "range violation detected, very bad"), \
45 FSCK_ERROR(NOT_A_REGULAR_FILE, "not a regular file"), \
46 FSCK_ERROR(SYNTAX, "fsck syntax error"),
48 #define FSCK_ERROR(num, txt) E_FSCK_ ## num
50 FSCK_DUMMY = (1 << FSCK_ERROR_BIT) - 1,
54 #define FSCK_ERROR(num, txt) txt
55 const char const *fsck_errors[] = {
59 static const char *fsck_strerror(int num)
61 if (num & (1 << FSCK_ERROR_BIT))
62 return fsck_errors[num & ((1 << FSCK_ERROR_BIT) - 1)];
63 return osl_strerror(num);
69 signed char hexval_table[256] = {
70 -1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */
71 -1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */
72 -1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */
73 -1, -1, -1, -1, -1, -1, -1, -1, /* 18-1f */
74 -1, -1, -1, -1, -1, -1, -1, -1, /* 20-27 */
75 -1, -1, -1, -1, -1, -1, -1, -1, /* 28-2f */
76 0, 1, 2, 3, 4, 5, 6, 7, /* 30-37 */
77 8, 9, -1, -1, -1, -1, -1, -1, /* 38-3f */
78 -1, 10, 11, 12, 13, 14, 15, -1, /* 40-47 */
79 -1, -1, -1, -1, -1, -1, -1, -1, /* 48-4f */
80 -1, -1, -1, -1, -1, -1, -1, -1, /* 50-57 */
81 -1, -1, -1, -1, -1, -1, -1, -1, /* 58-5f */
82 -1, 10, 11, 12, 13, 14, 15, -1, /* 60-67 */
83 -1, -1, -1, -1, -1, -1, -1, -1, /* 68-67 */
84 -1, -1, -1, -1, -1, -1, -1, -1, /* 70-77 */
85 -1, -1, -1, -1, -1, -1, -1, -1, /* 78-7f */
86 -1, -1, -1, -1, -1, -1, -1, -1, /* 80-87 */
87 -1, -1, -1, -1, -1, -1, -1, -1, /* 88-8f */
88 -1, -1, -1, -1, -1, -1, -1, -1, /* 90-97 */
89 -1, -1, -1, -1, -1, -1, -1, -1, /* 98-9f */
90 -1, -1, -1, -1, -1, -1, -1, -1, /* a0-a7 */
91 -1, -1, -1, -1, -1, -1, -1, -1, /* a8-af */
92 -1, -1, -1, -1, -1, -1, -1, -1, /* b0-b7 */
93 -1, -1, -1, -1, -1, -1, -1, -1, /* b8-bf */
94 -1, -1, -1, -1, -1, -1, -1, -1, /* c0-c7 */
95 -1, -1, -1, -1, -1, -1, -1, -1, /* c8-cf */
96 -1, -1, -1, -1, -1, -1, -1, -1, /* d0-d7 */
97 -1, -1, -1, -1, -1, -1, -1, -1, /* d8-df */
98 -1, -1, -1, -1, -1, -1, -1, -1, /* e0-e7 */
99 -1, -1, -1, -1, -1, -1, -1, -1, /* e8-ef */
100 -1, -1, -1, -1, -1, -1, -1, -1, /* f0-f7 */
101 -1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */
104 int asc_to_hash(const char *asc_hash, int len, HASH_TYPE *hash)
107 const unsigned char *asc = (const unsigned char *) asc_hash;
109 while (*asc && i++ < len) {
110 unsigned int val = (hexval_table[asc[0]] << 4) | hexval_table[asc[1]];
120 static int _write_all(int fd, const char *buf, size_t len)
122 return write_all(fd, buf, &len);
126 * Paraslash's version of malloc().
128 * \param size The desired new size.
130 * A wrapper for malloc(3) which exits on errors.
132 * \return A pointer to the allocated memory, which is suitably aligned for any
137 __must_check __malloc static void *para_malloc(size_t size)
140 void *p = malloc(size);
143 EMERG_LOG("malloc failed (size = %zu), aborting\n",
151 * Paraslash's version of calloc().
153 * \param size The desired new size.
155 * A wrapper for calloc(3) which exits on errors.
157 * \return A pointer to the allocated and zeroed-out memory, which is suitably
158 * aligned for any kind of variable.
162 __must_check __malloc static void *para_calloc(size_t size)
164 void *ret = para_malloc(size);
166 memset(ret, 0, size);
171 * Paraslash's version of strdup().
173 * \param s The string to be duplicated.
175 * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
176 * there is no need to check the return value in the caller.
178 * \return A pointer to the duplicated string. If \p s was the NULL pointer,
179 * an pointer to an empty string is returned.
183 __must_check __malloc static char *para_strdup(const char *s)
187 if ((ret = strdup(s? s: "")))
189 EMERG_LOG("strdup failed, aborting\n");
194 * Get the home directory of the current user.
196 * \return A dynammically allocated string that must be freed by the caller. If
197 * the home directory could not be found, this function returns "/tmp".
199 __must_check __malloc static char *para_homedir(void)
201 struct passwd *pw = getpwuid(getuid());
202 return para_strdup(pw? pw->pw_dir : "/tmp");
206 * Compare two osl objects pointing to unsigned integers of 32 bit size.
208 * \param obj1 Pointer to the first integer.
209 * \param obj2 Pointer to the second integer.
211 * \return The values required for an osl compare function.
213 * \sa osl_compare_func, osl_hash_compare().
215 static int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2)
217 uint32_t d1 = read_u32((const char *)obj1->data);
218 uint32_t d2 = read_u32((const char *)obj2->data);
228 * Traverse the given directory recursively.
230 * \param dirname The directory to traverse.
231 * \param func The function to call for each entry.
232 * \param private_data Pointer to an arbitrary data structure.
234 * For each regular file under \a dirname, the supplied function \a func is
235 * called. The full path of the regular file and the \a private_data pointer
236 * are passed to \a func. Directories for which the calling process has no
237 * permissions to change to are silently ignored.
241 static int for_each_file_in_dir(const char *dirname,
242 int (*func)(const char *, void *), void *private_data)
245 struct dirent *entry;
246 int cwd_fd, ret2, ret = para_opendir(dirname, &dir, &cwd_fd);
249 return ret == -ERRNO_TO_ERROR(EACCES)? 1 : ret;
250 /* scan cwd recursively */
251 while ((entry = readdir(dir))) {
256 if (!strcmp(entry->d_name, "."))
258 if (!strcmp(entry->d_name, ".."))
260 if (lstat(entry->d_name, &s) == -1)
263 if (!S_ISREG(m) && !S_ISDIR(m))
265 tmp = make_message("%s/%s", dirname, entry->d_name);
267 ret = func(tmp, private_data);
274 ret = for_each_file_in_dir(tmp, func, private_data);
282 ret2 = para_fchdir(cwd_fd);
283 if (ret2 < 0 && ret >= 0)
290 * check for object boundary violations
292 * test whether the range pointed to by the index entry for a given cell is
293 * contained in mapped data file. This should always be the case. Otherwise
294 * we are in real trouble.
296 static int check_range(struct osl_table *t, uint32_t row_num, uint32_t col_num)
299 struct osl_object obj;
300 struct osl_column *col;
302 char *map_start, *obj_start;
304 ret = get_cell_index(t, row_num, col_num, &index_entry);
307 ret = get_mapped_object(t, col_num, row_num, &obj);
310 col = t->columns + col_num;
311 obj_start = obj.data;
312 map_start = col->data_map.data;
313 // INFO_LOG("obj: %p..%p\n", obj_start, obj_start + obj.size);
314 // INFO_LOG("map: %p..%p\n", map_start, map_start + col->data_map.size);
315 if (obj_start < map_start || obj_start + obj.size > map_start + col->data_map.size) {
316 CRIT_LOG("range violation in row %u, col %u\n", row_num,
318 return -E_FSCK_RANGE_VIOLATION;
320 DEBUG_LOG("col %u: ok\n", col_num);
325 * check all cells of the given table for boundary violations
327 static int check_index_ranges(struct osl_table *t)
331 INFO_LOG("checking for range violations in index\n");
332 //DEBUG_LOG("%d rows. %d columns\n", t->num_rows, t->desc->num_columns);
333 t->num_invalid_rows = 0;
334 for (i = 0; i < t->num_rows; i++) {
335 if (row_is_invalid(t, i)) {
336 t->num_invalid_rows++;
339 for (j = 0; j < t->desc->num_columns; j++) { /* FXIME */
340 const struct osl_column_description *cd =
341 get_column_description(t->desc, j);
342 if (cd->storage_type != OSL_MAPPED_STORAGE)
344 ret = check_range(t, i, j);
346 if (ret != -E_OSL_INVALID_OBJECT &&
347 ret != -E_FSCK_RANGE_VIOLATION)
349 if (ret == -E_OSL_INVALID_OBJECT) {
350 CRIT_LOG("row %d, col %d maps to an "
351 "invalid object\n", i, j);
353 ret = mark_row_invalid(t, i);
356 t->num_invalid_rows++;
362 if (t->num_invalid_rows)
363 NOTICE_LOG("ranges OK. %d invalid row(s) detected\n",
364 t->num_invalid_rows);
366 INFO_LOG("no invalid rows, no range violations, good\n");
372 static int move_index_entry(struct osl_table *t, uint32_t dest, uint32_t src)
374 char *dest_ie, *src_ie;
375 int ret = get_row_index(t, dest, &dest_ie);
379 ret = get_row_index(t, src, &src_ie);
382 INFO_LOG("moving entry #%u to position %u\n", src, dest);
383 memcpy(dest_ie, src_ie, t->row_index_size);
387 static int map_index(const struct osl_table_description *desc, struct osl_object *map)
389 char *filename = index_filename(desc);
392 ret = mmap_full_file(filename, O_RDWR, &map->data, &map->size, NULL);
393 DEBUG_LOG("mapping index %s: ret: %d, size: %zu\n", filename, ret, map->size);
398 static int prune_invalid_rows_from_index(struct osl_table *t)
400 uint32_t top = 0, bottom;
404 if (!t->num_invalid_rows) {
405 INFO_LOG("all rows are valid, good\n");
408 NOTICE_LOG("deleting %u invalid row(s) (%d bytes) from index\n",
409 t->num_invalid_rows, t->row_index_size * t->num_invalid_rows);
410 bottom = t->num_rows - 1;
411 while (top < bottom) {
412 if (!row_is_invalid(t, top)) {
416 while (bottom > top) {
417 if (row_is_invalid(t, bottom)) {
421 /* move bottom index entry to top */
422 move_index_entry(t, top, bottom);
428 DEBUG_LOG("unmapping index\n");
429 osl_munmap(t->index_map.data, t->index_map.size);
430 filename = index_filename(t->desc);
431 ret = para_truncate(filename, t->row_index_size
432 * t->num_invalid_rows);
436 ret = map_index(t->desc, &t->index_map);
439 t->num_rows = table_num_rows(t);
443 static int check_for_invalid_objects(struct osl_table *t, uint32_t **lost_bytes)
446 const struct osl_column_description *cd;
447 uint32_t *loss = para_malloc(sizeof(uint32_t) * t->desc->num_columns);
449 INFO_LOG("looking for mapped objects not contained in index\n");
450 /* first count used bytes */
451 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
452 loss[i] = t->columns[i].data_map.size;
453 for (j = 0; j < t->num_rows; j++) {
454 struct osl_object obj;
455 ret = get_mapped_object(t, i, j, &obj);
457 loss[i] -= obj.size + 1; /* add one for header byte */
460 if (ret != -E_OSL_INVALID_OBJECT)
462 CRIT_LOG("row %d, col %d points to an invalid "
463 "mapped object, bad\n", j, i);
467 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
469 NOTICE_LOG("column %u contains %u lost bytes\n",
475 INFO_LOG("all mapped objects are valid, good\n");
483 /* prune_invalid_rows() must be run on the table before calling this */
484 static int prune_mapped_column(struct osl_table *t, uint32_t col_num, int fd)
487 uint32_t written = 0;
488 struct osl_column *col = t->columns + col_num;
490 INFO_LOG("pruning col %u\n", col_num);
491 for (i = 0; i < t->num_rows; i++) {
492 struct osl_object obj;
495 DEBUG_LOG("checking row %u/%u\n", i, t->num_rows);
496 ret = get_mapped_object(t, col_num, i, &obj);
499 ret = _write_all(fd, (char *)(obj.data) - 1, obj.size + 1);
502 written += obj.size + 1;
503 ret = get_row_index(t, i, &index_entry);
506 update_cell_index(index_entry, col, written, obj.size);
511 static int prune_objects(struct osl_table *t, uint32_t *lost_bytes)
514 const struct osl_column_description *cd;
515 char **col_filenames = para_calloc(t->desc->num_columns * sizeof(char *));
516 char **new_col_filenames = para_calloc(t->desc->num_columns * sizeof(char *));
517 char *idx_filename = index_filename(t->desc);
518 char *old_idx_filename = make_message("%s.bak", idx_filename);
521 NOTICE_LOG("removing unreferenced objects from data files\n");
522 /* first make a copy of the index */
523 ret = osl_open(old_idx_filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
527 ret = _write_all(fd, t->index_map.data, t->index_map.size);
531 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
534 col_filenames[i] = column_filename(t, i);
535 new_col_filenames[i] = make_message("%s.fsck", col_filenames[i]);
536 ret = osl_open(new_col_filenames[i], O_WRONLY | O_CREAT | O_EXCL, 0644);
538 goto out_unlink_data;
540 ret = prune_mapped_column(t, i, fd);
543 goto out_unlink_data;
545 ret = unmap_table(t, OSL_MARK_CLEAN);
547 goto out_unlink_data;
548 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
551 ret = osl_rename(new_col_filenames[i], col_filenames[i]);
552 if (ret < 0) { /* we're kinda screwed here */
553 CRIT_LOG("rename of col %i failed: %s\n", i,
554 osl_strerror(errno));
558 unlink(old_idx_filename);
559 ret = map_table(t, 0);
562 FOR_EACH_MAPPED_COLUMN(i, t, cd)
563 unlink(new_col_filenames[i]);
565 free(old_idx_filename);
567 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
568 free(col_filenames[i]);
569 free(new_col_filenames[i]);
572 free(new_col_filenames);
576 static struct osl_column_description hash_tree_table_cols[] = {
578 .storage_type = OSL_NO_STORAGE,
579 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
581 .compare_function = uint32_compare,
582 .data_size = HASH_SIZE
586 static const struct osl_table_description hash_tree_table_desc = {
587 .dir = "/", /* irrelevant */
591 .column_descriptions = hash_tree_table_cols
595 * The hash_tree table contains all hashes of the disk storage name column.
596 * of each row. It is used for checking if a disk storage file has a reference
599 static struct osl_table *hash_tree_table;
600 static HASH_TYPE *hashes;
602 static int check_disk_storage_column(struct osl_table *t, int row_num,
603 int col_num, char *ds_name, unsigned *num_missing_objects)
607 char *path = disk_storage_path(t, col_num, ds_name);
608 unsigned dsnc = t->disk_storage_name_column;
609 struct osl_object obj;
611 DEBUG_LOG("checking if %s is a regular file\n", path);
612 ret = stat(path, &statbuf);
613 if (ret < 0 && errno == ENOENT) {
615 (*num_missing_objects)++;
616 ERROR_LOG("row %d: object %s is missing\n", row_num, path);
617 NOTICE_LOG("trying to delete row %d\n", row_num);
618 ret = osl_get_row(t, dsnc, &obj, &row);
620 CRIT_LOG("unable to get row %d\n", row_num);
621 mark_row_invalid(t, row_num);
622 CRIT_LOG("Please re-run fsck\n");
625 ret = osl_del_row(t, row);
633 ret = -E_FSCK_NOT_A_REGULAR_FILE;
634 if (!(S_IFREG & statbuf.st_mode))
639 static int check_disk_storage_presence(struct osl_table *t)
642 struct osl_object obj, hash_obj = {.size = HASH_SIZE};
644 const struct osl_column_description *cd;
645 unsigned dsnc = t->disk_storage_name_column, missing_objects = 0;
649 hashes = para_malloc(t->num_rows * HASH_SIZE);
650 INFO_LOG("looking for missing disk storage objects\n");
651 for (i = 0; i < t->num_rows; i++) {
652 if (row_is_invalid(t, i))
654 ret = get_mapped_object(t, dsnc, i, &obj);
657 hash_object(&obj, hashes + i * HASH_SIZE);
658 hash_obj.data = hashes + i * HASH_SIZE;
659 osl_add_row(hash_tree_table, &hash_obj);
660 ds_name = disk_storage_name_of_hash(t, hashes + i * HASH_SIZE);
661 FOR_EACH_DISK_STORAGE_COLUMN(j, t, cd) {
662 ret = check_disk_storage_column(t, i, j, ds_name,
669 if (!missing_objects)
670 INFO_LOG("all referenced disk storage objects exist, good\n");
672 NOTICE_LOG("%d missing object(s)\n", missing_objects);
673 return missing_objects;
679 static int dummy_compare(const struct osl_object *obj1, const struct osl_object *obj2)
688 static unsigned files_pruned;
690 int prune_disk_storage_file(const char *path, void *private_data)
692 HASH_TYPE hash[HASH_SIZE];
693 unsigned flags = *(unsigned *)private_data;
694 struct osl_object obj = {.data = hash, .size = HASH_SIZE};
697 size_t len = strlen(path);
700 DEBUG_LOG("path: %s\n", path);
701 if (flags & OSL_LARGE_TABLE) {
702 if (len < HASH_SIZE * 2 + 2)
704 // NOTICE_LOG("p: %s\n", path + len - 2 * HASH_SIZE - 1);
705 ret = asc_to_hash(path + len - 2 * HASH_SIZE - 1, 1, hash);
708 ret = asc_to_hash(path + len - 2 * HASH_SIZE + 2, HASH_SIZE - 1,
712 // INFO_LOG("high: %x, low: %x, hash: %x\n", high, low, hash);
714 if (len < 2 * HASH_SIZE + 1)
716 ret = asc_to_hash(path + len - 2 * HASH_SIZE, 2 * HASH_SIZE, hash);
719 // INFO_LOG("hash: %x\n", hash);
723 char asc[2 * HASH_SIZE + 1];
724 hash_to_asc(hash, asc);
725 NOTICE_LOG("before: %s\nafter: %s\n", path, asc);
728 ret = osl_get_row(hash_tree_table, 0, &obj, &row);
731 NOTICE_LOG("unreferenced file in hash dir: %s\n", path);
734 ERROR_LOG("could not read hash value of %s\n", path);
736 NOTICE_LOG("removing %s\n", path);
742 static int prune_disk_storage_files(struct osl_table *t)
745 const struct osl_column_description *cd;
747 INFO_LOG("looking for unreferenced disk storage files\n");
748 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
749 char *dirname = column_filename(t, i);
750 ret = for_each_file_in_dir(dirname, prune_disk_storage_file,
751 (unsigned *)&t->desc->flags);
755 NOTICE_LOG("%u disk storage files deleted\n",
758 INFO_LOG("all files are are referenced, good\n");
762 static int check_disk_storage_columns(struct osl_table *t)
765 const struct osl_column_description *cd;
767 if (!t->num_disk_storage_columns) {
768 INFO_LOG("no disk storage columns in table '%s', "
769 "skipping checks\n", t->desc->name);
772 FOR_EACH_COLUMN(i, t->desc, cd)
773 t->desc->column_descriptions[i].compare_function = dummy_compare;
774 ret = init_rbtrees(t);
777 INFO_LOG("creating rbtree for disk storage hash values\n");
778 ret = osl_open_table(&hash_tree_table_desc, &hash_tree_table);
781 ret = check_disk_storage_presence(t);
783 goto out_close_hash_tree;
784 ret = prune_disk_storage_files(t);
786 osl_close_table(hash_tree_table, 0);
790 clear_rbtrees(t); /* TODO why are we doing that here? Seems odd */
794 static void set_dummy_contents(struct osl_table_description *desc)
797 struct osl_column_description *cd;
799 for (i = 0; i < desc->num_columns; i++) {
800 cd = get_column_description(desc, i);
801 cd->compare_function = dummy_compare;
805 static int fsck_init(struct osl_table_description *desc, struct osl_table **t)
807 struct osl_object map;
808 int ret = map_index(desc, &map);
812 ret = read_table_desc(&map, desc);
814 osl_munmap(map.data, map.size);
817 set_dummy_contents(desc);
818 ret = init_table_structure(desc, t);
820 osl_munmap(map.data, map.size);
823 DEBUG_LOG("unmapping index\n");
824 osl_munmap(map.data, map.size);
825 if (conf.force_given)
826 ret = map_table(*t, (MAP_TBL_FL_IGNORE_DIRTY));
828 ret = map_table(*t, 0);
830 (*t)->num_rows = table_num_rows(*t);
835 static void fsck_cleanup(struct osl_table *t)
841 if (t->desc->column_descriptions) {
842 struct osl_column_description *cd;
843 for (i = 0; i < t->desc->num_columns; i++) {
844 cd = get_column_description(t->desc, i);
845 free((char*)cd->name);
847 free(t->desc->column_descriptions);
854 #define ST_CASE(st) case st: return #st
856 const char *get_asc_storage_type(enum osl_storage_type st)
859 ST_CASE(OSL_MAPPED_STORAGE);
860 ST_CASE(OSL_DISK_STORAGE);
861 ST_CASE(OSL_NO_STORAGE);
866 #define APPEND_ASC_SF(sf, flag, str) do { if (sf & flag) { \
867 if (str) str = make_message("%s%s", str, " | " # flag); \
868 else str = para_strdup(#flag); }} while (0)
871 char *get_asc_storage_flags(enum osl_storage_type sf)
875 APPEND_ASC_SF(sf, OSL_RBTREE, asc_sf);
876 APPEND_ASC_SF(sf, OSL_FIXED_SIZE, asc_sf);
877 APPEND_ASC_SF(sf, OSL_UNIQUE, asc_sf);
881 static int dump_table_desc(struct osl_table *t, int fd)
883 const struct osl_table_description *desc = t->desc;
885 struct osl_column_description *cd;
886 char *msg = make_message("static struct osl_column_description cols[] = {\n");
887 ret = _write_all(fd, msg, strlen(msg));
891 FOR_EACH_COLUMN(i, desc, cd) {
893 msg = make_message("\t[%d] = {\n", i);
894 ret = _write_all(fd, msg, strlen(msg));
898 asc_st = get_asc_storage_type(cd->storage_type);
899 msg = make_message("\t\t.storage_type = %s,\n", asc_st);
900 ret = _write_all(fd, msg, strlen(msg));
904 if (cd->storage_flags) {
905 char *asc_sf = get_asc_storage_flags(cd->storage_flags);
906 msg = make_message("\t\t,storage_flags = %s,\n", asc_sf);
908 ret = _write_all(fd, msg, strlen(msg));
913 if (cd->storage_flags & OSL_FIXED_SIZE) {
914 msg = make_message("\t\t.data_size = %u,\n", cd->data_size);
915 ret = _write_all(fd, msg, strlen(msg));
920 msg = make_message("\t\t.name = \"%s\",\n", cd->name);
921 ret = _write_all(fd, msg, strlen(msg));
925 if (cd->storage_flags & OSL_RBTREE) {
926 msg = make_message("\t\t.compare_function = compare_func,\n");
927 ret = _write_all(fd, msg, strlen(msg));
932 msg = make_message("\t},\n");
933 ret = _write_all(fd, msg, strlen(msg));
938 msg = make_message("};\n");
939 ret = _write_all(fd, msg, strlen(msg));
946 static int dump_row(struct osl_table *t, unsigned row_num, const char *row_dir)
949 const struct osl_column_description *cd;
951 struct osl_object obj;
953 HASH_TYPE hash[HASH_SIZE];
956 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
957 ret = get_mapped_object(t, i, row_num, &obj);
960 filename = make_message("%s/col_%03u", row_dir, i);
961 ret = write_file(filename, obj.data, obj.size);
966 if (!t->num_disk_storage_columns)
968 dsnc = t->disk_storage_name_column;
969 ret = get_mapped_object(t, dsnc, row_num, &obj);
972 hash_object(&obj, hash);
973 ds_name = disk_storage_name_of_hash(t, hash);
974 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
975 filename = disk_storage_path(t, i, ds_name);
976 ret = mmap_full_file(filename, O_RDONLY, &obj.data, &obj.size, NULL);
980 filename = make_message("%s/col_%03u", row_dir, i);
981 ret = write_file(filename, obj.data, obj.size);
992 static int dump_rows(char *dump_dir, struct osl_table *t)
995 char *current_dir = NULL;
998 for (i = 0; i < t->num_rows; i++) {
1000 if (row_is_invalid(t, i))
1004 current_dir = make_message("%s/rows_%u-%u", dump_dir, i, i + 999);
1005 NOTICE_LOG("dumping rows %u - %u\n", i, i + 999);
1006 ret = osl_mkdir(current_dir, 0777);
1007 if (ret < 0 && !is_errno(-ret, EEXIST))
1010 row_dir = make_message("%s/row_%03u", current_dir, i);
1011 ret = osl_mkdir(row_dir, 0777);
1012 if (ret < 0 && !is_errno(-ret, EEXIST)) {
1016 ret = dump_row(t, i, row_dir);
1026 static int dump_table(char *dump_dir, struct osl_table_description *desc)
1028 struct osl_table *t = NULL;
1029 int fd, ret = fsck_init(desc, &t);
1031 char *table_dump_dir = NULL;
1035 ret = osl_mkdir(dump_dir, 0777);
1036 if (ret < 0 && !is_errno(-ret, EEXIST))
1038 table_dump_dir = make_message("%s/%s", dump_dir, desc->name);
1039 ret = osl_mkdir(table_dump_dir, 0777);
1040 if (ret < 0 && !is_errno(-ret, EEXIST))
1042 desc_file = make_message("%s/table_description.c", table_dump_dir);
1043 ret = osl_open(desc_file, O_WRONLY | O_CREAT | O_EXCL, 0644);
1048 ret = dump_table_desc(t, fd);
1052 ret = dump_rows(table_dump_dir, t);
1054 free(table_dump_dir);
1059 static int fsck(struct osl_table_description *desc)
1062 struct osl_table *t = NULL;
1063 uint32_t *lost_bytes = NULL;
1065 ret = fsck_init(desc, &t);
1068 ret = check_index_ranges(t);
1071 ret = check_disk_storage_columns(t);
1074 ret = prune_invalid_rows_from_index(t);
1077 ret = check_for_invalid_objects(t, &lost_bytes);
1080 if (ret > 0) { /* at least one mapped data file needs pruning */
1081 ret = prune_objects(t, lost_bytes);
1087 unmap_table(t, OSL_MARK_CLEAN);
1093 static int check_table(char *base_dir, char *table_name)
1095 struct osl_table_description desc = {
1096 .column_descriptions = NULL,
1102 INFO_LOG("checking table %s\n", table_name);
1103 if (!conf.no_fsck_given) {
1109 if (!conf.dump_dir_given || !*conf.dump_dir_arg)
1111 ret = dump_table(conf.dump_dir_arg, &desc);
1114 ERROR_LOG("failed to check table %s\n", table_name);
1116 NOTICE_LOG("successfully checked table %s\n", table_name);
1120 static int check_all_tables(char *base_dir)
1123 struct dirent *entry;
1124 int cwd_fd, ret2, ret = para_opendir(base_dir, &dir, &cwd_fd);
1128 while ((entry = readdir(dir))) {
1131 if (!strcmp(entry->d_name, "."))
1133 if (!strcmp(entry->d_name, ".."))
1135 if (lstat(entry->d_name, &s) == -1)
1140 ret = check_table(base_dir, entry->d_name);
1145 ret2 = para_fchdir(cwd_fd);
1146 if (ret2 < 0 && ret >= 0)
1152 int main(int argc, char **argv)
1155 char *base_dir = NULL;
1157 ret = fsck_cmdline_parser(argc, argv, &conf);
1159 ret = -E_FSCK_SYNTAX;
1162 HANDLE_VERSION_FLAG("fsck", conf);
1163 if (conf.base_dir_given)
1164 base_dir = para_strdup(conf.base_dir_arg);
1166 char *home = para_homedir();
1167 base_dir = make_message("%s/.paraslash/afs_database", home);
1170 if (!conf.inputs_num) {
1171 ret = check_all_tables(base_dir);
1174 for (i = 0; i < conf.inputs_num; i++) {
1175 ret = check_table(base_dir, conf.inputs[i]);
1181 ERROR_LOG("%s%s: %s\n",
1182 base_dir? "base_dir: " : "",
1183 base_dir? base_dir : "",
1186 if (conf.loglevel_arg > 1)
1187 EMERG_LOG("re-run with \"--loglevel %d\" to increase verbosity\n",
1188 conf.loglevel_arg - 1);
1190 NOTICE_LOG("success\n");
1193 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;