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