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