preen.c revision 23798
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
35static const char sccsid[] = "@(#)preen.c	8.5 (Berkeley) 4/28/95";
36#endif /* not lint */
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 <fstab.h>
46#include <string.h>
47
48#include "fsck.h"
49
50struct part {
51	struct	part *next;		/* forward link of partitions on disk */
52	char	*name;			/* device name */
53	char	*fsname;		/* mounted filesystem name */
54	long	auxdata;		/* auxillary data for application */
55} *badlist, **badnext = &badlist;
56
57struct disk {
58	char	*name;			/* disk base name */
59	struct	disk *next;		/* forward link for list of disks */
60	struct	part *part;		/* head of list of partitions on disk */
61	int	pid;			/* If != 0, pid of proc working on */
62} *disks;
63
64int	nrun, ndisks;
65char	hotroot;
66
67static void addpart __P((char *name, char *fsname, long auxdata));
68static struct disk *finddisk __P((char *name));
69static char *rawname __P((char *name));
70static int startdisk __P((struct disk *dk,
71		int (*checkit)(char *, char *, long, int)));
72static char *unrawname __P((char *name));
73
74int
75checkfstab(preen, maxrun, docheck, chkit)
76	int preen;
77	int maxrun;
78	int (*docheck)(struct fstab *);
79	int (*chkit)(char *, char *, long, int);
80{
81	register struct fstab *fsp;
82	register struct disk *dk, *nextdisk;
83	register struct part *pt;
84	int ret, pid, retcode, passno, sumstatus, status;
85	long auxdata;
86	char *name;
87
88	sumstatus = 0;
89	for (passno = 1; passno <= 2; passno++) {
90		if (setfsent() == 0) {
91			fprintf(stderr, "Can't open checklist file: %s\n",
92			    _PATH_FSTAB);
93			return (8);
94		}
95		while ((fsp = getfsent()) != 0) {
96			if ((auxdata = (*docheck)(fsp)) == 0)
97				continue;
98			if (preen == 0 ||
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)
103						return (sumstatus);
104				} else if (preen)
105					return (8);
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",
109						fsp->fs_spec);
110					sumstatus |= 8;
111					continue;
112				}
113				addpart(name, fsp->fs_file, auxdata);
114			}
115		}
116		if (preen == 0)
117			return (0);
118	}
119	if (preen) {
120		if (maxrun == 0)
121			maxrun = ndisks;
122		if (maxrun > ndisks)
123			maxrun = ndisks;
124		nextdisk = disks;
125		for (passno = 0; passno < maxrun; ++passno) {
126			while ((ret = startdisk(nextdisk, chkit)) && nrun > 0)
127				sleep(10);
128			if (ret)
129				return (ret);
130			nextdisk = nextdisk->next;
131		}
132		while ((pid = wait(&status)) != -1) {
133			for (dk = disks; dk; dk = dk->next)
134				if (dk->pid == pid)
135					break;
136			if (dk == 0) {
137				printf("Unknown pid %d\n", pid);
138				continue;
139			}
140			if (WIFEXITED(status))
141				retcode = WEXITSTATUS(status);
142			else
143				retcode = 0;
144			if (WIFSIGNALED(status)) {
145				printf("%s (%s): EXITED WITH SIGNAL %d\n",
146					dk->part->name, dk->part->fsname,
147					WTERMSIG(status));
148				retcode = 8;
149			}
150			if (retcode != 0) {
151				sumstatus |= retcode;
152				*badnext = dk->part;
153				badnext = &dk->part->next;
154				dk->part = dk->part->next;
155				*badnext = NULL;
156			} else
157				dk->part = dk->part->next;
158			dk->pid = 0;
159			nrun--;
160			if (dk->part == NULL)
161				ndisks--;
162
163			if (nextdisk == NULL) {
164				if (dk->part) {
165					while ((ret = startdisk(dk, chkit)) &&
166					    nrun > 0)
167						sleep(10);
168					if (ret)
169						return (ret);
170				}
171			} else if (nrun < maxrun && nrun < ndisks) {
172				for ( ;; ) {
173					if ((nextdisk = nextdisk->next) == NULL)
174						nextdisk = disks;
175					if (nextdisk->part != NULL &&
176					    nextdisk->pid == 0)
177						break;
178				}
179				while ((ret = startdisk(nextdisk, chkit)) &&
180				    nrun > 0)
181					sleep(10);
182				if (ret)
183					return (ret);
184			}
185		}
186	}
187	if (sumstatus) {
188		if (badlist == 0)
189			return (sumstatus);
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");
195		return (sumstatus);
196	}
197	(void)endfsent();
198	return (0);
199}
200
201static struct disk *
202finddisk(name)
203	char *name;
204{
205	register struct disk *dk, **dkp;
206	register char *p;
207	size_t len;
208
209	for (len = strlen(name), p = name + len - 1; p >= name; --p)
210		if (isdigit(*p)) {
211			len = p - name + 1;
212			break;
213		}
214
215	for (dk = disks, dkp = &disks; dk; dkp = &dk->next, dk = dk->next) {
216		if (strncmp(dk->name, name, len) == 0 &&
217		    dk->name[len] == 0)
218			return (dk);
219	}
220	if ((*dkp = (struct disk *)malloc(sizeof(struct disk))) == NULL) {
221		fprintf(stderr, "out of memory");
222		exit (8);
223	}
224	dk = *dkp;
225	if ((dk->name = malloc(len + 1)) == NULL) {
226		fprintf(stderr, "out of memory");
227		exit (8);
228	}
229	(void)strncpy(dk->name, name, len);
230	dk->name[len] = '\0';
231	dk->part = NULL;
232	dk->next = NULL;
233	dk->pid = 0;
234	ndisks++;
235	return (dk);
236}
237
238static void
239addpart(name, fsname, auxdata)
240	char *name, *fsname;
241	long auxdata;
242{
243	struct disk *dk = finddisk(name);
244	register struct part *pt, **ppt = &dk->part;
245
246	for (pt = dk->part; pt; ppt = &pt->next, pt = pt->next)
247		if (strcmp(pt->name, name) == 0) {
248			printf("%s in fstab more than once!\n", name);
249			return;
250		}
251	if ((*ppt = (struct part *)malloc(sizeof(struct part))) == NULL) {
252		fprintf(stderr, "out of memory");
253		exit (8);
254	}
255	pt = *ppt;
256	if ((pt->name = malloc(strlen(name) + 1)) == NULL) {
257		fprintf(stderr, "out of memory");
258		exit (8);
259	}
260	(void)strcpy(pt->name, name);
261	if ((pt->fsname = malloc(strlen(fsname) + 1)) == NULL) {
262		fprintf(stderr, "out of memory");
263		exit (8);
264	}
265	(void)strcpy(pt->fsname, fsname);
266	pt->next = NULL;
267	pt->auxdata = auxdata;
268}
269
270static int
271startdisk(dk, checkit)
272	register struct disk *dk;
273	int (*checkit)(char *, char *, long, int);
274{
275	register struct part *pt = dk->part;
276
277	dk->pid = fork();
278	if (dk->pid < 0) {
279		perror("fork");
280		return (8);
281	}
282	if (dk->pid == 0)
283		exit((*checkit)(pt->name, pt->fsname, pt->auxdata, 1));
284	nrun++;
285	return (0);
286}
287
288char *
289blockcheck(origname)
290	char *origname;
291{
292	struct stat stslash, stblock, stchar;
293	char *newname, *raw;
294	struct fstab *fsinfo;
295	int retried = 0, l;
296
297	hotroot = 0;
298	if (stat("/", &stslash) < 0) {
299		perror("/");
300		printf("Can't stat root\n");
301		return (origname);
302	}
303	newname = origname;
304retry:
305	if (stat(newname, &stblock) < 0) {
306		perror(newname);
307		printf("Can't stat %s\n", newname);
308		return (origname);
309	}
310	if ((stblock.st_mode & S_IFMT) == S_IFBLK) {
311		if (stslash.st_dev == stblock.st_rdev)
312			hotroot++;
313		raw = rawname(newname);
314		if (stat(raw, &stchar) < 0) {
315			perror(raw);
316			printf("Can't stat %s\n", raw);
317			return (origname);
318		}
319		if ((stchar.st_mode & S_IFMT) == S_IFCHR) {
320			return (raw);
321		} else {
322			printf("%s is not a character device\n", raw);
323			return (origname);
324		}
325	} else if ((stblock.st_mode & S_IFMT) == S_IFCHR && !retried) {
326		newname = unrawname(origname);
327		retried++;
328		goto retry;
329	} else if ((stblock.st_mode & S_IFMT) == S_IFDIR && !retried) {
330		l = strlen(origname) - 1;
331		if (l > 0 && origname[l] == '/')
332			/* remove trailing slash */
333			origname[l] = '\0';
334		if(!(fsinfo=getfsfile(origname))) {
335			printf("Can't resolve %s to character special device",
336			    origname);
337			return (0);
338		}
339		newname = fsinfo->fs_spec;
340		retried++;
341		goto retry;
342	}
343	/*
344	 * Not a block or character device, just return name and
345	 * let the user decide whether to use it.
346	 */
347	return (origname);
348}
349
350static char *
351unrawname(name)
352	char *name;
353{
354	char *dp;
355	struct stat stb;
356
357	if ((dp = strrchr(name, '/')) == 0)
358		return (name);
359	if (stat(name, &stb) < 0)
360		return (name);
361	if ((stb.st_mode & S_IFMT) != S_IFCHR)
362		return (name);
363	if (dp[1] != 'r')
364		return (name);
365	(void)strcpy(&dp[1], &dp[2]);
366	return (name);
367}
368
369static char *
370rawname(name)
371	char *name;
372{
373	static char rawbuf[32];
374	char *dp;
375
376	if ((dp = strrchr(name, '/')) == 0)
377		return (0);
378	*dp = 0;
379	(void)strcpy(rawbuf, name);
380	*dp = '/';
381	(void)strcat(rawbuf, "/r");
382	(void)strcat(rawbuf, &dp[1]);
383	return (rawbuf);
384}
385