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, "BdvpfFnyl: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;
134 catopt(&options, globopt);
140 catopt(&options, globopt);
144 warnx("Ignoring obsolete -l option\n");
153 if (!TAILQ_EMPTY(&selhead))
154 errx(1, "only one -t option may be specified.");
156 maketypelist(optarg);
170 return checkfstab(flags, isok, checkfs);
172 #define BADTYPE(type) \
173 (strcmp(type, FSTAB_RO) && \
174 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
177 for (; argc--; argv++) {
178 const char *spec, *mntpt, *type, *cp;
179 char device[MAXPATHLEN];
183 cp = strrchr(spec, '/');
185 (void)snprintf(device, sizeof(device), "%s%s",
189 mntp = getmntpt(spec);
191 spec = mntp->f_mntfromname;
192 mntpt = mntp->f_mntonname;
194 if ((fs = getfsfile(spec)) == NULL &&
195 (fs = getfsspec(spec)) == NULL) {
197 vfstype = getfslab(spec);
199 errx(1, "Could not determine filesystem type");
204 type = fs->fs_vfstype;
206 if (BADTYPE(fs->fs_type))
207 errx(1, "%s has unknown file system type.",
210 if ((flags & CHECK_BACKGRD) &&
211 checkfs(type, spec, mntpt, "-F", NULL) == 0) {
212 printf("%s: DEFER FOR BACKGROUND CHECKING\n", *argv);
215 if ((flags & DO_BACKGRD) && forceflag == 0 &&
216 checkfs(type, spec, mntpt, "-F", NULL) != 0)
219 rval |= checkfs(type, spec, mntpt, NULL, NULL);
227 isok(struct fstab *fs)
231 if (fs->fs_passno == 0)
233 if (BADTYPE(fs->fs_type))
235 if (!selected(fs->fs_vfstype))
238 * If the -B flag has been given, then process the needed
239 * background checks. Background checks cannot be run on
240 * file systems that will be mounted read-only or that were
241 * not mounted at boot time (typically those marked `noauto').
242 * If these basic tests are passed, check with the file system
243 * itself to see if it is willing to do background checking
244 * by invoking its check program with the -F flag.
246 if (flags & DO_BACKGRD) {
247 if (!strcmp(fs->fs_type, FSTAB_RO))
249 if (getmntpt(fs->fs_spec) == NULL)
251 if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", 0))
256 * If the -F flag has been given, then consider deferring the
257 * check to background. Background checks cannot be run on
258 * file systems that will be mounted read-only or that will
259 * not be mounted at boot time (e.g., marked `noauto'). If
260 * these basic tests are passed, check with the file system
261 * itself to see if it is willing to defer to background
262 * checking by invoking its check program with the -F flag.
264 if ((flags & CHECK_BACKGRD) == 0 || !strcmp(fs->fs_type, FSTAB_RO))
266 for (i = strlen(fs->fs_mntops) - 6; i >= 0; i--)
267 if (!strncmp(&fs->fs_mntops[i], "noauto", 6))
271 if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", NULL) != 0)
273 printf("%s: DEFER FOR BACKGROUND CHECKING\n", fs->fs_spec);
279 checkfs(const char *pvfstype, const char *spec, const char *mntpt,
280 char *auxopt, pid_t *pidp)
284 int argc, i, status, maxargc;
285 char *optbuf, execbase[MAXPATHLEN];
286 char *vfstype = NULL;
287 const char *extra = NULL;
290 /* Avoid vfork clobbering */
295 * We convert the vfstype to lowercase and any spaces to underscores
296 * to not confuse the issue
298 * XXX This is a kludge to make automatic filesystem type guessing
299 * from the disklabel work for "4.2BSD" filesystems. It does a
300 * very limited subset of transliteration to a normalised form of
301 * filesystem name, and we do not seem to enforce a filesystem
302 * name character set.
304 vfstype = strdup(pvfstype);
306 perror("strdup(pvfstype)");
307 for (i = 0; i < strlen(vfstype); i++) {
308 vfstype[i] = tolower(vfstype[i]);
309 if (vfstype[i] == ' ')
313 extra = getoptions(vfstype);
316 catopt(&optbuf, options);
318 catopt(&optbuf, extra);
320 catopt(&optbuf, auxopt);
321 else if (flags & DO_BACKGRD)
322 catopt(&optbuf, "-B");
325 argv = emalloc(sizeof(char *) * maxargc);
327 (void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
329 argv[argc++] = execbase;
331 mangle(optbuf, &argc, &argv, &maxargc);
335 if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
336 (void)printf("start %s %swait", mntpt,
338 for (i = 0; i < argc; i++)
339 (void)printf(" %s", argv[i]);
343 switch (pid = vfork()) {
344 case -1: /* Error. */
352 if ((flags & CHECK_DEBUG) && auxopt == NULL)
355 /* Go find an executable. */
356 execvP(execbase, _PATH_SYSPATH, (char * const *)argv);
358 warn("exec %s for %s in %s", execbase, spec, _PATH_SYSPATH);
360 warn("exec %s in %s", execbase, _PATH_SYSPATH);
364 default: /* Parent. */
375 if (waitpid(pid, &status, 0) < 0) {
380 if (WIFEXITED(status)) {
381 if (WEXITSTATUS(status) != 0)
382 return (WEXITSTATUS(status));
384 else if (WIFSIGNALED(status)) {
385 warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
396 selected(const char *type)
400 /* If no type specified, it's always selected. */
401 TAILQ_FOREACH(e, &selhead, entries)
402 if (!strncmp(e->type, type, MFSNAMELEN))
403 return which == IN_LIST ? 1 : 0;
405 return which == IN_LIST ? 0 : 1;
410 getoptions(const char *type)
414 TAILQ_FOREACH(e, &opthead, entries)
415 if (!strncmp(e->type, type, MFSNAMELEN))
422 addoption(char *optstr)
427 if ((newoptions = strchr(optstr, ':')) == NULL)
428 errx(1, "Invalid option string");
430 *newoptions++ = '\0';
432 TAILQ_FOREACH(e, &opthead, entries)
433 if (!strncmp(e->type, optstr, MFSNAMELEN)) {
434 catopt(&e->options, newoptions);
437 addentry(&opthead, optstr, newoptions);
442 addentry(struct fstypelist *list, const char *type, const char *opts)
446 e = emalloc(sizeof(struct entry));
447 e->type = estrdup(type);
448 e->options = estrdup(opts);
449 TAILQ_INSERT_TAIL(list, e, entries);
454 maketypelist(char *fslist)
458 if ((fslist == NULL) || (fslist[0] == '\0'))
459 errx(1, "empty type list");
461 if (fslist[0] == 'n' && fslist[1] == 'o') {
468 while ((ptr = strsep(&fslist, ",")) != NULL)
469 addentry(&selhead, ptr, "");
475 catopt(char **sp, const char *o)
483 j = i + 1 + strlen(o) + 1;
485 (void)snprintf(s + i, j, ",%s", o);
493 mangle(char *options, int *argcp, const char ***argvp, int *maxargcp)
503 for (s = options; (p = strsep(&s, ",")) != NULL;) {
504 /* Always leave space for one more argument and the NULL. */
505 if (argc >= maxargc - 3) {
507 argv = erealloc(argv, maxargc * sizeof(char *));
531 getfslab(const char *str)
539 /* deduce the file system type from the disk label */
540 if ((fd = open(str, O_RDONLY)) == -1)
541 err(1, "cannot open `%s'", str);
543 if (ioctl(fd, DIOCGDINFO, &dl) == -1)
548 p = str[strlen(str) - 1];
550 if ((p - 'a') >= dl.d_npartitions)
551 errx(1, "partition `%s' is not defined on disk", str);
553 if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES)
554 errx(1, "partition `%s' is not of a legal vfstype",
557 if ((vfstype = fstypenames[t]) == NULL)
558 errx(1, "vfstype `%s' on partition `%s' is not supported",
559 fstypenames[t], str);
568 static const char common[] =
569 "[-dfnpvy] [-B | -F] [-T fstype:fsoptions] [-t fstype]";
571 (void)fprintf(stderr, "usage: %s %s [special | node] ...\n",
572 getprogname(), common);