xref: /freebsd/sbin/fsck/preen.c (revision 5ebc7e6281887681c3a348a5a4c902e262ccd656)
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 static const char sccsid[] = "@(#)preen.c	8.1 (Berkeley) 6/5/93";
36 #endif /* not lint */
37 
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #include <sys/wait.h>
41 #include <ufs/ufs/dinode.h>
42 #include <fstab.h>
43 #include <string.h>
44 #include <stdio.h>
45 #include <unistd.h>
46 #include <stdlib.h>
47 #include <ctype.h>
48 #include "fsck.h"
49 
50 struct 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 
57 struct 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 
64 static void	addpart __P((char *name, char *fsname, long auxdata));
65 static int	startdisk __P((struct disk *dk, int (*checkit)()));
66 static struct disk 	*finddisk __P((char *name));
67 static char 	*unrawname __P((char *name));
68 static char 	*rawname __P((char *name));
69 
70 int	nrun, ndisks;
71 char	hotroot;
72 
73 int
74 checkfstab(preen, maxrun, docheck, chkit)
75 	int preen, maxrun;
76 	int (*docheck)(), (*chkit)();
77 {
78 	register struct fstab *fsp;
79 	register struct disk *dk, *nextdisk;
80 	register struct part *pt;
81 	int ret, pid, retcode, passno, sumstatus, status;
82 	long auxdata;
83 	char *name;
84 
85 	sumstatus = 0;
86 	for (passno = 1; passno <= 2; passno++) {
87 		if (setfsent() == 0) {
88 			fprintf(stderr, "Can't open checklist file: %s\n",
89 			    _PATH_FSTAB);
90 			return (8);
91 		}
92 		while ((fsp = getfsent()) != 0) {
93 			if ((auxdata = (*docheck)(fsp)) == 0)
94 				continue;
95 			if (!preen || (passno == 1 && fsp->fs_passno == 1)) {
96 				name = blockcheck(fsp->fs_spec);
97 				if (name) {
98 					sumstatus = (*chkit)(name,
99 					    fsp->fs_file, auxdata, 0);
100 					if (sumstatus)
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)) != 0 &&
125 			    nrun > 0)
126 				sleep(10);
127 			if (ret)
128 				return (ret);
129 			nextdisk = nextdisk->next;
130 		}
131 		while ((pid = wait(&status)) != -1) {
132 			for (dk = disks; dk; dk = dk->next)
133 				if (dk->pid == pid)
134 					break;
135 			if (dk == 0) {
136 				printf("Unknown pid %d\n", pid);
137 				continue;
138 			}
139 			if (WIFEXITED(status))
140 				retcode = WEXITSTATUS(status);
141 			else
142 				retcode = 0;
143 			if (WIFSIGNALED(status)) {
144 				printf("%s (%s): EXITED WITH SIGNAL %d\n",
145 					dk->part->name, dk->part->fsname,
146 					WTERMSIG(status));
147 				retcode = 8;
148 			}
149 			if (retcode != 0) {
150 				sumstatus |= retcode;
151 				*badnext = dk->part;
152 				badnext = &dk->part->next;
153 				dk->part = dk->part->next;
154 				*badnext = NULL;
155 			} else
156 				dk->part = dk->part->next;
157 			dk->pid = 0;
158 			nrun--;
159 			if (dk->part == NULL)
160 				ndisks--;
161 
162 			if (nextdisk == NULL) {
163 				if (dk->part) {
164 					while ((ret = startdisk(dk, chkit)) != 0
165 					    && nrun > 0)
166 						sleep(10);
167 					if (ret)
168 						return (ret);
169 				}
170 			} else if (nrun < maxrun && nrun < ndisks) {
171 				for ( ;; ) {
172 					if ((nextdisk = nextdisk->next) == NULL)
173 						nextdisk = disks;
174 					if (nextdisk->part != NULL &&
175 					    nextdisk->pid == 0)
176 						break;
177 				}
178 				while ((ret = startdisk(nextdisk, chkit)) != 0
179 				    && nrun > 0)
180 					sleep(10);
181 				if (ret)
182 					return (ret);
183 			}
184 		}
185 	}
186 	if (sumstatus) {
187 		if (badlist == 0)
188 			return (sumstatus);
189 		fprintf(stderr, "THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t",
190 			badlist->next ? "S" : "", "UNEXPECTED INCONSISTENCY:");
191 		for (pt = badlist; pt; pt = pt->next)
192 			fprintf(stderr, "%s (%s)%s", pt->name, pt->fsname,
193 			    pt->next ? ", " : "\n");
194 		return (sumstatus);
195 	}
196 	(void)endfsent();
197 	return (0);
198 }
199 
200 struct disk *
201 finddisk(name)
202 	char *name;
203 {
204 	register struct disk *dk, **dkp;
205 	register char *p;
206 	size_t len = 0;
207 
208 	for (p = name + strlen(name) - 1; p >= name; --p)
209 		if (isdigit(*p)) {
210 			len = p - name + 1;
211 			break;
212 		}
213 	if (p < name)
214 		len = strlen(name);
215 
216 	for (dk = disks, dkp = &disks; dk; dkp = &dk->next, dk = dk->next) {
217 		if (strncmp(dk->name, name, len) == 0 &&
218 		    dk->name[len] == 0)
219 			return (dk);
220 	}
221 	if ((*dkp = (struct disk *)malloc(sizeof(struct disk))) == NULL) {
222 		fprintf(stderr, "out of memory");
223 		exit (8);
224 	}
225 	dk = *dkp;
226 	if ((dk->name = malloc(len + 1)) == NULL) {
227 		fprintf(stderr, "out of memory");
228 		exit (8);
229 	}
230 	(void)strncpy(dk->name, name, len);
231 	dk->name[len] = '\0';
232 	dk->part = NULL;
233 	dk->next = NULL;
234 	dk->pid = 0;
235 	ndisks++;
236 	return (dk);
237 }
238 
239 void
240 addpart(name, fsname, auxdata)
241 	char *name, *fsname;
242 	long auxdata;
243 {
244 	struct disk *dk = finddisk(name);
245 	register struct part *pt, **ppt = &dk->part;
246 
247 	for (pt = dk->part; pt; ppt = &pt->next, pt = pt->next)
248 		if (strcmp(pt->name, name) == 0) {
249 			printf("%s in fstab more than once!\n", name);
250 			return;
251 		}
252 	if ((*ppt = (struct part *)malloc(sizeof(struct part))) == NULL) {
253 		fprintf(stderr, "out of memory");
254 		exit (8);
255 	}
256 	pt = *ppt;
257 	if ((pt->name = malloc(strlen(name) + 1)) == NULL) {
258 		fprintf(stderr, "out of memory");
259 		exit (8);
260 	}
261 	(void)strcpy(pt->name, name);
262 	if ((pt->fsname = malloc(strlen(fsname) + 1)) == NULL) {
263 		fprintf(stderr, "out of memory");
264 		exit (8);
265 	}
266 	(void)strcpy(pt->fsname, fsname);
267 	pt->next = NULL;
268 	pt->auxdata = auxdata;
269 }
270 
271 int
272 startdisk(dk, checkit)
273 	register struct disk *dk;
274 	int (*checkit)();
275 {
276 	register struct part *pt = dk->part;
277 
278 	dk->pid = fork();
279 	if (dk->pid < 0) {
280 		perror("fork");
281 		return (8);
282 	}
283 	if (dk->pid == 0)
284 		exit((*checkit)(pt->name, pt->fsname, pt->auxdata, 1));
285 	nrun++;
286 	return (0);
287 }
288 
289 char *
290 blockcheck(name)
291 	char *name;
292 {
293 	struct stat stslash, stblock, stchar;
294 	char *raw;
295 	int retried = 0;
296 
297 	hotroot = 0;
298 	if (stat("/", &stslash) < 0) {
299 		perror("/");
300 		printf("Can't stat root\n");
301 		return (0);
302 	}
303 retry:
304 	if (stat(name, &stblock) < 0) {
305 		perror(name);
306 		printf("Can't stat %s\n", name);
307 		return (0);
308 	}
309 	if ((stblock.st_mode & S_IFMT) == S_IFBLK) {
310 		if (stslash.st_dev == stblock.st_rdev)
311 			hotroot++;
312 		raw = rawname(name);
313 		if (stat(raw, &stchar) < 0) {
314 			perror(raw);
315 			printf("Can't stat %s\n", raw);
316 			return (name);
317 		}
318 		if ((stchar.st_mode & S_IFMT) == S_IFCHR) {
319 			return (raw);
320 		} else {
321 			printf("%s is not a character device\n", raw);
322 			return (name);
323 		}
324 	} else if ((stblock.st_mode & S_IFMT) == S_IFCHR && !retried) {
325 		name = unrawname(name);
326 		retried++;
327 		goto retry;
328 	}
329 	printf("Can't make sense out of name %s\n", name);
330 	return (0);
331 }
332 
333 char *
334 unrawname(name)
335 	char *name;
336 {
337 	char *dp;
338 	struct stat stb;
339 
340 	if ((dp = rindex(name, '/')) == 0)
341 		return (name);
342 	if (stat(name, &stb) < 0)
343 		return (name);
344 	if ((stb.st_mode & S_IFMT) != S_IFCHR)
345 		return (name);
346 	if (dp[1] != 'r')
347 		return (name);
348 	(void)strcpy(&dp[1], &dp[2]);
349 	return (name);
350 }
351 
352 char *
353 rawname(name)
354 	char *name;
355 {
356 	static char rawbuf[32];
357 	char *dp;
358 
359 	if ((dp = rindex(name, '/')) == 0)
360 		return (0);
361 	*dp = 0;
362 	(void)strcpy(rawbuf, name);
363 	*dp = '/';
364 	(void)strcat(rawbuf, "/r");
365 	(void)strcat(rawbuf, &dp[1]);
366 	return (rawbuf);
367 }
368