]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/fsck/fsck.c
Add PSCI affinity info return values
[FreeBSD/FreeBSD.git] / sbin / fsck / fsck.c
1 /*      $NetBSD: fsck.c,v 1.30 2003/08/07 10:04:15 agc Exp $    */
2
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 1996 Christos Zoulas. All rights reserved.
7  * Copyright (c) 1980, 1989, 1993, 1994
8  *      The Regents of the University of California.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * From: @(#)mount.c    8.19 (Berkeley) 4/19/94
35  * From: $NetBSD: mount.c,v 1.24 1995/11/18 03:34:29 cgd Exp 
36  * $NetBSD: fsck.c,v 1.30 2003/08/07 10:04:15 agc Exp $
37  */
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 #include <sys/param.h>
43 #include <sys/mount.h>
44 #include <sys/queue.h>
45 #include <sys/wait.h>
46 #include <sys/disk.h>
47 #include <sys/ioctl.h>
48
49 #include <ctype.h>
50 #include <err.h>
51 #include <fstab.h>
52 #include <fcntl.h>
53 #include <mntopts.h>
54 #include <paths.h>
55 #include <signal.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60
61 #include "fsutil.h"
62
63 static enum { IN_LIST, NOT_IN_LIST } which = NOT_IN_LIST;
64
65 static TAILQ_HEAD(fstypelist, entry) opthead, selhead;
66
67 struct entry {
68         char *type;
69         char *options;
70         TAILQ_ENTRY(entry) entries;
71 };
72
73 static char *options = NULL;
74 static int flags = 0;
75 static int forceflag = 0;
76
77 static int checkfs(const char *, const char *, const char *, const char *, pid_t *);
78 static int selected(const char *);
79 static void addoption(char *);
80 static const char *getoptions(const char *);
81 static void addentry(struct fstypelist *, const char *, const char *);
82 static void maketypelist(char *);
83 static void catopt(char **, const char *);
84 static void mangle(char *, int *, const char ** volatile *, int *);
85 static const char *getfstype(const char *);
86 static void usage(void) __dead2;
87 static int isok(struct fstab *);
88
89 static struct {
90         const char *ptype;
91         const char *name;
92 } ptype_map[] = {
93         { "ufs",        "ffs" },
94         { "ffs",        "ffs" },
95         { "fat",        "msdosfs" },
96         { "efi",        "msdosfs" },
97         { NULL,         NULL },
98 };
99
100 int
101 main(int argc, char *argv[])
102 {
103         struct fstab *fs;
104         int i, rval = 0;
105         const char *vfstype = NULL;
106         char globopt[3];
107         const char *etc_fstab;
108
109         globopt[0] = '-';
110         globopt[2] = '\0';
111
112         TAILQ_INIT(&selhead);
113         TAILQ_INIT(&opthead);
114
115         etc_fstab = NULL;
116         while ((i = getopt(argc, argv, "BCdvpfFnyl:t:T:c:")) != -1)
117                 switch (i) {
118                 case 'B':
119                         if (flags & CHECK_BACKGRD)
120                                 errx(1, "Cannot specify -B and -F.");
121                         flags |= DO_BACKGRD;
122                         break;
123
124                 case 'd':
125                         flags |= CHECK_DEBUG;
126                         break;
127
128                 case 'v':
129                         flags |= CHECK_VERBOSE;
130                         break;
131
132                 case 'F':
133                         if (flags & DO_BACKGRD)
134                                 errx(1, "Cannot specify -B and -F.");
135                         flags |= CHECK_BACKGRD;
136                         break;
137
138                 case 'p':
139                         flags |= CHECK_PREEN;
140                         /*FALLTHROUGH*/
141                 case 'C':
142                         flags |= CHECK_CLEAN;
143                         /*FALLTHROUGH*/
144                 case 'n':
145                 case 'y':
146                         globopt[1] = i;
147                         catopt(&options, globopt);
148                         break;
149
150                 case 'f':
151                         forceflag = 1;
152                         globopt[1] = i;
153                         catopt(&options, globopt);
154                         break;
155
156                 case 'l':
157                         warnx("Ignoring obsolete -l option\n");
158                         break;
159
160                 case 'T':
161                         if (*optarg)
162                                 addoption(optarg);
163                         break;
164
165                 case 't':
166                         if (!TAILQ_EMPTY(&selhead))
167                                 errx(1, "only one -t option may be specified.");
168
169                         maketypelist(optarg);
170                         vfstype = optarg;
171                         break;
172
173                 case 'c':
174                         etc_fstab = optarg;
175                         break;
176
177                 case '?':
178                 default:
179                         usage();
180                         /* NOTREACHED */
181                 }
182
183         argc -= optind;
184         argv += optind;
185
186         if (etc_fstab != NULL)
187                 setfstab(etc_fstab);
188
189         if (argc == 0)
190                 return checkfstab(flags, isok, checkfs);
191
192 #define BADTYPE(type)                                                   \
193         (strcmp(type, FSTAB_RO) &&                                      \
194             strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
195
196
197         for (; argc--; argv++) {
198                 const char *spec, *mntpt, *type, *cp;
199                 char device[MAXPATHLEN];
200                 struct statfs *mntp;
201
202                 mntpt = NULL;
203                 spec = *argv;
204                 cp = strrchr(spec, '/');
205                 if (cp == NULL) {
206                         (void)snprintf(device, sizeof(device), "%s%s",
207                                 _PATH_DEV, spec);
208                         spec = device;
209                 }
210                 mntp = getmntpoint(spec);
211                 if (mntp != NULL) {
212                         spec = mntp->f_mntfromname;
213                         mntpt = mntp->f_mntonname;
214                 }
215                 if ((fs = getfsfile(spec)) == NULL &&
216                     (fs = getfsspec(spec)) == NULL) {
217                         if (vfstype == NULL)
218                                 vfstype = getfstype(spec);
219                         if (vfstype == NULL)
220                                 vfstype = "ufs";
221                         type = vfstype;
222                         devcheck(spec);
223                 } else {
224                         spec = fs->fs_spec;
225                         type = fs->fs_vfstype;
226                         mntpt = fs->fs_file;
227                         if (BADTYPE(fs->fs_type))
228                                 errx(1, "%s has unknown file system type.",
229                                     spec);
230                 }
231                 if ((flags & CHECK_BACKGRD) &&
232                     checkfs(type, spec, mntpt, "-F", NULL) == 0) {
233                         printf("%s: DEFER FOR BACKGROUND CHECKING\n", *argv);
234                         continue;
235                 }
236                 if ((flags & DO_BACKGRD) && forceflag == 0 &&
237                     checkfs(type, spec, mntpt, "-F", NULL) != 0)
238                         continue;
239
240                 rval |= checkfs(type, spec, mntpt, NULL, NULL);
241         }
242
243         return rval;
244 }
245
246
247 static int
248 isok(struct fstab *fs)
249 {
250         int i;
251
252         if (fs->fs_passno == 0)
253                 return (0);
254         if (BADTYPE(fs->fs_type))
255                 return (0);
256         if (!selected(fs->fs_vfstype))
257                 return (0);
258         /* If failok, always check now */
259         if (getfsopt(fs, "failok"))
260                 return (1);
261         /*
262          * If the -B flag has been given, then process the needed
263          * background checks. Background checks cannot be run on
264          * file systems that will be mounted read-only or that were
265          * not mounted at boot time (typically those marked `noauto').
266          * If these basic tests are passed, check with the file system
267          * itself to see if it is willing to do background checking
268          * by invoking its check program with the -F flag.
269          */
270         if (flags & DO_BACKGRD) {
271                 if (!strcmp(fs->fs_type, FSTAB_RO))
272                         return (0);
273                 if (getmntpoint(fs->fs_spec) == NULL)
274                         return (0);
275                 if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", 0))
276                         return (0);
277                 return (1);
278         }
279         /*
280          * If the -F flag has been given, then consider deferring the
281          * check to background. Background checks cannot be run on
282          * file systems that will be mounted read-only or that will
283          * not be mounted at boot time (e.g., marked `noauto'). If
284          * these basic tests are passed, check with the file system
285          * itself to see if it is willing to defer to background
286          * checking by invoking its check program with the -F flag.
287          */
288         if ((flags & CHECK_BACKGRD) == 0 || !strcmp(fs->fs_type, FSTAB_RO))
289                 return (1);
290         for (i = strlen(fs->fs_mntops) - 6; i >= 0; i--)
291                 if (!strncmp(&fs->fs_mntops[i], "noauto", 6))
292                         break;
293         if (i >= 0)
294                 return (1);
295         if (checkfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, "-F", NULL) != 0)
296                 return (1);
297         printf("%s: DEFER FOR BACKGROUND CHECKING\n", fs->fs_spec);
298         return (0);
299 }
300
301
302 static int
303 checkfs(const char *pvfstype, const char *spec, const char *mntpt,
304     const char *auxopt, pid_t *pidp)
305 {
306         const char ** volatile argv;
307         pid_t pid;
308         int argc, i, status, maxargc;
309         char *optbuf, execbase[MAXPATHLEN];
310         char *vfstype = NULL;
311         const char *extra = NULL;
312
313 #ifdef __GNUC__
314         /* Avoid vfork clobbering */
315         (void) &optbuf;
316         (void) &vfstype;
317 #endif
318         /*
319          * We convert the vfstype to lowercase and any spaces to underscores
320          * to not confuse the issue
321          *
322          * XXX This is a kludge to make automatic filesystem type guessing
323          * from the disklabel work for "4.2BSD" filesystems.  It does a
324          * very limited subset of transliteration to a normalised form of
325          * filesystem name, and we do not seem to enforce a filesystem
326          * name character set.
327          */
328         vfstype = strdup(pvfstype);
329         if (vfstype == NULL)
330                 perr("strdup(pvfstype)");
331         for (i = 0; i < (int)strlen(vfstype); i++) {
332                 vfstype[i] = tolower(vfstype[i]);
333                 if (vfstype[i] == ' ')
334                         vfstype[i] = '_';
335         }
336
337         extra = getoptions(vfstype);
338         optbuf = NULL;
339         if (options)
340                 catopt(&optbuf, options);
341         if (extra)
342                 catopt(&optbuf, extra);
343         if (auxopt)
344                 catopt(&optbuf, auxopt);
345         else if (flags & DO_BACKGRD)
346                 catopt(&optbuf, "-B");
347
348         maxargc = 64;
349         argv = emalloc(sizeof(char *) * maxargc);
350
351         (void) snprintf(execbase, sizeof(execbase), "fsck_%s", vfstype);
352         argc = 0;
353         argv[argc++] = execbase;
354         if (optbuf)
355                 mangle(optbuf, &argc, &argv, &maxargc);
356         argv[argc++] = spec;
357         argv[argc] = NULL;
358
359         if (flags & (CHECK_DEBUG|CHECK_VERBOSE)) {
360                 (void)printf("start %s %swait", mntpt, 
361                         pidp ? "no" : "");
362                 for (i = 0; i < argc; i++)
363                         (void)printf(" %s", argv[i]);
364                 (void)printf("\n");
365         }
366
367         switch (pid = vfork()) {
368         case -1:                                /* Error. */
369                 warn("vfork");
370                 if (optbuf)
371                         free(optbuf);
372                 free(vfstype);
373                 return (1);
374
375         case 0:                                 /* Child. */
376                 if ((flags & CHECK_DEBUG) && auxopt == NULL)
377                         _exit(0);
378
379                 /* Go find an executable. */
380                 execvP(execbase, _PATH_SYSPATH, __DECONST(char * const *, argv));
381                 if (spec)
382                         warn("exec %s for %s in %s", execbase, spec, _PATH_SYSPATH);
383                 else
384                         warn("exec %s in %s", execbase, _PATH_SYSPATH);
385                 _exit(1);
386                 /* NOTREACHED */
387
388         default:                                /* Parent. */
389                 if (optbuf)
390                         free(optbuf);
391
392                 free(vfstype);
393
394                 if (pidp) {
395                         *pidp = pid;
396                         return 0;
397                 }
398
399                 if (waitpid(pid, &status, 0) < 0) {
400                         warn("waitpid");
401                         return (1);
402                 }
403
404                 if (WIFEXITED(status)) {
405                         if (WEXITSTATUS(status) != 0)
406                                 return (WEXITSTATUS(status));
407                 }
408                 else if (WIFSIGNALED(status)) {
409                         warnx("%s: %s", spec, strsignal(WTERMSIG(status)));
410                         return (1);
411                 }
412                 break;
413         }
414
415         return (0);
416 }
417
418
419 static int
420 selected(const char *type)
421 {
422         struct entry *e;
423
424         /* If no type specified, it's always selected. */
425         TAILQ_FOREACH(e, &selhead, entries)
426                 if (!strncmp(e->type, type, MFSNAMELEN))
427                         return which == IN_LIST ? 1 : 0;
428
429         return which == IN_LIST ? 0 : 1;
430 }
431
432
433 static const char *
434 getoptions(const char *type)
435 {
436         struct entry *e;
437
438         TAILQ_FOREACH(e, &opthead, entries)
439                 if (!strncmp(e->type, type, MFSNAMELEN))
440                         return e->options;
441         return "";
442 }
443
444
445 static void
446 addoption(char *optstr)
447 {
448         char *newoptions;
449         struct entry *e;
450
451         if ((newoptions = strchr(optstr, ':')) == NULL)
452                 errx(1, "Invalid option string");
453
454         *newoptions++ = '\0';
455
456         TAILQ_FOREACH(e, &opthead, entries)
457                 if (!strncmp(e->type, optstr, MFSNAMELEN)) {
458                         catopt(&e->options, newoptions);
459                         return;
460                 }
461         addentry(&opthead, optstr, newoptions);
462 }
463
464
465 static void
466 addentry(struct fstypelist *list, const char *type, const char *opts)
467 {
468         struct entry *e;
469
470         e = emalloc(sizeof(struct entry));
471         e->type = estrdup(type);
472         e->options = estrdup(opts);
473         TAILQ_INSERT_TAIL(list, e, entries);
474 }
475
476
477 static void
478 maketypelist(char *fslist)
479 {
480         char *ptr;
481
482         if ((fslist == NULL) || (fslist[0] == '\0'))
483                 errx(1, "empty type list");
484
485         if (fslist[0] == 'n' && fslist[1] == 'o') {
486                 fslist += 2;
487                 which = NOT_IN_LIST;
488         }
489         else
490                 which = IN_LIST;
491
492         while ((ptr = strsep(&fslist, ",")) != NULL)
493                 addentry(&selhead, ptr, "");
494
495 }
496
497
498 static void
499 catopt(char **sp, const char *o)
500 {
501         char *s;
502         size_t i, j;
503
504         s = *sp;
505         if (s) {
506                 i = strlen(s);
507                 j = i + 1 + strlen(o) + 1;
508                 s = erealloc(s, j);
509                 (void)snprintf(s + i, j, ",%s", o);
510         } else
511                 s = estrdup(o);
512         *sp = s;
513 }
514
515
516 static void
517 mangle(char *opts, int *argcp, const char ** volatile *argvp, int *maxargcp)
518 {
519         char *p, *s;
520         int argc, maxargc;
521         const char **argv;
522
523         argc = *argcp;
524         argv = *argvp;
525         maxargc = *maxargcp;
526
527         for (s = opts; (p = strsep(&s, ",")) != NULL;) {
528                 /* Always leave space for one more argument and the NULL. */
529                 if (argc >= maxargc - 3) {
530                         maxargc <<= 1;
531                         argv = erealloc(argv, maxargc * sizeof(char *));
532                 }
533                 if (*p != '\0')  {
534                         if (*p == '-') {
535                                 argv[argc++] = p;
536                                 p = strchr(p, '=');
537                                 if (p) {
538                                         *p = '\0';
539                                         argv[argc++] = p+1;
540                                 }
541                         } else {
542                                 argv[argc++] = "-o";
543                                 argv[argc++] = p;
544                         }
545                 }
546         }
547
548         *argcp = argc;
549         *argvp = argv;
550         *maxargcp = maxargc;
551 }
552
553 static const char *
554 getfstype(const char *str)
555 {
556         struct diocgattr_arg attr;
557         int fd, i;
558
559         if ((fd = open(str, O_RDONLY)) == -1)
560                 err(1, "cannot open `%s'", str);
561
562         strncpy(attr.name, "PART::type", sizeof(attr.name));
563         memset(&attr.value, 0, sizeof(attr.value));
564         attr.len = sizeof(attr.value);
565         if (ioctl(fd, DIOCGATTR, &attr) == -1) {
566                 (void) close(fd);
567                 return(NULL);
568         }
569         (void) close(fd);
570         for (i = 0; ptype_map[i].ptype != NULL; i++)
571                 if (strstr(attr.value.str, ptype_map[i].ptype) != NULL)
572                         return (ptype_map[i].name);
573         return (NULL);
574 }
575
576
577 static void
578 usage(void)
579 {
580         static const char common[] =
581             "[-Cdfnpvy] [-B | -F] [-T fstype:fsoptions] [-t fstype] [-c fstab]";
582
583         (void)fprintf(stderr, "usage: %s %s [special | node] ...\n",
584             getprogname(), common);
585         exit(1);
586 }