fat.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. free(fs->fat);
  78. free(fs->cluster_owner);
  79. fs->fat = NULL;
  80. fs->cluster_owner = NULL;
  81. total_num_clusters = fs->clusters + 2UL;
  82. eff_size = (total_num_clusters * fs->fat_bits + 7) / 8ULL;
  83. first = alloc(eff_size);
  84. fs_read(fs->fat_start, eff_size, first);
  85. if (fs->nfats > 1) {
  86. second = alloc(eff_size);
  87. fs_read(fs->fat_start + fs->fat_size, eff_size, second);
  88. }
  89. if (second && memcmp(first, second, eff_size) != 0) {
  90. FAT_ENTRY first_media, second_media;
  91. get_fat(&first_media, first, 0, fs);
  92. get_fat(&second_media, second, 0, fs);
  93. first_ok = (first_media.value & FAT_EXTD(fs)) == FAT_EXTD(fs);
  94. second_ok = (second_media.value & FAT_EXTD(fs)) == FAT_EXTD(fs);
  95. if (first_ok && !second_ok) {
  96. printf("FATs differ - using first FAT.\n");
  97. fs_write(fs->fat_start + fs->fat_size, eff_size, first);
  98. }
  99. if (!first_ok && second_ok) {
  100. printf("FATs differ - using second FAT.\n");
  101. fs_write(fs->fat_start, eff_size, second);
  102. memcpy(first, second, eff_size);
  103. }
  104. if (first_ok && second_ok) {
  105. if (interactive) {
  106. printf("FATs differ but appear to be intact. Use which FAT ?\n"
  107. "1) Use first FAT\n2) Use second FAT\n");
  108. if (get_key("12", "?") == '1') {
  109. fs_write(fs->fat_start + fs->fat_size, eff_size, first);
  110. } else {
  111. fs_write(fs->fat_start, eff_size, second);
  112. memcpy(first, second, eff_size);
  113. }
  114. } else {
  115. printf("FATs differ but appear to be intact. Using first "
  116. "FAT.\n");
  117. fs_write(fs->fat_start + fs->fat_size, eff_size, first);
  118. }
  119. }
  120. if (!first_ok && !second_ok) {
  121. printf("Both FATs appear to be corrupt. Giving up.\n");
  122. exit(1);
  123. }
  124. }
  125. if (second) {
  126. free(second);
  127. }
  128. fs->fat = (unsigned char *)first;
  129. fs->cluster_owner = alloc(total_num_clusters * sizeof(DOS_FILE *));
  130. memset(fs->cluster_owner, 0, (total_num_clusters * sizeof(DOS_FILE *)));
  131. /* Truncate any cluster chains that link to something out of range */
  132. for (i = 2; i < fs->clusters + 2; i++) {
  133. FAT_ENTRY curEntry;
  134. get_fat(&curEntry, fs->fat, i, fs);
  135. if (curEntry.value == 1) {
  136. printf("Cluster %ld out of range (1). Setting to EOF.\n", i - 2);
  137. set_fat(fs, i, -1);
  138. }
  139. if (curEntry.value >= fs->clusters + 2 &&
  140. (curEntry.value < FAT_MIN_BAD(fs))) {
  141. printf("Cluster %ld out of range (%ld > %ld). Setting to EOF.\n",
  142. i - 2, curEntry.value, fs->clusters + 2 - 1);
  143. set_fat(fs, i, -1);
  144. }
  145. }
  146. }
  147. /**
  148. * Update the FAT entry for a specified cluster
  149. * (i.e., change the cluster it links to).
  150. * Queue a command to write out this change.
  151. *
  152. * @param[in,out] fs Information about the filesystem
  153. * @param[in] cluster Cluster to change
  154. * @param[in] new Cluster to link to
  155. * Special values:
  156. * 0 == free cluster
  157. * -1 == end-of-chain
  158. * -2 == bad cluster
  159. */
  160. void set_fat(DOS_FS * fs, unsigned long cluster, unsigned long new)
  161. {
  162. unsigned char *data = NULL;
  163. int size;
  164. loff_t offs;
  165. if ((long)new == -1)
  166. new = FAT_EOF(fs);
  167. else if ((long)new == -2)
  168. new = FAT_BAD(fs);
  169. switch (fs->fat_bits) {
  170. case 12:
  171. data = fs->fat + cluster * 3 / 2;
  172. offs = fs->fat_start + cluster * 3 / 2;
  173. if (cluster & 1) {
  174. FAT_ENTRY prevEntry;
  175. get_fat(&prevEntry, fs->fat, cluster - 1, fs);
  176. data[0] = ((new & 0xf) << 4) | (prevEntry.value >> 8);
  177. data[1] = new >> 4;
  178. } else {
  179. FAT_ENTRY subseqEntry;
  180. get_fat(&subseqEntry, fs->fat, cluster + 1, fs);
  181. data[0] = new & 0xff;
  182. data[1] = (new >> 8) | (cluster == fs->clusters - 1 ? 0 :
  183. (0xff & subseqEntry.value) << 4);
  184. }
  185. size = 2;
  186. break;
  187. case 16:
  188. data = fs->fat + cluster * 2;
  189. offs = fs->fat_start + cluster * 2;
  190. *(unsigned short *)data = CT_LE_W(new);
  191. size = 2;
  192. break;
  193. case 32:
  194. {
  195. FAT_ENTRY curEntry;
  196. get_fat(&curEntry, fs->fat, cluster, fs);
  197. data = fs->fat + cluster * 4;
  198. offs = fs->fat_start + cluster * 4;
  199. /* According to M$, the high 4 bits of a FAT32 entry are reserved and
  200. * are not part of the cluster number. So we never touch them. */
  201. *(unsigned long *)data = CT_LE_L((new & 0xfffffff) |
  202. (curEntry.reserved << 28));
  203. size = 4;
  204. }
  205. break;
  206. default:
  207. die("Bad FAT entry size: %d bits.", fs->fat_bits);
  208. }
  209. fs_write(offs, size, data);
  210. if (fs->nfats > 1) {
  211. fs_write(offs + fs->fat_size, size, data);
  212. }
  213. }
  214. int bad_cluster(DOS_FS * fs, unsigned long cluster)
  215. {
  216. FAT_ENTRY curEntry;
  217. get_fat(&curEntry, fs->fat, cluster, fs);
  218. return FAT_IS_BAD(fs, curEntry.value);
  219. }
  220. /**
  221. * Get the cluster to which the specified cluster is linked.
  222. * If the linked cluster is marked bad, abort.
  223. *
  224. * @param[in] fs Information about the filesystem
  225. * @param[in] cluster Cluster to follow
  226. *
  227. * @return -1 'cluster' is at the end of the chain
  228. * @return Other values Next cluster in this chain
  229. */
  230. unsigned long next_cluster(DOS_FS * fs, unsigned long cluster)
  231. {
  232. unsigned long value;
  233. FAT_ENTRY curEntry;
  234. get_fat(&curEntry, fs->fat, cluster, fs);
  235. value = curEntry.value;
  236. if (FAT_IS_BAD(fs, value))
  237. die("Internal error: next_cluster on bad cluster");
  238. return FAT_IS_EOF(fs, value) ? -1 : value;
  239. }
  240. loff_t cluster_start(DOS_FS * fs, unsigned long cluster)
  241. {
  242. return fs->data_start + ((loff_t) cluster -
  243. 2) * (unsigned long long)fs->cluster_size;
  244. }
  245. /**
  246. * Update internal bookkeeping to show that the specified cluster belongs
  247. * to the specified dentry.
  248. *
  249. * @param[in,out] fs Information about the filesystem
  250. * @param[in] cluster Cluster being assigned
  251. * @param[in] owner Information on dentry that owns this cluster
  252. * (may be NULL)
  253. */
  254. void set_owner(DOS_FS * fs, unsigned long cluster, DOS_FILE * owner)
  255. {
  256. if (fs->cluster_owner == NULL)
  257. die("Internal error: attempt to set owner in non-existent table");
  258. if (owner && fs->cluster_owner[cluster]
  259. && (fs->cluster_owner[cluster] != owner))
  260. die("Internal error: attempt to change file owner");
  261. fs->cluster_owner[cluster] = owner;
  262. }
  263. DOS_FILE *get_owner(DOS_FS * fs, unsigned long cluster)
  264. {
  265. if (fs->cluster_owner == NULL)
  266. return NULL;
  267. else
  268. return fs->cluster_owner[cluster];
  269. }
  270. void fix_bad(DOS_FS * fs)
  271. {
  272. unsigned long i;
  273. if (verbose)
  274. printf("Checking for bad clusters.\n");
  275. for (i = 2; i < fs->clusters + 2; i++) {
  276. FAT_ENTRY curEntry;
  277. get_fat(&curEntry, fs->fat, i, fs);
  278. if (!get_owner(fs, i) && !FAT_IS_BAD(fs, curEntry.value))
  279. if (!fs_test(cluster_start(fs, i), fs->cluster_size)) {
  280. printf("Cluster %lu is unreadable.\n", i);
  281. set_fat(fs, i, -2);
  282. }
  283. }
  284. }
  285. void reclaim_free(DOS_FS * fs)
  286. {
  287. int reclaimed;
  288. unsigned long i;
  289. if (verbose)
  290. printf("Checking for unused clusters.\n");
  291. reclaimed = 0;
  292. for (i = 2; i < fs->clusters + 2; i++) {
  293. FAT_ENTRY curEntry;
  294. get_fat(&curEntry, fs->fat, i, fs);
  295. if (!get_owner(fs, i) && curEntry.value &&
  296. !FAT_IS_BAD(fs, curEntry.value)) {
  297. set_fat(fs, i, 0);
  298. reclaimed++;
  299. }
  300. }
  301. if (reclaimed)
  302. printf("Reclaimed %d unused cluster%s (%llu bytes).\n", reclaimed,
  303. reclaimed == 1 ? "" : "s",
  304. (unsigned long long)reclaimed * fs->cluster_size);
  305. }
  306. /**
  307. * Assign the specified owner to all orphan chains (except cycles).
  308. * Break cross-links between orphan chains.
  309. *
  310. * @param[in,out] fs Information about the filesystem
  311. * @param[in] owner dentry to be assigned ownership of orphans
  312. * @param[in,out] num_refs For each orphan cluster [index], how many
  313. * clusters link to it.
  314. * @param[in] start_cluster Where to start scanning for orphans
  315. */
  316. static void tag_free(DOS_FS * fs, DOS_FILE * owner, unsigned long *num_refs,
  317. unsigned long start_cluster)
  318. {
  319. int prev;
  320. unsigned long i, walk;
  321. if (start_cluster == 0)
  322. start_cluster = 2;
  323. for (i = start_cluster; i < fs->clusters + 2; i++) {
  324. FAT_ENTRY curEntry;
  325. get_fat(&curEntry, fs->fat, i, fs);
  326. /* If the current entry is the head of an un-owned chain... */
  327. if (curEntry.value && !FAT_IS_BAD(fs, curEntry.value) &&
  328. !get_owner(fs, i) && !num_refs[i]) {
  329. prev = 0;
  330. /* Walk the chain, claiming ownership as we go */
  331. for (walk = i; walk != -1; walk = next_cluster(fs, walk)) {
  332. if (!get_owner(fs, walk)) {
  333. set_owner(fs, walk, owner);
  334. } else {
  335. /* We've run into cross-links between orphaned chains,
  336. * or a cycle with a tail.
  337. * Terminate this orphan chain (break the link)
  338. */
  339. set_fat(fs, prev, -1);
  340. /* This is not necessary because 'walk' is owned and thus
  341. * will never become the head of a chain (the only case
  342. * that would matter during reclaim to files).
  343. * It's easier to decrement than to prove that it's
  344. * unnecessary.
  345. */
  346. num_refs[walk]--;
  347. break;
  348. }
  349. prev = walk;
  350. }
  351. }
  352. }
  353. }
  354. /**
  355. * Recover orphan chains to files, handling any cycles or cross-links.
  356. *
  357. * @param[in,out] fs Information about the filesystem
  358. */
  359. void reclaim_file(DOS_FS * fs)
  360. {
  361. DOS_FILE orphan;
  362. int reclaimed, files;
  363. int changed = 0;
  364. unsigned long i, next, walk;
  365. unsigned long *num_refs = NULL; /* Only for orphaned clusters */
  366. unsigned long total_num_clusters;
  367. if (verbose)
  368. printf("Reclaiming unconnected clusters.\n");
  369. total_num_clusters = fs->clusters + 2UL;
  370. num_refs = alloc(total_num_clusters * sizeof(unsigned long));
  371. memset(num_refs, 0, (total_num_clusters * sizeof(unsigned long)));
  372. /* Guarantee that all orphan chains (except cycles) end cleanly
  373. * with an end-of-chain mark.
  374. */
  375. for (i = 2; i < total_num_clusters; i++) {
  376. FAT_ENTRY curEntry;
  377. get_fat(&curEntry, fs->fat, i, fs);
  378. next = curEntry.value;
  379. if (!get_owner(fs, i) && next && next < fs->clusters + 2) {
  380. /* Cluster is linked, but not owned (orphan) */
  381. FAT_ENTRY nextEntry;
  382. get_fat(&nextEntry, fs->fat, next, fs);
  383. /* Mark it end-of-chain if it links into an owned cluster,
  384. * a free cluster, or a bad cluster.
  385. */
  386. if (get_owner(fs, next) || !nextEntry.value ||
  387. FAT_IS_BAD(fs, nextEntry.value))
  388. set_fat(fs, i, -1);
  389. else
  390. num_refs[next]++;
  391. }
  392. }
  393. /* Scan until all the orphans are accounted for,
  394. * and all cycles and cross-links are broken
  395. */
  396. do {
  397. tag_free(fs, &orphan, num_refs, changed);
  398. changed = 0;
  399. /* Any unaccounted-for orphans must be part of a cycle */
  400. for (i = 2; i < total_num_clusters; i++) {
  401. FAT_ENTRY curEntry;
  402. get_fat(&curEntry, fs->fat, i, fs);
  403. if (curEntry.value && !FAT_IS_BAD(fs, curEntry.value) &&
  404. !get_owner(fs, i)) {
  405. if (!num_refs[curEntry.value]--)
  406. die("Internal error: num_refs going below zero");
  407. set_fat(fs, i, -1);
  408. changed = curEntry.value;
  409. printf("Broke cycle at cluster %lu in free chain.\n", i);
  410. /* If we've created a new chain head,
  411. * tag_free() can claim it
  412. */
  413. if (num_refs[curEntry.value] == 0)
  414. break;
  415. }
  416. }
  417. }
  418. while (changed);
  419. /* Now we can start recovery */
  420. files = reclaimed = 0;
  421. for (i = 2; i < total_num_clusters; i++)
  422. /* If this cluster is the head of an orphan chain... */
  423. if (get_owner(fs, i) == &orphan && !num_refs[i]) {
  424. DIR_ENT de;
  425. loff_t offset;
  426. files++;
  427. offset = alloc_rootdir_entry(fs, &de, "FSCK%04dREC");
  428. de.start = CT_LE_W(i & 0xffff);
  429. if (fs->fat_bits == 32)
  430. de.starthi = CT_LE_W(i >> 16);
  431. for (walk = i; walk > 0 && walk != -1;
  432. walk = next_cluster(fs, walk)) {
  433. de.size = CT_LE_L(CF_LE_L(de.size) + fs->cluster_size);
  434. reclaimed++;
  435. }
  436. fs_write(offset, sizeof(DIR_ENT), &de);
  437. }
  438. if (reclaimed)
  439. printf("Reclaimed %d unused cluster%s (%llu bytes) in %d chain%s.\n",
  440. reclaimed, reclaimed == 1 ? "" : "s",
  441. (unsigned long long)reclaimed * fs->cluster_size, files,
  442. files == 1 ? "" : "s");
  443. free(num_refs);
  444. }
  445. unsigned long update_free(DOS_FS * fs)
  446. {
  447. unsigned long i;
  448. unsigned long free = 0;
  449. int do_set = 0;
  450. for (i = 2; i < fs->clusters + 2; i++) {
  451. FAT_ENTRY curEntry;
  452. get_fat(&curEntry, fs->fat, i, fs);
  453. if (!get_owner(fs, i) && !FAT_IS_BAD(fs, curEntry.value))
  454. ++free;
  455. }
  456. if (!fs->fsinfo_start)
  457. return free;
  458. if (verbose)
  459. printf("Checking free cluster summary.\n");
  460. if (fs->free_clusters != 0xFFFFFFFF) {
  461. if (free != fs->free_clusters) {
  462. printf("Free cluster summary wrong (%ld vs. really %ld)\n",
  463. fs->free_clusters, free);
  464. if (interactive)
  465. printf("1) Correct\n2) Don't correct\n");
  466. else
  467. printf(" Auto-correcting.\n");
  468. if (!interactive || get_key("12", "?") == '1')
  469. do_set = 1;
  470. }
  471. } else {
  472. printf("Free cluster summary uninitialized (should be %ld)\n", free);
  473. if (rw) {
  474. if (interactive)
  475. printf("1) Set it\n2) Leave it uninitialized\n");
  476. else
  477. printf(" Auto-setting.\n");
  478. if (!interactive || get_key("12", "?") == '1')
  479. do_set = 1;
  480. }
  481. }
  482. if (do_set) {
  483. unsigned long le_free = CT_LE_L(free);
  484. fs->free_clusters = free;
  485. fs_write(fs->fsinfo_start + offsetof(struct info_sector, free_clusters),
  486. sizeof(le_free), &le_free);
  487. }
  488. return free;
  489. }