core_fat.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  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 : core_fat.c */
  21. /* PURPOSE : FAT-fs core code for sdFAT */
  22. /* */
  23. /*----------------------------------------------------------------------*/
  24. /* NOTES */
  25. /* */
  26. /* */
  27. /************************************************************************/
  28. #include <linux/version.h>
  29. #include <linux/blkdev.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/kernel.h>
  32. #include <linux/log2.h>
  33. #include "sdfat.h"
  34. #include "core.h"
  35. #include <asm/byteorder.h>
  36. #include <asm/unaligned.h>
  37. /*----------------------------------------------------------------------*/
  38. /* Constant & Macro Definitions */
  39. /*----------------------------------------------------------------------*/
  40. #define MAX_LFN_ORDER (20)
  41. /*
  42. * MAX_EST_AU_SECT should be changed according to 32/64bits.
  43. * On 32bit, 4KB page supports 512 clusters per AU.
  44. * But, on 64bit, 4KB page can handle a half of total list_head of 32bit's.
  45. * Bcause the size of list_head structure on 64bit increases twofold over 32bit.
  46. */
  47. #if (BITS_PER_LONG == 64)
  48. //#define MAX_EST_AU_SECT (16384) /* upto 8MB */
  49. #define MAX_EST_AU_SECT (32768) /* upto 16MB, used more page for list_head */
  50. #else
  51. #define MAX_EST_AU_SECT (32768) /* upto 16MB */
  52. #endif
  53. /*======================================================================*/
  54. /* Local Function Declarations */
  55. /*======================================================================*/
  56. static s32 __extract_uni_name_from_ext_entry(EXT_DENTRY_T *, u16 *, s32);
  57. /*----------------------------------------------------------------------*/
  58. /* Global Variable Definitions */
  59. /*----------------------------------------------------------------------*/
  60. /*----------------------------------------------------------------------*/
  61. /* Local Variable Definitions */
  62. /*----------------------------------------------------------------------*/
  63. /*======================================================================*/
  64. /* Local Function Definitions */
  65. /*======================================================================*/
  66. static u32 __calc_default_au_size(struct super_block *sb)
  67. {
  68. struct block_device *bdev = sb->s_bdev;
  69. struct gendisk *disk;
  70. struct request_queue *queue;
  71. struct queue_limits *limit;
  72. unsigned int est_au_sect = MAX_EST_AU_SECT;
  73. unsigned int est_au_size = 0;
  74. unsigned int queue_au_size = 0;
  75. sector_t total_sect = 0;
  76. /* we assumed that sector size is 512 bytes */
  77. disk = bdev->bd_disk;
  78. if (!disk)
  79. goto out;
  80. queue = disk->queue;
  81. if (!queue)
  82. goto out;
  83. limit = &queue->limits;
  84. queue_au_size = limit->discard_granularity;
  85. /* estimate function(x) =
  86. * (total_sect / 2) * 512 / 1024
  87. * => (total_sect >> 1) >> 1)
  88. * => (total_sect >> 2)
  89. * => estimated bytes size
  90. *
  91. * ex1) <= 8GB -> 4MB
  92. * ex2) 16GB -> 8MB
  93. * ex3) >= 32GB -> 16MB
  94. */
  95. total_sect = disk->part0.nr_sects;
  96. est_au_size = total_sect >> 2;
  97. /* au_size assumed that bytes per sector is 512 */
  98. est_au_sect = est_au_size >> 9;
  99. MMSG("DBG1: total_sect(%llu) est_au_size(%u) est_au_sect(%u)\n",
  100. (u64)total_sect, est_au_size, est_au_sect);
  101. if (est_au_sect <= 8192) {
  102. /* 4MB */
  103. est_au_sect = 8192;
  104. } else if (est_au_sect <= 16384) {
  105. /* 8MB */
  106. est_au_sect = 16384;
  107. } else {
  108. /* 8MB or 16MB */
  109. est_au_sect = MAX_EST_AU_SECT;
  110. }
  111. MMSG("DBG2: total_sect(%llu) est_au_size(%u) est_au_sect(%u)\n",
  112. (u64)total_sect, est_au_size, est_au_sect);
  113. if (est_au_size < queue_au_size &&
  114. queue_au_size <= (MAX_EST_AU_SECT << 9)) {
  115. DMSG("use queue_au_size(%u) instead of est_au_size(%u)\n",
  116. queue_au_size, est_au_size);
  117. est_au_sect = queue_au_size >> 9;
  118. }
  119. out:
  120. if (sb->s_blocksize != 512) {
  121. ASSERT(sb->s_blocksize_bits > 9);
  122. sdfat_log_msg(sb, KERN_INFO,
  123. "adjustment est_au_size by logical block size(%lu)",
  124. sb->s_blocksize);
  125. est_au_sect >>= (sb->s_blocksize_bits - 9);
  126. }
  127. sdfat_log_msg(sb, KERN_INFO, "set default AU sectors : %u "
  128. "(queue_au_size : %u KB, disk_size : %llu MB)",
  129. est_au_sect, queue_au_size >> 10, (u64)(total_sect >> 11));
  130. return est_au_sect;
  131. }
  132. /*
  133. * Cluster Management Functions
  134. */
  135. static s32 fat_free_cluster(struct super_block *sb, CHAIN_T *p_chain, s32 do_relse)
  136. {
  137. s32 ret = -EIO;
  138. s32 num_clusters = 0;
  139. u32 clu, prev;
  140. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  141. s32 i;
  142. u64 sector;
  143. /* invalid cluster number */
  144. if (IS_CLUS_FREE(p_chain->dir) || IS_CLUS_EOF(p_chain->dir))
  145. return 0;
  146. /* no cluster to truncate */
  147. if (!p_chain->size) {
  148. DMSG("%s: cluster(%u) truncation is not required.",
  149. __func__, p_chain->dir);
  150. return 0;
  151. }
  152. /* check cluster validation */
  153. if ((p_chain->dir < 2) && (p_chain->dir >= fsi->num_clusters)) {
  154. EMSG("%s: invalid start cluster (%u)\n", __func__, p_chain->dir);
  155. sdfat_debug_bug_on(1);
  156. return -EIO;
  157. }
  158. set_sb_dirty(sb);
  159. clu = p_chain->dir;
  160. do {
  161. if (do_relse) {
  162. sector = CLUS_TO_SECT(fsi, clu);
  163. for (i = 0; i < fsi->sect_per_clus; i++) {
  164. if (dcache_release(sb, sector+i) == -EIO)
  165. goto out;
  166. }
  167. }
  168. prev = clu;
  169. if (get_next_clus_safe(sb, &clu)) {
  170. /* print more helpful log */
  171. if (IS_CLUS_BAD(clu)) {
  172. sdfat_log_msg(sb, KERN_ERR, "%s : "
  173. "deleting bad cluster (clu[%u]->BAD)",
  174. __func__, prev);
  175. } else if (IS_CLUS_FREE(clu)) {
  176. sdfat_log_msg(sb, KERN_ERR, "%s : "
  177. "deleting free cluster (clu[%u]->FREE)",
  178. __func__, prev);
  179. }
  180. goto out;
  181. }
  182. /* Free FAT chain */
  183. if (fat_ent_set(sb, prev, CLUS_FREE))
  184. goto out;
  185. /* Update AMAP if needed */
  186. if (fsi->amap) {
  187. if (amap_release_cluster(sb, prev))
  188. return -EIO;
  189. }
  190. num_clusters++;
  191. } while (!IS_CLUS_EOF(clu));
  192. /* success */
  193. ret = 0;
  194. out:
  195. fsi->used_clusters -= num_clusters;
  196. return ret;
  197. } /* end of fat_free_cluster */
  198. static s32 fat_alloc_cluster(struct super_block *sb, u32 num_alloc, CHAIN_T *p_chain, s32 dest)
  199. {
  200. s32 ret = -ENOSPC;
  201. u32 i, num_clusters = 0, total_cnt;
  202. u32 new_clu, last_clu = CLUS_EOF, read_clu;
  203. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  204. total_cnt = fsi->num_clusters - CLUS_BASE;
  205. if (unlikely(total_cnt < fsi->used_clusters)) {
  206. sdfat_fs_error_ratelimit(sb,
  207. "%s : invalid used clusters(t:%u,u:%u)\n",
  208. __func__, total_cnt, fsi->used_clusters);
  209. return -EIO;
  210. }
  211. if (num_alloc > total_cnt - fsi->used_clusters)
  212. return -ENOSPC;
  213. new_clu = p_chain->dir;
  214. if (IS_CLUS_EOF(new_clu))
  215. new_clu = fsi->clu_srch_ptr;
  216. else if (new_clu >= fsi->num_clusters)
  217. new_clu = CLUS_BASE;
  218. set_sb_dirty(sb);
  219. p_chain->dir = CLUS_EOF;
  220. for (i = CLUS_BASE; i < fsi->num_clusters; i++) {
  221. if (fat_ent_get(sb, new_clu, &read_clu)) {
  222. ret = -EIO;
  223. goto error;
  224. }
  225. if (IS_CLUS_FREE(read_clu)) {
  226. if (fat_ent_set(sb, new_clu, CLUS_EOF)) {
  227. ret = -EIO;
  228. goto error;
  229. }
  230. num_clusters++;
  231. if (IS_CLUS_EOF(p_chain->dir)) {
  232. p_chain->dir = new_clu;
  233. } else {
  234. if (fat_ent_set(sb, last_clu, new_clu)) {
  235. ret = -EIO;
  236. goto error;
  237. }
  238. }
  239. last_clu = new_clu;
  240. if ((--num_alloc) == 0) {
  241. fsi->clu_srch_ptr = new_clu;
  242. fsi->used_clusters += num_clusters;
  243. return 0;
  244. }
  245. }
  246. if ((++new_clu) >= fsi->num_clusters)
  247. new_clu = CLUS_BASE;
  248. }
  249. error:
  250. if (num_clusters)
  251. fat_free_cluster(sb, p_chain, 0);
  252. return ret;
  253. } /* end of fat_alloc_cluster */
  254. static s32 fat_count_used_clusters(struct super_block *sb, u32 *ret_count)
  255. {
  256. s32 i;
  257. u32 clu, count = 0;
  258. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  259. for (i = CLUS_BASE; i < fsi->num_clusters; i++) {
  260. if (fat_ent_get(sb, i, &clu))
  261. return -EIO;
  262. if (!IS_CLUS_FREE(clu))
  263. count++;
  264. }
  265. *ret_count = count;
  266. return 0;
  267. } /* end of fat_count_used_clusters */
  268. /*
  269. * Directory Entry Management Functions
  270. */
  271. static u32 fat_get_entry_type(DENTRY_T *p_entry)
  272. {
  273. DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
  274. /* first byte of 32bytes dummy */
  275. if (*(ep->name) == MSDOS_UNUSED)
  276. return TYPE_UNUSED;
  277. /* 0xE5 of Kanji Japanese is replaced to 0x05 */
  278. else if (*(ep->name) == MSDOS_DELETED)
  279. return TYPE_DELETED;
  280. /* 11th byte of 32bytes dummy */
  281. else if ((ep->attr & ATTR_EXTEND_MASK) == ATTR_EXTEND)
  282. return TYPE_EXTEND;
  283. else if (!(ep->attr & (ATTR_SUBDIR | ATTR_VOLUME)))
  284. return TYPE_FILE;
  285. else if ((ep->attr & (ATTR_SUBDIR | ATTR_VOLUME)) == ATTR_SUBDIR)
  286. return TYPE_DIR;
  287. else if ((ep->attr & (ATTR_SUBDIR | ATTR_VOLUME)) == ATTR_VOLUME)
  288. return TYPE_VOLUME;
  289. return TYPE_INVALID;
  290. } /* end of fat_get_entry_type */
  291. static void fat_set_entry_type(DENTRY_T *p_entry, u32 type)
  292. {
  293. DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
  294. if (type == TYPE_UNUSED)
  295. *(ep->name) = MSDOS_UNUSED; /* 0x0 */
  296. else if (type == TYPE_DELETED)
  297. *(ep->name) = MSDOS_DELETED; /* 0xE5 */
  298. else if (type == TYPE_EXTEND)
  299. ep->attr = ATTR_EXTEND;
  300. else if (type == TYPE_DIR)
  301. ep->attr = ATTR_SUBDIR;
  302. else if (type == TYPE_FILE)
  303. ep->attr = ATTR_ARCHIVE;
  304. else if (type == TYPE_SYMLINK)
  305. ep->attr = ATTR_ARCHIVE | ATTR_SYMLINK;
  306. } /* end of fat_set_entry_type */
  307. static u32 fat_get_entry_attr(DENTRY_T *p_entry)
  308. {
  309. DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
  310. return (u32)ep->attr;
  311. } /* end of fat_get_entry_attr */
  312. static void fat_set_entry_attr(DENTRY_T *p_entry, u32 attr)
  313. {
  314. DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
  315. ep->attr = (u8)attr;
  316. } /* end of fat_set_entry_attr */
  317. static u8 fat_get_entry_flag(DENTRY_T *p_entry)
  318. {
  319. return 0x01;
  320. } /* end of fat_get_entry_flag */
  321. static void fat_set_entry_flag(DENTRY_T *p_entry, u8 flags)
  322. {
  323. } /* end of fat_set_entry_flag */
  324. static u32 fat_get_entry_clu0(DENTRY_T *p_entry)
  325. {
  326. DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
  327. /* FIXME : is ok? */
  328. return(((u32)(le16_to_cpu(ep->start_clu_hi)) << 16) | le16_to_cpu(ep->start_clu_lo));
  329. } /* end of fat_get_entry_clu0 */
  330. static void fat_set_entry_clu0(DENTRY_T *p_entry, u32 start_clu)
  331. {
  332. DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
  333. ep->start_clu_lo = cpu_to_le16(CLUSTER_16(start_clu));
  334. ep->start_clu_hi = cpu_to_le16(CLUSTER_16(start_clu >> 16));
  335. } /* end of fat_set_entry_clu0 */
  336. static u64 fat_get_entry_size(DENTRY_T *p_entry)
  337. {
  338. DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
  339. return (u64)le32_to_cpu(ep->size);
  340. } /* end of fat_get_entry_size */
  341. static void fat_set_entry_size(DENTRY_T *p_entry, u64 size)
  342. {
  343. DOS_DENTRY_T *ep = (DOS_DENTRY_T *)p_entry;
  344. ep->size = cpu_to_le32((u32)size);
  345. } /* end of fat_set_entry_size */
  346. static void fat_get_entry_time(DENTRY_T *p_entry, TIMESTAMP_T *tp, u8 mode)
  347. {
  348. u16 t = 0x00, d = 0x21;
  349. DOS_DENTRY_T *ep = (DOS_DENTRY_T *) p_entry;
  350. switch (mode) {
  351. case TM_CREATE:
  352. t = le16_to_cpu(ep->create_time);
  353. d = le16_to_cpu(ep->create_date);
  354. break;
  355. case TM_MODIFY:
  356. t = le16_to_cpu(ep->modify_time);
  357. d = le16_to_cpu(ep->modify_date);
  358. break;
  359. }
  360. tp->tz.value = 0x00;
  361. tp->sec = (t & 0x001F) << 1;
  362. tp->min = (t >> 5) & 0x003F;
  363. tp->hour = (t >> 11);
  364. tp->day = (d & 0x001F);
  365. tp->mon = (d >> 5) & 0x000F;
  366. tp->year = (d >> 9);
  367. } /* end of fat_get_entry_time */
  368. static void fat_set_entry_time(DENTRY_T *p_entry, TIMESTAMP_T *tp, u8 mode)
  369. {
  370. u16 t, d;
  371. DOS_DENTRY_T *ep = (DOS_DENTRY_T *) p_entry;
  372. t = (tp->hour << 11) | (tp->min << 5) | (tp->sec >> 1);
  373. d = (tp->year << 9) | (tp->mon << 5) | tp->day;
  374. switch (mode) {
  375. case TM_CREATE:
  376. ep->create_time = cpu_to_le16(t);
  377. ep->create_date = cpu_to_le16(d);
  378. break;
  379. case TM_MODIFY:
  380. ep->modify_time = cpu_to_le16(t);
  381. ep->modify_date = cpu_to_le16(d);
  382. break;
  383. }
  384. } /* end of fat_set_entry_time */
  385. static void __init_dos_entry(struct super_block *sb, DOS_DENTRY_T *ep, u32 type, u32 start_clu)
  386. {
  387. TIMESTAMP_T tm, *tp;
  388. fat_set_entry_type((DENTRY_T *) ep, type);
  389. ep->start_clu_lo = cpu_to_le16(CLUSTER_16(start_clu));
  390. ep->start_clu_hi = cpu_to_le16(CLUSTER_16(start_clu >> 16));
  391. ep->size = 0;
  392. tp = tm_now(SDFAT_SB(sb), &tm);
  393. fat_set_entry_time((DENTRY_T *) ep, tp, TM_CREATE);
  394. fat_set_entry_time((DENTRY_T *) ep, tp, TM_MODIFY);
  395. ep->access_date = 0;
  396. ep->create_time_ms = 0;
  397. } /* end of __init_dos_entry */
  398. static void __init_ext_entry(EXT_DENTRY_T *ep, s32 order, u8 chksum, u16 *uniname)
  399. {
  400. s32 i;
  401. u8 end = false;
  402. fat_set_entry_type((DENTRY_T *) ep, TYPE_EXTEND);
  403. ep->order = (u8) order;
  404. ep->sysid = 0;
  405. ep->checksum = chksum;
  406. ep->start_clu = 0;
  407. /* unaligned name */
  408. for (i = 0; i < 5; i++) {
  409. if (!end) {
  410. put_unaligned_le16(*uniname, &(ep->unicode_0_4[i<<1]));
  411. if (*uniname == 0x0)
  412. end = true;
  413. else
  414. uniname++;
  415. } else {
  416. put_unaligned_le16(0xFFFF, &(ep->unicode_0_4[i<<1]));
  417. }
  418. }
  419. /* aligned name */
  420. for (i = 0; i < 6; i++) {
  421. if (!end) {
  422. ep->unicode_5_10[i] = cpu_to_le16(*uniname);
  423. if (*uniname == 0x0)
  424. end = true;
  425. else
  426. uniname++;
  427. } else {
  428. ep->unicode_5_10[i] = cpu_to_le16(0xFFFF);
  429. }
  430. }
  431. /* aligned name */
  432. for (i = 0; i < 2; i++) {
  433. if (!end) {
  434. ep->unicode_11_12[i] = cpu_to_le16(*uniname);
  435. if (*uniname == 0x0)
  436. end = true;
  437. else
  438. uniname++;
  439. } else {
  440. ep->unicode_11_12[i] = cpu_to_le16(0xFFFF);
  441. }
  442. }
  443. } /* end of __init_ext_entry */
  444. static s32 fat_init_dir_entry(struct super_block *sb, CHAIN_T *p_dir, s32 entry, u32 type,
  445. u32 start_clu, u64 size)
  446. {
  447. u64 sector;
  448. DOS_DENTRY_T *dos_ep;
  449. dos_ep = (DOS_DENTRY_T *) get_dentry_in_dir(sb, p_dir, entry, &sector);
  450. if (!dos_ep)
  451. return -EIO;
  452. __init_dos_entry(sb, dos_ep, type, start_clu);
  453. dcache_modify(sb, sector);
  454. return 0;
  455. } /* end of fat_init_dir_entry */
  456. static s32 fat_init_ext_entry(struct super_block *sb, CHAIN_T *p_dir, s32 entry, s32 num_entries,
  457. UNI_NAME_T *p_uniname, DOS_NAME_T *p_dosname)
  458. {
  459. s32 i;
  460. u64 sector;
  461. u8 chksum;
  462. u16 *uniname = p_uniname->name;
  463. DOS_DENTRY_T *dos_ep;
  464. EXT_DENTRY_T *ext_ep;
  465. dos_ep = (DOS_DENTRY_T *) get_dentry_in_dir(sb, p_dir, entry, &sector);
  466. if (!dos_ep)
  467. return -EIO;
  468. dos_ep->lcase = p_dosname->name_case;
  469. memcpy(dos_ep->name, p_dosname->name, DOS_NAME_LENGTH);
  470. if (dcache_modify(sb, sector))
  471. return -EIO;
  472. if ((--num_entries) > 0) {
  473. chksum = calc_chksum_1byte((void *) dos_ep->name, DOS_NAME_LENGTH, 0);
  474. for (i = 1; i < num_entries; i++) {
  475. ext_ep = (EXT_DENTRY_T *) get_dentry_in_dir(sb, p_dir, entry-i, &sector);
  476. if (!ext_ep)
  477. return -EIO;
  478. __init_ext_entry(ext_ep, i, chksum, uniname);
  479. if (dcache_modify(sb, sector))
  480. return -EIO;
  481. uniname += 13;
  482. }
  483. ext_ep = (EXT_DENTRY_T *) get_dentry_in_dir(sb, p_dir, entry-i, &sector);
  484. if (!ext_ep)
  485. return -EIO;
  486. __init_ext_entry(ext_ep, i+MSDOS_LAST_LFN, chksum, uniname);
  487. if (dcache_modify(sb, sector))
  488. return -EIO;
  489. }
  490. return 0;
  491. } /* end of fat_init_ext_entry */
  492. static s32 fat_delete_dir_entry(struct super_block *sb, CHAIN_T *p_dir, s32 entry, s32 order, s32 num_entries)
  493. {
  494. s32 i;
  495. u64 sector;
  496. DENTRY_T *ep;
  497. for (i = num_entries-1; i >= order; i--) {
  498. ep = get_dentry_in_dir(sb, p_dir, entry-i, &sector);
  499. if (!ep)
  500. return -EIO;
  501. fat_set_entry_type(ep, TYPE_DELETED);
  502. if (dcache_modify(sb, sector))
  503. return -EIO;
  504. }
  505. return 0;
  506. }
  507. /* return values of fat_find_dir_entry()
  508. * >= 0 : return dir entiry position with the name in dir
  509. * -EEXIST : (root dir, ".") it is the root dir itself
  510. * -ENOENT : entry with the name does not exist
  511. * -EIO : I/O error
  512. */
  513. static inline s32 __get_dentries_per_clu(FS_INFO_T *fsi, s32 clu)
  514. {
  515. if (IS_CLUS_FREE(clu)) /* FAT16 root_dir */
  516. return fsi->dentries_in_root;
  517. return fsi->dentries_per_clu;
  518. }
  519. static s32 fat_find_dir_entry(struct super_block *sb, FILE_ID_T *fid,
  520. CHAIN_T *p_dir, UNI_NAME_T *p_uniname, s32 num_entries, DOS_NAME_T *p_dosname, u32 type)
  521. {
  522. s32 i, rewind = 0, dentry = 0, end_eidx = 0;
  523. s32 chksum = 0, lfn_ord = 0, lfn_len = 0;
  524. s32 dentries_per_clu, num_empty = 0;
  525. u32 entry_type;
  526. u16 entry_uniname[14], *uniname = NULL;
  527. CHAIN_T clu;
  528. DENTRY_T *ep;
  529. HINT_T *hint_stat = &fid->hint_stat;
  530. HINT_FEMP_T candi_empty;
  531. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  532. /*
  533. * REMARK:
  534. * DOT and DOTDOT are handled by VFS layer
  535. */
  536. dentries_per_clu = __get_dentries_per_clu(fsi, p_dir->dir);
  537. clu.dir = p_dir->dir;
  538. clu.flags = p_dir->flags;
  539. if (hint_stat->eidx) {
  540. clu.dir = hint_stat->clu;
  541. dentry = hint_stat->eidx;
  542. end_eidx = dentry;
  543. }
  544. candi_empty.eidx = -1;
  545. MMSG("lookup dir= %s\n", p_dosname->name);
  546. rewind:
  547. while (!IS_CLUS_EOF(clu.dir)) {
  548. i = dentry % dentries_per_clu;
  549. for (; i < dentries_per_clu; i++, dentry++) {
  550. if (rewind && (dentry == end_eidx))
  551. goto not_found;
  552. ep = get_dentry_in_dir(sb, &clu, i, NULL);
  553. if (!ep)
  554. return -EIO;
  555. entry_type = fat_get_entry_type(ep);
  556. /*
  557. * Most directory entries have long name,
  558. * So, we check extend directory entry first.
  559. */
  560. if (entry_type == TYPE_EXTEND) {
  561. EXT_DENTRY_T *ext_ep = (EXT_DENTRY_T *)ep;
  562. u32 cur_ord = (u32)ext_ep->order;
  563. u32 cur_chksum = (s32)ext_ep->checksum;
  564. s32 len = 13;
  565. u16 unichar;
  566. num_empty = 0;
  567. candi_empty.eidx = -1;
  568. /* check whether new lfn or not */
  569. if (cur_ord & MSDOS_LAST_LFN) {
  570. cur_ord &= ~(MSDOS_LAST_LFN);
  571. chksum = cur_chksum;
  572. len = (13 * (cur_ord-1));
  573. uniname = (p_uniname->name + len);
  574. lfn_ord = cur_ord + 1;
  575. lfn_len = 0;
  576. /* check minimum name length */
  577. if (cur_ord &&
  578. (len > p_uniname->name_len)) {
  579. /* MISMATCHED NAME LENGTH */
  580. lfn_len = -1;
  581. }
  582. len = 0;
  583. }
  584. /* invalid lfn order */
  585. if (!cur_ord || (cur_ord > MAX_LFN_ORDER) ||
  586. ((cur_ord + 1) != lfn_ord))
  587. goto reset_dentry_set;
  588. /* check checksum of directory entry set */
  589. if (cur_chksum != chksum)
  590. goto reset_dentry_set;
  591. /* update order for next dentry */
  592. lfn_ord = cur_ord;
  593. /* check whether mismatched lfn or not */
  594. if (lfn_len == -1) {
  595. /* MISMATCHED LFN DENTRY SET */
  596. continue;
  597. }
  598. if (!uniname) {
  599. sdfat_fs_error(sb,
  600. "%s : abnormal dentry "
  601. "(start_clu[%u], "
  602. "idx[%u])", __func__,
  603. p_dir->dir, dentry);
  604. sdfat_debug_bug_on(1);
  605. return -EIO;
  606. }
  607. /* update position of name buffer */
  608. uniname -= len;
  609. /* get utf16 characters saved on this entry */
  610. len = __extract_uni_name_from_ext_entry(ext_ep, entry_uniname, lfn_ord);
  611. /* replace last char to null */
  612. unichar = *(uniname+len);
  613. *(uniname+len) = (u16)0x0;
  614. /* uniname ext_dentry unit compare repeatdly */
  615. if (nls_cmp_uniname(sb, uniname, entry_uniname)) {
  616. /* DO HANDLE WRONG NAME */
  617. lfn_len = -1;
  618. } else {
  619. /* add matched chars length */
  620. lfn_len += len;
  621. }
  622. /* restore previous character */
  623. *(uniname+len) = unichar;
  624. /* jump to check next dentry */
  625. continue;
  626. } else if ((entry_type == TYPE_FILE) || (entry_type == TYPE_DIR)) {
  627. DOS_DENTRY_T *dos_ep = (DOS_DENTRY_T *)ep;
  628. u32 cur_chksum = (s32)calc_chksum_1byte(
  629. (void *) dos_ep->name,
  630. DOS_NAME_LENGTH, 0);
  631. num_empty = 0;
  632. candi_empty.eidx = -1;
  633. MMSG("checking dir= %c%c%c%c%c%c%c%c%c%c%c\n",
  634. dos_ep->name[0], dos_ep->name[1],
  635. dos_ep->name[2], dos_ep->name[3],
  636. dos_ep->name[4], dos_ep->name[5],
  637. dos_ep->name[6], dos_ep->name[7],
  638. dos_ep->name[8], dos_ep->name[9],
  639. dos_ep->name[10]);
  640. /*
  641. * if there is no valid long filename,
  642. * we should check short filename.
  643. */
  644. if (!lfn_len || (cur_chksum != chksum)) {
  645. /* check shortname */
  646. if ((p_dosname->name[0] != '\0') &&
  647. !nls_cmp_sfn(sb,
  648. p_dosname->name,
  649. dos_ep->name)) {
  650. goto found;
  651. }
  652. /* check name length */
  653. } else if ((lfn_len > 0) &&
  654. ((s32)p_uniname->name_len ==
  655. lfn_len)) {
  656. goto found;
  657. }
  658. /* DO HANDLE MISMATCHED SFN, FALL THROUGH */
  659. } else if ((entry_type == TYPE_UNUSED) || (entry_type == TYPE_DELETED)) {
  660. num_empty++;
  661. if (candi_empty.eidx == -1) {
  662. if (num_empty == 1) {
  663. candi_empty.cur.dir = clu.dir;
  664. candi_empty.cur.size = clu.size;
  665. candi_empty.cur.flags = clu.flags;
  666. }
  667. if (num_empty >= num_entries) {
  668. candi_empty.eidx = dentry - (num_empty - 1);
  669. ASSERT(0 <= candi_empty.eidx);
  670. candi_empty.count = num_empty;
  671. if ((fid->hint_femp.eidx == -1) ||
  672. (candi_empty.eidx <= fid->hint_femp.eidx)) {
  673. memcpy(&fid->hint_femp,
  674. &candi_empty,
  675. sizeof(HINT_FEMP_T));
  676. }
  677. }
  678. }
  679. if (entry_type == TYPE_UNUSED)
  680. goto not_found;
  681. /* FALL THROUGH */
  682. }
  683. reset_dentry_set:
  684. /* TYPE_DELETED, TYPE_VOLUME OR MISMATCHED SFN */
  685. lfn_ord = 0;
  686. lfn_len = 0;
  687. chksum = 0;
  688. }
  689. if (IS_CLUS_FREE(p_dir->dir))
  690. break; /* FAT16 root_dir */
  691. if (get_next_clus_safe(sb, &clu.dir))
  692. return -EIO;
  693. }
  694. not_found:
  695. /* we started at not 0 index,so we should try to find target
  696. * from 0 index to the index we started at.
  697. */
  698. if (!rewind && end_eidx) {
  699. rewind = 1;
  700. dentry = 0;
  701. clu.dir = p_dir->dir;
  702. /* reset dentry set */
  703. lfn_ord = 0;
  704. lfn_len = 0;
  705. chksum = 0;
  706. /* reset empty hint_*/
  707. num_empty = 0;
  708. candi_empty.eidx = -1;
  709. goto rewind;
  710. }
  711. /* initialized hint_stat */
  712. hint_stat->clu = p_dir->dir;
  713. hint_stat->eidx = 0;
  714. return -ENOENT;
  715. found:
  716. /* next dentry we'll find is out of this cluster */
  717. if (!((dentry + 1) % dentries_per_clu)) {
  718. int ret = 0;
  719. /* FAT16 root_dir */
  720. if (IS_CLUS_FREE(p_dir->dir))
  721. clu.dir = CLUS_EOF;
  722. else
  723. ret = get_next_clus_safe(sb, &clu.dir);
  724. if (ret || IS_CLUS_EOF(clu.dir)) {
  725. /* just initialized hint_stat */
  726. hint_stat->clu = p_dir->dir;
  727. hint_stat->eidx = 0;
  728. return dentry;
  729. }
  730. }
  731. hint_stat->clu = clu.dir;
  732. hint_stat->eidx = dentry + 1;
  733. return dentry;
  734. } /* end of fat_find_dir_entry */
  735. /* returns -EIO on error */
  736. static s32 fat_count_ext_entries(struct super_block *sb, CHAIN_T *p_dir, s32 entry, DENTRY_T *p_entry)
  737. {
  738. s32 count = 0;
  739. u8 chksum;
  740. DOS_DENTRY_T *dos_ep = (DOS_DENTRY_T *) p_entry;
  741. EXT_DENTRY_T *ext_ep;
  742. chksum = calc_chksum_1byte((void *) dos_ep->name, DOS_NAME_LENGTH, 0);
  743. for (entry--; entry >= 0; entry--) {
  744. ext_ep = (EXT_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry, NULL);
  745. if (!ext_ep)
  746. return -EIO;
  747. if ((fat_get_entry_type((DENTRY_T *)ext_ep) == TYPE_EXTEND) &&
  748. (ext_ep->checksum == chksum)) {
  749. count++;
  750. if (ext_ep->order > MSDOS_LAST_LFN)
  751. return count;
  752. } else {
  753. return count;
  754. }
  755. }
  756. return count;
  757. }
  758. /*
  759. * Name Conversion Functions
  760. */
  761. static s32 __extract_uni_name_from_ext_entry(EXT_DENTRY_T *ep, u16 *uniname, s32 order)
  762. {
  763. s32 i, len = 0;
  764. for (i = 0; i < 5; i++) {
  765. *uniname = get_unaligned_le16(&(ep->unicode_0_4[i<<1]));
  766. if (*uniname == 0x0)
  767. return len;
  768. uniname++;
  769. len++;
  770. }
  771. if (order < 20) {
  772. for (i = 0; i < 6; i++) {
  773. /* FIXME : unaligned? */
  774. *uniname = le16_to_cpu(ep->unicode_5_10[i]);
  775. if (*uniname == 0x0)
  776. return len;
  777. uniname++;
  778. len++;
  779. }
  780. } else {
  781. for (i = 0; i < 4; i++) {
  782. /* FIXME : unaligned? */
  783. *uniname = le16_to_cpu(ep->unicode_5_10[i]);
  784. if (*uniname == 0x0)
  785. return len;
  786. uniname++;
  787. len++;
  788. }
  789. *uniname = 0x0; /* uniname[MAX_NAME_LENGTH] */
  790. return len;
  791. }
  792. for (i = 0; i < 2; i++) {
  793. /* FIXME : unaligned? */
  794. *uniname = le16_to_cpu(ep->unicode_11_12[i]);
  795. if (*uniname == 0x0)
  796. return len;
  797. uniname++;
  798. len++;
  799. }
  800. *uniname = 0x0;
  801. return len;
  802. } /* end of __extract_uni_name_from_ext_entry */
  803. static void fat_get_uniname_from_ext_entry(struct super_block *sb, CHAIN_T *p_dir, s32 entry, u16 *uniname)
  804. {
  805. u32 i;
  806. u16 *name = uniname;
  807. u32 chksum;
  808. DOS_DENTRY_T *dos_ep =
  809. (DOS_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry, NULL);
  810. if (unlikely(!dos_ep))
  811. goto invalid_lfn;
  812. chksum = (u32)calc_chksum_1byte(
  813. (void *) dos_ep->name,
  814. DOS_NAME_LENGTH, 0);
  815. for (entry--, i = 1; entry >= 0; entry--, i++) {
  816. EXT_DENTRY_T *ep;
  817. ep = (EXT_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry, NULL);
  818. if (!ep)
  819. goto invalid_lfn;
  820. if (fat_get_entry_type((DENTRY_T *) ep) != TYPE_EXTEND)
  821. goto invalid_lfn;
  822. if (chksum != (u32)ep->checksum)
  823. goto invalid_lfn;
  824. if (i != (u32)(ep->order & ~(MSDOS_LAST_LFN)))
  825. goto invalid_lfn;
  826. __extract_uni_name_from_ext_entry(ep, name, (s32)i);
  827. if (ep->order & MSDOS_LAST_LFN)
  828. return;
  829. name += 13;
  830. }
  831. invalid_lfn:
  832. *uniname = (u16)0x0;
  833. } /* end of fat_get_uniname_from_ext_entry */
  834. /* Find if the shortname exists
  835. * and check if there are free entries
  836. */
  837. static s32 __fat_find_shortname_entry(struct super_block *sb, CHAIN_T *p_dir,
  838. u8 *p_dosname, s32 *offset, __attribute__((unused))int n_entry_needed)
  839. {
  840. u32 type;
  841. s32 i, dentry = 0;
  842. s32 dentries_per_clu;
  843. DENTRY_T *ep = NULL;
  844. DOS_DENTRY_T *dos_ep = NULL;
  845. CHAIN_T clu = *p_dir;
  846. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  847. if (offset)
  848. *offset = -1;
  849. if (IS_CLUS_FREE(clu.dir)) /* FAT16 root_dir */
  850. dentries_per_clu = fsi->dentries_in_root;
  851. else
  852. dentries_per_clu = fsi->dentries_per_clu;
  853. while (!IS_CLUS_EOF(clu.dir)) {
  854. for (i = 0; i < dentries_per_clu; i++, dentry++) {
  855. ep = get_dentry_in_dir(sb, &clu, i, NULL);
  856. if (!ep)
  857. return -EIO;
  858. type = fat_get_entry_type(ep);
  859. if ((type == TYPE_FILE) || (type == TYPE_DIR)) {
  860. dos_ep = (DOS_DENTRY_T *)ep;
  861. if (!nls_cmp_sfn(sb, p_dosname, dos_ep->name)) {
  862. if (offset)
  863. *offset = dentry;
  864. return 0;
  865. }
  866. }
  867. }
  868. /* fat12/16 root dir */
  869. if (IS_CLUS_FREE(clu.dir))
  870. break;
  871. if (get_next_clus_safe(sb, &clu.dir))
  872. return -EIO;
  873. }
  874. return -ENOENT;
  875. }
  876. #ifdef CONFIG_SDFAT_FAT32_SHORTNAME_SEQ
  877. static void __fat_attach_count_to_dos_name(u8 *dosname, s32 count)
  878. {
  879. s32 i, j, length;
  880. s8 str_count[6];
  881. snprintf(str_count, sizeof(str_count), "~%d", count);
  882. length = strlen(str_count);
  883. i = j = 0;
  884. while (j <= (8 - length)) {
  885. i = j;
  886. if (dosname[j] == ' ')
  887. break;
  888. if (dosname[j] & 0x80)
  889. j += 2;
  890. else
  891. j++;
  892. }
  893. for (j = 0; j < length; i++, j++)
  894. dosname[i] = (u8) str_count[j];
  895. if (i == 7)
  896. dosname[7] = ' ';
  897. } /* end of __fat_attach_count_to_dos_name */
  898. #endif
  899. s32 fat_generate_dos_name_new(struct super_block *sb, CHAIN_T *p_dir, DOS_NAME_T *p_dosname, s32 n_entry_needed)
  900. {
  901. s32 i;
  902. s32 baselen, err;
  903. u8 work[DOS_NAME_LENGTH], buf[5];
  904. u8 tail;
  905. baselen = 8;
  906. memset(work, ' ', DOS_NAME_LENGTH);
  907. memcpy(work, p_dosname->name, DOS_NAME_LENGTH);
  908. while (baselen && (work[--baselen] == ' ')) {
  909. /* DO NOTHING, JUST FOR CHECK_PATCH */
  910. }
  911. if (baselen > 6)
  912. baselen = 6;
  913. BUG_ON(baselen < 0);
  914. #ifdef CONFIG_SDFAT_FAT32_SHORTNAME_SEQ
  915. /* example) namei_exfat.c -> NAMEI_~1 - NAMEI_~9 */
  916. work[baselen] = '~';
  917. for (i = 1; i < 10; i++) {
  918. // '0' + i = 1 ~ 9 ASCII
  919. work[baselen + 1] = '0' + i;
  920. err = __fat_find_shortname_entry(sb, p_dir, work, NULL, n_entry_needed);
  921. if (err == -ENOENT) {
  922. /* void return */
  923. __fat_attach_count_to_dos_name(p_dosname->name, i);
  924. return 0;
  925. }
  926. /* any other error */
  927. if (err)
  928. return err;
  929. }
  930. #endif
  931. i = jiffies;
  932. tail = (jiffies >> 16) & 0x7;
  933. if (baselen > 2)
  934. baselen = 2;
  935. BUG_ON(baselen < 0);
  936. work[baselen + 4] = '~';
  937. // 1 ~ 8 ASCII
  938. work[baselen + 5] = '1' + tail;
  939. while (1) {
  940. snprintf(buf, sizeof(buf), "%04X", i & 0xffff);
  941. memcpy(&work[baselen], buf, 4);
  942. err = __fat_find_shortname_entry(sb, p_dir, work, NULL, n_entry_needed);
  943. if (err == -ENOENT) {
  944. memcpy(p_dosname->name, work, DOS_NAME_LENGTH);
  945. break;
  946. }
  947. /* any other error */
  948. if (err)
  949. return err;
  950. i -= 11;
  951. }
  952. return 0;
  953. } /* end of generate_dos_name_new */
  954. static s32 fat_calc_num_entries(UNI_NAME_T *p_uniname)
  955. {
  956. s32 len;
  957. len = p_uniname->name_len;
  958. if (len == 0)
  959. return 0;
  960. /* 1 dos name entry + extended entries */
  961. return((len-1) / 13 + 2);
  962. } /* end of calc_num_enties */
  963. static s32 fat_check_max_dentries(FILE_ID_T *fid)
  964. {
  965. if ((fid->size >> DENTRY_SIZE_BITS) >= MAX_FAT_DENTRIES) {
  966. /* FAT spec allows a dir to grow upto 65536 dentries */
  967. return -ENOSPC;
  968. }
  969. return 0;
  970. } /* end of check_max_dentries */
  971. /*
  972. * File Operation Functions
  973. */
  974. static FS_FUNC_T fat_fs_func = {
  975. .alloc_cluster = fat_alloc_cluster,
  976. .free_cluster = fat_free_cluster,
  977. .count_used_clusters = fat_count_used_clusters,
  978. .init_dir_entry = fat_init_dir_entry,
  979. .init_ext_entry = fat_init_ext_entry,
  980. .find_dir_entry = fat_find_dir_entry,
  981. .delete_dir_entry = fat_delete_dir_entry,
  982. .get_uniname_from_ext_entry = fat_get_uniname_from_ext_entry,
  983. .count_ext_entries = fat_count_ext_entries,
  984. .calc_num_entries = fat_calc_num_entries,
  985. .check_max_dentries = fat_check_max_dentries,
  986. .get_entry_type = fat_get_entry_type,
  987. .set_entry_type = fat_set_entry_type,
  988. .get_entry_attr = fat_get_entry_attr,
  989. .set_entry_attr = fat_set_entry_attr,
  990. .get_entry_flag = fat_get_entry_flag,
  991. .set_entry_flag = fat_set_entry_flag,
  992. .get_entry_clu0 = fat_get_entry_clu0,
  993. .set_entry_clu0 = fat_set_entry_clu0,
  994. .get_entry_size = fat_get_entry_size,
  995. .set_entry_size = fat_set_entry_size,
  996. .get_entry_time = fat_get_entry_time,
  997. .set_entry_time = fat_set_entry_time,
  998. };
  999. static FS_FUNC_T amap_fat_fs_func = {
  1000. .alloc_cluster = amap_fat_alloc_cluster,
  1001. .free_cluster = fat_free_cluster,
  1002. .count_used_clusters = fat_count_used_clusters,
  1003. .init_dir_entry = fat_init_dir_entry,
  1004. .init_ext_entry = fat_init_ext_entry,
  1005. .find_dir_entry = fat_find_dir_entry,
  1006. .delete_dir_entry = fat_delete_dir_entry,
  1007. .get_uniname_from_ext_entry = fat_get_uniname_from_ext_entry,
  1008. .count_ext_entries = fat_count_ext_entries,
  1009. .calc_num_entries = fat_calc_num_entries,
  1010. .check_max_dentries = fat_check_max_dentries,
  1011. .get_entry_type = fat_get_entry_type,
  1012. .set_entry_type = fat_set_entry_type,
  1013. .get_entry_attr = fat_get_entry_attr,
  1014. .set_entry_attr = fat_set_entry_attr,
  1015. .get_entry_flag = fat_get_entry_flag,
  1016. .set_entry_flag = fat_set_entry_flag,
  1017. .get_entry_clu0 = fat_get_entry_clu0,
  1018. .set_entry_clu0 = fat_set_entry_clu0,
  1019. .get_entry_size = fat_get_entry_size,
  1020. .set_entry_size = fat_set_entry_size,
  1021. .get_entry_time = fat_get_entry_time,
  1022. .set_entry_time = fat_set_entry_time,
  1023. .get_au_stat = amap_get_au_stat,
  1024. };
  1025. s32 mount_fat16(struct super_block *sb, pbr_t *p_pbr)
  1026. {
  1027. s32 num_root_sectors;
  1028. bpb16_t *p_bpb = &(p_pbr->bpb.f16);
  1029. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  1030. if (!p_bpb->num_fats) {
  1031. sdfat_msg(sb, KERN_ERR, "bogus number of FAT structure");
  1032. return -EINVAL;
  1033. }
  1034. num_root_sectors = get_unaligned_le16(p_bpb->num_root_entries) << DENTRY_SIZE_BITS;
  1035. num_root_sectors = ((num_root_sectors-1) >> sb->s_blocksize_bits) + 1;
  1036. fsi->sect_per_clus = p_bpb->sect_per_clus;
  1037. fsi->sect_per_clus_bits = ilog2(p_bpb->sect_per_clus);
  1038. fsi->cluster_size_bits = fsi->sect_per_clus_bits + sb->s_blocksize_bits;
  1039. fsi->cluster_size = 1 << fsi->cluster_size_bits;
  1040. fsi->num_FAT_sectors = le16_to_cpu(p_bpb->num_fat_sectors);
  1041. fsi->FAT1_start_sector = le16_to_cpu(p_bpb->num_reserved);
  1042. if (p_bpb->num_fats == 1)
  1043. fsi->FAT2_start_sector = fsi->FAT1_start_sector;
  1044. else
  1045. fsi->FAT2_start_sector = fsi->FAT1_start_sector + fsi->num_FAT_sectors;
  1046. fsi->root_start_sector = fsi->FAT2_start_sector + fsi->num_FAT_sectors;
  1047. fsi->data_start_sector = fsi->root_start_sector + num_root_sectors;
  1048. fsi->num_sectors = get_unaligned_le16(p_bpb->num_sectors);
  1049. if (!fsi->num_sectors)
  1050. fsi->num_sectors = le32_to_cpu(p_bpb->num_huge_sectors);
  1051. if (!fsi->num_sectors) {
  1052. sdfat_msg(sb, KERN_ERR, "bogus number of total sector count");
  1053. return -EINVAL;
  1054. }
  1055. fsi->num_clusters = (u32)((fsi->num_sectors - fsi->data_start_sector) >> fsi->sect_per_clus_bits) + CLUS_BASE;
  1056. /* because the cluster index starts with 2 */
  1057. fsi->vol_type = FAT16;
  1058. if (fsi->num_clusters < FAT12_THRESHOLD)
  1059. fsi->vol_type = FAT12;
  1060. fsi->vol_id = get_unaligned_le32(p_bpb->vol_serial);
  1061. fsi->root_dir = 0;
  1062. fsi->dentries_in_root = get_unaligned_le16(p_bpb->num_root_entries);
  1063. if (!fsi->dentries_in_root) {
  1064. sdfat_msg(sb, KERN_ERR, "bogus number of max dentry count "
  1065. "of the root directory");
  1066. return -EINVAL;
  1067. }
  1068. fsi->dentries_per_clu = 1 << (fsi->cluster_size_bits - DENTRY_SIZE_BITS);
  1069. fsi->vol_flag = VOL_CLEAN;
  1070. fsi->clu_srch_ptr = 2;
  1071. fsi->used_clusters = (u32) ~0;
  1072. fsi->fs_func = &fat_fs_func;
  1073. fat_ent_ops_init(sb);
  1074. if (p_bpb->state & FAT_VOL_DIRTY) {
  1075. fsi->vol_flag |= VOL_DIRTY;
  1076. sdfat_log_msg(sb, KERN_WARNING, "Volume was not properly "
  1077. "unmounted. Some data may be corrupt. "
  1078. "Please run fsck.");
  1079. }
  1080. return 0;
  1081. } /* end of mount_fat16 */
  1082. static sector_t __calc_hidden_sect(struct super_block *sb)
  1083. {
  1084. struct block_device *bdev = sb->s_bdev;
  1085. sector_t hidden = 0;
  1086. if (!bdev)
  1087. goto out;
  1088. hidden = bdev->bd_part->start_sect;
  1089. /* a disk device, not a partition */
  1090. if (!hidden) {
  1091. if (bdev != bdev->bd_contains)
  1092. sdfat_log_msg(sb, KERN_WARNING,
  1093. "hidden(0), but disk has a partition table");
  1094. goto out;
  1095. }
  1096. if (sb->s_blocksize_bits != 9) {
  1097. ASSERT(sb->s_blocksize_bits > 9);
  1098. hidden >>= (sb->s_blocksize_bits - 9);
  1099. }
  1100. out:
  1101. sdfat_log_msg(sb, KERN_INFO, "start_sect of part(%d) : %lld",
  1102. bdev ? bdev->bd_part->partno : -1, (s64)hidden);
  1103. return hidden;
  1104. }
  1105. s32 mount_fat32(struct super_block *sb, pbr_t *p_pbr)
  1106. {
  1107. pbr32_t *p_bpb = (pbr32_t *)p_pbr;
  1108. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  1109. if (!p_bpb->bpb.num_fats) {
  1110. sdfat_msg(sb, KERN_ERR, "bogus number of FAT structure");
  1111. return -EINVAL;
  1112. }
  1113. fsi->sect_per_clus = p_bpb->bpb.sect_per_clus;
  1114. fsi->sect_per_clus_bits = ilog2(p_bpb->bpb.sect_per_clus);
  1115. fsi->cluster_size_bits = fsi->sect_per_clus_bits + sb->s_blocksize_bits;
  1116. fsi->cluster_size = 1 << fsi->cluster_size_bits;
  1117. fsi->num_FAT_sectors = le32_to_cpu(p_bpb->bpb.num_fat32_sectors);
  1118. fsi->FAT1_start_sector = le16_to_cpu(p_bpb->bpb.num_reserved);
  1119. if (p_bpb->bpb.num_fats == 1)
  1120. fsi->FAT2_start_sector = fsi->FAT1_start_sector;
  1121. else
  1122. fsi->FAT2_start_sector = fsi->FAT1_start_sector + fsi->num_FAT_sectors;
  1123. fsi->root_start_sector = fsi->FAT2_start_sector + fsi->num_FAT_sectors;
  1124. fsi->data_start_sector = fsi->root_start_sector;
  1125. /* SPEC violation for compatibility */
  1126. fsi->num_sectors = get_unaligned_le16(p_bpb->bpb.num_sectors);
  1127. if (!fsi->num_sectors)
  1128. fsi->num_sectors = le32_to_cpu(p_bpb->bpb.num_huge_sectors);
  1129. /* 2nd check */
  1130. if (!fsi->num_sectors) {
  1131. sdfat_msg(sb, KERN_ERR, "bogus number of total sector count");
  1132. return -EINVAL;
  1133. }
  1134. fsi->num_clusters = (u32)((fsi->num_sectors - fsi->data_start_sector) >> fsi->sect_per_clus_bits) + CLUS_BASE;
  1135. /* because the cluster index starts with 2 */
  1136. fsi->vol_type = FAT32;
  1137. fsi->vol_id = get_unaligned_le32(p_bpb->bsx.vol_serial);
  1138. fsi->root_dir = le32_to_cpu(p_bpb->bpb.root_cluster);
  1139. fsi->dentries_in_root = 0;
  1140. fsi->dentries_per_clu = 1 << (fsi->cluster_size_bits - DENTRY_SIZE_BITS);
  1141. fsi->vol_flag = VOL_CLEAN;
  1142. fsi->clu_srch_ptr = 2;
  1143. fsi->used_clusters = (u32) ~0;
  1144. fsi->fs_func = &fat_fs_func;
  1145. /* Delayed / smart allocation related init */
  1146. fsi->reserved_clusters = 0;
  1147. /* Should be initialized before calling amap_create() */
  1148. fat_ent_ops_init(sb);
  1149. /* AU Map Creation */
  1150. if (SDFAT_SB(sb)->options.improved_allocation & SDFAT_ALLOC_SMART) {
  1151. u32 hidden_sectors = le32_to_cpu(p_bpb->bpb.num_hid_sectors);
  1152. u32 calc_hid_sect = 0;
  1153. int ret;
  1154. /* calculate hidden sector size */
  1155. calc_hid_sect = __calc_hidden_sect(sb);
  1156. if (calc_hid_sect != hidden_sectors) {
  1157. sdfat_log_msg(sb, KERN_WARNING, "abnormal hidden "
  1158. "sector : bpb(%u) != ondisk(%u)",
  1159. hidden_sectors, calc_hid_sect);
  1160. if (SDFAT_SB(sb)->options.adj_hidsect) {
  1161. sdfat_log_msg(sb, KERN_INFO,
  1162. "adjustment hidden sector : "
  1163. "bpb(%u) -> ondisk(%u)",
  1164. hidden_sectors, calc_hid_sect);
  1165. hidden_sectors = calc_hid_sect;
  1166. }
  1167. }
  1168. SDFAT_SB(sb)->options.amap_opt.misaligned_sect = hidden_sectors;
  1169. /* calculate AU size if it's not set */
  1170. if (!SDFAT_SB(sb)->options.amap_opt.sect_per_au) {
  1171. SDFAT_SB(sb)->options.amap_opt.sect_per_au =
  1172. __calc_default_au_size(sb);
  1173. }
  1174. ret = amap_create(sb,
  1175. SDFAT_SB(sb)->options.amap_opt.pack_ratio,
  1176. SDFAT_SB(sb)->options.amap_opt.sect_per_au,
  1177. SDFAT_SB(sb)->options.amap_opt.misaligned_sect);
  1178. if (ret) {
  1179. sdfat_log_msg(sb, KERN_WARNING, "failed to create AMAP."
  1180. " disabling smart allocation. (err:%d)", ret);
  1181. SDFAT_SB(sb)->options.improved_allocation &= ~(SDFAT_ALLOC_SMART);
  1182. } else {
  1183. fsi->fs_func = &amap_fat_fs_func;
  1184. }
  1185. }
  1186. /* Check dependency of mount options */
  1187. if (SDFAT_SB(sb)->options.improved_allocation !=
  1188. (SDFAT_ALLOC_DELAY | SDFAT_ALLOC_SMART)) {
  1189. sdfat_log_msg(sb, KERN_INFO, "disabling defragmentation because"
  1190. " smart, delay options are disabled");
  1191. SDFAT_SB(sb)->options.defrag = 0;
  1192. }
  1193. if (p_bpb->bsx.state & FAT_VOL_DIRTY) {
  1194. fsi->vol_flag |= VOL_DIRTY;
  1195. sdfat_log_msg(sb, KERN_WARNING, "Volume was not properly "
  1196. "unmounted. Some data may be corrupt. "
  1197. "Please run fsck.");
  1198. }
  1199. return 0;
  1200. } /* end of mount_fat32 */
  1201. /* end of core_fat.c */