16f32c393065c192e432cb6a5153ef011da5f9c7
[dragonfly.git] / sbin / fsck / 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  * @(#)preen.c  8.5 (Berkeley) 4/28/95
34  * $FreeBSD: src/sbin/fsck/preen.c,v 1.16 1999/12/30 16:32:40 peter Exp $
35  * $DragonFly: src/sbin/fsck/preen.c,v 1.2 2003/06/17 04:27:32 dillon Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #include <sys/wait.h>
41
42 #include <ufs/ufs/dinode.h>
43
44 #include <ctype.h>
45 #include <errno.h>
46 #include <fstab.h>
47 #include <string.h>
48
49 #include "fsck.h"
50
51 struct part {
52         struct  part *next;             /* forward link of partitions on disk */
53         char    *name;                  /* device name */
54         char    *fsname;                /* mounted filesystem name */
55         long    auxdata;                /* auxillary data for application */
56 } *badlist, **badnext = &badlist;
57
58 struct disk {
59         char    *name;                  /* disk base name */
60         struct  disk *next;             /* forward link for list of disks */
61         struct  part *part;             /* head of list of partitions on disk */
62         int     pid;                    /* If != 0, pid of proc working on */
63 } *disks;
64
65 int     nrun, ndisks;
66
67 static void addpart __P((char *name, char *fsname, long auxdata));
68 static struct disk *finddisk __P((char *name));
69 static int startdisk __P((struct disk *dk,
70                 int (*checkit)(char *, char *, long, int)));
71
72 int
73 checkfstab(preen, maxrun, docheck, chkit)
74         int preen;
75         int maxrun;
76         int (*docheck)(struct fstab *);
77         int (*chkit)(char *, char *, long, int);
78 {
79         register struct fstab *fsp;
80         register struct disk *dk, *nextdisk;
81         register struct part *pt;
82         int ret, pid, retcode, passno, sumstatus, status;
83         long auxdata;
84         char *name;
85
86         sumstatus = 0;
87         for (passno = 1; passno <= 2; passno++) {
88                 if (setfsent() == 0) {
89                         fprintf(stderr, "Can't open checklist file: %s\n",
90                             _PATH_FSTAB);
91                         return (8);
92                 }
93                 while ((fsp = getfsent()) != 0) {
94                         if ((auxdata = (*docheck)(fsp)) == 0)
95                                 continue;
96                         if (preen == 0 ||
97                             (passno == 1 && fsp->fs_passno == 1)) {
98                                 if ((name = blockcheck(fsp->fs_spec)) != 0) {
99                                         if ((sumstatus = (*chkit)(name,
100                                             fsp->fs_file, auxdata, 0)) != 0)
101                                                 return (sumstatus);
102                                 } else if (preen)
103                                         return (8);
104                         } else if (passno == 2 && fsp->fs_passno > 1) {
105                                 if ((name = blockcheck(fsp->fs_spec)) == NULL) {
106                                         fprintf(stderr, "BAD DISK NAME %s\n",
107                                                 fsp->fs_spec);
108                                         sumstatus |= 8;
109                                         continue;
110                                 }
111                                 addpart(name, fsp->fs_file, auxdata);
112                         }
113                 }
114                 if (preen == 0)
115                         return (0);
116         }
117         if (preen) {
118                 if (maxrun == 0)
119                         maxrun = ndisks;
120                 if (maxrun > ndisks)
121                         maxrun = ndisks;
122                 nextdisk = disks;
123                 for (passno = 0; passno < maxrun; ++passno) {
124                         while ((ret = startdisk(nextdisk, chkit)) && nrun > 0)
125                                 sleep(10);
126                         if (ret)
127                                 return (ret);
128                         nextdisk = nextdisk->next;
129                 }
130                 while ((pid = wait(&status)) != -1) {
131                         for (dk = disks; dk; dk = dk->next)
132                                 if (dk->pid == pid)
133                                         break;
134                         if (dk == 0) {
135                                 printf("Unknown pid %d\n", pid);
136                                 continue;
137                         }
138                         if (WIFEXITED(status))
139                                 retcode = WEXITSTATUS(status);
140                         else
141                                 retcode = 0;
142                         if (WIFSIGNALED(status)) {
143                                 printf("%s (%s): EXITED WITH SIGNAL %d\n",
144                                         dk->part->name, dk->part->fsname,
145                                         WTERMSIG(status));
146                                 retcode = 8;
147                         }
148                         if (retcode != 0) {
149                                 sumstatus |= retcode;
150                                 *badnext = dk->part;
151                                 badnext = &dk->part->next;
152                                 dk->part = dk->part->next;
153                                 *badnext = NULL;
154                         } else
155                                 dk->part = dk->part->next;
156                         dk->pid = 0;
157                         nrun--;
158                         if (dk->part == NULL)
159                                 ndisks--;
160
161                         if (nextdisk == NULL) {
162                                 if (dk->part) {
163                                         while ((ret = startdisk(dk, chkit)) &&
164                                             nrun > 0)
165                                                 sleep(10);
166                                         if (ret)
167                                                 return (ret);
168                                 }
169                         } else if (nrun < maxrun && nrun < ndisks) {
170                                 for ( ;; ) {
171                                         if ((nextdisk = nextdisk->next) == NULL)
172                                                 nextdisk = disks;
173                                         if (nextdisk->part != NULL &&
174                                             nextdisk->pid == 0)
175                                                 break;
176                                 }
177                                 while ((ret = startdisk(nextdisk, chkit)) &&
178                                     nrun > 0)
179                                         sleep(10);
180                                 if (ret)
181                                         return (ret);
182                         }
183                 }
184         }
185         if (sumstatus) {
186                 if (badlist == 0)
187                         return (sumstatus);
188                 fprintf(stderr, "THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t",
189                         badlist->next ? "S" : "", "UNEXPECTED INCONSISTENCY:");
190                 for (pt = badlist; pt; pt = pt->next)
191                         fprintf(stderr, "%s (%s)%s", pt->name, pt->fsname,
192                             pt->next ? ", " : "\n");
193                 return (sumstatus);
194         }
195         (void)endfsent();
196         return (0);
197 }
198
199 static struct disk *
200 finddisk(name)
201         char *name;
202 {
203         register struct disk *dk, **dkp;
204         register char *p;
205         size_t len;
206
207         p = strrchr(name, '/');
208         p = p == NULL ? name : p + 1;
209         while (*p != '\0' && !isdigit((u_char)*p))
210                 p++;
211         while (isdigit((u_char)*p))
212                 p++;
213         len = (size_t)(p - name);
214         for (dk = disks, dkp = &disks; dk; dkp = &dk->next, dk = dk->next) {
215                 if (strncmp(dk->name, name, len) == 0 &&
216                     dk->name[len] == 0)
217                         return (dk);
218         }
219         if ((*dkp = (struct disk *)malloc(sizeof(struct disk))) == NULL) {
220                 fprintf(stderr, "out of memory");
221                 exit (8);
222         }
223         dk = *dkp;
224         if ((dk->name = malloc(len + 1)) == NULL) {
225                 fprintf(stderr, "out of memory");
226                 exit (8);
227         }
228         (void)strncpy(dk->name, name, len);
229         dk->name[len] = '\0';
230         dk->part = NULL;
231         dk->next = NULL;
232         dk->pid = 0;
233         ndisks++;
234         return (dk);
235 }
236
237 static void
238 addpart(name, fsname, auxdata)
239         char *name, *fsname;
240         long auxdata;
241 {
242         struct disk *dk = finddisk(name);
243         register struct part *pt, **ppt = &dk->part;
244
245         for (pt = dk->part; pt; ppt = &pt->next, pt = pt->next)
246                 if (strcmp(pt->name, name) == 0) {
247                         printf("%s in fstab more than once!\n", name);
248                         return;
249                 }
250         if ((*ppt = (struct part *)malloc(sizeof(struct part))) == NULL) {
251                 fprintf(stderr, "out of memory");
252                 exit (8);
253         }
254         pt = *ppt;
255         if ((pt->name = malloc(strlen(name) + 1)) == NULL) {
256                 fprintf(stderr, "out of memory");
257                 exit (8);
258         }
259         (void)strcpy(pt->name, name);
260         if ((pt->fsname = malloc(strlen(fsname) + 1)) == NULL) {
261                 fprintf(stderr, "out of memory");
262                 exit (8);
263         }
264         (void)strcpy(pt->fsname, fsname);
265         pt->next = NULL;
266         pt->auxdata = auxdata;
267 }
268
269 static int
270 startdisk(dk, checkit)
271         register struct disk *dk;
272         int (*checkit)(char *, char *, long, int);
273 {
274         register struct part *pt = dk->part;
275
276         dk->pid = fork();
277         if (dk->pid < 0) {
278                 perror("fork");
279                 return (8);
280         }
281         if (dk->pid == 0)
282                 exit((*checkit)(pt->name, pt->fsname, pt->auxdata, 1));
283         nrun++;
284         return (0);
285 }
286
287 char *
288 blockcheck(origname)
289         char *origname;
290 {
291         struct stat stslash, stblock, stchar;
292         char *newname, *raw;
293         struct fstab *fsinfo;
294         int retried = 0, len;
295
296         newname = origname;
297 retry:
298         if (stat(newname, &stblock) < 0) {
299                 printf("Can't stat %s: %s\n", newname, strerror(errno));
300                 return (origname);
301         }
302         switch(stblock.st_mode & S_IFMT) {
303         case S_IFCHR:
304         case S_IFBLK:
305                 return(newname);
306         case S_IFDIR:
307                 if (retried)
308                         break;
309                 
310                 len = strlen(origname) - 1;
311                 if (len > 0 && origname[len] == '/')
312                         /* remove trailing slash */
313                         origname[len] = '\0';
314                 if ((fsinfo = getfsfile(origname)) == NULL) {
315                         printf("Can't resolve %s to character special device",
316                             origname);
317                         return (0);
318                 }
319                 newname = fsinfo->fs_spec;
320                 retried++;
321                 goto retry;
322         }
323         /*
324          * Not a block or character device, just return name and
325          * let the user decide whether to use it.
326          */
327         return (origname);
328 }