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
)
399 INFO_LOG("checking for range violations in index\n");
400 //DEBUG_LOG("%d rows. %d columns\n", t->num_rows, t->desc->num_columns);
401 t
->num_invalid_rows
= 0;
402 for (n
= 0; n
< t
->num_rows
; n
++) {
403 const struct osl_column_description
*cd
;
405 if (row_is_invalid(t
, n
)) {
406 t
->num_invalid_rows
++;
409 FOR_EACH_MAPPED_COLUMN(k
, t
, cd
) {
410 ret
= check_range(t
, n
, k
);
412 if (ret
!= -E_FSCK_RANGE_VIOLATION
)
414 ret
= fsck_mark_row_invalid(t
, n
);
417 t
->num_invalid_rows
++;
423 if (t
->num_invalid_rows
)
424 NOTICE_LOG("ranges OK. %d invalid row(s) detected\n",
425 t
->num_invalid_rows
);
427 INFO_LOG("no invalid rows, no range violations, good\n");
433 static int move_index_entry(struct osl_table
*t
, uint32_t dest
, uint32_t src
)
435 char *dest_ie
, *src_ie
;
436 int ret
= get_row_index(t
, dest
, &dest_ie
);
440 ret
= get_row_index(t
, src
, &src_ie
);
443 INFO_LOG("moving entry #%u to position %u\n", src
, dest
);
444 memcpy(dest_ie
, src_ie
, t
->row_index_size
);
448 static int map_index(const struct osl_table_description
*desc
, struct osl_object
*map
)
450 char *filename
= index_filename(desc
);
453 ret
= mmap_full_file(filename
, O_RDWR
, &map
->data
, &map
->size
, NULL
);
454 DEBUG_LOG("mapping index %s: ret: %d, size: %zu\n", filename
, ret
, map
->size
);
459 static int prune_invalid_rows_from_index(struct osl_table
*t
)
461 uint32_t top
= 0, bottom
;
465 if (!t
->num_invalid_rows
) {
466 INFO_LOG("all rows are valid, good\n");
469 NOTICE_LOG("index contains %u invalid row(s) (%d bytes)\n",
470 t
->num_invalid_rows
, t
->row_index_size
* t
->num_invalid_rows
);
471 if (OPT_GIVEN(DRY_RUN
))
473 NOTICE_LOG("removing invalid rows from index\n");
474 bottom
= t
->num_rows
- 1;
475 while (top
< bottom
) {
476 if (!row_is_invalid(t
, top
)) {
480 while (bottom
> top
) {
481 if (row_is_invalid(t
, bottom
)) {
485 /* move bottom index entry to top */
486 move_index_entry(t
, top
, bottom
);
492 DEBUG_LOG("unmapping index\n");
493 osl_munmap(t
->index_map
.data
, t
->index_map
.size
);
494 filename
= index_filename(t
->desc
);
495 ret
= truncate_file(filename
, t
->row_index_size
496 * t
->num_invalid_rows
);
500 ret
= map_index(t
->desc
, &t
->index_map
);
503 t
->num_rows
= table_num_rows(t
);
507 static int check_for_invalid_objects(struct osl_table
*t
, uint32_t **lost_bytes
)
511 const struct osl_column_description
*cd
;
512 uint32_t *loss
= fsck_malloc(sizeof(uint32_t) * t
->desc
->num_columns
);
514 INFO_LOG("looking for mapped objects not contained in index\n");
515 /* first count used bytes */
516 FOR_EACH_MAPPED_COLUMN(k
, t
, cd
) {
517 loss
[k
] = t
->columns
[k
].data_map
.size
;
518 DEBUG_LOG("column %i data map: %zu bytes\n", k
,
519 t
->columns
[k
].data_map
.size
);
520 for (n
= 0; n
< t
->num_rows
; n
++) {
521 struct osl_object obj
;
522 ret
= get_mapped_object(t
, k
, n
, &obj
);
529 FOR_EACH_MAPPED_COLUMN(k
, t
, cd
) {
531 NOTICE_LOG("column %u contains %u lost bytes\n",
537 INFO_LOG("all mapped objects are valid, good\n");
545 /* prune_invalid_rows() must be run on the table before calling this */
546 static int prune_mapped_column(struct osl_table
*t
, uint32_t col_num
, int fd
)
550 uint32_t written
= 0;
551 struct osl_column
*col
= t
->columns
+ col_num
;
553 INFO_LOG("pruning col %u\n", col_num
);
554 for (n
= 0; n
< t
->num_rows
; n
++) {
555 struct osl_object obj
;
558 DEBUG_LOG("checking row %u/%u\n", n
, t
->num_rows
);
559 ret
= get_mapped_object(t
, col_num
, n
, &obj
);
562 ret
= _write_all(fd
, (char *)(obj
.data
), obj
.size
);
566 ret
= get_row_index(t
, n
, &index_entry
);
569 update_cell_index(index_entry
, col
, written
, obj
.size
);
574 static int prune_objects(struct osl_table
*t
, uint32_t *lost_bytes
)
577 const struct osl_column_description
*cd
;
578 char **col_filenames
= fsck_calloc(t
->desc
->num_columns
* sizeof(char *));
579 char **new_col_filenames
= fsck_calloc(t
->desc
->num_columns
* sizeof(char *));
580 char *idx_filename
= index_filename(t
->desc
);
581 char *old_idx_filename
= make_message("%s.bak", idx_filename
);
584 NOTICE_LOG("removing unreferenced objects from data files\n");
585 /* first make a copy of the index */
586 DEBUG_LOG("opening %s\n", old_idx_filename
);
587 ret
= osl_open(old_idx_filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
591 ret
= _write_all(fd
, t
->index_map
.data
, t
->index_map
.size
);
595 FOR_EACH_MAPPED_COLUMN(i
, t
, cd
) {
598 col_filenames
[i
] = column_filename(t
, i
);
599 new_col_filenames
[i
] = make_message("%s.fsck", col_filenames
[i
]);
600 ret
= osl_open(new_col_filenames
[i
], O_WRONLY
| O_CREAT
| O_EXCL
, 0644);
602 goto out_unlink_data
;
604 ret
= prune_mapped_column(t
, i
, fd
);
607 goto out_unlink_data
;
609 ret
= unmap_table(t
, OSL_MARK_CLEAN
);
611 goto out_unlink_data
;
612 FOR_EACH_MAPPED_COLUMN(i
, t
, cd
) {
615 ret
= osl_rename(new_col_filenames
[i
], col_filenames
[i
]);
616 if (ret
< 0) { /* we're kinda screwed here */
617 CRIT_LOG("rename of col %i failed: %s\n", i
,
618 osl_strerror(errno
));
622 unlink(old_idx_filename
);
623 ret
= map_table(t
, 0);
626 FOR_EACH_MAPPED_COLUMN(i
, t
, cd
)
627 unlink(new_col_filenames
[i
]);
629 free(old_idx_filename
);
631 FOR_EACH_MAPPED_COLUMN(i
, t
, cd
) {
632 free(col_filenames
[i
]);
633 free(new_col_filenames
[i
]);
636 free(new_col_filenames
);
640 static struct osl_column_description hash_tree_table_cols
[] = {
642 .storage_type
= OSL_NO_STORAGE
,
643 .storage_flags
= OSL_RBTREE
| OSL_FIXED_SIZE
| OSL_UNIQUE
,
645 .compare_function
= uint32_compare
,
646 .data_size
= HASH_SIZE
650 static const struct osl_table_description hash_tree_table_desc
= {
651 .dir
= "/", /* irrelevant */
655 .column_descriptions
= hash_tree_table_cols
659 * The hash_tree table contains all hashes of the disk storage name column.
660 * of each row. It is used for checking if a disk storage file has a reference
663 static struct osl_table
*hash_tree_table
;
664 static HASH_TYPE
*hashes
;
666 static int check_disk_storage_column(struct osl_table
*t
, int row_num
,
667 int col_num
, char *ds_name
, unsigned *num_missing_objects
)
671 char *path
= disk_storage_path(t
, col_num
, ds_name
);
672 unsigned dsnc
= t
->disk_storage_name_column
;
673 struct osl_object obj
;
675 DEBUG_LOG("checking if %s is a regular file\n", path
);
676 ret
= stat(path
, &statbuf
);
677 if (ret
< 0 && errno
== ENOENT
) {
679 (*num_missing_objects
)++;
680 ERROR_LOG("row %d: object %s is missing\n", row_num
, path
);
682 if (OPT_GIVEN(DRY_RUN
))
684 NOTICE_LOG("trying to delete row %d\n", row_num
);
685 ret
= osl_get_row(t
, dsnc
, &obj
, &row
);
687 CRIT_LOG("unable to get row %d\n", row_num
);
688 fsck_mark_row_invalid(t
, row_num
);
689 CRIT_LOG("Please re-run fsck\n");
692 ret
= osl_del_row(t
, row
);
700 ret
= -E_FSCK_NOT_A_REGULAR_FILE
;
701 if (!(S_IFREG
& statbuf
.st_mode
))
706 static int check_disk_storage_presence(struct osl_table
*t
)
710 struct osl_object obj
, hash_obj
= {.size
= HASH_SIZE
};
712 const struct osl_column_description
*cd
;
713 unsigned dsnc
= t
->disk_storage_name_column
, missing_objects
= 0;
717 hashes
= fsck_malloc(t
->num_rows
* HASH_SIZE
);
718 INFO_LOG("looking for missing disk storage objects\n");
719 for (k
= 0; k
< t
->num_rows
; k
++) {
720 if (row_is_invalid(t
, k
))
722 ret
= get_mapped_object(t
, dsnc
, k
, &obj
);
725 hash_object(t
, &obj
, hashes
+ k
* HASH_SIZE
);
726 hash_obj
.data
= hashes
+ k
* HASH_SIZE
;
727 osl_add_row(hash_tree_table
, &hash_obj
);
728 ds_name
= disk_storage_name_of_hash(t
, hashes
+ k
* HASH_SIZE
);
729 FOR_EACH_DISK_STORAGE_COLUMN(n
, t
, cd
) {
730 ret
= check_disk_storage_column(t
, k
, n
, ds_name
,
737 if (!missing_objects
)
738 INFO_LOG("all referenced disk storage objects exist, good\n");
740 NOTICE_LOG("%d missing object(s)\n", missing_objects
);
741 return missing_objects
;
747 static int dummy_compare(const struct osl_object
*obj1
, const struct osl_object
*obj2
)
756 static unsigned files_pruned
;
758 static int prune_disk_storage_file(const char *path
, void *private_data
)
760 HASH_TYPE hash
[HASH_SIZE
];
761 uint8_t flags
= *(uint8_t *)private_data
;
762 struct osl_object obj
= {.data
= hash
, .size
= HASH_SIZE
};
765 size_t len
= strlen(path
);
768 DEBUG_LOG("path: %s\n", path
);
769 if (flags
& OSL_LARGE_TABLE
) {
770 if (len
< HASH_SIZE
* 2 + 2)
772 // NOTICE_LOG("p: %s\n", path + len - 2 * HASH_SIZE - 1);
773 ret
= asc_to_hash(path
+ len
- 2 * HASH_SIZE
- 1, 1, hash
);
776 ret
= asc_to_hash(path
+ len
- 2 * HASH_SIZE
+ 2, HASH_SIZE
- 1,
780 // INFO_LOG("high: %x, low: %x, hash: %x\n", high, low, hash);
782 if (len
< 2 * HASH_SIZE
+ 1)
784 ret
= asc_to_hash(path
+ len
- 2 * HASH_SIZE
, 2 * HASH_SIZE
, hash
);
787 // INFO_LOG("hash: %x\n", hash);
791 char asc
[2 * HASH_SIZE
+ 1];
792 hash_to_asc(hash
, asc
);
793 NOTICE_LOG("before: %s\nafter: %s\n", path
, asc
);
796 ret
= osl_get_row(hash_tree_table
, 0, &obj
, &row
);
799 NOTICE_LOG("unreferenced file in hash dir: %s\n", path
);
802 ERROR_LOG("could not read hash value of %s\n", path
);
804 if (OPT_GIVEN(DRY_RUN
))
806 NOTICE_LOG("removing %s\n", path
);
812 static int prune_disk_storage_files(struct osl_table
*t
)
815 const struct osl_column_description
*cd
;
817 INFO_LOG("looking for unreferenced disk storage files\n");
818 FOR_EACH_DISK_STORAGE_COLUMN(i
, t
, cd
) {
819 char *dirname
= column_filename(t
, i
);
820 uint8_t flags
= t
->desc
->flags
;
821 ret
= for_each_file_in_dir(dirname
, prune_disk_storage_file
,
826 NOTICE_LOG("%u disk storage files deleted\n",
829 INFO_LOG("all files are are referenced, good\n");
833 static int check_disk_storage_columns(struct osl_table
*t
)
836 const struct osl_column_description
*cd
;
838 if (!t
->num_disk_storage_columns
) {
839 INFO_LOG("no disk storage columns in table '%s', "
840 "skipping checks\n", t
->desc
->name
);
843 FOR_EACH_COLUMN(i
, t
->desc
, cd
)
844 t
->desc
->column_descriptions
[i
].compare_function
= dummy_compare
;
845 ret
= init_rbtrees(t
);
848 INFO_LOG("creating rbtree for disk storage hash values\n");
849 ret
= osl_open_table(&hash_tree_table_desc
, &hash_tree_table
);
852 ret
= check_disk_storage_presence(t
);
854 goto out_close_hash_tree
;
855 ret
= prune_disk_storage_files(t
);
857 osl_close_table(hash_tree_table
, 0);
861 clear_rbtrees(t
); /* TODO why are we doing that here? Seems odd */
865 static void set_dummy_contents(struct osl_table_description
*desc
)
868 struct osl_column_description
*cd
;
870 for (i
= 0; i
< desc
->num_columns
; i
++) {
871 cd
= get_column_description(desc
, i
);
872 cd
->compare_function
= dummy_compare
;
876 static int fsck_init(struct osl_table_description
*desc
, struct osl_table
**t
)
878 struct osl_object map
;
879 int version
, ret
= map_index(desc
, &map
);
883 ret
= read_table_desc(&map
, desc
); /* checks table version */
885 osl_munmap(map
.data
, map
.size
);
889 set_dummy_contents(desc
);
890 ret
= init_table_structure(desc
, t
);
892 osl_munmap(map
.data
, map
.size
);
895 (*t
)->version
= version
;
896 DEBUG_LOG("unmapping index\n");
897 osl_munmap(map
.data
, map
.size
);
898 if (OPT_GIVEN(FORCE
))
899 ret
= map_table(*t
, (MAP_TBL_FL_IGNORE_DIRTY
));
901 ret
= map_table(*t
, 0);
903 (*t
)->num_rows
= table_num_rows(*t
);
904 DEBUG_LOG("index header size: %d\n", (*t
)->index_header_size
);
905 DEBUG_LOG("row index size: %d\n", (*t
)->row_index_size
);
911 static void fsck_cleanup(struct osl_table
*t
)
917 if (t
->desc
->column_descriptions
) {
918 struct osl_column_description
*cd
;
919 for (i
= 0; i
< t
->desc
->num_columns
; i
++) {
920 cd
= get_column_description(t
->desc
, i
);
921 free((char*)cd
->name
);
923 free(t
->desc
->column_descriptions
);
930 #define ST_CASE(st) case st: return #st
932 static const char *get_asc_storage_type(enum osl_storage_type st
)
935 ST_CASE(OSL_MAPPED_STORAGE
);
936 ST_CASE(OSL_DISK_STORAGE
);
937 ST_CASE(OSL_NO_STORAGE
);
942 #define APPEND_ASC_SF(sf, flag, str) do { if (sf & flag) { \
943 if (str) str = make_message("%s%s", str, " | " # flag); \
944 else str = fsck_strdup(#flag); }} while (0)
947 static char *get_asc_storage_flags(enum osl_storage_type sf
)
951 APPEND_ASC_SF(sf
, OSL_RBTREE
, asc_sf
);
952 APPEND_ASC_SF(sf
, OSL_FIXED_SIZE
, asc_sf
);
953 APPEND_ASC_SF(sf
, OSL_UNIQUE
, asc_sf
);
957 static int dump_table_desc(struct osl_table
*t
, int fd
)
959 const struct osl_table_description
*desc
= t
->desc
;
961 struct osl_column_description
*cd
;
962 char *msg
= make_message("static struct osl_column_description cols[] = {\n");
963 ret
= _write_all(fd
, msg
, strlen(msg
));
967 FOR_EACH_COLUMN(i
, desc
, cd
) {
969 msg
= make_message("\t[%d] = {\n", i
);
970 ret
= _write_all(fd
, msg
, strlen(msg
));
974 asc_st
= get_asc_storage_type(cd
->storage_type
);
975 msg
= make_message("\t\t.storage_type = %s,\n", asc_st
);
976 ret
= _write_all(fd
, msg
, strlen(msg
));
980 if (cd
->storage_flags
) {
981 char *asc_sf
= get_asc_storage_flags(cd
->storage_flags
);
982 msg
= make_message("\t\t,storage_flags = %s,\n", asc_sf
);
984 ret
= _write_all(fd
, msg
, strlen(msg
));
989 if (cd
->storage_flags
& OSL_FIXED_SIZE
) {
990 msg
= make_message("\t\t.data_size = %u,\n", cd
->data_size
);
991 ret
= _write_all(fd
, msg
, strlen(msg
));
996 msg
= make_message("\t\t.name = \"%s\",\n", cd
->name
);
997 ret
= _write_all(fd
, msg
, strlen(msg
));
1001 if (cd
->storage_flags
& OSL_RBTREE
) {
1002 msg
= make_message("\t\t.compare_function = compare_func,\n");
1003 ret
= _write_all(fd
, msg
, strlen(msg
));
1008 msg
= make_message("\t},\n");
1009 ret
= _write_all(fd
, msg
, strlen(msg
));
1014 msg
= make_message("};\n");
1015 ret
= _write_all(fd
, msg
, strlen(msg
));
1022 static int dump_row(struct osl_table
*t
, unsigned row_num
, const char *row_dir
)
1025 const struct osl_column_description
*cd
;
1027 struct osl_object obj
;
1029 HASH_TYPE hash
[HASH_SIZE
];
1032 FOR_EACH_MAPPED_COLUMN(i
, t
, cd
) {
1033 ret
= get_mapped_object(t
, i
, row_num
, &obj
);
1036 filename
= make_message("%s/col_%03u", row_dir
, i
);
1037 ret
= write_file(filename
, obj
.data
, obj
.size
);
1042 if (!t
->num_disk_storage_columns
)
1044 dsnc
= t
->disk_storage_name_column
;
1045 ret
= get_mapped_object(t
, dsnc
, row_num
, &obj
);
1048 hash_object(t
, &obj
, hash
);
1049 ds_name
= disk_storage_name_of_hash(t
, hash
);
1050 FOR_EACH_DISK_STORAGE_COLUMN(i
, t
, cd
) {
1051 filename
= disk_storage_path(t
, i
, ds_name
);
1052 ret
= mmap_full_file(filename
, O_RDONLY
, &obj
.data
, &obj
.size
, NULL
);
1056 filename
= make_message("%s/col_%03u", row_dir
, i
);
1057 ret
= write_file(filename
, obj
.data
, obj
.size
);
1068 static int dump_rows(char *dump_dir
, struct osl_table
*t
)
1071 char *current_dir
= NULL
;
1074 for (i
= 0; i
< t
->num_rows
; i
++) {
1076 if (row_is_invalid(t
, i
))
1080 current_dir
= make_message("%s/rows_%u-%u", dump_dir
, i
, i
+ 999);
1081 NOTICE_LOG("dumping rows %u - %u\n", i
, i
+ 999);
1082 ret
= osl_mkdir(current_dir
, 0777);
1083 if (ret
< 0 && ret
!= -E_OSL_DIR_EXISTS
)
1086 row_dir
= make_message("%s/row_%03u", current_dir
, i
);
1087 ret
= osl_mkdir(row_dir
, 0777);
1088 if (ret
< 0 && ret
!= -E_OSL_DIR_EXISTS
) {
1092 ret
= dump_row(t
, i
, row_dir
);
1102 static int dump_table(const char *dump_dir
, struct osl_table_description
*desc
)
1104 struct osl_table
*t
= NULL
;
1105 int fd
, ret
= fsck_init(desc
, &t
);
1107 char *table_dump_dir
= NULL
;
1111 ret
= osl_mkdir(dump_dir
, 0777);
1112 if (ret
< 0 && ret
!= -E_OSL_DIR_EXISTS
)
1114 table_dump_dir
= make_message("%s/%s", dump_dir
, desc
->name
);
1115 ret
= osl_mkdir(table_dump_dir
, 0777);
1116 if (ret
< 0 && ret
!= -E_OSL_DIR_EXISTS
)
1118 desc_file
= make_message("%s/table_description.c", table_dump_dir
);
1119 ret
= osl_open(desc_file
, O_WRONLY
| O_CREAT
| O_EXCL
, 0644);
1124 ret
= dump_table_desc(t
, fd
);
1128 ret
= dump_rows(table_dump_dir
, t
);
1130 free(table_dump_dir
);
1135 static int fsck(struct osl_table_description
*desc
)
1138 struct osl_table
*t
= NULL
;
1139 uint32_t *lost_bytes
= NULL
;
1141 ret
= fsck_init(desc
, &t
);
1144 ret
= check_index_ranges(t
);
1147 ret
= check_disk_storage_columns(t
);
1150 ret
= prune_invalid_rows_from_index(t
);
1153 ret
= check_for_invalid_objects(t
, &lost_bytes
);
1156 if (ret
> 0 && !OPT_GIVEN(DRY_RUN
)) {
1157 /* at least one mapped data file needs pruning */
1158 ret
= prune_objects(t
, lost_bytes
);
1164 unmap_table(t
, OSL_MARK_CLEAN
);
1170 static int check_table(const char *db_dir
, const char *table_name
)
1172 struct osl_table_description desc
= {
1173 .column_descriptions
= NULL
,
1179 INFO_LOG("checking table %s\n", table_name
);
1180 if (!OPT_GIVEN(NO_FSCK
)) {
1186 if (!OPT_GIVEN(DUMP_DIR
) || !*OPT_STRING_VAL(DUMP_DIR
))
1188 ret
= dump_table(OPT_STRING_VAL(DUMP_DIR
), &desc
);
1191 ERROR_LOG("failed to check table %s: %s\n", table_name
,
1192 fsck_strerror(-ret
));
1194 NOTICE_LOG("successfully checked table %s\n", table_name
);
1198 static int check_all_tables(const char *db_dir
)
1201 struct dirent
*entry
;
1202 int cwd_fd
, ret2
, ret
= fsck_opendir(db_dir
, &dir
, &cwd_fd
);
1206 while ((entry
= readdir(dir
))) {
1209 if (!strcmp(entry
->d_name
, "."))
1211 if (!strcmp(entry
->d_name
, ".."))
1213 if (lstat(entry
->d_name
, &s
) == -1)
1218 ret
= check_table(db_dir
, entry
->d_name
);
1223 ret2
= __fchdir(cwd_fd
);
1224 if (ret2
< 0 && ret
>= 0)
1230 int main(int argc
, char **argv
)
1234 char *errctx
= NULL
;
1238 ret
= lls_parse(argc
, argv
, CMD_PTR
, &lpr
, &errctx
);
1241 ERROR_LOG("%s\n", errctx
);
1242 EMERG_LOG("%s\n", lls_strerror(-ret
));
1245 loglevel
= OPT_UINT32_VAL(LOGLEVEL
);
1246 if (OPT_GIVEN(DETAILED_HELP
)) {
1247 printf("%s\n", lls_long_help(CMD_PTR
));
1250 if (OPT_GIVEN(HELP
)) {
1251 printf("%s\n", lls_short_help(CMD_PTR
));
1254 if (OPT_GIVEN(VERSION
)) {
1256 "oslfsck " OSL_VERSION
"\n"
1257 "Copyright (C) 2008-2009 Andre Noll\n"
1258 "This is free software with ABSOLUTELY NO WARRANTY."
1259 " See COPYING for details.\n"
1260 "Written by Andre Noll.\n"
1261 "Report bugs to <maan@tuebingen.mpg.de>.\n"
1265 dd
= OPT_STRING_VAL(DATABASE_DIR
);
1267 EMERG_LOG("--database-dir: mandatory option not given\n");
1270 INFO_LOG("database dir: %s\n", dd
);
1271 if (lls_num_inputs(lpr
) == 0) {
1272 ret
= check_all_tables(dd
);
1275 for (n
= 0; n
< lls_num_inputs(lpr
); n
++) {
1276 ret
= check_table(dd
, lls_input(n
, lpr
));
1282 ERROR_LOG("%s\n", fsck_strerror(-ret
));
1284 EMERG_LOG("re-run with \"--loglevel %d\" to increase verbosity\n",
1287 NOTICE_LOG("success\n");
1288 return ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
;