2 * Copyright (C) 2007-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file fsck.c The program used to check an osl table. */
10 #include <sys/types.h>
20 #include "fsck.cmdline.h"
22 /** version text used by various commands if -V switch was given */
23 #define VERSION_TEXT(prefix) "osl_" prefix " " VERSION " " "\n" \
24 "Copyright (C) 2008 Andre Noll\n" \
25 "This is free software with ABSOLUTELY NO WARRANTY." \
26 " See COPYING for details.\n" \
27 "Written by Andre Noll.\n" \
28 "Report bugs to <maan@systemlinux.org>.\n"
30 /** print out \p VERSION_TEXT and exit if version flag was given */
31 #define HANDLE_VERSION_FLAG(_prefix, _args_info_struct) \
32 if (_args_info_struct.version_given) { \
33 printf("%s", VERSION_TEXT(_prefix)); \
37 static struct fsck_args_info conf;
40 FSCK_ERROR(RANGE_VIOLATION, "range violation detected, very bad"), \
41 FSCK_ERROR(NOT_A_REGULAR_FILE, "not a regular file"), \
42 FSCK_ERROR(SYNTAX, "fsck syntax error"),
44 #define FSCK_ERROR(num, txt) E_FSCK_ ## num
46 FSCK_DUMMY = (1 << FSCK_ERROR_BIT) - 1,
50 #define FSCK_ERROR(num, txt) txt
51 const char const *fsck_errors[] = {
55 static const char *fsck_strerror(int num)
57 if (num & (1 << FSCK_ERROR_BIT))
58 return fsck_errors[num & ((1 << FSCK_ERROR_BIT) - 1)];
59 return osl_strerror(num);
65 signed char hexval_table[256] = {
66 -1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */
67 -1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */
68 -1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */
69 -1, -1, -1, -1, -1, -1, -1, -1, /* 18-1f */
70 -1, -1, -1, -1, -1, -1, -1, -1, /* 20-27 */
71 -1, -1, -1, -1, -1, -1, -1, -1, /* 28-2f */
72 0, 1, 2, 3, 4, 5, 6, 7, /* 30-37 */
73 8, 9, -1, -1, -1, -1, -1, -1, /* 38-3f */
74 -1, 10, 11, 12, 13, 14, 15, -1, /* 40-47 */
75 -1, -1, -1, -1, -1, -1, -1, -1, /* 48-4f */
76 -1, -1, -1, -1, -1, -1, -1, -1, /* 50-57 */
77 -1, -1, -1, -1, -1, -1, -1, -1, /* 58-5f */
78 -1, 10, 11, 12, 13, 14, 15, -1, /* 60-67 */
79 -1, -1, -1, -1, -1, -1, -1, -1, /* 68-67 */
80 -1, -1, -1, -1, -1, -1, -1, -1, /* 70-77 */
81 -1, -1, -1, -1, -1, -1, -1, -1, /* 78-7f */
82 -1, -1, -1, -1, -1, -1, -1, -1, /* 80-87 */
83 -1, -1, -1, -1, -1, -1, -1, -1, /* 88-8f */
84 -1, -1, -1, -1, -1, -1, -1, -1, /* 90-97 */
85 -1, -1, -1, -1, -1, -1, -1, -1, /* 98-9f */
86 -1, -1, -1, -1, -1, -1, -1, -1, /* a0-a7 */
87 -1, -1, -1, -1, -1, -1, -1, -1, /* a8-af */
88 -1, -1, -1, -1, -1, -1, -1, -1, /* b0-b7 */
89 -1, -1, -1, -1, -1, -1, -1, -1, /* b8-bf */
90 -1, -1, -1, -1, -1, -1, -1, -1, /* c0-c7 */
91 -1, -1, -1, -1, -1, -1, -1, -1, /* c8-cf */
92 -1, -1, -1, -1, -1, -1, -1, -1, /* d0-d7 */
93 -1, -1, -1, -1, -1, -1, -1, -1, /* d8-df */
94 -1, -1, -1, -1, -1, -1, -1, -1, /* e0-e7 */
95 -1, -1, -1, -1, -1, -1, -1, -1, /* e8-ef */
96 -1, -1, -1, -1, -1, -1, -1, -1, /* f0-f7 */
97 -1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */
100 int asc_to_hash(const char *asc_hash, int len, HASH_TYPE *hash)
103 const unsigned char *asc = (const unsigned char *) asc_hash;
105 while (*asc && i++ < len) {
106 unsigned int val = (hexval_table[asc[0]] << 4) | hexval_table[asc[1]];
116 static int _write_all(int fd, const char *buf, size_t len)
118 return write_all(fd, buf, &len);
122 * Paraslash's version of malloc().
124 * \param size The desired new size.
126 * A wrapper for malloc(3) which exits on errors.
128 * \return A pointer to the allocated memory, which is suitably aligned for any
133 __must_check __malloc static void *para_malloc(size_t size)
136 void *p = malloc(size);
139 EMERG_LOG("malloc failed (size = %zu), aborting\n",
147 * Paraslash's version of calloc().
149 * \param size The desired new size.
151 * A wrapper for calloc(3) which exits on errors.
153 * \return A pointer to the allocated and zeroed-out memory, which is suitably
154 * aligned for any kind of variable.
158 __must_check __malloc static void *para_calloc(size_t size)
160 void *ret = para_malloc(size);
162 memset(ret, 0, size);
167 * Paraslash's version of strdup().
169 * \param s The string to be duplicated.
171 * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
172 * there is no need to check the return value in the caller.
174 * \return A pointer to the duplicated string. If \p s was the NULL pointer,
175 * an pointer to an empty string is returned.
179 __must_check __malloc static char *para_strdup(const char *s)
183 if ((ret = strdup(s? s: "")))
185 EMERG_LOG("strdup failed, aborting\n");
190 * Get the home directory of the current user.
192 * \return A dynammically allocated string that must be freed by the caller. If
193 * the home directory could not be found, this function returns "/tmp".
195 __must_check __malloc static char *para_homedir(void)
197 struct passwd *pw = getpwuid(getuid());
198 return para_strdup(pw? pw->pw_dir : "/tmp");
202 * Compare two osl objects pointing to unsigned integers of 32 bit size.
204 * \param obj1 Pointer to the first integer.
205 * \param obj2 Pointer to the second integer.
207 * \return The values required for an osl compare function.
209 * \sa osl_compare_func, osl_hash_compare().
211 static int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2)
213 uint32_t d1 = read_u32((const char *)obj1->data);
214 uint32_t d2 = read_u32((const char *)obj2->data);
224 * Traverse the given directory recursively.
226 * \param dirname The directory to traverse.
227 * \param func The function to call for each entry.
228 * \param private_data Pointer to an arbitrary data structure.
230 * For each regular file under \a dirname, the supplied function \a func is
231 * called. The full path of the regular file and the \a private_data pointer
232 * are passed to \a func. Directories for which the calling process has no
233 * permissions to change to are silently ignored.
237 static int for_each_file_in_dir(const char *dirname,
238 int (*func)(const char *, void *), void *private_data)
241 struct dirent *entry;
242 int cwd_fd, ret2, ret = para_opendir(dirname, &dir, &cwd_fd);
245 return ret == -ERRNO_TO_ERROR(EACCES)? 1 : ret;
246 /* scan cwd recursively */
247 while ((entry = readdir(dir))) {
252 if (!strcmp(entry->d_name, "."))
254 if (!strcmp(entry->d_name, ".."))
256 if (lstat(entry->d_name, &s) == -1)
259 if (!S_ISREG(m) && !S_ISDIR(m))
261 tmp = make_message("%s/%s", dirname, entry->d_name);
263 ret = func(tmp, private_data);
270 ret = for_each_file_in_dir(tmp, func, private_data);
278 ret2 = para_fchdir(cwd_fd);
279 if (ret2 < 0 && ret >= 0)
286 * check for object boundary violations
288 * test whether the range pointed to by the index entry for a given cell is
289 * contained in mapped data file. This should always be the case. Otherwise
290 * we are in real trouble.
292 static int check_range(struct osl_table *t, uint32_t row_num, uint32_t col_num)
295 struct osl_object obj;
296 struct osl_column *col;
298 char *map_start, *obj_start;
300 ret = get_cell_index(t, row_num, col_num, &index_entry);
303 ret = get_mapped_object(t, col_num, row_num, &obj);
306 col = t->columns + col_num;
307 obj_start = obj.data;
308 map_start = col->data_map.data;
309 // INFO_LOG("obj: %p..%p\n", obj_start, obj_start + obj.size);
310 // INFO_LOG("map: %p..%p\n", map_start, map_start + col->data_map.size);
311 if (obj_start < map_start || obj_start + obj.size > map_start + col->data_map.size) {
312 CRIT_LOG("range violation in row %u, col %u\n", row_num,
314 return -E_FSCK_RANGE_VIOLATION;
316 DEBUG_LOG("col %u: ok\n", col_num);
321 * check all cells of the given table for boundary violations
323 static int check_index_ranges(struct osl_table *t)
327 INFO_LOG("checking for range violations in index\n");
328 //DEBUG_LOG("%d rows. %d columns\n", t->num_rows, t->desc->num_columns);
329 t->num_invalid_rows = 0;
330 for (i = 0; i < t->num_rows; i++) {
331 if (row_is_invalid(t, i)) {
332 t->num_invalid_rows++;
335 for (j = 0; j < t->desc->num_columns; j++) { /* FXIME */
336 const struct osl_column_description *cd =
337 get_column_description(t->desc, j);
338 if (cd->storage_type != OSL_MAPPED_STORAGE)
340 ret = check_range(t, i, j);
342 if (ret != -E_OSL_INVALID_OBJECT &&
343 ret != -E_FSCK_RANGE_VIOLATION)
345 if (ret == -E_OSL_INVALID_OBJECT) {
346 CRIT_LOG("row %d, col %d maps to an "
347 "invalid object\n", i, j);
349 ret = mark_row_invalid(t, i);
352 t->num_invalid_rows++;
358 if (t->num_invalid_rows)
359 NOTICE_LOG("ranges OK. %d invalid row(s) detected\n",
360 t->num_invalid_rows);
362 INFO_LOG("no invalid rows, no range violations, good\n");
368 static int move_index_entry(struct osl_table *t, uint32_t dest, uint32_t src)
370 char *dest_ie, *src_ie;
371 int ret = get_row_index(t, dest, &dest_ie);
375 ret = get_row_index(t, src, &src_ie);
378 INFO_LOG("moving entry #%u to position %u\n", src, dest);
379 memcpy(dest_ie, src_ie, t->row_index_size);
383 static int map_index(const struct osl_table_description *desc, struct osl_object *map)
385 char *filename = index_filename(desc);
388 ret = mmap_full_file(filename, O_RDWR, &map->data, &map->size, NULL);
389 DEBUG_LOG("mapping index %s: ret: %d, size: %zu\n", filename, ret, map->size);
394 static int prune_invalid_rows_from_index(struct osl_table *t)
396 uint32_t top = 0, bottom;
400 if (!t->num_invalid_rows) {
401 INFO_LOG("all rows are valid, good\n");
404 NOTICE_LOG("deleting %u invalid row(s) (%d bytes) from index\n",
405 t->num_invalid_rows, t->row_index_size * t->num_invalid_rows);
406 bottom = t->num_rows - 1;
407 while (top < bottom) {
408 if (!row_is_invalid(t, top)) {
412 while (bottom > top) {
413 if (row_is_invalid(t, bottom)) {
417 /* move bottom index entry to top */
418 move_index_entry(t, top, bottom);
424 DEBUG_LOG("unmapping index\n");
425 osl_munmap(t->index_map.data, t->index_map.size);
426 filename = index_filename(t->desc);
427 ret = para_truncate(filename, t->row_index_size
428 * t->num_invalid_rows);
432 ret = map_index(t->desc, &t->index_map);
435 t->num_rows = table_num_rows(t);
439 static int check_for_invalid_objects(struct osl_table *t, uint32_t **lost_bytes)
442 const struct osl_column_description *cd;
443 uint32_t *loss = para_malloc(sizeof(uint32_t) * t->desc->num_columns);
445 INFO_LOG("looking for mapped objects not contained in index\n");
446 /* first count used bytes */
447 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
448 loss[i] = t->columns[i].data_map.size;
449 for (j = 0; j < t->num_rows; j++) {
450 struct osl_object obj;
451 ret = get_mapped_object(t, i, j, &obj);
453 loss[i] -= obj.size + 1; /* add one for header byte */
456 if (ret != -E_OSL_INVALID_OBJECT)
458 CRIT_LOG("row %d, col %d points to an invalid "
459 "mapped object, bad\n", j, i);
463 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
465 NOTICE_LOG("column %u contains %u lost bytes\n",
471 INFO_LOG("all mapped objects are valid, good\n");
479 /* prune_invalid_rows() must be run on the table before calling this */
480 static int prune_mapped_column(struct osl_table *t, uint32_t col_num, int fd)
483 uint32_t written = 0;
484 struct osl_column *col = t->columns + col_num;
486 INFO_LOG("pruning col %u\n", col_num);
487 for (i = 0; i < t->num_rows; i++) {
488 struct osl_object obj;
491 DEBUG_LOG("checking row %u/%u\n", i, t->num_rows);
492 ret = get_mapped_object(t, col_num, i, &obj);
495 ret = _write_all(fd, (char *)(obj.data) - 1, obj.size + 1);
498 written += obj.size + 1;
499 ret = get_row_index(t, i, &index_entry);
502 update_cell_index(index_entry, col, written, obj.size);
507 static int prune_objects(struct osl_table *t, uint32_t *lost_bytes)
510 const struct osl_column_description *cd;
511 char **col_filenames = para_calloc(t->desc->num_columns * sizeof(char *));
512 char **new_col_filenames = para_calloc(t->desc->num_columns * sizeof(char *));
513 char *idx_filename = index_filename(t->desc);
514 char *old_idx_filename = make_message("%s.bak", idx_filename);
517 NOTICE_LOG("removing unreferenced objects from data files\n");
518 /* first make a copy of the index */
519 ret = osl_open(old_idx_filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
523 ret = _write_all(fd, t->index_map.data, t->index_map.size);
527 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
530 col_filenames[i] = column_filename(t, i);
531 new_col_filenames[i] = make_message("%s.fsck", col_filenames[i]);
532 ret = osl_open(new_col_filenames[i], O_WRONLY | O_CREAT | O_EXCL, 0644);
534 goto out_unlink_data;
536 ret = prune_mapped_column(t, i, fd);
539 goto out_unlink_data;
541 ret = unmap_table(t, OSL_MARK_CLEAN);
543 goto out_unlink_data;
544 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
547 ret = osl_rename(new_col_filenames[i], col_filenames[i]);
548 if (ret < 0) { /* we're kinda screwed here */
549 CRIT_LOG("rename of col %i failed: %s\n", i,
550 osl_strerror(errno));
554 unlink(old_idx_filename);
555 ret = map_table(t, 0);
558 FOR_EACH_MAPPED_COLUMN(i, t, cd)
559 unlink(new_col_filenames[i]);
561 free(old_idx_filename);
563 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
564 free(col_filenames[i]);
565 free(new_col_filenames[i]);
568 free(new_col_filenames);
572 static struct osl_column_description hash_tree_table_cols[] = {
574 .storage_type = OSL_NO_STORAGE,
575 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
577 .compare_function = uint32_compare,
578 .data_size = HASH_SIZE
582 static const struct osl_table_description hash_tree_table_desc = {
583 .dir = "/", /* irrelevant */
587 .column_descriptions = hash_tree_table_cols
591 * The hash_tree table contains all hashes of the disk storage name column.
592 * of each row. It is used for checking if a disk storage file has a reference
595 static struct osl_table *hash_tree_table;
596 static HASH_TYPE *hashes;
598 static int check_disk_storage_column(struct osl_table *t, int row_num,
599 int col_num, char *ds_name, unsigned *num_missing_objects)
603 char *path = disk_storage_path(t, col_num, ds_name);
604 unsigned dsnc = t->disk_storage_name_column;
605 struct osl_object obj;
607 DEBUG_LOG("checking if %s is a regular file\n", path);
608 ret = stat(path, &statbuf);
609 if (ret < 0 && errno == ENOENT) {
611 (*num_missing_objects)++;
612 ERROR_LOG("row %d: object %s is missing\n", row_num, path);
613 NOTICE_LOG("trying to delete row %d\n", row_num);
614 ret = osl_get_row(t, dsnc, &obj, &row);
616 CRIT_LOG("unable to get row %d\n", row_num);
617 mark_row_invalid(t, row_num);
618 CRIT_LOG("Please re-run fsck\n");
621 ret = osl_del_row(t, row);
629 ret = -E_FSCK_NOT_A_REGULAR_FILE;
630 if (!(S_IFREG & statbuf.st_mode))
635 static int check_disk_storage_presence(struct osl_table *t)
638 struct osl_object obj, hash_obj = {.size = HASH_SIZE};
640 const struct osl_column_description *cd;
641 unsigned dsnc = t->disk_storage_name_column, missing_objects = 0;
645 hashes = para_malloc(t->num_rows * HASH_SIZE);
646 INFO_LOG("looking for missing disk storage objects\n");
647 for (i = 0; i < t->num_rows; i++) {
648 if (row_is_invalid(t, i))
650 ret = get_mapped_object(t, dsnc, i, &obj);
653 hash_object(&obj, hashes + i * HASH_SIZE);
654 hash_obj.data = hashes + i * HASH_SIZE;
655 osl_add_row(hash_tree_table, &hash_obj);
656 ds_name = disk_storage_name_of_hash(t, hashes + i * HASH_SIZE);
657 FOR_EACH_DISK_STORAGE_COLUMN(j, t, cd) {
658 ret = check_disk_storage_column(t, i, j, ds_name,
665 if (!missing_objects)
666 INFO_LOG("all referenced disk storage objects exist, good\n");
668 NOTICE_LOG("%d missing object(s)\n", missing_objects);
669 return missing_objects;
675 static int dummy_compare(const struct osl_object *obj1, const struct osl_object *obj2)
684 static unsigned files_pruned;
686 int prune_disk_storage_file(const char *path, void *private_data)
688 HASH_TYPE hash[HASH_SIZE];
689 unsigned flags = *(unsigned *)private_data;
690 struct osl_object obj = {.data = hash, .size = HASH_SIZE};
693 size_t len = strlen(path);
696 DEBUG_LOG("path: %s\n", path);
697 if (flags & OSL_LARGE_TABLE) {
698 if (len < HASH_SIZE * 2 + 2)
700 // NOTICE_LOG("p: %s\n", path + len - 2 * HASH_SIZE - 1);
701 ret = asc_to_hash(path + len - 2 * HASH_SIZE - 1, 1, hash);
704 ret = asc_to_hash(path + len - 2 * HASH_SIZE + 2, HASH_SIZE - 1,
708 // INFO_LOG("high: %x, low: %x, hash: %x\n", high, low, hash);
710 if (len < 2 * HASH_SIZE + 1)
712 ret = asc_to_hash(path + len - 2 * HASH_SIZE, 2 * HASH_SIZE, hash);
715 // INFO_LOG("hash: %x\n", hash);
719 char asc[2 * HASH_SIZE + 1];
720 hash_to_asc(hash, asc);
721 NOTICE_LOG("before: %s\nafter: %s\n", path, asc);
724 ret = osl_get_row(hash_tree_table, 0, &obj, &row);
727 NOTICE_LOG("unreferenced file in hash dir: %s\n", path);
730 ERROR_LOG("could not read hash value of %s\n", path);
732 NOTICE_LOG("removing %s\n", path);
738 static int prune_disk_storage_files(struct osl_table *t)
741 const struct osl_column_description *cd;
743 INFO_LOG("looking for unreferenced disk storage files\n");
744 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
745 char *dirname = column_filename(t, i);
746 ret = for_each_file_in_dir(dirname, prune_disk_storage_file,
747 (unsigned *)&t->desc->flags);
751 NOTICE_LOG("%u disk storage files deleted\n",
754 INFO_LOG("all files are are referenced, good\n");
758 static int check_disk_storage_columns(struct osl_table *t)
761 const struct osl_column_description *cd;
763 if (!t->num_disk_storage_columns) {
764 INFO_LOG("no disk storage columns in table '%s', "
765 "skipping checks\n", t->desc->name);
768 FOR_EACH_COLUMN(i, t->desc, cd)
769 t->desc->column_descriptions[i].compare_function = dummy_compare;
770 ret = init_rbtrees(t);
773 INFO_LOG("creating rbtree for disk storage hash values\n");
774 ret = osl_open_table(&hash_tree_table_desc, &hash_tree_table);
777 ret = check_disk_storage_presence(t);
779 goto out_close_hash_tree;
780 ret = prune_disk_storage_files(t);
782 osl_close_table(hash_tree_table, 0);
786 clear_rbtrees(t); /* TODO why are we doing that here? Seems odd */
790 static void set_dummy_contents(struct osl_table_description *desc)
793 struct osl_column_description *cd;
795 for (i = 0; i < desc->num_columns; i++) {
796 cd = get_column_description(desc, i);
797 cd->compare_function = dummy_compare;
801 static int fsck_init(struct osl_table_description *desc, struct osl_table **t)
803 struct osl_object map;
804 int ret = map_index(desc, &map);
808 ret = read_table_desc(&map, desc);
810 osl_munmap(map.data, map.size);
813 set_dummy_contents(desc);
814 ret = init_table_structure(desc, t);
816 osl_munmap(map.data, map.size);
819 DEBUG_LOG("unmapping index\n");
820 osl_munmap(map.data, map.size);
821 if (conf.force_given)
822 ret = map_table(*t, (MAP_TBL_FL_IGNORE_DIRTY));
824 ret = map_table(*t, 0);
826 (*t)->num_rows = table_num_rows(*t);
831 static void fsck_cleanup(struct osl_table *t)
837 if (t->desc->column_descriptions) {
838 struct osl_column_description *cd;
839 for (i = 0; i < t->desc->num_columns; i++) {
840 cd = get_column_description(t->desc, i);
841 free((char*)cd->name);
843 free(t->desc->column_descriptions);
850 #define ST_CASE(st) case st: return #st
852 const char *get_asc_storage_type(enum osl_storage_type st)
855 ST_CASE(OSL_MAPPED_STORAGE);
856 ST_CASE(OSL_DISK_STORAGE);
857 ST_CASE(OSL_NO_STORAGE);
862 #define APPEND_ASC_SF(sf, flag, str) do { if (sf & flag) { \
863 if (str) str = make_message("%s%s", str, " | " # flag); \
864 else str = para_strdup(#flag); }} while (0)
867 char *get_asc_storage_flags(enum osl_storage_type sf)
871 APPEND_ASC_SF(sf, OSL_RBTREE, asc_sf);
872 APPEND_ASC_SF(sf, OSL_FIXED_SIZE, asc_sf);
873 APPEND_ASC_SF(sf, OSL_UNIQUE, asc_sf);
877 static int dump_table_desc(struct osl_table *t, int fd)
879 const struct osl_table_description *desc = t->desc;
881 struct osl_column_description *cd;
882 char *msg = make_message("static struct osl_column_description cols[] = {\n");
883 ret = _write_all(fd, msg, strlen(msg));
887 FOR_EACH_COLUMN(i, desc, cd) {
889 msg = make_message("\t[%d] = {\n", i);
890 ret = _write_all(fd, msg, strlen(msg));
894 asc_st = get_asc_storage_type(cd->storage_type);
895 msg = make_message("\t\t.storage_type = %s,\n", asc_st);
896 ret = _write_all(fd, msg, strlen(msg));
900 if (cd->storage_flags) {
901 char *asc_sf = get_asc_storage_flags(cd->storage_flags);
902 msg = make_message("\t\t,storage_flags = %s,\n", asc_sf);
904 ret = _write_all(fd, msg, strlen(msg));
909 if (cd->storage_flags & OSL_FIXED_SIZE) {
910 msg = make_message("\t\t.data_size = %u,\n", cd->data_size);
911 ret = _write_all(fd, msg, strlen(msg));
916 msg = make_message("\t\t.name = \"%s\",\n", cd->name);
917 ret = _write_all(fd, msg, strlen(msg));
921 if (cd->storage_flags & OSL_RBTREE) {
922 msg = make_message("\t\t.compare_function = compare_func,\n");
923 ret = _write_all(fd, msg, strlen(msg));
928 msg = make_message("\t},\n");
929 ret = _write_all(fd, msg, strlen(msg));
934 msg = make_message("};\n");
935 ret = _write_all(fd, msg, strlen(msg));
942 static int dump_row(struct osl_table *t, unsigned row_num, const char *row_dir)
945 const struct osl_column_description *cd;
947 struct osl_object obj;
949 HASH_TYPE hash[HASH_SIZE];
952 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
953 ret = get_mapped_object(t, i, row_num, &obj);
956 filename = make_message("%s/col_%03u", row_dir, i);
957 ret = write_file(filename, obj.data, obj.size);
962 if (!t->num_disk_storage_columns)
964 dsnc = t->disk_storage_name_column;
965 ret = get_mapped_object(t, dsnc, row_num, &obj);
968 hash_object(&obj, hash);
969 ds_name = disk_storage_name_of_hash(t, hash);
970 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
971 filename = disk_storage_path(t, i, ds_name);
972 ret = mmap_full_file(filename, O_RDONLY, &obj.data, &obj.size, NULL);
976 filename = make_message("%s/col_%03u", row_dir, i);
977 ret = write_file(filename, obj.data, obj.size);
988 static int dump_rows(char *dump_dir, struct osl_table *t)
991 char *current_dir = NULL;
994 for (i = 0; i < t->num_rows; i++) {
996 if (row_is_invalid(t, i))
1000 current_dir = make_message("%s/rows_%u-%u", dump_dir, i, i + 999);
1001 NOTICE_LOG("dumping rows %u - %u\n", i, i + 999);
1002 ret = osl_mkdir(current_dir, 0777);
1003 if (ret < 0 && !is_errno(-ret, EEXIST))
1006 row_dir = make_message("%s/row_%03u", current_dir, i);
1007 ret = osl_mkdir(row_dir, 0777);
1008 if (ret < 0 && !is_errno(-ret, EEXIST)) {
1012 ret = dump_row(t, i, row_dir);
1022 static int dump_table(char *dump_dir, struct osl_table_description *desc)
1024 struct osl_table *t = NULL;
1025 int fd, ret = fsck_init(desc, &t);
1027 char *table_dump_dir = NULL;
1031 ret = osl_mkdir(dump_dir, 0777);
1032 if (ret < 0 && !is_errno(-ret, EEXIST))
1034 table_dump_dir = make_message("%s/%s", dump_dir, desc->name);
1035 ret = osl_mkdir(table_dump_dir, 0777);
1036 if (ret < 0 && !is_errno(-ret, EEXIST))
1038 desc_file = make_message("%s/table_description.c", table_dump_dir);
1039 ret = osl_open(desc_file, O_WRONLY | O_CREAT | O_EXCL, 0644);
1044 ret = dump_table_desc(t, fd);
1048 ret = dump_rows(table_dump_dir, t);
1050 free(table_dump_dir);
1055 static int fsck(struct osl_table_description *desc)
1058 struct osl_table *t = NULL;
1059 uint32_t *lost_bytes = NULL;
1061 ret = fsck_init(desc, &t);
1064 ret = check_index_ranges(t);
1067 ret = check_disk_storage_columns(t);
1070 ret = prune_invalid_rows_from_index(t);
1073 ret = check_for_invalid_objects(t, &lost_bytes);
1076 if (ret > 0) { /* at least one mapped data file needs pruning */
1077 ret = prune_objects(t, lost_bytes);
1083 unmap_table(t, OSL_MARK_CLEAN);
1089 static int check_table(char *base_dir, char *table_name)
1091 struct osl_table_description desc = {
1092 .column_descriptions = NULL,
1098 INFO_LOG("checking table %s\n", table_name);
1099 if (!conf.no_fsck_given) {
1105 if (!conf.dump_dir_given || !*conf.dump_dir_arg)
1107 ret = dump_table(conf.dump_dir_arg, &desc);
1110 ERROR_LOG("failed to check table %s\n", table_name);
1112 NOTICE_LOG("successfully checked table %s\n", table_name);
1116 static int check_all_tables(char *base_dir)
1119 struct dirent *entry;
1120 int cwd_fd, ret2, ret = para_opendir(base_dir, &dir, &cwd_fd);
1124 while ((entry = readdir(dir))) {
1127 if (!strcmp(entry->d_name, "."))
1129 if (!strcmp(entry->d_name, ".."))
1131 if (lstat(entry->d_name, &s) == -1)
1136 ret = check_table(base_dir, entry->d_name);
1141 ret2 = para_fchdir(cwd_fd);
1142 if (ret2 < 0 && ret >= 0)
1148 int main(int argc, char **argv)
1151 char *base_dir = NULL;
1152 struct fsck_cmdline_parser_params params = {
1155 .check_required = 1,
1156 .check_ambiguity = 1,
1159 ret = fsck_cmdline_parser_ext(argc, argv, &conf, ¶ms);
1162 ret = -E_FSCK_SYNTAX;
1165 loglevel = conf.loglevel_arg;
1166 HANDLE_VERSION_FLAG("fsck", conf);
1167 if (conf.base_dir_given)
1168 base_dir = para_strdup(conf.base_dir_arg);
1170 char *home = para_homedir();
1171 base_dir = make_message("%s/.paraslash/afs_database", home);
1174 INFO_LOG("base_dir: %s\n", base_dir);
1175 if (!conf.inputs_num) {
1176 ret = check_all_tables(base_dir);
1179 for (i = 0; i < conf.inputs_num; i++) {
1180 ret = check_table(base_dir, conf.inputs[i]);
1186 ERROR_LOG("%s\n", fsck_strerror(-ret));
1187 if (conf.loglevel_arg > 1)
1188 EMERG_LOG("re-run with \"--loglevel %d\" to increase verbosity\n",
1189 conf.loglevel_arg - 1);
1191 NOTICE_LOG("success\n");
1194 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;