Home | History | Annotate | Line # | Download | only in fsck
fsck.c revision 1.40
      1 /*	$NetBSD: fsck.c,v 1.40 2005/01/19 17:48:15 xtraeme 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.40 2005/01/19 17:48:15 xtraeme 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 		return (1);
    296 
    297 	case 0:					/* Child. */
    298 		if ((flags & CHECK_FORCE) == 0) {
    299 			struct statvfs	sfs;
    300 
    301 				/*
    302 				 * if mntpt is a mountpoint of a mounted file
    303 				 * system and it's mounted read-write, skip it
    304 				 * unless -f is given.
    305 				 */
    306 			if ((statvfs(mntpt, &sfs) == 0) &&
    307 			    (strcmp(mntpt, sfs.f_mntonname) == 0) &&
    308 			    ((sfs.f_flag & MNT_RDONLY) == 0)) {
    309 				printf(
    310 		"%s: file system is mounted read-write on %s; not checking\n",
    311 				    spec, mntpt);
    312 				if ((flags & CHECK_PREEN) && auxarg != NULL)
    313 					_exit(0);	/* fsck -p */
    314 				else
    315 					_exit(1);	/* fsck [[-p] ...] */
    316 			}
    317 		}
    318 
    319 		if (flags & CHECK_DEBUG)
    320 			_exit(0);
    321 
    322 		/* Go find an executable. */
    323 		edir = edirs;
    324 		do {
    325 			(void)snprintf(execname,
    326 			    sizeof(execname), "%s/%s", *edir, execbase);
    327 			execv(execname, (char * const *)argv);
    328 			if (errno != ENOENT) {
    329 				if (spec)
    330 					warn("exec %s for %s", execname, spec);
    331 				else
    332 					warn("exec %s", execname);
    333 			}
    334 		} while (*++edir != NULL);
    335 
    336 		if (errno == ENOENT) {
    337 			if (spec)
    338 				warn("exec %s for %s", execname, spec);
    339 			else
    340 				warn("exec %s", execname);
    341 		}
    342 		_exit(1);
    343 		/* NOTREACHED */
    344 
    345 	default:				/* Parent. */
    346 		if (optbuf)
    347 			free(optbuf);
    348 
    349 		if (pidp) {
    350 			*pidp = pid;
    351 			return 0;
    352 		}
    353 
    354 		if (waitpid(pid, &status, 0) < 0) {
    355 			warn("waitpid");
    356 			return (1);
    357 		}
    358 
    359 		if (WIFEXITED(status)) {
    360 			if (WEXITSTATUS(status) != 0)
    361 				return (WEXITSTATUS(status));
    362 		}
    363 		else if (WIFSIGNALED(status)) {
    364 			warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
    365 			return (1);
    366 		}
    367 		break;
    368 	}
    369 
    370 	return (0);
    371 }
    372 
    373 
    374 static int
    375 selected(const char *type)
    376 {
    377 	struct entry *e;
    378 
    379 	/* If no type specified, it's always selected. */
    380 	TAILQ_FOREACH(e, &selhead, entries)
    381 		if (!strncmp(e->type, type, MFSNAMELEN))
    382 			return which == IN_LIST ? 1 : 0;
    383 
    384 	return which == IN_LIST ? 0 : 1;
    385 }
    386 
    387 
    388 static const char *
    389 getoptions(const char *type)
    390 {
    391 	struct entry *e;
    392 
    393 	TAILQ_FOREACH(e, &opthead, entries)
    394 		if (!strncmp(e->type, type, MFSNAMELEN))
    395 			return e->options;
    396 	return "";
    397 }
    398 
    399 
    400 static void
    401 addoption(char *optstr)
    402 {
    403 	char *newoptions;
    404 	struct entry *e;
    405 
    406 	if ((newoptions = strchr(optstr, ':')) == NULL)
    407 		errx(1, "Invalid option string");
    408 
    409 	*newoptions++ = '\0';
    410 
    411 	TAILQ_FOREACH(e, &opthead, entries)
    412 		if (!strncmp(e->type, optstr, MFSNAMELEN)) {
    413 			catopt(&e->options, newoptions);
    414 			return;
    415 		}
    416 	addentry(&opthead, optstr, newoptions);
    417 }
    418 
    419 
    420 static void
    421 addentry(struct fstypelist *list, const char *type, const char *opts)
    422 {
    423 	struct entry *e;
    424 
    425 	e = emalloc(sizeof(struct entry));
    426 	e->type = estrdup(type);
    427 	e->options = estrdup(opts);
    428 	TAILQ_INSERT_TAIL(list, e, entries);
    429 }
    430 
    431 
    432 static void
    433 maketypelist(char *fslist)
    434 {
    435 	char *ptr;
    436 
    437 	if ((fslist == NULL) || (fslist[0] == '\0'))
    438 		errx(1, "empty type list");
    439 
    440 	if (fslist[0] == 'n' && fslist[1] == 'o') {
    441 		fslist += 2;
    442 		which = NOT_IN_LIST;
    443 	}
    444 	else
    445 		which = IN_LIST;
    446 
    447 	while ((ptr = strsep(&fslist, ",")) != NULL)
    448 		addentry(&selhead, ptr, "");
    449 
    450 }
    451 
    452 
    453 static void
    454 catopt(char **sp, const char *o)
    455 {
    456 	char *s;
    457 	size_t i, j;
    458 
    459 	s = *sp;
    460 	if (s) {
    461 		i = strlen(s);
    462 		j = i + 1 + strlen(o) + 1;
    463 		s = erealloc(s, j);
    464 		(void)snprintf(s + i, j, ",%s", o);
    465 	} else
    466 		s = estrdup(o);
    467 	*sp = s;
    468 }
    469 
    470 
    471 static void
    472 mangle(char *opts, int *argcp, const char ***argvp, int *maxargcp)
    473 {
    474 	char *p, *s;
    475 	int argc, maxargc;
    476 	const char **argv;
    477 
    478 	argc = *argcp;
    479 	argv = *argvp;
    480 	maxargc = *maxargcp;
    481 
    482 	for (s = opts; (p = strsep(&s, ",")) != NULL;) {
    483 		/* Always leave space for one more argument and the NULL. */
    484 		if (argc >= maxargc - 3) {
    485 			maxargc <<= 1;
    486 			argv = erealloc(argv, maxargc * sizeof(char *));
    487 		}
    488 		if (*p != '\0')  {
    489 			if (*p == '-') {
    490 				argv[argc++] = p;
    491 				p = strchr(p, '=');
    492 				if (p) {
    493 					*p = '\0';
    494 					argv[argc++] = p+1;
    495 				}
    496 			} else {
    497 				argv[argc++] = "-o";
    498 				argv[argc++] = p;
    499 			}
    500 		}
    501 	}
    502 
    503 	*argcp = argc;
    504 	*argvp = argv;
    505 	*maxargcp = maxargc;
    506 }
    507 
    508 const static char *
    509 getfslab(const char *str)
    510 {
    511 	static struct dkwedge_info dkw;
    512 	struct disklabel dl;
    513 	int fd;
    514 	char p;
    515 	const char *vfstype;
    516 	u_char t;
    517 
    518 	/* deduce the file system type from the disk label */
    519 	if ((fd = open(str, O_RDONLY)) == -1)
    520 		err(1, "cannot open `%s'", str);
    521 
    522 	/* First check to see if it's a wedge. */
    523 	if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) {
    524 		/* Yup, this is easy. */
    525 		(void) close(fd);
    526 		return (dkw.dkw_ptype);
    527 	}
    528 
    529 	if (ioctl(fd, DIOCGDINFO, &dl) == -1)
    530 		err(1, "cannot get disklabel for `%s'", str);
    531 
    532 	(void) close(fd);
    533 
    534 	p = str[strlen(str) - 1];
    535 
    536 	if ((p - 'a') >= dl.d_npartitions)
    537 		errx(1, "partition `%s' is not defined on disk", str);
    538 
    539 	if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES)
    540 		errx(1, "partition `%s' is not of a legal vfstype",
    541 		    str);
    542 
    543 	if ((vfstype = fscknames[t]) == NULL)
    544 		errx(1, "vfstype `%s' on partition `%s' is not supported",
    545 		    fstypenames[t], str);
    546 
    547 	return vfstype;
    548 }
    549 
    550 
    551 static void
    552 usage(void)
    553 {
    554 	static const char common[] =
    555 	    "[-dfnPpqvy] [-l maxparallel] [-T fstype:fsoptions]\n\t\t[-t fstype]";
    556 
    557 	(void)fprintf(stderr, "usage: %s %s [special|node]...\n",
    558 	    getprogname(), common);
    559 	exit(1);
    560 }
    561