xref: /freebsd/sbin/fsck/fsck.c (revision ea906c4152774dff300bb26fbfc1e4188351c89a)
1 /*	$NetBSD: fsck.c,v 1.21 1999/04/22 04:20:53 abs Exp $	*/
2 
3 /*
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.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
23  *
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
34  * SUCH DAMAGE.
35  *
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 $
39  */
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include <sys/param.h>
45 #include <sys/mount.h>
46 #include <sys/queue.h>
47 #include <sys/wait.h>
48 #define FSTYPENAMES
49 #include <sys/disklabel.h>
50 #include <sys/ioctl.h>
51 
52 #include <ctype.h>
53 #include <err.h>
54 #include <errno.h>
55 #include <fstab.h>
56 #include <fcntl.h>
57 #include <paths.h>
58 #include <signal.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 
64 #include "fsutil.h"
65 
66 static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
67 
68 TAILQ_HEAD(fstypelist, entry) opthead, selhead;
69 
70 struct entry {
71 	char *type;
72 	char *options;
73 	TAILQ_ENTRY(entry) entries;
74 };
75 
76 static char *options = NULL;
77 static int flags = 0;
78 static int forceflag = 0;
79 
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 *);
91 
92 int
93 main(int argc, char *argv[])
94 {
95 	struct fstab *fs;
96 	int i, rval = 0;
97 	const char *vfstype = NULL;
98 	char globopt[3];
99 
100 	globopt[0] = '-';
101 	globopt[2] = '\0';
102 
103 	TAILQ_INIT(&selhead);
104 	TAILQ_INIT(&opthead);
105 
106 	while ((i = getopt(argc, argv, "BdvpfFnyl:t:T:")) != -1)
107 		switch (i) {
108 		case 'B':
109 			if (flags & CHECK_BACKGRD)
110 				errx(1, "Cannot specify -B and -F.");
111 			flags |= DO_BACKGRD;
112 			break;
113 
114 		case 'd':
115 			flags |= CHECK_DEBUG;
116 			break;
117 
118 		case 'v':
119 			flags |= CHECK_VERBOSE;
120 			break;
121 
122 		case 'F':
123 			if (flags & DO_BACKGRD)
124 				errx(1, "Cannot specify -B and -F.");
125 			flags |= CHECK_BACKGRD;
126 			break;
127 
128 		case 'p':
129 			flags |= CHECK_PREEN;
130 			/*FALLTHROUGH*/
131 		case 'n':
132 		case 'y':
133 			globopt[1] = i;
134 			catopt(&options, globopt);
135 			break;
136 
137 		case 'f':
138 			forceflag = 1;
139 			globopt[1] = i;
140 			catopt(&options, globopt);
141 			break;
142 
143 		case 'l':
144 			warnx("Ignoring obsolete -l option\n");
145 			break;
146 
147 		case 'T':
148 			if (*optarg)
149 				addoption(optarg);
150 			break;
151 
152 		case 't':
153 			if (!TAILQ_EMPTY(&selhead))
154 				errx(1, "only one -t option may be specified.");
155 
156 			maketypelist(optarg);
157 			vfstype = optarg;
158 			break;
159 
160 		case '?':
161 		default:
162 			usage();
163 			/* NOTREACHED */
164 		}
165 
166 	argc -= optind;
167 	argv += optind;
168 
169 	if (argc == 0)
170 		return checkfstab(flags, isok, checkfs);
171 
172 #define	BADTYPE(type)							\
173 	(strcmp(type, FSTAB_RO) &&					\
174 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
175 
176 
177 	for (; argc--; argv++) {
178 		const char *spec, *mntpt, *type, *cp;
179 		char device[MAXPATHLEN];
180 		struct statfs *mntp;
181 
182 		spec = *argv;
183 		cp = strrchr(spec, '/');
184 		if (cp == 0) {
185 			(void)snprintf(device, sizeof(device), "%s%s",
186 				_PATH_DEV, spec);
187 			spec = device;
188 		}
189 		mntp = getmntpt(spec);
190 		if (mntp != NULL) {
191 			spec = mntp->f_mntfromname;
192 			mntpt = mntp->f_mntonname;
193 		}
194 		if ((fs = getfsfile(spec)) == NULL &&
195 		    (fs = getfsspec(spec)) == NULL) {
196 			if (vfstype == NULL)
197 				vfstype = getfslab(spec);
198 			if (vfstype == NULL)
199 				errx(1, "Could not determine filesystem type");
200 			type = vfstype;
201 			devcheck(spec);
202 		} else {
203 			spec = fs->fs_spec;
204 			type = fs->fs_vfstype;
205 			mntpt = fs->fs_file;
206 			if (BADTYPE(fs->fs_type))
207 				errx(1, "%s has unknown file system type.",
208 				    spec);
209 		}
210 		if ((flags & CHECK_BACKGRD) &&
211 		    checkfs(type, spec, mntpt, "-F", NULL) == 0) {
212 			printf("%s: DEFER FOR BACKGROUND CHECKING\n", *argv);
213 			continue;
214 		}
215 		if ((flags & DO_BACKGRD) && forceflag == 0 &&
216 		    checkfs(type, spec, mntpt, "-F", NULL) != 0)
217 			continue;
218 
219 		rval |= checkfs(type, spec, mntpt, NULL, NULL);
220 	}
221 
222 	return rval;
223 }
224 
225 
226 static int
227 isok(struct fstab *fs)
228 {
229 	int i;
230 
231 	if (fs->fs_passno == 0)
232 		return (0);
233 	if (BADTYPE(fs->fs_type))
234 		return (0);
235 	if (!selected(fs->fs_vfstype))
236 		return (0);
237 	/*
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.
245 	 */
246 	if (flags & DO_BACKGRD) {
247 		if (!strcmp(fs->fs_type, FSTAB_RO))
248 			return (0);
249 		if (getmntpt(fs->fs_spec) == NULL)
250 			return (0);
251 		if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", 0))
252 			return (0);
253 		return (1);
254 	}
255 	/*
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.
263 	 */
264 	if ((flags & CHECK_BACKGRD) == 0 || !strcmp(fs->fs_type, FSTAB_RO))
265 		return (1);
266 	for (i = strlen(fs->fs_mntops) - 6; i >= 0; i--)
267 		if (!strncmp(&fs->fs_mntops[i], "noauto", 6))
268 			break;
269 	if (i >= 0)
270 		return (1);
271 	if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", NULL) != 0)
272 		return (1);
273 	printf("%s: DEFER FOR BACKGROUND CHECKING\n", fs->fs_spec);
274 	return (0);
275 }
276 
277 
278 static int
279 checkfs(const char *pvfstype, const char *spec, const char *mntpt,
280     char *auxopt, pid_t *pidp)
281 {
282 	const char **argv;
283 	pid_t pid;
284 	int argc, i, status, maxargc;
285 	char *optbuf, execbase[MAXPATHLEN];
286 	char *vfstype = NULL;
287 	const char *extra = NULL;
288 
289 #ifdef __GNUC__
290 	/* Avoid vfork clobbering */
291 	(void) &optbuf;
292 	(void) &vfstype;
293 #endif
294 	/*
295 	 * We convert the vfstype to lowercase and any spaces to underscores
296 	 * to not confuse the issue
297 	 *
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.
303 	 */
304 	vfstype = strdup(pvfstype);
305 	if (vfstype == NULL)
306 		perror("strdup(pvfstype)");
307 	for (i = 0; i < strlen(vfstype); i++) {
308 		vfstype[i] = tolower(vfstype[i]);
309 		if (vfstype[i] == ' ')
310 			vfstype[i] = '_';
311 	}
312 
313 	extra = getoptions(vfstype);
314 	optbuf = NULL;
315 	if (options)
316 		catopt(&optbuf, options);
317 	if (extra)
318 		catopt(&optbuf, extra);
319 	if (auxopt)
320 		catopt(&optbuf, auxopt);
321 	else if (flags & DO_BACKGRD)
322 		catopt(&optbuf, "-B");
323 
324 	maxargc = 64;
325 	argv = emalloc(sizeof(char *) * maxargc);
326 
327 	(void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
328 	argc = 0;
329 	argv[argc++] = execbase;
330 	if (optbuf)
331 		mangle(optbuf, &argc, &argv, &maxargc);
332 	argv[argc++] = spec;
333 	argv[argc] = NULL;
334 
335 	if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
336 		(void)printf("start %s %swait", mntpt,
337 			pidp ? "no" : "");
338 		for (i = 0; i < argc; i++)
339 			(void)printf(" %s", argv[i]);
340 		(void)printf("\n");
341 	}
342 
343 	switch (pid = vfork()) {
344 	case -1:				/* Error. */
345 		warn("vfork");
346 		if (optbuf)
347 			free(optbuf);
348 		free(vfstype);
349 		return (1);
350 
351 	case 0:					/* Child. */
352 		if ((flags & CHECK_DEBUG) && auxopt == NULL)
353 			_exit(0);
354 
355 		/* Go find an executable. */
356 		execvP(execbase, _PATH_SYSPATH, (char * const *)argv);
357 		if (spec)
358 			warn("exec %s for %s in %s", execbase, spec, _PATH_SYSPATH);
359 		else
360 			warn("exec %s in %s", execbase, _PATH_SYSPATH);
361 		_exit(1);
362 		/* NOTREACHED */
363 
364 	default:				/* Parent. */
365 		if (optbuf)
366 			free(optbuf);
367 
368 		free(vfstype);
369 
370 		if (pidp) {
371 			*pidp = pid;
372 			return 0;
373 		}
374 
375 		if (waitpid(pid, &status, 0) < 0) {
376 			warn("waitpid");
377 			return (1);
378 		}
379 
380 		if (WIFEXITED(status)) {
381 			if (WEXITSTATUS(status) != 0)
382 				return (WEXITSTATUS(status));
383 		}
384 		else if (WIFSIGNALED(status)) {
385 			warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
386 			return (1);
387 		}
388 		break;
389 	}
390 
391 	return (0);
392 }
393 
394 
395 static int
396 selected(const char *type)
397 {
398 	struct entry *e;
399 
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;
404 
405 	return which == IN_LIST ? 0 : 1;
406 }
407 
408 
409 static const char *
410 getoptions(const char *type)
411 {
412 	struct entry *e;
413 
414 	TAILQ_FOREACH(e, &opthead, entries)
415 		if (!strncmp(e->type, type, MFSNAMELEN))
416 			return e->options;
417 	return "";
418 }
419 
420 
421 static void
422 addoption(char *optstr)
423 {
424 	char *newoptions;
425 	struct entry *e;
426 
427 	if ((newoptions = strchr(optstr, ':')) == NULL)
428 		errx(1, "Invalid option string");
429 
430 	*newoptions++ = '\0';
431 
432 	TAILQ_FOREACH(e, &opthead, entries)
433 		if (!strncmp(e->type, optstr, MFSNAMELEN)) {
434 			catopt(&e->options, newoptions);
435 			return;
436 		}
437 	addentry(&opthead, optstr, newoptions);
438 }
439 
440 
441 static void
442 addentry(struct fstypelist *list, const char *type, const char *opts)
443 {
444 	struct entry *e;
445 
446 	e = emalloc(sizeof(struct entry));
447 	e->type = estrdup(type);
448 	e->options = estrdup(opts);
449 	TAILQ_INSERT_TAIL(list, e, entries);
450 }
451 
452 
453 static void
454 maketypelist(char *fslist)
455 {
456 	char *ptr;
457 
458 	if ((fslist == NULL) || (fslist[0] == '\0'))
459 		errx(1, "empty type list");
460 
461 	if (fslist[0] == 'n' && fslist[1] == 'o') {
462 		fslist += 2;
463 		which = NOT_IN_LIST;
464 	}
465 	else
466 		which = IN_LIST;
467 
468 	while ((ptr = strsep(&fslist, ",")) != NULL)
469 		addentry(&selhead, ptr, "");
470 
471 }
472 
473 
474 static void
475 catopt(char **sp, const char *o)
476 {
477 	char *s;
478 	size_t i, j;
479 
480 	s = *sp;
481 	if (s) {
482 		i = strlen(s);
483 		j = i + 1 + strlen(o) + 1;
484 		s = erealloc(s, j);
485 		(void)snprintf(s + i, j, ",%s", o);
486 	} else
487 		s = estrdup(o);
488 	*sp = s;
489 }
490 
491 
492 static void
493 mangle(char *options, int *argcp, const char ***argvp, int *maxargcp)
494 {
495 	char *p, *s;
496 	int argc, maxargc;
497 	const char **argv;
498 
499 	argc = *argcp;
500 	argv = *argvp;
501 	maxargc = *maxargcp;
502 
503 	for (s = options; (p = strsep(&s, ",")) != NULL;) {
504 		/* Always leave space for one more argument and the NULL. */
505 		if (argc >= maxargc - 3) {
506 			maxargc <<= 1;
507 			argv = erealloc(argv, maxargc * sizeof(char *));
508 		}
509 		if (*p != '\0')  {
510 			if (*p == '-') {
511 				argv[argc++] = p;
512 				p = strchr(p, '=');
513 				if (p) {
514 					*p = '\0';
515 					argv[argc++] = p+1;
516 				}
517 			} else {
518 				argv[argc++] = "-o";
519 				argv[argc++] = p;
520 			}
521 		}
522 	}
523 
524 	*argcp = argc;
525 	*argvp = argv;
526 	*maxargcp = maxargc;
527 }
528 
529 
530 const static char *
531 getfslab(const char *str)
532 {
533 	struct disklabel dl;
534 	int fd;
535 	char p;
536 	const char *vfstype;
537 	u_char t;
538 
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);
542 
543 	if (ioctl(fd, DIOCGDINFO, &dl) == -1)
544 		return(NULL);
545 
546 	(void) close(fd);
547 
548 	p = str[strlen(str) - 1];
549 
550 	if ((p - 'a') >= dl.d_npartitions)
551 		errx(1, "partition `%s' is not defined on disk", str);
552 
553 	if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES)
554 		errx(1, "partition `%s' is not of a legal vfstype",
555 		    str);
556 
557 	if ((vfstype = fstypenames[t]) == NULL)
558 		errx(1, "vfstype `%s' on partition `%s' is not supported",
559 		    fstypenames[t], str);
560 
561 	return vfstype;
562 }
563 
564 
565 static void
566 usage(void)
567 {
568 	static const char common[] =
569 	    "[-dfnpvy] [-B | -F] [-T fstype:fsoptions] [-t fstype]";
570 
571 	(void)fprintf(stderr, "usage: %s %s [special | node] ...\n",
572 	    getprogname(), common);
573 	exit(1);
574 }
575