Home | History | Annotate | Line # | Download | only in fsck
fsck.c revision 1.42
      1 /*	$NetBSD: fsck.c,v 1.42 2006/03/20 01:27:44 christos 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. 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.
     19  *
     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
     30  * SUCH DAMAGE.
     31  *
     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  *
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 #ifndef lint
     39 __RCSID("$NetBSD: fsck.c,v 1.42 2006/03/20 01:27:44 christos Exp $");
     40 #endif /* not lint */
     41 
     42 #include <sys/param.h>
     43 #include <sys/mount.h>
     44 #include <sys/queue.h>
     45 #include <sys/wait.h>
     46 #define FSTYPENAMES
     47 #define FSCKNAMES
     48 #include <sys/disk.h>
     49 #include <sys/disklabel.h>
     50 #include <sys/ioctl.h>
     51 
     52 #include <err.h>
     53 #include <errno.h>
     54 #include <fstab.h>
     55 #include <fcntl.h>
     56 #include <paths.h>
     57 #include <signal.h>
     58 #include <stdio.h>
     59 #include <stdlib.h>
     60 #include <string.h>
     61 #include <unistd.h>
     62 
     63 #include "pathnames.h"
     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 int maxrun = 0;
     77 static char *options = NULL;
     78 static int flags = 0;
     79 
     80 static int checkfs(const char *, const char *, const char *, void *, 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);
     90 static void *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, "dfl:nPpqT:t:vy")) != -1) {
    107 		switch (i) {
    108 		case 'd':
    109 			flags |= CHECK_DEBUG;
    110 			continue;
    111 
    112 		case 'f':
    113 			flags |= CHECK_FORCE;
    114 			break;
    115 
    116 		case 'n':
    117 			flags |= CHECK_NOFIX;
    118 			break;
    119 
    120 		case 'p':
    121 			flags |= CHECK_PREEN;
    122 			break;
    123 
    124 		case 'P':
    125 			flags |= CHECK_PROGRESS;
    126 			break;
    127 
    128 		case 'q':
    129 			break;
    130 
    131 		case 'l':
    132 			maxrun = atoi(optarg);
    133 			continue;
    134 
    135 		case 'T':
    136 			if (*optarg)
    137 				addoption(optarg);
    138 			continue;
    139 
    140 		case 't':
    141 			if (TAILQ_FIRST(&selhead) != NULL)
    142 				errx(1, "only one -t option may be specified.");
    143 
    144 			maketypelist(optarg);
    145 			vfstype = optarg;
    146 			continue;
    147 
    148 		case 'v':
    149 			flags |= CHECK_VERBOSE;
    150 			continue;
    151 
    152 		case 'y':
    153 			break;
    154 
    155 		case '?':
    156 		default:
    157 			usage();
    158 			/* NOTREACHED */
    159 		}
    160 
    161 		/* Pass option to fsck_xxxfs */
    162 		globopt[1] = i;
    163 		catopt(&options, globopt);
    164 	}
    165 
    166 	/* Don't do progress meters if we're debugging. */
    167 	if (flags & CHECK_DEBUG)
    168 		flags &= ~CHECK_PROGRESS;
    169 
    170 	/*
    171 	 * If progress meters are being used, force max parallel to 1
    172 	 * so the progress meter outputs don't interfere with one another.
    173 	 */
    174 	if (flags & CHECK_PROGRESS)
    175 		maxrun = 1;
    176 
    177 	argc -= optind;
    178 	argv += optind;
    179 
    180 	if (argc == 0)
    181 		return checkfstab(flags, maxrun, isok, checkfs);
    182 
    183 #define	BADTYPE(type)							\
    184 	(strcmp(type, FSTAB_RO) &&					\
    185 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
    186 
    187 
    188 	for (; argc--; argv++) {
    189 		const char *spec, *type, *cp;
    190 		char	device[MAXPATHLEN];
    191 
    192 		spec = *argv;
    193 		cp = strrchr(spec, '/');
    194 		if (cp == 0) {
    195 			(void)snprintf(device, sizeof(device), "%s%s",
    196 				_PATH_DEV, spec);
    197 			spec = device;
    198 		}
    199 		if ((fs = getfsfile(spec)) == NULL &&
    200 		    (fs = getfsspec(spec)) == NULL) {
    201 			if (vfstype == NULL)
    202 				vfstype = getfslab(spec);
    203 			type = vfstype;
    204 		}
    205 		else {
    206 			spec = fs->fs_spec;
    207 			type = fs->fs_vfstype;
    208 			if (BADTYPE(fs->fs_type))
    209 				errx(1, "%s has unknown file system type.",
    210 				    spec);
    211 		}
    212 
    213 		rval |= checkfs(type, blockcheck(spec), *argv, NULL, NULL);
    214 	}
    215 
    216 	return rval;
    217 }
    218 
    219 
    220 static void *
    221 isok(struct fstab *fs)
    222 {
    223 
    224 	if (fs->fs_passno == 0)
    225 		return NULL;
    226 
    227 	if (BADTYPE(fs->fs_type))
    228 		return NULL;
    229 
    230 	if (!selected(fs->fs_vfstype))
    231 		return NULL;
    232 
    233 	return fs;
    234 }
    235 
    236 
    237 static int
    238 checkfs(const char *vfstype, const char *spec, const char *mntpt, void *auxarg,
    239     pid_t *pidp)
    240 {
    241 	/* List of directories containing fsck_xxx subcommands. */
    242 	static const char *edirs[] = {
    243 #ifdef RESCUEDIR
    244 		RESCUEDIR,
    245 #endif
    246 		_PATH_SBIN,
    247 		_PATH_USRSBIN,
    248 		NULL
    249 	};
    250 	const char **argv, **edir;
    251 	pid_t pid;
    252 	int argc, i, status, maxargc;
    253 	char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN];
    254 	const char *extra = getoptions(vfstype);
    255 
    256 #ifdef __GNUC__
    257 	/* Avoid vfork clobbering */
    258 	(void) &optbuf;
    259 	(void) &vfstype;
    260 #endif
    261 
    262 	if (!strcmp(vfstype, "ufs"))
    263 		vfstype = MOUNT_UFS;
    264 
    265 	optbuf = NULL;
    266 	if (options)
    267 		catopt(&optbuf, options);
    268 	if (extra)
    269 		catopt(&optbuf, extra);
    270 
    271 	maxargc = 64;
    272 	argv = emalloc(sizeof(char *) * maxargc);
    273 
    274 	(void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
    275 	argc = 0;
    276 	argv[argc++] = execbase;
    277 	if (optbuf)
    278 		mangle(optbuf, &argc, &argv, &maxargc);
    279 	argv[argc++] = spec;
    280 	argv[argc] = NULL;
    281 
    282 	if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
    283 		(void)printf("start %s %swait", mntpt,
    284 			pidp ? "no" : "");
    285 		for (i = 0; i < argc; i++)
    286 			(void)printf(" %s", argv[i]);
    287 		(void)printf("\n");
    288 	}
    289 
    290 	switch (pid = vfork()) {
    291 	case -1:				/* Error. */
    292 		warn("vfork");
    293 		if (optbuf)
    294 			free(optbuf);
    295 		free(argv);
    296 		return (1);
    297 
    298 	case 0:					/* Child. */
    299 		if ((flags & CHECK_FORCE) == 0) {
    300 			struct statvfs	sfs;
    301 
    302 				/*
    303 				 * if mntpt is a mountpoint of a mounted file
    304 				 * system and it's mounted read-write, skip it
    305 				 * unless -f is given.
    306 				 */
    307 			if ((statvfs(mntpt, &sfs) == 0) &&
    308 			    (strcmp(mntpt, sfs.f_mntonname) == 0) &&
    309 			    ((sfs.f_flag & MNT_RDONLY) == 0)) {
    310 				printf(
    311 		"%s: file system is mounted read-write on %s; not checking\n",
    312 				    spec, mntpt);
    313 				if ((flags & CHECK_PREEN) && auxarg != NULL)
    314 					_exit(0);	/* fsck -p */
    315 				else
    316 					_exit(1);	/* fsck [[-p] ...] */
    317 			}
    318 		}
    319 
    320 		if (flags & CHECK_DEBUG)
    321 			_exit(0);
    322 
    323 		/* Go find an executable. */
    324 		edir = edirs;
    325 		do {
    326 			(void)snprintf(execname,
    327 			    sizeof(execname), "%s/%s", *edir, execbase);
    328 			execv(execname, (char * const *)__UNCONST(argv));
    329 			if (errno != ENOENT) {
    330 				if (spec)
    331 					warn("exec %s for %s", execname, spec);
    332 				else
    333 					warn("exec %s", execname);
    334 			}
    335 		} while (*++edir != NULL);
    336 
    337 		if (errno == ENOENT) {
    338 			if (spec)
    339 				warn("exec %s for %s", execname, spec);
    340 			else
    341 				warn("exec %s", execname);
    342 		}
    343 		_exit(1);
    344 		/* NOTREACHED */
    345 
    346 	default:				/* Parent. */
    347 		if (optbuf)
    348 			free(optbuf);
    349 		free(argv);
    350 
    351 		if (pidp) {
    352 			*pidp = pid;
    353 			return 0;
    354 		}
    355 
    356 		if (waitpid(pid, &status, 0) < 0) {
    357 			warn("waitpid");
    358 			return (1);
    359 		}
    360 
    361 		if (WIFEXITED(status)) {
    362 			if (WEXITSTATUS(status) != 0)
    363 				return (WEXITSTATUS(status));
    364 		}
    365 		else if (WIFSIGNALED(status)) {
    366 			warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
    367 			return (1);
    368 		}
    369 		break;
    370 	}
    371 
    372 	return (0);
    373 }
    374 
    375 
    376 static int
    377 selected(const char *type)
    378 {
    379 	struct entry *e;
    380 
    381 	/* If no type specified, it's always selected. */
    382 	TAILQ_FOREACH(e, &selhead, entries)
    383 		if (!strncmp(e->type, type, MFSNAMELEN))
    384 			return which == IN_LIST ? 1 : 0;
    385 
    386 	return which == IN_LIST ? 0 : 1;
    387 }
    388 
    389 
    390 static const char *
    391 getoptions(const char *type)
    392 {
    393 	struct entry *e;
    394 
    395 	TAILQ_FOREACH(e, &opthead, entries)
    396 		if (!strncmp(e->type, type, MFSNAMELEN))
    397 			return e->options;
    398 	return "";
    399 }
    400 
    401 
    402 static void
    403 addoption(char *optstr)
    404 {
    405 	char *newoptions;
    406 	struct entry *e;
    407 
    408 	if ((newoptions = strchr(optstr, ':')) == NULL)
    409 		errx(1, "Invalid option string");
    410 
    411 	*newoptions++ = '\0';
    412 
    413 	TAILQ_FOREACH(e, &opthead, entries)
    414 		if (!strncmp(e->type, optstr, MFSNAMELEN)) {
    415 			catopt(&e->options, newoptions);
    416 			return;
    417 		}
    418 	addentry(&opthead, optstr, newoptions);
    419 }
    420 
    421 
    422 static void
    423 addentry(struct fstypelist *list, const char *type, const char *opts)
    424 {
    425 	struct entry *e;
    426 
    427 	e = emalloc(sizeof(struct entry));
    428 	e->type = estrdup(type);
    429 	e->options = estrdup(opts);
    430 	TAILQ_INSERT_TAIL(list, e, entries);
    431 }
    432 
    433 
    434 static void
    435 maketypelist(char *fslist)
    436 {
    437 	char *ptr;
    438 
    439 	if ((fslist == NULL) || (fslist[0] == '\0'))
    440 		errx(1, "empty type list");
    441 
    442 	if (fslist[0] == 'n' && fslist[1] == 'o') {
    443 		fslist += 2;
    444 		which = NOT_IN_LIST;
    445 	}
    446 	else
    447 		which = IN_LIST;
    448 
    449 	while ((ptr = strsep(&fslist, ",")) != NULL)
    450 		addentry(&selhead, ptr, "");
    451 
    452 }
    453 
    454 
    455 static void
    456 catopt(char **sp, const char *o)
    457 {
    458 	char *s;
    459 	size_t i, j;
    460 
    461 	s = *sp;
    462 	if (s) {
    463 		i = strlen(s);
    464 		j = i + 1 + strlen(o) + 1;
    465 		s = erealloc(s, j);
    466 		(void)snprintf(s + i, j, ",%s", o);
    467 	} else
    468 		s = estrdup(o);
    469 	*sp = s;
    470 }
    471 
    472 
    473 static void
    474 mangle(char *opts, int *argcp, const char ***argvp, int *maxargcp)
    475 {
    476 	char *p, *s;
    477 	int argc, maxargc;
    478 	const char **argv;
    479 
    480 	argc = *argcp;
    481 	argv = *argvp;
    482 	maxargc = *maxargcp;
    483 
    484 	for (s = opts; (p = strsep(&s, ",")) != NULL;) {
    485 		/* Always leave space for one more argument and the NULL. */
    486 		if (argc >= maxargc - 3) {
    487 			maxargc <<= 1;
    488 			argv = erealloc(argv, maxargc * sizeof(char *));
    489 		}
    490 		if (*p != '\0')  {
    491 			if (*p == '-') {
    492 				argv[argc++] = p;
    493 				p = strchr(p, '=');
    494 				if (p) {
    495 					*p = '\0';
    496 					argv[argc++] = p+1;
    497 				}
    498 			} else {
    499 				argv[argc++] = "-o";
    500 				argv[argc++] = p;
    501 			}
    502 		}
    503 	}
    504 
    505 	*argcp = argc;
    506 	*argvp = argv;
    507 	*maxargcp = maxargc;
    508 }
    509 
    510 const static char *
    511 getfslab(const char *str)
    512 {
    513 	static struct dkwedge_info dkw;
    514 	struct disklabel dl;
    515 	int fd;
    516 	char p;
    517 	const char *vfstype;
    518 	u_char t;
    519 
    520 	/* deduce the file system type from the disk label */
    521 	if ((fd = open(str, O_RDONLY)) == -1)
    522 		err(1, "cannot open `%s'", str);
    523 
    524 	/* First check to see if it's a wedge. */
    525 	if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) {
    526 		/* Yup, this is easy. */
    527 		(void) close(fd);
    528 		return (dkw.dkw_ptype);
    529 	}
    530 
    531 	if (ioctl(fd, DIOCGDINFO, &dl) == -1)
    532 		err(1, "cannot get disklabel for `%s'", str);
    533 
    534 	(void) close(fd);
    535 
    536 	p = str[strlen(str) - 1];
    537 
    538 	if ((p - 'a') >= dl.d_npartitions)
    539 		errx(1, "partition `%s' is not defined on disk", str);
    540 
    541 	if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES)
    542 		errx(1, "partition `%s' is not of a legal vfstype",
    543 		    str);
    544 
    545 	if ((vfstype = fscknames[t]) == NULL)
    546 		errx(1, "vfstype `%s' on partition `%s' is not supported",
    547 		    fstypenames[t], str);
    548 
    549 	return vfstype;
    550 }
    551 
    552 
    553 static void
    554 usage(void)
    555 {
    556 	static const char common[] =
    557 	    "[-dfnPpqvy] [-l maxparallel] [-T fstype:fsoptions]\n\t\t[-t fstype]";
    558 
    559 	(void)fprintf(stderr, "usage: %s %s [special|node]...\n",
    560 	    getprogname(), common);
    561 	exit(1);
    562 }
    563