core_exfat.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  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_exfat.c */
  21. /* PURPOSE : exFAT-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. /*----------------------------------------------------------------------*/
  41. /* Global Variable Definitions */
  42. /*----------------------------------------------------------------------*/
  43. /*----------------------------------------------------------------------*/
  44. /* Local Variable Definitions */
  45. /*----------------------------------------------------------------------*/
  46. static u8 free_bit[] = {
  47. 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2,/* 0 ~ 19*/
  48. 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3,/* 20 ~ 39*/
  49. 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/* 40 ~ 59*/
  50. 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,/* 60 ~ 79*/
  51. 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2,/* 80 ~ 99*/
  52. 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3,/*100 ~ 119*/
  53. 0, 1, 0, 2, 0, 1, 0, 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/*120 ~ 139*/
  54. 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,/*140 ~ 159*/
  55. 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2,/*160 ~ 179*/
  56. 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3,/*180 ~ 199*/
  57. 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/*200 ~ 219*/
  58. 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,/*220 ~ 239*/
  59. 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 /*240 ~ 254*/
  60. };
  61. static u8 used_bit[] = {
  62. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3,/* 0 ~ 19*/
  63. 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4,/* 20 ~ 39*/
  64. 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5,/* 40 ~ 59*/
  65. 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,/* 60 ~ 79*/
  66. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4,/* 80 ~ 99*/
  67. 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6,/*100 ~ 119*/
  68. 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4,/*120 ~ 139*/
  69. 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,/*140 ~ 159*/
  70. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5,/*160 ~ 179*/
  71. 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5,/*180 ~ 199*/
  72. 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6,/*200 ~ 219*/
  73. 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,/*220 ~ 239*/
  74. 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 /*240 ~ 255*/
  75. };
  76. /*======================================================================*/
  77. /* Local Function Definitions */
  78. /*======================================================================*/
  79. /*
  80. * Directory Entry Management Functions
  81. */
  82. static u32 exfat_get_entry_type(DENTRY_T *p_entry)
  83. {
  84. FILE_DENTRY_T *ep = (FILE_DENTRY_T *) p_entry;
  85. if (ep->type == EXFAT_UNUSED)
  86. return TYPE_UNUSED;
  87. if (ep->type < 0x80)
  88. return TYPE_DELETED;
  89. if (ep->type == 0x80)
  90. return TYPE_INVALID;
  91. if (ep->type < 0xA0) {
  92. if (ep->type == 0x81)
  93. return TYPE_BITMAP;
  94. if (ep->type == 0x82)
  95. return TYPE_UPCASE;
  96. if (ep->type == 0x83)
  97. return TYPE_VOLUME;
  98. if (ep->type == 0x85) {
  99. if (le16_to_cpu(ep->attr) & ATTR_SUBDIR)
  100. return TYPE_DIR;
  101. return TYPE_FILE;
  102. }
  103. return TYPE_CRITICAL_PRI;
  104. }
  105. if (ep->type < 0xC0) {
  106. if (ep->type == 0xA0)
  107. return TYPE_GUID;
  108. if (ep->type == 0xA1)
  109. return TYPE_PADDING;
  110. if (ep->type == 0xA2)
  111. return TYPE_ACLTAB;
  112. return TYPE_BENIGN_PRI;
  113. }
  114. if (ep->type < 0xE0) {
  115. if (ep->type == 0xC0)
  116. return TYPE_STREAM;
  117. if (ep->type == 0xC1)
  118. return TYPE_EXTEND;
  119. if (ep->type == 0xC2)
  120. return TYPE_ACL;
  121. return TYPE_CRITICAL_SEC;
  122. }
  123. return TYPE_BENIGN_SEC;
  124. } /* end of exfat_get_entry_type */
  125. static void exfat_set_entry_type(DENTRY_T *p_entry, u32 type)
  126. {
  127. FILE_DENTRY_T *ep = (FILE_DENTRY_T *) p_entry;
  128. if (type == TYPE_UNUSED) {
  129. ep->type = 0x0;
  130. } else if (type == TYPE_DELETED) {
  131. ep->type &= ~0x80;
  132. } else if (type == TYPE_STREAM) {
  133. ep->type = 0xC0;
  134. } else if (type == TYPE_EXTEND) {
  135. ep->type = 0xC1;
  136. } else if (type == TYPE_BITMAP) {
  137. ep->type = 0x81;
  138. } else if (type == TYPE_UPCASE) {
  139. ep->type = 0x82;
  140. } else if (type == TYPE_VOLUME) {
  141. ep->type = 0x83;
  142. } else if (type == TYPE_DIR) {
  143. ep->type = 0x85;
  144. ep->attr = cpu_to_le16(ATTR_SUBDIR);
  145. } else if (type == TYPE_FILE) {
  146. ep->type = 0x85;
  147. ep->attr = cpu_to_le16(ATTR_ARCHIVE);
  148. } else if (type == TYPE_SYMLINK) {
  149. ep->type = 0x85;
  150. ep->attr = cpu_to_le16(ATTR_ARCHIVE | ATTR_SYMLINK);
  151. }
  152. } /* end of exfat_set_entry_type */
  153. static u32 exfat_get_entry_attr(DENTRY_T *p_entry)
  154. {
  155. FILE_DENTRY_T *ep = (FILE_DENTRY_T *)p_entry;
  156. return (u32)le16_to_cpu(ep->attr);
  157. } /* end of exfat_get_entry_attr */
  158. static void exfat_set_entry_attr(DENTRY_T *p_entry, u32 attr)
  159. {
  160. FILE_DENTRY_T *ep = (FILE_DENTRY_T *)p_entry;
  161. ep->attr = cpu_to_le16((u16) attr);
  162. } /* end of exfat_set_entry_attr */
  163. static u8 exfat_get_entry_flag(DENTRY_T *p_entry)
  164. {
  165. STRM_DENTRY_T *ep = (STRM_DENTRY_T *)p_entry;
  166. return ep->flags;
  167. } /* end of exfat_get_entry_flag */
  168. static void exfat_set_entry_flag(DENTRY_T *p_entry, u8 flags)
  169. {
  170. STRM_DENTRY_T *ep = (STRM_DENTRY_T *)p_entry;
  171. ep->flags = flags;
  172. } /* end of exfat_set_entry_flag */
  173. static u32 exfat_get_entry_clu0(DENTRY_T *p_entry)
  174. {
  175. STRM_DENTRY_T *ep = (STRM_DENTRY_T *)p_entry;
  176. return (u32)le32_to_cpu(ep->start_clu);
  177. } /* end of exfat_get_entry_clu0 */
  178. static void exfat_set_entry_clu0(DENTRY_T *p_entry, u32 start_clu)
  179. {
  180. STRM_DENTRY_T *ep = (STRM_DENTRY_T *)p_entry;
  181. ep->start_clu = cpu_to_le32(start_clu);
  182. } /* end of exfat_set_entry_clu0 */
  183. static u64 exfat_get_entry_size(DENTRY_T *p_entry)
  184. {
  185. STRM_DENTRY_T *ep = (STRM_DENTRY_T *)p_entry;
  186. return le64_to_cpu(ep->valid_size);
  187. } /* end of exfat_get_entry_size */
  188. static void exfat_set_entry_size(DENTRY_T *p_entry, u64 size)
  189. {
  190. STRM_DENTRY_T *ep = (STRM_DENTRY_T *)p_entry;
  191. ep->valid_size = cpu_to_le64(size);
  192. ep->size = cpu_to_le64(size);
  193. } /* end of exfat_set_entry_size */
  194. #define TENS_MS_PER_SEC (100)
  195. #define SEC_TO_TENS_MS(sec) (((sec) & 0x01) ? TENS_MS_PER_SEC : 0)
  196. #define TENS_MS_TO_SEC(tens_ms) (((tens_ms) / TENS_MS_PER_SEC) ? 1 : 0)
  197. static void exfat_get_entry_time(DENTRY_T *p_entry, TIMESTAMP_T *tp, u8 mode)
  198. {
  199. u16 t = 0x00, d = 0x21, tz = 0x00, s = 0x00;
  200. FILE_DENTRY_T *ep = (FILE_DENTRY_T *)p_entry;
  201. switch (mode) {
  202. case TM_CREATE:
  203. t = le16_to_cpu(ep->create_time);
  204. d = le16_to_cpu(ep->create_date);
  205. s = TENS_MS_TO_SEC(ep->create_time_ms);
  206. tz = ep->create_tz;
  207. break;
  208. case TM_MODIFY:
  209. t = le16_to_cpu(ep->modify_time);
  210. d = le16_to_cpu(ep->modify_date);
  211. s = TENS_MS_TO_SEC(ep->modify_time_ms);
  212. tz = ep->modify_tz;
  213. break;
  214. case TM_ACCESS:
  215. t = le16_to_cpu(ep->access_time);
  216. d = le16_to_cpu(ep->access_date);
  217. tz = ep->access_tz;
  218. break;
  219. }
  220. tp->tz.value = tz;
  221. tp->sec = ((t & 0x001F) << 1) + s;
  222. tp->min = (t >> 5) & 0x003F;
  223. tp->hour = (t >> 11);
  224. tp->day = (d & 0x001F);
  225. tp->mon = (d >> 5) & 0x000F;
  226. tp->year = (d >> 9);
  227. } /* end of exfat_get_entry_time */
  228. static void exfat_set_entry_time(DENTRY_T *p_entry, TIMESTAMP_T *tp, u8 mode)
  229. {
  230. u16 t, d;
  231. FILE_DENTRY_T *ep = (FILE_DENTRY_T *)p_entry;
  232. t = (tp->hour << 11) | (tp->min << 5) | (tp->sec >> 1);
  233. d = (tp->year << 9) | (tp->mon << 5) | tp->day;
  234. switch (mode) {
  235. case TM_CREATE:
  236. ep->create_time = cpu_to_le16(t);
  237. ep->create_time_ms = SEC_TO_TENS_MS(tp->sec);
  238. ep->create_date = cpu_to_le16(d);
  239. ep->create_tz = tp->tz.value;
  240. break;
  241. case TM_MODIFY:
  242. ep->modify_time = cpu_to_le16(t);
  243. ep->modify_date = cpu_to_le16(d);
  244. ep->modify_time_ms = (tp->sec & 0x1) ? TENS_MS_PER_SEC : 0;
  245. ep->modify_tz = tp->tz.value;
  246. break;
  247. case TM_ACCESS:
  248. ep->access_time = cpu_to_le16(t);
  249. ep->access_date = cpu_to_le16(d);
  250. ep->access_tz = tp->tz.value;
  251. break;
  252. }
  253. } /* end of exfat_set_entry_time */
  254. static void __init_file_entry(struct super_block *sb, FILE_DENTRY_T *ep, u32 type)
  255. {
  256. TIMESTAMP_T tm, *tp;
  257. exfat_set_entry_type((DENTRY_T *) ep, type);
  258. tp = tm_now_sb(sb, &tm);
  259. exfat_set_entry_time((DENTRY_T *) ep, tp, TM_CREATE);
  260. exfat_set_entry_time((DENTRY_T *) ep, tp, TM_MODIFY);
  261. exfat_set_entry_time((DENTRY_T *) ep, tp, TM_ACCESS);
  262. } /* end of __init_file_entry */
  263. static void __init_strm_entry(STRM_DENTRY_T *ep, u8 flags, u32 start_clu, u64 size)
  264. {
  265. exfat_set_entry_type((DENTRY_T *) ep, TYPE_STREAM);
  266. ep->flags = flags;
  267. ep->start_clu = cpu_to_le32(start_clu);
  268. ep->valid_size = cpu_to_le64(size);
  269. ep->size = cpu_to_le64(size);
  270. } /* end of __init_strm_entry */
  271. static void __init_name_entry(NAME_DENTRY_T *ep, u16 *uniname)
  272. {
  273. s32 i;
  274. exfat_set_entry_type((DENTRY_T *) ep, TYPE_EXTEND);
  275. ep->flags = 0x0;
  276. for (i = 0; i < 15; i++) {
  277. ep->unicode_0_14[i] = cpu_to_le16(*uniname);
  278. if (*uniname == 0x0)
  279. break;
  280. uniname++;
  281. }
  282. } /* end of __init_name_entry */
  283. static s32 exfat_init_dir_entry(struct super_block *sb, CHAIN_T *p_dir, s32 entry, u32 type, u32 start_clu, u64 size)
  284. {
  285. u64 sector;
  286. u8 flags;
  287. FILE_DENTRY_T *file_ep;
  288. STRM_DENTRY_T *strm_ep;
  289. flags = (type == TYPE_FILE) ? 0x01 : 0x03;
  290. /* we cannot use get_dentry_set_in_dir here because file ep is not initialized yet */
  291. file_ep = (FILE_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry, &sector);
  292. if (!file_ep)
  293. return -EIO;
  294. strm_ep = (STRM_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry+1, &sector);
  295. if (!strm_ep)
  296. return -EIO;
  297. __init_file_entry(sb, file_ep, type);
  298. if (dcache_modify(sb, sector))
  299. return -EIO;
  300. __init_strm_entry(strm_ep, flags, start_clu, size);
  301. if (dcache_modify(sb, sector))
  302. return -EIO;
  303. return 0;
  304. } /* end of exfat_init_dir_entry */
  305. s32 update_dir_chksum(struct super_block *sb, CHAIN_T *p_dir, s32 entry)
  306. {
  307. s32 ret = -EIO;
  308. s32 i, num_entries;
  309. u64 sector;
  310. u16 chksum;
  311. FILE_DENTRY_T *file_ep;
  312. DENTRY_T *ep;
  313. file_ep = (FILE_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry, &sector);
  314. if (!file_ep)
  315. return -EIO;
  316. dcache_lock(sb, sector);
  317. num_entries = (s32) file_ep->num_ext + 1;
  318. chksum = calc_chksum_2byte((void *) file_ep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
  319. for (i = 1; i < num_entries; i++) {
  320. ep = get_dentry_in_dir(sb, p_dir, entry+i, NULL);
  321. if (!ep)
  322. goto out_unlock;
  323. chksum = calc_chksum_2byte((void *) ep, DENTRY_SIZE, chksum, CS_DEFAULT);
  324. }
  325. file_ep->checksum = cpu_to_le16(chksum);
  326. ret = dcache_modify(sb, sector);
  327. out_unlock:
  328. dcache_unlock(sb, sector);
  329. return ret;
  330. } /* end of update_dir_chksum */
  331. static s32 exfat_init_ext_entry(struct super_block *sb, CHAIN_T *p_dir, s32 entry, s32 num_entries,
  332. UNI_NAME_T *p_uniname, DOS_NAME_T *p_dosname)
  333. {
  334. s32 i;
  335. u64 sector;
  336. u16 *uniname = p_uniname->name;
  337. FILE_DENTRY_T *file_ep;
  338. STRM_DENTRY_T *strm_ep;
  339. NAME_DENTRY_T *name_ep;
  340. file_ep = (FILE_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry, &sector);
  341. if (!file_ep)
  342. return -EIO;
  343. file_ep->num_ext = (u8)(num_entries - 1);
  344. dcache_modify(sb, sector);
  345. strm_ep = (STRM_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry+1, &sector);
  346. if (!strm_ep)
  347. return -EIO;
  348. strm_ep->name_len = p_uniname->name_len;
  349. strm_ep->name_hash = cpu_to_le16(p_uniname->name_hash);
  350. dcache_modify(sb, sector);
  351. for (i = 2; i < num_entries; i++) {
  352. name_ep = (NAME_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry+i, &sector);
  353. if (!name_ep)
  354. return -EIO;
  355. __init_name_entry(name_ep, uniname);
  356. dcache_modify(sb, sector);
  357. uniname += 15;
  358. }
  359. update_dir_chksum(sb, p_dir, entry);
  360. return 0;
  361. } /* end of exfat_init_ext_entry */
  362. static s32 exfat_delete_dir_entry(struct super_block *sb, CHAIN_T *p_dir, s32 entry, s32 order, s32 num_entries)
  363. {
  364. s32 i;
  365. u64 sector;
  366. DENTRY_T *ep;
  367. for (i = order; i < num_entries; i++) {
  368. ep = get_dentry_in_dir(sb, p_dir, entry+i, &sector);
  369. if (!ep)
  370. return -EIO;
  371. exfat_set_entry_type(ep, TYPE_DELETED);
  372. if (dcache_modify(sb, sector))
  373. return -EIO;
  374. }
  375. return 0;
  376. }
  377. static s32 __write_partial_entries_in_entry_set(struct super_block *sb,
  378. ENTRY_SET_CACHE_T *es, u64 sec, u32 off, u32 count)
  379. {
  380. s32 num_entries;
  381. u32 buf_off = (off - es->offset);
  382. u32 remaining_byte_in_sector, copy_entries;
  383. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  384. u32 clu;
  385. u8 *buf, *esbuf = (u8 *)&(es->__buf);
  386. TMSG("%s entered\n", __func__);
  387. MMSG("%s: es %p sec %llu off %u cnt %d\n", __func__, es, sec, off, count);
  388. num_entries = count;
  389. while (num_entries) {
  390. /* write per sector base */
  391. remaining_byte_in_sector = (1 << sb->s_blocksize_bits) - off;
  392. copy_entries = min((s32)(remaining_byte_in_sector >> DENTRY_SIZE_BITS), num_entries);
  393. buf = dcache_getblk(sb, sec);
  394. if (!buf)
  395. goto err_out;
  396. MMSG("es->buf %p buf_off %u\n", esbuf, buf_off);
  397. MMSG("copying %d entries from %p to sector %llu\n", copy_entries, (esbuf + buf_off), sec);
  398. memcpy(buf + off, esbuf + buf_off, copy_entries << DENTRY_SIZE_BITS);
  399. dcache_modify(sb, sec);
  400. num_entries -= copy_entries;
  401. if (num_entries) {
  402. // get next sector
  403. if (IS_LAST_SECT_IN_CLUS(fsi, sec)) {
  404. clu = SECT_TO_CLUS(fsi, sec);
  405. if (es->alloc_flag == 0x03)
  406. clu++;
  407. else if (get_next_clus_safe(sb, &clu))
  408. goto err_out;
  409. sec = CLUS_TO_SECT(fsi, clu);
  410. } else {
  411. sec++;
  412. }
  413. off = 0;
  414. buf_off += copy_entries << DENTRY_SIZE_BITS;
  415. }
  416. }
  417. TMSG("%s exited successfully\n", __func__);
  418. return 0;
  419. err_out:
  420. TMSG("%s failed\n", __func__);
  421. return -EIO;
  422. }
  423. /* write back all entries in entry set */
  424. static s32 __write_whole_entry_set(struct super_block *sb, ENTRY_SET_CACHE_T *es)
  425. {
  426. return __write_partial_entries_in_entry_set(sb, es, es->sector, es->offset, es->num_entries);
  427. }
  428. s32 update_dir_chksum_with_entry_set(struct super_block *sb, ENTRY_SET_CACHE_T *es)
  429. {
  430. DENTRY_T *ep;
  431. u16 chksum = 0;
  432. s32 chksum_type = CS_DIR_ENTRY, i;
  433. ep = (DENTRY_T *)&(es->__buf);
  434. for (i = 0; i < es->num_entries; i++) {
  435. MMSG("%s %p\n", __func__, ep);
  436. chksum = calc_chksum_2byte((void *) ep, DENTRY_SIZE, chksum, chksum_type);
  437. ep++;
  438. chksum_type = CS_DEFAULT;
  439. }
  440. ep = (DENTRY_T *)&(es->__buf);
  441. ((FILE_DENTRY_T *)ep)->checksum = cpu_to_le16(chksum);
  442. return __write_whole_entry_set(sb, es);
  443. }
  444. /* returns a set of dentries for a file or dir.
  445. * Note that this is a copy (dump) of dentries so that user should call write_entry_set()
  446. * to apply changes made in this entry set to the real device.
  447. * in:
  448. * sb+p_dir+entry: indicates a file/dir
  449. * type: specifies how many dentries should be included.
  450. * out:
  451. * file_ep: will point the first dentry(= file dentry) on success
  452. * return:
  453. * pointer of entry set on success,
  454. * NULL on failure.
  455. */
  456. #define ES_MODE_STARTED 0
  457. #define ES_MODE_GET_FILE_ENTRY 1
  458. #define ES_MODE_GET_STRM_ENTRY 2
  459. #define ES_MODE_GET_NAME_ENTRY 3
  460. #define ES_MODE_GET_CRITICAL_SEC_ENTRY 4
  461. ENTRY_SET_CACHE_T *get_dentry_set_in_dir(struct super_block *sb,
  462. CHAIN_T *p_dir, s32 entry, u32 type, DENTRY_T **file_ep)
  463. {
  464. s32 ret;
  465. u32 off, byte_offset, clu = 0;
  466. u32 entry_type;
  467. u64 sec;
  468. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  469. ENTRY_SET_CACHE_T *es = NULL;
  470. DENTRY_T *ep, *pos;
  471. u8 *buf;
  472. u8 num_entries;
  473. s32 mode = ES_MODE_STARTED;
  474. /* FIXME : is available in error case? */
  475. if (p_dir->dir == DIR_DELETED) {
  476. EMSG("%s : access to deleted dentry\n", __func__);
  477. BUG_ON(!fsi->prev_eio);
  478. return NULL;
  479. }
  480. TMSG("%s entered\n", __func__);
  481. MMSG("p_dir dir %u flags %x size %d\n", p_dir->dir, p_dir->flags, p_dir->size);
  482. MMSG("entry %d type %d\n", entry, type);
  483. byte_offset = entry << DENTRY_SIZE_BITS;
  484. ret = walk_fat_chain(sb, p_dir, byte_offset, &clu);
  485. if (ret)
  486. return NULL;
  487. /* byte offset in cluster */
  488. byte_offset &= fsi->cluster_size - 1;
  489. /* byte offset in sector */
  490. off = byte_offset & (u32)(sb->s_blocksize - 1);
  491. /* sector offset in cluster */
  492. sec = byte_offset >> (sb->s_blocksize_bits);
  493. sec += CLUS_TO_SECT(fsi, clu);
  494. buf = dcache_getblk(sb, sec);
  495. if (!buf)
  496. goto err_out;
  497. ep = (DENTRY_T *)(buf + off);
  498. entry_type = exfat_get_entry_type(ep);
  499. if ((entry_type != TYPE_FILE)
  500. && (entry_type != TYPE_DIR))
  501. goto err_out;
  502. if (type == ES_ALL_ENTRIES)
  503. num_entries = ((FILE_DENTRY_T *)ep)->num_ext+1;
  504. else
  505. num_entries = type;
  506. MMSG("trying to malloc %lx bytes for %d entries\n",
  507. (unsigned long)(offsetof(ENTRY_SET_CACHE_T, __buf) + (num_entries) * sizeof(DENTRY_T)), num_entries);
  508. es = kmalloc((offsetof(ENTRY_SET_CACHE_T, __buf) + (num_entries) * sizeof(DENTRY_T)), GFP_KERNEL);
  509. if (!es) {
  510. EMSG("%s: failed to alloc entryset\n", __func__);
  511. goto err_out;
  512. }
  513. es->num_entries = num_entries;
  514. es->sector = sec;
  515. es->offset = off;
  516. es->alloc_flag = p_dir->flags;
  517. pos = (DENTRY_T *) &(es->__buf);
  518. while (num_entries) {
  519. // instead of copying whole sector, we will check every entry.
  520. // this will provide minimum stablity and consistency.
  521. entry_type = exfat_get_entry_type(ep);
  522. if ((entry_type == TYPE_UNUSED) || (entry_type == TYPE_DELETED))
  523. goto err_out;
  524. switch (mode) {
  525. case ES_MODE_STARTED:
  526. if ((entry_type == TYPE_FILE) || (entry_type == TYPE_DIR))
  527. mode = ES_MODE_GET_FILE_ENTRY;
  528. else
  529. goto err_out;
  530. break;
  531. case ES_MODE_GET_FILE_ENTRY:
  532. if (entry_type == TYPE_STREAM)
  533. mode = ES_MODE_GET_STRM_ENTRY;
  534. else
  535. goto err_out;
  536. break;
  537. case ES_MODE_GET_STRM_ENTRY:
  538. if (entry_type == TYPE_EXTEND)
  539. mode = ES_MODE_GET_NAME_ENTRY;
  540. else
  541. goto err_out;
  542. break;
  543. case ES_MODE_GET_NAME_ENTRY:
  544. if (entry_type == TYPE_EXTEND)
  545. break;
  546. else if (entry_type == TYPE_STREAM)
  547. goto err_out;
  548. else if (entry_type & TYPE_CRITICAL_SEC)
  549. mode = ES_MODE_GET_CRITICAL_SEC_ENTRY;
  550. else
  551. goto err_out;
  552. break;
  553. case ES_MODE_GET_CRITICAL_SEC_ENTRY:
  554. if ((entry_type == TYPE_EXTEND) || (entry_type == TYPE_STREAM))
  555. goto err_out;
  556. else if ((entry_type & TYPE_CRITICAL_SEC) != TYPE_CRITICAL_SEC)
  557. goto err_out;
  558. break;
  559. }
  560. /* copy dentry */
  561. memcpy(pos, ep, sizeof(DENTRY_T));
  562. if (--num_entries == 0)
  563. break;
  564. if (((off + DENTRY_SIZE) & (u32)(sb->s_blocksize - 1)) <
  565. (off & (u32)(sb->s_blocksize - 1))) {
  566. // get the next sector
  567. if (IS_LAST_SECT_IN_CLUS(fsi, sec)) {
  568. if (es->alloc_flag == 0x03)
  569. clu++;
  570. else if (get_next_clus_safe(sb, &clu))
  571. goto err_out;
  572. sec = CLUS_TO_SECT(fsi, clu);
  573. } else {
  574. sec++;
  575. }
  576. buf = dcache_getblk(sb, sec);
  577. if (!buf)
  578. goto err_out;
  579. off = 0;
  580. ep = (DENTRY_T *)(buf);
  581. } else {
  582. ep++;
  583. off += DENTRY_SIZE;
  584. }
  585. pos++;
  586. }
  587. if (file_ep)
  588. *file_ep = (DENTRY_T *)&(es->__buf);
  589. MMSG("es sec %llu offset %u flags %d, num_entries %u buf ptr %p\n",
  590. es->sector, es->offset, es->alloc_flag, es->num_entries, &(es->__buf));
  591. TMSG("%s exited %p\n", __func__, es);
  592. return es;
  593. err_out:
  594. TMSG("%s exited (return NULL) (es %p)\n", __func__, es);
  595. /* kfree(NULL) is safe */
  596. kfree(es);
  597. es = NULL;
  598. return NULL;
  599. }
  600. void release_dentry_set(ENTRY_SET_CACHE_T *es)
  601. {
  602. TMSG("%s %p\n", __func__, es);
  603. /* kfree(NULL) is safe */
  604. kfree(es);
  605. es = NULL;
  606. }
  607. static s32 __extract_uni_name_from_name_entry(NAME_DENTRY_T *ep, u16 *uniname, s32 order)
  608. {
  609. s32 i, len = 0;
  610. for (i = 0; i < 15; i++) {
  611. /* FIXME : unaligned? */
  612. *uniname = le16_to_cpu(ep->unicode_0_14[i]);
  613. if (*uniname == 0x0)
  614. return len;
  615. uniname++;
  616. len++;
  617. }
  618. *uniname = 0x0;
  619. return len;
  620. } /* end of __extract_uni_name_from_name_entry */
  621. #define DIRENT_STEP_FILE (0)
  622. #define DIRENT_STEP_STRM (1)
  623. #define DIRENT_STEP_NAME (2)
  624. #define DIRENT_STEP_SECD (3)
  625. /* return values of exfat_find_dir_entry()
  626. * >= 0 : return dir entiry position with the name in dir
  627. * -EEXIST : (root dir, ".") it is the root dir itself
  628. * -ENOENT : entry with the name does not exist
  629. * -EIO : I/O error
  630. */
  631. static s32 exfat_find_dir_entry(struct super_block *sb, FILE_ID_T *fid,
  632. CHAIN_T *p_dir, UNI_NAME_T *p_uniname, s32 num_entries, DOS_NAME_T *unused, u32 type)
  633. {
  634. s32 i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
  635. s32 order, step, name_len;
  636. s32 dentries_per_clu, num_empty = 0;
  637. u32 entry_type;
  638. u16 entry_uniname[16], *uniname = NULL, unichar;
  639. CHAIN_T clu;
  640. DENTRY_T *ep;
  641. HINT_T *hint_stat = &fid->hint_stat;
  642. HINT_FEMP_T candi_empty;
  643. FILE_DENTRY_T *file_ep;
  644. STRM_DENTRY_T *strm_ep;
  645. NAME_DENTRY_T *name_ep;
  646. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  647. /*
  648. * REMARK:
  649. * DOT and DOTDOT are handled by VFS layer
  650. */
  651. if (IS_CLUS_FREE(p_dir->dir))
  652. return -EIO;
  653. dentries_per_clu = fsi->dentries_per_clu;
  654. clu.dir = p_dir->dir;
  655. clu.size = p_dir->size;
  656. clu.flags = p_dir->flags;
  657. if (hint_stat->eidx) {
  658. clu.dir = hint_stat->clu;
  659. dentry = hint_stat->eidx;
  660. end_eidx = dentry;
  661. }
  662. candi_empty.eidx = -1;
  663. rewind:
  664. order = 0;
  665. step = DIRENT_STEP_FILE;
  666. while (!IS_CLUS_EOF(clu.dir)) {
  667. i = dentry & (dentries_per_clu - 1);
  668. for (; i < dentries_per_clu; i++, dentry++) {
  669. if (rewind && (dentry == end_eidx))
  670. goto not_found;
  671. ep = get_dentry_in_dir(sb, &clu, i, NULL);
  672. if (!ep)
  673. return -EIO;
  674. entry_type = exfat_get_entry_type(ep);
  675. if ((entry_type == TYPE_UNUSED) || (entry_type == TYPE_DELETED)) {
  676. step = DIRENT_STEP_FILE;
  677. num_empty++;
  678. if (candi_empty.eidx == -1) {
  679. if (num_empty == 1) {
  680. candi_empty.cur.dir = clu.dir;
  681. candi_empty.cur.size = clu.size;
  682. candi_empty.cur.flags = clu.flags;
  683. }
  684. if (num_empty >= num_entries) {
  685. candi_empty.eidx = dentry - (num_empty - 1);
  686. ASSERT(0 <= candi_empty.eidx);
  687. candi_empty.count = num_empty;
  688. if ((fid->hint_femp.eidx == -1) ||
  689. (candi_empty.eidx <= fid->hint_femp.eidx)) {
  690. memcpy(&fid->hint_femp,
  691. &candi_empty,
  692. sizeof(HINT_FEMP_T));
  693. }
  694. }
  695. }
  696. if (entry_type == TYPE_UNUSED)
  697. goto not_found;
  698. continue;
  699. }
  700. num_empty = 0;
  701. candi_empty.eidx = -1;
  702. if ((entry_type == TYPE_FILE) || (entry_type == TYPE_DIR)) {
  703. step = DIRENT_STEP_FILE;
  704. if ((type == TYPE_ALL) || (type == entry_type)) {
  705. file_ep = (FILE_DENTRY_T *) ep;
  706. num_ext = file_ep->num_ext;
  707. step = DIRENT_STEP_STRM;
  708. }
  709. continue;
  710. }
  711. if (entry_type == TYPE_STREAM) {
  712. if (step != DIRENT_STEP_STRM) {
  713. step = DIRENT_STEP_FILE;
  714. continue;
  715. }
  716. step = DIRENT_STEP_FILE;
  717. strm_ep = (STRM_DENTRY_T *) ep;
  718. if ((p_uniname->name_hash == le16_to_cpu(strm_ep->name_hash)) &&
  719. (p_uniname->name_len == strm_ep->name_len)) {
  720. step = DIRENT_STEP_NAME;
  721. order = 1;
  722. name_len = 0;
  723. }
  724. continue;
  725. }
  726. if (entry_type == TYPE_EXTEND) {
  727. if (step != DIRENT_STEP_NAME) {
  728. step = DIRENT_STEP_FILE;
  729. continue;
  730. }
  731. name_ep = (NAME_DENTRY_T *) ep;
  732. if ((++order) == 2)
  733. uniname = p_uniname->name;
  734. else
  735. uniname += 15;
  736. len = __extract_uni_name_from_name_entry(name_ep, entry_uniname, order);
  737. name_len += len;
  738. unichar = *(uniname+len);
  739. *(uniname+len) = 0x0;
  740. if (nls_cmp_uniname(sb, uniname, entry_uniname)) {
  741. step = DIRENT_STEP_FILE;
  742. } else if (name_len == p_uniname->name_len) {
  743. if (order == num_ext) {
  744. //fid->hint_femp.eidx = -1;
  745. goto found;
  746. }
  747. step = DIRENT_STEP_SECD;
  748. }
  749. *(uniname+len) = unichar;
  750. continue;
  751. }
  752. if (entry_type & (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
  753. if (step == DIRENT_STEP_SECD) {
  754. if (++order == num_ext)
  755. goto found;
  756. continue;
  757. }
  758. }
  759. step = DIRENT_STEP_FILE;
  760. }
  761. if (clu.flags == 0x03) {
  762. if ((--clu.size) > 0)
  763. clu.dir++;
  764. else
  765. clu.dir = CLUS_EOF;
  766. } else {
  767. if (get_next_clus_safe(sb, &clu.dir))
  768. return -EIO;
  769. }
  770. }
  771. not_found:
  772. /* we started at not 0 index,so we should try to find target
  773. * from 0 index to the index we started at.
  774. */
  775. if (!rewind && end_eidx) {
  776. rewind = 1;
  777. dentry = 0;
  778. clu.dir = p_dir->dir;
  779. /* reset empty hint */
  780. num_empty = 0;
  781. candi_empty.eidx = -1;
  782. goto rewind;
  783. }
  784. /* initialized hint_stat */
  785. hint_stat->clu = p_dir->dir;
  786. hint_stat->eidx = 0;
  787. return -ENOENT;
  788. found:
  789. /* next dentry we'll find is out of this cluster */
  790. if (!((dentry + 1) & (dentries_per_clu-1))) {
  791. int ret = 0;
  792. if (clu.flags == 0x03) {
  793. if ((--clu.size) > 0)
  794. clu.dir++;
  795. else
  796. clu.dir = CLUS_EOF;
  797. } else {
  798. ret = get_next_clus_safe(sb, &clu.dir);
  799. }
  800. if (ret || IS_CLUS_EOF(clu.dir)) {
  801. /* just initialized hint_stat */
  802. hint_stat->clu = p_dir->dir;
  803. hint_stat->eidx = 0;
  804. return (dentry - num_ext);
  805. }
  806. }
  807. hint_stat->clu = clu.dir;
  808. hint_stat->eidx = dentry + 1;
  809. return (dentry - num_ext);
  810. } /* end of exfat_find_dir_entry */
  811. /* returns -EIO on error */
  812. static s32 exfat_count_ext_entries(struct super_block *sb, CHAIN_T *p_dir, s32 entry, DENTRY_T *p_entry)
  813. {
  814. s32 i, count = 0;
  815. u32 type;
  816. FILE_DENTRY_T *file_ep = (FILE_DENTRY_T *) p_entry;
  817. DENTRY_T *ext_ep;
  818. for (i = 0, entry++; i < file_ep->num_ext; i++, entry++) {
  819. ext_ep = get_dentry_in_dir(sb, p_dir, entry, NULL);
  820. if (!ext_ep)
  821. return -EIO;
  822. type = exfat_get_entry_type(ext_ep);
  823. if ((type == TYPE_EXTEND) || (type == TYPE_STREAM))
  824. count++;
  825. else
  826. return count;
  827. }
  828. return count;
  829. } /* end of exfat_count_ext_entries */
  830. /*
  831. * Name Conversion Functions
  832. */
  833. static void exfat_get_uniname_from_ext_entry(struct super_block *sb, CHAIN_T *p_dir, s32 entry, u16 *uniname)
  834. {
  835. s32 i;
  836. DENTRY_T *ep;
  837. ENTRY_SET_CACHE_T *es;
  838. es = get_dentry_set_in_dir(sb, p_dir, entry, ES_ALL_ENTRIES, &ep);
  839. if (!es)
  840. return;
  841. if (es->num_entries < 3)
  842. goto out;
  843. ep += 2;
  844. /*
  845. * First entry : file entry
  846. * Second entry : stream-extension entry
  847. * Third entry : first file-name entry
  848. * So, the index of first file-name dentry should start from 2.
  849. */
  850. for (i = 2; i < es->num_entries; i++, ep++) {
  851. /* end of name entry */
  852. if (exfat_get_entry_type(ep) != TYPE_EXTEND)
  853. goto out;
  854. __extract_uni_name_from_name_entry((NAME_DENTRY_T *)ep, uniname, i);
  855. uniname += 15;
  856. }
  857. out:
  858. release_dentry_set(es);
  859. } /* end of exfat_get_uniname_from_ext_entry */
  860. static s32 exfat_calc_num_entries(UNI_NAME_T *p_uniname)
  861. {
  862. s32 len;
  863. len = p_uniname->name_len;
  864. if (len == 0)
  865. return 0;
  866. /* 1 file entry + 1 stream entry + name entries */
  867. return((len-1) / 15 + 3);
  868. } /* end of exfat_calc_num_entries */
  869. static s32 exfat_check_max_dentries(FILE_ID_T *fid)
  870. {
  871. if ((fid->size >> DENTRY_SIZE_BITS) >= MAX_EXFAT_DENTRIES) {
  872. /* exFAT spec allows a dir to grow upto 8388608(256MB) dentries */
  873. return -ENOSPC;
  874. }
  875. return 0;
  876. } /* end of check_max_dentries */
  877. /*
  878. * Allocation Bitmap Management Functions
  879. */
  880. s32 load_alloc_bmp(struct super_block *sb)
  881. {
  882. s32 ret;
  883. u32 i, j, map_size, type, need_map_size;
  884. u64 sector;
  885. CHAIN_T clu;
  886. BMAP_DENTRY_T *ep;
  887. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  888. clu.dir = fsi->root_dir;
  889. clu.flags = 0x01;
  890. while (!IS_CLUS_EOF(clu.dir)) {
  891. for (i = 0; i < fsi->dentries_per_clu; i++) {
  892. ep = (BMAP_DENTRY_T *) get_dentry_in_dir(sb, &clu, i, NULL);
  893. if (!ep)
  894. return -EIO;
  895. type = exfat_get_entry_type((DENTRY_T *) ep);
  896. if (type == TYPE_UNUSED)
  897. break;
  898. if (type != TYPE_BITMAP)
  899. continue;
  900. if (ep->flags == 0x0) {
  901. fsi->map_clu = le32_to_cpu(ep->start_clu);
  902. map_size = (u32) le64_to_cpu(ep->size);
  903. need_map_size = (((fsi->num_clusters - CLUS_BASE) - 1) >> 3) + 1;
  904. if (need_map_size != map_size) {
  905. sdfat_log_msg(sb, KERN_ERR,
  906. "bogus allocation bitmap size(need : %u, cur : %u)",
  907. need_map_size, map_size);
  908. /* Only allowed when bogus allocation bitmap size is large */
  909. if (need_map_size > map_size)
  910. return -EIO;
  911. }
  912. fsi->map_sectors = ((need_map_size - 1) >> (sb->s_blocksize_bits)) + 1;
  913. fsi->vol_amap =
  914. kmalloc((sizeof(struct buffer_head *) * fsi->map_sectors), GFP_KERNEL);
  915. if (!fsi->vol_amap)
  916. return -ENOMEM;
  917. sector = CLUS_TO_SECT(fsi, fsi->map_clu);
  918. for (j = 0; j < fsi->map_sectors; j++) {
  919. fsi->vol_amap[j] = NULL;
  920. ret = read_sect(sb, sector+j, &(fsi->vol_amap[j]), 1);
  921. if (ret) {
  922. /* release all buffers and free vol_amap */
  923. i = 0;
  924. while (i < j)
  925. brelse(fsi->vol_amap[i++]);
  926. /* kfree(NULL) is safe */
  927. kfree(fsi->vol_amap);
  928. fsi->vol_amap = NULL;
  929. return ret;
  930. }
  931. }
  932. fsi->pbr_bh = NULL;
  933. return 0;
  934. }
  935. }
  936. if (get_next_clus_safe(sb, &clu.dir))
  937. return -EIO;
  938. }
  939. return -EINVAL;
  940. } /* end of load_alloc_bmp */
  941. void free_alloc_bmp(struct super_block *sb)
  942. {
  943. s32 i;
  944. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  945. brelse(fsi->pbr_bh);
  946. for (i = 0; i < fsi->map_sectors; i++)
  947. __brelse(fsi->vol_amap[i]);
  948. /* kfree(NULL) is safe */
  949. kfree(fsi->vol_amap);
  950. fsi->vol_amap = NULL;
  951. }
  952. /* WARN :
  953. * If the value of "clu" is 0, it means cluster 2 which is
  954. * the first cluster of cluster heap.
  955. */
  956. static s32 set_alloc_bitmap(struct super_block *sb, u32 clu)
  957. {
  958. s32 i, b;
  959. u64 sector;
  960. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  961. i = clu >> (sb->s_blocksize_bits + 3);
  962. b = clu & (u32)((sb->s_blocksize << 3) - 1);
  963. sector = CLUS_TO_SECT(fsi, fsi->map_clu) + i;
  964. bitmap_set((unsigned long *)(fsi->vol_amap[i]->b_data), b, 1);
  965. return write_sect(sb, sector, fsi->vol_amap[i], 0);
  966. } /* end of set_alloc_bitmap */
  967. /* WARN :
  968. * If the value of "clu" is 0, it means cluster 2 which is
  969. * the first cluster of cluster heap.
  970. */
  971. static s32 clr_alloc_bitmap(struct super_block *sb, u32 clu)
  972. {
  973. s32 ret;
  974. s32 i, b;
  975. u64 sector;
  976. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  977. struct sdfat_mount_options *opts = &sbi->options;
  978. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  979. i = clu >> (sb->s_blocksize_bits + 3);
  980. b = clu & (u32)((sb->s_blocksize << 3) - 1);
  981. sector = CLUS_TO_SECT(fsi, fsi->map_clu) + i;
  982. bitmap_clear((unsigned long *)(fsi->vol_amap[i]->b_data), b, 1);
  983. ret = write_sect(sb, sector, fsi->vol_amap[i], 0);
  984. if (opts->discard) {
  985. s32 ret_discard;
  986. TMSG("discard cluster(%08x)\n", clu+2);
  987. ret_discard = sb_issue_discard(sb, CLUS_TO_SECT(fsi, clu+2),
  988. (1 << fsi->sect_per_clus_bits), GFP_NOFS, 0);
  989. if (ret_discard == -EOPNOTSUPP) {
  990. sdfat_msg(sb, KERN_ERR,
  991. "discard not supported by device, disabling");
  992. opts->discard = 0;
  993. }
  994. }
  995. return ret;
  996. } /* end of clr_alloc_bitmap */
  997. /* WARN :
  998. * If the value of "clu" is 0, it means cluster 2 which is
  999. * the first cluster of cluster heap.
  1000. */
  1001. static u32 test_alloc_bitmap(struct super_block *sb, u32 clu)
  1002. {
  1003. u32 i, map_i, map_b;
  1004. u32 clu_base, clu_free;
  1005. u8 k, clu_mask;
  1006. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  1007. clu_base = (clu & ~(0x7)) + 2;
  1008. clu_mask = (1 << (clu - clu_base + 2)) - 1;
  1009. map_i = clu >> (sb->s_blocksize_bits + 3);
  1010. map_b = (clu >> 3) & (u32)(sb->s_blocksize - 1);
  1011. for (i = 2; i < fsi->num_clusters; i += 8) {
  1012. k = *(((u8 *) fsi->vol_amap[map_i]->b_data) + map_b);
  1013. if (clu_mask > 0) {
  1014. k |= clu_mask;
  1015. clu_mask = 0;
  1016. }
  1017. if (k < 0xFF) {
  1018. clu_free = clu_base + free_bit[k];
  1019. if (clu_free < fsi->num_clusters)
  1020. return clu_free;
  1021. }
  1022. clu_base += 8;
  1023. if (((++map_b) >= (u32)sb->s_blocksize) ||
  1024. (clu_base >= fsi->num_clusters)) {
  1025. if ((++map_i) >= fsi->map_sectors) {
  1026. clu_base = 2;
  1027. map_i = 0;
  1028. }
  1029. map_b = 0;
  1030. }
  1031. }
  1032. return CLUS_EOF;
  1033. } /* end of test_alloc_bitmap */
  1034. void sync_alloc_bmp(struct super_block *sb)
  1035. {
  1036. s32 i;
  1037. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  1038. if (fsi->vol_amap == NULL)
  1039. return;
  1040. for (i = 0; i < fsi->map_sectors; i++)
  1041. sync_dirty_buffer(fsi->vol_amap[i]);
  1042. }
  1043. static s32 exfat_chain_cont_cluster(struct super_block *sb, u32 chain, u32 len)
  1044. {
  1045. if (!len)
  1046. return 0;
  1047. while (len > 1) {
  1048. if (fat_ent_set(sb, chain, chain+1))
  1049. return -EIO;
  1050. chain++;
  1051. len--;
  1052. }
  1053. if (fat_ent_set(sb, chain, CLUS_EOF))
  1054. return -EIO;
  1055. return 0;
  1056. }
  1057. s32 chain_cont_cluster(struct super_block *sb, u32 chain, u32 len)
  1058. {
  1059. return exfat_chain_cont_cluster(sb, chain, len);
  1060. }
  1061. static s32 exfat_free_cluster(struct super_block *sb, CHAIN_T *p_chain, s32 do_relse)
  1062. {
  1063. s32 ret = -EIO;
  1064. u32 num_clusters = 0;
  1065. u32 clu;
  1066. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  1067. s32 i;
  1068. u64 sector;
  1069. /* invalid cluster number */
  1070. if (IS_CLUS_FREE(p_chain->dir) || IS_CLUS_EOF(p_chain->dir))
  1071. return 0;
  1072. /* no cluster to truncate */
  1073. if (p_chain->size == 0) {
  1074. DMSG("%s: cluster(%u) truncation is not required.",
  1075. __func__, p_chain->dir);
  1076. return 0;
  1077. }
  1078. /* check cluster validation */
  1079. if (!is_valid_clus(fsi, p_chain->dir)) {
  1080. EMSG("%s: invalid start cluster (%u)\n", __func__, p_chain->dir);
  1081. sdfat_debug_bug_on(1);
  1082. return -EIO;
  1083. }
  1084. set_sb_dirty(sb);
  1085. clu = p_chain->dir;
  1086. if (p_chain->flags == 0x03) {
  1087. do {
  1088. if (do_relse) {
  1089. sector = CLUS_TO_SECT(fsi, clu);
  1090. for (i = 0; i < fsi->sect_per_clus; i++) {
  1091. if (dcache_release(sb, sector+i) == -EIO)
  1092. goto out;
  1093. }
  1094. }
  1095. if (clr_alloc_bitmap(sb, clu-2))
  1096. goto out;
  1097. clu++;
  1098. num_clusters++;
  1099. } while (num_clusters < p_chain->size);
  1100. } else {
  1101. do {
  1102. if (do_relse) {
  1103. sector = CLUS_TO_SECT(fsi, clu);
  1104. for (i = 0; i < fsi->sect_per_clus; i++) {
  1105. if (dcache_release(sb, sector+i) == -EIO)
  1106. goto out;
  1107. }
  1108. }
  1109. if (clr_alloc_bitmap(sb, (clu - CLUS_BASE)))
  1110. goto out;
  1111. if (get_next_clus_safe(sb, &clu))
  1112. goto out;
  1113. num_clusters++;
  1114. } while (!IS_CLUS_EOF(clu));
  1115. }
  1116. /* success */
  1117. ret = 0;
  1118. out:
  1119. fsi->used_clusters -= num_clusters;
  1120. return ret;
  1121. } /* end of exfat_free_cluster */
  1122. static s32 exfat_alloc_cluster(struct super_block *sb, u32 num_alloc, CHAIN_T *p_chain, s32 dest)
  1123. {
  1124. s32 ret = -ENOSPC;
  1125. u32 num_clusters = 0, total_cnt;
  1126. u32 hint_clu, new_clu, last_clu = CLUS_EOF;
  1127. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  1128. total_cnt = fsi->num_clusters - CLUS_BASE;
  1129. if (unlikely(total_cnt < fsi->used_clusters)) {
  1130. sdfat_fs_error_ratelimit(sb,
  1131. "%s: invalid used clusters(t:%u,u:%u)\n",
  1132. __func__, total_cnt, fsi->used_clusters);
  1133. return -EIO;
  1134. }
  1135. if (num_alloc > total_cnt - fsi->used_clusters)
  1136. return -ENOSPC;
  1137. hint_clu = p_chain->dir;
  1138. /* find new cluster */
  1139. if (IS_CLUS_EOF(hint_clu)) {
  1140. if (fsi->clu_srch_ptr < CLUS_BASE) {
  1141. EMSG("%s: fsi->clu_srch_ptr is invalid (%u)\n",
  1142. __func__, fsi->clu_srch_ptr);
  1143. ASSERT(0);
  1144. fsi->clu_srch_ptr = CLUS_BASE;
  1145. }
  1146. hint_clu = test_alloc_bitmap(sb, fsi->clu_srch_ptr - CLUS_BASE);
  1147. if (IS_CLUS_EOF(hint_clu))
  1148. return -ENOSPC;
  1149. }
  1150. /* check cluster validation */
  1151. if (!is_valid_clus(fsi, hint_clu)) {
  1152. /* "last + 1" can be passed as hint_clu. Otherwise, bug_on */
  1153. if (hint_clu != fsi->num_clusters) {
  1154. EMSG("%s: hint_cluster is invalid (%u)\n",
  1155. __func__, hint_clu);
  1156. sdfat_debug_bug_on(1);
  1157. }
  1158. hint_clu = CLUS_BASE;
  1159. if (p_chain->flags == 0x03) {
  1160. if (exfat_chain_cont_cluster(sb, p_chain->dir, num_clusters))
  1161. return -EIO;
  1162. p_chain->flags = 0x01;
  1163. }
  1164. }
  1165. set_sb_dirty(sb);
  1166. p_chain->dir = CLUS_EOF;
  1167. while ((new_clu = test_alloc_bitmap(sb, hint_clu - CLUS_BASE)) != CLUS_EOF) {
  1168. if ((new_clu != hint_clu) && (p_chain->flags == 0x03)) {
  1169. if (exfat_chain_cont_cluster(sb, p_chain->dir, num_clusters)) {
  1170. ret = -EIO;
  1171. goto error;
  1172. }
  1173. p_chain->flags = 0x01;
  1174. }
  1175. /* update allocation bitmap */
  1176. if (set_alloc_bitmap(sb, new_clu - CLUS_BASE)) {
  1177. ret = -EIO;
  1178. goto error;
  1179. }
  1180. num_clusters++;
  1181. /* update FAT table */
  1182. if (p_chain->flags == 0x01) {
  1183. if (fat_ent_set(sb, new_clu, CLUS_EOF)) {
  1184. ret = -EIO;
  1185. goto error;
  1186. }
  1187. }
  1188. if (IS_CLUS_EOF(p_chain->dir)) {
  1189. p_chain->dir = new_clu;
  1190. } else if (p_chain->flags == 0x01) {
  1191. if (fat_ent_set(sb, last_clu, new_clu)) {
  1192. ret = -EIO;
  1193. goto error;
  1194. }
  1195. }
  1196. last_clu = new_clu;
  1197. if ((--num_alloc) == 0) {
  1198. fsi->clu_srch_ptr = hint_clu;
  1199. fsi->used_clusters += num_clusters;
  1200. p_chain->size += num_clusters;
  1201. return 0;
  1202. }
  1203. hint_clu = new_clu + 1;
  1204. if (hint_clu >= fsi->num_clusters) {
  1205. hint_clu = CLUS_BASE;
  1206. if (p_chain->flags == 0x03) {
  1207. if (exfat_chain_cont_cluster(sb, p_chain->dir, num_clusters)) {
  1208. ret = -EIO;
  1209. goto error;
  1210. }
  1211. p_chain->flags = 0x01;
  1212. }
  1213. }
  1214. }
  1215. error:
  1216. if (num_clusters)
  1217. exfat_free_cluster(sb, p_chain, 0);
  1218. return ret;
  1219. } /* end of exfat_alloc_cluster */
  1220. static s32 exfat_count_used_clusters(struct super_block *sb, u32 *ret_count)
  1221. {
  1222. u32 count = 0;
  1223. u32 i, map_i, map_b;
  1224. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  1225. u32 total_clus = fsi->num_clusters - 2;
  1226. map_i = map_b = 0;
  1227. for (i = 0; i < total_clus; i += 8) {
  1228. u8 k = *(((u8 *) fsi->vol_amap[map_i]->b_data) + map_b);
  1229. count += used_bit[k];
  1230. if ((++map_b) >= (u32)sb->s_blocksize) {
  1231. map_i++;
  1232. map_b = 0;
  1233. }
  1234. }
  1235. /* FIXME : abnormal bitmap count should be handled as more smart */
  1236. if (total_clus < count)
  1237. count = total_clus;
  1238. *ret_count = count;
  1239. return 0;
  1240. } /* end of exfat_count_used_clusters */
  1241. /*
  1242. * File Operation Functions
  1243. */
  1244. static FS_FUNC_T exfat_fs_func = {
  1245. .alloc_cluster = exfat_alloc_cluster,
  1246. .free_cluster = exfat_free_cluster,
  1247. .count_used_clusters = exfat_count_used_clusters,
  1248. .init_dir_entry = exfat_init_dir_entry,
  1249. .init_ext_entry = exfat_init_ext_entry,
  1250. .find_dir_entry = exfat_find_dir_entry,
  1251. .delete_dir_entry = exfat_delete_dir_entry,
  1252. .get_uniname_from_ext_entry = exfat_get_uniname_from_ext_entry,
  1253. .count_ext_entries = exfat_count_ext_entries,
  1254. .calc_num_entries = exfat_calc_num_entries,
  1255. .check_max_dentries = exfat_check_max_dentries,
  1256. .get_entry_type = exfat_get_entry_type,
  1257. .set_entry_type = exfat_set_entry_type,
  1258. .get_entry_attr = exfat_get_entry_attr,
  1259. .set_entry_attr = exfat_set_entry_attr,
  1260. .get_entry_flag = exfat_get_entry_flag,
  1261. .set_entry_flag = exfat_set_entry_flag,
  1262. .get_entry_clu0 = exfat_get_entry_clu0,
  1263. .set_entry_clu0 = exfat_set_entry_clu0,
  1264. .get_entry_size = exfat_get_entry_size,
  1265. .set_entry_size = exfat_set_entry_size,
  1266. .get_entry_time = exfat_get_entry_time,
  1267. .set_entry_time = exfat_set_entry_time,
  1268. };
  1269. s32 mount_exfat(struct super_block *sb, pbr_t *p_pbr)
  1270. {
  1271. pbr64_t *p_bpb = (pbr64_t *)p_pbr;
  1272. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  1273. fsi->sect_per_clus = 1 << p_bpb->bsx.sect_per_clus_bits;
  1274. fsi->sect_per_clus_bits = p_bpb->bsx.sect_per_clus_bits;
  1275. fsi->cluster_size_bits = fsi->sect_per_clus_bits + sb->s_blocksize_bits;
  1276. fsi->cluster_size = 1 << fsi->cluster_size_bits;
  1277. if (!p_bpb->bsx.num_fats) {
  1278. sdfat_msg(sb, KERN_ERR, "bogus number of FAT structure");
  1279. return -EINVAL;
  1280. }
  1281. if (p_bpb->bsx.num_fats >= 2) {
  1282. sdfat_msg(sb, KERN_WARNING,
  1283. "unsupported number of FAT structure :%u, try with 1",
  1284. p_bpb->bsx.num_fats);
  1285. }
  1286. fsi->num_FAT_sectors = le32_to_cpu(p_bpb->bsx.fat_length);
  1287. if (!fsi->num_FAT_sectors) {
  1288. sdfat_msg(sb, KERN_ERR, "bogus fat size");
  1289. return -EINVAL;
  1290. }
  1291. fsi->FAT1_start_sector = le32_to_cpu(p_bpb->bsx.fat_offset);
  1292. fsi->FAT2_start_sector = fsi->FAT1_start_sector;
  1293. fsi->root_start_sector = le32_to_cpu(p_bpb->bsx.clu_offset);
  1294. fsi->data_start_sector = fsi->root_start_sector;
  1295. fsi->num_sectors = le64_to_cpu(p_bpb->bsx.vol_length);
  1296. if (!fsi->num_sectors) {
  1297. sdfat_msg(sb, KERN_ERR, "bogus number of total sector count");
  1298. return -EINVAL;
  1299. }
  1300. /* because the cluster index starts with 2 */
  1301. fsi->num_clusters = le32_to_cpu(p_bpb->bsx.clu_count) + CLUS_BASE;
  1302. fsi->vol_id = le32_to_cpu(p_bpb->bsx.vol_serial);
  1303. fsi->root_dir = le32_to_cpu(p_bpb->bsx.root_cluster);
  1304. fsi->dentries_in_root = 0;
  1305. fsi->dentries_per_clu = 1 << (fsi->cluster_size_bits - DENTRY_SIZE_BITS);
  1306. fsi->vol_flag = (u32) le16_to_cpu(p_bpb->bsx.vol_flags);
  1307. fsi->clu_srch_ptr = CLUS_BASE;
  1308. fsi->used_clusters = (u32) ~0;
  1309. fsi->fs_func = &exfat_fs_func;
  1310. fsi->vol_type = EXFAT;
  1311. fat_ent_ops_init(sb);
  1312. if (p_bpb->bsx.vol_flags & VOL_DIRTY) {
  1313. fsi->vol_flag |= VOL_DIRTY;
  1314. sdfat_log_msg(sb, KERN_WARNING, "Volume was not properly "
  1315. "unmounted. Some data may be corrupt. "
  1316. "Please run fsck.");
  1317. }
  1318. return 0;
  1319. } /* end of mount_exfat */
  1320. /* end of core_exfat.c */