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