fsck.c revision 1.29 1 1.29 lukem /* $NetBSD: fsck.c,v 1.29 2002/08/23 03:17:18 lukem Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 1996 Christos Zoulas. All rights reserved.
5 1.1 christos * Copyright (c) 1980, 1989, 1993, 1994
6 1.1 christos * The Regents of the University of California. All rights reserved.
7 1.1 christos *
8 1.1 christos * Redistribution and use in source and binary forms, with or without
9 1.1 christos * modification, are permitted provided that the following conditions
10 1.1 christos * are met:
11 1.1 christos * 1. Redistributions of source code must retain the above copyright
12 1.1 christos * notice, this list of conditions and the following disclaimer.
13 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer in the
15 1.1 christos * documentation and/or other materials provided with the distribution.
16 1.1 christos * 3. All advertising materials mentioning features or use of this software
17 1.1 christos * must display the following acknowledgement:
18 1.1 christos * This product includes software developed by the University of
19 1.1 christos * California, Berkeley and its contributors.
20 1.1 christos * 4. Neither the name of the University nor the names of its contributors
21 1.1 christos * may be used to endorse or promote products derived from this software
22 1.1 christos * without specific prior written permission.
23 1.1 christos *
24 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 christos * SUCH DAMAGE.
35 1.1 christos *
36 1.1 christos * From: @(#)mount.c 8.19 (Berkeley) 4/19/94
37 1.1 christos * From: NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp
38 1.1 christos *
39 1.1 christos */
40 1.1 christos
41 1.12 lukem #include <sys/cdefs.h>
42 1.12 lukem #ifndef lint
43 1.29 lukem __RCSID("$NetBSD: fsck.c,v 1.29 2002/08/23 03:17:18 lukem Exp $");
44 1.12 lukem #endif /* not lint */
45 1.1 christos
46 1.1 christos #include <sys/param.h>
47 1.1 christos #include <sys/mount.h>
48 1.1 christos #include <sys/queue.h>
49 1.1 christos #include <sys/wait.h>
50 1.20 christos #define FSTYPENAMES
51 1.20 christos #define FSCKNAMES
52 1.8 christos #include <sys/disklabel.h>
53 1.8 christos #include <sys/ioctl.h>
54 1.1 christos
55 1.1 christos #include <err.h>
56 1.1 christos #include <errno.h>
57 1.1 christos #include <fstab.h>
58 1.8 christos #include <fcntl.h>
59 1.21 abs #include <paths.h>
60 1.1 christos #include <signal.h>
61 1.1 christos #include <stdio.h>
62 1.1 christos #include <stdlib.h>
63 1.1 christos #include <string.h>
64 1.1 christos #include <unistd.h>
65 1.1 christos
66 1.1 christos #include "pathnames.h"
67 1.4 christos #include "fsutil.h"
68 1.1 christos
69 1.2 christos static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
70 1.1 christos
71 1.2 christos TAILQ_HEAD(fstypelist, entry) opthead, selhead;
72 1.1 christos
73 1.1 christos struct entry {
74 1.1 christos char *type;
75 1.2 christos char *options;
76 1.1 christos TAILQ_ENTRY(entry) entries;
77 1.1 christos };
78 1.1 christos
79 1.4 christos static int maxrun = 0;
80 1.2 christos static char *options = NULL;
81 1.4 christos static int flags = 0;
82 1.2 christos
83 1.25 lukem int main(int, char *[]);
84 1.1 christos
85 1.25 lukem static int checkfs(const char *, const char *, const char *, void *, pid_t *);
86 1.25 lukem static int selected(const char *);
87 1.25 lukem static void addoption(char *);
88 1.25 lukem static const char *getoptions(const char *);
89 1.25 lukem static void addentry(struct fstypelist *, const char *, const char *);
90 1.25 lukem static void maketypelist(char *);
91 1.25 lukem static void catopt(char **, const char *);
92 1.25 lukem static void mangle(char *, int *, const char ***, int *);
93 1.25 lukem static const char *getfslab(const char *);
94 1.25 lukem static void usage(void);
95 1.25 lukem static void *isok(struct fstab *);
96 1.1 christos
97 1.1 christos int
98 1.25 lukem main(int argc, char *argv[])
99 1.1 christos {
100 1.1 christos struct fstab *fs;
101 1.1 christos int i, rval = 0;
102 1.17 mycroft const char *vfstype = NULL;
103 1.7 christos char globopt[3];
104 1.7 christos
105 1.7 christos globopt[0] = '-';
106 1.7 christos globopt[2] = '\0';
107 1.1 christos
108 1.2 christos TAILQ_INIT(&selhead);
109 1.2 christos TAILQ_INIT(&opthead);
110 1.2 christos
111 1.7 christos while ((i = getopt(argc, argv, "dvpfnyl:t:T:")) != -1)
112 1.1 christos switch (i) {
113 1.1 christos case 'd':
114 1.4 christos flags |= CHECK_DEBUG;
115 1.1 christos break;
116 1.1 christos
117 1.1 christos case 'v':
118 1.4 christos flags |= CHECK_VERBOSE;
119 1.1 christos break;
120 1.1 christos
121 1.7 christos case 'p':
122 1.26 lukem case 'f':
123 1.26 lukem if (i == 'p')
124 1.26 lukem flags |= CHECK_PREEN;
125 1.26 lukem else
126 1.26 lukem flags |= CHECK_FORCE;
127 1.7 christos /*FALLTHROUGH*/
128 1.7 christos case 'n':
129 1.1 christos case 'y':
130 1.7 christos globopt[1] = i;
131 1.16 mycroft catopt(&options, globopt);
132 1.2 christos break;
133 1.2 christos
134 1.2 christos case 'l':
135 1.2 christos maxrun = atoi(optarg);
136 1.1 christos break;
137 1.1 christos
138 1.2 christos case 'T':
139 1.1 christos if (*optarg)
140 1.2 christos addoption(optarg);
141 1.1 christos break;
142 1.1 christos
143 1.1 christos case 't':
144 1.24 lukem if (TAILQ_FIRST(&selhead) != NULL)
145 1.1 christos errx(1, "only one -t option may be specified.");
146 1.2 christos
147 1.1 christos maketypelist(optarg);
148 1.1 christos vfstype = optarg;
149 1.1 christos break;
150 1.1 christos
151 1.1 christos case '?':
152 1.1 christos default:
153 1.1 christos usage();
154 1.1 christos /* NOTREACHED */
155 1.1 christos }
156 1.1 christos
157 1.1 christos argc -= optind;
158 1.1 christos argv += optind;
159 1.1 christos
160 1.2 christos if (argc == 0)
161 1.4 christos return checkfstab(flags, maxrun, isok, checkfs);
162 1.2 christos
163 1.1 christos #define BADTYPE(type) \
164 1.1 christos (strcmp(type, FSTAB_RO) && \
165 1.1 christos strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
166 1.1 christos
167 1.1 christos
168 1.1 christos for (; argc--; argv++) {
169 1.21 abs const char *spec, *type, *cp;
170 1.21 abs char device[MAXPATHLEN];
171 1.1 christos
172 1.21 abs spec = *argv;
173 1.21 abs cp = strrchr(spec, '/');
174 1.21 abs if (cp == 0) {
175 1.21 abs (void)snprintf(device, sizeof(device), "%s%s",
176 1.21 abs _PATH_DEV, spec);
177 1.21 abs spec = device;
178 1.21 abs }
179 1.21 abs if ((fs = getfsfile(spec)) == NULL &&
180 1.21 abs (fs = getfsspec(spec)) == NULL) {
181 1.1 christos if (vfstype == NULL)
182 1.21 abs vfstype = getfslab(spec);
183 1.1 christos type = vfstype;
184 1.1 christos }
185 1.1 christos else {
186 1.1 christos spec = fs->fs_spec;
187 1.1 christos type = fs->fs_vfstype;
188 1.1 christos if (BADTYPE(fs->fs_type))
189 1.1 christos errx(1, "%s has unknown file system type.",
190 1.21 abs spec);
191 1.1 christos }
192 1.1 christos
193 1.6 christos rval |= checkfs(type, blockcheck(spec), *argv, NULL, NULL);
194 1.1 christos }
195 1.1 christos
196 1.1 christos return rval;
197 1.1 christos }
198 1.1 christos
199 1.2 christos
200 1.4 christos static void *
201 1.25 lukem isok(struct fstab *fs)
202 1.2 christos {
203 1.25 lukem
204 1.2 christos if (fs->fs_passno == 0)
205 1.4 christos return NULL;
206 1.2 christos
207 1.2 christos if (BADTYPE(fs->fs_type))
208 1.4 christos return NULL;
209 1.2 christos
210 1.2 christos if (!selected(fs->fs_vfstype))
211 1.4 christos return NULL;
212 1.2 christos
213 1.4 christos return fs;
214 1.2 christos }
215 1.2 christos
216 1.2 christos
217 1.1 christos static int
218 1.25 lukem checkfs(const char *vfstype, const char *spec, const char *mntpt, void *auxarg,
219 1.25 lukem pid_t *pidp)
220 1.1 christos {
221 1.1 christos /* List of directories containing fsck_xxx subcommands. */
222 1.1 christos static const char *edirs[] = {
223 1.29 lukem #ifdef _PATH_RESCUE
224 1.29 lukem _PATH_RESCUE,
225 1.29 lukem #endif
226 1.1 christos _PATH_SBIN,
227 1.1 christos _PATH_USRSBIN,
228 1.1 christos NULL
229 1.1 christos };
230 1.7 christos const char **argv, **edir;
231 1.1 christos pid_t pid;
232 1.15 mycroft int argc, i, status, maxargc;
233 1.15 mycroft char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN];
234 1.2 christos const char *extra = getoptions(vfstype);
235 1.1 christos
236 1.1 christos #ifdef __GNUC__
237 1.1 christos /* Avoid vfork clobbering */
238 1.1 christos (void) &optbuf;
239 1.8 christos (void) &vfstype;
240 1.1 christos #endif
241 1.1 christos
242 1.15 mycroft if (!strcmp(vfstype, "ufs"))
243 1.3 cgd vfstype = MOUNT_UFS;
244 1.3 cgd
245 1.16 mycroft optbuf = NULL;
246 1.16 mycroft if (options)
247 1.16 mycroft catopt(&optbuf, options);
248 1.16 mycroft if (extra)
249 1.16 mycroft catopt(&optbuf, extra);
250 1.2 christos
251 1.15 mycroft maxargc = 64;
252 1.15 mycroft argv = emalloc(sizeof(char *) * maxargc);
253 1.15 mycroft
254 1.15 mycroft (void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
255 1.15 mycroft argc = 0;
256 1.15 mycroft argv[argc++] = execbase;
257 1.2 christos if (optbuf)
258 1.7 christos mangle(optbuf, &argc, &argv, &maxargc);
259 1.2 christos argv[argc++] = spec;
260 1.7 christos argv[argc] = NULL;
261 1.1 christos
262 1.4 christos if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
263 1.8 christos (void)printf("start %s %swait", mntpt,
264 1.8 christos pidp ? "no" : "");
265 1.8 christos for (i = 0; i < argc; i++)
266 1.1 christos (void)printf(" %s", argv[i]);
267 1.1 christos (void)printf("\n");
268 1.1 christos }
269 1.1 christos
270 1.1 christos switch (pid = vfork()) {
271 1.1 christos case -1: /* Error. */
272 1.1 christos warn("vfork");
273 1.1 christos if (optbuf)
274 1.1 christos free(optbuf);
275 1.1 christos return (1);
276 1.1 christos
277 1.1 christos case 0: /* Child. */
278 1.26 lukem if ((flags & CHECK_FORCE) == 0) {
279 1.26 lukem struct statfs sfs;
280 1.26 lukem
281 1.26 lukem /*
282 1.26 lukem * if mntpt is a mountpoint of a mounted file
283 1.26 lukem * system and it's mounted read-write, skip it
284 1.26 lukem * unless -f is given.
285 1.26 lukem */
286 1.26 lukem if ((statfs(mntpt, &sfs) == 0) &&
287 1.26 lukem (strcmp(mntpt, sfs.f_mntonname) == 0) &&
288 1.26 lukem ((sfs.f_flags & MNT_RDONLY) == 0)) {
289 1.26 lukem printf(
290 1.26 lukem "%s: file system is mounted read-write on %s; not checking\n",
291 1.26 lukem spec, mntpt);
292 1.26 lukem if ((flags & CHECK_PREEN) && auxarg != NULL)
293 1.26 lukem _exit(0); /* fsck -p */
294 1.26 lukem else
295 1.26 lukem _exit(1); /* fsck [[-p] ...] */
296 1.26 lukem }
297 1.26 lukem }
298 1.26 lukem
299 1.4 christos if (flags & CHECK_DEBUG)
300 1.2 christos _exit(0);
301 1.2 christos
302 1.1 christos /* Go find an executable. */
303 1.1 christos edir = edirs;
304 1.1 christos do {
305 1.1 christos (void)snprintf(execname,
306 1.8 christos sizeof(execname), "%s/%s", *edir, execbase);
307 1.1 christos execv(execname, (char * const *)argv);
308 1.19 ross if (errno != ENOENT) {
309 1.1 christos if (spec)
310 1.1 christos warn("exec %s for %s", execname, spec);
311 1.1 christos else
312 1.1 christos warn("exec %s", execname);
313 1.19 ross }
314 1.1 christos } while (*++edir != NULL);
315 1.1 christos
316 1.19 ross if (errno == ENOENT) {
317 1.1 christos if (spec)
318 1.1 christos warn("exec %s for %s", execname, spec);
319 1.1 christos else
320 1.1 christos warn("exec %s", execname);
321 1.19 ross }
322 1.8 christos _exit(1);
323 1.1 christos /* NOTREACHED */
324 1.1 christos
325 1.1 christos default: /* Parent. */
326 1.1 christos if (optbuf)
327 1.1 christos free(optbuf);
328 1.1 christos
329 1.2 christos if (pidp) {
330 1.2 christos *pidp = pid;
331 1.2 christos return 0;
332 1.2 christos }
333 1.2 christos
334 1.1 christos if (waitpid(pid, &status, 0) < 0) {
335 1.1 christos warn("waitpid");
336 1.1 christos return (1);
337 1.1 christos }
338 1.1 christos
339 1.1 christos if (WIFEXITED(status)) {
340 1.1 christos if (WEXITSTATUS(status) != 0)
341 1.1 christos return (WEXITSTATUS(status));
342 1.1 christos }
343 1.1 christos else if (WIFSIGNALED(status)) {
344 1.1 christos warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
345 1.1 christos return (1);
346 1.1 christos }
347 1.1 christos break;
348 1.1 christos }
349 1.1 christos
350 1.1 christos return (0);
351 1.1 christos }
352 1.1 christos
353 1.1 christos
354 1.1 christos static int
355 1.25 lukem selected(const char *type)
356 1.1 christos {
357 1.1 christos struct entry *e;
358 1.1 christos
359 1.1 christos /* If no type specified, it's always selected. */
360 1.24 lukem TAILQ_FOREACH(e, &selhead, entries)
361 1.1 christos if (!strncmp(e->type, type, MFSNAMELEN))
362 1.1 christos return which == IN_LIST ? 1 : 0;
363 1.1 christos
364 1.1 christos return which == IN_LIST ? 0 : 1;
365 1.1 christos }
366 1.1 christos
367 1.1 christos
368 1.2 christos static const char *
369 1.25 lukem getoptions(const char *type)
370 1.1 christos {
371 1.1 christos struct entry *e;
372 1.1 christos
373 1.24 lukem TAILQ_FOREACH(e, &opthead, entries)
374 1.2 christos if (!strncmp(e->type, type, MFSNAMELEN))
375 1.2 christos return e->options;
376 1.2 christos return "";
377 1.1 christos }
378 1.1 christos
379 1.1 christos
380 1.1 christos static void
381 1.25 lukem addoption(char *optstr)
382 1.1 christos {
383 1.2 christos char *newoptions;
384 1.1 christos struct entry *e;
385 1.1 christos
386 1.2 christos if ((newoptions = strchr(optstr, ':')) == NULL)
387 1.2 christos errx(1, "Invalid option string");
388 1.2 christos
389 1.2 christos *newoptions++ = '\0';
390 1.2 christos
391 1.24 lukem TAILQ_FOREACH(e, &opthead, entries)
392 1.2 christos if (!strncmp(e->type, optstr, MFSNAMELEN)) {
393 1.16 mycroft catopt(&e->options, newoptions);
394 1.1 christos return;
395 1.1 christos }
396 1.2 christos addentry(&opthead, optstr, newoptions);
397 1.1 christos }
398 1.1 christos
399 1.1 christos
400 1.1 christos static void
401 1.25 lukem addentry(struct fstypelist *list, const char *type, const char *opts)
402 1.1 christos {
403 1.2 christos struct entry *e;
404 1.2 christos
405 1.2 christos e = emalloc(sizeof(struct entry));
406 1.2 christos e->type = estrdup(type);
407 1.2 christos e->options = estrdup(opts);
408 1.2 christos TAILQ_INSERT_TAIL(list, e, entries);
409 1.1 christos }
410 1.1 christos
411 1.1 christos
412 1.1 christos static void
413 1.25 lukem maketypelist(char *fslist)
414 1.1 christos {
415 1.1 christos char *ptr;
416 1.1 christos
417 1.1 christos if ((fslist == NULL) || (fslist[0] == '\0'))
418 1.1 christos errx(1, "empty type list");
419 1.1 christos
420 1.1 christos if (fslist[0] == 'n' && fslist[1] == 'o') {
421 1.1 christos fslist += 2;
422 1.1 christos which = NOT_IN_LIST;
423 1.1 christos }
424 1.1 christos else
425 1.1 christos which = IN_LIST;
426 1.1 christos
427 1.1 christos while ((ptr = strsep(&fslist, ",")) != NULL)
428 1.2 christos addentry(&selhead, ptr, "");
429 1.1 christos
430 1.1 christos }
431 1.1 christos
432 1.1 christos
433 1.16 mycroft static void
434 1.25 lukem catopt(char **sp, const char *o)
435 1.1 christos {
436 1.16 mycroft char *s;
437 1.16 mycroft size_t i, j;
438 1.1 christos
439 1.16 mycroft s = *sp;
440 1.16 mycroft if (s) {
441 1.16 mycroft i = strlen(s);
442 1.16 mycroft j = i + 1 + strlen(o) + 1;
443 1.16 mycroft s = erealloc(s, j);
444 1.16 mycroft (void)snprintf(s + i, j, ",%s", o);
445 1.15 mycroft } else
446 1.16 mycroft s = estrdup(o);
447 1.16 mycroft *sp = s;
448 1.1 christos }
449 1.1 christos
450 1.1 christos
451 1.1 christos static void
452 1.27 lukem mangle(char *opts, int *argcp, const char ***argvp, int *maxargcp)
453 1.1 christos {
454 1.1 christos char *p, *s;
455 1.15 mycroft int argc, maxargc;
456 1.15 mycroft const char **argv;
457 1.1 christos
458 1.1 christos argc = *argcp;
459 1.15 mycroft argv = *argvp;
460 1.7 christos maxargc = *maxargcp;
461 1.7 christos
462 1.27 lukem for (s = opts; (p = strsep(&s, ",")) != NULL;) {
463 1.15 mycroft /* Always leave space for one more argument and the NULL. */
464 1.7 christos if (argc >= maxargc - 3) {
465 1.15 mycroft maxargc <<= 1;
466 1.7 christos argv = erealloc(argv, maxargc * sizeof(char *));
467 1.7 christos }
468 1.19 ross if (*p != '\0') {
469 1.1 christos if (*p == '-') {
470 1.1 christos argv[argc++] = p;
471 1.1 christos p = strchr(p, '=');
472 1.1 christos if (p) {
473 1.1 christos *p = '\0';
474 1.1 christos argv[argc++] = p+1;
475 1.1 christos }
476 1.15 mycroft } else {
477 1.1 christos argv[argc++] = "-o";
478 1.1 christos argv[argc++] = p;
479 1.1 christos }
480 1.19 ross }
481 1.7 christos }
482 1.1 christos
483 1.1 christos *argcp = argc;
484 1.7 christos *argvp = argv;
485 1.7 christos *maxargcp = maxargc;
486 1.1 christos }
487 1.8 christos
488 1.8 christos
489 1.17 mycroft const static char *
490 1.25 lukem getfslab(const char *str)
491 1.8 christos {
492 1.8 christos struct disklabel dl;
493 1.8 christos int fd;
494 1.17 mycroft char p;
495 1.17 mycroft const char *vfstype;
496 1.8 christos u_char t;
497 1.8 christos
498 1.26 lukem /* deduce the file system type from the disk label */
499 1.8 christos if ((fd = open(str, O_RDONLY)) == -1)
500 1.8 christos err(1, "cannot open `%s'", str);
501 1.8 christos
502 1.8 christos if (ioctl(fd, DIOCGDINFO, &dl) == -1)
503 1.8 christos err(1, "cannot get disklabel for `%s'", str);
504 1.8 christos
505 1.8 christos (void) close(fd);
506 1.8 christos
507 1.8 christos p = str[strlen(str) - 1];
508 1.8 christos
509 1.8 christos if ((p - 'a') >= dl.d_npartitions)
510 1.8 christos errx(1, "partition `%s' is not defined on disk", str);
511 1.8 christos
512 1.13 bouyer if ((t = dl.d_partitions[p - 'a'].p_fstype) >= FSMAXTYPES)
513 1.8 christos errx(1, "partition `%s' is not of a legal vfstype",
514 1.11 mikel str);
515 1.8 christos
516 1.8 christos if ((vfstype = fscknames[t]) == NULL)
517 1.8 christos errx(1, "vfstype `%s' on partition `%s' is not supported",
518 1.8 christos fstypenames[t], str);
519 1.8 christos
520 1.8 christos return vfstype;
521 1.8 christos }
522 1.1 christos
523 1.1 christos
524 1.1 christos static void
525 1.25 lukem usage(void)
526 1.1 christos {
527 1.2 christos static const char common[] =
528 1.28 soren "[-dfnpvy] [-T fstype:fsoptions] [-t fstype]";
529 1.1 christos
530 1.4 christos (void)fprintf(stderr, "Usage: %s %s [special|node]...\n",
531 1.23 cgd getprogname(), common);
532 1.1 christos exit(1);
533 1.1 christos }
534