Fix number of whitespaces in duration status item.
[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->data, &map->size, NULL);
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 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_INFO_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 if (t->desc->column_descriptions) {
625 struct osl_column_description *cd;
626 for (i = 0; i < t->desc->num_columns; i++) {
627 cd = get_column_description(t->desc, i);
628 free((char*)cd->name);
629 }
630 free(t->desc->column_descriptions);
631 }
632 if (t) {
633 free(t->columns);
634 free(t);
635 }
636
637 }
638
639 #define ST_CASE(st) case st: return #st
640
641 const char *get_asc_storage_type(enum osl_storage_type st)
642 {
643 switch (st) {
644 ST_CASE(OSL_MAPPED_STORAGE);
645 ST_CASE(OSL_DISK_STORAGE);
646 ST_CASE(OSL_NO_STORAGE);
647 }
648 return NULL;
649 }
650
651 #define APPEND_ASC_SF(sf, flag, str) do { if (sf & flag) { \
652 if (str) str = para_strcat(str, " | " # flag); \
653 else str = para_strdup(#flag); }} while (0)
654
655
656 char *get_asc_storage_flags(enum osl_storage_type sf)
657 {
658 char *asc_sf = NULL;
659
660 APPEND_ASC_SF(sf, OSL_RBTREE, asc_sf);
661 APPEND_ASC_SF(sf, OSL_FIXED_SIZE, asc_sf);
662 APPEND_ASC_SF(sf, OSL_UNIQUE, asc_sf);
663 return asc_sf;
664 }
665
666 static int dump_table_desc(struct osl_table *t, int fd)
667 {
668 const struct osl_table_description *desc = t->desc;
669 int ret, i;
670 struct osl_column_description *cd;
671 char *msg = make_message("static struct osl_column_description cols[] = {\n");
672 ret = para_write_all(fd, msg, strlen(msg));
673 if (ret < 0)
674 return ret;
675 free(msg);
676 FOR_EACH_COLUMN(i, desc, cd) {
677 const char *asc_st;
678 msg = make_message("\t[%d] = {\n", i);
679 ret = para_write_all(fd, msg, strlen(msg));
680 if (ret < 0)
681 return ret;
682 free(msg);
683 asc_st = get_asc_storage_type(cd->storage_type);
684 msg = make_message("\t\t.storage_type = %s,\n", asc_st);
685 ret = para_write_all(fd, msg, strlen(msg));
686 if (ret < 0)
687 return ret;
688 free(msg);
689 if (cd->storage_flags) {
690 char *asc_sf = get_asc_storage_flags(cd->storage_flags);
691 msg = make_message("\t\t,storage_flags = %s,\n", asc_sf);
692 free(asc_sf);
693 ret = para_write_all(fd, msg, strlen(msg));
694 if (ret < 0)
695 return ret;
696 free(msg);
697 }
698 if (cd->storage_flags & OSL_FIXED_SIZE) {
699 msg = make_message("\t\t.data_size = %u,\n", cd->data_size);
700 ret = para_write_all(fd, msg, strlen(msg));
701 if (ret < 0)
702 return ret;
703 free(msg);
704 }
705 msg = make_message("\t\t.name = \"%s\",\n", cd->name);
706 ret = para_write_all(fd, msg, strlen(msg));
707 if (ret < 0)
708 return ret;
709 free(msg);
710 if (cd->storage_flags & OSL_RBTREE) {
711 msg = make_message("\t\t.compare_function = compare_func,\n");
712 ret = para_write_all(fd, msg, strlen(msg));
713 if (ret < 0)
714 return ret;
715 free(msg);
716 }
717 msg = make_message("\t},\n");
718 ret = para_write_all(fd, msg, strlen(msg));
719 if (ret < 0)
720 return ret;
721 free(msg);
722 }
723 msg = make_message("};\n");
724 ret = para_write_all(fd, msg, strlen(msg));
725 if (ret < 0)
726 return ret;
727 free(msg);
728 return 1;
729 }
730
731 static int dump_row(struct osl_table *t, unsigned row_num, const char *row_dir)
732 {
733 int ret, i;
734 const struct osl_column_description *cd;
735 unsigned dsnc;
736 struct osl_object obj;
737 char *ds_name;
738 HASH_TYPE hash[HASH_SIZE];
739 char *filename;
740
741 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
742 ret = get_mapped_object(t, i, row_num, &obj);
743 if (ret < 0)
744 return ret;
745 filename = make_message("%s/col_%03u", row_dir, i);
746 ret = para_write_file(filename, obj.data, obj.size);
747 free(filename);
748 if (ret < 0)
749 return ret;
750 }
751 if (!t->num_disk_storage_columns)
752 return 1;
753 dsnc = t->disk_storage_name_column;
754 ret = get_mapped_object(t, dsnc, row_num, &obj);
755 if (ret < 0)
756 return ret;
757 hash_object(&obj, hash);
758 ds_name = disk_storage_name_of_hash(t, hash);
759 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
760 filename = disk_storage_path(t, i, ds_name);
761 ret = mmap_full_file(filename, O_RDONLY, &obj.data, &obj.size, NULL);
762 free(filename);
763 if (ret < 0)
764 goto out;
765 filename = make_message("%s/col_%03u", row_dir, i);
766 ret = para_write_file(filename, obj.data, obj.size);
767 free(filename);
768 if (ret < 0)
769 goto out;
770 }
771 ret = 1;
772 out:
773 free(ds_name);
774 return ret;
775 }
776
777 static int dump_rows(char *dump_dir, struct osl_table *t)
778 {
779 unsigned i;
780 char *current_dir = NULL;
781 int ret = 0;
782
783 for (i = 0; i < t->num_rows; i++) {
784 char *row_dir;
785 if (row_is_invalid(t, i))
786 continue;
787 if (!(i % 1000)) {
788 free(current_dir);
789 current_dir = make_message("%s/rows_%u-%u", dump_dir, i, i + 999);
790 PARA_NOTICE_LOG("dumping rows %u - %u\n", i, i + 999);
791 ret = para_mkdir(current_dir, 0777);
792 if (ret < 0 && !is_errno(-ret, EEXIST))
793 goto out;
794 }
795 row_dir = make_message("%s/row_%03u", current_dir, i);
796 ret = para_mkdir(row_dir, 0777);
797 if (ret < 0 && !is_errno(-ret, EEXIST)) {
798 free(row_dir);
799 goto out;
800 }
801 ret = dump_row(t, i, row_dir);
802 free(row_dir);
803 if (ret < 0)
804 goto out;
805 }
806 out:
807 free(current_dir);
808 return ret;
809 }
810
811 static int dump_table(char *dump_dir, struct osl_table_description *desc)
812 {
813 struct osl_table *t = NULL;
814 int fd, ret = fsck_init(desc, &t);
815 char *desc_file;
816 char *table_dump_dir = NULL;
817
818 if (ret < 0)
819 goto out;
820 ret = para_mkdir(dump_dir, 0777);
821 if (ret < 0 && !is_errno(-ret, EEXIST))
822 goto out;
823 table_dump_dir = make_message("%s/%s", dump_dir, desc->name);
824 ret = para_mkdir(table_dump_dir, 0777);
825 if (ret < 0 && !is_errno(-ret, EEXIST))
826 goto out;
827 desc_file = make_message("%s/table_description.c", table_dump_dir);
828 ret = para_open(desc_file, O_WRONLY | O_CREAT | O_EXCL, 0644);
829 free(desc_file);
830 if (ret < 0)
831 goto out;
832 fd = ret;
833 ret = dump_table_desc(t, fd);
834 close(fd);
835 if (ret < 0)
836 goto out;
837 ret = dump_rows(table_dump_dir, t);
838 out:
839 free(table_dump_dir);
840 fsck_cleanup(t);
841 return ret;
842 }
843
844 static int fsck(struct osl_table_description *desc)
845 {
846 int ret;
847 struct osl_table *t = NULL;
848 uint32_t *lost_bytes = NULL;
849
850 ret = fsck_init(desc, &t);
851 if (ret < 0)
852 goto out;
853 ret = check_index_ranges(t);
854 if (ret < 0)
855 goto out_unmap;
856 ret = check_disk_storage_columns(t);
857 if (ret < 0)
858 goto out_unmap;
859 ret = prune_invalid_rows_from_index(t);
860 if (ret < 0)
861 goto out_unmap;
862 ret = check_for_invalid_objects(t, &lost_bytes);
863 if (ret < 0)
864 goto out_unmap;
865 if (ret > 0) { /* at least one mapped data file needs pruning */
866 ret = prune_objects(t, lost_bytes);
867 if (ret < 0)
868 goto out_unmap;
869 }
870 free(lost_bytes);
871 PARA_INFO_LOG("success\n");
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 if (!conf.no_fsck_given) {
889 ret = fsck(&desc);
890 if (ret < 0)
891 return ret;
892 }
893 if (!conf.dump_dir_given || !*conf.dump_dir_arg)
894 return 1;
895 return dump_table(conf.dump_dir_arg, &desc);
896 }
897
898 static int check_all_tables(char *base_dir)
899 {
900 DIR *dir;
901 struct dirent *entry;
902 int cwd_fd, ret2, ret = para_opendir(base_dir, &dir, &cwd_fd);
903
904 if (ret < 0)
905 return ret;
906 while ((entry = readdir(dir))) {
907 mode_t m;
908 struct stat s;
909 if (!strcmp(entry->d_name, "."))
910 continue;
911 if (!strcmp(entry->d_name, ".."))
912 continue;
913 if (lstat(entry->d_name, &s) == -1)
914 continue;
915 m = s.st_mode;
916 if (!S_ISDIR(m))
917 continue;
918 ret = check_table(base_dir, entry->d_name);
919 if (ret < 0)
920 break;
921 }
922 closedir(dir);
923 ret2 = para_fchdir(cwd_fd);
924 if (ret2 < 0 && ret >= 0)
925 ret = ret2;
926 close(cwd_fd);
927 return ret;
928 }
929
930 int main(int argc, char **argv)
931 {
932 int i, ret;
933 char *base_dir = NULL;
934
935 ret = fsck_cmdline_parser(argc, argv, &conf);
936 if (ret < 0) {
937 ret = -E_FSCK_SYNTAX;
938 goto out;
939 }
940 HANDLE_VERSION_FLAG("fsck", conf);
941 if (conf.base_dir_given)
942 base_dir = conf.base_dir_arg;
943 else {
944 char *home = para_homedir();
945 base_dir = make_message("%s/.paraslash/afs_database", home);
946 free(home);
947 }
948 if (!conf.inputs_num) {
949 ret = check_all_tables(base_dir);
950 goto out;
951 }
952 for (i = 0; i < conf.inputs_num; i++) {
953 ret = check_table(base_dir, conf.inputs[i]);
954 if (ret < 0)
955 break;
956 }
957 out:
958 if (!conf.base_dir_given)
959 free(base_dir);
960 if (ret < 0)
961 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
962 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
963 }