1 /* $NetBSD: fsck.c,v 1.21 1999/04/22 04:20:53 abs Exp $ */
4 * Copyright (c) 1996 Christos Zoulas. All rights reserved.
5 * Copyright (c) 1980, 1989, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * From: @(#)mount.c 8.19 (Berkeley) 4/19/94
37 * From: $NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp
38 * $NetBSD: fsck.c,v 1.21 1999/04/22 04:20:53 abs Exp $
41 #include <sys/cdefs.h>
46 #include <sys/param.h>
47 #include <sys/mount.h>
48 #include <sys/queue.h>
52 #include <sys/disklabel.h>
53 #include <sys/ioctl.h>
66 #include "pathnames.h"
69 static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
71 TAILQ_HEAD(fstypelist, entry) opthead, selhead;
76 TAILQ_ENTRY(entry) entries;
79 static char *options = NULL;
81 static int forceflag = 0;
83 int main __P((int, char *[]));
85 static int checkfs __P((const char *, const char *, const char *, char *,
87 static int selected __P((const char *));
88 static void addoption __P((char *));
89 static const char *getoptions __P((const char *));
90 static void addentry __P((struct fstypelist *, const char *, const char *));
91 static void maketypelist __P((char *));
92 static void catopt __P((char **, const char *));
93 static void mangle __P((char *, int *, const char ***, int *));
94 static const char *getfslab __P((const char *));
95 static void usage __P((void));
96 static int isok __P((struct fstab *));
105 const char *vfstype = NULL;
111 TAILQ_INIT(&selhead);
112 TAILQ_INIT(&opthead);
114 while ((i = getopt(argc, argv, "BdvpfFnyl:t:T:")) != -1)
117 if (flags & CHECK_BACKGRD)
118 errx(1, "Cannot specify -B and -F.");
123 flags |= CHECK_DEBUG;
127 flags |= CHECK_VERBOSE;
131 if (flags & DO_BACKGRD)
132 errx(1, "Cannot specify -B and -F.");
133 flags |= CHECK_BACKGRD;
137 flags |= CHECK_PREEN;
142 catopt(&options, globopt);
148 catopt(&options, globopt);
152 warnx("Ignoring obsolete -l option\n");
161 if (!TAILQ_EMPTY(&selhead))
162 errx(1, "only one -t option may be specified.");
164 maketypelist(optarg);
178 return checkfstab(flags, isok, checkfs);
180 #define BADTYPE(type) \
181 (strcmp(type, FSTAB_RO) && \
182 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
185 for (; argc--; argv++) {
186 const char *spec, *mntpt, *type, *cp;
187 char device[MAXPATHLEN];
191 cp = strrchr(spec, '/');
193 (void)snprintf(device, sizeof(device), "%s%s",
197 mntp = getmntpt(spec);
199 spec = mntp->f_mntfromname;
200 mntpt = mntp->f_mntonname;
202 if ((fs = getfsfile(spec)) == NULL &&
203 (fs = getfsspec(spec)) == NULL) {
205 vfstype = getfslab(spec);
210 type = fs->fs_vfstype;
212 if (BADTYPE(fs->fs_type))
213 errx(1, "%s has unknown file system type.",
216 if ((flags & CHECK_BACKGRD) &&
217 checkfs(type, spec, mntpt, "-F", NULL) == 0) {
218 printf("%s: DEFER FOR BACKGROUND CHECKING\n", *argv);
221 if ((flags & DO_BACKGRD) && forceflag == 0 &&
222 checkfs(type, spec, mntpt, "-F", NULL) != 0)
225 rval |= checkfs(type, spec, mntpt, NULL, NULL);
238 if (fs->fs_passno == 0)
240 if (BADTYPE(fs->fs_type))
242 if (!selected(fs->fs_vfstype))
245 * If the -B flag has been given, then process the needed
246 * background checks. Background checks cannot be run on
247 * filesystems that will be mounted read-only or that were
248 * not mounted at boot time (typically those marked `noauto').
249 * If these basic tests are passed, check with the filesystem
250 * itself to see if it is willing to do background checking
251 * by invoking its check program with the -F flag.
253 if (flags & DO_BACKGRD) {
254 if (!strcmp(fs->fs_type, FSTAB_RO))
256 if (getmntpt(fs->fs_spec) == NULL)
258 if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", 0))
263 * If the -F flag has been given, then consider deferring the
264 * check to background. Background checks cannot be run on
265 * filesystems that will be mounted read-only or that will
266 * not be mounted at boot time (e.g., marked `noauto'). If
267 * these basic tests are passed, check with the filesystem
268 * itself to see if it is willing to defer to background
269 * checking by invoking its check program with the -F flag.
271 if ((flags & CHECK_BACKGRD) == 0 || !strcmp(fs->fs_type, FSTAB_RO))
273 for (i = strlen(fs->fs_mntops) - 6; i >= 0; i--)
274 if (!strncmp(&fs->fs_mntops[i], "noauto", 6))
278 if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", NULL) != 0)
280 printf("%s: DEFER FOR BACKGROUND CHECKING\n", fs->fs_spec);
286 checkfs(pvfstype, spec, mntpt, auxopt, pidp)
287 const char *pvfstype, *spec, *mntpt;
291 /* List of directories containing fsck_xxx subcommands. */
292 static const char *edirs[] = {
297 const char **argv, **edir;
299 int argc, i, status, maxargc;
300 char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN];
301 char *vfstype = NULL;
302 const char *extra = NULL;
305 /* Avoid vfork clobbering */
310 * We convert the vfstype to lowercase and any spaces to underscores
311 * to not confuse the issue
313 vfstype = strdup(pvfstype);
315 perror("strdup(pvfstype)");
316 for (i = 0; i < strlen(vfstype); i++) {
317 vfstype[i] = tolower(vfstype[i]);
318 if (vfstype[i] == ' ')
322 extra = getoptions(vfstype);
325 catopt(&optbuf, options);
327 catopt(&optbuf, extra);
329 catopt(&optbuf, auxopt);
330 else if (flags & DO_BACKGRD)
331 catopt(&optbuf, "-B");
334 argv = emalloc(sizeof(char *) * maxargc);
336 (void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
338 argv[argc++] = execbase;
340 mangle(optbuf, &argc, &argv, &maxargc);
344 if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
345 (void)printf("start %s %swait", mntpt,
347 for (i = 0; i < argc; i++)
348 (void)printf(" %s", argv[i]);
352 switch (pid = vfork()) {
353 case -1: /* Error. */
361 if ((flags & CHECK_DEBUG) && auxopt == NULL)
364 /* Go find an executable. */
367 (void)snprintf(execname,
368 sizeof(execname), "%s/%s", *edir, execbase);
369 execv(execname, (char * const *)argv);
370 if (errno != ENOENT) {
372 warn("exec %s for %s", execname, spec);
374 warn("exec %s", execname);
376 } while (*++edir != NULL);
378 if (errno == ENOENT) {
380 warn("exec %s for %s", execname, spec);
382 warn("exec %s", execname);
387 default: /* Parent. */
398 if (waitpid(pid, &status, 0) < 0) {
403 if (WIFEXITED(status)) {
404 if (WEXITSTATUS(status) != 0)
405 return (WEXITSTATUS(status));
407 else if (WIFSIGNALED(status)) {
408 warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
424 /* If no type specified, it's always selected. */
425 TAILQ_FOREACH(e, &selhead, entries)
426 if (!strncmp(e->type, type, MFSNAMELEN))
427 return which == IN_LIST ? 1 : 0;
429 return which == IN_LIST ? 0 : 1;
439 TAILQ_FOREACH(e, &opthead, entries)
440 if (!strncmp(e->type, type, MFSNAMELEN))
453 if ((newoptions = strchr(optstr, ':')) == NULL)
454 errx(1, "Invalid option string");
456 *newoptions++ = '\0';
458 TAILQ_FOREACH(e, &opthead, entries)
459 if (!strncmp(e->type, optstr, MFSNAMELEN)) {
460 catopt(&e->options, newoptions);
463 addentry(&opthead, optstr, newoptions);
468 addentry(list, type, opts)
469 struct fstypelist *list;
475 e = emalloc(sizeof(struct entry));
476 e->type = estrdup(type);
477 e->options = estrdup(opts);
478 TAILQ_INSERT_TAIL(list, e, entries);
488 if ((fslist == NULL) || (fslist[0] == '\0'))
489 errx(1, "empty type list");
491 if (fslist[0] == 'n' && fslist[1] == 'o') {
498 while ((ptr = strsep(&fslist, ",")) != NULL)
499 addentry(&selhead, ptr, "");
515 j = i + 1 + strlen(o) + 1;
517 (void)snprintf(s + i, j, ",%s", o);
525 mangle(options, argcp, argvp, maxargcp)
527 int *argcp, *maxargcp;
538 for (s = options; (p = strsep(&s, ",")) != NULL;) {
539 /* Always leave space for one more argument and the NULL. */
540 if (argc >= maxargc - 3) {
542 argv = erealloc(argv, maxargc * sizeof(char *));
575 /* deduce the filesystem type from the disk label */
576 if ((fd = open(str, O_RDONLY)) == -1)
577 err(1, "cannot open `%s'", str);
579 if (ioctl(fd, DIOCGDINFO, &dl) == -1)
580 err(1, "cannot get disklabel for `%s'", str);
584 p = str[strlen(str) - 1];
586 if ((p - 'a') >= dl.d_npartitions)
587 errx(1, "partition `%s' is not defined on disk", str);
589 if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES)
590 errx(1, "partition `%s' is not of a legal vfstype",
593 if ((vfstype = fstypenames[t]) == NULL)
594 errx(1, "vfstype `%s' on partition `%s' is not supported",
595 fstypenames[t], str);
604 extern char *__progname;
605 static const char common[] =
606 "[-BFdpvlyn] [-T fstype:fsoptions] [-t fstype]";
608 (void)fprintf(stderr, "Usage: %s %s [special|node]...\n",