README: Remove duplicate "network".
[osl.git] / fsck.c
1 /*
2  * Copyright (C) 2007-2009 Andre Noll <maan@tuebingen.mpg.de>
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@tuebingen.mpg.de>.\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                 int __a_unused ret2 = fchdir(*cwd); /* STFU, gcc */
285         }
286 close_cwd:
287         if (cwd)
288                 close(*cwd);
289         return ret;
290 }
291
292
293
294 /**
295  * Traverse the given directory recursively.
296  *
297  * \param dirname The directory to traverse.
298  * \param func The function to call for each entry.
299  * \param private_data Pointer to an arbitrary data structure.
300  *
301  * For each regular file under \a dirname, the supplied function \a func is
302  * called.  The full path of the regular file and the \a private_data pointer
303  * are passed to \a func. Directories for which the calling process has no
304  * permissions to change to are silently ignored.
305  *
306  * \return Standard.
307  */
308 static int for_each_file_in_dir(const char *dirname,
309                 int (*func)(const char *, void *), void *private_data)
310 {
311         DIR *dir;
312         struct dirent *entry;
313         int cwd_fd, ret2, ret = fsck_opendir(dirname, &dir, &cwd_fd);
314
315         if (ret < 0)
316                 return ret == -E_FSCK_ACCESS? 1 : ret;
317         /* scan cwd recursively */
318         while ((entry = readdir(dir))) {
319                 mode_t m;
320                 char *tmp;
321                 struct stat s;
322
323                 if (!strcmp(entry->d_name, "."))
324                         continue;
325                 if (!strcmp(entry->d_name, ".."))
326                         continue;
327                 if (lstat(entry->d_name, &s) == -1)
328                         continue;
329                 m = s.st_mode;
330                 if (!S_ISREG(m) && !S_ISDIR(m))
331                         continue;
332                 tmp = make_message("%s/%s", dirname, entry->d_name);
333                 if (!S_ISDIR(m)) {
334                         ret = func(tmp, private_data);
335                         free(tmp);
336                         if (ret < 0)
337                                 goto out;
338                         continue;
339                 }
340                 /* directory */
341                 ret = for_each_file_in_dir(tmp, func, private_data);
342                 free(tmp);
343                 if (ret < 0)
344                         goto out;
345         }
346         ret = 1;
347 out:
348         closedir(dir);
349         ret2 = __fchdir(cwd_fd);
350         if (ret2 < 0 && ret >= 0)
351                 ret = ret2;
352         close(cwd_fd);
353         return ret;
354 }
355
356 /*
357  * check for object boundary violations
358  *
359  * test whether the range pointed to by the index entry for a given cell is
360  * contained in mapped data file. This should always be the case. Otherwise
361  * we are in real trouble.
362  */
363 static int check_range(struct osl_table *t, uint32_t row_num, uint32_t col_num)
364 {
365         char *index_entry;
366         struct osl_object obj;
367         struct osl_column *col;
368         int ret;
369         char *map_start, *obj_start;
370
371         ret = get_cell_index(t, row_num, col_num, &index_entry);
372         if (ret < 0)
373                 return ret;
374         ret = get_mapped_object(t, col_num, row_num, &obj);
375         if (ret < 0)
376                 return ret;
377         col = t->columns + col_num;
378         obj_start = obj.data;
379         map_start = col->data_map.data;
380 //      INFO_LOG("obj: %p..%p\n", obj_start, obj_start + obj.size);
381 //      INFO_LOG("map: %p..%p\n", map_start, map_start + col->data_map.size);
382         if (obj_start < map_start || obj_start + obj.size > map_start + col->data_map.size) {
383                 CRIT_LOG("range violation in row %u, col %u\n", row_num,
384                         col_num);
385                 return -E_FSCK_RANGE_VIOLATION;
386         }
387         DEBUG_LOG("col %u: ok\n", col_num);
388         return 1;
389 }
390
391 static int fsck_mark_row_invalid(struct osl_table *t, int i)
392 {
393         if (conf.dry_run_given)
394                 return 0;
395         return mark_row_invalid(t, i);
396 }
397
398 /*
399  * check all cells of the given table for boundary violations
400  */
401 static int check_index_ranges(struct osl_table *t)
402 {
403         int i, j, ret;
404
405         INFO_LOG("checking for range violations in index\n");
406         //DEBUG_LOG("%d rows. %d columns\n", t->num_rows, t->desc->num_columns);
407         t->num_invalid_rows = 0;
408         for (i = 0; i < t->num_rows; i++) {
409                 const struct osl_column_description *cd;
410
411                 if (row_is_invalid(t, i)) {
412                         t->num_invalid_rows++;
413                         continue;
414                 }
415                 FOR_EACH_MAPPED_COLUMN(j, t, cd) {
416                         ret = check_range(t, i, j);
417                         if (ret < 0) {
418                                 if (ret != -E_FSCK_RANGE_VIOLATION)
419                                         goto err;
420                                 ret = fsck_mark_row_invalid(t, i);
421                                 if (ret < 0)
422                                         goto err;
423                                 t->num_invalid_rows++;
424                                 break;
425                         }
426                 }
427
428         }
429         if (t->num_invalid_rows)
430                 NOTICE_LOG("ranges OK. %d invalid row(s) detected\n",
431                         t->num_invalid_rows);
432         else
433                 INFO_LOG("no invalid rows, no range violations, good\n");
434         return 1;
435 err:
436         return ret;
437 }
438
439 static int move_index_entry(struct osl_table *t, uint32_t dest, uint32_t src)
440 {
441         char *dest_ie, *src_ie;
442         int ret = get_row_index(t, dest, &dest_ie);
443
444         if (ret < 0)
445                 return ret;
446         ret = get_row_index(t, src, &src_ie);
447         if (ret < 0)
448                 return ret;
449         INFO_LOG("moving entry #%u to position %u\n", src, dest);
450         memcpy(dest_ie, src_ie, t->row_index_size);
451         return 1;
452 }
453
454 static int map_index(const struct osl_table_description *desc, struct osl_object *map)
455 {
456         char *filename = index_filename(desc);
457         int ret;
458
459         ret = mmap_full_file(filename, O_RDWR, &map->data, &map->size, NULL);
460         DEBUG_LOG("mapping index %s: ret: %d, size: %zu\n", filename, ret, map->size);
461         free(filename);
462         return ret;
463 }
464
465 static int prune_invalid_rows_from_index(struct osl_table *t)
466 {
467         uint32_t top = 0, bottom;
468         char *filename;
469         int ret;
470
471         if (!t->num_invalid_rows) {
472                 INFO_LOG("all rows are valid, good\n");
473                 return 1;
474         }
475         NOTICE_LOG("index contains %u invalid row(s) (%d bytes)\n",
476                 t->num_invalid_rows, t->row_index_size * t->num_invalid_rows);
477         if (conf.dry_run_given)
478                 return 0;
479         NOTICE_LOG("removing invalid rows from index\n");
480         bottom = t->num_rows - 1;
481         while (top < bottom) {
482                 if (!row_is_invalid(t, top)) {
483                         top++;
484                         continue;
485                 }
486                 while (bottom > top) {
487                         if (row_is_invalid(t, bottom)) {
488                                 bottom--;
489                                 continue;
490                         }
491                         /* move bottom index entry to top */
492                         move_index_entry(t, top, bottom);
493                         bottom--;
494                         top++;
495                         break;
496                 }
497         }
498         DEBUG_LOG("unmapping index\n");
499         osl_munmap(t->index_map.data, t->index_map.size);
500         filename = index_filename(t->desc);
501         ret = truncate_file(filename, t->row_index_size
502                 * t->num_invalid_rows);
503         free(filename);
504         if (ret < 0)
505                 return ret;
506         ret = map_index(t->desc, &t->index_map);
507         if (ret < 0)
508                 return ret;
509         t->num_rows = table_num_rows(t);
510         return 1;
511 }
512
513 static int check_for_invalid_objects(struct osl_table *t, uint32_t **lost_bytes)
514 {
515         int i, j, ret;
516         const struct osl_column_description *cd;
517         uint32_t *loss = fsck_malloc(sizeof(uint32_t) * t->desc->num_columns);
518
519         INFO_LOG("looking for mapped objects not contained in index\n");
520         /* first count used bytes */
521         FOR_EACH_MAPPED_COLUMN(i, t, cd) {
522                 loss[i] = t->columns[i].data_map.size;
523                 DEBUG_LOG("column %i data map: %zu bytes\n", i,
524                         t->columns[i].data_map.size);
525                 for (j = 0; j < t->num_rows; j++) {
526                         struct osl_object obj;
527                         ret = get_mapped_object(t, i, j, &obj);
528                         if (ret < 0)
529                                 goto err;
530                         loss[i] -= obj.size;
531                 }
532         }
533         ret = 0;
534         FOR_EACH_MAPPED_COLUMN(i, t, cd) {
535                 if (loss[i]) {
536                         NOTICE_LOG("column %u contains %u lost bytes\n",
537                                 i, loss[i]);
538                         ret = 1;
539                 }
540         }
541         if (!ret)
542                 INFO_LOG("all mapped objects are valid, good\n");
543         *lost_bytes = loss;
544         return ret;
545 err:
546         free(loss);
547         return ret;
548 }
549
550 /* prune_invalid_rows() must be run on the table before calling this */
551 static int prune_mapped_column(struct osl_table *t, uint32_t col_num, int fd)
552 {
553         int i, ret;
554         uint32_t written = 0;
555         struct osl_column *col = t->columns + col_num;
556
557         INFO_LOG("pruning col %u\n", col_num);
558         for (i = 0; i < t->num_rows; i++) {
559                 struct osl_object obj;
560                 char *index_entry;
561
562                 DEBUG_LOG("checking row %u/%u\n", i, t->num_rows);
563                 ret = get_mapped_object(t, col_num, i, &obj);
564                 if (ret < 0)
565                         return ret;
566                 ret = _write_all(fd, (char *)(obj.data), obj.size);
567                 if (ret < 0)
568                         return ret;
569                 written += obj.size;
570                 ret = get_row_index(t, i, &index_entry);
571                 if (ret < 0)
572                         return ret;
573                 update_cell_index(index_entry, col, written, obj.size);
574         }
575         return 1;
576 }
577
578 static int prune_objects(struct osl_table *t, uint32_t *lost_bytes)
579 {
580         int i, ret;
581         const struct osl_column_description *cd;
582         char **col_filenames = fsck_calloc(t->desc->num_columns * sizeof(char *));
583         char **new_col_filenames = fsck_calloc(t->desc->num_columns * sizeof(char *));
584         char *idx_filename = index_filename(t->desc);
585         char *old_idx_filename = make_message("%s.bak", idx_filename);
586         int fd;
587
588         NOTICE_LOG("removing unreferenced objects from data files\n");
589         /* first make a copy of the index */
590         DEBUG_LOG("opening %s\n", old_idx_filename);
591         ret = osl_open(old_idx_filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
592         if (ret < 0)
593                 goto out_free;
594         fd = ret;
595         ret = _write_all(fd, t->index_map.data, t->index_map.size);
596         close(fd);
597         if (ret < 0)
598                 goto out_free;
599         FOR_EACH_MAPPED_COLUMN(i, t, cd) {
600                 if (!lost_bytes[i])
601                         continue;
602                 col_filenames[i] = column_filename(t, i);
603                 new_col_filenames[i] = make_message("%s.fsck", col_filenames[i]);
604                 ret = osl_open(new_col_filenames[i], O_WRONLY | O_CREAT | O_EXCL, 0644);
605                 if (ret < 0)
606                         goto out_unlink_data;
607                 fd = ret;
608                 ret = prune_mapped_column(t, i, fd);
609                 close(fd);
610                 if (ret < 0)
611                         goto out_unlink_data;
612         }
613         ret = unmap_table(t, OSL_MARK_CLEAN);
614         if (ret < 0)
615                 goto out_unlink_data;
616         FOR_EACH_MAPPED_COLUMN(i, t, cd) {
617                 if (!lost_bytes[i])
618                         continue;
619                 ret = osl_rename(new_col_filenames[i], col_filenames[i]);
620                 if (ret < 0) { /* we're kinda screwed here */
621                         CRIT_LOG("rename of col %i failed: %s\n", i,
622                                 osl_strerror(errno));
623                         goto out_free;
624                 }
625         }
626         unlink(old_idx_filename);
627         ret = map_table(t, 0);
628         goto out_free;
629 out_unlink_data:
630         FOR_EACH_MAPPED_COLUMN(i, t, cd)
631                 unlink(new_col_filenames[i]);
632 out_free:
633         free(old_idx_filename);
634         free(idx_filename);
635         FOR_EACH_MAPPED_COLUMN(i, t, cd) {
636                 free(col_filenames[i]);
637                 free(new_col_filenames[i]);
638         }
639         free(col_filenames);
640         free(new_col_filenames);
641         return ret;
642 }
643
644 static struct osl_column_description hash_tree_table_cols[] = {
645         {
646                 .storage_type = OSL_NO_STORAGE,
647                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
648                 .name = "hash",
649                 .compare_function = uint32_compare,
650                 .data_size = HASH_SIZE
651         },
652 };
653
654 static const struct osl_table_description hash_tree_table_desc = {
655         .dir = "/", /* irrelevant */
656         .name = "hash_tree",
657         .num_columns = 1,
658         .flags = 0,
659         .column_descriptions = hash_tree_table_cols
660 };
661
662 /**
663  * The hash_tree table contains all hashes of the disk storage name column.
664  * of each row. It is used for checking if a disk storage file has a reference
665  * in the table.
666  */
667 static struct osl_table *hash_tree_table;
668 static HASH_TYPE *hashes;
669
670 static int check_disk_storage_column(struct osl_table *t, int row_num,
671                 int col_num, char *ds_name, unsigned *num_missing_objects)
672 {
673         int ret;
674         struct stat statbuf;
675         char *path = disk_storage_path(t, col_num, ds_name);
676         unsigned dsnc = t->disk_storage_name_column;
677         struct osl_object obj;
678
679         DEBUG_LOG("checking if %s is a regular file\n", path);
680         ret = stat(path, &statbuf);
681         if (ret < 0 && errno == ENOENT) {
682                 struct osl_row *row;
683                 (*num_missing_objects)++;
684                 ERROR_LOG("row %d: object %s is missing\n", row_num, path);
685                 ret = 0;
686                 if (conf.dry_run_given)
687                         goto out;
688                 NOTICE_LOG("trying to delete row %d\n", row_num);
689                 ret = osl_get_row(t, dsnc, &obj, &row);
690                 if (ret < 0) {
691                         CRIT_LOG("unable to get row %d\n", row_num);
692                         fsck_mark_row_invalid(t, row_num);
693                         CRIT_LOG("Please re-run fsck\n");
694                         goto out;
695                 }
696                 ret = osl_del_row(t, row);
697                 if (ret < 0)
698                         goto out;
699         }
700 out:
701         free(path);
702         if (ret < 0)
703                 return ret;
704         ret = -E_FSCK_NOT_A_REGULAR_FILE;
705         if (!(S_IFREG & statbuf.st_mode))
706                 return ret;
707         return 1;
708 }
709
710 static int check_disk_storage_presence(struct osl_table *t)
711 {
712         int ret, i, j;
713         struct osl_object obj, hash_obj = {.size = HASH_SIZE};
714         char *ds_name;
715         const struct osl_column_description *cd;
716         unsigned dsnc = t->disk_storage_name_column, missing_objects = 0;
717
718         if (!t->num_rows)
719                 return 1;
720         hashes = fsck_malloc(t->num_rows * HASH_SIZE);
721         INFO_LOG("looking for missing disk storage objects\n");
722         for (i = 0; i < t->num_rows; i++) {
723                 if (row_is_invalid(t, i))
724                         continue;
725                 ret = get_mapped_object(t, dsnc, i, &obj);
726                 if (ret < 0)
727                         return ret;
728                 hash_object(&obj, hashes + i * HASH_SIZE);
729                 hash_obj.data = hashes + i * HASH_SIZE;
730                 osl_add_row(hash_tree_table, &hash_obj);
731                 ds_name = disk_storage_name_of_hash(t, hashes + i * HASH_SIZE);
732                 FOR_EACH_DISK_STORAGE_COLUMN(j, t, cd) {
733                         ret = check_disk_storage_column(t, i, j, ds_name,
734                                 &missing_objects);
735                         if (ret < 0)
736                                 goto err;
737                 }
738                 free(ds_name);
739         }
740         if (!missing_objects)
741                 INFO_LOG("all referenced disk storage objects exist, good\n");
742         else
743                 NOTICE_LOG("%d missing object(s)\n", missing_objects);
744         return missing_objects;
745 err:
746         free(ds_name);
747         return ret;
748 }
749
750 static int dummy_compare(const struct osl_object *obj1, const struct osl_object *obj2)
751 {
752         if (obj1 < obj2)
753                 return -1;
754         if (obj1 > obj2)
755                 return 1;
756         return 0;
757 }
758
759 static unsigned files_pruned;
760
761 static int prune_disk_storage_file(const char *path, void *private_data)
762 {
763         HASH_TYPE hash[HASH_SIZE];
764         uint8_t flags = *(uint8_t *)private_data;
765         struct osl_object obj = {.data = hash, .size = HASH_SIZE};
766         struct osl_row *row;
767         int ret = -1;
768         size_t len = strlen(path);
769
770
771         DEBUG_LOG("path: %s\n", path);
772         if (flags & OSL_LARGE_TABLE) {
773                 if (len < HASH_SIZE * 2 + 2)
774                         goto invalid;
775 //              NOTICE_LOG("p: %s\n", path + len - 2 * HASH_SIZE - 1);
776                 ret = asc_to_hash(path + len - 2 * HASH_SIZE - 1, 1, hash);
777                 if (ret < 0)
778                         goto invalid;
779                 ret = asc_to_hash(path + len - 2 * HASH_SIZE + 2, HASH_SIZE - 1,
780                         hash + 1);
781                 if (ret < 0)
782                         goto invalid;
783 //              INFO_LOG("high: %x, low: %x, hash: %x\n", high, low, hash);
784         } else {
785                 if (len < 2 * HASH_SIZE + 1)
786                         goto invalid;
787                 ret = asc_to_hash(path + len - 2 * HASH_SIZE, 2 * HASH_SIZE, hash);
788                 if (ret < 0)
789                         goto invalid;
790 //              INFO_LOG("hash: %x\n", hash);
791         }
792 #if 0
793 {
794         char asc[2 * HASH_SIZE + 1];
795         hash_to_asc(hash, asc);
796         NOTICE_LOG("before: %s\nafter: %s\n", path, asc);
797 }
798 #endif
799         ret = osl_get_row(hash_tree_table, 0, &obj, &row);
800         if (ret >= 0)
801                 return 1;
802         NOTICE_LOG("unreferenced file in hash dir: %s\n", path);
803         goto remove;
804 invalid:
805         ERROR_LOG("could not read hash value of %s\n", path);
806 remove:
807         if (conf.dry_run_given)
808                 return 0;
809         NOTICE_LOG("removing %s\n", path);
810         unlink(path);
811         files_pruned++;
812         return 1;
813 }
814
815 static int prune_disk_storage_files(struct osl_table *t)
816 {
817         int i, ret = 1;
818         const struct osl_column_description *cd;
819
820         INFO_LOG("looking for unreferenced disk storage files\n");
821         FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
822                 char *dirname = column_filename(t, i);
823                 uint8_t flags = t->desc->flags;
824                 ret = for_each_file_in_dir(dirname, prune_disk_storage_file,
825                         &flags);
826                 free(dirname);
827         }
828         if (files_pruned)
829                 NOTICE_LOG("%u disk storage files deleted\n",
830                         files_pruned);
831         else
832                 INFO_LOG("all files are are referenced, good\n");
833         return ret;
834 }
835
836 static int check_disk_storage_columns(struct osl_table *t)
837 {
838         int ret, i;
839         const struct osl_column_description *cd;
840
841         if (!t->num_disk_storage_columns) {
842                 INFO_LOG("no disk storage columns in table '%s', "
843                         "skipping checks\n", t->desc->name);
844                 return 1;
845         }
846         FOR_EACH_COLUMN(i, t->desc, cd)
847                 t->desc->column_descriptions[i].compare_function = dummy_compare;
848         ret = init_rbtrees(t);
849         if (ret < 0)
850                 return ret;
851         INFO_LOG("creating rbtree for disk storage hash values\n");
852         ret = osl_open_table(&hash_tree_table_desc, &hash_tree_table);
853         if (ret < 0)
854                 goto out;
855         ret = check_disk_storage_presence(t);
856         if (ret < 0)
857                 goto out_close_hash_tree;
858         ret = prune_disk_storage_files(t);
859 out_close_hash_tree:
860         osl_close_table(hash_tree_table, 0);
861         free(hashes);
862         hashes = NULL;
863 out:
864         clear_rbtrees(t); /* TODO why are we doing that here? Seems odd */
865         return ret;
866 }
867
868 static void set_dummy_contents(struct osl_table_description *desc)
869 {
870         int i;
871         struct osl_column_description *cd;
872
873         for (i = 0; i < desc->num_columns; i++) {
874                 cd = get_column_description(desc, i);
875                 cd->compare_function = dummy_compare;
876         }
877 }
878
879 static int fsck_init(struct osl_table_description *desc, struct osl_table **t)
880 {
881         struct osl_object map;
882         int ret = map_index(desc, &map);
883
884         if (ret < 0)
885                 goto out;
886         ret = read_table_desc(&map, desc);
887         if (ret < 0) {
888                 osl_munmap(map.data, map.size);
889                 goto out;
890         }
891         set_dummy_contents(desc);
892         ret = init_table_structure(desc, t);
893         if (ret < 0) {
894                 osl_munmap(map.data, map.size);
895                 goto out;
896         }
897         DEBUG_LOG("unmapping index\n");
898         osl_munmap(map.data, map.size);
899         if (conf.force_given)
900                 ret = map_table(*t, (MAP_TBL_FL_IGNORE_DIRTY));
901         else
902                 ret = map_table(*t, 0);
903         if (ret >= 0) {
904                 (*t)->num_rows = table_num_rows(*t);
905                 DEBUG_LOG("index header size: %d\n", (*t)->index_header_size);
906                 DEBUG_LOG("row index size: %d\n", (*t)->row_index_size);
907         }
908 out:
909         return ret;
910 }
911
912 static void fsck_cleanup(struct osl_table *t)
913 {
914         int i;
915
916         if (!t)
917                 return;
918         if (t->desc->column_descriptions) {
919                 struct osl_column_description *cd;
920                 for (i = 0; i < t->desc->num_columns; i++) {
921                         cd = get_column_description(t->desc, i);
922                         free((char*)cd->name);
923                 }
924                 free(t->desc->column_descriptions);
925         }
926         free(t->columns);
927         free(t);
928
929 }
930
931 #define ST_CASE(st) case st: return #st
932
933 static const char *get_asc_storage_type(enum osl_storage_type st)
934 {
935         switch (st) {
936                 ST_CASE(OSL_MAPPED_STORAGE);
937                 ST_CASE(OSL_DISK_STORAGE);
938                 ST_CASE(OSL_NO_STORAGE);
939         }
940         return NULL;
941 }
942
943 #define APPEND_ASC_SF(sf, flag, str) do { if (sf & flag) { \
944         if (str) str = make_message("%s%s", str, " | " # flag); \
945         else str = fsck_strdup(#flag); }} while (0)
946
947
948 static char *get_asc_storage_flags(enum osl_storage_type sf)
949 {
950         char *asc_sf = NULL;
951
952         APPEND_ASC_SF(sf, OSL_RBTREE, asc_sf);
953         APPEND_ASC_SF(sf, OSL_FIXED_SIZE, asc_sf);
954         APPEND_ASC_SF(sf, OSL_UNIQUE, asc_sf);
955         return asc_sf;
956 }
957
958 static int dump_table_desc(struct osl_table *t, int fd)
959 {
960         const struct osl_table_description *desc = t->desc;
961         int ret, i;
962         struct osl_column_description *cd;
963         char *msg = make_message("static struct osl_column_description cols[] = {\n");
964         ret = _write_all(fd, msg, strlen(msg));
965         if (ret < 0)
966                 return ret;
967         free(msg);
968         FOR_EACH_COLUMN(i, desc, cd) {
969                 const char *asc_st;
970                 msg = make_message("\t[%d] = {\n", i);
971                 ret = _write_all(fd, msg, strlen(msg));
972                 if (ret < 0)
973                         return ret;
974                 free(msg);
975                 asc_st = get_asc_storage_type(cd->storage_type);
976                 msg = make_message("\t\t.storage_type = %s,\n", asc_st);
977                 ret = _write_all(fd, msg, strlen(msg));
978                 if (ret < 0)
979                         return ret;
980                 free(msg);
981                 if (cd->storage_flags) {
982                         char *asc_sf = get_asc_storage_flags(cd->storage_flags);
983                         msg = make_message("\t\t,storage_flags = %s,\n", asc_sf);
984                         free(asc_sf);
985                         ret = _write_all(fd, msg, strlen(msg));
986                         if (ret < 0)
987                                 return ret;
988                         free(msg);
989                 }
990                 if (cd->storage_flags & OSL_FIXED_SIZE) {
991                         msg = make_message("\t\t.data_size = %u,\n", cd->data_size);
992                         ret = _write_all(fd, msg, strlen(msg));
993                         if (ret < 0)
994                                 return ret;
995                         free(msg);
996                 }
997                 msg = make_message("\t\t.name = \"%s\",\n", cd->name);
998                 ret = _write_all(fd, msg, strlen(msg));
999                 if (ret < 0)
1000                         return ret;
1001                 free(msg);
1002                 if (cd->storage_flags & OSL_RBTREE) {
1003                         msg = make_message("\t\t.compare_function = compare_func,\n");
1004                         ret = _write_all(fd, msg, strlen(msg));
1005                         if (ret < 0)
1006                                 return ret;
1007                         free(msg);
1008                 }
1009                 msg = make_message("\t},\n");
1010                 ret = _write_all(fd, msg, strlen(msg));
1011                 if (ret < 0)
1012                         return ret;
1013                 free(msg);
1014         }
1015         msg = make_message("};\n");
1016         ret = _write_all(fd, msg, strlen(msg));
1017         if (ret < 0)
1018                 return ret;
1019         free(msg);
1020         return 1;
1021 }
1022
1023 static int dump_row(struct osl_table *t, unsigned row_num, const char *row_dir)
1024 {
1025         int ret, i;
1026         const struct osl_column_description *cd;
1027         unsigned dsnc;
1028         struct osl_object obj;
1029         char *ds_name;
1030         HASH_TYPE hash[HASH_SIZE];
1031         char *filename;
1032
1033         FOR_EACH_MAPPED_COLUMN(i, t, cd) {
1034                 ret = get_mapped_object(t, i, row_num, &obj);
1035                 if (ret < 0)
1036                         return ret;
1037                 filename = make_message("%s/col_%03u", row_dir, i);
1038                 ret = write_file(filename, obj.data, obj.size);
1039                 free(filename);
1040                 if (ret < 0)
1041                         return ret;
1042         }
1043         if (!t->num_disk_storage_columns)
1044                 return 1;
1045         dsnc = t->disk_storage_name_column;
1046         ret = get_mapped_object(t, dsnc, row_num, &obj);
1047         if (ret < 0)
1048                 return ret;
1049         hash_object(&obj, hash);
1050         ds_name = disk_storage_name_of_hash(t, hash);
1051         FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
1052                 filename = disk_storage_path(t, i, ds_name);
1053                 ret = mmap_full_file(filename, O_RDONLY, &obj.data, &obj.size, NULL);
1054                 free(filename);
1055                 if (ret < 0)
1056                         goto out;
1057                 filename = make_message("%s/col_%03u", row_dir, i);
1058                 ret = write_file(filename, obj.data, obj.size);
1059                 free(filename);
1060                 if (ret < 0)
1061                         goto out;
1062         }
1063         ret = 1;
1064 out:
1065         free(ds_name);
1066         return ret;
1067 }
1068
1069 static int dump_rows(char *dump_dir, struct osl_table *t)
1070 {
1071         unsigned i;
1072         char *current_dir = NULL;
1073         int ret = 0;
1074
1075         for (i = 0; i < t->num_rows; i++) {
1076                 char *row_dir;
1077                 if (row_is_invalid(t, i))
1078                         continue;
1079                 if (!(i % 1000)) {
1080                         free(current_dir);
1081                         current_dir = make_message("%s/rows_%u-%u", dump_dir, i, i + 999);
1082                         NOTICE_LOG("dumping rows %u - %u\n", i, i + 999);
1083                         ret = osl_mkdir(current_dir, 0777);
1084                         if (ret < 0 && ret != -E_OSL_DIR_EXISTS)
1085                                 goto out;
1086                 }
1087                 row_dir = make_message("%s/row_%03u", current_dir, i);
1088                 ret = osl_mkdir(row_dir, 0777);
1089                 if (ret < 0 && ret != -E_OSL_DIR_EXISTS) {
1090                         free(row_dir);
1091                         goto out;
1092                 }
1093                 ret = dump_row(t, i, row_dir);
1094                 free(row_dir);
1095                 if (ret < 0)
1096                         goto out;
1097         }
1098 out:
1099         free(current_dir);
1100         return ret;
1101 }
1102
1103 static int dump_table(char *dump_dir, struct osl_table_description *desc)
1104 {
1105         struct osl_table *t = NULL;
1106         int fd, ret = fsck_init(desc, &t);
1107         char *desc_file;
1108         char *table_dump_dir = NULL;
1109
1110         if (ret < 0)
1111                 goto out;
1112         ret = osl_mkdir(dump_dir, 0777);
1113         if (ret < 0 && ret != -E_OSL_DIR_EXISTS)
1114                 goto out;
1115         table_dump_dir = make_message("%s/%s", dump_dir, desc->name);
1116         ret = osl_mkdir(table_dump_dir, 0777);
1117         if (ret < 0 && ret != -E_OSL_DIR_EXISTS)
1118                 goto out;
1119         desc_file = make_message("%s/table_description.c", table_dump_dir);
1120         ret = osl_open(desc_file, O_WRONLY | O_CREAT | O_EXCL, 0644);
1121         free(desc_file);
1122         if (ret < 0)
1123                 goto out;
1124         fd = ret;
1125         ret = dump_table_desc(t, fd);
1126         close(fd);
1127         if (ret < 0)
1128                 goto out;
1129         ret = dump_rows(table_dump_dir, t);
1130 out:
1131         free(table_dump_dir);
1132         fsck_cleanup(t);
1133         return ret;
1134 }
1135
1136 static int fsck(struct osl_table_description *desc)
1137 {
1138         int ret;
1139         struct osl_table *t = NULL;
1140         uint32_t *lost_bytes = NULL;
1141
1142         ret = fsck_init(desc, &t);
1143         if (ret < 0)
1144                 goto out;
1145         ret = check_index_ranges(t);
1146         if (ret < 0)
1147                 goto out_unmap;
1148         ret = check_disk_storage_columns(t);
1149         if (ret < 0)
1150                 goto out_unmap;
1151         ret = prune_invalid_rows_from_index(t);
1152         if (ret < 0)
1153                 goto out_unmap;
1154         ret = check_for_invalid_objects(t, &lost_bytes);
1155         if (ret < 0)
1156                 goto out_unmap;
1157         if (ret > 0 && !conf.dry_run_given) { /* at least one mapped data file needs pruning */
1158                 ret = prune_objects(t, lost_bytes);
1159                 if (ret < 0)
1160                         goto out_unmap;
1161         }
1162         free(lost_bytes);
1163 out_unmap:
1164         unmap_table(t, OSL_MARK_CLEAN);
1165 out:
1166         fsck_cleanup(t);
1167         return ret;
1168 }
1169
1170 static int check_table(char *db_dir, char *table_name)
1171 {
1172         struct osl_table_description desc = {
1173                 .column_descriptions = NULL,
1174                 .dir = db_dir,
1175                 .name = table_name
1176         };
1177         int ret;
1178
1179         INFO_LOG("checking table %s\n", table_name);
1180         if (!conf.no_fsck_given) {
1181                 ret = fsck(&desc);
1182                 if (ret < 0)
1183                         goto out;
1184         }
1185         ret = 1;
1186         if (!conf.dump_dir_given || !*conf.dump_dir_arg)
1187                 goto out;
1188         ret = dump_table(conf.dump_dir_arg, &desc);
1189 out:
1190         if (ret < 0)
1191                 ERROR_LOG("failed to check table %s: %s\n", table_name,
1192                         fsck_strerror(-ret));
1193         else
1194                 NOTICE_LOG("successfully checked table %s\n", table_name);
1195         return ret;
1196 }
1197
1198 static int check_all_tables(char *db_dir)
1199 {
1200         DIR *dir;
1201         struct dirent *entry;
1202         int cwd_fd, ret2, ret = fsck_opendir(db_dir, &dir, &cwd_fd);
1203
1204         if (ret < 0)
1205                 return ret;
1206         while ((entry = readdir(dir))) {
1207                 mode_t m;
1208                 struct stat s;
1209                 if (!strcmp(entry->d_name, "."))
1210                         continue;
1211                 if (!strcmp(entry->d_name, ".."))
1212                         continue;
1213                 if (lstat(entry->d_name, &s) == -1)
1214                         continue;
1215                 m = s.st_mode;
1216                 if (!S_ISDIR(m))
1217                         continue;
1218                 ret = check_table(db_dir, entry->d_name);
1219                 if (ret < 0)
1220                         break;
1221         }
1222         closedir(dir);
1223         ret2 = __fchdir(cwd_fd);
1224         if (ret2 < 0 && ret >= 0)
1225                 ret = ret2;
1226         close(cwd_fd);
1227         return ret;
1228 }
1229
1230 int main(int argc, char **argv)
1231 {
1232         int i, ret;
1233         struct fsck_cmdline_parser_params params = {
1234                 .override = 0,
1235                 .initialize = 1,
1236                 .check_required = 1,
1237                 .check_ambiguity = 1,
1238                 .print_errors = 1
1239         };
1240         ret = fsck_cmdline_parser_ext(argc, argv, &conf, &params);
1241         if (ret < 0) {
1242                 loglevel = EMERG;
1243                 ret = -E_FSCK_SYNTAX;
1244                 goto out;
1245         }
1246         loglevel = conf.loglevel_arg;
1247         HANDLE_VERSION_FLAG("fsck", conf);
1248         INFO_LOG("database dir: %s\n", conf.database_dir_arg);
1249         if (!conf.inputs_num) {
1250                 ret = check_all_tables(conf.database_dir_arg);
1251                 goto out;
1252         }
1253         for (i = 0; i < conf.inputs_num; i++) {
1254                 ret = check_table(conf.database_dir_arg, conf.inputs[i]);
1255                 if (ret < 0)
1256                         break;
1257         }
1258 out:
1259         if (ret < 0) {
1260                 ERROR_LOG("%s\n", fsck_strerror(-ret));
1261                 if (conf.loglevel_arg > 1)
1262                         EMERG_LOG("re-run with \"--loglevel %d\" to increase verbosity\n",
1263                                 conf.loglevel_arg - 1);
1264         } else
1265                 NOTICE_LOG("success\n");
1266         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
1267 }