fatent.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /************************************************************************/
  18. /* */
  19. /* PROJECT : exFAT & FAT12/16/32 File System */
  20. /* FILE : fatent.c */
  21. /* PURPOSE : sdFAT FAT entry manager */
  22. /* */
  23. /*----------------------------------------------------------------------*/
  24. /* NOTES */
  25. /* */
  26. /* */
  27. /************************************************************************/
  28. #include <asm/unaligned.h>
  29. #include "sdfat.h"
  30. #include "core.h"
  31. /*----------------------------------------------------------------------*/
  32. /* Global Variable Definitions */
  33. /*----------------------------------------------------------------------*/
  34. /* All buffer structures are protected w/ fsi->v_sem */
  35. /*----------------------------------------------------------------------*/
  36. /* Static functions */
  37. /*----------------------------------------------------------------------*/
  38. /*======================================================================*/
  39. /* FAT Read/Write Functions */
  40. /*======================================================================*/
  41. /* in : sb, loc
  42. * out: content
  43. * returns 0 on success, -1 on error
  44. */
  45. static s32 exfat_ent_get(struct super_block *sb, u32 loc, u32 *content)
  46. {
  47. u32 off, _content;
  48. u64 sec;
  49. u8 *fat_sector;
  50. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  51. /* fsi->vol_type == EXFAT */
  52. sec = fsi->FAT1_start_sector + (loc >> (sb->s_blocksize_bits-2));
  53. off = (loc << 2) & (u32)(sb->s_blocksize - 1);
  54. fat_sector = fcache_getblk(sb, sec);
  55. if (!fat_sector)
  56. return -EIO;
  57. _content = le32_to_cpu(*(__le32 *)(&fat_sector[off]));
  58. /* remap reserved clusters to simplify code */
  59. if (_content >= CLUSTER_32(0xFFFFFFF8))
  60. _content = CLUS_EOF;
  61. *content = CLUSTER_32(_content);
  62. return 0;
  63. }
  64. static s32 exfat_ent_set(struct super_block *sb, u32 loc, u32 content)
  65. {
  66. u32 off;
  67. u64 sec;
  68. u8 *fat_sector;
  69. __le32 *fat_entry;
  70. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  71. sec = fsi->FAT1_start_sector + (loc >> (sb->s_blocksize_bits-2));
  72. off = (loc << 2) & (u32)(sb->s_blocksize - 1);
  73. fat_sector = fcache_getblk(sb, sec);
  74. if (!fat_sector)
  75. return -EIO;
  76. fat_entry = (__le32 *)&(fat_sector[off]);
  77. *fat_entry = cpu_to_le32(content);
  78. return fcache_modify(sb, sec);
  79. }
  80. #define FATENT_FAT32_VALID_MASK (0x0FFFFFFFU)
  81. #define FATENT_FAT32_IGNORE_MASK (0xF0000000U)
  82. static s32 fat32_ent_get(struct super_block *sb, u32 loc, u32 *content)
  83. {
  84. u32 off, _content;
  85. u64 sec;
  86. u8 *fat_sector;
  87. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  88. sec = fsi->FAT1_start_sector + (loc >> (sb->s_blocksize_bits-2));
  89. off = (loc << 2) & (u32)(sb->s_blocksize - 1);
  90. fat_sector = fcache_getblk(sb, sec);
  91. if (!fat_sector)
  92. return -EIO;
  93. _content = le32_to_cpu(*(__le32 *)(&fat_sector[off]));
  94. _content &= FATENT_FAT32_VALID_MASK;
  95. /* remap reserved clusters to simplify code */
  96. if (_content == CLUSTER_32(0x0FFFFFF7U))
  97. _content = CLUS_BAD;
  98. else if (_content >= CLUSTER_32(0x0FFFFFF8U))
  99. _content = CLUS_EOF;
  100. *content = CLUSTER_32(_content);
  101. return 0;
  102. }
  103. static s32 fat32_ent_set(struct super_block *sb, u32 loc, u32 content)
  104. {
  105. u32 off;
  106. u64 sec;
  107. u8 *fat_sector;
  108. __le32 *fat_entry;
  109. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  110. content &= FATENT_FAT32_VALID_MASK;
  111. sec = fsi->FAT1_start_sector + (loc >> (sb->s_blocksize_bits-2));
  112. off = (loc << 2) & (u32)(sb->s_blocksize - 1);
  113. fat_sector = fcache_getblk(sb, sec);
  114. if (!fat_sector)
  115. return -EIO;
  116. fat_entry = (__le32 *)&(fat_sector[off]);
  117. content |= (le32_to_cpu(*fat_entry) & FATENT_FAT32_IGNORE_MASK);
  118. *fat_entry = cpu_to_le32(content);
  119. return fcache_modify(sb, sec);
  120. }
  121. #define FATENT_FAT16_VALID_MASK (0x0000FFFFU)
  122. static s32 fat16_ent_get(struct super_block *sb, u32 loc, u32 *content)
  123. {
  124. u32 off, _content;
  125. u64 sec;
  126. u8 *fat_sector;
  127. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  128. sec = fsi->FAT1_start_sector + (loc >> (sb->s_blocksize_bits-1));
  129. off = (loc << 1) & (u32)(sb->s_blocksize - 1);
  130. fat_sector = fcache_getblk(sb, sec);
  131. if (!fat_sector)
  132. return -EIO;
  133. _content = (u32)le16_to_cpu(*(__le16 *)(&fat_sector[off]));
  134. _content &= FATENT_FAT16_VALID_MASK;
  135. /* remap reserved clusters to simplify code */
  136. if (_content == CLUSTER_16(0xFFF7U))
  137. _content = CLUS_BAD;
  138. else if (_content >= CLUSTER_16(0xFFF8U))
  139. _content = CLUS_EOF;
  140. *content = CLUSTER_32(_content);
  141. return 0;
  142. }
  143. static s32 fat16_ent_set(struct super_block *sb, u32 loc, u32 content)
  144. {
  145. u32 off;
  146. u64 sec;
  147. u8 *fat_sector;
  148. __le16 *fat_entry;
  149. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  150. content &= FATENT_FAT16_VALID_MASK;
  151. sec = fsi->FAT1_start_sector + (loc >> (sb->s_blocksize_bits-1));
  152. off = (loc << 1) & (u32)(sb->s_blocksize - 1);
  153. fat_sector = fcache_getblk(sb, sec);
  154. if (!fat_sector)
  155. return -EIO;
  156. fat_entry = (__le16 *)&(fat_sector[off]);
  157. *fat_entry = cpu_to_le16(content);
  158. return fcache_modify(sb, sec);
  159. }
  160. #define FATENT_FAT12_VALID_MASK (0x00000FFFU)
  161. static s32 fat12_ent_get(struct super_block *sb, u32 loc, u32 *content)
  162. {
  163. u32 off, _content;
  164. u64 sec;
  165. u8 *fat_sector;
  166. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  167. sec = fsi->FAT1_start_sector + ((loc + (loc >> 1)) >> sb->s_blocksize_bits);
  168. off = (loc + (loc >> 1)) & (u32)(sb->s_blocksize - 1);
  169. fat_sector = fcache_getblk(sb, sec);
  170. if (!fat_sector)
  171. return -EIO;
  172. if (off == (u32)(sb->s_blocksize - 1)) {
  173. _content = (u32) fat_sector[off];
  174. fat_sector = fcache_getblk(sb, ++sec);
  175. if (!fat_sector)
  176. return -EIO;
  177. _content |= (u32) fat_sector[0] << 8;
  178. } else {
  179. _content = get_unaligned_le16(&fat_sector[off]);
  180. }
  181. if (loc & 1)
  182. _content >>= 4;
  183. _content &= FATENT_FAT12_VALID_MASK;
  184. /* remap reserved clusters to simplify code */
  185. if (_content == CLUSTER_16(0x0FF7U))
  186. _content = CLUS_BAD;
  187. else if (_content >= CLUSTER_16(0x0FF8U))
  188. _content = CLUS_EOF;
  189. *content = CLUSTER_32(_content);
  190. return 0;
  191. }
  192. static s32 fat12_ent_set(struct super_block *sb, u32 loc, u32 content)
  193. {
  194. u32 off;
  195. u64 sec;
  196. u8 *fat_sector, *fat_entry;
  197. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  198. content &= FATENT_FAT12_VALID_MASK;
  199. sec = fsi->FAT1_start_sector + ((loc + (loc >> 1)) >> sb->s_blocksize_bits);
  200. off = (loc + (loc >> 1)) & (u32)(sb->s_blocksize - 1);
  201. fat_sector = fcache_getblk(sb, sec);
  202. if (!fat_sector)
  203. return -EIO;
  204. if (loc & 1) { /* odd */
  205. content <<= 4;
  206. if (off == (u32)(sb->s_blocksize-1)) {
  207. fat_sector[off] = (u8)(content | (fat_sector[off] & 0x0F));
  208. if (fcache_modify(sb, sec))
  209. return -EIO;
  210. fat_sector = fcache_getblk(sb, ++sec);
  211. if (!fat_sector)
  212. return -EIO;
  213. fat_sector[0] = (u8)(content >> 8);
  214. } else {
  215. fat_entry = &(fat_sector[off]);
  216. content |= 0x000F & get_unaligned_le16(fat_entry);
  217. put_unaligned_le16(content, fat_entry);
  218. }
  219. } else { /* even */
  220. fat_sector[off] = (u8)(content);
  221. if (off == (u32)(sb->s_blocksize-1)) {
  222. fat_sector[off] = (u8)(content);
  223. if (fcache_modify(sb, sec))
  224. return -EIO;
  225. fat_sector = fcache_getblk(sb, ++sec);
  226. if (!fat_sector)
  227. return -EIO;
  228. fat_sector[0] = (u8)((fat_sector[0] & 0xF0) | (content >> 8));
  229. } else {
  230. fat_entry = &(fat_sector[off]);
  231. content |= 0xF000 & get_unaligned_le16(fat_entry);
  232. put_unaligned_le16(content, fat_entry);
  233. }
  234. }
  235. return fcache_modify(sb, sec);
  236. }
  237. static FATENT_OPS_T fat12_ent_ops = {
  238. fat12_ent_get,
  239. fat12_ent_set
  240. };
  241. static FATENT_OPS_T fat16_ent_ops = {
  242. fat16_ent_get,
  243. fat16_ent_set
  244. };
  245. static FATENT_OPS_T fat32_ent_ops = {
  246. fat32_ent_get,
  247. fat32_ent_set
  248. };
  249. static FATENT_OPS_T exfat_ent_ops = {
  250. exfat_ent_get,
  251. exfat_ent_set
  252. };
  253. s32 fat_ent_ops_init(struct super_block *sb)
  254. {
  255. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  256. switch (fsi->vol_type) {
  257. case EXFAT:
  258. fsi->fatent_ops = &exfat_ent_ops;
  259. break;
  260. case FAT32:
  261. fsi->fatent_ops = &fat32_ent_ops;
  262. break;
  263. case FAT16:
  264. fsi->fatent_ops = &fat16_ent_ops;
  265. break;
  266. case FAT12:
  267. fsi->fatent_ops = &fat12_ent_ops;
  268. break;
  269. default:
  270. fsi->fatent_ops = NULL;
  271. EMSG("Unknown volume type : %d", (int)fsi->vol_type);
  272. return -ENOTSUPP;
  273. }
  274. return 0;
  275. }
  276. static inline bool is_reserved_clus(u32 clus)
  277. {
  278. if (IS_CLUS_FREE(clus))
  279. return true;
  280. if (IS_CLUS_EOF(clus))
  281. return true;
  282. if (IS_CLUS_BAD(clus))
  283. return true;
  284. return false;
  285. }
  286. s32 fat_ent_get(struct super_block *sb, u32 loc, u32 *content)
  287. {
  288. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  289. s32 err;
  290. if (!is_valid_clus(fsi, loc)) {
  291. sdfat_fs_error(sb, "invalid access to FAT (entry 0x%08x)", loc);
  292. return -EIO;
  293. }
  294. err = fsi->fatent_ops->ent_get(sb, loc, content);
  295. if (err) {
  296. sdfat_fs_error(sb, "failed to access to FAT "
  297. "(entry 0x%08x, err:%d)", loc, err);
  298. return err;
  299. }
  300. if (!is_reserved_clus(*content) && !is_valid_clus(fsi, *content)) {
  301. sdfat_fs_error(sb, "invalid access to FAT (entry 0x%08x) "
  302. "bogus content (0x%08x)", loc, *content);
  303. return -EIO;
  304. }
  305. return 0;
  306. }
  307. s32 fat_ent_set(struct super_block *sb, u32 loc, u32 content)
  308. {
  309. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  310. return fsi->fatent_ops->ent_set(sb, loc, content);
  311. }
  312. s32 fat_ent_get_safe(struct super_block *sb, u32 loc, u32 *content)
  313. {
  314. s32 err = fat_ent_get(sb, loc, content);
  315. if (err)
  316. return err;
  317. if (IS_CLUS_FREE(*content)) {
  318. sdfat_fs_error(sb, "invalid access to FAT free cluster "
  319. "(entry 0x%08x)", loc);
  320. return -EIO;
  321. }
  322. if (IS_CLUS_BAD(*content)) {
  323. sdfat_fs_error(sb, "invalid access to FAT bad cluster "
  324. "(entry 0x%08x)", loc);
  325. return -EIO;
  326. }
  327. return 0;
  328. }
  329. /* end of fatent.c */