]> git.tuebingen.mpg.de Git - paraslash.git/blob - fsck.c
7bca01637beddba149f7476bd994f1e2940618a0
[paraslash.git] / fsck.c
1 #include "para.h"
2 #include "error.h"
3 #include "osl_core.h"
4 #include "fsck.cmdline.h"
5
6 static struct fsck_args_info conf;
7
8 INIT_FSCK_ERRLISTS;
9 INIT_STDERR_LOGGING(conf.loglevel_arg);
10
11 /* taken from git */
12 signed char hexval_table[256] = {
13          -1, -1, -1, -1, -1, -1, -1, -1,                /* 00-07 */
14          -1, -1, -1, -1, -1, -1, -1, -1,                /* 08-0f */
15          -1, -1, -1, -1, -1, -1, -1, -1,                /* 10-17 */
16          -1, -1, -1, -1, -1, -1, -1, -1,                /* 18-1f */
17          -1, -1, -1, -1, -1, -1, -1, -1,                /* 20-27 */
18          -1, -1, -1, -1, -1, -1, -1, -1,                /* 28-2f */
19           0,  1,  2,  3,  4,  5,  6,  7,                /* 30-37 */
20           8,  9, -1, -1, -1, -1, -1, -1,                /* 38-3f */
21          -1, 10, 11, 12, 13, 14, 15, -1,                /* 40-47 */
22          -1, -1, -1, -1, -1, -1, -1, -1,                /* 48-4f */
23          -1, -1, -1, -1, -1, -1, -1, -1,                /* 50-57 */
24          -1, -1, -1, -1, -1, -1, -1, -1,                /* 58-5f */
25          -1, 10, 11, 12, 13, 14, 15, -1,                /* 60-67 */
26          -1, -1, -1, -1, -1, -1, -1, -1,                /* 68-67 */
27          -1, -1, -1, -1, -1, -1, -1, -1,                /* 70-77 */
28          -1, -1, -1, -1, -1, -1, -1, -1,                /* 78-7f */
29          -1, -1, -1, -1, -1, -1, -1, -1,                /* 80-87 */
30          -1, -1, -1, -1, -1, -1, -1, -1,                /* 88-8f */
31          -1, -1, -1, -1, -1, -1, -1, -1,                /* 90-97 */
32          -1, -1, -1, -1, -1, -1, -1, -1,                /* 98-9f */
33          -1, -1, -1, -1, -1, -1, -1, -1,                /* a0-a7 */
34          -1, -1, -1, -1, -1, -1, -1, -1,                /* a8-af */
35          -1, -1, -1, -1, -1, -1, -1, -1,                /* b0-b7 */
36          -1, -1, -1, -1, -1, -1, -1, -1,                /* b8-bf */
37          -1, -1, -1, -1, -1, -1, -1, -1,                /* c0-c7 */
38          -1, -1, -1, -1, -1, -1, -1, -1,                /* c8-cf */
39          -1, -1, -1, -1, -1, -1, -1, -1,                /* d0-d7 */
40          -1, -1, -1, -1, -1, -1, -1, -1,                /* d8-df */
41          -1, -1, -1, -1, -1, -1, -1, -1,                /* e0-e7 */
42          -1, -1, -1, -1, -1, -1, -1, -1,                /* e8-ef */
43          -1, -1, -1, -1, -1, -1, -1, -1,                /* f0-f7 */
44          -1, -1, -1, -1, -1, -1, -1, -1,                /* f8-ff */
45 };
46
47 int asc_to_hash(const char *asc_hash, int len, HASH_TYPE *hash)
48 {
49         int i = 0;
50         const unsigned char *asc = (const unsigned char *) asc_hash;
51
52         while (*asc && i++ < len) {
53                 unsigned int val = (hexval_table[asc[0]] << 4) | hexval_table[asc[1]];
54                 if (val & ~0xff)
55                         return -1;
56                 *hash++ = val;
57                 asc += 2;
58
59         }
60         return 1;
61 }
62
63 /*
64  * check for object boundary violations
65  *
66  * test whether the range pointed to by the index entry for a given cell is
67  * contained in mapped data file. This should always be the case. Otherwise
68  * we are in real trouble.
69  */
70 static int check_range(struct osl_table *t, uint32_t row_num, uint32_t col_num)
71 {
72         char *index_entry;
73         struct osl_object obj;
74         struct osl_column *col;
75         int ret;
76         char *map_start, *obj_start;
77
78         ret = get_cell_index(t, row_num, col_num, &index_entry);
79         if (ret < 0)
80                 return ret;
81         ret = get_mapped_object(t, col_num, row_num, &obj);
82         if (ret < 0)
83                 return ret;
84         col = t->columns + col_num;
85         obj_start = obj.data;
86         map_start = col->data_map.data;
87 //      PARA_INFO_LOG("obj: %p..%p\n", obj_start, obj_start + obj.size);
88 //      PARA_INFO_LOG("map: %p..%p\n", map_start, map_start + col->data_map.size);
89         if (obj_start < map_start || obj_start + obj.size > map_start + col->data_map.size) {
90                 PARA_CRIT_LOG("range violation in row %u, col %u\n", row_num,
91                         col_num);
92                 return -E_RANGE_VIOLATION;
93         }
94         PARA_DEBUG_LOG("col %u: ok\n", col_num);
95         return 1;
96 }
97
98 /*
99  * check all cells of the given table for boundary violations
100  */
101 static int check_index_ranges(struct osl_table *t)
102 {
103         int i, j, ret;
104
105         PARA_NOTICE_LOG("checking for range violations in index\n");
106         //PARA_DEBUG_LOG("%d rows. %d columns\n", t->num_rows, t->desc->num_columns);
107         t->num_invalid_rows = 0;
108         for (i = 0; i < t->num_rows; i++) {
109                 if (row_is_invalid(t, i)) {
110                         t->num_invalid_rows++;
111                         continue;
112                 }
113                 for (j = 0; j < t->desc->num_columns; j++) { /* FXIME */
114                         const struct osl_column_description *cd =
115                                 get_column_description(t->desc, j);
116                         if (cd->storage_type != OSL_MAPPED_STORAGE)
117                                 continue;
118                         ret = check_range(t, i, j);
119                         if (ret < 0) {
120                                 if (ret != -E_INVALID_OBJECT &&
121                                                 ret != -E_RANGE_VIOLATION)
122                                         goto err;
123                                 if (ret == -E_INVALID_OBJECT) {
124                                         PARA_CRIT_LOG("row %d, col %d maps to an "
125                                                 "invalid object\n", i, j);
126                                 }
127                                 ret = mark_row_invalid(t, i);
128                                 if (ret < 0)
129                                         goto err;
130                                 t->num_invalid_rows++;
131                                 break;
132                         }
133                 }
134
135         }
136         if (t->num_invalid_rows)
137                 PARA_NOTICE_LOG("ranges OK. %d invalid row(s) detected\n",
138                         t->num_invalid_rows);
139         else
140                 PARA_INFO_LOG("no invalid rows, no range violations, good\n");
141         return 1;
142 err:
143         return ret;
144 }
145
146 static int move_index_entry(struct osl_table *t, uint32_t dest, uint32_t src)
147 {
148         char *dest_ie, *src_ie;
149         int ret = get_row_index(t, dest, &dest_ie);
150
151         if (ret < 0)
152                 return ret;
153         ret = get_row_index(t, src, &src_ie);
154         if (ret < 0)
155                 return ret;
156         PARA_INFO_LOG("moving entry #%u to position %u\n", src, dest);
157         memcpy(dest_ie, src_ie, t->row_index_size);
158         return 1;
159 }
160
161 static int map_index(const struct osl_table_description *desc, struct osl_object *map)
162 {
163         char *filename = index_filename(desc);
164         int ret;
165
166         ret = mmap_full_file(filename, O_RDWR, map);
167         PARA_INFO_LOG("mapping index %s: ret: %d, size: %zu\n", filename, ret, map->size);
168         free(filename);
169         return ret;
170 }
171
172 static int prune_invalid_rows_from_index(struct osl_table *t)
173 {
174         uint32_t top = 0, bottom;
175         char *filename;
176         int ret;
177
178         if (!t->num_invalid_rows) {
179                 PARA_INFO_LOG("all rows are valid, good\n");
180                 return 1;
181         }
182         PARA_NOTICE_LOG("deleting %u invalid row(s) (%d bytes) from index\n",
183                 t->num_invalid_rows, t->row_index_size * t->num_invalid_rows);
184         bottom = t->num_rows - 1;
185         while (top < bottom) {
186                 if (!row_is_invalid(t, top)) {
187                         top++;
188                         continue;
189                 }
190                 while (bottom > top) {
191                         if (row_is_invalid(t, bottom)) {
192                                 bottom--;
193                                 continue;
194                         }
195                         /* move bottom index entry to top */
196                         move_index_entry(t, top, bottom);
197                         bottom--;
198                         top++;
199                         break;
200                 }
201         }
202         PARA_INFO_LOG("unmapping index\n");
203         para_munmap(t->index_map.data, t->index_map.size);
204         filename = index_filename(t->desc);
205         ret = para_truncate(filename, t->row_index_size
206                 * t->num_invalid_rows);
207         free(filename);
208         if (ret < 0)
209                 return ret;
210         ret = map_index(t->desc, &t->index_map);
211         if (ret < 0)
212                 return ret;
213         t->num_rows = table_num_rows(t);
214         return 1;
215 }
216
217 static int check_for_invalid_objects(struct osl_table *t, uint32_t **lost_bytes)
218 {
219         int i, j, ret;
220         const struct osl_column_description *cd;
221         uint32_t *loss = para_malloc(sizeof(uint32_t) * t->desc->num_columns);
222
223         PARA_NOTICE_LOG("looking for mapped objects not contained in index\n");
224         /* first count used bytes */
225         FOR_EACH_MAPPED_COLUMN(i, t, cd) {
226                 loss[i] = t->columns[i].data_map.size;
227                 for (j = 0; j < t->num_rows; j++) {
228                         struct osl_object obj;
229                         ret = get_mapped_object(t, i, j, &obj);
230                         if (ret >= 0) {
231                                 loss[i] -= obj.size + 1; /* add one for header byte */
232                                 continue;
233                         }
234                         if (ret != -E_INVALID_OBJECT)
235                                 goto err;
236                         PARA_CRIT_LOG("row %d, col %d points to an invalid "
237                                 "mapped object, bad\n", j, i);
238                 }
239         }
240         ret = 0;
241         FOR_EACH_MAPPED_COLUMN(i, t, cd) {
242                 if (loss[i]) {
243                         PARA_NOTICE_LOG("column %u contains %u lost bytes\n",
244                                 i, loss[i]);
245                         ret = 1;
246                 }
247         }
248         if (!ret)
249                 PARA_INFO_LOG("all mapped objects are valid, good\n");
250         *lost_bytes = loss;
251         return ret;
252 err:
253         free(loss);
254         return ret;
255 }
256
257 /* prune_invalid_rows() must be run on the table before calling this */
258 static int prune_mapped_column(struct osl_table *t, uint32_t col_num, int fd)
259 {
260         int i, ret;
261         uint32_t written = 0;
262         struct osl_column *col = t->columns + col_num;
263
264         PARA_INFO_LOG("pruning col %u\n", col_num);
265         for (i = 0; i < t->num_rows; i++) {
266                 struct osl_object obj;
267                 char *index_entry;
268
269                 PARA_DEBUG_LOG("checking row %u/%u\n", i, t->num_rows);
270                 ret = get_mapped_object(t, col_num, i, &obj);
271                 if (ret < 0)
272                         return ret;
273                 ret = para_write_all(fd, (char *)(obj.data) - 1, obj.size + 1);
274                 if (ret < 0)
275                         return ret;
276                 written += obj.size + 1;
277                 ret = get_row_index(t, i, &index_entry);
278                 if (ret < 0)
279                         return ret;
280                 update_cell_index(index_entry, col, written, obj.size);
281         }
282         return 1;
283 }
284
285 static int prune_objects(struct osl_table *t, uint32_t *lost_bytes)
286 {
287         int i, ret;
288         const struct osl_column_description *cd;
289         char **col_filenames = para_calloc(t->desc->num_columns * sizeof(char *));
290         char **new_col_filenames = para_calloc(t->desc->num_columns * sizeof(char *));
291         char *idx_filename = index_filename(t->desc);
292         char *old_idx_filename = make_message("%s.bak", idx_filename);
293         int fd;
294
295         PARA_NOTICE_LOG("removing unreferenced objects from data files\n");
296         /* first make a copy of the index */
297         ret = para_open(old_idx_filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
298         if (ret < 0)
299                 goto out_free;
300         fd = ret;
301         ret = para_write_all(fd, t->index_map.data, t->index_map.size);
302         close(fd);
303         if (ret < 0)
304                 goto out_free;
305         FOR_EACH_MAPPED_COLUMN(i, t, cd) {
306                 if (!lost_bytes[i])
307                         continue;
308                 col_filenames[i] = column_filename(t, i);
309                 new_col_filenames[i] = make_message("%s.fsck", col_filenames[i]);
310                 ret = para_open(new_col_filenames[i], O_WRONLY | O_CREAT | O_EXCL, 0644);
311                 if (ret < 0)
312                         goto out_unlink_data;
313                 fd = ret;
314                 ret = prune_mapped_column(t, i, fd);
315                 close(fd);
316                 if (ret < 0)
317                         goto out_unlink_data;
318         }
319         ret = unmap_table(t, OSL_MARK_CLEAN);
320         if (ret < 0)
321                 goto out_unlink_data;
322         FOR_EACH_MAPPED_COLUMN(i, t, cd) {
323                 if (!lost_bytes[i])
324                         continue;
325                 ret = para_rename(new_col_filenames[i], col_filenames[i]);
326                 if (ret < 0) { /* we're kinda screwed here */
327                         PARA_CRIT_LOG("rename of col %i failed: %s\n", i,
328                                 strerror(errno));
329                         goto out_free;
330                 }
331         }
332         unlink(old_idx_filename);
333         ret = map_table(t, 0);
334         goto out_free;
335 out_unlink_data:
336         FOR_EACH_MAPPED_COLUMN(i, t, cd)
337                 unlink(new_col_filenames[i]);
338 out_free:
339         free(old_idx_filename);
340         free(idx_filename);
341         FOR_EACH_MAPPED_COLUMN(i, t, cd) {
342                 free(col_filenames[i]);
343                 free(new_col_filenames[i]);
344         }
345         free(col_filenames);
346         free(new_col_filenames);
347         return ret;
348 }
349
350 static struct osl_column_description hash_tree_table_cols[] = {
351         {
352                 .storage_type = OSL_NO_STORAGE,
353                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
354                 .name = "hash",
355                 .compare_function = uint32_compare,
356                 .data_size = HASH_SIZE
357         },
358 };
359
360 static const struct osl_table_description hash_tree_table_desc = {
361         .dir = "/", /* irrelevant */
362         .name = "hash_tree",
363         .num_columns = 1,
364         .flags = 0,
365         .column_descriptions = hash_tree_table_cols
366 };
367
368 /**
369  * The hash_tree table contains all hashes of the disk storage name column.
370  * of each row. It is used for checking if a disk storage file has a reference
371  * in the table.
372  */
373 static struct osl_table *hash_tree_table;
374 static HASH_TYPE *hashes;
375
376 static int check_disk_storage_column(struct osl_table *t, int row_num,
377                 int col_num, char *ds_name, unsigned *num_missing_objects)
378 {
379         int ret;
380         struct stat statbuf;
381         char *path = disk_storage_path(t, col_num, ds_name);
382         unsigned dsnc = t->disk_storage_name_column;
383         struct osl_object obj;
384
385         PARA_DEBUG_LOG("checking if %s is a regular file\n", path);
386         ret = stat(path, &statbuf);
387         if (ret < 0 && errno == ENOENT) {
388                 struct osl_row *row;
389                 (*num_missing_objects)++;
390                 PARA_ERROR_LOG("row %d: object %s is missing\n", row_num, path);
391                 PARA_NOTICE_LOG("trying to delete row %d\n", row_num);
392                 ret = osl_get_row(t, dsnc, &obj, &row);
393                 if (ret < 0) {
394                         PARA_CRIT_LOG("unable to get row %d\n", row_num);
395                         mark_row_invalid(t, row_num);
396                         PARA_CRIT_LOG("Please re-run fsck\n");
397                         goto out;
398                 }
399                 ret = osl_del_row(t, row);
400                 if (ret < 0)
401                         goto out;
402         }
403 out:
404         free(path);
405         if (ret < 0)
406                 return ret;
407         ret = -E_NOT_A_REGULAR_FILE;
408         if (!(S_IFREG & statbuf.st_mode))
409                 return ret;
410         return 1;
411 }
412
413 static int check_disk_storage_presence(struct osl_table *t)
414 {
415         int ret, i, j;
416         struct osl_object obj, hash_obj = {.size = HASH_SIZE};
417         char *ds_name;
418         const struct osl_column_description *cd;
419         unsigned dsnc = t->disk_storage_name_column, missing_objects = 0;
420
421         if (!t->num_rows)
422                 return 1;
423         hashes = para_malloc(t->num_rows * HASH_SIZE);
424         PARA_NOTICE_LOG("looking for missing disk storage objects\n");
425         for (i = 0; i < t->num_rows; i++) {
426                 if (row_is_invalid(t, i))
427                         continue;
428                 ret = get_mapped_object(t, dsnc, i, &obj);
429                 if (ret < 0)
430                         return ret;
431                 hash_object(&obj, hashes + i * HASH_SIZE);
432                 hash_obj.data = hashes + i * HASH_SIZE;
433                 osl_add_row(hash_tree_table, &hash_obj);
434                 ds_name = disk_storage_name_of_hash(t, hashes + i * HASH_SIZE);
435                 FOR_EACH_DISK_STORAGE_COLUMN(j, t, cd) {
436                         ret = check_disk_storage_column(t, i, j, ds_name,
437                                 &missing_objects);
438                         if (ret < 0)
439                                 goto err;
440                 }
441                 free(ds_name);
442         }
443         if (!missing_objects)
444                 PARA_INFO_LOG("all referenced disk storage objects exist, good\n");
445         else
446                 PARA_NOTICE_LOG("%d missing object(s)\n", missing_objects);
447         return missing_objects;
448 err:
449         free(ds_name);
450         return ret;
451 }
452
453 static int dummy_compare(const struct osl_object *obj1, const struct osl_object *obj2)
454 {
455         if (obj1 < obj2)
456                 return -1;
457         if (obj1 > obj2)
458                 return 1;
459         return 0;
460 }
461
462 static unsigned files_pruned;
463
464 int prune_disk_storage_file(const char *path, const void *private_data)
465 {
466         HASH_TYPE hash[HASH_SIZE];
467         unsigned flags = *(unsigned *)private_data;
468         struct osl_object obj = {.data = hash, .size = HASH_SIZE};
469         struct osl_row *row;
470         int ret = -1;
471         size_t len = strlen(path);
472
473
474         PARA_DEBUG_LOG("path: %s\n", path);
475         if (flags & OSL_LARGE_TABLE) {
476                 if (len < HASH_SIZE * 2 + 2)
477                         goto invalid;
478 //              PARA_NOTICE_LOG("p: %s\n", path + len - 2 * HASH_SIZE - 1);
479                 ret = asc_to_hash(path + len - 2 * HASH_SIZE - 1, 1, hash);
480                 if (ret < 0)
481                         goto invalid;
482                 ret = asc_to_hash(path + len - 2 * HASH_SIZE + 2, HASH_SIZE - 1,
483                         hash + 1);
484                 if (ret < 0)
485                         goto invalid;
486 //              PARA_INFO_LOG("high: %x, low: %x, hash: %x\n", high, low, hash);
487         } else {
488                 if (len < 2 * HASH_SIZE + 1)
489                         goto invalid;
490                 ret = asc_to_hash(path + len - 2 * HASH_SIZE, 2 * HASH_SIZE, hash);
491                 if (ret < 0)
492                         goto invalid;
493 //              PARA_INFO_LOG("hash: %x\n", hash);
494         }
495 #if 0
496 {
497         char asc[2 * HASH_SIZE + 1];
498         hash_to_asc(hash, asc);
499         PARA_NOTICE_LOG("before: %s\nafter: %s\n", path, asc);
500 }
501 #endif
502         ret = osl_get_row(hash_tree_table, 0, &obj, &row);
503         if (ret >= 0)
504                 return 1;
505         PARA_NOTICE_LOG("unreferenced file in hash dir: %s\n", path);
506         goto remove;
507 invalid:
508         PARA_ERROR_LOG("could not read hash value of %s\n", path);
509 remove:
510         PARA_NOTICE_LOG("removing %s\n", path);
511         unlink(path);
512         files_pruned++;
513         return 1;
514 }
515
516 static int prune_disk_storage_files(struct osl_table *t)
517 {
518         int i, ret = 1;
519         const struct osl_column_description *cd;
520
521         PARA_NOTICE_LOG("looking for unreferenced disk storage files\n");
522         FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
523                 char *dirname = column_filename(t, i);
524                 ret = for_each_file_in_dir(dirname, prune_disk_storage_file, &t->desc->flags);
525                 free(dirname);
526         }
527         if (files_pruned)
528                 PARA_NOTICE_LOG("%u disk storage files deleted\n",
529                         files_pruned);
530         else
531                 PARA_INFO_LOG("all files are are referenced, good\n");
532         return ret;
533 }
534
535 static int check_disk_storage_columns(struct osl_table *t)
536 {
537         int ret, i;
538         const struct osl_column_description *cd;
539
540         if (!t->num_disk_storage_columns) {
541                 PARA_NOTICE_LOG("no disk storage columns in table '%s', "
542                         "skipping checks\n", t->desc->name);
543                 return 1;
544         }
545         FOR_EACH_COLUMN(i, t->desc, cd)
546                 t->desc->column_descriptions[i].compare_function = dummy_compare;
547         ret = init_rbtrees(t);
548         if (ret < 0)
549                 return ret;
550         PARA_NOTICE_LOG("creating rbtree for disk storage hash values\n");
551         ret = osl_open_table(&hash_tree_table_desc, &hash_tree_table);
552         if (ret < 0)
553                 goto out;
554         ret = check_disk_storage_presence(t);
555         if (ret < 0)
556                 goto out_close_hash_tree;
557         ret = prune_disk_storage_files(t);
558 out_close_hash_tree:
559         osl_close_table(hash_tree_table, 0);
560         free(hashes);
561 out:
562         clear_rbtrees(t); /* TODO why are we doing that here? Seems odd */
563         return ret;
564 }
565
566 static void set_dummy_contents(struct osl_table_description *desc)
567 {
568         int i;
569         struct osl_column_description *cd;
570
571         for (i = 0; i < desc->num_columns; i++) {
572                 cd = get_column_description(desc, i);
573                 cd->compare_function = dummy_compare;
574         }
575 }
576
577 static int fsck_init(struct osl_table_description *desc, struct osl_table **t)
578 {
579         struct osl_object map;
580         int ret = map_index(desc, &map);
581
582         if (ret < 0)
583                 goto out;
584         ret = read_table_desc(&map, desc);
585         if (ret < 0) {
586                 para_munmap(map.data, map.size);
587                 goto out;
588         }
589         set_dummy_contents(desc);
590         ret = init_table_structure(desc, t);
591         if (ret < 0) {
592                 para_munmap(map.data, map.size);
593                 goto out;
594         }
595         PARA_INFO_LOG("unmapping index\n");
596         para_munmap(map.data, map.size);
597         if (conf.force_given)
598                 ret = map_table(*t, (MAP_TBL_FL_IGNORE_DIRTY));
599         else
600                 ret = map_table(*t, 0);
601         if (ret >= 0)
602                 (*t)->num_rows = table_num_rows(*t);
603 out:
604         return ret;
605 }
606
607 static void fsck_cleanup(struct osl_table *t)
608 {
609         int i;
610         if (t->desc->column_descriptions) {
611                 struct osl_column_description *cd;
612                 for (i = 0; i < t->desc->num_columns; i++) {
613                         cd = get_column_description(t->desc, i);
614                         free((char*)cd->name);
615                 }
616                 free(t->desc->column_descriptions);
617         }
618         if (t) {
619                 free(t->columns);
620                 free(t);
621         }
622
623 }
624
625 #define ST_CASE(st) case st: return #st
626
627 const char *get_asc_storage_type(enum osl_storage_type st)
628 {
629         switch (st) {
630                 ST_CASE(OSL_MAPPED_STORAGE);
631                 ST_CASE(OSL_DISK_STORAGE);
632                 ST_CASE(OSL_NO_STORAGE);
633         }
634         return NULL;
635 }
636
637 #define APPEND_ASC_SF(sf, flag, str) do { if (sf & flag) { \
638         if (str) str = para_strcat(str, " | " # flag); \
639         else str = para_strdup(#flag); }} while (0)
640
641
642 char *get_asc_storage_flags(enum osl_storage_type sf)
643 {
644         char *asc_sf = NULL;
645
646         APPEND_ASC_SF(sf, OSL_RBTREE, asc_sf);
647         APPEND_ASC_SF(sf, OSL_FIXED_SIZE, asc_sf);
648         APPEND_ASC_SF(sf, OSL_UNIQUE, asc_sf);
649         return asc_sf;
650 }
651
652 static int dump_table_desc(struct osl_table *t, int fd)
653 {
654         const struct osl_table_description *desc = t->desc;
655         int ret, i;
656         struct osl_column_description *cd;
657         char *msg = make_message("static struct osl_column_description cols[] = {\n");
658         ret = para_write_all(fd, msg, strlen(msg));
659         if (ret < 0)
660                 return ret;
661         free(msg);
662         FOR_EACH_COLUMN(i, desc, cd) {
663                 const char *asc_st;
664                 msg = make_message("\t[%d] = {\n", i);
665                 ret = para_write_all(fd, msg, strlen(msg));
666                 if (ret < 0)
667                         return ret;
668                 free(msg);
669                 asc_st = get_asc_storage_type(cd->storage_type);
670                 msg = make_message("\t\t.storage_type = %s,\n", asc_st);
671                 ret = para_write_all(fd, msg, strlen(msg));
672                 if (ret < 0)
673                         return ret;
674                 free(msg);
675                 if (cd->storage_flags) {
676                         char *asc_sf = get_asc_storage_flags(cd->storage_flags);
677                         msg = make_message("\t\t,storage_flags = %s,\n", asc_sf);
678                         free(asc_sf);
679                         ret = para_write_all(fd, msg, strlen(msg));
680                         if (ret < 0)
681                                 return ret;
682                         free(msg);
683                 }
684                 if (cd->storage_flags & OSL_FIXED_SIZE) {
685                         msg = make_message("\t\t.data_size = %u,\n", cd->data_size);
686                         ret = para_write_all(fd, msg, strlen(msg));
687                         if (ret < 0)
688                                 return ret;
689                         free(msg);
690                 }
691                 msg = make_message("\t\t.name = \"%s\",\n", cd->name);
692                 ret = para_write_all(fd, msg, strlen(msg));
693                 if (ret < 0)
694                         return ret;
695                 free(msg);
696                 if (cd->storage_flags & OSL_RBTREE) {
697                         msg = make_message("\t\t.compare_function = compare_func,\n");
698                         ret = para_write_all(fd, msg, strlen(msg));
699                         if (ret < 0)
700                                 return ret;
701                         free(msg);
702                 }
703                 msg = make_message("\t},\n");
704                 ret = para_write_all(fd, msg, strlen(msg));
705                 if (ret < 0)
706                         return ret;
707                 free(msg);
708         }
709         msg = make_message("};\n");
710         ret = para_write_all(fd, msg, strlen(msg));
711         if (ret < 0)
712                 return ret;
713         free(msg);
714         return 1;
715 }
716
717 static int dump_row(struct osl_table *t, unsigned row_num, const char *row_dir)
718 {
719         int ret, i;
720         const struct osl_column_description *cd;
721         unsigned dsnc;
722         struct osl_object obj;
723         char *ds_name;
724         HASH_TYPE hash[HASH_SIZE];
725         char *filename;
726
727         FOR_EACH_MAPPED_COLUMN(i, t, cd) {
728                 ret = get_mapped_object(t, i, row_num, &obj);
729                 if (ret < 0)
730                         return ret;
731                 filename = make_message("%s/col_%03u", row_dir, i);
732                 ret = para_write_file(filename, obj.data, obj.size);
733                 free(filename);
734                 if (ret < 0)
735                         return ret;
736         }
737         if (!t->num_disk_storage_columns)
738                 return 1;
739         dsnc = t->disk_storage_name_column;
740         ret = get_mapped_object(t, dsnc, i, &obj);
741         if (ret < 0)
742                 return ret;
743         hash_object(&obj, hash);
744         ds_name = disk_storage_name_of_hash(t, hash);
745         FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
746                 filename = disk_storage_path(t, i, ds_name);
747                 ret = mmap_full_file(filename, O_RDONLY, &obj);
748                 free(filename);
749                 if (ret < 0)
750                         goto out;
751                 filename = make_message("%s/col_%03u", row_dir, i);
752                 ret = para_write_file(filename, obj.data, obj.size);
753                 free(filename);
754                 if (ret < 0)
755                         goto out;
756         }
757         ret = 1;
758 out:
759         free(ds_name);
760         return ret;
761 }
762
763 static int dump_rows(char *dump_dir, struct osl_table *t)
764 {
765         unsigned i;
766         char *current_dir = NULL;
767         int ret = 0;
768
769         for (i = 0; i < t->num_rows; i++) {
770                 char *row_dir;
771                 if (row_is_invalid(t, i))
772                         continue;
773                 if (!(i % 1000)) {
774                         free(current_dir);
775                         current_dir = make_message("%s/rows_%u-%u", dump_dir, i, i + 999);
776                         PARA_NOTICE_LOG("dumping rows %u - %u\n", i, i + 999);
777                         ret = para_mkdir(current_dir, 0777);
778                         if (ret < 0)
779                                 goto out;
780                 }
781                 row_dir = make_message("%s/row_%03u", current_dir, i);
782                 ret = para_mkdir(row_dir, 0777);
783                 if (ret < 0) {
784                         free(row_dir);
785                         goto out;
786                 }
787                 ret = dump_row(t, i, row_dir);
788                 free(row_dir);
789                 if (ret < 0)
790                         goto out;
791         }
792 out:
793         free(current_dir);
794         return ret;
795 }
796
797 static int dump_table(char *dump_dir, struct osl_table_description *desc)
798 {
799         struct osl_table *t = NULL;
800         int fd, ret = fsck_init(desc, &t);
801         char *desc_file;
802
803         if (ret < 0)
804                 goto out;
805         ret = para_mkdir(dump_dir, 0777);
806         if (ret < 0)
807                 goto out;
808         desc_file = make_message("%s/table_description.c", dump_dir);
809         ret = para_open(desc_file, O_WRONLY | O_CREAT | O_EXCL, 0644);
810         free(desc_file);
811         if (ret < 0)
812                 goto out;
813         fd = ret;
814         ret = dump_table_desc(t, fd);
815         close(fd);
816         if (ret < 0)
817                 goto out;
818         ret = dump_rows(dump_dir, t);
819 out:
820         fsck_cleanup(t);
821         return ret;
822 }
823
824 static int fsck(struct osl_table_description *desc)
825 {
826         int ret;
827         struct osl_table *t = NULL;
828         uint32_t *lost_bytes = NULL;
829
830         ret = fsck_init(desc, &t);
831         if (ret < 0)
832                 goto out;
833         ret = check_index_ranges(t);
834         if (ret < 0)
835                 goto out_unmap;
836         ret = check_disk_storage_columns(t);
837         if (ret < 0)
838                 goto out_unmap;
839         ret = prune_invalid_rows_from_index(t);
840         if (ret < 0)
841                 goto out_unmap;
842         ret = check_for_invalid_objects(t, &lost_bytes);
843         if (ret < 0)
844                 goto out_unmap;
845         if (ret > 0) { /* at least one mapped data file needs pruning */
846                 ret = prune_objects(t, lost_bytes);
847                 if (ret < 0)
848                         goto out_unmap;
849         }
850         free(lost_bytes);
851         PARA_INFO_LOG("success\n");
852 out_unmap:
853         unmap_table(t, OSL_MARK_CLEAN);
854 out:
855         fsck_cleanup(t);
856         return ret;
857 }
858
859 int main(__a_unused int argc, char **argv)
860 {
861         int i, ret;
862         struct osl_table_description desc = {.column_descriptions = NULL};
863         char *base_dir = NULL;
864
865         ret = fsck_cmdline_parser(argc, argv, &conf);
866         if (ret < 0) {
867                 ret = -E_FSCK_SYNTAX;
868                 goto out;
869         }
870         HANDLE_VERSION_FLAG("fsck", conf);
871         if (!conf.inputs_num) {
872                 /* FIXME: Find all subdirs */
873                 ret = -E_FSCK_SYNTAX;
874                 goto out;
875         }
876         desc.dir = conf.base_dir_arg;
877         if (!conf.base_dir_given) {
878                 char *home = para_homedir();
879                 base_dir = make_message("%s/.paraslash/afs_database", home);
880                 free(home);
881                 desc.dir = base_dir;
882         }
883         for (i = 0; i < conf.inputs_num; i++) {
884                 desc.name = conf.inputs[i];
885                 if (!conf.no_fsck_given) {
886                         ret = fsck(&desc);
887                         if (ret < 0)
888                                 break;
889                 }
890                 if (conf.dump_dir_given && *conf.dump_dir_arg) {
891                         ret = dump_table(conf.dump_dir_arg, &desc);
892                         if (ret < 0)
893                                 break;
894                 }
895         }
896         free(base_dir);
897         ret = 1;
898 out:
899         if (ret < 0)
900                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
901         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
902 }