2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char sccsid[] = "@(#)preen.c 8.5 (Berkeley) 4/28/95";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
42 #include <ufs/ufs/dinode.h>
43 #include <ufs/ffs/fs.h>
53 char *blockcheck(char *origname);
57 struct part *next; /* forward link of partitions on disk */
58 char *name; /* device name */
59 char *fsname; /* mounted file system name */
60 void *auxdata; /* auxiliary data for application */
61 } *badlist, **badnext = &badlist;
64 char *name; /* disk base name */
65 struct disk *next; /* forward link for list of disks */
66 struct part *part; /* head of list of partitions on disk */
67 int pid; /* If != 0, pid of proc working on */
72 static void addpart(char *name, char *fsname, void *auxdata);
73 static struct disk *finddisk(char *name);
74 static int startdisk(struct disk *dk,
75 int (*checkit)(char *, char *, void *, int));
78 checkfstab(int preen, int maxrun, void *(*docheck)(struct fstab *),
79 int (*chkit)(char *, char *, void *, int))
82 struct disk *dk, *nextdisk;
84 int ret, pid, retcode, passno, sumstatus, status;
89 for (passno = 1; passno <= 2; passno++) {
90 if (setfsent() == 0) {
91 fprintf(stderr, "Can't open checklist file: %s\n",
95 while ((fsp = getfsent()) != 0) {
96 if ((auxdata = (*docheck)(fsp)) == NULL)
99 (passno == 1 && fsp->fs_passno == 1)) {
100 if ((name = blockcheck(fsp->fs_spec)) != 0) {
101 if ((sumstatus = (*chkit)(name,
102 fsp->fs_file, auxdata, 0)) != 0)
106 } else if (passno == 2 && fsp->fs_passno > 1) {
107 if ((name = blockcheck(fsp->fs_spec)) == NULL) {
108 fprintf(stderr, "BAD DISK NAME %s\n",
113 addpart(name, fsp->fs_file, auxdata);
125 for (passno = 0; passno < maxrun; ++passno) {
126 while ((ret = startdisk(nextdisk, chkit)) && nrun > 0)
130 nextdisk = nextdisk->next;
132 while ((pid = wait(&status)) != -1) {
133 for (dk = disks; dk; dk = dk->next)
137 printf("Unknown pid %d\n", pid);
140 if (WIFEXITED(status))
141 retcode = WEXITSTATUS(status);
144 if (WIFSIGNALED(status)) {
145 printf("%s (%s): EXITED WITH SIGNAL %d\n",
146 dk->part->name, dk->part->fsname,
151 sumstatus |= retcode;
153 badnext = &dk->part->next;
154 dk->part = dk->part->next;
157 dk->part = dk->part->next;
160 if (dk->part == NULL)
163 if (nextdisk == NULL) {
165 while ((ret = startdisk(dk, chkit)) &&
171 } else if (nrun < maxrun && nrun < ndisks) {
173 if ((nextdisk = nextdisk->next) == NULL)
175 if (nextdisk->part != NULL &&
179 while ((ret = startdisk(nextdisk, chkit)) &&
190 fprintf(stderr, "THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t",
191 badlist->next ? "S" : "", "UNEXPECTED INCONSISTENCY:");
192 for (pt = badlist; pt; pt = pt->next)
193 fprintf(stderr, "%s (%s)%s", pt->name, pt->fsname,
194 pt->next ? ", " : "\n");
204 struct disk *dk, **dkp;
208 p = strrchr(name, '/');
209 p = p == NULL ? name : p + 1;
210 while (*p != '\0' && !isdigit((u_char)*p))
212 while (isdigit((u_char)*p))
214 len = (size_t)(p - name);
215 for (dk = disks, dkp = &disks; dk; dkp = &dk->next, dk = dk->next) {
216 if (strncmp(dk->name, name, len) == 0 &&
220 if ((*dkp = (struct disk *)malloc(sizeof(struct disk))) == NULL) {
221 fprintf(stderr, "out of memory");
225 if ((dk->name = malloc(len + 1)) == NULL) {
226 fprintf(stderr, "out of memory");
229 (void)strncpy(dk->name, name, len);
230 dk->name[len] = '\0';
239 addpart(char *name, char *fsname, void *auxdata)
241 struct disk *dk = finddisk(name);
242 struct part *pt, **ppt = &dk->part;
244 for (pt = dk->part; pt; ppt = &pt->next, pt = pt->next)
245 if (strcmp(pt->name, name) == 0) {
246 printf("%s in fstab more than once!\n", name);
249 if ((*ppt = (struct part *)malloc(sizeof(struct part))) == NULL) {
250 fprintf(stderr, "out of memory");
254 if ((pt->name = malloc(strlen(name) + 1)) == NULL) {
255 fprintf(stderr, "out of memory");
258 (void)strcpy(pt->name, name);
259 if ((pt->fsname = malloc(strlen(fsname) + 1)) == NULL) {
260 fprintf(stderr, "out of memory");
263 (void)strcpy(pt->fsname, fsname);
265 pt->auxdata = auxdata;
269 startdisk(struct disk *dk, int (*checkit)(char *, char *, void *, int))
271 struct part *pt = dk->part;
279 exit((*checkit)(pt->name, pt->fsname, pt->auxdata, 1));