xref: /freebsd/sbin/fsck/preen.c (revision bcd92649c9952c9c9e8845dbd34276a60dd16664)
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 <fstab.h>
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 static void	addpart __P((char *name, char *fsname, long auxdata));
66 static int	startdisk __P((struct disk *dk, int (*checkit)()));
67 static struct disk 	*finddisk __P((char *name));
68 static char 	*unrawname __P((char *name));
69 static char 	*rawname __P((char *name));
70 
71 int	nrun, ndisks;
72 char	hotroot;
73 
74 int
75 checkfstab(preen, maxrun, docheck, chkit)
76 	int preen, maxrun;
77 	int (*docheck)(), (*chkit)();
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 || (passno == 1 && fsp->fs_passno == 1)) {
97 				name = blockcheck(fsp->fs_spec);
98 				if (name) {
99 					sumstatus = (*chkit)(name,
100 					    fsp->fs_file, auxdata, 0);
101 					if (sumstatus)
102 						return (sumstatus);
103 				} else if (preen)
104 					return (8);
105 			} else if (passno == 2 && fsp->fs_passno > 1) {
106 				if ((name = blockcheck(fsp->fs_spec)) == NULL) {
107 					fprintf(stderr, "BAD DISK NAME %s\n",
108 						fsp->fs_spec);
109 					sumstatus |= 8;
110 					continue;
111 				}
112 				addpart(name, fsp->fs_file, auxdata);
113 			}
114 		}
115 		if (preen == 0)
116 			return (0);
117 	}
118 	if (preen) {
119 		if (maxrun == 0)
120 			maxrun = ndisks;
121 		if (maxrun > ndisks)
122 			maxrun = ndisks;
123 		nextdisk = disks;
124 		for (passno = 0; passno < maxrun; ++passno) {
125 			while ((ret = startdisk(nextdisk, chkit)) != 0 &&
126 			    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)) != 0
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)) != 0
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 
201 struct disk *
202 finddisk(name)
203 	char *name;
204 {
205 	register struct disk *dk, **dkp;
206 	register char *p;
207 	size_t len = 0;
208 
209 	for (p = name + strlen(name) - 1; p >= name; --p)
210 		if (isdigit(*p)) {
211 			len = p - name + 1;
212 			break;
213 		}
214 	if (p < name)
215 		len = strlen(name);
216 
217 	for (dk = disks, dkp = &disks; dk; dkp = &dk->next, dk = dk->next) {
218 		if (strncmp(dk->name, name, len) == 0 &&
219 		    dk->name[len] == 0)
220 			return (dk);
221 	}
222 	if ((*dkp = (struct disk *)malloc(sizeof(struct disk))) == NULL) {
223 		fprintf(stderr, "out of memory");
224 		exit (8);
225 	}
226 	dk = *dkp;
227 	if ((dk->name = malloc(len + 1)) == NULL) {
228 		fprintf(stderr, "out of memory");
229 		exit (8);
230 	}
231 	(void)strncpy(dk->name, name, len);
232 	dk->name[len] = '\0';
233 	dk->part = NULL;
234 	dk->next = NULL;
235 	dk->pid = 0;
236 	ndisks++;
237 	return (dk);
238 }
239 
240 void
241 addpart(name, fsname, auxdata)
242 	char *name, *fsname;
243 	long auxdata;
244 {
245 	struct disk *dk = finddisk(name);
246 	register struct part *pt, **ppt = &dk->part;
247 
248 	for (pt = dk->part; pt; ppt = &pt->next, pt = pt->next)
249 		if (strcmp(pt->name, name) == 0) {
250 			printf("%s in fstab more than once!\n", name);
251 			return;
252 		}
253 	if ((*ppt = (struct part *)malloc(sizeof(struct part))) == NULL) {
254 		fprintf(stderr, "out of memory");
255 		exit (8);
256 	}
257 	pt = *ppt;
258 	if ((pt->name = malloc(strlen(name) + 1)) == NULL) {
259 		fprintf(stderr, "out of memory");
260 		exit (8);
261 	}
262 	(void)strcpy(pt->name, name);
263 	if ((pt->fsname = malloc(strlen(fsname) + 1)) == NULL) {
264 		fprintf(stderr, "out of memory");
265 		exit (8);
266 	}
267 	(void)strcpy(pt->fsname, fsname);
268 	pt->next = NULL;
269 	pt->auxdata = auxdata;
270 }
271 
272 int
273 startdisk(dk, checkit)
274 	register struct disk *dk;
275 	int (*checkit)();
276 {
277 	register struct part *pt = dk->part;
278 
279 	dk->pid = fork();
280 	if (dk->pid < 0) {
281 		perror("fork");
282 		return (8);
283 	}
284 	if (dk->pid == 0)
285 		exit((*checkit)(pt->name, pt->fsname, pt->auxdata, 1));
286 	nrun++;
287 	return (0);
288 }
289 
290 char *
291 blockcheck(name)
292 	char *name;
293 {
294 	struct stat stslash, stblock, stchar;
295 	char *raw;
296 	struct fstab *fsinfo;
297 	int retried = 0, l;
298 
299 	hotroot = 0;
300 	if (stat("/", &stslash) < 0) {
301 		perror("/");
302 		printf("Can't stat root\n");
303 		return (0);
304 	}
305 retry:
306 	if (stat(name, &stblock) < 0) {
307 		perror(name);
308 		printf("Can't stat %s\n", name);
309 		return (0);
310 	}
311 	if ((stblock.st_mode & S_IFMT) == S_IFBLK) {
312 		if (stslash.st_dev == stblock.st_rdev)
313 			hotroot++;
314 		raw = rawname(name);
315 		if (stat(raw, &stchar) < 0) {
316 			perror(raw);
317 			printf("Can't stat %s\n", raw);
318 			return (name);
319 		}
320 		if ((stchar.st_mode & S_IFMT) == S_IFCHR) {
321 			return (raw);
322 		} else {
323 			printf("%s is not a character device\n", raw);
324 			return (name);
325 		}
326 	} else if ((stblock.st_mode & S_IFMT) == S_IFCHR && !retried) {
327 		name = unrawname(name);
328 		retried++;
329 		goto retry;
330 	} else if ((stblock.st_mode & S_IFMT) == S_IFDIR && !retried) {
331 		l = strlen(name) - 1;
332 		if (l > 0 && name[l] == '/')
333 			/* remove trailing slash */
334 			name[l] = '\0';
335 		if(!(fsinfo=getfsfile(name))) {
336 			printf("Can't resolve %s to character special device",
337 			    name);
338 			return (0);
339 		}
340 		name = fsinfo->fs_spec;
341 		retried++;
342 		goto retry;
343 	}
344 	printf("Warning: Can't find blockdevice corresponding to name %s\n",
345 	    name);
346 	return (name);
347 }
348 
349 char *
350 unrawname(name)
351 	char *name;
352 {
353 	char *dp;
354 	struct stat stb;
355 
356 	if ((dp = rindex(name, '/')) == 0)
357 		return (name);
358 	if (stat(name, &stb) < 0)
359 		return (name);
360 	if ((stb.st_mode & S_IFMT) != S_IFCHR)
361 		return (name);
362 	if (dp[1] != 'r')
363 		return (name);
364 	(void)strcpy(&dp[1], &dp[2]);
365 	return (name);
366 }
367 
368 char *
369 rawname(name)
370 	char *name;
371 {
372 	static char rawbuf[32];
373 	char *dp;
374 
375 	if ((dp = rindex(name, '/')) == 0)
376 		return (0);
377 	*dp = 0;
378 	(void)strcpy(rawbuf, name);
379 	*dp = '/';
380 	(void)strcat(rawbuf, "/r");
381 	(void)strcat(rawbuf, &dp[1]);
382 	return (rawbuf);
383 }
384