fsck.c revision 113994
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: head/sbin/fsck/fsck.c 113994 2003-04-25 01:12:35Z jmallett $");
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#define DKTYPENAMES
50#include <sys/disklabel.h>
51#include <sys/ioctl.h>
52
53#include <ctype.h>
54#include <err.h>
55#include <errno.h>
56#include <fstab.h>
57#include <fcntl.h>
58#include <paths.h>
59#include <signal.h>
60#include <stdio.h>
61#include <stdlib.h>
62#include <string.h>
63#include <unistd.h>
64
65#include "pathnames.h"
66#include "fsutil.h"
67
68static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
69
70TAILQ_HEAD(fstypelist, entry) opthead, selhead;
71
72struct entry {
73	char *type;
74	char *options;
75	TAILQ_ENTRY(entry) entries;
76};
77
78static char *options = NULL;
79static int flags = 0;
80static int forceflag = 0;
81
82static int checkfs(const char *, const char *, const char *, char *, pid_t *);
83static int selected(const char *);
84static void addoption(char *);
85static const char *getoptions(const char *);
86static void addentry(struct fstypelist *, const char *, const char *);
87static void maketypelist(char *);
88static void catopt(char **, const char *);
89static void mangle(char *, int *, const char ***, int *);
90static const char *getfslab(const char *);
91static void usage(void) __dead2;
92static int isok(struct fstab *);
93
94int
95main(int argc, char *argv[])
96{
97	struct fstab *fs;
98	int i, rval = 0;
99	const char *vfstype = NULL;
100	char globopt[3];
101
102	globopt[0] = '-';
103	globopt[2] = '\0';
104
105	TAILQ_INIT(&selhead);
106	TAILQ_INIT(&opthead);
107
108	while ((i = getopt(argc, argv, "BdvpfFnyl:t:T:")) != -1)
109		switch (i) {
110		case 'B':
111			if (flags & CHECK_BACKGRD)
112				errx(1, "Cannot specify -B and -F.");
113			flags |= DO_BACKGRD;
114			break;
115
116		case 'd':
117			flags |= CHECK_DEBUG;
118			break;
119
120		case 'v':
121			flags |= CHECK_VERBOSE;
122			break;
123
124		case 'F':
125			if (flags & DO_BACKGRD)
126				errx(1, "Cannot specify -B and -F.");
127			flags |= CHECK_BACKGRD;
128			break;
129
130		case 'p':
131			flags |= CHECK_PREEN;
132			/*FALLTHROUGH*/
133		case 'n':
134		case 'y':
135			globopt[1] = i;
136			catopt(&options, globopt);
137			break;
138
139		case 'f':
140			forceflag = 1;
141			globopt[1] = i;
142			catopt(&options, globopt);
143			break;
144
145		case 'l':
146			warnx("Ignoring obsolete -l option\n");
147			break;
148
149		case 'T':
150			if (*optarg)
151				addoption(optarg);
152			break;
153
154		case 't':
155			if (!TAILQ_EMPTY(&selhead))
156				errx(1, "only one -t option may be specified.");
157
158			maketypelist(optarg);
159			vfstype = optarg;
160			break;
161
162		case '?':
163		default:
164			usage();
165			/* NOTREACHED */
166		}
167
168	argc -= optind;
169	argv += optind;
170
171	if (argc == 0)
172		return checkfstab(flags, isok, checkfs);
173
174#define	BADTYPE(type)							\
175	(strcmp(type, FSTAB_RO) &&					\
176	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
177
178
179	for (; argc--; argv++) {
180		const char *spec, *mntpt, *type, *cp;
181		char device[MAXPATHLEN];
182		struct statfs *mntp;
183
184		spec = *argv;
185		cp = strrchr(spec, '/');
186		if (cp == 0) {
187			(void)snprintf(device, sizeof(device), "%s%s",
188				_PATH_DEV, spec);
189			spec = device;
190		}
191		mntp = getmntpt(spec);
192		if (mntp != NULL) {
193			spec = mntp->f_mntfromname;
194			mntpt = mntp->f_mntonname;
195		}
196		if ((fs = getfsfile(spec)) == NULL &&
197		    (fs = getfsspec(spec)) == NULL) {
198			if (vfstype == NULL)
199				vfstype = getfslab(spec);
200			if (vfstype == NULL)
201				errx(1, "Could not determine filesystem type");
202			type = vfstype;
203			devcheck(spec);
204		} else {
205			spec = fs->fs_spec;
206			type = fs->fs_vfstype;
207			mntpt = fs->fs_file;
208			if (BADTYPE(fs->fs_type))
209				errx(1, "%s has unknown file system type.",
210				    spec);
211		}
212		if ((flags & CHECK_BACKGRD) &&
213		    checkfs(type, spec, mntpt, "-F", NULL) == 0) {
214			printf("%s: DEFER FOR BACKGROUND CHECKING\n", *argv);
215			continue;
216		}
217		if ((flags & DO_BACKGRD) && forceflag == 0 &&
218		    checkfs(type, spec, mntpt, "-F", NULL) != 0)
219			continue;
220
221		rval |= checkfs(type, spec, mntpt, NULL, NULL);
222	}
223
224	return rval;
225}
226
227
228static int
229isok(struct fstab *fs)
230{
231	int i;
232
233	if (fs->fs_passno == 0)
234		return (0);
235	if (BADTYPE(fs->fs_type))
236		return (0);
237	if (!selected(fs->fs_vfstype))
238		return (0);
239	/*
240	 * If the -B flag has been given, then process the needed
241	 * background checks. Background checks cannot be run on
242	 * file systems that will be mounted read-only or that were
243	 * not mounted at boot time (typically those marked `noauto').
244	 * If these basic tests are passed, check with the file system
245	 * itself to see if it is willing to do background checking
246	 * by invoking its check program with the -F flag.
247	 */
248	if (flags & DO_BACKGRD) {
249		if (!strcmp(fs->fs_type, FSTAB_RO))
250			return (0);
251		if (getmntpt(fs->fs_spec) == NULL)
252			return (0);
253		if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", 0))
254			return (0);
255		return (1);
256	}
257	/*
258	 * If the -F flag has been given, then consider deferring the
259	 * check to background. Background checks cannot be run on
260	 * file systems that will be mounted read-only or that will
261	 * not be mounted at boot time (e.g., marked `noauto'). If
262	 * these basic tests are passed, check with the file system
263	 * itself to see if it is willing to defer to background
264	 * checking by invoking its check program with the -F flag.
265	 */
266	if ((flags & CHECK_BACKGRD) == 0 || !strcmp(fs->fs_type, FSTAB_RO))
267		return (1);
268	for (i = strlen(fs->fs_mntops) - 6; i >= 0; i--)
269		if (!strncmp(&fs->fs_mntops[i], "noauto", 6))
270			break;
271	if (i >= 0)
272		return (1);
273	if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", NULL) != 0)
274		return (1);
275	printf("%s: DEFER FOR BACKGROUND CHECKING\n", fs->fs_spec);
276	return (0);
277}
278
279
280static int
281checkfs(const char *pvfstype, const char *spec, const char *mntpt,
282    char *auxopt, pid_t *pidp)
283{
284	/* List of directories containing fsck_xxx subcommands. */
285	static const char *edirs[] = {
286		_PATH_SBIN,
287		_PATH_USRSBIN,
288		NULL
289	};
290	const char **argv, **edir;
291	pid_t pid;
292	int argc, i, status, maxargc;
293	char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN];
294	char *vfstype = NULL;
295	const char *extra = NULL;
296
297#ifdef __GNUC__
298	/* Avoid vfork clobbering */
299	(void) &optbuf;
300	(void) &vfstype;
301#endif
302	/*
303	 * We convert the vfstype to lowercase and any spaces to underscores
304	 * to not confuse the issue
305	 *
306	 * XXX This is a kludge to make automatic filesystem type guessing
307	 * from the disklabel work for "4.2BSD" filesystems.  It does a
308	 * very limited subset of transliteration to a normalised form of
309	 * filesystem name, and we do not seem to enforce a filesystem
310	 * name character set.
311	 */
312	vfstype = strdup(pvfstype);
313	if (vfstype == NULL)
314		perror("strdup(pvfstype)");
315	for (i = 0; i < strlen(vfstype); i++) {
316		vfstype[i] = tolower(vfstype[i]);
317		if (vfstype[i] == ' ')
318			vfstype[i] = '_';
319	}
320
321	extra = getoptions(vfstype);
322	optbuf = NULL;
323	if (options)
324		catopt(&optbuf, options);
325	if (extra)
326		catopt(&optbuf, extra);
327	if (auxopt)
328		catopt(&optbuf, auxopt);
329	else if (flags & DO_BACKGRD)
330		catopt(&optbuf, "-B");
331
332	maxargc = 64;
333	argv = emalloc(sizeof(char *) * maxargc);
334
335	(void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
336	argc = 0;
337	argv[argc++] = execbase;
338	if (optbuf)
339		mangle(optbuf, &argc, &argv, &maxargc);
340	argv[argc++] = spec;
341	argv[argc] = NULL;
342
343	if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
344		(void)printf("start %s %swait", mntpt,
345			pidp ? "no" : "");
346		for (i = 0; i < argc; i++)
347			(void)printf(" %s", argv[i]);
348		(void)printf("\n");
349	}
350
351	switch (pid = vfork()) {
352	case -1:				/* Error. */
353		warn("vfork");
354		if (optbuf)
355			free(optbuf);
356		free(vfstype);
357		return (1);
358
359	case 0:					/* Child. */
360		if ((flags & CHECK_DEBUG) && auxopt == NULL)
361			_exit(0);
362
363		/* Go find an executable. */
364		edir = edirs;
365		do {
366			(void)snprintf(execname,
367			    sizeof(execname), "%s/%s", *edir, execbase);
368			execv(execname, (char * const *)argv);
369			if (errno != ENOENT) {
370				if (spec)
371					warn("exec %s for %s", execname, spec);
372				else
373					warn("exec %s", execname);
374			}
375		} while (*++edir != NULL);
376
377		if (errno == ENOENT) {
378			if (spec)
379				warn("exec %s for %s", execname, spec);
380			else
381				warn("exec %s", execname);
382		}
383		_exit(1);
384		/* NOTREACHED */
385
386	default:				/* Parent. */
387		if (optbuf)
388			free(optbuf);
389
390		free(vfstype);
391
392		if (pidp) {
393			*pidp = pid;
394			return 0;
395		}
396
397		if (waitpid(pid, &status, 0) < 0) {
398			warn("waitpid");
399			return (1);
400		}
401
402		if (WIFEXITED(status)) {
403			if (WEXITSTATUS(status) != 0)
404				return (WEXITSTATUS(status));
405		}
406		else if (WIFSIGNALED(status)) {
407			warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
408			return (1);
409		}
410		break;
411	}
412
413	return (0);
414}
415
416
417static int
418selected(const char *type)
419{
420	struct entry *e;
421
422	/* If no type specified, it's always selected. */
423	TAILQ_FOREACH(e, &selhead, entries)
424		if (!strncmp(e->type, type, MFSNAMELEN))
425			return which == IN_LIST ? 1 : 0;
426
427	return which == IN_LIST ? 0 : 1;
428}
429
430
431static const char *
432getoptions(const char *type)
433{
434	struct entry *e;
435
436	TAILQ_FOREACH(e, &opthead, entries)
437		if (!strncmp(e->type, type, MFSNAMELEN))
438			return e->options;
439	return "";
440}
441
442
443static void
444addoption(char *optstr)
445{
446	char *newoptions;
447	struct entry *e;
448
449	if ((newoptions = strchr(optstr, ':')) == NULL)
450		errx(1, "Invalid option string");
451
452	*newoptions++ = '\0';
453
454	TAILQ_FOREACH(e, &opthead, entries)
455		if (!strncmp(e->type, optstr, MFSNAMELEN)) {
456			catopt(&e->options, newoptions);
457			return;
458		}
459	addentry(&opthead, optstr, newoptions);
460}
461
462
463static void
464addentry(struct fstypelist *list, const char *type, const char *opts)
465{
466	struct entry *e;
467
468	e = emalloc(sizeof(struct entry));
469	e->type = estrdup(type);
470	e->options = estrdup(opts);
471	TAILQ_INSERT_TAIL(list, e, entries);
472}
473
474
475static void
476maketypelist(char *fslist)
477{
478	char *ptr;
479
480	if ((fslist == NULL) || (fslist[0] == '\0'))
481		errx(1, "empty type list");
482
483	if (fslist[0] == 'n' && fslist[1] == 'o') {
484		fslist += 2;
485		which = NOT_IN_LIST;
486	}
487	else
488		which = IN_LIST;
489
490	while ((ptr = strsep(&fslist, ",")) != NULL)
491		addentry(&selhead, ptr, "");
492
493}
494
495
496static void
497catopt(char **sp, const char *o)
498{
499	char *s;
500	size_t i, j;
501
502	s = *sp;
503	if (s) {
504		i = strlen(s);
505		j = i + 1 + strlen(o) + 1;
506		s = erealloc(s, j);
507		(void)snprintf(s + i, j, ",%s", o);
508	} else
509		s = estrdup(o);
510	*sp = s;
511}
512
513
514static void
515mangle(char *options, int *argcp, const char ***argvp, int *maxargcp)
516{
517	char *p, *s;
518	int argc, maxargc;
519	const char **argv;
520
521	argc = *argcp;
522	argv = *argvp;
523	maxargc = *maxargcp;
524
525	for (s = options; (p = strsep(&s, ",")) != NULL;) {
526		/* Always leave space for one more argument and the NULL. */
527		if (argc >= maxargc - 3) {
528			maxargc <<= 1;
529			argv = erealloc(argv, maxargc * sizeof(char *));
530		}
531		if (*p != '\0')  {
532			if (*p == '-') {
533				argv[argc++] = p;
534				p = strchr(p, '=');
535				if (p) {
536					*p = '\0';
537					argv[argc++] = p+1;
538				}
539			} else {
540				argv[argc++] = "-o";
541				argv[argc++] = p;
542			}
543		}
544	}
545
546	*argcp = argc;
547	*argvp = argv;
548	*maxargcp = maxargc;
549}
550
551
552const static char *
553getfslab(const char *str)
554{
555	struct disklabel dl;
556	int fd;
557	char p;
558	const char *vfstype;
559	u_char t;
560
561	/* deduce the file system type from the disk label */
562	if ((fd = open(str, O_RDONLY)) == -1)
563		err(1, "cannot open `%s'", str);
564
565	if (ioctl(fd, DIOCGDINFO, &dl) == -1)
566		return(NULL);
567
568	(void) close(fd);
569
570	p = str[strlen(str) - 1];
571
572	if ((p - 'a') >= dl.d_npartitions)
573		errx(1, "partition `%s' is not defined on disk", str);
574
575	if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES)
576		errx(1, "partition `%s' is not of a legal vfstype",
577		    str);
578
579	if ((vfstype = fstypenames[t]) == NULL)
580		errx(1, "vfstype `%s' on partition `%s' is not supported",
581		    fstypenames[t], str);
582
583	return vfstype;
584}
585
586
587static void
588usage(void)
589{
590	static const char common[] =
591	    "[-BFdpvlyn] [-T fstype:fsoptions] [-t fstype]";
592
593	(void)fprintf(stderr, "usage: %s %s [special|node]...\n",
594	    getprogname(), common);
595	exit(1);
596}
597