NEWS update.
[paraslash.git] / fsck.c
1 /*
2 * Copyright (C) 1997-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
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_INFO_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->data, &map->size, NULL);
180 PARA_DEBUG_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_DEBUG_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_INFO_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_INFO_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_INFO_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_INFO_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_INFO_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 hashes = NULL;
575 out:
576 clear_rbtrees(t); /* TODO why are we doing that here? Seems odd */
577 return ret;
578 }
579
580 static void set_dummy_contents(struct osl_table_description *desc)
581 {
582 int i;
583 struct osl_column_description *cd;
584
585 for (i = 0; i < desc->num_columns; i++) {
586 cd = get_column_description(desc, i);
587 cd->compare_function = dummy_compare;
588 }
589 }
590
591 static int fsck_init(struct osl_table_description *desc, struct osl_table **t)
592 {
593 struct osl_object map;
594 int ret = map_index(desc, &map);
595
596 if (ret < 0)
597 goto out;
598 ret = read_table_desc(&map, desc);
599 if (ret < 0) {
600 para_munmap(map.data, map.size);
601 goto out;
602 }
603 set_dummy_contents(desc);
604 ret = init_table_structure(desc, t);
605 if (ret < 0) {
606 para_munmap(map.data, map.size);
607 goto out;
608 }
609 PARA_DEBUG_LOG("unmapping index\n");
610 para_munmap(map.data, map.size);
611 if (conf.force_given)
612 ret = map_table(*t, (MAP_TBL_FL_IGNORE_DIRTY));
613 else
614 ret = map_table(*t, 0);
615 if (ret >= 0)
616 (*t)->num_rows = table_num_rows(*t);
617 out:
618 return ret;
619 }
620
621 static void fsck_cleanup(struct osl_table *t)
622 {
623 int i;
624
625 if (!t)
626 return;
627 if (t->desc->column_descriptions) {
628 struct osl_column_description *cd;
629 for (i = 0; i < t->desc->num_columns; i++) {
630 cd = get_column_description(t->desc, i);
631 free((char*)cd->name);
632 }
633 free(t->desc->column_descriptions);
634 }
635 free(t->columns);
636 free(t);
637
638 }
639
640 #define ST_CASE(st) case st: return #st
641
642 const char *get_asc_storage_type(enum osl_storage_type st)
643 {
644 switch (st) {
645 ST_CASE(OSL_MAPPED_STORAGE);
646 ST_CASE(OSL_DISK_STORAGE);
647 ST_CASE(OSL_NO_STORAGE);
648 }
649 return NULL;
650 }
651
652 #define APPEND_ASC_SF(sf, flag, str) do { if (sf & flag) { \
653 if (str) str = para_strcat(str, " | " # flag); \
654 else str = para_strdup(#flag); }} while (0)
655
656
657 char *get_asc_storage_flags(enum osl_storage_type sf)
658 {
659 char *asc_sf = NULL;
660
661 APPEND_ASC_SF(sf, OSL_RBTREE, asc_sf);
662 APPEND_ASC_SF(sf, OSL_FIXED_SIZE, asc_sf);
663 APPEND_ASC_SF(sf, OSL_UNIQUE, asc_sf);
664 return asc_sf;
665 }
666
667 static int dump_table_desc(struct osl_table *t, int fd)
668 {
669 const struct osl_table_description *desc = t->desc;
670 int ret, i;
671 struct osl_column_description *cd;
672 char *msg = make_message("static struct osl_column_description cols[] = {\n");
673 ret = para_write_all(fd, msg, strlen(msg));
674 if (ret < 0)
675 return ret;
676 free(msg);
677 FOR_EACH_COLUMN(i, desc, cd) {
678 const char *asc_st;
679 msg = make_message("\t[%d] = {\n", i);
680 ret = para_write_all(fd, msg, strlen(msg));
681 if (ret < 0)
682 return ret;
683 free(msg);
684 asc_st = get_asc_storage_type(cd->storage_type);
685 msg = make_message("\t\t.storage_type = %s,\n", asc_st);
686 ret = para_write_all(fd, msg, strlen(msg));
687 if (ret < 0)
688 return ret;
689 free(msg);
690 if (cd->storage_flags) {
691 char *asc_sf = get_asc_storage_flags(cd->storage_flags);
692 msg = make_message("\t\t,storage_flags = %s,\n", asc_sf);
693 free(asc_sf);
694 ret = para_write_all(fd, msg, strlen(msg));
695 if (ret < 0)
696 return ret;
697 free(msg);
698 }
699 if (cd->storage_flags & OSL_FIXED_SIZE) {
700 msg = make_message("\t\t.data_size = %u,\n", cd->data_size);
701 ret = para_write_all(fd, msg, strlen(msg));
702 if (ret < 0)
703 return ret;
704 free(msg);
705 }
706 msg = make_message("\t\t.name = \"%s\",\n", cd->name);
707 ret = para_write_all(fd, msg, strlen(msg));
708 if (ret < 0)
709 return ret;
710 free(msg);
711 if (cd->storage_flags & OSL_RBTREE) {
712 msg = make_message("\t\t.compare_function = compare_func,\n");
713 ret = para_write_all(fd, msg, strlen(msg));
714 if (ret < 0)
715 return ret;
716 free(msg);
717 }
718 msg = make_message("\t},\n");
719 ret = para_write_all(fd, msg, strlen(msg));
720 if (ret < 0)
721 return ret;
722 free(msg);
723 }
724 msg = make_message("};\n");
725 ret = para_write_all(fd, msg, strlen(msg));
726 if (ret < 0)
727 return ret;
728 free(msg);
729 return 1;
730 }
731
732 static int dump_row(struct osl_table *t, unsigned row_num, const char *row_dir)
733 {
734 int ret, i;
735 const struct osl_column_description *cd;
736 unsigned dsnc;
737 struct osl_object obj;
738 char *ds_name;
739 HASH_TYPE hash[HASH_SIZE];
740 char *filename;
741
742 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
743 ret = get_mapped_object(t, i, row_num, &obj);
744 if (ret < 0)
745 return ret;
746 filename = make_message("%s/col_%03u", row_dir, i);
747 ret = para_write_file(filename, obj.data, obj.size);
748 free(filename);
749 if (ret < 0)
750 return ret;
751 }
752 if (!t->num_disk_storage_columns)
753 return 1;
754 dsnc = t->disk_storage_name_column;
755 ret = get_mapped_object(t, dsnc, row_num, &obj);
756 if (ret < 0)
757 return ret;
758 hash_object(&obj, hash);
759 ds_name = disk_storage_name_of_hash(t, hash);
760 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
761 filename = disk_storage_path(t, i, ds_name);
762 ret = mmap_full_file(filename, O_RDONLY, &obj.data, &obj.size, NULL);
763 free(filename);
764 if (ret < 0)
765 goto out;
766 filename = make_message("%s/col_%03u", row_dir, i);
767 ret = para_write_file(filename, obj.data, obj.size);
768 free(filename);
769 if (ret < 0)
770 goto out;
771 }
772 ret = 1;
773 out:
774 free(ds_name);
775 return ret;
776 }
777
778 static int dump_rows(char *dump_dir, struct osl_table *t)
779 {
780 unsigned i;
781 char *current_dir = NULL;
782 int ret = 0;
783
784 for (i = 0; i < t->num_rows; i++) {
785 char *row_dir;
786 if (row_is_invalid(t, i))
787 continue;
788 if (!(i % 1000)) {
789 free(current_dir);
790 current_dir = make_message("%s/rows_%u-%u", dump_dir, i, i + 999);
791 PARA_NOTICE_LOG("dumping rows %u - %u\n", i, i + 999);
792 ret = para_mkdir(current_dir, 0777);
793 if (ret < 0 && !is_errno(-ret, EEXIST))
794 goto out;
795 }
796 row_dir = make_message("%s/row_%03u", current_dir, i);
797 ret = para_mkdir(row_dir, 0777);
798 if (ret < 0 && !is_errno(-ret, EEXIST)) {
799 free(row_dir);
800 goto out;
801 }
802 ret = dump_row(t, i, row_dir);
803 free(row_dir);
804 if (ret < 0)
805 goto out;
806 }
807 out:
808 free(current_dir);
809 return ret;
810 }
811
812 static int dump_table(char *dump_dir, struct osl_table_description *desc)
813 {
814 struct osl_table *t = NULL;
815 int fd, ret = fsck_init(desc, &t);
816 char *desc_file;
817 char *table_dump_dir = NULL;
818
819 if (ret < 0)
820 goto out;
821 ret = para_mkdir(dump_dir, 0777);
822 if (ret < 0 && !is_errno(-ret, EEXIST))
823 goto out;
824 table_dump_dir = make_message("%s/%s", dump_dir, desc->name);
825 ret = para_mkdir(table_dump_dir, 0777);
826 if (ret < 0 && !is_errno(-ret, EEXIST))
827 goto out;
828 desc_file = make_message("%s/table_description.c", table_dump_dir);
829 ret = para_open(desc_file, O_WRONLY | O_CREAT | O_EXCL, 0644);
830 free(desc_file);
831 if (ret < 0)
832 goto out;
833 fd = ret;
834 ret = dump_table_desc(t, fd);
835 close(fd);
836 if (ret < 0)
837 goto out;
838 ret = dump_rows(table_dump_dir, t);
839 out:
840 free(table_dump_dir);
841 fsck_cleanup(t);
842 return ret;
843 }
844
845 static int fsck(struct osl_table_description *desc)
846 {
847 int ret;
848 struct osl_table *t = NULL;
849 uint32_t *lost_bytes = NULL;
850
851 ret = fsck_init(desc, &t);
852 if (ret < 0)
853 goto out;
854 ret = check_index_ranges(t);
855 if (ret < 0)
856 goto out_unmap;
857 ret = check_disk_storage_columns(t);
858 if (ret < 0)
859 goto out_unmap;
860 ret = prune_invalid_rows_from_index(t);
861 if (ret < 0)
862 goto out_unmap;
863 ret = check_for_invalid_objects(t, &lost_bytes);
864 if (ret < 0)
865 goto out_unmap;
866 if (ret > 0) { /* at least one mapped data file needs pruning */
867 ret = prune_objects(t, lost_bytes);
868 if (ret < 0)
869 goto out_unmap;
870 }
871 free(lost_bytes);
872 out_unmap:
873 unmap_table(t, OSL_MARK_CLEAN);
874 out:
875 fsck_cleanup(t);
876 return ret;
877 }
878
879 static int check_table(char *base_dir, char *table_name)
880 {
881 struct osl_table_description desc = {
882 .column_descriptions = NULL,
883 .dir = base_dir,
884 .name = table_name
885 };
886 int ret;
887
888 PARA_INFO_LOG("checking table %s\n", table_name);
889 if (!conf.no_fsck_given) {
890 ret = fsck(&desc);
891 if (ret < 0)
892 goto out;
893 }
894 ret = 1;
895 if (!conf.dump_dir_given || !*conf.dump_dir_arg)
896 goto out;
897 ret = dump_table(conf.dump_dir_arg, &desc);
898 out:
899 if (ret < 0)
900 PARA_ERROR_LOG("failed to check table %s\n", table_name);
901 else
902 PARA_NOTICE_LOG("successfully checked table %s\n", table_name);
903 return ret;
904 }
905
906 static int check_all_tables(char *base_dir)
907 {
908 DIR *dir;
909 struct dirent *entry;
910 int cwd_fd, ret2, ret = para_opendir(base_dir, &dir, &cwd_fd);
911
912 if (ret < 0)
913 return ret;
914 while ((entry = readdir(dir))) {
915 mode_t m;
916 struct stat s;
917 if (!strcmp(entry->d_name, "."))
918 continue;
919 if (!strcmp(entry->d_name, ".."))
920 continue;
921 if (lstat(entry->d_name, &s) == -1)
922 continue;
923 m = s.st_mode;
924 if (!S_ISDIR(m))
925 continue;
926 ret = check_table(base_dir, entry->d_name);
927 if (ret < 0)
928 break;
929 }
930 closedir(dir);
931 ret2 = para_fchdir(cwd_fd);
932 if (ret2 < 0 && ret >= 0)
933 ret = ret2;
934 close(cwd_fd);
935 return ret;
936 }
937
938 int main(int argc, char **argv)
939 {
940 int i, ret;
941 char *base_dir = NULL;
942
943 ret = fsck_cmdline_parser(argc, argv, &conf);
944 if (ret < 0) {
945 ret = -E_FSCK_SYNTAX;
946 goto out;
947 }
948 HANDLE_VERSION_FLAG("fsck", conf);
949 if (conf.base_dir_given)
950 base_dir = para_strdup(conf.base_dir_arg);
951 else {
952 char *home = para_homedir();
953 base_dir = make_message("%s/.paraslash/afs_database", home);
954 free(home);
955 }
956 if (!conf.inputs_num) {
957 ret = check_all_tables(base_dir);
958 goto out;
959 }
960 for (i = 0; i < conf.inputs_num; i++) {
961 ret = check_table(base_dir, conf.inputs[i]);
962 if (ret < 0)
963 break;
964 }
965 out:
966 if (ret < 0) {
967 PARA_ERROR_LOG("%s%s: %s\n",
968 base_dir? "base_dir: " : "",
969 base_dir? base_dir : "",
970 para_strerror(-ret)
971 );
972 if (conf.loglevel_arg > 1)
973 PARA_EMERG_LOG("re-run with \"--logelvel %d\" to increase verbosity\n",
974 conf.loglevel_arg - 1);
975 } else
976 PARA_NOTICE_LOG("success\n");
977 if (base_dir)
978 free(base_dir);
979 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
980 }