[Makefile]: Also create bindir and mandir on install.
[osl.git] / fsck.c
1 /*
2  * Copyright (C) 2007-2009 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file fsck.c The program used to check an osl table. */
8
9 #include <sys/stat.h>
10 #include <sys/types.h>
11 #include <dirent.h>
12 #include <assert.h>
13 #include <pwd.h>
14
15 #include "log.h"
16 #include "osl.h"
17 #include "util.h"
18 #include "osl_core.h"
19 #include "fsck.cmdline.h"
20
21 #define VERSION_TEXT(prefix) "osl" prefix " " VERSION " " "\n" \
22         "Copyright (C) 2008-2009 Andre Noll\n" \
23         "This is free software with ABSOLUTELY NO WARRANTY." \
24         " See COPYING for details.\n" \
25         "Written by Andre Noll.\n" \
26         "Report bugs to <maan@systemlinux.org>.\n"
27
28 /** print out \p VERSION_TEXT and exit if version flag was given */
29 #define HANDLE_VERSION_FLAG(_prefix, _args_info_struct) \
30         if (_args_info_struct.version_given) { \
31                 printf("%s", VERSION_TEXT(_prefix)); \
32                 exit(EXIT_SUCCESS); \
33         }
34
35 static struct fsck_args_info conf;
36
37 #define FSCK_ERRORS \
38         FSCK_ERROR(RANGE_VIOLATION, "range violation detected, very bad"), \
39         FSCK_ERROR(NOT_A_REGULAR_FILE, "not a regular file"), \
40         FSCK_ERROR(SYNTAX, "fsck syntax error"), \
41         FSCK_ERROR(ACCESS, "permission denied"), \
42         FSCK_ERROR(CHDIR, "could not change directory"), \
43         FSCK_ERROR(OPENDIR, "could not open directory"),
44
45 #define FSCK_ERROR_BIT 29
46 #define FSCK_ERROR(num, txt) E_FSCK_ ## num
47 enum {
48         FSCK_DUMMY = (1 << FSCK_ERROR_BIT) - 1,
49         FSCK_ERRORS
50 };
51 #undef FSCK_ERROR
52 #define FSCK_ERROR(num, txt) txt
53 static const char *fsck_errors[] = {
54         FSCK_ERRORS
55 };
56
57 static const char *fsck_strerror(int num)
58 {
59         if (num & (1 << FSCK_ERROR_BIT))
60                 return fsck_errors[num & ((1 << FSCK_ERROR_BIT) - 1)];
61         return osl_strerror(num);
62 }
63
64 extern int loglevel;
65
66 /* taken from git */
67 static signed char hexval_table[256] = {
68          -1, -1, -1, -1, -1, -1, -1, -1,                /* 00-07 */
69          -1, -1, -1, -1, -1, -1, -1, -1,                /* 08-0f */
70          -1, -1, -1, -1, -1, -1, -1, -1,                /* 10-17 */
71          -1, -1, -1, -1, -1, -1, -1, -1,                /* 18-1f */
72          -1, -1, -1, -1, -1, -1, -1, -1,                /* 20-27 */
73          -1, -1, -1, -1, -1, -1, -1, -1,                /* 28-2f */
74           0,  1,  2,  3,  4,  5,  6,  7,                /* 30-37 */
75           8,  9, -1, -1, -1, -1, -1, -1,                /* 38-3f */
76          -1, 10, 11, 12, 13, 14, 15, -1,                /* 40-47 */
77          -1, -1, -1, -1, -1, -1, -1, -1,                /* 48-4f */
78          -1, -1, -1, -1, -1, -1, -1, -1,                /* 50-57 */
79          -1, -1, -1, -1, -1, -1, -1, -1,                /* 58-5f */
80          -1, 10, 11, 12, 13, 14, 15, -1,                /* 60-67 */
81          -1, -1, -1, -1, -1, -1, -1, -1,                /* 68-67 */
82          -1, -1, -1, -1, -1, -1, -1, -1,                /* 70-77 */
83          -1, -1, -1, -1, -1, -1, -1, -1,                /* 78-7f */
84          -1, -1, -1, -1, -1, -1, -1, -1,                /* 80-87 */
85          -1, -1, -1, -1, -1, -1, -1, -1,                /* 88-8f */
86          -1, -1, -1, -1, -1, -1, -1, -1,                /* 90-97 */
87          -1, -1, -1, -1, -1, -1, -1, -1,                /* 98-9f */
88          -1, -1, -1, -1, -1, -1, -1, -1,                /* a0-a7 */
89          -1, -1, -1, -1, -1, -1, -1, -1,                /* a8-af */
90          -1, -1, -1, -1, -1, -1, -1, -1,                /* b0-b7 */
91          -1, -1, -1, -1, -1, -1, -1, -1,                /* b8-bf */
92          -1, -1, -1, -1, -1, -1, -1, -1,                /* c0-c7 */
93          -1, -1, -1, -1, -1, -1, -1, -1,                /* c8-cf */
94          -1, -1, -1, -1, -1, -1, -1, -1,                /* d0-d7 */
95          -1, -1, -1, -1, -1, -1, -1, -1,                /* d8-df */
96          -1, -1, -1, -1, -1, -1, -1, -1,                /* e0-e7 */
97          -1, -1, -1, -1, -1, -1, -1, -1,                /* e8-ef */
98          -1, -1, -1, -1, -1, -1, -1, -1,                /* f0-f7 */
99          -1, -1, -1, -1, -1, -1, -1, -1,                /* f8-ff */
100 };
101
102 static int asc_to_hash(const char *asc_hash, int len, HASH_TYPE *hash)
103 {
104         int i = 0;
105         const unsigned char *asc = (const unsigned char *) asc_hash;
106
107         while (*asc && i++ < len) {
108                 unsigned int val = (hexval_table[asc[0]] << 4) | hexval_table[asc[1]];
109                 if (val & ~0xff)
110                         return -1;
111                 *hash++ = val;
112                 asc += 2;
113
114         }
115         return 1;
116 }
117
118 static int _write_all(int fd, const char *buf, size_t len)
119 {
120         return write_all(fd, buf, &len);
121 }
122
123 /*
124  * Wrapper for malloc().
125  *
126  * \param size The desired new size.
127  *
128  * A wrapper for malloc(3) which exits on errors.
129  *
130  * \return A pointer to the allocated memory, which is suitably aligned for any
131  * kind of variable.
132  *
133  * \sa malloc(3).
134  */
135 __must_check __malloc static void *fsck_malloc(size_t size)
136 {
137         assert(size);
138         void *p = malloc(size);
139
140         if (!p) {
141                 EMERG_LOG("malloc failed (size = %zu),  aborting\n",
142                         size);
143                 exit(EXIT_FAILURE);
144         }
145         return p;
146 }
147
148 /**
149  * Allocate memory and fill with zeros.
150  *
151  * \param size The desired new size.
152  *
153  * A wrapper for calloc(3) which exits on errors.
154  *
155  * \return A pointer to the allocated and zeroed-out memory, which is suitably
156  * aligned for any kind of variable.
157  *
158  * \sa calloc(3)
159  */
160 __must_check __malloc static void *fsck_calloc(size_t size)
161 {
162         void *ret = fsck_malloc(size);
163
164         memset(ret, 0, size);
165         return ret;
166 }
167
168 /**
169  * Save version of strdup().
170  *
171  * \param s The string to be duplicated.
172  *
173  * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
174  * there is no need to check the return value in the caller.
175  *
176  * \return A pointer to the duplicated string. If \p s was the NULL pointer,
177  * an pointer to an empty string is returned.
178  *
179  * \sa strdup(3)
180  */
181 __must_check __malloc static char *fsck_strdup(const char *s)
182 {
183         char *ret;
184
185         if ((ret = strdup(s? s: "")))
186                 return ret;
187         EMERG_LOG("strdup failed, aborting\n");
188         exit(EXIT_FAILURE);
189 }
190
191 /**
192  * Compare two osl objects pointing to unsigned integers of 32 bit size.
193  *
194  * \param obj1 Pointer to the first integer.
195  * \param obj2 Pointer to the second integer.
196  *
197  * \return The values required for an osl compare function.
198  *
199  * \sa osl_compare_func, osl_hash_compare().
200  */
201 static int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2)
202 {
203         uint32_t d1 = read_u32((const char *)obj1->data);
204         uint32_t d2 = read_u32((const char *)obj2->data);
205
206         if (d1 < d2)
207                 return 1;
208         if (d1 > d2)
209                 return -1;
210         return 0;
211 }
212
213 /**
214  * A wrapper for fchdir().
215  *
216  * \param fd An open file descriptor.
217  *
218  * \return Standard.
219  */
220 static inline int __fchdir(int fd)
221 {
222         if (fchdir(fd) >= 0)
223                 return 1;
224         return errno == EACCES? -E_FSCK_ACCESS : -E_FSCK_CHDIR;
225 }
226
227 /**
228  * Wrapper for chdir(2).
229  *
230  * \param path The specified directory.
231  *
232  * \return Standard.
233  */
234 _static_inline_ int __chdir(const char *path)
235 {
236         if (chdir(path) >= 0)
237                 return 1;
238         return errno == EACCES? -E_FSCK_ACCESS : -E_FSCK_CHDIR;
239 }
240
241 /**
242  * Save the cwd and open a given directory.
243  *
244  * \param dirname Path to the directory to open.
245  * \param dir Result pointer.
246  * \param cwd File descriptor of the current working directory.
247  *
248  * \return Standard.
249  *
250  * Opening the current directory (".") and calling fchdir() to return is
251  * usually faster and more reliable than saving cwd in some buffer and calling
252  * chdir() afterwards.
253  *
254  * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
255  * stored in \a cwd. If the function returns success, and \a cwd is not \p
256  * NULL, the caller must close this file descriptor (probably after calling
257  * fchdir(*cwd)).
258  *
259  * On errors, the function undos everything, so the caller needs neither close
260  * any files, nor change back to the original working directory.
261  *
262  * \sa getcwd(3).
263  *
264  */
265 static int fsck_opendir(const char *dirname, DIR **dir, int *cwd)
266 {
267         int ret;
268
269         if (cwd) {
270                 ret = osl_open(".", O_RDONLY, 0);
271                 if (ret < 0)
272                         return ret;
273                 *cwd = ret;
274         }
275         ret = __chdir(dirname);
276         if (ret < 0)
277                 goto close_cwd;
278         *dir = opendir(".");
279         if (*dir)
280                 return 1;
281         ret = errno == EACCES? -E_FSCK_ACCESS : -E_FSCK_OPENDIR;
282 /* Ignore return value of fchdir() and close(). We're busted anyway. */
283         if (cwd)
284                 fchdir(*cwd);
285 close_cwd:
286         if (cwd)
287                 close(*cwd);
288         return ret;
289 }
290
291
292
293 /**
294  * Traverse the given directory recursively.
295  *
296  * \param dirname The directory to traverse.
297  * \param func The function to call for each entry.
298  * \param private_data Pointer to an arbitrary data structure.
299  *
300  * For each regular file under \a dirname, the supplied function \a func is
301  * called.  The full path of the regular file and the \a private_data pointer
302  * are passed to \a func. Directories for which the calling process has no
303  * permissions to change to are silently ignored.
304  *
305  * \return Standard.
306  */
307 static int for_each_file_in_dir(const char *dirname,
308                 int (*func)(const char *, void *), void *private_data)
309 {
310         DIR *dir;
311         struct dirent *entry;
312         int cwd_fd, ret2, ret = fsck_opendir(dirname, &dir, &cwd_fd);
313
314         if (ret < 0)
315                 return ret == -E_FSCK_ACCESS? 1 : ret;
316         /* scan cwd recursively */
317         while ((entry = readdir(dir))) {
318                 mode_t m;
319                 char *tmp;
320                 struct stat s;
321
322                 if (!strcmp(entry->d_name, "."))
323                         continue;
324                 if (!strcmp(entry->d_name, ".."))
325                         continue;
326                 if (lstat(entry->d_name, &s) == -1)
327                         continue;
328                 m = s.st_mode;
329                 if (!S_ISREG(m) && !S_ISDIR(m))
330                         continue;
331                 tmp = make_message("%s/%s", dirname, entry->d_name);
332                 if (!S_ISDIR(m)) {
333                         ret = func(tmp, private_data);
334                         free(tmp);
335                         if (ret < 0)
336                                 goto out;
337                         continue;
338                 }
339                 /* directory */
340                 ret = for_each_file_in_dir(tmp, func, private_data);
341                 free(tmp);
342                 if (ret < 0)
343                         goto out;
344         }
345         ret = 1;
346 out:
347         closedir(dir);
348         ret2 = __fchdir(cwd_fd);
349         if (ret2 < 0 && ret >= 0)
350                 ret = ret2;
351         close(cwd_fd);
352         return ret;
353 }
354
355 /*
356  * check for object boundary violations
357  *
358  * test whether the range pointed to by the index entry for a given cell is
359  * contained in mapped data file. This should always be the case. Otherwise
360  * we are in real trouble.
361  */
362 static int check_range(struct osl_table *t, uint32_t row_num, uint32_t col_num)
363 {
364         char *index_entry;
365         struct osl_object obj;
366         struct osl_column *col;
367         int ret;
368         char *map_start, *obj_start;
369
370         ret = get_cell_index(t, row_num, col_num, &index_entry);
371         if (ret < 0)
372                 return ret;
373         ret = get_mapped_object(t, col_num, row_num, &obj);
374         if (ret < 0)
375                 return ret;
376         col = t->columns + col_num;
377         obj_start = obj.data;
378         map_start = col->data_map.data;
379 //      INFO_LOG("obj: %p..%p\n", obj_start, obj_start + obj.size);
380 //      INFO_LOG("map: %p..%p\n", map_start, map_start + col->data_map.size);
381         if (obj_start < map_start || obj_start + obj.size > map_start + col->data_map.size) {
382                 CRIT_LOG("range violation in row %u, col %u\n", row_num,
383                         col_num);
384                 return -E_FSCK_RANGE_VIOLATION;
385         }
386         DEBUG_LOG("col %u: ok\n", col_num);
387         return 1;
388 }
389
390 static int fsck_mark_row_invalid(struct osl_table *t, int i)
391 {
392         if (conf.dry_run_given)
393                 return 0;
394         return mark_row_invalid(t, i);
395 }
396
397 /*
398  * check all cells of the given table for boundary violations
399  */
400 static int check_index_ranges(struct osl_table *t)
401 {
402         int i, j, ret;
403
404         INFO_LOG("checking for range violations in index\n");
405         //DEBUG_LOG("%d rows. %d columns\n", t->num_rows, t->desc->num_columns);
406         t->num_invalid_rows = 0;
407         for (i = 0; i < t->num_rows; i++) {
408                 const struct osl_column_description *cd;
409
410                 if (row_is_invalid(t, i)) {
411                         t->num_invalid_rows++;
412                         continue;
413                 }
414                 FOR_EACH_MAPPED_COLUMN(j, t, cd) {
415                         ret = check_range(t, i, j);
416                         if (ret < 0) {
417                                 if (ret != -E_FSCK_RANGE_VIOLATION)
418                                         goto err;
419                                 ret = fsck_mark_row_invalid(t, i);
420                                 if (ret < 0)
421                                         goto err;
422                                 t->num_invalid_rows++;
423                                 break;
424                         }
425                 }
426
427         }
428         if (t->num_invalid_rows)
429                 NOTICE_LOG("ranges OK. %d invalid row(s) detected\n",
430                         t->num_invalid_rows);
431         else
432                 INFO_LOG("no invalid rows, no range violations, good\n");
433         return 1;
434 err:
435         return ret;
436 }
437
438 static int move_index_entry(struct osl_table *t, uint32_t dest, uint32_t src)
439 {
440         char *dest_ie, *src_ie;
441         int ret = get_row_index(t, dest, &dest_ie);
442
443         if (ret < 0)
444                 return ret;
445         ret = get_row_index(t, src, &src_ie);
446         if (ret < 0)
447                 return ret;
448         INFO_LOG("moving entry #%u to position %u\n", src, dest);
449         memcpy(dest_ie, src_ie, t->row_index_size);
450         return 1;
451 }
452
453 static int map_index(const struct osl_table_description *desc, struct osl_object *map)
454 {
455         char *filename = index_filename(desc);
456         int ret;
457
458         ret = mmap_full_file(filename, O_RDWR, &map->data, &map->size, NULL);
459         DEBUG_LOG("mapping index %s: ret: %d, size: %zu\n", filename, ret, map->size);
460         free(filename);
461         return ret;
462 }
463
464 static int prune_invalid_rows_from_index(struct osl_table *t)
465 {
466         uint32_t top = 0, bottom;
467         char *filename;
468         int ret;
469
470         if (!t->num_invalid_rows) {
471                 INFO_LOG("all rows are valid, good\n");
472                 return 1;
473         }
474         NOTICE_LOG("index contains %u invalid row(s) (%d bytes)\n",
475                 t->num_invalid_rows, t->row_index_size * t->num_invalid_rows);
476         if (conf.dry_run_given)
477                 return 0;
478         NOTICE_LOG("removing invalid rows from index\n");
479         bottom = t->num_rows - 1;
480         while (top < bottom) {
481                 if (!row_is_invalid(t, top)) {
482                         top++;
483                         continue;
484                 }
485                 while (bottom > top) {
486                         if (row_is_invalid(t, bottom)) {
487                                 bottom--;
488                                 continue;
489                         }
490                         /* move bottom index entry to top */
491                         move_index_entry(t, top, bottom);
492                         bottom--;
493                         top++;
494                         break;
495                 }
496         }
497         DEBUG_LOG("unmapping index\n");
498         osl_munmap(t->index_map.data, t->index_map.size);
499         filename = index_filename(t->desc);
500         ret = truncate_file(filename, t->row_index_size
501                 * t->num_invalid_rows);
502         free(filename);
503         if (ret < 0)
504                 return ret;
505         ret = map_index(t->desc, &t->index_map);
506         if (ret < 0)
507                 return ret;
508         t->num_rows = table_num_rows(t);
509         return 1;
510 }
511
512 static int check_for_invalid_objects(struct osl_table *t, uint32_t **lost_bytes)
513 {
514         int i, j, ret;
515         const struct osl_column_description *cd;
516         uint32_t *loss = fsck_malloc(sizeof(uint32_t) * t->desc->num_columns);
517
518         INFO_LOG("looking for mapped objects not contained in index\n");
519         /* first count used bytes */
520         FOR_EACH_MAPPED_COLUMN(i, t, cd) {
521                 loss[i] = t->columns[i].data_map.size;
522                 DEBUG_LOG("column %i data map: %zu bytes\n", i,
523                         t->columns[i].data_map.size);
524                 for (j = 0; j < t->num_rows; j++) {
525                         struct osl_object obj;
526                         ret = get_mapped_object(t, i, j, &obj);
527                         if (ret < 0)
528                                 goto err;
529                         loss[i] -= obj.size;
530                 }
531         }
532         ret = 0;
533         FOR_EACH_MAPPED_COLUMN(i, t, cd) {
534                 if (loss[i]) {
535                         NOTICE_LOG("column %u contains %u lost bytes\n",
536                                 i, loss[i]);
537                         ret = 1;
538                 }
539         }
540         if (!ret)
541                 INFO_LOG("all mapped objects are valid, good\n");
542         *lost_bytes = loss;
543         return ret;
544 err:
545         free(loss);
546         return ret;
547 }
548
549 /* prune_invalid_rows() must be run on the table before calling this */
550 static int prune_mapped_column(struct osl_table *t, uint32_t col_num, int fd)
551 {
552         int i, ret;
553         uint32_t written = 0;
554         struct osl_column *col = t->columns + col_num;
555
556         INFO_LOG("pruning col %u\n", col_num);
557         for (i = 0; i < t->num_rows; i++) {
558                 struct osl_object obj;
559                 char *index_entry;
560
561                 DEBUG_LOG("checking row %u/%u\n", i, t->num_rows);
562                 ret = get_mapped_object(t, col_num, i, &obj);
563                 if (ret < 0)
564                         return ret;
565                 ret = _write_all(fd, (char *)(obj.data), obj.size);
566                 if (ret < 0)
567                         return ret;
568                 written += obj.size;
569                 ret = get_row_index(t, i, &index_entry);
570                 if (ret < 0)
571                         return ret;
572                 update_cell_index(index_entry, col, written, obj.size);
573         }
574         return 1;
575 }
576
577 static int prune_objects(struct osl_table *t, uint32_t *lost_bytes)
578 {
579         int i, ret;
580         const struct osl_column_description *cd;
581         char **col_filenames = fsck_calloc(t->desc->num_columns * sizeof(char *));
582         char **new_col_filenames = fsck_calloc(t->desc->num_columns * sizeof(char *));
583         char *idx_filename = index_filename(t->desc);
584         char *old_idx_filename = make_message("%s.bak", idx_filename);
585         int fd;
586
587         NOTICE_LOG("removing unreferenced objects from data files\n");
588         /* first make a copy of the index */
589         DEBUG_LOG("opening %s\n", old_idx_filename);
590         ret = osl_open(old_idx_filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
591         if (ret < 0)
592                 goto out_free;
593         fd = ret;
594         ret = _write_all(fd, t->index_map.data, t->index_map.size);
595         close(fd);
596         if (ret < 0)
597                 goto out_free;
598         FOR_EACH_MAPPED_COLUMN(i, t, cd) {
599                 if (!lost_bytes[i])
600                         continue;
601                 col_filenames[i] = column_filename(t, i);
602                 new_col_filenames[i] = make_message("%s.fsck", col_filenames[i]);
603                 ret = osl_open(new_col_filenames[i], O_WRONLY | O_CREAT | O_EXCL, 0644);
604                 if (ret < 0)
605                         goto out_unlink_data;
606                 fd = ret;
607                 ret = prune_mapped_column(t, i, fd);
608                 close(fd);
609                 if (ret < 0)
610                         goto out_unlink_data;
611         }
612         ret = unmap_table(t, OSL_MARK_CLEAN);
613         if (ret < 0)
614                 goto out_unlink_data;
615         FOR_EACH_MAPPED_COLUMN(i, t, cd) {
616                 if (!lost_bytes[i])
617                         continue;
618                 ret = osl_rename(new_col_filenames[i], col_filenames[i]);
619                 if (ret < 0) { /* we're kinda screwed here */
620                         CRIT_LOG("rename of col %i failed: %s\n", i,
621                                 osl_strerror(errno));
622                         goto out_free;
623                 }
624         }
625         unlink(old_idx_filename);
626         ret = map_table(t, 0);
627         goto out_free;
628 out_unlink_data:
629         FOR_EACH_MAPPED_COLUMN(i, t, cd)
630                 unlink(new_col_filenames[i]);
631 out_free:
632         free(old_idx_filename);
633         free(idx_filename);
634         FOR_EACH_MAPPED_COLUMN(i, t, cd) {
635                 free(col_filenames[i]);
636                 free(new_col_filenames[i]);
637         }
638         free(col_filenames);
639         free(new_col_filenames);
640         return ret;
641 }
642
643 static struct osl_column_description hash_tree_table_cols[] = {
644         {
645                 .storage_type = OSL_NO_STORAGE,
646                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
647                 .name = "hash",
648                 .compare_function = uint32_compare,
649                 .data_size = HASH_SIZE
650         },
651 };
652
653 static const struct osl_table_description hash_tree_table_desc = {
654         .dir = "/", /* irrelevant */
655         .name = "hash_tree",
656         .num_columns = 1,
657         .flags = 0,
658         .column_descriptions = hash_tree_table_cols
659 };
660
661 /**
662  * The hash_tree table contains all hashes of the disk storage name column.
663  * of each row. It is used for checking if a disk storage file has a reference
664  * in the table.
665  */
666 static struct osl_table *hash_tree_table;
667 static HASH_TYPE *hashes;
668
669 static int check_disk_storage_column(struct osl_table *t, int row_num,
670                 int col_num, char *ds_name, unsigned *num_missing_objects)
671 {
672         int ret;
673         struct stat statbuf;
674         char *path = disk_storage_path(t, col_num, ds_name);
675         unsigned dsnc = t->disk_storage_name_column;
676         struct osl_object obj;
677
678         DEBUG_LOG("checking if %s is a regular file\n", path);
679         ret = stat(path, &statbuf);
680         if (ret < 0 && errno == ENOENT) {
681                 struct osl_row *row;
682                 (*num_missing_objects)++;
683                 ERROR_LOG("row %d: object %s is missing\n", row_num, path);
684                 ret = 0;
685                 if (conf.dry_run_given)
686                         goto out;
687                 NOTICE_LOG("trying to delete row %d\n", row_num);
688                 ret = osl_get_row(t, dsnc, &obj, &row);
689                 if (ret < 0) {
690                         CRIT_LOG("unable to get row %d\n", row_num);
691                         fsck_mark_row_invalid(t, row_num);
692                         CRIT_LOG("Please re-run fsck\n");
693                         goto out;
694                 }
695                 ret = osl_del_row(t, row);
696                 if (ret < 0)
697                         goto out;
698         }
699 out:
700         free(path);
701         if (ret < 0)
702                 return ret;
703         ret = -E_FSCK_NOT_A_REGULAR_FILE;
704         if (!(S_IFREG & statbuf.st_mode))
705                 return ret;
706         return 1;
707 }
708
709 static int check_disk_storage_presence(struct osl_table *t)
710 {
711         int ret, i, j;
712         struct osl_object obj, hash_obj = {.size = HASH_SIZE};
713         char *ds_name;
714         const struct osl_column_description *cd;
715         unsigned dsnc = t->disk_storage_name_column, missing_objects = 0;
716
717         if (!t->num_rows)
718                 return 1;
719         hashes = fsck_malloc(t->num_rows * HASH_SIZE);
720         INFO_LOG("looking for missing disk storage objects\n");
721         for (i = 0; i < t->num_rows; i++) {
722                 if (row_is_invalid(t, i))
723                         continue;
724                 ret = get_mapped_object(t, dsnc, i, &obj);
725                 if (ret < 0)
726                         return ret;
727                 hash_object(&obj, hashes + i * HASH_SIZE);
728                 hash_obj.data = hashes + i * HASH_SIZE;
729                 osl_add_row(hash_tree_table, &hash_obj);
730                 ds_name = disk_storage_name_of_hash(t, hashes + i * HASH_SIZE);
731                 FOR_EACH_DISK_STORAGE_COLUMN(j, t, cd) {
732                         ret = check_disk_storage_column(t, i, j, ds_name,
733                                 &missing_objects);
734                         if (ret < 0)
735                                 goto err;
736                 }
737                 free(ds_name);
738         }
739         if (!missing_objects)
740                 INFO_LOG("all referenced disk storage objects exist, good\n");
741         else
742                 NOTICE_LOG("%d missing object(s)\n", missing_objects);
743         return missing_objects;
744 err:
745         free(ds_name);
746         return ret;
747 }
748
749 static int dummy_compare(const struct osl_object *obj1, const struct osl_object *obj2)
750 {
751         if (obj1 < obj2)
752                 return -1;
753         if (obj1 > obj2)
754                 return 1;
755         return 0;
756 }
757
758 static unsigned files_pruned;
759
760 static int prune_disk_storage_file(const char *path, void *private_data)
761 {
762         HASH_TYPE hash[HASH_SIZE];
763         uint8_t flags = *(uint8_t *)private_data;
764         struct osl_object obj = {.data = hash, .size = HASH_SIZE};
765         struct osl_row *row;
766         int ret = -1;
767         size_t len = strlen(path);
768
769
770         DEBUG_LOG("path: %s\n", path);
771         if (flags & OSL_LARGE_TABLE) {
772                 if (len < HASH_SIZE * 2 + 2)
773                         goto invalid;
774 //              NOTICE_LOG("p: %s\n", path + len - 2 * HASH_SIZE - 1);
775                 ret = asc_to_hash(path + len - 2 * HASH_SIZE - 1, 1, hash);
776                 if (ret < 0)
777                         goto invalid;
778                 ret = asc_to_hash(path + len - 2 * HASH_SIZE + 2, HASH_SIZE - 1,
779                         hash + 1);
780                 if (ret < 0)
781                         goto invalid;
782 //              INFO_LOG("high: %x, low: %x, hash: %x\n", high, low, hash);
783         } else {
784                 if (len < 2 * HASH_SIZE + 1)
785                         goto invalid;
786                 ret = asc_to_hash(path + len - 2 * HASH_SIZE, 2 * HASH_SIZE, hash);
787                 if (ret < 0)
788                         goto invalid;
789 //              INFO_LOG("hash: %x\n", hash);
790         }
791 #if 0
792 {
793         char asc[2 * HASH_SIZE + 1];
794         hash_to_asc(hash, asc);
795         NOTICE_LOG("before: %s\nafter: %s\n", path, asc);
796 }
797 #endif
798         ret = osl_get_row(hash_tree_table, 0, &obj, &row);
799         if (ret >= 0)
800                 return 1;
801         NOTICE_LOG("unreferenced file in hash dir: %s\n", path);
802         goto remove;
803 invalid:
804         ERROR_LOG("could not read hash value of %s\n", path);
805 remove:
806         if (conf.dry_run_given)
807                 return 0;
808         NOTICE_LOG("removing %s\n", path);
809         unlink(path);
810         files_pruned++;
811         return 1;
812 }
813
814 static int prune_disk_storage_files(struct osl_table *t)
815 {
816         int i, ret = 1;
817         const struct osl_column_description *cd;
818
819         INFO_LOG("looking for unreferenced disk storage files\n");
820         FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
821                 char *dirname = column_filename(t, i);
822                 uint8_t flags = t->desc->flags;
823                 ret = for_each_file_in_dir(dirname, prune_disk_storage_file,
824                         &flags);
825                 free(dirname);
826         }
827         if (files_pruned)
828                 NOTICE_LOG("%u disk storage files deleted\n",
829                         files_pruned);
830         else
831                 INFO_LOG("all files are are referenced, good\n");
832         return ret;
833 }
834
835 static int check_disk_storage_columns(struct osl_table *t)
836 {
837         int ret, i;
838         const struct osl_column_description *cd;
839
840         if (!t->num_disk_storage_columns) {
841                 INFO_LOG("no disk storage columns in table '%s', "
842                         "skipping checks\n", t->desc->name);
843                 return 1;
844         }
845         FOR_EACH_COLUMN(i, t->desc, cd)
846                 t->desc->column_descriptions[i].compare_function = dummy_compare;
847         ret = init_rbtrees(t);
848         if (ret < 0)
849                 return ret;
850         INFO_LOG("creating rbtree for disk storage hash values\n");
851         ret = osl_open_table(&hash_tree_table_desc, &hash_tree_table);
852         if (ret < 0)
853                 goto out;
854         ret = check_disk_storage_presence(t);
855         if (ret < 0)
856                 goto out_close_hash_tree;
857         ret = prune_disk_storage_files(t);
858 out_close_hash_tree:
859         osl_close_table(hash_tree_table, 0);
860         free(hashes);
861         hashes = NULL;
862 out:
863         clear_rbtrees(t); /* TODO why are we doing that here? Seems odd */
864         return ret;
865 }
866
867 static void set_dummy_contents(struct osl_table_description *desc)
868 {
869         int i;
870         struct osl_column_description *cd;
871
872         for (i = 0; i < desc->num_columns; i++) {
873                 cd = get_column_description(desc, i);
874                 cd->compare_function = dummy_compare;
875         }
876 }
877
878 static int fsck_init(struct osl_table_description *desc, struct osl_table **t)
879 {
880         struct osl_object map;
881         int ret = map_index(desc, &map);
882
883         if (ret < 0)
884                 goto out;
885         ret = read_table_desc(&map, desc);
886         if (ret < 0) {
887                 osl_munmap(map.data, map.size);
888                 goto out;
889         }
890         set_dummy_contents(desc);
891         ret = init_table_structure(desc, t);
892         if (ret < 0) {
893                 osl_munmap(map.data, map.size);
894                 goto out;
895         }
896         DEBUG_LOG("unmapping index\n");
897         osl_munmap(map.data, map.size);
898         if (conf.force_given)
899                 ret = map_table(*t, (MAP_TBL_FL_IGNORE_DIRTY));
900         else
901                 ret = map_table(*t, 0);
902         if (ret >= 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);
906         }
907 out:
908         return ret;
909 }
910
911 static void fsck_cleanup(struct osl_table *t)
912 {
913         int i;
914
915         if (!t)
916                 return;
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);
922                 }
923                 free(t->desc->column_descriptions);
924         }
925         free(t->columns);
926         free(t);
927
928 }
929
930 #define ST_CASE(st) case st: return #st
931
932 static const char *get_asc_storage_type(enum osl_storage_type st)
933 {
934         switch (st) {
935                 ST_CASE(OSL_MAPPED_STORAGE);
936                 ST_CASE(OSL_DISK_STORAGE);
937                 ST_CASE(OSL_NO_STORAGE);
938         }
939         return NULL;
940 }
941
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)
945
946
947 static char *get_asc_storage_flags(enum osl_storage_type sf)
948 {
949         char *asc_sf = NULL;
950
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);
954         return asc_sf;
955 }
956
957 static int dump_table_desc(struct osl_table *t, int fd)
958 {
959         const struct osl_table_description *desc = t->desc;
960         int ret, i;
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));
964         if (ret < 0)
965                 return ret;
966         free(msg);
967         FOR_EACH_COLUMN(i, desc, cd) {
968                 const char *asc_st;
969                 msg = make_message("\t[%d] = {\n", i);
970                 ret = _write_all(fd, msg, strlen(msg));
971                 if (ret < 0)
972                         return ret;
973                 free(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));
977                 if (ret < 0)
978                         return ret;
979                 free(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);
983                         free(asc_sf);
984                         ret = _write_all(fd, msg, strlen(msg));
985                         if (ret < 0)
986                                 return ret;
987                         free(msg);
988                 }
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));
992                         if (ret < 0)
993                                 return ret;
994                         free(msg);
995                 }
996                 msg = make_message("\t\t.name = \"%s\",\n", cd->name);
997                 ret = _write_all(fd, msg, strlen(msg));
998                 if (ret < 0)
999                         return ret;
1000                 free(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));
1004                         if (ret < 0)
1005                                 return ret;
1006                         free(msg);
1007                 }
1008                 msg = make_message("\t},\n");
1009                 ret = _write_all(fd, msg, strlen(msg));
1010                 if (ret < 0)
1011                         return ret;
1012                 free(msg);
1013         }
1014         msg = make_message("};\n");
1015         ret = _write_all(fd, msg, strlen(msg));
1016         if (ret < 0)
1017                 return ret;
1018         free(msg);
1019         return 1;
1020 }
1021
1022 static int dump_row(struct osl_table *t, unsigned row_num, const char *row_dir)
1023 {
1024         int ret, i;
1025         const struct osl_column_description *cd;
1026         unsigned dsnc;
1027         struct osl_object obj;
1028         char *ds_name;
1029         HASH_TYPE hash[HASH_SIZE];
1030         char *filename;
1031
1032         FOR_EACH_MAPPED_COLUMN(i, t, cd) {
1033                 ret = get_mapped_object(t, i, row_num, &obj);
1034                 if (ret < 0)
1035                         return ret;
1036                 filename = make_message("%s/col_%03u", row_dir, i);
1037                 ret = write_file(filename, obj.data, obj.size);
1038                 free(filename);
1039                 if (ret < 0)
1040                         return ret;
1041         }
1042         if (!t->num_disk_storage_columns)
1043                 return 1;
1044         dsnc = t->disk_storage_name_column;
1045         ret = get_mapped_object(t, dsnc, row_num, &obj);
1046         if (ret < 0)
1047                 return ret;
1048         hash_object(&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);
1053                 free(filename);
1054                 if (ret < 0)
1055                         goto out;
1056                 filename = make_message("%s/col_%03u", row_dir, i);
1057                 ret = write_file(filename, obj.data, obj.size);
1058                 free(filename);
1059                 if (ret < 0)
1060                         goto out;
1061         }
1062         ret = 1;
1063 out:
1064         free(ds_name);
1065         return ret;
1066 }
1067
1068 static int dump_rows(char *dump_dir, struct osl_table *t)
1069 {
1070         unsigned i;
1071         char *current_dir = NULL;
1072         int ret = 0;
1073
1074         for (i = 0; i < t->num_rows; i++) {
1075                 char *row_dir;
1076                 if (row_is_invalid(t, i))
1077                         continue;
1078                 if (!(i % 1000)) {
1079                         free(current_dir);
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)
1084                                 goto out;
1085                 }
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) {
1089                         free(row_dir);
1090                         goto out;
1091                 }
1092                 ret = dump_row(t, i, row_dir);
1093                 free(row_dir);
1094                 if (ret < 0)
1095                         goto out;
1096         }
1097 out:
1098         free(current_dir);
1099         return ret;
1100 }
1101
1102 static int dump_table(char *dump_dir, struct osl_table_description *desc)
1103 {
1104         struct osl_table *t = NULL;
1105         int fd, ret = fsck_init(desc, &t);
1106         char *desc_file;
1107         char *table_dump_dir = NULL;
1108
1109         if (ret < 0)
1110                 goto out;
1111         ret = osl_mkdir(dump_dir, 0777);
1112         if (ret < 0 && ret != -E_OSL_DIR_EXISTS)
1113                 goto out;
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)
1117                 goto out;
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);
1120         free(desc_file);
1121         if (ret < 0)
1122                 goto out;
1123         fd = ret;
1124         ret = dump_table_desc(t, fd);
1125         close(fd);
1126         if (ret < 0)
1127                 goto out;
1128         ret = dump_rows(table_dump_dir, t);
1129 out:
1130         free(table_dump_dir);
1131         fsck_cleanup(t);
1132         return ret;
1133 }
1134
1135 static int fsck(struct osl_table_description *desc)
1136 {
1137         int ret;
1138         struct osl_table *t = NULL;
1139         uint32_t *lost_bytes = NULL;
1140
1141         ret = fsck_init(desc, &t);
1142         if (ret < 0)
1143                 goto out;
1144         ret = check_index_ranges(t);
1145         if (ret < 0)
1146                 goto out_unmap;
1147         ret = check_disk_storage_columns(t);
1148         if (ret < 0)
1149                 goto out_unmap;
1150         ret = prune_invalid_rows_from_index(t);
1151         if (ret < 0)
1152                 goto out_unmap;
1153         ret = check_for_invalid_objects(t, &lost_bytes);
1154         if (ret < 0)
1155                 goto out_unmap;
1156         if (ret > 0 && !conf.dry_run_given) { /* at least one mapped data file needs pruning */
1157                 ret = prune_objects(t, lost_bytes);
1158                 if (ret < 0)
1159                         goto out_unmap;
1160         }
1161         free(lost_bytes);
1162 out_unmap:
1163         unmap_table(t, OSL_MARK_CLEAN);
1164 out:
1165         fsck_cleanup(t);
1166         return ret;
1167 }
1168
1169 static int check_table(char *db_dir, char *table_name)
1170 {
1171         struct osl_table_description desc = {
1172                 .column_descriptions = NULL,
1173                 .dir = db_dir,
1174                 .name = table_name
1175         };
1176         int ret;
1177
1178         INFO_LOG("checking table %s\n", table_name);
1179         if (!conf.no_fsck_given) {
1180                 ret = fsck(&desc);
1181                 if (ret < 0)
1182                         goto out;
1183         }
1184         ret = 1;
1185         if (!conf.dump_dir_given || !*conf.dump_dir_arg)
1186                 goto out;
1187         ret = dump_table(conf.dump_dir_arg, &desc);
1188 out:
1189         if (ret < 0)
1190                 ERROR_LOG("failed to check table %s: %s\n", table_name,
1191                         fsck_strerror(-ret));
1192         else
1193                 NOTICE_LOG("successfully checked table %s\n", table_name);
1194         return ret;
1195 }
1196
1197 static int check_all_tables(char *db_dir)
1198 {
1199         DIR *dir;
1200         struct dirent *entry;
1201         int cwd_fd, ret2, ret = fsck_opendir(db_dir, &dir, &cwd_fd);
1202
1203         if (ret < 0)
1204                 return ret;
1205         while ((entry = readdir(dir))) {
1206                 mode_t m;
1207                 struct stat s;
1208                 if (!strcmp(entry->d_name, "."))
1209                         continue;
1210                 if (!strcmp(entry->d_name, ".."))
1211                         continue;
1212                 if (lstat(entry->d_name, &s) == -1)
1213                         continue;
1214                 m = s.st_mode;
1215                 if (!S_ISDIR(m))
1216                         continue;
1217                 ret = check_table(db_dir, entry->d_name);
1218                 if (ret < 0)
1219                         break;
1220         }
1221         closedir(dir);
1222         ret2 = __fchdir(cwd_fd);
1223         if (ret2 < 0 && ret >= 0)
1224                 ret = ret2;
1225         close(cwd_fd);
1226         return ret;
1227 }
1228
1229 int main(int argc, char **argv)
1230 {
1231         int i, ret;
1232         struct fsck_cmdline_parser_params params = {
1233                 .override = 0,
1234                 .initialize = 1,
1235                 .check_required = 1,
1236                 .check_ambiguity = 1,
1237                 .print_errors = 1
1238         };
1239         ret = fsck_cmdline_parser_ext(argc, argv, &conf, &params);
1240         if (ret < 0) {
1241                 loglevel = EMERG;
1242                 ret = -E_FSCK_SYNTAX;
1243                 goto out;
1244         }
1245         loglevel = conf.loglevel_arg;
1246         HANDLE_VERSION_FLAG("fsck", conf);
1247         INFO_LOG("database dir: %s\n", conf.database_dir_arg);
1248         if (!conf.inputs_num) {
1249                 ret = check_all_tables(conf.database_dir_arg);
1250                 goto out;
1251         }
1252         for (i = 0; i < conf.inputs_num; i++) {
1253                 ret = check_table(conf.database_dir_arg, conf.inputs[i]);
1254                 if (ret < 0)
1255                         break;
1256         }
1257 out:
1258         if (ret < 0) {
1259                 ERROR_LOG("%s\n", fsck_strerror(-ret));
1260                 if (conf.loglevel_arg > 1)
1261                         EMERG_LOG("re-run with \"--loglevel %d\" to increase verbosity\n",
1262                                 conf.loglevel_arg - 1);
1263         } else
1264                 NOTICE_LOG("success\n");
1265         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
1266 }