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>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/mount.h>
46 #include <sys/queue.h>
49 #include <sys/disklabel.h>
50 #include <sys/ioctl.h>
66 static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
68 TAILQ_HEAD(fstypelist, entry) opthead, selhead;
73 TAILQ_ENTRY(entry) entries;
76 static char *options = NULL;
78 static int forceflag = 0;
80 static int checkfs(const char *, const char *, const char *, char *, pid_t *);
81 static int selected(const char *);
82 static void addoption(char *);
83 static const char *getoptions(const char *);
84 static void addentry(struct fstypelist *, const char *, const char *);
85 static void maketypelist(char *);
86 static void catopt(char **, const char *);
87 static void mangle(char *, int *, const char ***, int *);
88 static const char *getfslab(const char *);
89 static void usage(void) __dead2;
90 static int isok(struct fstab *);
93 main(int argc, char *argv[])
97 const char *vfstype = NULL;
103 TAILQ_INIT(&selhead);
104 TAILQ_INIT(&opthead);
106 while ((i = getopt(argc, argv, "BCdvpfFnyl:t:T:")) != -1)
109 if (flags & CHECK_BACKGRD)
110 errx(1, "Cannot specify -B and -F.");
115 flags |= CHECK_DEBUG;
119 flags |= CHECK_VERBOSE;
123 if (flags & DO_BACKGRD)
124 errx(1, "Cannot specify -B and -F.");
125 flags |= CHECK_BACKGRD;
129 flags |= CHECK_PREEN;
132 flags |= CHECK_CLEAN;
137 catopt(&options, globopt);
143 catopt(&options, globopt);
147 warnx("Ignoring obsolete -l option\n");
156 if (!TAILQ_EMPTY(&selhead))
157 errx(1, "only one -t option may be specified.");
159 maketypelist(optarg);
173 return checkfstab(flags, isok, checkfs);
175 #define BADTYPE(type) \
176 (strcmp(type, FSTAB_RO) && \
177 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
180 for (; argc--; argv++) {
181 const char *spec, *mntpt, *type, *cp;
182 char device[MAXPATHLEN];
186 cp = strrchr(spec, '/');
188 (void)snprintf(device, sizeof(device), "%s%s",
192 mntp = getmntpt(spec);
194 spec = mntp->f_mntfromname;
195 mntpt = mntp->f_mntonname;
197 if ((fs = getfsfile(spec)) == NULL &&
198 (fs = getfsspec(spec)) == NULL) {
200 vfstype = getfslab(spec);
202 errx(1, "Could not determine filesystem type");
207 type = fs->fs_vfstype;
209 if (BADTYPE(fs->fs_type))
210 errx(1, "%s has unknown file system type.",
213 if ((flags & CHECK_BACKGRD) &&
214 checkfs(type, spec, mntpt, "-F", NULL) == 0) {
215 printf("%s: DEFER FOR BACKGROUND CHECKING\n", *argv);
218 if ((flags & DO_BACKGRD) && forceflag == 0 &&
219 checkfs(type, spec, mntpt, "-F", NULL) != 0)
222 rval |= checkfs(type, spec, mntpt, NULL, NULL);
230 isok(struct fstab *fs)
234 if (fs->fs_passno == 0)
236 if (BADTYPE(fs->fs_type))
238 if (!selected(fs->fs_vfstype))
241 * If the -B flag has been given, then process the needed
242 * background checks. Background checks cannot be run on
243 * file systems that will be mounted read-only or that were
244 * not mounted at boot time (typically those marked `noauto').
245 * If these basic tests are passed, check with the file system
246 * itself to see if it is willing to do background checking
247 * by invoking its check program with the -F flag.
249 if (flags & DO_BACKGRD) {
250 if (!strcmp(fs->fs_type, FSTAB_RO))
252 if (getmntpt(fs->fs_spec) == NULL)
254 if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", 0))
259 * If the -F flag has been given, then consider deferring the
260 * check to background. Background checks cannot be run on
261 * file systems that will be mounted read-only or that will
262 * not be mounted at boot time (e.g., marked `noauto'). If
263 * these basic tests are passed, check with the file system
264 * itself to see if it is willing to defer to background
265 * checking by invoking its check program with the -F flag.
267 if ((flags & CHECK_BACKGRD) == 0 || !strcmp(fs->fs_type, FSTAB_RO))
269 for (i = strlen(fs->fs_mntops) - 6; i >= 0; i--)
270 if (!strncmp(&fs->fs_mntops[i], "noauto", 6))
274 if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", NULL) != 0)
276 printf("%s: DEFER FOR BACKGROUND CHECKING\n", fs->fs_spec);
282 checkfs(const char *pvfstype, const char *spec, const char *mntpt,
283 char *auxopt, pid_t *pidp)
287 int argc, i, status, maxargc;
288 char *optbuf, execbase[MAXPATHLEN];
289 char *vfstype = NULL;
290 const char *extra = NULL;
293 /* Avoid vfork clobbering */
298 * We convert the vfstype to lowercase and any spaces to underscores
299 * to not confuse the issue
301 * XXX This is a kludge to make automatic filesystem type guessing
302 * from the disklabel work for "4.2BSD" filesystems. It does a
303 * very limited subset of transliteration to a normalised form of
304 * filesystem name, and we do not seem to enforce a filesystem
305 * name character set.
307 vfstype = strdup(pvfstype);
309 perror("strdup(pvfstype)");
310 for (i = 0; i < strlen(vfstype); i++) {
311 vfstype[i] = tolower(vfstype[i]);
312 if (vfstype[i] == ' ')
316 extra = getoptions(vfstype);
319 catopt(&optbuf, options);
321 catopt(&optbuf, extra);
323 catopt(&optbuf, auxopt);
324 else if (flags & DO_BACKGRD)
325 catopt(&optbuf, "-B");
328 argv = emalloc(sizeof(char *) * maxargc);
330 (void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
332 argv[argc++] = execbase;
334 mangle(optbuf, &argc, &argv, &maxargc);
338 if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
339 (void)printf("start %s %swait", mntpt,
341 for (i = 0; i < argc; i++)
342 (void)printf(" %s", argv[i]);
346 switch (pid = vfork()) {
347 case -1: /* Error. */
355 if ((flags & CHECK_DEBUG) && auxopt == NULL)
358 /* Go find an executable. */
359 execvP(execbase, _PATH_SYSPATH, (char * const *)argv);
361 warn("exec %s for %s in %s", execbase, spec, _PATH_SYSPATH);
363 warn("exec %s in %s", execbase, _PATH_SYSPATH);
367 default: /* Parent. */
378 if (waitpid(pid, &status, 0) < 0) {
383 if (WIFEXITED(status)) {
384 if (WEXITSTATUS(status) != 0)
385 return (WEXITSTATUS(status));
387 else if (WIFSIGNALED(status)) {
388 warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
399 selected(const char *type)
403 /* If no type specified, it's always selected. */
404 TAILQ_FOREACH(e, &selhead, entries)
405 if (!strncmp(e->type, type, MFSNAMELEN))
406 return which == IN_LIST ? 1 : 0;
408 return which == IN_LIST ? 0 : 1;
413 getoptions(const char *type)
417 TAILQ_FOREACH(e, &opthead, entries)
418 if (!strncmp(e->type, type, MFSNAMELEN))
425 addoption(char *optstr)
430 if ((newoptions = strchr(optstr, ':')) == NULL)
431 errx(1, "Invalid option string");
433 *newoptions++ = '\0';
435 TAILQ_FOREACH(e, &opthead, entries)
436 if (!strncmp(e->type, optstr, MFSNAMELEN)) {
437 catopt(&e->options, newoptions);
440 addentry(&opthead, optstr, newoptions);
445 addentry(struct fstypelist *list, const char *type, const char *opts)
449 e = emalloc(sizeof(struct entry));
450 e->type = estrdup(type);
451 e->options = estrdup(opts);
452 TAILQ_INSERT_TAIL(list, e, entries);
457 maketypelist(char *fslist)
461 if ((fslist == NULL) || (fslist[0] == '\0'))
462 errx(1, "empty type list");
464 if (fslist[0] == 'n' && fslist[1] == 'o') {
471 while ((ptr = strsep(&fslist, ",")) != NULL)
472 addentry(&selhead, ptr, "");
478 catopt(char **sp, const char *o)
486 j = i + 1 + strlen(o) + 1;
488 (void)snprintf(s + i, j, ",%s", o);
496 mangle(char *options, int *argcp, const char ***argvp, int *maxargcp)
506 for (s = options; (p = strsep(&s, ",")) != NULL;) {
507 /* Always leave space for one more argument and the NULL. */
508 if (argc >= maxargc - 3) {
510 argv = erealloc(argv, maxargc * sizeof(char *));
534 getfslab(const char *str)
542 /* deduce the file system type from the disk label */
543 if ((fd = open(str, O_RDONLY)) == -1)
544 err(1, "cannot open `%s'", str);
546 if (ioctl(fd, DIOCGDINFO, &dl) == -1)
551 p = str[strlen(str) - 1];
553 if ((p - 'a') >= dl.d_npartitions)
554 errx(1, "partition `%s' is not defined on disk", str);
556 if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES)
557 errx(1, "partition `%s' is not of a legal vfstype",
560 if ((vfstype = fstypenames[t]) == NULL)
561 errx(1, "vfstype `%s' on partition `%s' is not supported",
562 fstypenames[t], str);
571 static const char common[] =
572 "[-Cdfnpvy] [-B | -F] [-T fstype:fsoptions] [-t fstype]";
574 (void)fprintf(stderr, "usage: %s %s [special | node] ...\n",
575 getprogname(), common);