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