1 /* $NetBSD: fsck.c,v 1.30 2003/08/07 10:04:15 agc 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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * From: @(#)mount.c 8.19 (Berkeley) 4/19/94
33 * From: $NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp
34 * $NetBSD: fsck.c,v 1.30 2003/08/07 10:04:15 agc Exp $
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/mount.h>
42 #include <sys/queue.h>
45 #include <sys/ioctl.h>
61 static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
63 static TAILQ_HEAD(fstypelist, entry) opthead, selhead;
68 TAILQ_ENTRY(entry) entries;
71 static char *options = NULL;
73 static int forceflag = 0;
75 static int checkfs(const char *, const char *, const char *, const char *, pid_t *);
76 static int selected(const char *);
77 static void addoption(char *);
78 static const char *getoptions(const char *);
79 static void addentry(struct fstypelist *, const char *, const char *);
80 static void maketypelist(char *);
81 static void catopt(char **, const char *);
82 static void mangle(char *, int *, const char ** volatile *, int *);
83 static const char *getfstype(const char *);
84 static void usage(void) __dead2;
85 static int isok(struct fstab *);
99 main(int argc, char *argv[])
103 const char *vfstype = NULL;
105 const char *etc_fstab;
110 TAILQ_INIT(&selhead);
111 TAILQ_INIT(&opthead);
114 while ((i = getopt(argc, argv, "BCdvpfFnyl:t:T:c:")) != -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;
140 flags |= CHECK_CLEAN;
145 catopt(&options, globopt);
151 catopt(&options, globopt);
155 warnx("Ignoring obsolete -l option\n");
164 if (!TAILQ_EMPTY(&selhead))
165 errx(1, "only one -t option may be specified.");
167 maketypelist(optarg);
184 if (etc_fstab != NULL)
188 return checkfstab(flags, isok, checkfs);
190 #define BADTYPE(type) \
191 (strcmp(type, FSTAB_RO) && \
192 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
195 for (; argc--; argv++) {
196 const char *spec, *mntpt, *type, *cp;
197 char device[MAXPATHLEN];
202 cp = strrchr(spec, '/');
204 (void)snprintf(device, sizeof(device), "%s%s",
208 mntp = getmntpt(spec);
210 spec = mntp->f_mntfromname;
211 mntpt = mntp->f_mntonname;
213 if ((fs = getfsfile(spec)) == NULL &&
214 (fs = getfsspec(spec)) == NULL) {
216 vfstype = getfstype(spec);
218 errx(1, "Could not determine filesystem type");
223 type = fs->fs_vfstype;
225 if (BADTYPE(fs->fs_type))
226 errx(1, "%s has unknown file system type.",
229 if ((flags & CHECK_BACKGRD) &&
230 checkfs(type, spec, mntpt, "-F", NULL) == 0) {
231 printf("%s: DEFER FOR BACKGROUND CHECKING\n", *argv);
234 if ((flags & DO_BACKGRD) && forceflag == 0 &&
235 checkfs(type, spec, mntpt, "-F", NULL) != 0)
238 rval |= checkfs(type, spec, mntpt, NULL, NULL);
246 isok(struct fstab *fs)
250 if (fs->fs_passno == 0)
252 if (BADTYPE(fs->fs_type))
254 if (!selected(fs->fs_vfstype))
257 * If the -B flag has been given, then process the needed
258 * background checks. Background checks cannot be run on
259 * file systems that will be mounted read-only or that were
260 * not mounted at boot time (typically those marked `noauto').
261 * If these basic tests are passed, check with the file system
262 * itself to see if it is willing to do background checking
263 * by invoking its check program with the -F flag.
265 if (flags & DO_BACKGRD) {
266 if (!strcmp(fs->fs_type, FSTAB_RO))
268 if (getmntpt(fs->fs_spec) == NULL)
270 if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", 0))
275 * If the -F flag has been given, then consider deferring the
276 * check to background. Background checks cannot be run on
277 * file systems that will be mounted read-only or that will
278 * not be mounted at boot time (e.g., marked `noauto'). If
279 * these basic tests are passed, check with the file system
280 * itself to see if it is willing to defer to background
281 * checking by invoking its check program with the -F flag.
283 if ((flags & CHECK_BACKGRD) == 0 || !strcmp(fs->fs_type, FSTAB_RO))
285 for (i = strlen(fs->fs_mntops) - 6; i >= 0; i--)
286 if (!strncmp(&fs->fs_mntops[i], "noauto", 6))
290 if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", NULL) != 0)
292 printf("%s: DEFER FOR BACKGROUND CHECKING\n", fs->fs_spec);
298 checkfs(const char *pvfstype, const char *spec, const char *mntpt,
299 const char *auxopt, pid_t *pidp)
301 const char ** volatile argv;
303 int argc, i, status, maxargc;
304 char *optbuf, execbase[MAXPATHLEN];
305 char *vfstype = NULL;
306 const char *extra = NULL;
309 /* Avoid vfork clobbering */
314 * We convert the vfstype to lowercase and any spaces to underscores
315 * to not confuse the issue
317 * XXX This is a kludge to make automatic filesystem type guessing
318 * from the disklabel work for "4.2BSD" filesystems. It does a
319 * very limited subset of transliteration to a normalised form of
320 * filesystem name, and we do not seem to enforce a filesystem
321 * name character set.
323 vfstype = strdup(pvfstype);
325 perr("strdup(pvfstype)");
326 for (i = 0; i < (int)strlen(vfstype); i++) {
327 vfstype[i] = tolower(vfstype[i]);
328 if (vfstype[i] == ' ')
332 extra = getoptions(vfstype);
335 catopt(&optbuf, options);
337 catopt(&optbuf, extra);
339 catopt(&optbuf, auxopt);
340 else if (flags & DO_BACKGRD)
341 catopt(&optbuf, "-B");
344 argv = emalloc(sizeof(char *) * maxargc);
346 (void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
348 argv[argc++] = execbase;
350 mangle(optbuf, &argc, &argv, &maxargc);
354 if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
355 (void)printf("start %s %swait", mntpt,
357 for (i = 0; i < argc; i++)
358 (void)printf(" %s", argv[i]);
362 switch (pid = vfork()) {
363 case -1: /* Error. */
371 if ((flags & CHECK_DEBUG) && auxopt == NULL)
374 /* Go find an executable. */
375 execvP(execbase, _PATH_SYSPATH, __DECONST(char * const *, argv));
377 warn("exec %s for %s in %s", execbase, spec, _PATH_SYSPATH);
379 warn("exec %s in %s", execbase, _PATH_SYSPATH);
383 default: /* Parent. */
394 if (waitpid(pid, &status, 0) < 0) {
399 if (WIFEXITED(status)) {
400 if (WEXITSTATUS(status) != 0)
401 return (WEXITSTATUS(status));
403 else if (WIFSIGNALED(status)) {
404 warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
415 selected(const char *type)
419 /* If no type specified, it's always selected. */
420 TAILQ_FOREACH(e, &selhead, entries)
421 if (!strncmp(e->type, type, MFSNAMELEN))
422 return which == IN_LIST ? 1 : 0;
424 return which == IN_LIST ? 0 : 1;
429 getoptions(const char *type)
433 TAILQ_FOREACH(e, &opthead, entries)
434 if (!strncmp(e->type, type, MFSNAMELEN))
441 addoption(char *optstr)
446 if ((newoptions = strchr(optstr, ':')) == NULL)
447 errx(1, "Invalid option string");
449 *newoptions++ = '\0';
451 TAILQ_FOREACH(e, &opthead, entries)
452 if (!strncmp(e->type, optstr, MFSNAMELEN)) {
453 catopt(&e->options, newoptions);
456 addentry(&opthead, optstr, newoptions);
461 addentry(struct fstypelist *list, const char *type, const char *opts)
465 e = emalloc(sizeof(struct entry));
466 e->type = estrdup(type);
467 e->options = estrdup(opts);
468 TAILQ_INSERT_TAIL(list, e, entries);
473 maketypelist(char *fslist)
477 if ((fslist == NULL) || (fslist[0] == '\0'))
478 errx(1, "empty type list");
480 if (fslist[0] == 'n' && fslist[1] == 'o') {
487 while ((ptr = strsep(&fslist, ",")) != NULL)
488 addentry(&selhead, ptr, "");
494 catopt(char **sp, const char *o)
502 j = i + 1 + strlen(o) + 1;
504 (void)snprintf(s + i, j, ",%s", o);
512 mangle(char *opts, int *argcp, const char ** volatile *argvp, int *maxargcp)
522 for (s = opts; (p = strsep(&s, ",")) != NULL;) {
523 /* Always leave space for one more argument and the NULL. */
524 if (argc >= maxargc - 3) {
526 argv = erealloc(argv, maxargc * sizeof(char *));
549 getfstype(const char *str)
551 struct diocgattr_arg attr;
554 if ((fd = open(str, O_RDONLY)) == -1)
555 err(1, "cannot open `%s'", str);
557 strncpy(attr.name, "PART::type", sizeof(attr.name));
558 memset(&attr.value, 0, sizeof(attr.value));
559 attr.len = sizeof(attr.value);
560 if (ioctl(fd, DIOCGATTR, &attr) == -1) {
565 for (i = 0; ptype_map[i].ptype != NULL; i++)
566 if (strstr(attr.value.str, ptype_map[i].ptype) != NULL)
567 return (ptype_map[i].name);
575 static const char common[] =
576 "[-Cdfnpvy] [-B | -F] [-T fstype:fsoptions] [-t fstype] [-c fstab]";
578 (void)fprintf(stderr, "usage: %s %s [special | node] ...\n",
579 getprogname(), common);