]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/fsck_ffs/preen.c
This commit was generated by cvs2svn to compensate for changes in r92688,
[FreeBSD/FreeBSD.git] / sbin / fsck_ffs / preen.c
1 /*
2  * Copyright (c) 1990, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 #if 0
36 static const char sccsid[] = "@(#)preen.c       8.5 (Berkeley) 4/28/95";
37 #endif
38 static const char rcsid[] =
39   "$FreeBSD$";
40 #endif /* not lint */
41
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 #include <sys/wait.h>
45
46 #include <ufs/ufs/dinode.h>
47 #include <ufs/ffs/fs.h>
48
49 #include <ctype.h>
50 #include <errno.h>
51 #include <fstab.h>
52 #include <string.h>
53
54 #include "fsck.h"
55
56 struct part {
57         struct  part *next;             /* forward link of partitions on disk */
58         char    *name;                  /* device name */
59         char    *fsname;                /* mounted filesystem name */
60         long    auxdata;                /* auxiliary data for application */
61 } *badlist, **badnext = &badlist;
62
63 struct disk {
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 */
68 } *disks;
69
70 int     nrun, ndisks;
71
72 static void addpart __P((char *name, char *fsname, long auxdata));
73 static struct disk *finddisk __P((char *name));
74 static int startdisk __P((struct disk *dk,
75                 int (*checkit)(char *, char *, long, int)));
76
77 int
78 checkfstab(preen, maxrun, docheck, chkit)
79         int preen;
80         int maxrun;
81         int (*docheck)(struct fstab *);
82         int (*chkit)(char *, char *, long, int);
83 {
84         register struct fstab *fsp;
85         register struct disk *dk, *nextdisk;
86         register struct part *pt;
87         int ret, pid, retcode, passno, sumstatus, status;
88         long auxdata;
89         char *name;
90
91         sumstatus = 0;
92         for (passno = 1; passno <= 2; passno++) {
93                 if (setfsent() == 0) {
94                         fprintf(stderr, "Can't open checklist file: %s\n",
95                             _PATH_FSTAB);
96                         return (8);
97                 }
98                 while ((fsp = getfsent()) != 0) {
99                         if ((auxdata = (*docheck)(fsp)) == 0)
100                                 continue;
101                         if (preen == 0 ||
102                             (passno == 1 && fsp->fs_passno == 1)) {
103                                 if ((name = blockcheck(fsp->fs_spec)) != 0) {
104                                         if ((sumstatus = (*chkit)(name,
105                                             fsp->fs_file, auxdata, 0)) != 0)
106                                                 return (sumstatus);
107                                 } else if (preen)
108                                         return (8);
109                         } else if (passno == 2 && fsp->fs_passno > 1) {
110                                 if ((name = blockcheck(fsp->fs_spec)) == NULL) {
111                                         fprintf(stderr, "BAD DISK NAME %s\n",
112                                                 fsp->fs_spec);
113                                         sumstatus |= 8;
114                                         continue;
115                                 }
116                                 addpart(name, fsp->fs_file, auxdata);
117                         }
118                 }
119                 if (preen == 0)
120                         return (0);
121         }
122         if (preen) {
123                 if (maxrun == 0)
124                         maxrun = ndisks;
125                 if (maxrun > ndisks)
126                         maxrun = ndisks;
127                 nextdisk = disks;
128                 for (passno = 0; passno < maxrun; ++passno) {
129                         while ((ret = startdisk(nextdisk, chkit)) && nrun > 0)
130                                 sleep(10);
131                         if (ret)
132                                 return (ret);
133                         nextdisk = nextdisk->next;
134                 }
135                 while ((pid = wait(&status)) != -1) {
136                         for (dk = disks; dk; dk = dk->next)
137                                 if (dk->pid == pid)
138                                         break;
139                         if (dk == 0) {
140                                 printf("Unknown pid %d\n", pid);
141                                 continue;
142                         }
143                         if (WIFEXITED(status))
144                                 retcode = WEXITSTATUS(status);
145                         else
146                                 retcode = 0;
147                         if (WIFSIGNALED(status)) {
148                                 printf("%s (%s): EXITED WITH SIGNAL %d\n",
149                                         dk->part->name, dk->part->fsname,
150                                         WTERMSIG(status));
151                                 retcode = 8;
152                         }
153                         if (retcode != 0) {
154                                 sumstatus |= retcode;
155                                 *badnext = dk->part;
156                                 badnext = &dk->part->next;
157                                 dk->part = dk->part->next;
158                                 *badnext = NULL;
159                         } else
160                                 dk->part = dk->part->next;
161                         dk->pid = 0;
162                         nrun--;
163                         if (dk->part == NULL)
164                                 ndisks--;
165
166                         if (nextdisk == NULL) {
167                                 if (dk->part) {
168                                         while ((ret = startdisk(dk, chkit)) &&
169                                             nrun > 0)
170                                                 sleep(10);
171                                         if (ret)
172                                                 return (ret);
173                                 }
174                         } else if (nrun < maxrun && nrun < ndisks) {
175                                 for ( ;; ) {
176                                         if ((nextdisk = nextdisk->next) == NULL)
177                                                 nextdisk = disks;
178                                         if (nextdisk->part != NULL &&
179                                             nextdisk->pid == 0)
180                                                 break;
181                                 }
182                                 while ((ret = startdisk(nextdisk, chkit)) &&
183                                     nrun > 0)
184                                         sleep(10);
185                                 if (ret)
186                                         return (ret);
187                         }
188                 }
189         }
190         if (sumstatus) {
191                 if (badlist == 0)
192                         return (sumstatus);
193                 fprintf(stderr, "THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t",
194                         badlist->next ? "S" : "", "UNEXPECTED INCONSISTENCY:");
195                 for (pt = badlist; pt; pt = pt->next)
196                         fprintf(stderr, "%s (%s)%s", pt->name, pt->fsname,
197                             pt->next ? ", " : "\n");
198                 return (sumstatus);
199         }
200         (void)endfsent();
201         return (0);
202 }
203
204 static struct disk *
205 finddisk(name)
206         char *name;
207 {
208         register struct disk *dk, **dkp;
209         register char *p;
210         size_t len;
211
212         p = strrchr(name, '/');
213         p = p == NULL ? name : p + 1;
214         while (*p != '\0' && !isdigit((u_char)*p))
215                 p++;
216         while (isdigit((u_char)*p))
217                 p++;
218         len = (size_t)(p - name);
219         for (dk = disks, dkp = &disks; dk; dkp = &dk->next, dk = dk->next) {
220                 if (strncmp(dk->name, name, len) == 0 &&
221                     dk->name[len] == 0)
222                         return (dk);
223         }
224         if ((*dkp = (struct disk *)malloc(sizeof(struct disk))) == NULL) {
225                 fprintf(stderr, "out of memory");
226                 exit (8);
227         }
228         dk = *dkp;
229         if ((dk->name = malloc(len + 1)) == NULL) {
230                 fprintf(stderr, "out of memory");
231                 exit (8);
232         }
233         (void)strncpy(dk->name, name, len);
234         dk->name[len] = '\0';
235         dk->part = NULL;
236         dk->next = NULL;
237         dk->pid = 0;
238         ndisks++;
239         return (dk);
240 }
241
242 static void
243 addpart(name, fsname, auxdata)
244         char *name, *fsname;
245         long auxdata;
246 {
247         struct disk *dk = finddisk(name);
248         register struct part *pt, **ppt = &dk->part;
249
250         for (pt = dk->part; pt; ppt = &pt->next, pt = pt->next)
251                 if (strcmp(pt->name, name) == 0) {
252                         printf("%s in fstab more than once!\n", name);
253                         return;
254                 }
255         if ((*ppt = (struct part *)malloc(sizeof(struct part))) == NULL) {
256                 fprintf(stderr, "out of memory");
257                 exit (8);
258         }
259         pt = *ppt;
260         if ((pt->name = malloc(strlen(name) + 1)) == NULL) {
261                 fprintf(stderr, "out of memory");
262                 exit (8);
263         }
264         (void)strcpy(pt->name, name);
265         if ((pt->fsname = malloc(strlen(fsname) + 1)) == NULL) {
266                 fprintf(stderr, "out of memory");
267                 exit (8);
268         }
269         (void)strcpy(pt->fsname, fsname);
270         pt->next = NULL;
271         pt->auxdata = auxdata;
272 }
273
274 static int
275 startdisk(dk, checkit)
276         register struct disk *dk;
277         int (*checkit)(char *, char *, long, int);
278 {
279         register struct part *pt = dk->part;
280
281         dk->pid = fork();
282         if (dk->pid < 0) {
283                 perror("fork");
284                 return (8);
285         }
286         if (dk->pid == 0)
287                 exit((*checkit)(pt->name, pt->fsname, pt->auxdata, 1));
288         nrun++;
289         return (0);
290 }
291