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