fat.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /* fat.c - Read/write access to the FAT
  2. Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
  3. Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. On Debian systems, the complete text of the GNU General Public License
  15. can be found in /usr/share/common-licenses/GPL-3 file.
  16. */
  17. /* FAT32, VFAT, Atari format support, and various fixes additions May 1998
  18. * by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include "common.h"
  24. #include "dosfsck.h"
  25. #include "io.h"
  26. #include "check.h"
  27. #include "fat.h"
  28. /**
  29. * Fetch the FAT entry for a specified cluster.
  30. *
  31. * @param[out] entry Cluster to which cluster of interest is linked
  32. * @param[in] fat FAT table for the partition
  33. * @param[in] cluster Cluster of interest
  34. * @param[in] fs Information from the FAT boot sectors (bits per FAT entry)
  35. */
  36. void get_fat(FAT_ENTRY * entry, void *fat, unsigned long cluster, DOS_FS * fs)
  37. {
  38. unsigned char *ptr;
  39. switch (fs->fat_bits) {
  40. case 12:
  41. ptr = &((unsigned char *)fat)[cluster * 3 / 2];
  42. entry->value = 0xfff & (cluster & 1 ? (ptr[0] >> 4) | (ptr[1] << 4) :
  43. (ptr[0] | ptr[1] << 8));
  44. break;
  45. case 16:
  46. entry->value = CF_LE_W(((unsigned short *)fat)[cluster]);
  47. break;
  48. case 32:
  49. /* According to M$, the high 4 bits of a FAT32 entry are reserved and
  50. * are not part of the cluster number. So we cut them off. */
  51. {
  52. unsigned long e = CF_LE_L(((unsigned int *)fat)[cluster]);
  53. entry->value = e & 0xfffffff;
  54. entry->reserved = e >> 28;
  55. }
  56. break;
  57. default:
  58. die("Bad FAT entry size: %d bits.", fs->fat_bits);
  59. }
  60. }
  61. /**
  62. * Build a bookkeeping structure from the partition's FAT table.
  63. * If the partition has multiple FATs and they don't agree, try to pick a winner,
  64. * and queue a command to overwrite the loser.
  65. * One error that is fixed here is a cluster that links to something out of range.
  66. *
  67. * @param[inout] fs Information about the filesystem
  68. */
  69. void read_fat(DOS_FS * fs)
  70. {
  71. int eff_size;
  72. unsigned long i;
  73. void *first, *second = NULL;
  74. int first_ok, second_ok;
  75. unsigned long total_num_clusters;
  76. /* Clean up from previous pass */
  77. if (fs->fat)
  78. free(fs->fat);
  79. if (fs->cluster_owner)
  80. free(fs->cluster_owner);
  81. fs->fat = NULL;
  82. fs->cluster_owner = NULL;
  83. total_num_clusters = fs->clusters + 2UL;
  84. eff_size = (total_num_clusters * fs->fat_bits + 7) / 8ULL;
  85. first = alloc(eff_size);
  86. fs_read(fs->fat_start, eff_size, first);
  87. if (fs->nfats > 1) {
  88. second = alloc(eff_size);
  89. fs_read(fs->fat_start + fs->fat_size, eff_size, second);
  90. }
  91. if (second && memcmp(first, second, eff_size) != 0) {
  92. FAT_ENTRY first_media, second_media;
  93. get_fat(&first_media, first, 0, fs);
  94. get_fat(&second_media, second, 0, fs);
  95. first_ok = (first_media.value & FAT_EXTD(fs)) == FAT_EXTD(fs);
  96. second_ok = (second_media.value & FAT_EXTD(fs)) == FAT_EXTD(fs);
  97. if (first_ok && !second_ok) {
  98. printf("FATs differ - using first FAT.\n");
  99. fs_write(fs->fat_start + fs->fat_size, eff_size, first);
  100. }
  101. if (!first_ok && second_ok) {
  102. printf("FATs differ - using second FAT.\n");
  103. fs_write(fs->fat_start, eff_size, second);
  104. memcpy(first, second, eff_size);
  105. }
  106. if (first_ok && second_ok) {
  107. if (interactive) {
  108. printf("FATs differ but appear to be intact. Use which FAT ?\n"
  109. "1) Use first FAT\n2) Use second FAT\n");
  110. if (get_key("12", "?") == '1') {
  111. fs_write(fs->fat_start + fs->fat_size, eff_size, first);
  112. } else {
  113. fs_write(fs->fat_start, eff_size, second);
  114. memcpy(first, second, eff_size);
  115. }
  116. } else {
  117. printf("FATs differ but appear to be intact. Using first "
  118. "FAT.\n");
  119. fs_write(fs->fat_start + fs->fat_size, eff_size, first);
  120. }
  121. }
  122. if (!first_ok && !second_ok) {
  123. printf("Both FATs appear to be corrupt. Giving up.\n");
  124. exit(1);
  125. }
  126. }
  127. if (second) {
  128. free(second);
  129. }
  130. fs->fat = (unsigned char *)first;
  131. fs->cluster_owner = alloc(total_num_clusters * sizeof(DOS_FILE *));
  132. memset(fs->cluster_owner, 0, (total_num_clusters * sizeof(DOS_FILE *)));
  133. /* Truncate any cluster chains that link to something out of range */
  134. for (i = 2; i < fs->clusters + 2; i++) {
  135. FAT_ENTRY curEntry;
  136. get_fat(&curEntry, fs->fat, i, fs);
  137. if (curEntry.value == 1) {
  138. printf("Cluster %ld out of range (1). Setting to EOF.\n", i - 2);
  139. set_fat(fs, i, -1);
  140. }
  141. if (curEntry.value >= fs->clusters + 2 &&
  142. (curEntry.value < FAT_MIN_BAD(fs))) {
  143. printf("Cluster %ld out of range (%ld > %ld). Setting to EOF.\n",
  144. i - 2, curEntry.value, fs->clusters + 2 - 1);
  145. set_fat(fs, i, -1);
  146. }
  147. }
  148. }
  149. /**
  150. * Update the FAT entry for a specified cluster
  151. * (i.e., change the cluster it links to).
  152. * Queue a command to write out this change.
  153. *
  154. * @param[in,out] fs Information about the filesystem
  155. * @param[in] cluster Cluster to change
  156. * @param[in] new Cluster to link to
  157. * Special values:
  158. * 0 == free cluster
  159. * -1 == end-of-chain
  160. * -2 == bad cluster
  161. */
  162. void set_fat(DOS_FS * fs, unsigned long cluster, unsigned long new)
  163. {
  164. unsigned char *data = NULL;
  165. int size;
  166. loff_t offs;
  167. if ((long)new == -1)
  168. new = FAT_EOF(fs);
  169. else if ((long)new == -2)
  170. new = FAT_BAD(fs);
  171. switch (fs->fat_bits) {
  172. case 12:
  173. data = fs->fat + cluster * 3 / 2;
  174. offs = fs->fat_start + cluster * 3 / 2;
  175. if (cluster & 1) {
  176. FAT_ENTRY prevEntry;
  177. get_fat(&prevEntry, fs->fat, cluster - 1, fs);
  178. data[0] = ((new & 0xf) << 4) | (prevEntry.value >> 8);
  179. data[1] = new >> 4;
  180. } else {
  181. FAT_ENTRY subseqEntry;
  182. get_fat(&subseqEntry, fs->fat, cluster + 1, fs);
  183. data[0] = new & 0xff;
  184. data[1] = (new >> 8) | (cluster == fs->clusters - 1 ? 0 :
  185. (0xff & subseqEntry.value) << 4);
  186. }
  187. size = 2;
  188. break;
  189. case 16:
  190. data = fs->fat + cluster * 2;
  191. offs = fs->fat_start + cluster * 2;
  192. *(unsigned short *)data = CT_LE_W(new);
  193. size = 2;
  194. break;
  195. case 32:
  196. {
  197. FAT_ENTRY curEntry;
  198. get_fat(&curEntry, fs->fat, cluster, fs);
  199. data = fs->fat + cluster * 4;
  200. offs = fs->fat_start + cluster * 4;
  201. /* According to M$, the high 4 bits of a FAT32 entry are reserved and
  202. * are not part of the cluster number. So we never touch them. */
  203. *(unsigned long *)data = CT_LE_L((new & 0xfffffff) |
  204. (curEntry.reserved << 28));
  205. size = 4;
  206. }
  207. break;
  208. default:
  209. die("Bad FAT entry size: %d bits.", fs->fat_bits);
  210. }
  211. fs_write(offs, size, data);
  212. if (fs->nfats > 1) {
  213. fs_write(offs + fs->fat_size, size, data);
  214. }
  215. }
  216. int bad_cluster(DOS_FS * fs, unsigned long cluster)
  217. {
  218. FAT_ENTRY curEntry;
  219. get_fat(&curEntry, fs->fat, cluster, fs);
  220. return FAT_IS_BAD(fs, curEntry.value);
  221. }
  222. /**
  223. * Get the cluster to which the specified cluster is linked.
  224. * If the linked cluster is marked bad, abort.
  225. *
  226. * @param[in] fs Information about the filesystem
  227. * @param[in] cluster Cluster to follow
  228. *
  229. * @return -1 'cluster' is at the end of the chain
  230. * @return Other values Next cluster in this chain
  231. */
  232. unsigned long next_cluster(DOS_FS * fs, unsigned long cluster)
  233. {
  234. unsigned long value;
  235. FAT_ENTRY curEntry;
  236. get_fat(&curEntry, fs->fat, cluster, fs);
  237. value = curEntry.value;
  238. if (FAT_IS_BAD(fs, value))
  239. die("Internal error: next_cluster on bad cluster");
  240. return FAT_IS_EOF(fs, value) ? -1 : value;
  241. }
  242. loff_t cluster_start(DOS_FS * fs, unsigned long cluster)
  243. {
  244. return fs->data_start + ((loff_t) cluster -
  245. 2) * (unsigned long long)fs->cluster_size;
  246. }
  247. /**
  248. * Update internal bookkeeping to show that the specified cluster belongs
  249. * to the specified dentry.
  250. *
  251. * @param[in,out] fs Information about the filesystem
  252. * @param[in] cluster Cluster being assigned
  253. * @param[in] owner Information on dentry that owns this cluster
  254. * (may be NULL)
  255. */
  256. void set_owner(DOS_FS * fs, unsigned long cluster, DOS_FILE * owner)
  257. {
  258. if (fs->cluster_owner == NULL)
  259. die("Internal error: attempt to set owner in non-existent table");
  260. if (owner && fs->cluster_owner[cluster]
  261. && (fs->cluster_owner[cluster] != owner))
  262. die("Internal error: attempt to change file owner");
  263. fs->cluster_owner[cluster] = owner;
  264. }
  265. DOS_FILE *get_owner(DOS_FS * fs, unsigned long cluster)
  266. {
  267. if (fs->cluster_owner == NULL)
  268. return NULL;
  269. else
  270. return fs->cluster_owner[cluster];
  271. }
  272. void fix_bad(DOS_FS * fs)
  273. {
  274. unsigned long i;
  275. if (verbose)
  276. printf("Checking for bad clusters.\n");
  277. for (i = 2; i < fs->clusters + 2; i++) {
  278. FAT_ENTRY curEntry;
  279. get_fat(&curEntry, fs->fat, i, fs);
  280. if (!get_owner(fs, i) && !FAT_IS_BAD(fs, curEntry.value))
  281. if (!fs_test(cluster_start(fs, i), fs->cluster_size)) {
  282. printf("Cluster %lu is unreadable.\n", i);
  283. set_fat(fs, i, -2);
  284. }
  285. }
  286. }
  287. void reclaim_free(DOS_FS * fs)
  288. {
  289. int reclaimed;
  290. unsigned long i;
  291. if (verbose)
  292. printf("Checking for unused clusters.\n");
  293. reclaimed = 0;
  294. for (i = 2; i < fs->clusters + 2; i++) {
  295. FAT_ENTRY curEntry;
  296. get_fat(&curEntry, fs->fat, i, fs);
  297. if (!get_owner(fs, i) && curEntry.value &&
  298. !FAT_IS_BAD(fs, curEntry.value)) {
  299. set_fat(fs, i, 0);
  300. reclaimed++;
  301. }
  302. }
  303. if (reclaimed)
  304. printf("Reclaimed %d unused cluster%s (%llu bytes).\n", reclaimed,
  305. reclaimed == 1 ? "" : "s",
  306. (unsigned long long)reclaimed * fs->cluster_size);
  307. }
  308. /**
  309. * Assign the specified owner to all orphan chains (except cycles).
  310. * Break cross-links between orphan chains.
  311. *
  312. * @param[in,out] fs Information about the filesystem
  313. * @param[in] owner dentry to be assigned ownership of orphans
  314. * @param[in,out] num_refs For each orphan cluster [index], how many
  315. * clusters link to it.
  316. * @param[in] start_cluster Where to start scanning for orphans
  317. */
  318. static void tag_free(DOS_FS * fs, DOS_FILE * owner, unsigned long *num_refs,
  319. unsigned long start_cluster)
  320. {
  321. int prev;
  322. unsigned long i, walk;
  323. if (start_cluster == 0)
  324. start_cluster = 2;
  325. for (i = start_cluster; i < fs->clusters + 2; i++) {
  326. FAT_ENTRY curEntry;
  327. get_fat(&curEntry, fs->fat, i, fs);
  328. /* If the current entry is the head of an un-owned chain... */
  329. if (curEntry.value && !FAT_IS_BAD(fs, curEntry.value) &&
  330. !get_owner(fs, i) && !num_refs[i]) {
  331. prev = 0;
  332. /* Walk the chain, claiming ownership as we go */
  333. for (walk = i; walk != -1; walk = next_cluster(fs, walk)) {
  334. if (!get_owner(fs, walk)) {
  335. set_owner(fs, walk, owner);
  336. } else {
  337. /* We've run into cross-links between orphaned chains,
  338. * or a cycle with a tail.
  339. * Terminate this orphan chain (break the link)
  340. */
  341. set_fat(fs, prev, -1);
  342. /* This is not necessary because 'walk' is owned and thus
  343. * will never become the head of a chain (the only case
  344. * that would matter during reclaim to files).
  345. * It's easier to decrement than to prove that it's
  346. * unnecessary.
  347. */
  348. num_refs[walk]--;
  349. break;
  350. }
  351. prev = walk;
  352. }
  353. }
  354. }
  355. }
  356. /**
  357. * Recover orphan chains to files, handling any cycles or cross-links.
  358. *
  359. * @param[in,out] fs Information about the filesystem
  360. */
  361. void reclaim_file(DOS_FS * fs)
  362. {
  363. DOS_FILE orphan;
  364. int reclaimed, files;
  365. int changed = 0;
  366. unsigned long i, next, walk;
  367. unsigned long *num_refs = NULL; /* Only for orphaned clusters */
  368. unsigned long total_num_clusters;
  369. if (verbose)
  370. printf("Reclaiming unconnected clusters.\n");
  371. total_num_clusters = fs->clusters + 2UL;
  372. num_refs = alloc(total_num_clusters * sizeof(unsigned long));
  373. memset(num_refs, 0, (total_num_clusters * sizeof(unsigned long)));
  374. /* Guarantee that all orphan chains (except cycles) end cleanly
  375. * with an end-of-chain mark.
  376. */
  377. for (i = 2; i < total_num_clusters; i++) {
  378. FAT_ENTRY curEntry;
  379. get_fat(&curEntry, fs->fat, i, fs);
  380. next = curEntry.value;
  381. if (!get_owner(fs, i) && next && next < fs->clusters + 2) {
  382. /* Cluster is linked, but not owned (orphan) */
  383. FAT_ENTRY nextEntry;
  384. get_fat(&nextEntry, fs->fat, next, fs);
  385. /* Mark it end-of-chain if it links into an owned cluster,
  386. * a free cluster, or a bad cluster.
  387. */
  388. if (get_owner(fs, next) || !nextEntry.value ||
  389. FAT_IS_BAD(fs, nextEntry.value))
  390. set_fat(fs, i, -1);
  391. else
  392. num_refs[next]++;
  393. }
  394. }
  395. /* Scan until all the orphans are accounted for,
  396. * and all cycles and cross-links are broken
  397. */
  398. do {
  399. tag_free(fs, &orphan, num_refs, changed);
  400. changed = 0;
  401. /* Any unaccounted-for orphans must be part of a cycle */
  402. for (i = 2; i < total_num_clusters; i++) {
  403. FAT_ENTRY curEntry;
  404. get_fat(&curEntry, fs->fat, i, fs);
  405. if (curEntry.value && !FAT_IS_BAD(fs, curEntry.value) &&
  406. !get_owner(fs, i)) {
  407. if (!num_refs[curEntry.value]--)
  408. die("Internal error: num_refs going below zero");
  409. set_fat(fs, i, -1);
  410. changed = curEntry.value;
  411. printf("Broke cycle at cluster %lu in free chain.\n", i);
  412. /* If we've created a new chain head,
  413. * tag_free() can claim it
  414. */
  415. if (num_refs[curEntry.value] == 0)
  416. break;
  417. }
  418. }
  419. }
  420. while (changed);
  421. /* Now we can start recovery */
  422. files = reclaimed = 0;
  423. for (i = 2; i < total_num_clusters; i++)
  424. /* If this cluster is the head of an orphan chain... */
  425. if (get_owner(fs, i) == &orphan && !num_refs[i]) {
  426. DIR_ENT de;
  427. loff_t offset;
  428. files++;
  429. offset = alloc_rootdir_entry(fs, &de, "FSCK%04d");
  430. de.start = CT_LE_W(i & 0xffff);
  431. if (fs->fat_bits == 32)
  432. de.starthi = CT_LE_W(i >> 16);
  433. for (walk = i; walk > 0 && walk != -1;
  434. walk = next_cluster(fs, walk)) {
  435. de.size = CT_LE_L(CF_LE_L(de.size) + fs->cluster_size);
  436. reclaimed++;
  437. }
  438. fs_write(offset, sizeof(DIR_ENT), &de);
  439. }
  440. if (reclaimed)
  441. printf("Reclaimed %d unused cluster%s (%llu bytes) in %d chain%s.\n",
  442. reclaimed, reclaimed == 1 ? "" : "s",
  443. (unsigned long long)reclaimed * fs->cluster_size, files,
  444. files == 1 ? "" : "s");
  445. free(num_refs);
  446. }
  447. unsigned long update_free(DOS_FS * fs)
  448. {
  449. unsigned long i;
  450. unsigned long free = 0;
  451. int do_set = 0;
  452. for (i = 2; i < fs->clusters + 2; i++) {
  453. FAT_ENTRY curEntry;
  454. get_fat(&curEntry, fs->fat, i, fs);
  455. if (!get_owner(fs, i) && !FAT_IS_BAD(fs, curEntry.value))
  456. ++free;
  457. }
  458. if (!fs->fsinfo_start)
  459. return free;
  460. if (verbose)
  461. printf("Checking free cluster summary.\n");
  462. if (fs->free_clusters != 0xFFFFFFFF) {
  463. if (free != fs->free_clusters) {
  464. printf("Free cluster summary wrong (%ld vs. really %ld)\n",
  465. fs->free_clusters, free);
  466. if (interactive)
  467. printf("1) Correct\n2) Don't correct\n");
  468. else
  469. printf(" Auto-correcting.\n");
  470. if (!interactive || get_key("12", "?") == '1')
  471. do_set = 1;
  472. }
  473. } else {
  474. printf("Free cluster summary uninitialized (should be %ld)\n", free);
  475. if (rw) {
  476. if (interactive)
  477. printf("1) Set it\n2) Leave it uninitialized\n");
  478. else
  479. printf(" Auto-setting.\n");
  480. if (!interactive || get_key("12", "?") == '1')
  481. do_set = 1;
  482. }
  483. }
  484. if (do_set) {
  485. unsigned long le_free = CT_LE_L(free);
  486. fs->free_clusters = free;
  487. fs_write(fs->fsinfo_start + offsetof(struct info_sector, free_clusters),
  488. sizeof(le_free), &le_free);
  489. }
  490. return free;
  491. }