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