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