[VFATLIB]
[reactos.git] / reactos / lib / fslib / vfatlib / check / fat.c
1 /* fat.c - Read/write access to the FAT */
2
3 /* Written 1993 by Werner Almesberger */
4
5 /* FAT32, VFAT, Atari format support, and various fixes additions May 1998
6 * by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
7
8
9 #include "vfatlib.h"
10
11 #define NDEBUG
12 #include <debug.h>
13
14 #include "check.h"
15
16 //#pragma warning(disable: 4018)
17
18 static void get_fat(FAT_ENTRY *entry,void *fat,unsigned long cluster,DOS_FS *fs)
19 {
20 unsigned char *ptr;
21
22 switch(fs->fat_bits) {
23 case 12:
24 ptr = &((unsigned char *) fat)[cluster*3/2];
25 entry->value = 0xfff & (cluster & 1 ? (ptr[0] >> 4) | (ptr[1] << 4) :
26 (ptr[0] | ptr[1] << 8));
27 break;
28 case 16:
29 entry->value = CF_LE_W(((unsigned short *) fat)[cluster]);
30 break;
31 case 32:
32 /* According to M$, the high 4 bits of a FAT32 entry are reserved and
33 * are not part of the cluster number. So we cut them off. */
34 {
35 unsigned long e = CF_LE_L(((unsigned int *) fat)[cluster]);
36 entry->value = e & 0xfffffff;
37 entry->reserved = e >> 28;
38 }
39 break;
40 default:
41 die("Bad FAT entry size: %d bits.",fs->fat_bits);
42 }
43 entry->owner = NULL;
44 }
45
46
47 void read_fat(DOS_FS *fs)
48 {
49 int eff_size;
50 unsigned long i;
51 void *first,*second,*use;
52 int first_ok,second_ok;
53
54 eff_size = ((fs->clusters+2)*fs->fat_bits+7)/8;
55 // TMN: Must round up to disk-sector boundary. For now, assume 512-byte disk.
56 if (eff_size % 512) {
57 eff_size += 512 - (eff_size % 512);
58 }
59 first = vfalloc(eff_size);
60 fs_read(fs->fat_start,eff_size,first);
61 use = first;
62 if (fs->nfats > 1) {
63 second = vfalloc(eff_size);
64 fs_read(fs->fat_start+fs->fat_size,eff_size,second);
65 }
66 else
67 second = NULL;
68 if (second && memcmp(first,second,eff_size) != 0) {
69 FAT_ENTRY first_media, second_media;
70 memset (&first_media, 0, sizeof (first_media));
71 memset (&second_media, 0, sizeof (second_media));
72 get_fat(&first_media,first,0,fs);
73 get_fat(&second_media,second,0,fs);
74 first_ok = (first_media.value & FAT_EXTD(fs)) == FAT_EXTD(fs);
75 second_ok = (second_media.value & FAT_EXTD(fs)) == FAT_EXTD(fs);
76 if (first_ok && !second_ok) {
77 VfatPrint("FATs differ - using first FAT.\n");
78 fs_write(fs->fat_start+fs->fat_size,eff_size,use = first);
79 }
80 if (!first_ok && second_ok) {
81 VfatPrint("FATs differ - using second FAT.\n");
82 fs_write(fs->fat_start,eff_size,use = second);
83 }
84 if (first_ok && second_ok) {
85 if (FsCheckFlags & FSCHECK_INTERACTIVE) {
86 VfatPrint("FATs differ but appear to be intact. Use which FAT ?\n"
87 "1) Use first FAT\n2) Use second FAT\n");
88 if (get_key("12","?") == '1')
89 fs_write(fs->fat_start+fs->fat_size,eff_size,use = first);
90 else fs_write(fs->fat_start,eff_size,use = second);
91 }
92 else {
93 VfatPrint("FATs differ but appear to be intact. Using first "
94 "FAT.\n");
95 fs_write(fs->fat_start+fs->fat_size,eff_size,use = first);
96 }
97 }
98 if (!first_ok && !second_ok) {
99 VfatPrint("Both FATs appear to be corrupt. Giving up.\n");
100 //exit(1);
101 }
102 }
103 fs->fat = qalloc(&FsCheckMemQueue,sizeof(FAT_ENTRY)*(fs->clusters+2));
104 for (i = 2; i < fs->clusters+2; i++) get_fat(&fs->fat[i],use,i,fs);
105 for (i = 2; i < fs->clusters+2; i++)
106 if (fs->fat[i].value >= fs->clusters+2 &&
107 (fs->fat[i].value < FAT_MIN_BAD(fs))) {
108 VfatPrint("Cluster %ld out of range (%ld > %ld). Setting to EOF.\n",
109 i-2,fs->fat[i].value,fs->clusters+2-1);
110 set_fat(fs,i,-1);
111 }
112 vffree(first);
113 if (second)
114 vffree(second);
115 }
116
117
118 void set_fat(DOS_FS *fs,unsigned long cluster,unsigned long new)
119 {
120 unsigned char data[4];
121 int size = 0;
122 loff_t offs = 0LL;
123
124 if ((long)new == -1)
125 new = FAT_EOF(fs);
126 else if ((long)new == -2)
127 new = FAT_BAD(fs);
128 switch( fs->fat_bits ) {
129 case 12:
130 offs = fs->fat_start+cluster*3/2;
131 if (cluster & 1) {
132 data[0] = ((new & 0xf) << 4) | (fs->fat[cluster-1].value >> 8);
133 data[1] = new >> 4;
134 }
135 else {
136 data[0] = new & 0xff;
137 data[1] = (new >> 8) | (cluster == fs->clusters-1 ? 0 :
138 (0xff & fs->fat[cluster+1].value) << 4);
139 }
140 size = 2;
141 break;
142 case 16:
143 offs = fs->fat_start+cluster*2;
144 *(unsigned short *) data = CT_LE_W(new);
145 size = 2;
146 break;
147 case 32:
148 offs = fs->fat_start+cluster*4;
149 /* According to M$, the high 4 bits of a FAT32 entry are reserved and
150 * are not part of the cluster number. So we never touch them. */
151 *(unsigned long *) data = CT_LE_L( (new & 0xfffffff) |
152 (fs->fat[cluster].reserved << 28) );
153 size = 4;
154 break;
155 default:
156 die("Bad FAT entry size: %d bits.",fs->fat_bits);
157 }
158 fs->fat[cluster].value = new;
159 fs_write(offs,size,&data);
160 fs_write(offs+fs->fat_size,size,&data);
161 }
162
163
164 int bad_cluster(DOS_FS *fs,unsigned long cluster)
165 {
166 return FAT_IS_BAD(fs,fs->fat[cluster].value);
167 }
168
169
170 unsigned long next_cluster(DOS_FS *fs,unsigned long cluster)
171 {
172 unsigned long value;
173
174 value = fs->fat[cluster].value;
175 if (FAT_IS_BAD(fs,value))
176 die("Internal error: next_cluster on bad cluster");
177 return FAT_IS_EOF(fs,value) ? -1 : value;
178 }
179
180
181 loff_t cluster_start(DOS_FS *fs,unsigned long cluster)
182 {
183 return fs->data_start+((loff_t)cluster-2)*fs->cluster_size;
184 }
185
186
187 void set_owner(DOS_FS *fs,unsigned long cluster,DOS_FILE *owner)
188 {
189 if (owner && fs->fat[cluster].owner)
190 die("Internal error: attempt to change file owner");
191 fs->fat[cluster].owner = owner;
192 }
193
194
195 DOS_FILE *get_owner(DOS_FS *fs,unsigned long cluster)
196 {
197 return fs->fat[cluster].owner;
198 }
199
200
201 void fix_bad(DOS_FS *fs)
202 {
203 unsigned long i;
204
205 if (FsCheckFlags & FSCHECK_VERBOSE)
206 VfatPrint("Checking for bad clusters.\n");
207 for (i = 2; i < fs->clusters+2; i++)
208 if (!get_owner(fs,i) && !FAT_IS_BAD(fs,fs->fat[i].value))
209 if (!fs_test(cluster_start(fs,i),fs->cluster_size)) {
210 VfatPrint("Cluster %lu is unreadable.\n",i);
211 set_fat(fs,i,-2);
212 }
213 }
214
215
216 void reclaim_free(DOS_FS *fs)
217 {
218 int reclaimed;
219 unsigned long i;
220
221 if (FsCheckFlags & FSCHECK_VERBOSE)
222 VfatPrint("Checking for unused clusters.\n");
223 reclaimed = 0;
224 for (i = 2; i < fs->clusters+2; i++)
225 if (!get_owner(fs,i) && fs->fat[i].value &&
226 !FAT_IS_BAD(fs,fs->fat[i].value)) {
227 set_fat(fs,i,0);
228 reclaimed++;
229 }
230 if (reclaimed)
231 VfatPrint("Reclaimed %d unused cluster%s (%d bytes).\n",reclaimed,
232 reclaimed == 1 ? "" : "s",reclaimed*fs->cluster_size);
233 }
234
235
236 static void tag_free(DOS_FS *fs,DOS_FILE *ptr)
237 {
238 DOS_FILE *owner;
239 int prev;
240 unsigned long i,walk;
241
242 for (i = 2; i < fs->clusters+2; i++)
243 if (fs->fat[i].value && !FAT_IS_BAD(fs,fs->fat[i].value) &&
244 !get_owner(fs,i) && !fs->fat[i].prev) {
245 prev = 0;
246 for (walk = i; walk > 0 && walk != -1;
247 walk = next_cluster(fs,walk)) {
248 if (!(owner = get_owner(fs,walk))) set_owner(fs,walk,ptr);
249 else if (owner != ptr)
250 die("Internal error: free chain collides with file");
251 else {
252 set_fat(fs,prev,-1);
253 break;
254 }
255 prev = walk;
256 }
257 }
258 }
259
260
261 void reclaim_file(DOS_FS *fs)
262 {
263 DOS_FILE dummy;
264 int reclaimed,files,changed;
265 unsigned long i,next,walk;
266
267 if (FsCheckFlags & FSCHECK_VERBOSE)
268 VfatPrint("Reclaiming unconnected clusters.\n");
269 for (i = 2; i < fs->clusters+2; i++) fs->fat[i].prev = 0;
270 for (i = 2; i < fs->clusters+2; i++) {
271 next = fs->fat[i].value;
272 if (!get_owner(fs,i) && next && next < fs->clusters+2) {
273 if (get_owner(fs,next) || !fs->fat[next].value ||
274 FAT_IS_BAD(fs,fs->fat[next].value)) set_fat(fs,i,-1);
275 else fs->fat[next].prev++;
276 }
277 }
278 do {
279 tag_free(fs,&dummy);
280 changed = 0;
281 for (i = 2; i < fs->clusters+2; i++)
282 if (fs->fat[i].value && !FAT_IS_BAD(fs,fs->fat[i].value) &&
283 !get_owner(fs, i)) {
284 if (!fs->fat[fs->fat[i].value].prev--)
285 die("Internal error: prev going below zero");
286 set_fat(fs,i,-1);
287 changed = 1;
288 VfatPrint("Broke cycle at cluster %lu in free chain.\n",i);
289 break;
290 }
291 }
292 while (changed);
293 files = reclaimed = 0;
294 for (i = 2; i < fs->clusters+2; i++)
295 if (get_owner(fs,i) == &dummy && !fs->fat[i].prev) {
296 DIR_ENT de;
297 loff_t offset;
298 files++;
299 offset = alloc_rootdir_entry(fs,&de,"FSCK%04dREC");
300 de.start = CT_LE_W(i&0xffff);
301 if (fs->fat_bits == 32)
302 de.starthi = CT_LE_W(i>>16);
303 for (walk = i; walk > 0 && walk != -1;
304 walk = next_cluster(fs,walk)) {
305 de.size = CT_LE_L(CF_LE_L(de.size)+fs->cluster_size);
306 reclaimed++;
307 }
308 fs_write(offset,sizeof(DIR_ENT),&de);
309 }
310 if (reclaimed)
311 VfatPrint("Reclaimed %d unused cluster%s (%d bytes) in %d chain%s.\n",
312 reclaimed,reclaimed == 1 ? "" : "s",reclaimed*fs->cluster_size,files,
313 files == 1 ? "" : "s");
314 }
315
316
317 unsigned long update_free(DOS_FS *fs)
318 {
319 unsigned long i;
320 unsigned long free = 0;
321 int do_set = 0;
322
323 for (i = 2; i < fs->clusters+2; i++)
324 if (!get_owner(fs,i) && !FAT_IS_BAD(fs,fs->fat[i].value))
325 ++free;
326
327 if (!fs->fsinfo_start)
328 return free;
329
330 if (FsCheckFlags & FSCHECK_VERBOSE)
331 VfatPrint("Checking free cluster summary.\n");
332 if (fs->free_clusters >= 0) {
333 if (free != fs->free_clusters) {
334 VfatPrint( "Free cluster summary wrong (%ld vs. really %ld)\n",
335 fs->free_clusters,free);
336 if (FsCheckFlags & FSCHECK_INTERACTIVE)
337 VfatPrint( "1) Correct\n2) Don't correct\n" );
338 else VfatPrint( " Auto-correcting.\n" );
339 if (!(FsCheckFlags & FSCHECK_INTERACTIVE) || get_key("12","?") == '1')
340 do_set = 1;
341 }
342 }
343 else {
344 VfatPrint( "Free cluster summary uninitialized (should be %ld)\n", free );
345 if (FsCheckFlags & FSCHECK_INTERACTIVE)
346 VfatPrint( "1) Set it\n2) Leave it uninitialized\n" );
347 else VfatPrint( " Auto-setting.\n" );
348 if (!(FsCheckFlags & FSCHECK_INTERACTIVE) || get_key("12","?") == '1')
349 do_set = 1;
350 }
351
352 if (do_set) {
353 fs->free_clusters = free;
354 free = CT_LE_L(free);
355 fs_write(fs->fsinfo_start+offsetof(struct info_sector,free_clusters),
356 sizeof(free),&free);
357 }
358
359 return free;
360 }
361
362 /* Local Variables: */
363 /* tab-width: 8 */
364 /* End: */