]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/fsck.c
delpart: minor man page improvements
[thirdparty/util-linux.git] / disk-utils / fsck.c
1 /*
2 * fsck --- A generic, parallelizing front-end for the fsck program.
3 * It will automatically try to run fsck programs in parallel if the
4 * devices are on separate spindles. It is based on the same ideas as
5 * the generic front end for fsck by David Engel and Fred van Kempen,
6 * but it has been completely rewritten from scratch to support
7 * parallel execution.
8 *
9 * Written by Theodore Ts'o, <tytso@mit.edu>
10 *
11 * Miquel van Smoorenburg (miquels@drinkel.ow.org) 20-Oct-1994:
12 * o Changed -t fstype to behave like with mount when -A (all file
13 * systems) or -M (like mount) is specified.
14 * o fsck looks if it can find the fsck.type program to decide
15 * if it should ignore the fs type. This way more fsck programs
16 * can be added without changing this front-end.
17 * o -R flag skip root file system.
18 *
19 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
20 * 2001, 2002, 2003, 2004, 2005 by Theodore Ts'o.
21 *
22 * Copyright (C) 2009, 2012 Karel Zak <kzak@redhat.com>
23 *
24 * This file may be redistributed under the terms of the GNU Public
25 * License.
26 */
27
28 #define _XOPEN_SOURCE 600 /* for inclusion of sa_handler in Solaris */
29
30 #include <sys/types.h>
31 #include <sys/wait.h>
32 #include <sys/stat.h>
33 #include <sys/file.h>
34 #include <fcntl.h>
35 #include <limits.h>
36 #include <stdio.h>
37 #include <ctype.h>
38 #include <string.h>
39 #include <time.h>
40 #include <stdlib.h>
41 #include <paths.h>
42 #include <unistd.h>
43 #include <errno.h>
44 #include <signal.h>
45 #include <dirent.h>
46 #include <sys/resource.h>
47 #include <sys/time.h>
48 #include <blkid.h>
49 #include <libmount.h>
50
51 #include "nls.h"
52 #include "pathnames.h"
53 #include "exitcodes.h"
54 #include "c.h"
55 #include "closestream.h"
56
57 #define XALLOC_EXIT_CODE FSCK_EX_ERROR
58 #include "xalloc.h"
59
60 #ifndef DEFAULT_FSTYPE
61 # define DEFAULT_FSTYPE "ext2"
62 #endif
63
64 #define MAX_DEVICES 32
65 #define MAX_ARGS 32
66
67 static const char *ignored_types[] = {
68 "ignore",
69 "iso9660",
70 "sw",
71 NULL
72 };
73
74 static const char *really_wanted[] = {
75 "minix",
76 "ext2",
77 "ext3",
78 "ext4",
79 "ext4dev",
80 "jfs",
81 "reiserfs",
82 "xiafs",
83 "xfs",
84 NULL
85 };
86
87 /*
88 * Internal structure for mount tabel entries.
89 */
90 struct fsck_fs_data
91 {
92 const char *device;
93 dev_t disk;
94 unsigned int stacked:1,
95 done:1,
96 eval_device:1;
97 };
98
99 /*
100 * Structure to allow exit codes to be stored
101 */
102 struct fsck_instance {
103 int pid;
104 int flags; /* FLAG_{DONE|PROGRESS} */
105 int lock; /* flock()ed whole disk file descriptor or -1 */
106 int exit_status;
107 struct timeval start_time;
108 struct timeval end_time;
109 char * prog;
110 char * type;
111
112 struct rusage rusage;
113 struct libmnt_fs *fs;
114 struct fsck_instance *next;
115 };
116
117 #define FLAG_DONE 1
118 #define FLAG_PROGRESS 2
119
120 /*
121 * Global variables for options
122 */
123 static char *devices[MAX_DEVICES];
124 static char *args[MAX_ARGS];
125 static int num_devices, num_args;
126
127 static int lockdisk;
128 static int verbose;
129 static int doall;
130 static int noexecute;
131 static int serialize;
132 static int skip_root;
133 static int ignore_mounted;
134 static int notitle;
135 static int parallel_root;
136 static int progress;
137 static int progress_fd;
138 static int force_all_parallel;
139 static int report_stats;
140
141 static int num_running;
142 static int max_running;
143
144 static volatile int cancel_requested;
145 static int kill_sent;
146 static char *fstype;
147 static struct fsck_instance *instance_list;
148
149 static const char fsck_prefix_path[] = FS_SEARCH_PATH;
150 static char *fsck_path;
151
152 /* parsed fstab and mtab */
153 static struct libmnt_table *fstab, *mtab;
154 static struct libmnt_cache *mntcache;
155
156 static int count_slaves(dev_t disk);
157
158 static int string_to_int(const char *s)
159 {
160 long l;
161 char *p;
162
163 l = strtol(s, &p, 0);
164 if (*p || l == LONG_MIN || l == LONG_MAX || l < 0 || l > INT_MAX)
165 return -1;
166 else
167 return (int) l;
168 }
169
170 static int is_mounted(struct libmnt_fs *fs)
171 {
172 int rc;
173 const char *src;
174
175 src = mnt_fs_get_source(fs);
176 if (!src)
177 return 0;
178 if (!mntcache)
179 mntcache = mnt_new_cache();
180 if (!mtab) {
181 mtab = mnt_new_table();
182 if (!mtab)
183 err(FSCK_EX_ERROR, ("failed to initialize libmount table"));
184 mnt_table_set_cache(mtab, mntcache);
185 mnt_table_parse_mtab(mtab, NULL);
186 }
187
188 rc = mnt_table_find_source(mtab, src, MNT_ITER_BACKWARD) ? 1 : 0;
189 if (verbose) {
190 if (rc)
191 printf(_("%s is mounted\n"), src);
192 else
193 printf(_("%s is not mounted\n"), src);
194 }
195 return rc;
196 }
197
198 static int ignore(struct libmnt_fs *);
199
200 static struct fsck_fs_data *fs_create_data(struct libmnt_fs *fs)
201 {
202 struct fsck_fs_data *data = mnt_fs_get_userdata(fs);
203
204 if (!data) {
205 data = xcalloc(1, sizeof(*data));
206 mnt_fs_set_userdata(fs, data);
207 }
208 return data;
209 }
210
211 /*
212 * fs from fstab might contains real device name as well as symlink,
213 * LABEL or UUID, this function returns canonicalized result.
214 */
215 static const char *fs_get_device(struct libmnt_fs *fs)
216 {
217 struct fsck_fs_data *data = mnt_fs_get_userdata(fs);
218
219 if (!data || !data->eval_device) {
220 const char *spec = mnt_fs_get_source(fs);
221
222 if (!data)
223 data = fs_create_data(fs);
224
225 data->eval_device = 1;
226 data->device = mnt_resolve_spec(spec, mnt_table_get_cache(fstab));
227 if (!data->device)
228 data->device = xstrdup(spec);
229 }
230
231 return data->device;
232 }
233
234 static dev_t fs_get_disk(struct libmnt_fs *fs, int check)
235 {
236 struct fsck_fs_data *data;
237 const char *device;
238 struct stat st;
239
240 data = mnt_fs_get_userdata(fs);
241 if (data && data->disk)
242 return data->disk;
243
244 if (!check)
245 return 0;
246
247 if (mnt_fs_is_netfs(fs) || mnt_fs_is_pseudofs(fs))
248 return 0;
249
250 device = fs_get_device(fs);
251 if (!device)
252 return 0;
253
254 data = fs_create_data(fs);
255
256 if (!stat(device, &st) &&
257 !blkid_devno_to_wholedisk(st.st_rdev, NULL, 0, &data->disk)) {
258
259 if (data->disk)
260 data->stacked = count_slaves(data->disk) > 0 ? 1 : 0;
261 return data->disk;
262 }
263 return 0;
264 }
265
266 static int fs_is_stacked(struct libmnt_fs *fs)
267 {
268 struct fsck_fs_data *data = mnt_fs_get_userdata(fs);
269 return data ? data->stacked : 0;
270 }
271
272 static int fs_is_done(struct libmnt_fs *fs)
273 {
274 struct fsck_fs_data *data = mnt_fs_get_userdata(fs);
275 return data ? data->done : 0;
276 }
277
278 static void fs_set_done(struct libmnt_fs *fs)
279 {
280 struct fsck_fs_data *data = fs_create_data(fs);
281
282 if (data)
283 data->done = 1;
284 }
285
286 static int is_irrotational_disk(dev_t disk)
287 {
288 char path[PATH_MAX];
289 FILE *f;
290 int rc, x;
291
292
293 rc = snprintf(path, sizeof(path),
294 "/sys/dev/block/%d:%d/queue/rotational",
295 major(disk), minor(disk));
296
297 if (rc < 0 || (unsigned int) (rc + 1) > sizeof(path))
298 return 0;
299
300 f = fopen(path, "r");
301 if (!f)
302 return 0;
303
304 rc = fscanf(f, "%d", &x);
305 if (rc != 1) {
306 if (ferror(f))
307 warn(_("cannot read %s"), path);
308 else
309 warnx(_("parse error: %s"), path);
310 }
311 fclose(f);
312
313 return rc == 1 ? !x : 0;
314 }
315
316 static void lock_disk(struct fsck_instance *inst)
317 {
318 dev_t disk = fs_get_disk(inst->fs, 1);
319 char *diskname;
320
321 if (!disk || is_irrotational_disk(disk))
322 return;
323
324 diskname = blkid_devno_to_devname(disk);
325 if (!diskname)
326 return;
327
328 if (verbose)
329 printf(_("Locking disk %s ... "), diskname);
330
331 inst->lock = open(diskname, O_CLOEXEC | O_RDONLY);
332 if (inst->lock >= 0) {
333 int rc = -1;
334
335 /* inform users that we're waiting on the lock */
336 if (verbose &&
337 (rc = flock(inst->lock, LOCK_EX | LOCK_NB)) != 0 &&
338 errno == EWOULDBLOCK)
339 printf(_("(waiting) "));
340
341 if (rc != 0 && flock(inst->lock, LOCK_EX) != 0) {
342 close(inst->lock); /* failed */
343 inst->lock = -1;
344 }
345 }
346
347 if (verbose)
348 /* TRANSLATORS: These are followups to "Locking disk...". */
349 printf("%s.\n", inst->lock >= 0 ? _("succeeded") : _("failed"));
350
351 free(diskname);
352 return;
353 }
354
355 static void unlock_disk(struct fsck_instance *inst)
356 {
357 if (inst->lock >= 0) {
358 /* explicitly unlock, don't rely on close(), maybe some library
359 * (e.g. liblkid) has still open the device.
360 */
361 flock(inst->lock, LOCK_UN);
362 close(inst->lock);
363
364 inst->lock = -1;
365 }
366 }
367
368 static void free_instance(struct fsck_instance *i)
369 {
370 if (lockdisk)
371 unlock_disk(i);
372 free(i->prog);
373 mnt_unref_fs(i->fs);
374 free(i);
375 return;
376 }
377
378 static struct libmnt_fs *add_dummy_fs(const char *device)
379 {
380 struct libmnt_fs *fs = mnt_new_fs();
381
382 if (fs && mnt_fs_set_source(fs, device) == 0 &&
383 mnt_table_add_fs(fstab, fs) == 0) {
384 mnt_unref_fs(fs);
385 return fs;
386 }
387
388 mnt_unref_fs(fs);
389 err(FSCK_EX_ERROR, _("failed to setup description for %s"), device);
390 }
391
392 static void fs_interpret_type(struct libmnt_fs *fs)
393 {
394 const char *device;
395 const char *type = mnt_fs_get_fstype(fs);
396
397 if (type && strcmp(type, "auto") != 0)
398 return;
399
400 mnt_fs_set_fstype(fs, NULL);
401
402 device = fs_get_device(fs);
403 if (device) {
404 int ambi = 0;
405
406 type = mnt_get_fstype(device, &ambi, mnt_table_get_cache(fstab));
407 if (!ambi)
408 mnt_fs_set_fstype(fs, type);
409 }
410 }
411
412 static int parser_errcb(struct libmnt_table *tb __attribute__ ((__unused__)),
413 const char *filename, int line)
414 {
415 warnx(_("%s: parse error at line %d -- ignore"), filename, line);
416 return 0;
417 }
418
419 /*
420 * Load the filesystem database from /etc/fstab
421 */
422 static void load_fs_info(void)
423 {
424 const char *path;
425
426 fstab = mnt_new_table();
427 if (!fstab)
428 err(FSCK_EX_ERROR, ("failed to initialize libmount table"));
429
430 mnt_table_set_parser_errcb(fstab, parser_errcb);
431 mnt_table_set_cache(fstab, mntcache);
432
433 errno = 0;
434
435 /*
436 * Let's follow libmount defauls if $FSTAB_FILE is not specified
437 */
438 path = getenv("FSTAB_FILE");
439
440 if (mnt_table_parse_fstab(fstab, path)) {
441 if (!path)
442 path = mnt_get_fstab_path();
443
444 /* don't print error when there is no fstab at all */
445 if (access(path, F_OK) == 0) {
446 if (errno)
447 warn(_("%s: failed to parse fstab"), path);
448 else
449 warnx(_("%s: failed to parse fstab"), path);
450 }
451 }
452 }
453
454 /*
455 * Lookup filesys in /etc/fstab and return the corresponding entry.
456 * The @path has to be real path (no TAG) by mnt_resolve_spec().
457 */
458 static struct libmnt_fs *lookup(char *path)
459 {
460 struct libmnt_fs *fs;
461
462 if (!path)
463 return NULL;
464
465 fs = mnt_table_find_srcpath(fstab, path, MNT_ITER_FORWARD);
466 if (!fs) {
467 /*
468 * Maybe mountpoint has been specified on fsck command line.
469 * Yeah, crazy feature...
470 *
471 * Note that mnt_table_find_target() may canonicalize paths in
472 * the fstab to support symlinks. This is really unwanted,
473 * because readlink() on mountpoints may trigger automounts.
474 *
475 * So, disable the cache and compare the paths as strings
476 * without care about symlinks...
477 */
478 mnt_table_set_cache(fstab, NULL);
479 fs = mnt_table_find_target(fstab, path, MNT_ITER_FORWARD);
480 mnt_table_set_cache(fstab, mntcache);
481 }
482 return fs;
483 }
484
485 /* Find fsck program for a given fs type. */
486 static char *find_fsck(const char *type)
487 {
488 char *s;
489 const char *tpl;
490 static char prog[256];
491 char *p = xstrdup(fsck_path);
492 struct stat st;
493
494 /* Are we looking for a program or just a type? */
495 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
496
497 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
498 sprintf(prog, tpl, s, type);
499 if (stat(prog, &st) == 0)
500 break;
501 }
502 free(p);
503
504 return(s ? prog : NULL);
505 }
506
507 static int progress_active(void)
508 {
509 struct fsck_instance *inst;
510
511 for (inst = instance_list; inst; inst = inst->next) {
512 if (inst->flags & FLAG_DONE)
513 continue;
514 if (inst->flags & FLAG_PROGRESS)
515 return 1;
516 }
517 return 0;
518 }
519
520 /*
521 * Process run statistics for finished fsck instances.
522 *
523 * If report_stats is 0, do nothing, otherwise print a selection of
524 * interesting rusage statistics as well as elapsed wallclock time.
525 */
526 static void print_stats(struct fsck_instance *inst)
527 {
528 double time_diff;
529
530 if (!inst || !report_stats || noexecute)
531 return;
532
533 time_diff = (inst->end_time.tv_sec - inst->start_time.tv_sec)
534 + (inst->end_time.tv_usec - inst->start_time.tv_usec) / 1E6;
535
536 fprintf(stdout, "%s: status %d, rss %ld, "
537 "real %f, user %d.%06d, sys %d.%06d\n",
538 fs_get_device(inst->fs),
539 inst->exit_status,
540 inst->rusage.ru_maxrss,
541 time_diff,
542 (int)inst->rusage.ru_utime.tv_sec,
543 (int)inst->rusage.ru_utime.tv_usec,
544 (int)inst->rusage.ru_stime.tv_sec,
545 (int)inst->rusage.ru_stime.tv_usec);
546 }
547
548 /*
549 * Execute a particular fsck program, and link it into the list of
550 * child processes we are waiting for.
551 */
552 static int execute(const char *type, struct libmnt_fs *fs, int interactive)
553 {
554 char *s, *argv[80], prog[80];
555 int argc, i;
556 struct fsck_instance *inst, *p;
557 pid_t pid;
558
559 inst = xcalloc(1, sizeof(*inst));
560
561 sprintf(prog, "fsck.%s", type);
562 argv[0] = xstrdup(prog);
563 argc = 1;
564
565 for (i=0; i <num_args; i++)
566 argv[argc++] = xstrdup(args[i]);
567
568 if (progress) {
569 if ((strcmp(type, "ext2") == 0) ||
570 (strcmp(type, "ext3") == 0) ||
571 (strcmp(type, "ext4") == 0) ||
572 (strcmp(type, "ext4dev") == 0)) {
573 char tmp[80];
574
575 tmp[0] = 0;
576 if (!progress_active()) {
577 snprintf(tmp, 80, "-C%d", progress_fd);
578 inst->flags |= FLAG_PROGRESS;
579 } else if (progress_fd)
580 snprintf(tmp, 80, "-C%d", progress_fd * -1);
581 if (tmp[0])
582 argv[argc++] = xstrdup(tmp);
583 }
584 }
585
586 argv[argc++] = xstrdup(fs_get_device(fs));
587 argv[argc] = 0;
588
589 s = find_fsck(prog);
590 if (s == NULL) {
591 warnx(_("%s: not found"), prog);
592 free_instance(inst);
593 return ENOENT;
594 }
595
596 if (verbose || noexecute) {
597 const char *tgt = mnt_fs_get_target(fs);
598
599 if (!tgt)
600 tgt = fs_get_device(fs);
601 printf("[%s (%d) -- %s] ", s, num_running, tgt);
602 for (i=0; i < argc; i++)
603 printf("%s ", argv[i]);
604 printf("\n");
605 }
606
607 mnt_ref_fs(fs);
608 inst->fs = fs;
609 inst->lock = -1;
610
611 if (lockdisk)
612 lock_disk(inst);
613
614 /* Fork and execute the correct program. */
615 if (noexecute)
616 pid = -1;
617 else if ((pid = fork()) < 0) {
618 warn(_("fork failed"));
619 free_instance(inst);
620 return errno;
621 } else if (pid == 0) {
622 if (!interactive)
623 close(0);
624 execv(s, argv);
625 err(FSCK_EX_ERROR, _("%s: execute failed"), s);
626 }
627
628 for (i=0; i < argc; i++)
629 free(argv[i]);
630
631 inst->pid = pid;
632 inst->prog = xstrdup(prog);
633 inst->type = xstrdup(type);
634 gettimeofday(&inst->start_time, NULL);
635 inst->next = NULL;
636
637 /*
638 * Find the end of the list, so we add the instance on at the end.
639 */
640 for (p = instance_list; p && p->next; p = p->next);
641
642 if (p)
643 p->next = inst;
644 else
645 instance_list = inst;
646
647 return 0;
648 }
649
650 /*
651 * Send a signal to all outstanding fsck child processes
652 */
653 static int kill_all(int signum)
654 {
655 struct fsck_instance *inst;
656 int n = 0;
657
658 for (inst = instance_list; inst; inst = inst->next) {
659 if (inst->flags & FLAG_DONE)
660 continue;
661 kill(inst->pid, signum);
662 n++;
663 }
664 return n;
665 }
666
667 /*
668 * Wait for one child process to exit; when it does, unlink it from
669 * the list of executing child processes, and return it.
670 */
671 static struct fsck_instance *wait_one(int flags)
672 {
673 int status;
674 int sig;
675 struct fsck_instance *inst, *inst2, *prev;
676 pid_t pid;
677 struct rusage rusage;
678
679 if (!instance_list)
680 return NULL;
681
682 if (noexecute) {
683 inst = instance_list;
684 prev = 0;
685 #ifdef RANDOM_DEBUG
686 while (inst->next && (random() & 1)) {
687 prev = inst;
688 inst = inst->next;
689 }
690 #endif
691 inst->exit_status = 0;
692 goto ret_inst;
693 }
694
695 /*
696 * gcc -Wall fails saving throw against stupidity
697 * (inst and prev are thought to be uninitialized variables)
698 */
699 inst = prev = NULL;
700
701 do {
702 pid = wait4(-1, &status, flags, &rusage);
703 if (cancel_requested && !kill_sent) {
704 kill_all(SIGTERM);
705 kill_sent++;
706 }
707 if ((pid == 0) && (flags & WNOHANG))
708 return NULL;
709 if (pid < 0) {
710 if ((errno == EINTR) || (errno == EAGAIN))
711 continue;
712 if (errno == ECHILD) {
713 warnx(_("wait: no more child process?!?"));
714 return NULL;
715 }
716 warn(_("waitpid failed"));
717 continue;
718 }
719 for (prev = 0, inst = instance_list;
720 inst;
721 prev = inst, inst = inst->next) {
722 if (inst->pid == pid)
723 break;
724 }
725 } while (!inst);
726
727 if (WIFEXITED(status))
728 status = WEXITSTATUS(status);
729 else if (WIFSIGNALED(status)) {
730 sig = WTERMSIG(status);
731 if (sig == SIGINT) {
732 status = FSCK_EX_UNCORRECTED;
733 } else {
734 warnx(_("Warning... %s for device %s exited "
735 "with signal %d."),
736 inst->prog, fs_get_device(inst->fs), sig);
737 status = FSCK_EX_ERROR;
738 }
739 } else {
740 warnx(_("%s %s: status is %x, should never happen."),
741 inst->prog, fs_get_device(inst->fs), status);
742 status = FSCK_EX_ERROR;
743 }
744
745 inst->exit_status = status;
746 inst->flags |= FLAG_DONE;
747 gettimeofday(&inst->end_time, NULL);
748 memcpy(&inst->rusage, &rusage, sizeof(struct rusage));
749
750 if (progress && (inst->flags & FLAG_PROGRESS) &&
751 !progress_active()) {
752 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
753 if (inst2->flags & FLAG_DONE)
754 continue;
755 if (strcmp(inst2->type, "ext2") &&
756 strcmp(inst2->type, "ext3") &&
757 strcmp(inst2->type, "ext4") &&
758 strcmp(inst2->type, "ext4dev"))
759 continue;
760 /*
761 * If we've just started the fsck, wait a tiny
762 * bit before sending the kill, to give it
763 * time to set up the signal handler
764 */
765 if (inst2->start_time.tv_sec < time(0) + 2) {
766 if (fork() == 0) {
767 sleep(1);
768 kill(inst2->pid, SIGUSR1);
769 exit(FSCK_EX_OK);
770 }
771 } else
772 kill(inst2->pid, SIGUSR1);
773 inst2->flags |= FLAG_PROGRESS;
774 break;
775 }
776 }
777 ret_inst:
778 if (prev)
779 prev->next = inst->next;
780 else
781 instance_list = inst->next;
782
783 print_stats(inst);
784
785 if (verbose > 1)
786 printf(_("Finished with %s (exit status %d)\n"),
787 fs_get_device(inst->fs), inst->exit_status);
788 num_running--;
789 return inst;
790 }
791
792 #define FLAG_WAIT_ALL 0
793 #define FLAG_WAIT_ATLEAST_ONE 1
794 /*
795 * Wait until all executing child processes have exited; return the
796 * logical OR of all of their exit code values.
797 */
798 static int wait_many(int flags)
799 {
800 struct fsck_instance *inst;
801 int global_status = 0;
802 int wait_flags = 0;
803
804 while ((inst = wait_one(wait_flags))) {
805 global_status |= inst->exit_status;
806 free_instance(inst);
807 #ifdef RANDOM_DEBUG
808 if (noexecute && (flags & WNOHANG) && !(random() % 3))
809 break;
810 #endif
811 if (flags & FLAG_WAIT_ATLEAST_ONE)
812 wait_flags = WNOHANG;
813 }
814 return global_status;
815 }
816
817 /*
818 * Run the fsck program on a particular device
819 *
820 * If the type is specified using -t, and it isn't prefixed with "no"
821 * (as in "noext2") and only one filesystem type is specified, then
822 * use that type regardless of what is specified in /etc/fstab.
823 *
824 * If the type isn't specified by the user, then use either the type
825 * specified in /etc/fstab, or DEFAULT_FSTYPE.
826 */
827 static int fsck_device(struct libmnt_fs *fs, int interactive)
828 {
829 const char *type;
830 int retval;
831
832 fs_interpret_type(fs);
833
834 type = mnt_fs_get_fstype(fs);
835
836 if (type && strcmp(type, "auto") != 0)
837 ;
838 else if (fstype && strncmp(fstype, "no", 2) &&
839 strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
840 !strchr(fstype, ','))
841 type = fstype;
842 else
843 type = DEFAULT_FSTYPE;
844
845 num_running++;
846 retval = execute(type, fs, interactive);
847 if (retval) {
848 warnx(_("error %d while executing fsck.%s for %s"),
849 retval, type, fs_get_device(fs));
850 num_running--;
851 return FSCK_EX_ERROR;
852 }
853 return 0;
854 }
855
856
857 /*
858 * Deal with the fsck -t argument.
859 */
860 struct fs_type_compile {
861 char **list;
862 int *type;
863 int negate;
864 } fs_type_compiled;
865
866 #define FS_TYPE_NORMAL 0
867 #define FS_TYPE_OPT 1
868 #define FS_TYPE_NEGOPT 2
869
870 static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
871 {
872 char *cp, *list, *s;
873 int num = 2;
874 int negate, first_negate = 1;
875
876 if (fs_type) {
877 for (cp=fs_type; *cp; cp++) {
878 if (*cp == ',')
879 num++;
880 }
881 }
882
883 cmp->list = xcalloc(num, sizeof(char *));
884 cmp->type = xcalloc(num, sizeof(int));
885 cmp->negate = 0;
886
887 if (!fs_type)
888 return;
889
890 list = xstrdup(fs_type);
891 num = 0;
892 s = strtok(list, ",");
893 while(s) {
894 negate = 0;
895 if (strncmp(s, "no", 2) == 0) {
896 s += 2;
897 negate = 1;
898 } else if (*s == '!') {
899 s++;
900 negate = 1;
901 }
902 if (strcmp(s, "loop") == 0)
903 /* loop is really short-hand for opts=loop */
904 goto loop_special_case;
905 else if (strncmp(s, "opts=", 5) == 0) {
906 s += 5;
907 loop_special_case:
908 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
909 } else {
910 if (first_negate) {
911 cmp->negate = negate;
912 first_negate = 0;
913 }
914 if ((negate && !cmp->negate) ||
915 (!negate && cmp->negate)) {
916 errx(FSCK_EX_USAGE,
917 _("Either all or none of the filesystem types passed to -t must be prefixed\n"
918 "with 'no' or '!'."));
919 }
920 }
921
922 cmp->list[num++] = xstrdup(s);
923 s = strtok(NULL, ",");
924 }
925 free(list);
926 }
927
928 /*
929 * This function returns true if a particular option appears in a
930 * comma-delimited options list
931 */
932 static int opt_in_list(const char *opt, const char *optlist)
933 {
934 char *list, *s;
935
936 if (!optlist)
937 return 0;
938 list = xstrdup(optlist);
939
940 s = strtok(list, ",");
941 while(s) {
942 if (strcmp(s, opt) == 0) {
943 free(list);
944 return 1;
945 }
946 s = strtok(NULL, ",");
947 }
948 free(list);
949 return 0;
950 }
951
952 /* See if the filesystem matches the criteria given by the -t option */
953 static int fs_match(struct libmnt_fs *fs, struct fs_type_compile *cmp)
954 {
955 int n, ret = 0, checked_type = 0;
956 char *cp;
957
958 if (cmp->list == 0 || cmp->list[0] == 0)
959 return 1;
960
961 for (n=0; (cp = cmp->list[n]); n++) {
962 switch (cmp->type[n]) {
963 case FS_TYPE_NORMAL:
964 {
965 const char *type = mnt_fs_get_fstype(fs);
966
967 checked_type++;
968 if (type && strcmp(cp, type) == 0)
969 ret = 1;
970 break;
971 }
972 case FS_TYPE_NEGOPT:
973 if (opt_in_list(cp, mnt_fs_get_options(fs)))
974 return 0;
975 break;
976 case FS_TYPE_OPT:
977 if (!opt_in_list(cp, mnt_fs_get_options(fs)))
978 return 0;
979 break;
980 }
981 }
982 if (checked_type == 0)
983 return 1;
984 return (cmp->negate ? !ret : ret);
985 }
986
987 /*
988 * Check if a device exists
989 */
990 static int device_exists(const char *device)
991 {
992 struct stat st;
993
994 if (stat(device, &st) == -1)
995 return 0;
996 if (!S_ISBLK(st.st_mode))
997 return 0;
998 return 1;
999 }
1000
1001 static int fs_ignored_type(struct libmnt_fs *fs)
1002 {
1003 const char **ip, *type;
1004
1005 if (mnt_fs_is_netfs(fs) || mnt_fs_is_pseudofs(fs) || mnt_fs_is_swaparea(fs))
1006 return 1;
1007
1008 type = mnt_fs_get_fstype(fs);
1009
1010 for(ip = ignored_types; type && *ip; ip++) {
1011 if (strcmp(type, *ip) == 0)
1012 return 1;
1013 }
1014
1015 return 0;
1016 }
1017
1018 /* Check if we should ignore this filesystem. */
1019 static int ignore(struct libmnt_fs *fs)
1020 {
1021 const char **ip, *type;
1022 int wanted = 0;
1023
1024 /*
1025 * If the pass number is 0, ignore it.
1026 */
1027 if (mnt_fs_get_passno(fs) == 0)
1028 return 1;
1029
1030 /*
1031 * If this is a bind mount, ignore it.
1032 */
1033 if (opt_in_list("bind", mnt_fs_get_options(fs))) {
1034 warnx(_("%s: skipping bad line in /etc/fstab: "
1035 "bind mount with nonzero fsck pass number"),
1036 mnt_fs_get_target(fs));
1037 return 1;
1038 }
1039
1040 /*
1041 * ignore devices that don't exist and have the "nofail" mount option
1042 */
1043 if (!device_exists(fs_get_device(fs))) {
1044 if (opt_in_list("nofail", mnt_fs_get_options(fs))) {
1045 if (verbose)
1046 printf(_("%s: skipping nonexistent device\n"),
1047 fs_get_device(fs));
1048 return 1;
1049 }
1050 if (verbose)
1051 printf(_("%s: nonexistent device (\"nofail\" fstab "
1052 "option may be used to skip this device)\n"),
1053 fs_get_device(fs));
1054 }
1055
1056 fs_interpret_type(fs);
1057
1058 /*
1059 * If a specific fstype is specified, and it doesn't match,
1060 * ignore it.
1061 */
1062 if (!fs_match(fs, &fs_type_compiled))
1063 return 1;
1064
1065 type = mnt_fs_get_fstype(fs);
1066 if (!type) {
1067 if (verbose)
1068 printf(_("%s: skipping unknown filesystem type\n"),
1069 fs_get_device(fs));
1070 return 1;
1071 }
1072
1073 /* Are we ignoring this type? */
1074 if (fs_ignored_type(fs))
1075 return 1;
1076
1077 /* Do we really really want to check this fs? */
1078 for(ip = really_wanted; *ip; ip++)
1079 if (strcmp(type, *ip) == 0) {
1080 wanted = 1;
1081 break;
1082 }
1083
1084 /* See if the <fsck.fs> program is available. */
1085 if (find_fsck(type) == NULL) {
1086 if (wanted)
1087 warnx(_("cannot check %s: fsck.%s not found"),
1088 fs_get_device(fs), type);
1089 return 1;
1090 }
1091
1092 /* We can and want to check this file system type. */
1093 return 0;
1094 }
1095
1096 static int count_slaves(dev_t disk)
1097 {
1098 DIR *dir;
1099 struct dirent *dp;
1100 char dirname[PATH_MAX];
1101 int count = 0;
1102
1103 snprintf(dirname, sizeof(dirname),
1104 "/sys/dev/block/%u:%u/slaves/",
1105 major(disk), minor(disk));
1106
1107 if (!(dir = opendir(dirname)))
1108 return -1;
1109
1110 while ((dp = readdir(dir)) != 0) {
1111 #ifdef _DIRENT_HAVE_D_TYPE
1112 if (dp->d_type != DT_UNKNOWN && dp->d_type != DT_LNK)
1113 continue;
1114 #endif
1115 if (dp->d_name[0] == '.' &&
1116 ((dp->d_name[1] == 0) ||
1117 ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
1118 continue;
1119
1120 count++;
1121 }
1122
1123 closedir(dir);
1124 return count;
1125 }
1126
1127 /*
1128 * Returns TRUE if a partition on the same disk is already being
1129 * checked.
1130 */
1131 static int disk_already_active(struct libmnt_fs *fs)
1132 {
1133 struct fsck_instance *inst;
1134 dev_t disk;
1135
1136 if (force_all_parallel)
1137 return 0;
1138
1139 if (instance_list && fs_is_stacked(instance_list->fs))
1140 /* any instance for a stacked device is already running */
1141 return 1;
1142
1143 disk = fs_get_disk(fs, 1);
1144
1145 /*
1146 * If we don't know the base device, assume that the device is
1147 * already active if there are any fsck instances running.
1148 *
1149 * Don't check a stacked device with any other disk too.
1150 */
1151 if (!disk || fs_is_stacked(fs))
1152 return (instance_list != 0);
1153
1154 for (inst = instance_list; inst; inst = inst->next) {
1155 dev_t idisk = fs_get_disk(inst->fs, 0);
1156
1157 if (!idisk || disk == idisk)
1158 return 1;
1159 }
1160
1161 return 0;
1162 }
1163
1164 /* Check all file systems, using the /etc/fstab table. */
1165 static int check_all(void)
1166 {
1167 int not_done_yet = 1;
1168 int passno = 1;
1169 int pass_done;
1170 int status = FSCK_EX_OK;
1171
1172 struct libmnt_fs *fs;
1173 struct libmnt_iter *itr = mnt_new_iter(MNT_ITER_FORWARD);
1174
1175 if (!itr)
1176 err(FSCK_EX_ERROR, _("failed to allocate iterator"));
1177
1178 /*
1179 * Do an initial scan over the filesystem; mark filesystems
1180 * which should be ignored as done, and resolve any "auto"
1181 * filesystem types (done as a side-effect of calling ignore()).
1182 */
1183 while (mnt_table_next_fs(fstab, itr, &fs) == 0) {
1184 if (ignore(fs)) {
1185 fs_set_done(fs);
1186 continue;
1187 }
1188 }
1189
1190 if (verbose)
1191 fputs(_("Checking all file systems.\n"), stdout);
1192
1193 /*
1194 * Find and check the root filesystem.
1195 */
1196 if (!parallel_root) {
1197 fs = mnt_table_find_target(fstab, "/", MNT_ITER_FORWARD);
1198 if (fs) {
1199 if (!skip_root &&
1200 !fs_is_done(fs) &&
1201 !(ignore_mounted && is_mounted(fs))) {
1202 status |= fsck_device(fs, 1);
1203 status |= wait_many(FLAG_WAIT_ALL);
1204 if (status > FSCK_EX_NONDESTRUCT) {
1205 mnt_free_iter(itr);
1206 return status;
1207 }
1208 }
1209 fs_set_done(fs);
1210 }
1211 }
1212
1213 /*
1214 * This is for the bone-headed user who enters the root
1215 * filesystem twice. Skip root will skep all root entries.
1216 */
1217 if (skip_root) {
1218 mnt_reset_iter(itr, MNT_ITER_FORWARD);
1219
1220 while(mnt_table_next_fs(fstab, itr, &fs) == 0) {
1221 const char *tgt = mnt_fs_get_target(fs);
1222
1223 if (tgt && strcmp(tgt, "/") == 0)
1224 fs_set_done(fs);
1225 }
1226 }
1227
1228 while (not_done_yet) {
1229 not_done_yet = 0;
1230 pass_done = 1;
1231
1232 mnt_reset_iter(itr, MNT_ITER_FORWARD);
1233
1234 while(mnt_table_next_fs(fstab, itr, &fs) == 0) {
1235
1236 if (cancel_requested)
1237 break;
1238 if (fs_is_done(fs))
1239 continue;
1240 /*
1241 * If the filesystem's pass number is higher
1242 * than the current pass number, then we don't
1243 * do it yet.
1244 */
1245 if (mnt_fs_get_passno(fs) > passno) {
1246 not_done_yet++;
1247 continue;
1248 }
1249 if (ignore_mounted && is_mounted(fs)) {
1250 fs_set_done(fs);
1251 continue;
1252 }
1253 /*
1254 * If a filesystem on a particular device has
1255 * already been spawned, then we need to defer
1256 * this to another pass.
1257 */
1258 if (disk_already_active(fs)) {
1259 pass_done = 0;
1260 continue;
1261 }
1262 /*
1263 * Spawn off the fsck process
1264 */
1265 status |= fsck_device(fs, serialize);
1266 fs_set_done(fs);
1267
1268 /*
1269 * Only do one filesystem at a time, or if we
1270 * have a limit on the number of fsck's extant
1271 * at one time, apply that limit.
1272 */
1273 if (serialize ||
1274 (max_running && (num_running >= max_running))) {
1275 pass_done = 0;
1276 break;
1277 }
1278 }
1279 if (cancel_requested)
1280 break;
1281 if (verbose > 1)
1282 printf(_("--waiting-- (pass %d)\n"), passno);
1283
1284 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
1285 FLAG_WAIT_ATLEAST_ONE);
1286 if (pass_done) {
1287 if (verbose > 1)
1288 printf("----------------------------------\n");
1289 passno++;
1290 } else
1291 not_done_yet++;
1292 }
1293
1294 if (cancel_requested && !kill_sent) {
1295 kill_all(SIGTERM);
1296 kill_sent++;
1297 }
1298
1299 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
1300 mnt_free_iter(itr);
1301 return status;
1302 }
1303
1304 static void __attribute__((__noreturn__)) usage(FILE *out)
1305 {
1306 fputs(USAGE_HEADER, out);
1307 fprintf(out, _(" %s [options] -- [fs-options] [<filesystem> ...]\n"),
1308 program_invocation_short_name);
1309
1310 fputs(USAGE_OPTIONS, out);
1311 fputs(_(" -A check all filesystems\n"), out);
1312 fputs(_(" -C [<fd>] display progress bar; file descriptor is for GUIs\n"), out);
1313 fputs(_(" -l lock the device to guarantee exclusive access\n"), out);
1314 fputs(_(" -M do not check mounted filesystems\n"), out);
1315 fputs(_(" -N do not execute, just show what would be done\n"), out);
1316 fputs(_(" -P check filesystems in parallel, including root\n"), out);
1317 fputs(_(" -R skip root filesystem; useful only with '-A'\n"), out);
1318 fputs(_(" -r report statistics for each device checked\n"), out);
1319 fputs(_(" -s serialize the checking operations\n"), out);
1320 fputs(_(" -T do not show the title on startup\n"), out);
1321 fputs(_(" -t <type> specify filesystem types to be checked;\n"
1322 " <type> is allowed to be a comma-separated list\n"), out);
1323 fputs(_(" -V explain what is being done\n"), out);
1324 fputs(_(" -? display this help and exit\n"), out);
1325
1326 fputs(USAGE_SEPARATOR, out);
1327 fputs(_("See the specific fsck.* commands for available fs-options."), out);
1328 fprintf(out, USAGE_MAN_TAIL("fsck(8)"));
1329
1330 exit(out == stderr ? FSCK_EX_USAGE : FSCK_EX_OK);
1331 }
1332
1333 static void signal_cancel(int sig __attribute__((__unused__)))
1334 {
1335 cancel_requested++;
1336 }
1337
1338 static void parse_argv(int argc, char *argv[])
1339 {
1340 int i, j;
1341 char *arg, *dev, *tmp = 0;
1342 char options[128];
1343 int opt = 0;
1344 int opts_for_fsck = 0;
1345 struct sigaction sa;
1346
1347 /*
1348 * Set up signal action
1349 */
1350 memset(&sa, 0, sizeof(struct sigaction));
1351 sa.sa_handler = signal_cancel;
1352 sigaction(SIGINT, &sa, 0);
1353 sigaction(SIGTERM, &sa, 0);
1354
1355 num_devices = 0;
1356 num_args = 0;
1357 instance_list = 0;
1358
1359 for (i=1; i < argc; i++) {
1360 arg = argv[i];
1361 if (!arg)
1362 continue;
1363 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1364 if (num_devices >= MAX_DEVICES)
1365 errx(FSCK_EX_ERROR, _("too many devices"));
1366
1367 dev = mnt_resolve_spec(arg, mntcache);
1368
1369 if (!dev && strchr(arg, '=')) {
1370 /*
1371 * Check to see if we failed because
1372 * /proc/partitions isn't found.
1373 */
1374 if (access(_PATH_PROC_PARTITIONS, R_OK) < 0) {
1375 warn(_("cannot open %s"),
1376 _PATH_PROC_PARTITIONS);
1377 errx(FSCK_EX_ERROR, _("Is /proc mounted?"));
1378 }
1379 /*
1380 * Check to see if this is because
1381 * we're not running as root
1382 */
1383 if (geteuid())
1384 errx(FSCK_EX_ERROR,
1385 _("must be root to scan for matching filesystems: %s"),
1386 arg);
1387 else
1388 errx(FSCK_EX_ERROR,
1389 _("couldn't find matching filesystem: %s"),
1390 arg);
1391 }
1392 devices[num_devices++] = dev ? dev : xstrdup(arg);
1393 continue;
1394 }
1395 if (arg[0] != '-' || opts_for_fsck) {
1396 if (num_args >= MAX_ARGS)
1397 errx(FSCK_EX_ERROR, _("too many arguments"));
1398 args[num_args++] = xstrdup(arg);
1399 continue;
1400 }
1401 for (j=1; arg[j]; j++) {
1402 if (opts_for_fsck) {
1403 options[++opt] = arg[j];
1404 continue;
1405 }
1406 switch (arg[j]) {
1407 case 'A':
1408 doall = 1;
1409 break;
1410 case 'C':
1411 progress = 1;
1412 if (arg[j+1]) {
1413 progress_fd = string_to_int(arg+j+1);
1414 if (progress_fd < 0)
1415 progress_fd = 0;
1416 else
1417 goto next_arg;
1418 } else if ((i+1) < argc &&
1419 !strncmp(argv[i+1], "-", 1) == 0) {
1420 progress_fd = string_to_int(argv[i]);
1421 if (progress_fd < 0)
1422 progress_fd = 0;
1423 else {
1424 ++i;
1425 goto next_arg;
1426 }
1427 }
1428 break;
1429 case 'l':
1430 lockdisk = 1;
1431 break;
1432 case 'V':
1433 verbose++;
1434 break;
1435 case 'N':
1436 noexecute = 1;
1437 break;
1438 case 'R':
1439 skip_root = 1;
1440 break;
1441 case 'T':
1442 notitle = 1;
1443 break;
1444 case 'M':
1445 ignore_mounted = 1;
1446 break;
1447 case 'P':
1448 parallel_root = 1;
1449 break;
1450 case 'r':
1451 report_stats = 1;
1452 break;
1453 case 's':
1454 serialize = 1;
1455 break;
1456 case 't':
1457 tmp = 0;
1458 if (fstype)
1459 usage(stderr);
1460 if (arg[j+1])
1461 tmp = arg+j+1;
1462 else if ((i+1) < argc)
1463 tmp = argv[++i];
1464 else
1465 usage(stderr);
1466 fstype = xstrdup(tmp);
1467 compile_fs_type(fstype, &fs_type_compiled);
1468 goto next_arg;
1469 case '-':
1470 opts_for_fsck++;
1471 break;
1472 case '?':
1473 usage(stdout);
1474 break;
1475 default:
1476 options[++opt] = arg[j];
1477 break;
1478 }
1479 }
1480 next_arg:
1481 if (opt) {
1482 options[0] = '-';
1483 options[++opt] = '\0';
1484 if (num_args >= MAX_ARGS)
1485 errx(FSCK_EX_ERROR, _("too many arguments"));
1486 args[num_args++] = xstrdup(options);
1487 opt = 0;
1488 }
1489 }
1490 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1491 force_all_parallel++;
1492 if ((tmp = getenv("FSCK_MAX_INST")))
1493 max_running = atoi(tmp);
1494 }
1495
1496 int main(int argc, char *argv[])
1497 {
1498 int i, status = 0;
1499 int interactive = 0;
1500 char *oldpath = getenv("PATH");
1501 struct libmnt_fs *fs;
1502
1503 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
1504 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
1505
1506 setlocale(LC_MESSAGES, "");
1507 setlocale(LC_CTYPE, "");
1508 bindtextdomain(PACKAGE, LOCALEDIR);
1509 textdomain(PACKAGE);
1510 atexit(close_stdout);
1511
1512 mnt_init_debug(0); /* init libmount debug mask */
1513 mntcache = mnt_new_cache(); /* no fatal error if failed */
1514
1515 parse_argv(argc, argv);
1516
1517 if (!notitle)
1518 printf(UTIL_LINUX_VERSION);
1519
1520 load_fs_info();
1521
1522 /* Update our search path to include uncommon directories. */
1523 if (oldpath) {
1524 fsck_path = xmalloc (strlen (fsck_prefix_path) + 1 +
1525 strlen (oldpath) + 1);
1526 strcpy (fsck_path, fsck_prefix_path);
1527 strcat (fsck_path, ":");
1528 strcat (fsck_path, oldpath);
1529 } else {
1530 fsck_path = xstrdup(fsck_prefix_path);
1531 }
1532
1533 if ((num_devices == 1) || (serialize))
1534 interactive = 1;
1535
1536 if (lockdisk && (doall || num_devices > 1)) {
1537 warnx(_("the -l option can be used with one "
1538 "device only -- ignore"));
1539 lockdisk = 0;
1540 }
1541
1542 /* If -A was specified ("check all"), do that! */
1543 if (doall)
1544 return check_all();
1545
1546 if (num_devices == 0) {
1547 serialize++;
1548 interactive++;
1549 return check_all();
1550 }
1551 for (i = 0 ; i < num_devices; i++) {
1552 if (cancel_requested) {
1553 if (!kill_sent) {
1554 kill_all(SIGTERM);
1555 kill_sent++;
1556 }
1557 break;
1558 }
1559 fs = lookup(devices[i]);
1560 if (!fs)
1561 fs = add_dummy_fs(devices[i]);
1562 else if (fs_ignored_type(fs))
1563 continue;
1564
1565 if (ignore_mounted && is_mounted(fs))
1566 continue;
1567 status |= fsck_device(fs, interactive);
1568 if (serialize ||
1569 (max_running && (num_running >= max_running))) {
1570 struct fsck_instance *inst;
1571
1572 inst = wait_one(0);
1573 if (inst) {
1574 status |= inst->exit_status;
1575 free_instance(inst);
1576 }
1577 if (verbose > 1)
1578 printf("----------------------------------\n");
1579 }
1580 }
1581 status |= wait_many(FLAG_WAIT_ALL);
1582 free(fsck_path);
1583 mnt_unref_cache(mntcache);
1584 mnt_unref_table(fstab);
1585 mnt_unref_table(mtab);
1586 return status;
1587 }