Fix fsck error codes.
[osl.git] / fsck.c
1 /*
2 * Copyright (C) 2007-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 /* FIXME: check error codes of make_message or write wrapper */
10
11
12
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include <dirent.h>
16 #include <assert.h>
17 #include <pwd.h>
18
19 #include "log.h"
20 #include "osl.h"
21 #include "error.h"
22 #include "fd.h"
23 #include "osl_core.h"
24 #include "fsck.cmdline.h"
25
26 /** version text used by various commands if -V switch was given */
27 #define VERSION_TEXT(prefix) "osl_" prefix " " VERSION " " "\n" \
28 "Copyright (C) 2008 Andre Noll\n" \
29 "This is free software with ABSOLUTELY NO WARRANTY." \
30 " See COPYING for details.\n" \
31 "Written by Andre Noll.\n" \
32 "Report bugs to <maan@systemlinux.org>.\n"
33
34 /** print out \p VERSION_TEXT and exit if version flag was given */
35 #define HANDLE_VERSION_FLAG(_prefix, _args_info_struct) \
36 if (_args_info_struct.version_given) { \
37 printf("%s", VERSION_TEXT(_prefix)); \
38 exit(EXIT_SUCCESS); \
39 }
40
41 static struct fsck_args_info conf;
42
43 #define FSCK_ERRORS \
44 FSCK_ERROR(RANGE_VIOLATION, "range violation detected, very bad"), \
45 FSCK_ERROR(NOT_A_REGULAR_FILE, "not a regular file"), \
46 FSCK_ERROR(SYNTAX, "fsck syntax error"),
47
48 #define FSCK_ERROR(num, txt) E_FSCK_ ## num
49 enum {
50 FSCK_DUMMY = (1 << FSCK_ERROR_BIT) - 1,
51 FSCK_ERRORS
52 };
53 #undef FSCK_ERROR
54 #define FSCK_ERROR(num, txt) txt
55 const char const *fsck_errors[] = {
56 FSCK_ERRORS
57 };
58
59 static const char *fsck_strerror(int num)
60 {
61 if (num & (1 << FSCK_ERROR_BIT))
62 return fsck_errors[num & ((1 << FSCK_ERROR_BIT) - 1)];
63 return osl_strerror(num);
64 }
65
66 __printf_2_3 void para_log(int ll, const char* fmt,...)
67 {
68 va_list argp;
69
70 if (ll < conf.loglevel_arg)
71 return;
72 va_start(argp, fmt);
73 vfprintf(stderr, fmt, argp);
74 va_end(argp);
75 }
76
77 /* taken from git */
78 signed char hexval_table[256] = {
79 -1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */
80 -1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */
81 -1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */
82 -1, -1, -1, -1, -1, -1, -1, -1, /* 18-1f */
83 -1, -1, -1, -1, -1, -1, -1, -1, /* 20-27 */
84 -1, -1, -1, -1, -1, -1, -1, -1, /* 28-2f */
85 0, 1, 2, 3, 4, 5, 6, 7, /* 30-37 */
86 8, 9, -1, -1, -1, -1, -1, -1, /* 38-3f */
87 -1, 10, 11, 12, 13, 14, 15, -1, /* 40-47 */
88 -1, -1, -1, -1, -1, -1, -1, -1, /* 48-4f */
89 -1, -1, -1, -1, -1, -1, -1, -1, /* 50-57 */
90 -1, -1, -1, -1, -1, -1, -1, -1, /* 58-5f */
91 -1, 10, 11, 12, 13, 14, 15, -1, /* 60-67 */
92 -1, -1, -1, -1, -1, -1, -1, -1, /* 68-67 */
93 -1, -1, -1, -1, -1, -1, -1, -1, /* 70-77 */
94 -1, -1, -1, -1, -1, -1, -1, -1, /* 78-7f */
95 -1, -1, -1, -1, -1, -1, -1, -1, /* 80-87 */
96 -1, -1, -1, -1, -1, -1, -1, -1, /* 88-8f */
97 -1, -1, -1, -1, -1, -1, -1, -1, /* 90-97 */
98 -1, -1, -1, -1, -1, -1, -1, -1, /* 98-9f */
99 -1, -1, -1, -1, -1, -1, -1, -1, /* a0-a7 */
100 -1, -1, -1, -1, -1, -1, -1, -1, /* a8-af */
101 -1, -1, -1, -1, -1, -1, -1, -1, /* b0-b7 */
102 -1, -1, -1, -1, -1, -1, -1, -1, /* b8-bf */
103 -1, -1, -1, -1, -1, -1, -1, -1, /* c0-c7 */
104 -1, -1, -1, -1, -1, -1, -1, -1, /* c8-cf */
105 -1, -1, -1, -1, -1, -1, -1, -1, /* d0-d7 */
106 -1, -1, -1, -1, -1, -1, -1, -1, /* d8-df */
107 -1, -1, -1, -1, -1, -1, -1, -1, /* e0-e7 */
108 -1, -1, -1, -1, -1, -1, -1, -1, /* e8-ef */
109 -1, -1, -1, -1, -1, -1, -1, -1, /* f0-f7 */
110 -1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */
111 };
112
113 int asc_to_hash(const char *asc_hash, int len, HASH_TYPE *hash)
114 {
115 int i = 0;
116 const unsigned char *asc = (const unsigned char *) asc_hash;
117
118 while (*asc && i++ < len) {
119 unsigned int val = (hexval_table[asc[0]] << 4) | hexval_table[asc[1]];
120 if (val & ~0xff)
121 return -1;
122 *hash++ = val;
123 asc += 2;
124
125 }
126 return 1;
127 }
128
129 static int _write_all(int fd, const char *buf, size_t len)
130 {
131 return write_all(fd, buf, &len);
132 }
133
134 /**
135 * Paraslash's version of malloc().
136 *
137 * \param size The desired new size.
138 *
139 * A wrapper for malloc(3) which exits on errors.
140 *
141 * \return A pointer to the allocated memory, which is suitably aligned for any
142 * kind of variable.
143 *
144 * \sa malloc(3).
145 */
146 __must_check __malloc static void *para_malloc(size_t size)
147 {
148 assert(size);
149 void *p = malloc(size);
150
151 if (!p) {
152 EMERG_LOG("malloc failed (size = %zu), aborting\n",
153 size);
154 exit(EXIT_FAILURE);
155 }
156 return p;
157 }
158
159 /**
160 * Paraslash's version of calloc().
161 *
162 * \param size The desired new size.
163 *
164 * A wrapper for calloc(3) which exits on errors.
165 *
166 * \return A pointer to the allocated and zeroed-out memory, which is suitably
167 * aligned for any kind of variable.
168 *
169 * \sa calloc(3)
170 */
171 __must_check __malloc static void *para_calloc(size_t size)
172 {
173 void *ret = para_malloc(size);
174
175 memset(ret, 0, size);
176 return ret;
177 }
178
179 /**
180 * Paraslash's version of strdup().
181 *
182 * \param s The string to be duplicated.
183 *
184 * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
185 * there is no need to check the return value in the caller.
186 *
187 * \return A pointer to the duplicated string. If \p s was the NULL pointer,
188 * an pointer to an empty string is returned.
189 *
190 * \sa strdup(3)
191 */
192 __must_check __malloc static char *para_strdup(const char *s)
193 {
194 char *ret;
195
196 if ((ret = strdup(s? s: "")))
197 return ret;
198 EMERG_LOG("strdup failed, aborting\n");
199 exit(EXIT_FAILURE);
200 }
201
202 /**
203 * Get the home directory of the current user.
204 *
205 * \return A dynammically allocated string that must be freed by the caller. If
206 * the home directory could not be found, this function returns "/tmp".
207 */
208 __must_check __malloc static char *para_homedir(void)
209 {
210 struct passwd *pw = getpwuid(getuid());
211 return para_strdup(pw? pw->pw_dir : "/tmp");
212 }
213
214 /**
215 * Compare two osl objects pointing to unsigned integers of 32 bit size.
216 *
217 * \param obj1 Pointer to the first integer.
218 * \param obj2 Pointer to the second integer.
219 *
220 * \return The values required for an osl compare function.
221 *
222 * \sa osl_compare_func, osl_hash_compare().
223 */
224 static int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2)
225 {
226 uint32_t d1 = read_u32((const char *)obj1->data);
227 uint32_t d2 = read_u32((const char *)obj2->data);
228
229 if (d1 < d2)
230 return 1;
231 if (d1 > d2)
232 return -1;
233 return 0;
234 }
235
236 /**
237 * Traverse the given directory recursively.
238 *
239 * \param dirname The directory to traverse.
240 * \param func The function to call for each entry.
241 * \param private_data Pointer to an arbitrary data structure.
242 *
243 * For each regular file under \a dirname, the supplied function \a func is
244 * called. The full path of the regular file and the \a private_data pointer
245 * are passed to \a func. Directories for which the calling process has no
246 * permissions to change to are silently ignored.
247 *
248 * \return Standard.
249 */
250 static int for_each_file_in_dir(const char *dirname,
251 int (*func)(const char *, void *), void *private_data)
252 {
253 DIR *dir;
254 struct dirent *entry;
255 int cwd_fd, ret2, ret = para_opendir(dirname, &dir, &cwd_fd);
256
257 if (ret < 0)
258 return ret == -ERRNO_TO_ERROR(EACCES)? 1 : ret;
259 /* scan cwd recursively */
260 while ((entry = readdir(dir))) {
261 mode_t m;
262 char *tmp;
263 struct stat s;
264
265 if (!strcmp(entry->d_name, "."))
266 continue;
267 if (!strcmp(entry->d_name, ".."))
268 continue;
269 if (lstat(entry->d_name, &s) == -1)
270 continue;
271 m = s.st_mode;
272 if (!S_ISREG(m) && !S_ISDIR(m))
273 continue;
274 tmp = make_message("%s/%s", dirname, entry->d_name);
275 if (!S_ISDIR(m)) {
276 ret = func(tmp, private_data);
277 free(tmp);
278 if (ret < 0)
279 goto out;
280 continue;
281 }
282 /* directory */
283 ret = for_each_file_in_dir(tmp, func, private_data);
284 free(tmp);
285 if (ret < 0)
286 goto out;
287 }
288 ret = 1;
289 out:
290 closedir(dir);
291 ret2 = para_fchdir(cwd_fd);
292 if (ret2 < 0 && ret >= 0)
293 ret = ret2;
294 close(cwd_fd);
295 return ret;
296 }
297
298 /*
299 * check for object boundary violations
300 *
301 * test whether the range pointed to by the index entry for a given cell is
302 * contained in mapped data file. This should always be the case. Otherwise
303 * we are in real trouble.
304 */
305 static int check_range(struct osl_table *t, uint32_t row_num, uint32_t col_num)
306 {
307 char *index_entry;
308 struct osl_object obj;
309 struct osl_column *col;
310 int ret;
311 char *map_start, *obj_start;
312
313 ret = get_cell_index(t, row_num, col_num, &index_entry);
314 if (ret < 0)
315 return ret;
316 ret = get_mapped_object(t, col_num, row_num, &obj);
317 if (ret < 0)
318 return ret;
319 col = t->columns + col_num;
320 obj_start = obj.data;
321 map_start = col->data_map.data;
322 // INFO_LOG("obj: %p..%p\n", obj_start, obj_start + obj.size);
323 // INFO_LOG("map: %p..%p\n", map_start, map_start + col->data_map.size);
324 if (obj_start < map_start || obj_start + obj.size > map_start + col->data_map.size) {
325 CRIT_LOG("range violation in row %u, col %u\n", row_num,
326 col_num);
327 return -E_FSCK_RANGE_VIOLATION;
328 }
329 DEBUG_LOG("col %u: ok\n", col_num);
330 return 1;
331 }
332
333 /*
334 * check all cells of the given table for boundary violations
335 */
336 static int check_index_ranges(struct osl_table *t)
337 {
338 int i, j, ret;
339
340 INFO_LOG("checking for range violations in index\n");
341 //DEBUG_LOG("%d rows. %d columns\n", t->num_rows, t->desc->num_columns);
342 t->num_invalid_rows = 0;
343 for (i = 0; i < t->num_rows; i++) {
344 if (row_is_invalid(t, i)) {
345 t->num_invalid_rows++;
346 continue;
347 }
348 for (j = 0; j < t->desc->num_columns; j++) { /* FXIME */
349 const struct osl_column_description *cd =
350 get_column_description(t->desc, j);
351 if (cd->storage_type != OSL_MAPPED_STORAGE)
352 continue;
353 ret = check_range(t, i, j);
354 if (ret < 0) {
355 if (ret != -E_OSL_INVALID_OBJECT &&
356 ret != -E_FSCK_RANGE_VIOLATION)
357 goto err;
358 if (ret == -E_OSL_INVALID_OBJECT) {
359 CRIT_LOG("row %d, col %d maps to an "
360 "invalid object\n", i, j);
361 }
362 ret = mark_row_invalid(t, i);
363 if (ret < 0)
364 goto err;
365 t->num_invalid_rows++;
366 break;
367 }
368 }
369
370 }
371 if (t->num_invalid_rows)
372 NOTICE_LOG("ranges OK. %d invalid row(s) detected\n",
373 t->num_invalid_rows);
374 else
375 INFO_LOG("no invalid rows, no range violations, good\n");
376 return 1;
377 err:
378 return ret;
379 }
380
381 static int move_index_entry(struct osl_table *t, uint32_t dest, uint32_t src)
382 {
383 char *dest_ie, *src_ie;
384 int ret = get_row_index(t, dest, &dest_ie);
385
386 if (ret < 0)
387 return ret;
388 ret = get_row_index(t, src, &src_ie);
389 if (ret < 0)
390 return ret;
391 INFO_LOG("moving entry #%u to position %u\n", src, dest);
392 memcpy(dest_ie, src_ie, t->row_index_size);
393 return 1;
394 }
395
396 static int map_index(const struct osl_table_description *desc, struct osl_object *map)
397 {
398 char *filename = index_filename(desc);
399 int ret;
400
401 ret = mmap_full_file(filename, O_RDWR, &map->data, &map->size, NULL);
402 DEBUG_LOG("mapping index %s: ret: %d, size: %zu\n", filename, ret, map->size);
403 free(filename);
404 return ret;
405 }
406
407 static int prune_invalid_rows_from_index(struct osl_table *t)
408 {
409 uint32_t top = 0, bottom;
410 char *filename;
411 int ret;
412
413 if (!t->num_invalid_rows) {
414 INFO_LOG("all rows are valid, good\n");
415 return 1;
416 }
417 NOTICE_LOG("deleting %u invalid row(s) (%d bytes) from index\n",
418 t->num_invalid_rows, t->row_index_size * t->num_invalid_rows);
419 bottom = t->num_rows - 1;
420 while (top < bottom) {
421 if (!row_is_invalid(t, top)) {
422 top++;
423 continue;
424 }
425 while (bottom > top) {
426 if (row_is_invalid(t, bottom)) {
427 bottom--;
428 continue;
429 }
430 /* move bottom index entry to top */
431 move_index_entry(t, top, bottom);
432 bottom--;
433 top++;
434 break;
435 }
436 }
437 DEBUG_LOG("unmapping index\n");
438 osl_munmap(t->index_map.data, t->index_map.size);
439 filename = index_filename(t->desc);
440 ret = para_truncate(filename, t->row_index_size
441 * t->num_invalid_rows);
442 free(filename);
443 if (ret < 0)
444 return ret;
445 ret = map_index(t->desc, &t->index_map);
446 if (ret < 0)
447 return ret;
448 t->num_rows = table_num_rows(t);
449 return 1;
450 }
451
452 static int check_for_invalid_objects(struct osl_table *t, uint32_t **lost_bytes)
453 {
454 int i, j, ret;
455 const struct osl_column_description *cd;
456 uint32_t *loss = para_malloc(sizeof(uint32_t) * t->desc->num_columns);
457
458 INFO_LOG("looking for mapped objects not contained in index\n");
459 /* first count used bytes */
460 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
461 loss[i] = t->columns[i].data_map.size;
462 for (j = 0; j < t->num_rows; j++) {
463 struct osl_object obj;
464 ret = get_mapped_object(t, i, j, &obj);
465 if (ret >= 0) {
466 loss[i] -= obj.size + 1; /* add one for header byte */
467 continue;
468 }
469 if (ret != -E_OSL_INVALID_OBJECT)
470 goto err;
471 CRIT_LOG("row %d, col %d points to an invalid "
472 "mapped object, bad\n", j, i);
473 }
474 }
475 ret = 0;
476 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
477 if (loss[i]) {
478 NOTICE_LOG("column %u contains %u lost bytes\n",
479 i, loss[i]);
480 ret = 1;
481 }
482 }
483 if (!ret)
484 INFO_LOG("all mapped objects are valid, good\n");
485 *lost_bytes = loss;
486 return ret;
487 err:
488 free(loss);
489 return ret;
490 }
491
492 /* prune_invalid_rows() must be run on the table before calling this */
493 static int prune_mapped_column(struct osl_table *t, uint32_t col_num, int fd)
494 {
495 int i, ret;
496 uint32_t written = 0;
497 struct osl_column *col = t->columns + col_num;
498
499 INFO_LOG("pruning col %u\n", col_num);
500 for (i = 0; i < t->num_rows; i++) {
501 struct osl_object obj;
502 char *index_entry;
503
504 DEBUG_LOG("checking row %u/%u\n", i, t->num_rows);
505 ret = get_mapped_object(t, col_num, i, &obj);
506 if (ret < 0)
507 return ret;
508 ret = _write_all(fd, (char *)(obj.data) - 1, obj.size + 1);
509 if (ret < 0)
510 return ret;
511 written += obj.size + 1;
512 ret = get_row_index(t, i, &index_entry);
513 if (ret < 0)
514 return ret;
515 update_cell_index(index_entry, col, written, obj.size);
516 }
517 return 1;
518 }
519
520 static int prune_objects(struct osl_table *t, uint32_t *lost_bytes)
521 {
522 int i, ret;
523 const struct osl_column_description *cd;
524 char **col_filenames = para_calloc(t->desc->num_columns * sizeof(char *));
525 char **new_col_filenames = para_calloc(t->desc->num_columns * sizeof(char *));
526 char *idx_filename = index_filename(t->desc);
527 char *old_idx_filename = make_message("%s.bak", idx_filename);
528 int fd;
529
530 NOTICE_LOG("removing unreferenced objects from data files\n");
531 /* first make a copy of the index */
532 ret = osl_open(old_idx_filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
533 if (ret < 0)
534 goto out_free;
535 fd = ret;
536 ret = _write_all(fd, t->index_map.data, t->index_map.size);
537 close(fd);
538 if (ret < 0)
539 goto out_free;
540 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
541 if (!lost_bytes[i])
542 continue;
543 col_filenames[i] = column_filename(t, i);
544 new_col_filenames[i] = make_message("%s.fsck", col_filenames[i]);
545 ret = osl_open(new_col_filenames[i], O_WRONLY | O_CREAT | O_EXCL, 0644);
546 if (ret < 0)
547 goto out_unlink_data;
548 fd = ret;
549 ret = prune_mapped_column(t, i, fd);
550 close(fd);
551 if (ret < 0)
552 goto out_unlink_data;
553 }
554 ret = unmap_table(t, OSL_MARK_CLEAN);
555 if (ret < 0)
556 goto out_unlink_data;
557 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
558 if (!lost_bytes[i])
559 continue;
560 ret = osl_rename(new_col_filenames[i], col_filenames[i]);
561 if (ret < 0) { /* we're kinda screwed here */
562 CRIT_LOG("rename of col %i failed: %s\n", i,
563 osl_strerror(errno));
564 goto out_free;
565 }
566 }
567 unlink(old_idx_filename);
568 ret = map_table(t, 0);
569 goto out_free;
570 out_unlink_data:
571 FOR_EACH_MAPPED_COLUMN(i, t, cd)
572 unlink(new_col_filenames[i]);
573 out_free:
574 free(old_idx_filename);
575 free(idx_filename);
576 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
577 free(col_filenames[i]);
578 free(new_col_filenames[i]);
579 }
580 free(col_filenames);
581 free(new_col_filenames);
582 return ret;
583 }
584
585 static struct osl_column_description hash_tree_table_cols[] = {
586 {
587 .storage_type = OSL_NO_STORAGE,
588 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
589 .name = "hash",
590 .compare_function = uint32_compare,
591 .data_size = HASH_SIZE
592 },
593 };
594
595 static const struct osl_table_description hash_tree_table_desc = {
596 .dir = "/", /* irrelevant */
597 .name = "hash_tree",
598 .num_columns = 1,
599 .flags = 0,
600 .column_descriptions = hash_tree_table_cols
601 };
602
603 /**
604 * The hash_tree table contains all hashes of the disk storage name column.
605 * of each row. It is used for checking if a disk storage file has a reference
606 * in the table.
607 */
608 static struct osl_table *hash_tree_table;
609 static HASH_TYPE *hashes;
610
611 static int check_disk_storage_column(struct osl_table *t, int row_num,
612 int col_num, char *ds_name, unsigned *num_missing_objects)
613 {
614 int ret;
615 struct stat statbuf;
616 char *path = disk_storage_path(t, col_num, ds_name);
617 unsigned dsnc = t->disk_storage_name_column;
618 struct osl_object obj;
619
620 DEBUG_LOG("checking if %s is a regular file\n", path);
621 ret = stat(path, &statbuf);
622 if (ret < 0 && errno == ENOENT) {
623 struct osl_row *row;
624 (*num_missing_objects)++;
625 ERROR_LOG("row %d: object %s is missing\n", row_num, path);
626 NOTICE_LOG("trying to delete row %d\n", row_num);
627 ret = osl_get_row(t, dsnc, &obj, &row);
628 if (ret < 0) {
629 CRIT_LOG("unable to get row %d\n", row_num);
630 mark_row_invalid(t, row_num);
631 CRIT_LOG("Please re-run fsck\n");
632 goto out;
633 }
634 ret = osl_del_row(t, row);
635 if (ret < 0)
636 goto out;
637 }
638 out:
639 free(path);
640 if (ret < 0)
641 return ret;
642 ret = -E_FSCK_NOT_A_REGULAR_FILE;
643 if (!(S_IFREG & statbuf.st_mode))
644 return ret;
645 return 1;
646 }
647
648 static int check_disk_storage_presence(struct osl_table *t)
649 {
650 int ret, i, j;
651 struct osl_object obj, hash_obj = {.size = HASH_SIZE};
652 char *ds_name;
653 const struct osl_column_description *cd;
654 unsigned dsnc = t->disk_storage_name_column, missing_objects = 0;
655
656 if (!t->num_rows)
657 return 1;
658 hashes = para_malloc(t->num_rows * HASH_SIZE);
659 INFO_LOG("looking for missing disk storage objects\n");
660 for (i = 0; i < t->num_rows; i++) {
661 if (row_is_invalid(t, i))
662 continue;
663 ret = get_mapped_object(t, dsnc, i, &obj);
664 if (ret < 0)
665 return ret;
666 hash_object(&obj, hashes + i * HASH_SIZE);
667 hash_obj.data = hashes + i * HASH_SIZE;
668 osl_add_row(hash_tree_table, &hash_obj);
669 ds_name = disk_storage_name_of_hash(t, hashes + i * HASH_SIZE);
670 FOR_EACH_DISK_STORAGE_COLUMN(j, t, cd) {
671 ret = check_disk_storage_column(t, i, j, ds_name,
672 &missing_objects);
673 if (ret < 0)
674 goto err;
675 }
676 free(ds_name);
677 }
678 if (!missing_objects)
679 INFO_LOG("all referenced disk storage objects exist, good\n");
680 else
681 NOTICE_LOG("%d missing object(s)\n", missing_objects);
682 return missing_objects;
683 err:
684 free(ds_name);
685 return ret;
686 }
687
688 static int dummy_compare(const struct osl_object *obj1, const struct osl_object *obj2)
689 {
690 if (obj1 < obj2)
691 return -1;
692 if (obj1 > obj2)
693 return 1;
694 return 0;
695 }
696
697 static unsigned files_pruned;
698
699 int prune_disk_storage_file(const char *path, void *private_data)
700 {
701 HASH_TYPE hash[HASH_SIZE];
702 unsigned flags = *(unsigned *)private_data;
703 struct osl_object obj = {.data = hash, .size = HASH_SIZE};
704 struct osl_row *row;
705 int ret = -1;
706 size_t len = strlen(path);
707
708
709 DEBUG_LOG("path: %s\n", path);
710 if (flags & OSL_LARGE_TABLE) {
711 if (len < HASH_SIZE * 2 + 2)
712 goto invalid;
713 // NOTICE_LOG("p: %s\n", path + len - 2 * HASH_SIZE - 1);
714 ret = asc_to_hash(path + len - 2 * HASH_SIZE - 1, 1, hash);
715 if (ret < 0)
716 goto invalid;
717 ret = asc_to_hash(path + len - 2 * HASH_SIZE + 2, HASH_SIZE - 1,
718 hash + 1);
719 if (ret < 0)
720 goto invalid;
721 // INFO_LOG("high: %x, low: %x, hash: %x\n", high, low, hash);
722 } else {
723 if (len < 2 * HASH_SIZE + 1)
724 goto invalid;
725 ret = asc_to_hash(path + len - 2 * HASH_SIZE, 2 * HASH_SIZE, hash);
726 if (ret < 0)
727 goto invalid;
728 // INFO_LOG("hash: %x\n", hash);
729 }
730 #if 0
731 {
732 char asc[2 * HASH_SIZE + 1];
733 hash_to_asc(hash, asc);
734 NOTICE_LOG("before: %s\nafter: %s\n", path, asc);
735 }
736 #endif
737 ret = osl_get_row(hash_tree_table, 0, &obj, &row);
738 if (ret >= 0)
739 return 1;
740 NOTICE_LOG("unreferenced file in hash dir: %s\n", path);
741 goto remove;
742 invalid:
743 ERROR_LOG("could not read hash value of %s\n", path);
744 remove:
745 NOTICE_LOG("removing %s\n", path);
746 unlink(path);
747 files_pruned++;
748 return 1;
749 }
750
751 static int prune_disk_storage_files(struct osl_table *t)
752 {
753 int i, ret = 1;
754 const struct osl_column_description *cd;
755
756 INFO_LOG("looking for unreferenced disk storage files\n");
757 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
758 char *dirname = column_filename(t, i);
759 ret = for_each_file_in_dir(dirname, prune_disk_storage_file,
760 (unsigned *)&t->desc->flags);
761 free(dirname);
762 }
763 if (files_pruned)
764 NOTICE_LOG("%u disk storage files deleted\n",
765 files_pruned);
766 else
767 INFO_LOG("all files are are referenced, good\n");
768 return ret;
769 }
770
771 static int check_disk_storage_columns(struct osl_table *t)
772 {
773 int ret, i;
774 const struct osl_column_description *cd;
775
776 if (!t->num_disk_storage_columns) {
777 INFO_LOG("no disk storage columns in table '%s', "
778 "skipping checks\n", t->desc->name);
779 return 1;
780 }
781 FOR_EACH_COLUMN(i, t->desc, cd)
782 t->desc->column_descriptions[i].compare_function = dummy_compare;
783 ret = init_rbtrees(t);
784 if (ret < 0)
785 return ret;
786 INFO_LOG("creating rbtree for disk storage hash values\n");
787 ret = osl_open_table(&hash_tree_table_desc, &hash_tree_table);
788 if (ret < 0)
789 goto out;
790 ret = check_disk_storage_presence(t);
791 if (ret < 0)
792 goto out_close_hash_tree;
793 ret = prune_disk_storage_files(t);
794 out_close_hash_tree:
795 osl_close_table(hash_tree_table, 0);
796 free(hashes);
797 hashes = NULL;
798 out:
799 clear_rbtrees(t); /* TODO why are we doing that here? Seems odd */
800 return ret;
801 }
802
803 static void set_dummy_contents(struct osl_table_description *desc)
804 {
805 int i;
806 struct osl_column_description *cd;
807
808 for (i = 0; i < desc->num_columns; i++) {
809 cd = get_column_description(desc, i);
810 cd->compare_function = dummy_compare;
811 }
812 }
813
814 static int fsck_init(struct osl_table_description *desc, struct osl_table **t)
815 {
816 struct osl_object map;
817 int ret = map_index(desc, &map);
818
819 if (ret < 0)
820 goto out;
821 ret = read_table_desc(&map, desc);
822 if (ret < 0) {
823 osl_munmap(map.data, map.size);
824 goto out;
825 }
826 set_dummy_contents(desc);
827 ret = init_table_structure(desc, t);
828 if (ret < 0) {
829 osl_munmap(map.data, map.size);
830 goto out;
831 }
832 DEBUG_LOG("unmapping index\n");
833 osl_munmap(map.data, map.size);
834 if (conf.force_given)
835 ret = map_table(*t, (MAP_TBL_FL_IGNORE_DIRTY));
836 else
837 ret = map_table(*t, 0);
838 if (ret >= 0)
839 (*t)->num_rows = table_num_rows(*t);
840 out:
841 return ret;
842 }
843
844 static void fsck_cleanup(struct osl_table *t)
845 {
846 int i;
847
848 if (!t)
849 return;
850 if (t->desc->column_descriptions) {
851 struct osl_column_description *cd;
852 for (i = 0; i < t->desc->num_columns; i++) {
853 cd = get_column_description(t->desc, i);
854 free((char*)cd->name);
855 }
856 free(t->desc->column_descriptions);
857 }
858 free(t->columns);
859 free(t);
860
861 }
862
863 #define ST_CASE(st) case st: return #st
864
865 const char *get_asc_storage_type(enum osl_storage_type st)
866 {
867 switch (st) {
868 ST_CASE(OSL_MAPPED_STORAGE);
869 ST_CASE(OSL_DISK_STORAGE);
870 ST_CASE(OSL_NO_STORAGE);
871 }
872 return NULL;
873 }
874
875 #define APPEND_ASC_SF(sf, flag, str) do { if (sf & flag) { \
876 if (str) str = make_message("%s%s", str, " | " # flag); \
877 else str = para_strdup(#flag); }} while (0)
878
879
880 char *get_asc_storage_flags(enum osl_storage_type sf)
881 {
882 char *asc_sf = NULL;
883
884 APPEND_ASC_SF(sf, OSL_RBTREE, asc_sf);
885 APPEND_ASC_SF(sf, OSL_FIXED_SIZE, asc_sf);
886 APPEND_ASC_SF(sf, OSL_UNIQUE, asc_sf);
887 return asc_sf;
888 }
889
890 static int dump_table_desc(struct osl_table *t, int fd)
891 {
892 const struct osl_table_description *desc = t->desc;
893 int ret, i;
894 struct osl_column_description *cd;
895 char *msg = make_message("static struct osl_column_description cols[] = {\n");
896 ret = _write_all(fd, msg, strlen(msg));
897 if (ret < 0)
898 return ret;
899 free(msg);
900 FOR_EACH_COLUMN(i, desc, cd) {
901 const char *asc_st;
902 msg = make_message("\t[%d] = {\n", i);
903 ret = _write_all(fd, msg, strlen(msg));
904 if (ret < 0)
905 return ret;
906 free(msg);
907 asc_st = get_asc_storage_type(cd->storage_type);
908 msg = make_message("\t\t.storage_type = %s,\n", asc_st);
909 ret = _write_all(fd, msg, strlen(msg));
910 if (ret < 0)
911 return ret;
912 free(msg);
913 if (cd->storage_flags) {
914 char *asc_sf = get_asc_storage_flags(cd->storage_flags);
915 msg = make_message("\t\t,storage_flags = %s,\n", asc_sf);
916 free(asc_sf);
917 ret = _write_all(fd, msg, strlen(msg));
918 if (ret < 0)
919 return ret;
920 free(msg);
921 }
922 if (cd->storage_flags & OSL_FIXED_SIZE) {
923 msg = make_message("\t\t.data_size = %u,\n", cd->data_size);
924 ret = _write_all(fd, msg, strlen(msg));
925 if (ret < 0)
926 return ret;
927 free(msg);
928 }
929 msg = make_message("\t\t.name = \"%s\",\n", cd->name);
930 ret = _write_all(fd, msg, strlen(msg));
931 if (ret < 0)
932 return ret;
933 free(msg);
934 if (cd->storage_flags & OSL_RBTREE) {
935 msg = make_message("\t\t.compare_function = compare_func,\n");
936 ret = _write_all(fd, msg, strlen(msg));
937 if (ret < 0)
938 return ret;
939 free(msg);
940 }
941 msg = make_message("\t},\n");
942 ret = _write_all(fd, msg, strlen(msg));
943 if (ret < 0)
944 return ret;
945 free(msg);
946 }
947 msg = make_message("};\n");
948 ret = _write_all(fd, msg, strlen(msg));
949 if (ret < 0)
950 return ret;
951 free(msg);
952 return 1;
953 }
954
955 static int dump_row(struct osl_table *t, unsigned row_num, const char *row_dir)
956 {
957 int ret, i;
958 const struct osl_column_description *cd;
959 unsigned dsnc;
960 struct osl_object obj;
961 char *ds_name;
962 HASH_TYPE hash[HASH_SIZE];
963 char *filename;
964
965 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
966 ret = get_mapped_object(t, i, row_num, &obj);
967 if (ret < 0)
968 return ret;
969 filename = make_message("%s/col_%03u", row_dir, i);
970 ret = write_file(filename, obj.data, obj.size);
971 free(filename);
972 if (ret < 0)
973 return ret;
974 }
975 if (!t->num_disk_storage_columns)
976 return 1;
977 dsnc = t->disk_storage_name_column;
978 ret = get_mapped_object(t, dsnc, row_num, &obj);
979 if (ret < 0)
980 return ret;
981 hash_object(&obj, hash);
982 ds_name = disk_storage_name_of_hash(t, hash);
983 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
984 filename = disk_storage_path(t, i, ds_name);
985 ret = mmap_full_file(filename, O_RDONLY, &obj.data, &obj.size, NULL);
986 free(filename);
987 if (ret < 0)
988 goto out;
989 filename = make_message("%s/col_%03u", row_dir, i);
990 ret = write_file(filename, obj.data, obj.size);
991 free(filename);
992 if (ret < 0)
993 goto out;
994 }
995 ret = 1;
996 out:
997 free(ds_name);
998 return ret;
999 }
1000
1001 static int dump_rows(char *dump_dir, struct osl_table *t)
1002 {
1003 unsigned i;
1004 char *current_dir = NULL;
1005 int ret = 0;
1006
1007 for (i = 0; i < t->num_rows; i++) {
1008 char *row_dir;
1009 if (row_is_invalid(t, i))
1010 continue;
1011 if (!(i % 1000)) {
1012 free(current_dir);
1013 current_dir = make_message("%s/rows_%u-%u", dump_dir, i, i + 999);
1014 NOTICE_LOG("dumping rows %u - %u\n", i, i + 999);
1015 ret = osl_mkdir(current_dir, 0777);
1016 if (ret < 0 && !is_errno(-ret, EEXIST))
1017 goto out;
1018 }
1019 row_dir = make_message("%s/row_%03u", current_dir, i);
1020 ret = osl_mkdir(row_dir, 0777);
1021 if (ret < 0 && !is_errno(-ret, EEXIST)) {
1022 free(row_dir);
1023 goto out;
1024 }
1025 ret = dump_row(t, i, row_dir);
1026 free(row_dir);
1027 if (ret < 0)
1028 goto out;
1029 }
1030 out:
1031 free(current_dir);
1032 return ret;
1033 }
1034
1035 static int dump_table(char *dump_dir, struct osl_table_description *desc)
1036 {
1037 struct osl_table *t = NULL;
1038 int fd, ret = fsck_init(desc, &t);
1039 char *desc_file;
1040 char *table_dump_dir = NULL;
1041
1042 if (ret < 0)
1043 goto out;
1044 ret = osl_mkdir(dump_dir, 0777);
1045 if (ret < 0 && !is_errno(-ret, EEXIST))
1046 goto out;
1047 table_dump_dir = make_message("%s/%s", dump_dir, desc->name);
1048 ret = osl_mkdir(table_dump_dir, 0777);
1049 if (ret < 0 && !is_errno(-ret, EEXIST))
1050 goto out;
1051 desc_file = make_message("%s/table_description.c", table_dump_dir);
1052 ret = osl_open(desc_file, O_WRONLY | O_CREAT | O_EXCL, 0644);
1053 free(desc_file);
1054 if (ret < 0)
1055 goto out;
1056 fd = ret;
1057 ret = dump_table_desc(t, fd);
1058 close(fd);
1059 if (ret < 0)
1060 goto out;
1061 ret = dump_rows(table_dump_dir, t);
1062 out:
1063 free(table_dump_dir);
1064 fsck_cleanup(t);
1065 return ret;
1066 }
1067
1068 static int fsck(struct osl_table_description *desc)
1069 {
1070 int ret;
1071 struct osl_table *t = NULL;
1072 uint32_t *lost_bytes = NULL;
1073
1074 ret = fsck_init(desc, &t);
1075 if (ret < 0)
1076 goto out;
1077 ret = check_index_ranges(t);
1078 if (ret < 0)
1079 goto out_unmap;
1080 ret = check_disk_storage_columns(t);
1081 if (ret < 0)
1082 goto out_unmap;
1083 ret = prune_invalid_rows_from_index(t);
1084 if (ret < 0)
1085 goto out_unmap;
1086 ret = check_for_invalid_objects(t, &lost_bytes);
1087 if (ret < 0)
1088 goto out_unmap;
1089 if (ret > 0) { /* at least one mapped data file needs pruning */
1090 ret = prune_objects(t, lost_bytes);
1091 if (ret < 0)
1092 goto out_unmap;
1093 }
1094 free(lost_bytes);
1095 out_unmap:
1096 unmap_table(t, OSL_MARK_CLEAN);
1097 out:
1098 fsck_cleanup(t);
1099 return ret;
1100 }
1101
1102 static int check_table(char *base_dir, char *table_name)
1103 {
1104 struct osl_table_description desc = {
1105 .column_descriptions = NULL,
1106 .dir = base_dir,
1107 .name = table_name
1108 };
1109 int ret;
1110
1111 INFO_LOG("checking table %s\n", table_name);
1112 if (!conf.no_fsck_given) {
1113 ret = fsck(&desc);
1114 if (ret < 0)
1115 goto out;
1116 }
1117 ret = 1;
1118 if (!conf.dump_dir_given || !*conf.dump_dir_arg)
1119 goto out;
1120 ret = dump_table(conf.dump_dir_arg, &desc);
1121 out:
1122 if (ret < 0)
1123 ERROR_LOG("failed to check table %s\n", table_name);
1124 else
1125 NOTICE_LOG("successfully checked table %s\n", table_name);
1126 return ret;
1127 }
1128
1129 static int check_all_tables(char *base_dir)
1130 {
1131 DIR *dir;
1132 struct dirent *entry;
1133 int cwd_fd, ret2, ret = para_opendir(base_dir, &dir, &cwd_fd);
1134
1135 if (ret < 0)
1136 return ret;
1137 while ((entry = readdir(dir))) {
1138 mode_t m;
1139 struct stat s;
1140 if (!strcmp(entry->d_name, "."))
1141 continue;
1142 if (!strcmp(entry->d_name, ".."))
1143 continue;
1144 if (lstat(entry->d_name, &s) == -1)
1145 continue;
1146 m = s.st_mode;
1147 if (!S_ISDIR(m))
1148 continue;
1149 ret = check_table(base_dir, entry->d_name);
1150 if (ret < 0)
1151 break;
1152 }
1153 closedir(dir);
1154 ret2 = para_fchdir(cwd_fd);
1155 if (ret2 < 0 && ret >= 0)
1156 ret = ret2;
1157 close(cwd_fd);
1158 return ret;
1159 }
1160
1161 int main(int argc, char **argv)
1162 {
1163 int i, ret;
1164 char *base_dir = NULL;
1165
1166 ret = fsck_cmdline_parser(argc, argv, &conf);
1167 if (ret < 0) {
1168 ret = -E_FSCK_SYNTAX;
1169 goto out;
1170 }
1171 HANDLE_VERSION_FLAG("fsck", conf);
1172 if (conf.base_dir_given)
1173 base_dir = para_strdup(conf.base_dir_arg);
1174 else {
1175 char *home = para_homedir();
1176 base_dir = make_message("%s/.paraslash/afs_database", home);
1177 free(home);
1178 }
1179 if (!conf.inputs_num) {
1180 ret = check_all_tables(base_dir);
1181 goto out;
1182 }
1183 for (i = 0; i < conf.inputs_num; i++) {
1184 ret = check_table(base_dir, conf.inputs[i]);
1185 if (ret < 0)
1186 break;
1187 }
1188 out:
1189 if (ret < 0) {
1190 ERROR_LOG("%s%s: %s\n",
1191 base_dir? "base_dir: " : "",
1192 base_dir? base_dir : "",
1193 fsck_strerror(-ret)
1194 );
1195 if (conf.loglevel_arg > 1)
1196 EMERG_LOG("re-run with \"--loglevel %d\" to increase verbosity\n",
1197 conf.loglevel_arg - 1);
1198 } else
1199 NOTICE_LOG("success\n");
1200 if (base_dir)
1201 free(base_dir);
1202 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
1203 }