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