xref: /dragonfly/sbin/fsck/preen.c (revision 0ca59c34)
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)preen.c	8.5 (Berkeley) 4/28/95
30  * $FreeBSD: src/sbin/fsck/preen.c,v 1.16 1999/12/30 16:32:40 peter Exp $
31  */
32 
33 #include <sys/param.h>
34 #include <sys/stat.h>
35 #include <sys/wait.h>
36 
37 #include <vfs/ufs/dinode.h>
38 
39 #include <ctype.h>
40 #include <errno.h>
41 #include <fstab.h>
42 #include <string.h>
43 
44 #include "fsck.h"
45 
46 struct part {
47 	struct	part *next;		/* forward link of partitions on disk */
48 	char	*name;			/* device name */
49 	char	*fsname;		/* mounted filesystem name */
50 	long	auxdata;		/* auxillary data for application */
51 } *badlist, **badnext = &badlist;
52 
53 struct disk {
54 	char	*name;			/* disk base name */
55 	struct	disk *next;		/* forward link for list of disks */
56 	struct	part *part;		/* head of list of partitions on disk */
57 	int	pid;			/* If != 0, pid of proc working on */
58 } *disks;
59 
60 int	nrun, ndisks;
61 
62 static void addpart(char *name, char *fsname, long auxdata);
63 static struct disk *finddisk(char *name);
64 static int startdisk(struct disk *dk,
65 		int (*checkit)(char *, char *, long, int));
66 
67 int
68 checkfstab(int do_preen, int maxrun, int (*docheck)(struct fstab *),
69            int (*chkit)(char *, char *, long, int))
70 {
71 	struct fstab *fsp;
72 	struct disk *dk, *nextdisk;
73 	struct part *pt;
74 	int ret, pid, retcode, passno, sumstatus, status;
75 	long auxdata;
76 	char *name;
77 
78 	sumstatus = 0;
79 	for (passno = 1; passno <= 2; passno++) {
80 		if (setfsent() == 0) {
81 			fprintf(stderr, "Can't open checklist file: %s\n",
82 			    _PATH_FSTAB);
83 			return (8);
84 		}
85 		while ((fsp = getfsent()) != NULL) {
86 			if ((auxdata = (*docheck)(fsp)) == 0)
87 				continue;
88 			if (do_preen == 0 ||
89 			    (passno == 1 && fsp->fs_passno == 1)) {
90 				if ((name = blockcheck(fsp->fs_spec)) != NULL) {
91 					if ((sumstatus = (*chkit)(name,
92 					    fsp->fs_file, auxdata, 0)) != 0)
93 						return (sumstatus);
94 				} else if (do_preen)
95 					return (8);
96 			} else if (passno == 2 && fsp->fs_passno > 1) {
97 				if ((name = blockcheck(fsp->fs_spec)) == NULL) {
98 					fprintf(stderr, "BAD DISK NAME %s\n",
99 						fsp->fs_spec);
100 					sumstatus |= 8;
101 					continue;
102 				}
103 				addpart(name, fsp->fs_file, auxdata);
104 			}
105 		}
106 		if (do_preen == 0)
107 			return (0);
108 	}
109 	if (do_preen) {
110 		if (maxrun == 0)
111 			maxrun = ndisks;
112 		if (maxrun > ndisks)
113 			maxrun = ndisks;
114 		nextdisk = disks;
115 		for (passno = 0; passno < maxrun; ++passno) {
116 			while ((ret = startdisk(nextdisk, chkit)) && nrun > 0)
117 				sleep(10);
118 			if (ret)
119 				return (ret);
120 			nextdisk = nextdisk->next;
121 		}
122 		while ((pid = wait(&status)) != -1) {
123 			for (dk = disks; dk; dk = dk->next)
124 				if (dk->pid == pid)
125 					break;
126 			if (dk == NULL) {
127 				printf("Unknown pid %d\n", pid);
128 				continue;
129 			}
130 			if (WIFEXITED(status))
131 				retcode = WEXITSTATUS(status);
132 			else
133 				retcode = 0;
134 			if (WIFSIGNALED(status)) {
135 				printf("%s (%s): EXITED WITH SIGNAL %d\n",
136 					dk->part->name, dk->part->fsname,
137 					WTERMSIG(status));
138 				retcode = 8;
139 			}
140 			if (retcode != 0) {
141 				sumstatus |= retcode;
142 				*badnext = dk->part;
143 				badnext = &dk->part->next;
144 				dk->part = dk->part->next;
145 				*badnext = NULL;
146 			} else
147 				dk->part = dk->part->next;
148 			dk->pid = 0;
149 			nrun--;
150 			if (dk->part == NULL)
151 				ndisks--;
152 
153 			if (nextdisk == NULL) {
154 				if (dk->part) {
155 					while ((ret = startdisk(dk, chkit)) &&
156 					    nrun > 0)
157 						sleep(10);
158 					if (ret)
159 						return (ret);
160 				}
161 			} else if (nrun < maxrun && nrun < ndisks) {
162 				for ( ;; ) {
163 					if ((nextdisk = nextdisk->next) == NULL)
164 						nextdisk = disks;
165 					if (nextdisk->part != NULL &&
166 					    nextdisk->pid == 0)
167 						break;
168 				}
169 				while ((ret = startdisk(nextdisk, chkit)) &&
170 				    nrun > 0)
171 					sleep(10);
172 				if (ret)
173 					return (ret);
174 			}
175 		}
176 	}
177 	if (sumstatus) {
178 		if (badlist == NULL)
179 			return (sumstatus);
180 		fprintf(stderr, "THE FOLLOWING FILE SYSTEM%s HAD AN %s\n\t",
181 			badlist->next ? "S" : "", "UNEXPECTED INCONSISTENCY:");
182 		for (pt = badlist; pt; pt = pt->next)
183 			fprintf(stderr, "%s (%s)%s", pt->name, pt->fsname,
184 			    pt->next ? ", " : "\n");
185 		return (sumstatus);
186 	}
187 	endfsent();
188 	return (0);
189 }
190 
191 static struct disk *
192 finddisk(char *name)
193 {
194 	struct disk *dk, **dkp;
195 	char *p;
196 	size_t len;
197 
198 	p = strrchr(name, '/');
199 	p = p == NULL ? name : p + 1;
200 	while (*p != '\0' && !isdigit((u_char)*p))
201 		p++;
202 	while (isdigit((u_char)*p))
203 		p++;
204 	len = (size_t)(p - name);
205 	for (dk = disks, dkp = &disks; dk; dkp = &dk->next, dk = dk->next) {
206 		if (strncmp(dk->name, name, len) == 0 &&
207 		    dk->name[len] == 0)
208 			return (dk);
209 	}
210 	if ((*dkp = (struct disk *)malloc(sizeof(struct disk))) == NULL) {
211 		fprintf(stderr, "out of memory");
212 		exit (8);
213 	}
214 	dk = *dkp;
215 	if ((dk->name = malloc(len + 1)) == NULL) {
216 		fprintf(stderr, "out of memory");
217 		exit (8);
218 	}
219 	strncpy(dk->name, name, len);
220 	dk->name[len] = '\0';
221 	dk->part = NULL;
222 	dk->next = NULL;
223 	dk->pid = 0;
224 	ndisks++;
225 	return (dk);
226 }
227 
228 static void
229 addpart(char *name, char *fsname, long auxdata)
230 {
231 	struct disk *dk = finddisk(name);
232 	struct part *pt, **ppt = &dk->part;
233 
234 	for (pt = dk->part; pt; ppt = &pt->next, pt = pt->next)
235 		if (strcmp(pt->name, name) == 0) {
236 			printf("%s in fstab more than once!\n", name);
237 			return;
238 		}
239 	if ((*ppt = (struct part *)malloc(sizeof(struct part))) == NULL) {
240 		fprintf(stderr, "out of memory");
241 		exit (8);
242 	}
243 	pt = *ppt;
244 	if ((pt->name = malloc(strlen(name) + 1)) == NULL) {
245 		fprintf(stderr, "out of memory");
246 		exit (8);
247 	}
248 	strcpy(pt->name, name);
249 	if ((pt->fsname = malloc(strlen(fsname) + 1)) == NULL) {
250 		fprintf(stderr, "out of memory");
251 		exit (8);
252 	}
253 	strcpy(pt->fsname, fsname);
254 	pt->next = NULL;
255 	pt->auxdata = auxdata;
256 }
257 
258 static int
259 startdisk(struct disk *dk, int (*checkit)(char *, char *, long ,int))
260 {
261 	struct part *pt = dk->part;
262 
263 	dk->pid = fork();
264 	if (dk->pid < 0) {
265 		perror("fork");
266 		return (8);
267 	}
268 	if (dk->pid == 0)
269 		exit((*checkit)(pt->name, pt->fsname, pt->auxdata, 1));
270 	nrun++;
271 	return (0);
272 }
273 
274 char *
275 blockcheck(char *origname)
276 {
277 	struct stat stblock;
278 	char *newname;
279 	struct fstab *fsinfo;
280 	int retried = 0, len;
281 
282 	newname = origname;
283 retry:
284 	if (stat(newname, &stblock) < 0) {
285 		newname = getdevpath(newname, 0);
286 		if (stat(newname, &stblock) < 0) {
287 			printf("Can't stat %s: %s\n", newname, strerror(errno));
288 			return (origname);
289 		}
290 	}
291 	switch(stblock.st_mode & S_IFMT) {
292 	case S_IFCHR:
293 	case S_IFBLK:
294 		return(newname);
295 	case S_IFREG:
296 		return(newname);
297 	case S_IFDIR:
298 		if (retried)
299 			break;
300 
301 		len = strlen(origname) - 1;
302 		if (len > 0 && origname[len] == '/')
303 			/* remove trailing slash */
304 			origname[len] = '\0';
305 		if ((fsinfo = getfsfile(origname)) == NULL) {
306 			printf("Can't resolve %s to character special device",
307 			    origname);
308 			return (0);
309 		}
310 		newname = fsinfo->fs_spec;
311 		retried++;
312 		goto retry;
313 	}
314 	/*
315 	 * Not a block or character device, just return name and
316 	 * let the user decide whether to use it.
317 	 */
318 	return (origname);
319 }
320