xref: /openbsd-src/sbin/fsck_msdos/check.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: check.c,v 1.16 2014/06/16 18:33:33 tobias Exp $	*/
2 /*	$NetBSD: check.c,v 1.8 1997/10/17 11:19:29 ws Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
6  * Copyright (c) 1995 Martin Husemann
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/ioctl.h>
31 #include <sys/dkio.h>
32 #include <sys/disklabel.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <util.h>
40 
41 #include "ext.h"
42 
43 struct disklabel lab;
44 
45 int
46 checkfilesys(const char *fname)
47 {
48 	int dosfs;
49 	struct bootblock boot;
50 	struct fatEntry *fat = NULL;
51 	char *realdev;
52 	int i;
53 	int mod = 0;
54 
55 	rdonly = alwaysno;
56 
57 	dosfs = opendev(fname, rdonly ? O_RDONLY : O_RDWR, 0, &realdev);
58 	if (dosfs < 0 && !rdonly) {
59 		dosfs = opendev(fname, O_RDONLY, 0, &realdev);
60 		rdonly = 1;
61 	}
62 	if (dosfs < 0) {
63 		xperror("Can't open");
64 		return (8);
65 	}
66 
67 	if (!preen) {
68 		printf("** %s", realdev);
69 		if (strncmp(fname, realdev, PATH_MAX) != 0)
70 			printf(" (%s)", fname);
71 		if (rdonly)
72 			printf(" (NO WRITE)");
73 		printf("\n");
74 	}
75 
76 	if (ioctl(dosfs, DIOCGDINFO, (char *)&lab) < 0)
77 		pfatal("can't read disk label for %s\n", fname);
78 
79 	if (readboot(dosfs, &boot) != FSOK) {
80 		(void)close(dosfs);
81 		return (8);
82 	}
83 
84 	if (!preen) {
85 		if (boot.ValidFat < 0)
86 			printf("** Phase 1 - Read and Compare FATs\n");
87 		else
88 			printf("** Phase 1 - Read FAT\n");
89 	}
90 
91 	mod |= readfat(dosfs, &boot, boot.ValidFat >= 0 ? boot.ValidFat : 0, &fat);
92 	if (mod & FSFATAL) {
93 		(void)close(dosfs);
94 		return 8;
95 	}
96 
97 	if (boot.ValidFat < 0)
98 		for (i = 1; i < boot.FATs; i++) {
99 			struct fatEntry *currentFat;
100 			mod |= readfat(dosfs, &boot, i, &currentFat);
101 
102 			if (mod & FSFATAL) {
103 				free(fat);
104 				(void)close(dosfs);
105 				return 8;
106 			}
107 
108 			mod |= comparefat(&boot, fat, currentFat, i);
109 			free(currentFat);
110 			if (mod & FSFATAL) {
111 				free(fat);
112 				(void)close(dosfs);
113 				return (8);
114 			}
115 		}
116 
117 	if (!preen)
118 		printf("** Phase 2 - Check Cluster Chains\n");
119 
120 	mod |= checkfat(&boot, fat);
121 	if (mod & FSFATAL) {
122 		free(fat);
123 		(void)close(dosfs);
124 		return (8);
125 	}
126 
127 	if (mod & FSFATMOD)
128 		mod |= writefat(dosfs, &boot, fat); /* delay writing fats?	XXX */
129 	if (mod & FSFATAL) {
130 		free(fat);
131 		(void)close(dosfs);
132 		return (8);
133 	}
134 
135 	if (!preen)
136 		printf("** Phase 3 - Check Directories\n");
137 
138 	mod |= resetDosDirSection(&boot, fat);
139 	if (mod & FSFATAL) {
140 		free(fat);
141 		close(dosfs);
142 		return 8;
143 	}
144 
145 	if (mod & FSFATMOD)
146 		mod |= writefat(dosfs, &boot, fat); /* delay writing fats?	XXX */
147 	if (mod & FSFATAL) {
148 		finishDosDirSection();
149 		free(fat);
150 		(void)close(dosfs);
151 		return (8);
152 	}
153 
154 	mod |= handleDirTree(dosfs, &boot, fat);
155 	if (mod & FSFATAL) {
156 		finishDosDirSection();
157 		free(fat);
158 		(void)close(dosfs);
159 		return (8);
160 	}
161 
162 	if (!preen)
163 		printf("** Phase 4 - Check for Lost Files\n");
164 
165 	mod |= checklost(dosfs, &boot, fat);
166 	if (mod & FSFATAL) {
167 		finishDosDirSection();
168 		free(fat);
169 		(void)close(dosfs);
170 		return 8;
171 	}
172 
173 	if (mod & FSFATMOD)
174 		mod |= writefat(dosfs, &boot, fat); /* delay writing fats?    XXX */
175 
176 	finishDosDirSection();
177 	free(fat);
178 	(void)close(dosfs);
179 	if (mod & FSFATAL)
180 		return 8;
181 
182 	if (boot.NumBad)
183 		pwarn("%d files, %d free (%d clusters), %d bad (%d clusters)\n",
184 		      boot.NumFiles,
185 		      boot.NumFree * boot.ClusterSize / 1024, boot.NumFree,
186 		      boot.NumBad * boot.ClusterSize / 1024, boot.NumBad);
187 	else
188 		pwarn("%d files, %d free (%d clusters)\n",
189 		      boot.NumFiles,
190 		      boot.NumFree * boot.ClusterSize / 1024, boot.NumFree);
191 
192 	if (mod & (FSFATAL | FSERROR))
193 		return (8);
194 	if (mod) {
195 		pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
196 		return (4);
197 	}
198 	return (0);
199 }
200