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);
66 __printf_2_3 void para_log(int ll, const char* fmt,...)
70 if (ll < conf.loglevel_arg)
73 vfprintf(stderr, fmt, argp);
78 signed char hexval_table[256] = {
79 -1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */
80 -1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */
81 -1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */
82 -1, -1, -1, -1, -1, -1, -1, -1, /* 18-1f */
83 -1, -1, -1, -1, -1, -1, -1, -1, /* 20-27 */
84 -1, -1, -1, -1, -1, -1, -1, -1, /* 28-2f */
85 0, 1, 2, 3, 4, 5, 6, 7, /* 30-37 */
86 8, 9, -1, -1, -1, -1, -1, -1, /* 38-3f */
87 -1, 10, 11, 12, 13, 14, 15, -1, /* 40-47 */
88 -1, -1, -1, -1, -1, -1, -1, -1, /* 48-4f */
89 -1, -1, -1, -1, -1, -1, -1, -1, /* 50-57 */
90 -1, -1, -1, -1, -1, -1, -1, -1, /* 58-5f */
91 -1, 10, 11, 12, 13, 14, 15, -1, /* 60-67 */
92 -1, -1, -1, -1, -1, -1, -1, -1, /* 68-67 */
93 -1, -1, -1, -1, -1, -1, -1, -1, /* 70-77 */
94 -1, -1, -1, -1, -1, -1, -1, -1, /* 78-7f */
95 -1, -1, -1, -1, -1, -1, -1, -1, /* 80-87 */
96 -1, -1, -1, -1, -1, -1, -1, -1, /* 88-8f */
97 -1, -1, -1, -1, -1, -1, -1, -1, /* 90-97 */
98 -1, -1, -1, -1, -1, -1, -1, -1, /* 98-9f */
99 -1, -1, -1, -1, -1, -1, -1, -1, /* a0-a7 */
100 -1, -1, -1, -1, -1, -1, -1, -1, /* a8-af */
101 -1, -1, -1, -1, -1, -1, -1, -1, /* b0-b7 */
102 -1, -1, -1, -1, -1, -1, -1, -1, /* b8-bf */
103 -1, -1, -1, -1, -1, -1, -1, -1, /* c0-c7 */
104 -1, -1, -1, -1, -1, -1, -1, -1, /* c8-cf */
105 -1, -1, -1, -1, -1, -1, -1, -1, /* d0-d7 */
106 -1, -1, -1, -1, -1, -1, -1, -1, /* d8-df */
107 -1, -1, -1, -1, -1, -1, -1, -1, /* e0-e7 */
108 -1, -1, -1, -1, -1, -1, -1, -1, /* e8-ef */
109 -1, -1, -1, -1, -1, -1, -1, -1, /* f0-f7 */
110 -1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */
113 int asc_to_hash(const char *asc_hash, int len, HASH_TYPE *hash)
116 const unsigned char *asc = (const unsigned char *) asc_hash;
118 while (*asc && i++ < len) {
119 unsigned int val = (hexval_table[asc[0]] << 4) | hexval_table[asc[1]];
129 static int _write_all(int fd, const char *buf, size_t len)
131 return write_all(fd, buf, &len);
135 * Paraslash's version of malloc().
137 * \param size The desired new size.
139 * A wrapper for malloc(3) which exits on errors.
141 * \return A pointer to the allocated memory, which is suitably aligned for any
146 __must_check __malloc static void *para_malloc(size_t size)
149 void *p = malloc(size);
152 EMERG_LOG("malloc failed (size = %zu), aborting\n",
160 * Paraslash's version of calloc().
162 * \param size The desired new size.
164 * A wrapper for calloc(3) which exits on errors.
166 * \return A pointer to the allocated and zeroed-out memory, which is suitably
167 * aligned for any kind of variable.
171 __must_check __malloc static void *para_calloc(size_t size)
173 void *ret = para_malloc(size);
175 memset(ret, 0, size);
180 * Paraslash's version of strdup().
182 * \param s The string to be duplicated.
184 * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
185 * there is no need to check the return value in the caller.
187 * \return A pointer to the duplicated string. If \p s was the NULL pointer,
188 * an pointer to an empty string is returned.
192 __must_check __malloc static char *para_strdup(const char *s)
196 if ((ret = strdup(s? s: "")))
198 EMERG_LOG("strdup failed, aborting\n");
203 * Get the home directory of the current user.
205 * \return A dynammically allocated string that must be freed by the caller. If
206 * the home directory could not be found, this function returns "/tmp".
208 __must_check __malloc static char *para_homedir(void)
210 struct passwd *pw = getpwuid(getuid());
211 return para_strdup(pw? pw->pw_dir : "/tmp");
215 * Compare two osl objects pointing to unsigned integers of 32 bit size.
217 * \param obj1 Pointer to the first integer.
218 * \param obj2 Pointer to the second integer.
220 * \return The values required for an osl compare function.
222 * \sa osl_compare_func, osl_hash_compare().
224 static int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2)
226 uint32_t d1 = read_u32((const char *)obj1->data);
227 uint32_t d2 = read_u32((const char *)obj2->data);
237 * Traverse the given directory recursively.
239 * \param dirname The directory to traverse.
240 * \param func The function to call for each entry.
241 * \param private_data Pointer to an arbitrary data structure.
243 * For each regular file under \a dirname, the supplied function \a func is
244 * called. The full path of the regular file and the \a private_data pointer
245 * are passed to \a func. Directories for which the calling process has no
246 * permissions to change to are silently ignored.
250 static int for_each_file_in_dir(const char *dirname,
251 int (*func)(const char *, void *), void *private_data)
254 struct dirent *entry;
255 int cwd_fd, ret2, ret = para_opendir(dirname, &dir, &cwd_fd);
258 return ret == -ERRNO_TO_ERROR(EACCES)? 1 : ret;
259 /* scan cwd recursively */
260 while ((entry = readdir(dir))) {
265 if (!strcmp(entry->d_name, "."))
267 if (!strcmp(entry->d_name, ".."))
269 if (lstat(entry->d_name, &s) == -1)
272 if (!S_ISREG(m) && !S_ISDIR(m))
274 tmp = make_message("%s/%s", dirname, entry->d_name);
276 ret = func(tmp, private_data);
283 ret = for_each_file_in_dir(tmp, func, private_data);
291 ret2 = para_fchdir(cwd_fd);
292 if (ret2 < 0 && ret >= 0)
299 * check for object boundary violations
301 * test whether the range pointed to by the index entry for a given cell is
302 * contained in mapped data file. This should always be the case. Otherwise
303 * we are in real trouble.
305 static int check_range(struct osl_table *t, uint32_t row_num, uint32_t col_num)
308 struct osl_object obj;
309 struct osl_column *col;
311 char *map_start, *obj_start;
313 ret = get_cell_index(t, row_num, col_num, &index_entry);
316 ret = get_mapped_object(t, col_num, row_num, &obj);
319 col = t->columns + col_num;
320 obj_start = obj.data;
321 map_start = col->data_map.data;
322 // INFO_LOG("obj: %p..%p\n", obj_start, obj_start + obj.size);
323 // INFO_LOG("map: %p..%p\n", map_start, map_start + col->data_map.size);
324 if (obj_start < map_start || obj_start + obj.size > map_start + col->data_map.size) {
325 CRIT_LOG("range violation in row %u, col %u\n", row_num,
327 return -E_FSCK_RANGE_VIOLATION;
329 DEBUG_LOG("col %u: ok\n", col_num);
334 * check all cells of the given table for boundary violations
336 static int check_index_ranges(struct osl_table *t)
340 INFO_LOG("checking for range violations in index\n");
341 //DEBUG_LOG("%d rows. %d columns\n", t->num_rows, t->desc->num_columns);
342 t->num_invalid_rows = 0;
343 for (i = 0; i < t->num_rows; i++) {
344 if (row_is_invalid(t, i)) {
345 t->num_invalid_rows++;
348 for (j = 0; j < t->desc->num_columns; j++) { /* FXIME */
349 const struct osl_column_description *cd =
350 get_column_description(t->desc, j);
351 if (cd->storage_type != OSL_MAPPED_STORAGE)
353 ret = check_range(t, i, j);
355 if (ret != -E_OSL_INVALID_OBJECT &&
356 ret != -E_FSCK_RANGE_VIOLATION)
358 if (ret == -E_OSL_INVALID_OBJECT) {
359 CRIT_LOG("row %d, col %d maps to an "
360 "invalid object\n", i, j);
362 ret = mark_row_invalid(t, i);
365 t->num_invalid_rows++;
371 if (t->num_invalid_rows)
372 NOTICE_LOG("ranges OK. %d invalid row(s) detected\n",
373 t->num_invalid_rows);
375 INFO_LOG("no invalid rows, no range violations, good\n");
381 static int move_index_entry(struct osl_table *t, uint32_t dest, uint32_t src)
383 char *dest_ie, *src_ie;
384 int ret = get_row_index(t, dest, &dest_ie);
388 ret = get_row_index(t, src, &src_ie);
391 INFO_LOG("moving entry #%u to position %u\n", src, dest);
392 memcpy(dest_ie, src_ie, t->row_index_size);
396 static int map_index(const struct osl_table_description *desc, struct osl_object *map)
398 char *filename = index_filename(desc);
401 ret = mmap_full_file(filename, O_RDWR, &map->data, &map->size, NULL);
402 DEBUG_LOG("mapping index %s: ret: %d, size: %zu\n", filename, ret, map->size);
407 static int prune_invalid_rows_from_index(struct osl_table *t)
409 uint32_t top = 0, bottom;
413 if (!t->num_invalid_rows) {
414 INFO_LOG("all rows are valid, good\n");
417 NOTICE_LOG("deleting %u invalid row(s) (%d bytes) from index\n",
418 t->num_invalid_rows, t->row_index_size * t->num_invalid_rows);
419 bottom = t->num_rows - 1;
420 while (top < bottom) {
421 if (!row_is_invalid(t, top)) {
425 while (bottom > top) {
426 if (row_is_invalid(t, bottom)) {
430 /* move bottom index entry to top */
431 move_index_entry(t, top, bottom);
437 DEBUG_LOG("unmapping index\n");
438 osl_munmap(t->index_map.data, t->index_map.size);
439 filename = index_filename(t->desc);
440 ret = para_truncate(filename, t->row_index_size
441 * t->num_invalid_rows);
445 ret = map_index(t->desc, &t->index_map);
448 t->num_rows = table_num_rows(t);
452 static int check_for_invalid_objects(struct osl_table *t, uint32_t **lost_bytes)
455 const struct osl_column_description *cd;
456 uint32_t *loss = para_malloc(sizeof(uint32_t) * t->desc->num_columns);
458 INFO_LOG("looking for mapped objects not contained in index\n");
459 /* first count used bytes */
460 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
461 loss[i] = t->columns[i].data_map.size;
462 for (j = 0; j < t->num_rows; j++) {
463 struct osl_object obj;
464 ret = get_mapped_object(t, i, j, &obj);
466 loss[i] -= obj.size + 1; /* add one for header byte */
469 if (ret != -E_OSL_INVALID_OBJECT)
471 CRIT_LOG("row %d, col %d points to an invalid "
472 "mapped object, bad\n", j, i);
476 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
478 NOTICE_LOG("column %u contains %u lost bytes\n",
484 INFO_LOG("all mapped objects are valid, good\n");
492 /* prune_invalid_rows() must be run on the table before calling this */
493 static int prune_mapped_column(struct osl_table *t, uint32_t col_num, int fd)
496 uint32_t written = 0;
497 struct osl_column *col = t->columns + col_num;
499 INFO_LOG("pruning col %u\n", col_num);
500 for (i = 0; i < t->num_rows; i++) {
501 struct osl_object obj;
504 DEBUG_LOG("checking row %u/%u\n", i, t->num_rows);
505 ret = get_mapped_object(t, col_num, i, &obj);
508 ret = _write_all(fd, (char *)(obj.data) - 1, obj.size + 1);
511 written += obj.size + 1;
512 ret = get_row_index(t, i, &index_entry);
515 update_cell_index(index_entry, col, written, obj.size);
520 static int prune_objects(struct osl_table *t, uint32_t *lost_bytes)
523 const struct osl_column_description *cd;
524 char **col_filenames = para_calloc(t->desc->num_columns * sizeof(char *));
525 char **new_col_filenames = para_calloc(t->desc->num_columns * sizeof(char *));
526 char *idx_filename = index_filename(t->desc);
527 char *old_idx_filename = make_message("%s.bak", idx_filename);
530 NOTICE_LOG("removing unreferenced objects from data files\n");
531 /* first make a copy of the index */
532 ret = osl_open(old_idx_filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
536 ret = _write_all(fd, t->index_map.data, t->index_map.size);
540 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
543 col_filenames[i] = column_filename(t, i);
544 new_col_filenames[i] = make_message("%s.fsck", col_filenames[i]);
545 ret = osl_open(new_col_filenames[i], O_WRONLY | O_CREAT | O_EXCL, 0644);
547 goto out_unlink_data;
549 ret = prune_mapped_column(t, i, fd);
552 goto out_unlink_data;
554 ret = unmap_table(t, OSL_MARK_CLEAN);
556 goto out_unlink_data;
557 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
560 ret = osl_rename(new_col_filenames[i], col_filenames[i]);
561 if (ret < 0) { /* we're kinda screwed here */
562 CRIT_LOG("rename of col %i failed: %s\n", i,
563 osl_strerror(errno));
567 unlink(old_idx_filename);
568 ret = map_table(t, 0);
571 FOR_EACH_MAPPED_COLUMN(i, t, cd)
572 unlink(new_col_filenames[i]);
574 free(old_idx_filename);
576 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
577 free(col_filenames[i]);
578 free(new_col_filenames[i]);
581 free(new_col_filenames);
585 static struct osl_column_description hash_tree_table_cols[] = {
587 .storage_type = OSL_NO_STORAGE,
588 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
590 .compare_function = uint32_compare,
591 .data_size = HASH_SIZE
595 static const struct osl_table_description hash_tree_table_desc = {
596 .dir = "/", /* irrelevant */
600 .column_descriptions = hash_tree_table_cols
604 * The hash_tree table contains all hashes of the disk storage name column.
605 * of each row. It is used for checking if a disk storage file has a reference
608 static struct osl_table *hash_tree_table;
609 static HASH_TYPE *hashes;
611 static int check_disk_storage_column(struct osl_table *t, int row_num,
612 int col_num, char *ds_name, unsigned *num_missing_objects)
616 char *path = disk_storage_path(t, col_num, ds_name);
617 unsigned dsnc = t->disk_storage_name_column;
618 struct osl_object obj;
620 DEBUG_LOG("checking if %s is a regular file\n", path);
621 ret = stat(path, &statbuf);
622 if (ret < 0 && errno == ENOENT) {
624 (*num_missing_objects)++;
625 ERROR_LOG("row %d: object %s is missing\n", row_num, path);
626 NOTICE_LOG("trying to delete row %d\n", row_num);
627 ret = osl_get_row(t, dsnc, &obj, &row);
629 CRIT_LOG("unable to get row %d\n", row_num);
630 mark_row_invalid(t, row_num);
631 CRIT_LOG("Please re-run fsck\n");
634 ret = osl_del_row(t, row);
642 ret = -E_FSCK_NOT_A_REGULAR_FILE;
643 if (!(S_IFREG & statbuf.st_mode))
648 static int check_disk_storage_presence(struct osl_table *t)
651 struct osl_object obj, hash_obj = {.size = HASH_SIZE};
653 const struct osl_column_description *cd;
654 unsigned dsnc = t->disk_storage_name_column, missing_objects = 0;
658 hashes = para_malloc(t->num_rows * HASH_SIZE);
659 INFO_LOG("looking for missing disk storage objects\n");
660 for (i = 0; i < t->num_rows; i++) {
661 if (row_is_invalid(t, i))
663 ret = get_mapped_object(t, dsnc, i, &obj);
666 hash_object(&obj, hashes + i * HASH_SIZE);
667 hash_obj.data = hashes + i * HASH_SIZE;
668 osl_add_row(hash_tree_table, &hash_obj);
669 ds_name = disk_storage_name_of_hash(t, hashes + i * HASH_SIZE);
670 FOR_EACH_DISK_STORAGE_COLUMN(j, t, cd) {
671 ret = check_disk_storage_column(t, i, j, ds_name,
678 if (!missing_objects)
679 INFO_LOG("all referenced disk storage objects exist, good\n");
681 NOTICE_LOG("%d missing object(s)\n", missing_objects);
682 return missing_objects;
688 static int dummy_compare(const struct osl_object *obj1, const struct osl_object *obj2)
697 static unsigned files_pruned;
699 int prune_disk_storage_file(const char *path, void *private_data)
701 HASH_TYPE hash[HASH_SIZE];
702 unsigned flags = *(unsigned *)private_data;
703 struct osl_object obj = {.data = hash, .size = HASH_SIZE};
706 size_t len = strlen(path);
709 DEBUG_LOG("path: %s\n", path);
710 if (flags & OSL_LARGE_TABLE) {
711 if (len < HASH_SIZE * 2 + 2)
713 // NOTICE_LOG("p: %s\n", path + len - 2 * HASH_SIZE - 1);
714 ret = asc_to_hash(path + len - 2 * HASH_SIZE - 1, 1, hash);
717 ret = asc_to_hash(path + len - 2 * HASH_SIZE + 2, HASH_SIZE - 1,
721 // INFO_LOG("high: %x, low: %x, hash: %x\n", high, low, hash);
723 if (len < 2 * HASH_SIZE + 1)
725 ret = asc_to_hash(path + len - 2 * HASH_SIZE, 2 * HASH_SIZE, hash);
728 // INFO_LOG("hash: %x\n", hash);
732 char asc[2 * HASH_SIZE + 1];
733 hash_to_asc(hash, asc);
734 NOTICE_LOG("before: %s\nafter: %s\n", path, asc);
737 ret = osl_get_row(hash_tree_table, 0, &obj, &row);
740 NOTICE_LOG("unreferenced file in hash dir: %s\n", path);
743 ERROR_LOG("could not read hash value of %s\n", path);
745 NOTICE_LOG("removing %s\n", path);
751 static int prune_disk_storage_files(struct osl_table *t)
754 const struct osl_column_description *cd;
756 INFO_LOG("looking for unreferenced disk storage files\n");
757 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
758 char *dirname = column_filename(t, i);
759 ret = for_each_file_in_dir(dirname, prune_disk_storage_file,
760 (unsigned *)&t->desc->flags);
764 NOTICE_LOG("%u disk storage files deleted\n",
767 INFO_LOG("all files are are referenced, good\n");
771 static int check_disk_storage_columns(struct osl_table *t)
774 const struct osl_column_description *cd;
776 if (!t->num_disk_storage_columns) {
777 INFO_LOG("no disk storage columns in table '%s', "
778 "skipping checks\n", t->desc->name);
781 FOR_EACH_COLUMN(i, t->desc, cd)
782 t->desc->column_descriptions[i].compare_function = dummy_compare;
783 ret = init_rbtrees(t);
786 INFO_LOG("creating rbtree for disk storage hash values\n");
787 ret = osl_open_table(&hash_tree_table_desc, &hash_tree_table);
790 ret = check_disk_storage_presence(t);
792 goto out_close_hash_tree;
793 ret = prune_disk_storage_files(t);
795 osl_close_table(hash_tree_table, 0);
799 clear_rbtrees(t); /* TODO why are we doing that here? Seems odd */
803 static void set_dummy_contents(struct osl_table_description *desc)
806 struct osl_column_description *cd;
808 for (i = 0; i < desc->num_columns; i++) {
809 cd = get_column_description(desc, i);
810 cd->compare_function = dummy_compare;
814 static int fsck_init(struct osl_table_description *desc, struct osl_table **t)
816 struct osl_object map;
817 int ret = map_index(desc, &map);
821 ret = read_table_desc(&map, desc);
823 osl_munmap(map.data, map.size);
826 set_dummy_contents(desc);
827 ret = init_table_structure(desc, t);
829 osl_munmap(map.data, map.size);
832 DEBUG_LOG("unmapping index\n");
833 osl_munmap(map.data, map.size);
834 if (conf.force_given)
835 ret = map_table(*t, (MAP_TBL_FL_IGNORE_DIRTY));
837 ret = map_table(*t, 0);
839 (*t)->num_rows = table_num_rows(*t);
844 static void fsck_cleanup(struct osl_table *t)
850 if (t->desc->column_descriptions) {
851 struct osl_column_description *cd;
852 for (i = 0; i < t->desc->num_columns; i++) {
853 cd = get_column_description(t->desc, i);
854 free((char*)cd->name);
856 free(t->desc->column_descriptions);
863 #define ST_CASE(st) case st: return #st
865 const char *get_asc_storage_type(enum osl_storage_type st)
868 ST_CASE(OSL_MAPPED_STORAGE);
869 ST_CASE(OSL_DISK_STORAGE);
870 ST_CASE(OSL_NO_STORAGE);
875 #define APPEND_ASC_SF(sf, flag, str) do { if (sf & flag) { \
876 if (str) str = make_message("%s%s", str, " | " # flag); \
877 else str = para_strdup(#flag); }} while (0)
880 char *get_asc_storage_flags(enum osl_storage_type sf)
884 APPEND_ASC_SF(sf, OSL_RBTREE, asc_sf);
885 APPEND_ASC_SF(sf, OSL_FIXED_SIZE, asc_sf);
886 APPEND_ASC_SF(sf, OSL_UNIQUE, asc_sf);
890 static int dump_table_desc(struct osl_table *t, int fd)
892 const struct osl_table_description *desc = t->desc;
894 struct osl_column_description *cd;
895 char *msg = make_message("static struct osl_column_description cols[] = {\n");
896 ret = _write_all(fd, msg, strlen(msg));
900 FOR_EACH_COLUMN(i, desc, cd) {
902 msg = make_message("\t[%d] = {\n", i);
903 ret = _write_all(fd, msg, strlen(msg));
907 asc_st = get_asc_storage_type(cd->storage_type);
908 msg = make_message("\t\t.storage_type = %s,\n", asc_st);
909 ret = _write_all(fd, msg, strlen(msg));
913 if (cd->storage_flags) {
914 char *asc_sf = get_asc_storage_flags(cd->storage_flags);
915 msg = make_message("\t\t,storage_flags = %s,\n", asc_sf);
917 ret = _write_all(fd, msg, strlen(msg));
922 if (cd->storage_flags & OSL_FIXED_SIZE) {
923 msg = make_message("\t\t.data_size = %u,\n", cd->data_size);
924 ret = _write_all(fd, msg, strlen(msg));
929 msg = make_message("\t\t.name = \"%s\",\n", cd->name);
930 ret = _write_all(fd, msg, strlen(msg));
934 if (cd->storage_flags & OSL_RBTREE) {
935 msg = make_message("\t\t.compare_function = compare_func,\n");
936 ret = _write_all(fd, msg, strlen(msg));
941 msg = make_message("\t},\n");
942 ret = _write_all(fd, msg, strlen(msg));
947 msg = make_message("};\n");
948 ret = _write_all(fd, msg, strlen(msg));
955 static int dump_row(struct osl_table *t, unsigned row_num, const char *row_dir)
958 const struct osl_column_description *cd;
960 struct osl_object obj;
962 HASH_TYPE hash[HASH_SIZE];
965 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
966 ret = get_mapped_object(t, i, row_num, &obj);
969 filename = make_message("%s/col_%03u", row_dir, i);
970 ret = write_file(filename, obj.data, obj.size);
975 if (!t->num_disk_storage_columns)
977 dsnc = t->disk_storage_name_column;
978 ret = get_mapped_object(t, dsnc, row_num, &obj);
981 hash_object(&obj, hash);
982 ds_name = disk_storage_name_of_hash(t, hash);
983 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
984 filename = disk_storage_path(t, i, ds_name);
985 ret = mmap_full_file(filename, O_RDONLY, &obj.data, &obj.size, NULL);
989 filename = make_message("%s/col_%03u", row_dir, i);
990 ret = write_file(filename, obj.data, obj.size);
1001 static int dump_rows(char *dump_dir, struct osl_table *t)
1004 char *current_dir = NULL;
1007 for (i = 0; i < t->num_rows; i++) {
1009 if (row_is_invalid(t, i))
1013 current_dir = make_message("%s/rows_%u-%u", dump_dir, i, i + 999);
1014 NOTICE_LOG("dumping rows %u - %u\n", i, i + 999);
1015 ret = osl_mkdir(current_dir, 0777);
1016 if (ret < 0 && !is_errno(-ret, EEXIST))
1019 row_dir = make_message("%s/row_%03u", current_dir, i);
1020 ret = osl_mkdir(row_dir, 0777);
1021 if (ret < 0 && !is_errno(-ret, EEXIST)) {
1025 ret = dump_row(t, i, row_dir);
1035 static int dump_table(char *dump_dir, struct osl_table_description *desc)
1037 struct osl_table *t = NULL;
1038 int fd, ret = fsck_init(desc, &t);
1040 char *table_dump_dir = NULL;
1044 ret = osl_mkdir(dump_dir, 0777);
1045 if (ret < 0 && !is_errno(-ret, EEXIST))
1047 table_dump_dir = make_message("%s/%s", dump_dir, desc->name);
1048 ret = osl_mkdir(table_dump_dir, 0777);
1049 if (ret < 0 && !is_errno(-ret, EEXIST))
1051 desc_file = make_message("%s/table_description.c", table_dump_dir);
1052 ret = osl_open(desc_file, O_WRONLY | O_CREAT | O_EXCL, 0644);
1057 ret = dump_table_desc(t, fd);
1061 ret = dump_rows(table_dump_dir, t);
1063 free(table_dump_dir);
1068 static int fsck(struct osl_table_description *desc)
1071 struct osl_table *t = NULL;
1072 uint32_t *lost_bytes = NULL;
1074 ret = fsck_init(desc, &t);
1077 ret = check_index_ranges(t);
1080 ret = check_disk_storage_columns(t);
1083 ret = prune_invalid_rows_from_index(t);
1086 ret = check_for_invalid_objects(t, &lost_bytes);
1089 if (ret > 0) { /* at least one mapped data file needs pruning */
1090 ret = prune_objects(t, lost_bytes);
1096 unmap_table(t, OSL_MARK_CLEAN);
1102 static int check_table(char *base_dir, char *table_name)
1104 struct osl_table_description desc = {
1105 .column_descriptions = NULL,
1111 INFO_LOG("checking table %s\n", table_name);
1112 if (!conf.no_fsck_given) {
1118 if (!conf.dump_dir_given || !*conf.dump_dir_arg)
1120 ret = dump_table(conf.dump_dir_arg, &desc);
1123 ERROR_LOG("failed to check table %s\n", table_name);
1125 NOTICE_LOG("successfully checked table %s\n", table_name);
1129 static int check_all_tables(char *base_dir)
1132 struct dirent *entry;
1133 int cwd_fd, ret2, ret = para_opendir(base_dir, &dir, &cwd_fd);
1137 while ((entry = readdir(dir))) {
1140 if (!strcmp(entry->d_name, "."))
1142 if (!strcmp(entry->d_name, ".."))
1144 if (lstat(entry->d_name, &s) == -1)
1149 ret = check_table(base_dir, entry->d_name);
1154 ret2 = para_fchdir(cwd_fd);
1155 if (ret2 < 0 && ret >= 0)
1161 int main(int argc, char **argv)
1164 char *base_dir = NULL;
1166 ret = fsck_cmdline_parser(argc, argv, &conf);
1168 ret = -E_FSCK_SYNTAX;
1171 HANDLE_VERSION_FLAG("fsck", conf);
1172 if (conf.base_dir_given)
1173 base_dir = para_strdup(conf.base_dir_arg);
1175 char *home = para_homedir();
1176 base_dir = make_message("%s/.paraslash/afs_database", home);
1179 if (!conf.inputs_num) {
1180 ret = check_all_tables(base_dir);
1183 for (i = 0; i < conf.inputs_num; i++) {
1184 ret = check_table(base_dir, conf.inputs[i]);
1190 ERROR_LOG("%s%s: %s\n",
1191 base_dir? "base_dir: " : "",
1192 base_dir? base_dir : "",
1195 if (conf.loglevel_arg > 1)
1196 EMERG_LOG("re-run with \"--loglevel %d\" to increase verbosity\n",
1197 conf.loglevel_arg - 1);
1199 NOTICE_LOG("success\n");
1202 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;