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