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 static 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;
99 const char *etc_fstab;
104 TAILQ_INIT(&selhead);
105 TAILQ_INIT(&opthead);
108 while ((i = getopt(argc, argv, "BCdvpfFnyl:t:T:c:")) != -1)
111 if (flags & CHECK_BACKGRD)
112 errx(1, "Cannot specify -B and -F.");
117 flags |= CHECK_DEBUG;
121 flags |= CHECK_VERBOSE;
125 if (flags & DO_BACKGRD)
126 errx(1, "Cannot specify -B and -F.");
127 flags |= CHECK_BACKGRD;
131 flags |= CHECK_PREEN;
134 flags |= CHECK_CLEAN;
139 catopt(&options, globopt);
145 catopt(&options, globopt);
149 warnx("Ignoring obsolete -l option\n");
158 if (!TAILQ_EMPTY(&selhead))
159 errx(1, "only one -t option may be specified.");
161 maketypelist(optarg);
178 if (etc_fstab != NULL)
182 return checkfstab(flags, isok, checkfs);
184 #define BADTYPE(type) \
185 (strcmp(type, FSTAB_RO) && \
186 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
189 for (; argc--; argv++) {
190 const char *spec, *mntpt, *type, *cp;
191 char device[MAXPATHLEN];
195 cp = strrchr(spec, '/');
197 (void)snprintf(device, sizeof(device), "%s%s",
201 mntp = getmntpt(spec);
203 spec = mntp->f_mntfromname;
204 mntpt = mntp->f_mntonname;
206 if ((fs = getfsfile(spec)) == NULL &&
207 (fs = getfsspec(spec)) == NULL) {
209 vfstype = getfslab(spec);
211 errx(1, "Could not determine filesystem type");
216 type = fs->fs_vfstype;
218 if (BADTYPE(fs->fs_type))
219 errx(1, "%s has unknown file system type.",
222 if ((flags & CHECK_BACKGRD) &&
223 checkfs(type, spec, mntpt, "-F", NULL) == 0) {
224 printf("%s: DEFER FOR BACKGROUND CHECKING\n", *argv);
227 if ((flags & DO_BACKGRD) && forceflag == 0 &&
228 checkfs(type, spec, mntpt, "-F", NULL) != 0)
231 rval |= checkfs(type, spec, mntpt, NULL, NULL);
239 isok(struct fstab *fs)
243 if (fs->fs_passno == 0)
245 if (BADTYPE(fs->fs_type))
247 if (!selected(fs->fs_vfstype))
250 * If the -B flag has been given, then process the needed
251 * background checks. Background checks cannot be run on
252 * file systems that will be mounted read-only or that were
253 * not mounted at boot time (typically those marked `noauto').
254 * If these basic tests are passed, check with the file system
255 * itself to see if it is willing to do background checking
256 * by invoking its check program with the -F flag.
258 if (flags & DO_BACKGRD) {
259 if (!strcmp(fs->fs_type, FSTAB_RO))
261 if (getmntpt(fs->fs_spec) == NULL)
263 if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", 0))
268 * If the -F flag has been given, then consider deferring the
269 * check to background. Background checks cannot be run on
270 * file systems that will be mounted read-only or that will
271 * not be mounted at boot time (e.g., marked `noauto'). If
272 * these basic tests are passed, check with the file system
273 * itself to see if it is willing to defer to background
274 * checking by invoking its check program with the -F flag.
276 if ((flags & CHECK_BACKGRD) == 0 || !strcmp(fs->fs_type, FSTAB_RO))
278 for (i = strlen(fs->fs_mntops) - 6; i >= 0; i--)
279 if (!strncmp(&fs->fs_mntops[i], "noauto", 6))
283 if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", NULL) != 0)
285 printf("%s: DEFER FOR BACKGROUND CHECKING\n", fs->fs_spec);
291 checkfs(const char *pvfstype, const char *spec, const char *mntpt,
292 char *auxopt, pid_t *pidp)
296 int argc, i, status, maxargc;
297 char *optbuf, execbase[MAXPATHLEN];
298 char *vfstype = NULL;
299 const char *extra = NULL;
302 /* Avoid vfork clobbering */
307 * We convert the vfstype to lowercase and any spaces to underscores
308 * to not confuse the issue
310 * XXX This is a kludge to make automatic filesystem type guessing
311 * from the disklabel work for "4.2BSD" filesystems. It does a
312 * very limited subset of transliteration to a normalised form of
313 * filesystem name, and we do not seem to enforce a filesystem
314 * name character set.
316 vfstype = strdup(pvfstype);
318 perror("strdup(pvfstype)");
319 for (i = 0; i < strlen(vfstype); i++) {
320 vfstype[i] = tolower(vfstype[i]);
321 if (vfstype[i] == ' ')
325 extra = getoptions(vfstype);
328 catopt(&optbuf, options);
330 catopt(&optbuf, extra);
332 catopt(&optbuf, auxopt);
333 else if (flags & DO_BACKGRD)
334 catopt(&optbuf, "-B");
337 argv = emalloc(sizeof(char *) * maxargc);
339 (void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
341 argv[argc++] = execbase;
343 mangle(optbuf, &argc, &argv, &maxargc);
347 if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
348 (void)printf("start %s %swait", mntpt,
350 for (i = 0; i < argc; i++)
351 (void)printf(" %s", argv[i]);
355 switch (pid = vfork()) {
356 case -1: /* Error. */
364 if ((flags & CHECK_DEBUG) && auxopt == NULL)
367 /* Go find an executable. */
368 execvP(execbase, _PATH_SYSPATH, (char * const *)argv);
370 warn("exec %s for %s in %s", execbase, spec, _PATH_SYSPATH);
372 warn("exec %s in %s", execbase, _PATH_SYSPATH);
376 default: /* Parent. */
387 if (waitpid(pid, &status, 0) < 0) {
392 if (WIFEXITED(status)) {
393 if (WEXITSTATUS(status) != 0)
394 return (WEXITSTATUS(status));
396 else if (WIFSIGNALED(status)) {
397 warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
408 selected(const char *type)
412 /* If no type specified, it's always selected. */
413 TAILQ_FOREACH(e, &selhead, entries)
414 if (!strncmp(e->type, type, MFSNAMELEN))
415 return which == IN_LIST ? 1 : 0;
417 return which == IN_LIST ? 0 : 1;
422 getoptions(const char *type)
426 TAILQ_FOREACH(e, &opthead, entries)
427 if (!strncmp(e->type, type, MFSNAMELEN))
434 addoption(char *optstr)
439 if ((newoptions = strchr(optstr, ':')) == NULL)
440 errx(1, "Invalid option string");
442 *newoptions++ = '\0';
444 TAILQ_FOREACH(e, &opthead, entries)
445 if (!strncmp(e->type, optstr, MFSNAMELEN)) {
446 catopt(&e->options, newoptions);
449 addentry(&opthead, optstr, newoptions);
454 addentry(struct fstypelist *list, const char *type, const char *opts)
458 e = emalloc(sizeof(struct entry));
459 e->type = estrdup(type);
460 e->options = estrdup(opts);
461 TAILQ_INSERT_TAIL(list, e, entries);
466 maketypelist(char *fslist)
470 if ((fslist == NULL) || (fslist[0] == '\0'))
471 errx(1, "empty type list");
473 if (fslist[0] == 'n' && fslist[1] == 'o') {
480 while ((ptr = strsep(&fslist, ",")) != NULL)
481 addentry(&selhead, ptr, "");
487 catopt(char **sp, const char *o)
495 j = i + 1 + strlen(o) + 1;
497 (void)snprintf(s + i, j, ",%s", o);
505 mangle(char *options, int *argcp, const char ***argvp, int *maxargcp)
515 for (s = options; (p = strsep(&s, ",")) != NULL;) {
516 /* Always leave space for one more argument and the NULL. */
517 if (argc >= maxargc - 3) {
519 argv = erealloc(argv, maxargc * sizeof(char *));
543 getfslab(const char *str)
551 /* deduce the file system type from the disk label */
552 if ((fd = open(str, O_RDONLY)) == -1)
553 err(1, "cannot open `%s'", str);
555 if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
562 p = str[strlen(str) - 1];
564 if ((p - 'a') >= dl.d_npartitions)
565 errx(1, "partition `%s' is not defined on disk", str);
567 if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES)
568 errx(1, "partition `%s' is not of a legal vfstype",
571 if ((vfstype = fstypenames[t]) == NULL)
572 errx(1, "vfstype `%s' on partition `%s' is not supported",
573 fstypenames[t], str);
582 static const char common[] =
583 "[-Cdfnpvy] [-B | -F] [-T fstype:fsoptions] [-t fstype] [-c fstab]";
585 (void)fprintf(stderr, "usage: %s %s [special | node] ...\n",
586 getprogname(), common);