core.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690
  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.c */
  21. /* PURPOSE : FAT & exFAT common 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/writeback.h>
  32. #include <linux/kernel.h>
  33. #include <linux/log2.h>
  34. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0)
  35. #include <linux/iversion.h>
  36. #endif
  37. #include "sdfat.h"
  38. #include "core.h"
  39. #include <asm/byteorder.h>
  40. #include <asm/unaligned.h>
  41. /*************************************************************************
  42. * FUNCTIONS WHICH HAS KERNEL VERSION DEPENDENCY
  43. *************************************************************************/
  44. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)
  45. static inline u64 inode_peek_iversion(struct inode *inode)
  46. {
  47. return inode->i_version;
  48. }
  49. #endif
  50. /*----------------------------------------------------------------------*/
  51. /* Constant & Macro Definitions */
  52. /*----------------------------------------------------------------------*/
  53. static inline void __set_sb_dirty(struct super_block *sb)
  54. {
  55. #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
  56. sb->s_dirt = 1;
  57. #else /* LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0) */
  58. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  59. sbi->s_dirt = 1;
  60. /* Insert work */
  61. spin_lock(&sbi->work_lock);
  62. if (!sbi->write_super_queued) {
  63. unsigned long delay;
  64. delay = msecs_to_jiffies(CONFIG_SDFAT_WRITE_SB_INTERVAL_CSECS * 10);
  65. queue_delayed_work(system_long_wq, &sbi->write_super_work, delay);
  66. sbi->write_super_queued = 1;
  67. }
  68. spin_unlock(&sbi->work_lock);
  69. #endif
  70. }
  71. void set_sb_dirty(struct super_block *sb)
  72. {
  73. __set_sb_dirty(sb);
  74. // XXX: to be removed later, prints too much output
  75. //TMSG("%s finished.\n", __func__);
  76. }
  77. /*----------------------------------------------------------------------*/
  78. /* Global Variable Definitions */
  79. /*----------------------------------------------------------------------*/
  80. /*----------------------------------------------------------------------*/
  81. /* Local Variable Definitions */
  82. /*----------------------------------------------------------------------*/
  83. static s8 *reserved_names[] = {
  84. "AUX ", "CON ", "NUL ", "PRN ",
  85. "COM1 ", "COM2 ", "COM3 ", "COM4 ",
  86. "COM5 ", "COM6 ", "COM7 ", "COM8 ", "COM9 ",
  87. "LPT1 ", "LPT2 ", "LPT3 ", "LPT4 ",
  88. "LPT5 ", "LPT6 ", "LPT7 ", "LPT8 ", "LPT9 ",
  89. NULL
  90. };
  91. /*======================================================================*/
  92. /* Local Function Definitions */
  93. /*======================================================================*/
  94. /*
  95. * File System Management Functions
  96. */
  97. static s32 check_type_size(void)
  98. {
  99. /* critical check for system requirement on size of DENTRY_T structure */
  100. if (sizeof(DENTRY_T) != DENTRY_SIZE)
  101. return -EINVAL;
  102. if (sizeof(DOS_DENTRY_T) != DENTRY_SIZE)
  103. return -EINVAL;
  104. if (sizeof(EXT_DENTRY_T) != DENTRY_SIZE)
  105. return -EINVAL;
  106. if (sizeof(FILE_DENTRY_T) != DENTRY_SIZE)
  107. return -EINVAL;
  108. if (sizeof(STRM_DENTRY_T) != DENTRY_SIZE)
  109. return -EINVAL;
  110. if (sizeof(NAME_DENTRY_T) != DENTRY_SIZE)
  111. return -EINVAL;
  112. if (sizeof(BMAP_DENTRY_T) != DENTRY_SIZE)
  113. return -EINVAL;
  114. if (sizeof(CASE_DENTRY_T) != DENTRY_SIZE)
  115. return -EINVAL;
  116. if (sizeof(VOLM_DENTRY_T) != DENTRY_SIZE)
  117. return -EINVAL;
  118. return 0;
  119. }
  120. static s32 __fs_set_vol_flags(struct super_block *sb, u16 new_flag, s32 always_sync)
  121. {
  122. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  123. s32 err;
  124. s32 sync = 0;
  125. /* flags are not changed */
  126. if (fsi->vol_flag == new_flag)
  127. return 0;
  128. fsi->vol_flag = new_flag;
  129. /* skip updating volume dirty flag,
  130. * if this volume has been mounted with read-only
  131. */
  132. if (sb_rdonly(sb))
  133. return 0;
  134. if (!fsi->pbr_bh) {
  135. err = read_sect(sb, 0, &(fsi->pbr_bh), 1);
  136. if (err) {
  137. EMSG("%s : failed to read boot sector\n", __func__);
  138. return err;
  139. }
  140. }
  141. if (fsi->vol_type == EXFAT) {
  142. pbr64_t *bpb = (pbr64_t *)fsi->pbr_bh->b_data;
  143. bpb->bsx.vol_flags = cpu_to_le16(new_flag);
  144. } else if (fsi->vol_type == FAT32) {
  145. pbr32_t *bpb = (pbr32_t *)fsi->pbr_bh->b_data;
  146. bpb->bsx.state = new_flag & VOL_DIRTY ? FAT_VOL_DIRTY : 0x00;
  147. } else { /* FAT16/12 */
  148. pbr16_t *bpb = (pbr16_t *) fsi->pbr_bh->b_data;
  149. bpb->bpb.f16.state = new_flag & VOL_DIRTY ?
  150. FAT_VOL_DIRTY : 0x00;
  151. }
  152. if (always_sync)
  153. sync = 1;
  154. else if ((new_flag == VOL_DIRTY) && (!buffer_dirty(fsi->pbr_bh)))
  155. sync = 1;
  156. else
  157. sync = 0;
  158. err = write_sect(sb, 0, fsi->pbr_bh, sync);
  159. if (err)
  160. EMSG("%s : failed to modify volume flag\n", __func__);
  161. return err;
  162. }
  163. static s32 fs_set_vol_flags(struct super_block *sb, u16 new_flag)
  164. {
  165. return __fs_set_vol_flags(sb, new_flag, 0);
  166. }
  167. s32 fscore_set_vol_flags(struct super_block *sb, u16 new_flag, s32 always_sync)
  168. {
  169. return __fs_set_vol_flags(sb, new_flag, always_sync);
  170. }
  171. static inline s32 __fs_meta_sync(struct super_block *sb, s32 do_sync)
  172. {
  173. #ifdef CONFIG_SDFAT_DELAYED_META_DIRTY
  174. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  175. if (fsi->vol_type != EXFAT) {
  176. MMSG("meta flush in fs_sync(sync=%d)\n", do_sync);
  177. fcache_flush(sb, 0);
  178. dcache_flush(sb, 0);
  179. }
  180. #else
  181. /* DO NOTHING */
  182. #endif
  183. return 0;
  184. }
  185. static s32 fs_sync(struct super_block *sb, s32 do_sync)
  186. {
  187. s32 err;
  188. if (!do_sync)
  189. return 0;
  190. err = __fs_meta_sync(sb, do_sync);
  191. if (!err)
  192. err = bdev_sync_all(sb);
  193. if (err)
  194. EMSG("%s : failed to sync. (err:%d)\n", __func__, err);
  195. return err;
  196. }
  197. /*
  198. * Cluster Management Functions
  199. */
  200. static s32 __clear_cluster(struct inode *inode, u32 clu)
  201. {
  202. u64 s, n;
  203. struct super_block *sb = inode->i_sb;
  204. u32 sect_size = (u32)sb->s_blocksize;
  205. s32 ret = 0;
  206. struct buffer_head *tmp_bh = NULL;
  207. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  208. if (IS_CLUS_FREE(clu)) { /* FAT16 root_dir */
  209. s = fsi->root_start_sector;
  210. n = fsi->data_start_sector;
  211. } else {
  212. s = CLUS_TO_SECT(fsi, clu);
  213. n = s + fsi->sect_per_clus;
  214. }
  215. if (IS_DIRSYNC(inode)) {
  216. ret = write_msect_zero(sb, s, (u64)fsi->sect_per_clus);
  217. if (ret != -EAGAIN)
  218. return ret;
  219. }
  220. /* Trying buffered zero writes
  221. * if it doesn't have DIRSYNC or write_msect_zero() returned -EAGAIN
  222. */
  223. for ( ; s < n; s++) {
  224. #if 0
  225. dcache_release(sb, s);
  226. #endif
  227. ret = read_sect(sb, s, &tmp_bh, 0);
  228. if (ret)
  229. goto out;
  230. memset((u8 *)tmp_bh->b_data, 0x0, sect_size);
  231. ret = write_sect(sb, s, tmp_bh, 0);
  232. if (ret)
  233. goto out;
  234. }
  235. out:
  236. brelse(tmp_bh);
  237. return ret;
  238. } /* end of __clear_cluster */
  239. static s32 __find_last_cluster(struct super_block *sb, CHAIN_T *p_chain, u32 *ret_clu)
  240. {
  241. u32 clu, next;
  242. u32 count = 0;
  243. next = p_chain->dir;
  244. if (p_chain->flags == 0x03) {
  245. *ret_clu = next + p_chain->size - 1;
  246. return 0;
  247. }
  248. do {
  249. count++;
  250. clu = next;
  251. if (fat_ent_get_safe(sb, clu, &next))
  252. return -EIO;
  253. } while (!IS_CLUS_EOF(next));
  254. if (p_chain->size != count) {
  255. sdfat_fs_error(sb, "bogus directory size "
  256. "(clus : ondisk(%d) != counted(%d))",
  257. p_chain->size, count);
  258. sdfat_debug_bug_on(1);
  259. return -EIO;
  260. }
  261. *ret_clu = clu;
  262. return 0;
  263. }
  264. static s32 __count_num_clusters(struct super_block *sb, CHAIN_T *p_chain, u32 *ret_count)
  265. {
  266. u32 i, count;
  267. u32 clu;
  268. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  269. if (!p_chain->dir || IS_CLUS_EOF(p_chain->dir)) {
  270. *ret_count = 0;
  271. return 0;
  272. }
  273. if (p_chain->flags == 0x03) {
  274. *ret_count = p_chain->size;
  275. return 0;
  276. }
  277. clu = p_chain->dir;
  278. count = 0;
  279. for (i = CLUS_BASE; i < fsi->num_clusters; i++) {
  280. count++;
  281. if (fat_ent_get_safe(sb, clu, &clu))
  282. return -EIO;
  283. if (IS_CLUS_EOF(clu))
  284. break;
  285. }
  286. *ret_count = count;
  287. return 0;
  288. }
  289. /*
  290. * Upcase table Management Functions
  291. */
  292. static void free_upcase_table(struct super_block *sb)
  293. {
  294. u32 i;
  295. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  296. u16 **upcase_table;
  297. upcase_table = fsi->vol_utbl;
  298. for (i = 0 ; i < UTBL_COL_COUNT ; i++) {
  299. /* kfree(NULL) is safe */
  300. kfree(upcase_table[i]);
  301. upcase_table[i] = NULL;
  302. }
  303. /* kfree(NULL) is safe */
  304. kfree(fsi->vol_utbl);
  305. fsi->vol_utbl = NULL;
  306. }
  307. static s32 __load_upcase_table(struct super_block *sb, u64 sector, u64 num_sectors, u32 utbl_checksum)
  308. {
  309. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  310. struct buffer_head *tmp_bh = NULL;
  311. u32 sect_size = (u32)sb->s_blocksize;
  312. s32 ret = -EIO;
  313. u32 i, j;
  314. u8 skip = false;
  315. u32 index = 0;
  316. u32 checksum = 0;
  317. u16 **upcase_table = kzalloc((UTBL_COL_COUNT * sizeof(u16 *)), GFP_KERNEL);
  318. if (!upcase_table)
  319. return -ENOMEM;
  320. /* thanks for kzalloc
  321. * memset(upcase_table, 0, UTBL_COL_COUNT * sizeof(u16 *));
  322. */
  323. fsi->vol_utbl = upcase_table;
  324. num_sectors += sector;
  325. while (sector < num_sectors) {
  326. ret = read_sect(sb, sector, &tmp_bh, 1);
  327. if (ret) {
  328. EMSG("%s: failed to read sector(0x%llx)\n",
  329. __func__, sector);
  330. goto error;
  331. }
  332. sector++;
  333. for (i = 0; i < sect_size && index <= 0xFFFF; i += 2) {
  334. /* FIXME : is __le16 ok? */
  335. //u16 uni = le16_to_cpu(((__le16*)(tmp_bh->b_data))[i]);
  336. u16 uni = get_unaligned_le16((u8 *)tmp_bh->b_data+i);
  337. checksum = ((checksum & 1) ? 0x80000000 : 0) +
  338. (checksum >> 1) + *(((u8 *)tmp_bh->b_data)+i);
  339. checksum = ((checksum & 1) ? 0x80000000 : 0) +
  340. (checksum >> 1) + *(((u8 *)tmp_bh->b_data)+(i+1));
  341. if (skip) {
  342. MMSG("skip from 0x%X to 0x%X(amount of 0x%X)\n",
  343. index, index+uni, uni);
  344. index += uni;
  345. skip = false;
  346. } else if (uni == index) {
  347. index++;
  348. } else if (uni == 0xFFFF) {
  349. skip = true;
  350. } else { /* uni != index , uni != 0xFFFF */
  351. u16 col_index = get_col_index(index);
  352. if (!upcase_table[col_index]) {
  353. upcase_table[col_index] =
  354. kmalloc((UTBL_ROW_COUNT * sizeof(u16)), GFP_KERNEL);
  355. if (!upcase_table[col_index]) {
  356. EMSG("failed to allocate memory"
  357. " for column 0x%X\n",
  358. col_index);
  359. ret = -ENOMEM;
  360. goto error;
  361. }
  362. for (j = 0; j < UTBL_ROW_COUNT; j++)
  363. upcase_table[col_index][j] = (col_index << LOW_INDEX_BIT) | j;
  364. }
  365. upcase_table[col_index][get_row_index(index)] = uni;
  366. index++;
  367. }
  368. }
  369. }
  370. if (index >= 0xFFFF && utbl_checksum == checksum) {
  371. DMSG("%s: load upcase table successfully"
  372. "(idx:0x%08x, utbl_chksum:0x%08x)\n",
  373. __func__, index, utbl_checksum);
  374. if (tmp_bh)
  375. brelse(tmp_bh);
  376. return 0;
  377. }
  378. EMSG("%s: failed to load upcase table"
  379. "(idx:0x%08x, chksum:0x%08x, utbl_chksum:0x%08x)\n",
  380. __func__, index, checksum, utbl_checksum);
  381. ret = -EINVAL;
  382. error:
  383. if (tmp_bh)
  384. brelse(tmp_bh);
  385. free_upcase_table(sb);
  386. return ret;
  387. }
  388. static s32 __load_default_upcase_table(struct super_block *sb)
  389. {
  390. s32 i, ret = -EIO;
  391. u32 j;
  392. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  393. u8 skip = false;
  394. u32 index = 0;
  395. u16 uni = 0;
  396. u16 **upcase_table;
  397. upcase_table = kmalloc((UTBL_COL_COUNT * sizeof(u16 *)), GFP_KERNEL);
  398. if (!upcase_table)
  399. return -ENOMEM;
  400. fsi->vol_utbl = upcase_table;
  401. memset(upcase_table, 0, UTBL_COL_COUNT * sizeof(u16 *));
  402. for (i = 0; index <= 0xFFFF && i < SDFAT_NUM_UPCASE*2; i += 2) {
  403. /* FIXME : is __le16 ok? */
  404. //uni = le16_to_cpu(((__le16*)uni_def_upcase)[i>>1]);
  405. uni = get_unaligned_le16((u8 *)uni_def_upcase+i);
  406. if (skip) {
  407. MMSG("skip from 0x%x ", index);
  408. index += uni;
  409. MMSG("to 0x%x (amount of 0x%x)\n", index, uni);
  410. skip = false;
  411. } else if (uni == index) {
  412. index++;
  413. } else if (uni == 0xFFFF) {
  414. skip = true;
  415. } else { /* uni != index , uni != 0xFFFF */
  416. u16 col_index = get_col_index(index);
  417. if (!upcase_table[col_index]) {
  418. upcase_table[col_index] = kmalloc((UTBL_ROW_COUNT * sizeof(u16)), GFP_KERNEL);
  419. if (!upcase_table[col_index]) {
  420. EMSG("failed to allocate memory for "
  421. "new column 0x%x\n", col_index);
  422. ret = -ENOMEM;
  423. goto error;
  424. }
  425. for (j = 0; j < UTBL_ROW_COUNT; j++)
  426. upcase_table[col_index][j] = (col_index << LOW_INDEX_BIT) | j;
  427. }
  428. upcase_table[col_index][get_row_index(index)] = uni;
  429. index++;
  430. }
  431. }
  432. if (index >= 0xFFFF)
  433. return 0;
  434. error:
  435. /* FATAL error: default upcase table has error */
  436. free_upcase_table(sb);
  437. return ret;
  438. }
  439. static s32 load_upcase_table(struct super_block *sb)
  440. {
  441. s32 i, ret;
  442. u32 tbl_clu, type;
  443. u64 sector, tbl_size, num_sectors;
  444. u8 blksize_bits = sb->s_blocksize_bits;
  445. CHAIN_T clu;
  446. CASE_DENTRY_T *ep;
  447. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  448. clu.dir = fsi->root_dir;
  449. clu.flags = 0x01;
  450. if (fsi->vol_type != EXFAT)
  451. goto load_default;
  452. while (!IS_CLUS_EOF(clu.dir)) {
  453. for (i = 0; i < fsi->dentries_per_clu; i++) {
  454. ep = (CASE_DENTRY_T *) get_dentry_in_dir(sb, &clu, i, NULL);
  455. if (!ep)
  456. return -EIO;
  457. type = fsi->fs_func->get_entry_type((DENTRY_T *) ep);
  458. if (type == TYPE_UNUSED)
  459. break;
  460. if (type != TYPE_UPCASE)
  461. continue;
  462. tbl_clu = le32_to_cpu(ep->start_clu);
  463. tbl_size = le64_to_cpu(ep->size);
  464. sector = CLUS_TO_SECT(fsi, tbl_clu);
  465. num_sectors = ((tbl_size - 1) >> blksize_bits) + 1;
  466. ret = __load_upcase_table(sb, sector, num_sectors,
  467. le32_to_cpu(ep->checksum));
  468. if (ret && (ret != -EIO))
  469. goto load_default;
  470. /* load successfully */
  471. return ret;
  472. }
  473. if (get_next_clus_safe(sb, &(clu.dir)))
  474. return -EIO;
  475. }
  476. load_default:
  477. sdfat_log_msg(sb, KERN_INFO, "trying to load default upcase table");
  478. /* load default upcase table */
  479. return __load_default_upcase_table(sb);
  480. } /* end of load_upcase_table */
  481. /*
  482. * Directory Entry Management Functions
  483. */
  484. s32 walk_fat_chain(struct super_block *sb, CHAIN_T *p_dir, u32 byte_offset, u32 *clu)
  485. {
  486. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  487. u32 clu_offset;
  488. u32 cur_clu;
  489. clu_offset = byte_offset >> fsi->cluster_size_bits;
  490. cur_clu = p_dir->dir;
  491. if (p_dir->flags == 0x03) {
  492. cur_clu += clu_offset;
  493. } else {
  494. while (clu_offset > 0) {
  495. if (get_next_clus_safe(sb, &cur_clu))
  496. return -EIO;
  497. if (IS_CLUS_EOF(cur_clu)) {
  498. sdfat_fs_error(sb, "invalid dentry access "
  499. "beyond EOF (clu : %u, eidx : %d)",
  500. p_dir->dir,
  501. byte_offset >> DENTRY_SIZE_BITS);
  502. return -EIO;
  503. }
  504. clu_offset--;
  505. }
  506. }
  507. if (clu)
  508. *clu = cur_clu;
  509. return 0;
  510. }
  511. static s32 find_location(struct super_block *sb, CHAIN_T *p_dir, s32 entry, u64 *sector, s32 *offset)
  512. {
  513. s32 ret;
  514. u32 off, clu = 0;
  515. u32 blksize_mask = (u32)(sb->s_blocksize-1);
  516. u8 blksize_bits = sb->s_blocksize_bits;
  517. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  518. off = entry << DENTRY_SIZE_BITS;
  519. /* FAT16 root_dir */
  520. if (IS_CLUS_FREE(p_dir->dir)) {
  521. *offset = off & blksize_mask;
  522. *sector = off >> blksize_bits;
  523. *sector += fsi->root_start_sector;
  524. return 0;
  525. }
  526. ret = walk_fat_chain(sb, p_dir, off, &clu);
  527. if (ret)
  528. return ret;
  529. /* byte offset in cluster */
  530. off &= (fsi->cluster_size - 1);
  531. /* byte offset in sector */
  532. *offset = off & blksize_mask;
  533. /* sector offset in cluster */
  534. *sector = off >> blksize_bits;
  535. *sector += CLUS_TO_SECT(fsi, clu);
  536. return 0;
  537. } /* end of find_location */
  538. DENTRY_T *get_dentry_in_dir(struct super_block *sb, CHAIN_T *p_dir, s32 entry, u64 *sector)
  539. {
  540. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  541. u32 dentries_per_page = PAGE_SIZE >> DENTRY_SIZE_BITS;
  542. s32 off;
  543. u64 sec;
  544. u8 *buf;
  545. if (p_dir->dir == DIR_DELETED) {
  546. EMSG("%s : abnormal access to deleted dentry\n", __func__);
  547. BUG_ON(!fsi->prev_eio);
  548. return NULL;
  549. }
  550. if (find_location(sb, p_dir, entry, &sec, &off))
  551. return NULL;
  552. /* DIRECTORY READAHEAD :
  553. * Try to read ahead per a page except root directory of fat12/16
  554. */
  555. if ((!IS_CLUS_FREE(p_dir->dir)) &&
  556. !(entry & (dentries_per_page - 1)))
  557. dcache_readahead(sb, sec);
  558. buf = dcache_getblk(sb, sec);
  559. if (!buf)
  560. return NULL;
  561. if (sector)
  562. *sector = sec;
  563. return (DENTRY_T *)(buf + off);
  564. } /* end of get_dentry_in_dir */
  565. /* used only in search empty_slot() */
  566. #define CNT_UNUSED_NOHIT (-1)
  567. #define CNT_UNUSED_HIT (-2)
  568. /* search EMPTY CONTINUOUS "num_entries" entries */
  569. static s32 search_empty_slot(struct super_block *sb, HINT_FEMP_T *hint_femp, CHAIN_T *p_dir, s32 num_entries)
  570. {
  571. s32 i, dentry, num_empty = 0;
  572. s32 dentries_per_clu;
  573. u32 type;
  574. CHAIN_T clu;
  575. DENTRY_T *ep;
  576. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  577. if (IS_CLUS_FREE(p_dir->dir)) /* FAT16 root_dir */
  578. dentries_per_clu = fsi->dentries_in_root;
  579. else
  580. dentries_per_clu = fsi->dentries_per_clu;
  581. ASSERT(-1 <= hint_femp->eidx);
  582. if (hint_femp->eidx != -1) {
  583. clu.dir = hint_femp->cur.dir;
  584. clu.size = hint_femp->cur.size;
  585. clu.flags = hint_femp->cur.flags;
  586. dentry = hint_femp->eidx;
  587. if (num_entries <= hint_femp->count) {
  588. MMSG("%s: empty slot(HIT) - found "
  589. "(clu : 0x%08x eidx : %d)\n",
  590. __func__, hint_femp->cur.dir, hint_femp->eidx);
  591. hint_femp->eidx = -1;
  592. if (fsi->vol_type == EXFAT)
  593. return dentry;
  594. return dentry + (num_entries - 1);
  595. }
  596. MMSG("%s: empty slot(HIT) - search from "
  597. "(clu : 0x%08x eidx : %d)\n",
  598. __func__, hint_femp->cur.dir, hint_femp->eidx);
  599. } else {
  600. MMSG("%s: empty slot(MISS) - search from "
  601. "(clu:0x%08x eidx : 0)\n",
  602. __func__, p_dir->dir);
  603. clu.dir = p_dir->dir;
  604. clu.size = p_dir->size;
  605. clu.flags = p_dir->flags;
  606. dentry = 0;
  607. }
  608. while (!IS_CLUS_EOF(clu.dir)) {
  609. /* FAT16 root_dir */
  610. if (IS_CLUS_FREE(p_dir->dir))
  611. i = dentry % dentries_per_clu;
  612. else
  613. i = dentry & (dentries_per_clu-1);
  614. for ( ; i < dentries_per_clu; i++, dentry++) {
  615. ep = get_dentry_in_dir(sb, &clu, i, NULL);
  616. if (!ep)
  617. return -EIO;
  618. type = fsi->fs_func->get_entry_type(ep);
  619. if ((type == TYPE_UNUSED) || (type == TYPE_DELETED)) {
  620. num_empty++;
  621. if (hint_femp->eidx == -1) {
  622. hint_femp->eidx = dentry;
  623. hint_femp->count = CNT_UNUSED_NOHIT;
  624. hint_femp->cur.dir = clu.dir;
  625. hint_femp->cur.size = clu.size;
  626. hint_femp->cur.flags = clu.flags;
  627. }
  628. if ((type == TYPE_UNUSED) &&
  629. (hint_femp->count != CNT_UNUSED_HIT)) {
  630. hint_femp->count = CNT_UNUSED_HIT;
  631. }
  632. } else {
  633. if ((hint_femp->eidx != -1) &&
  634. (hint_femp->count == CNT_UNUSED_HIT)) {
  635. /* unused empty group means
  636. * an empty group which includes
  637. * unused dentry
  638. */
  639. sdfat_fs_error(sb,
  640. "found bogus dentry(%d) "
  641. "beyond unused empty group(%d) "
  642. "(start_clu : %u, cur_clu : %u)",
  643. dentry, hint_femp->eidx, p_dir->dir,
  644. clu.dir);
  645. return -EIO;
  646. }
  647. num_empty = 0;
  648. hint_femp->eidx = -1;
  649. }
  650. if (num_empty >= num_entries) {
  651. /* found and invalidate hint_femp */
  652. hint_femp->eidx = -1;
  653. if (fsi->vol_type == EXFAT)
  654. return (dentry - (num_entries-1));
  655. return dentry;
  656. }
  657. }
  658. if (IS_CLUS_FREE(p_dir->dir))
  659. break; /* FAT16 root_dir */
  660. if (clu.flags == 0x03) {
  661. if ((--clu.size) > 0)
  662. clu.dir++;
  663. else
  664. clu.dir = CLUS_EOF;
  665. } else {
  666. if (get_next_clus_safe(sb, &(clu.dir)))
  667. return -EIO;
  668. }
  669. }
  670. return -ENOSPC;
  671. } /* end of search_empty_slot */
  672. /* find empty directory entry.
  673. * if there isn't any empty slot, expand cluster chain.
  674. */
  675. static s32 find_empty_entry(struct inode *inode, CHAIN_T *p_dir, s32 num_entries)
  676. {
  677. s32 dentry;
  678. u32 ret, last_clu;
  679. u64 sector;
  680. u64 size = 0;
  681. CHAIN_T clu;
  682. DENTRY_T *ep = NULL;
  683. struct super_block *sb = inode->i_sb;
  684. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  685. FILE_ID_T *fid = &(SDFAT_I(inode)->fid);
  686. HINT_FEMP_T hint_femp;
  687. hint_femp.eidx = -1;
  688. ASSERT(-1 <= fid->hint_femp.eidx);
  689. if (fid->hint_femp.eidx != -1) {
  690. memcpy(&hint_femp, &fid->hint_femp, sizeof(HINT_FEMP_T));
  691. fid->hint_femp.eidx = -1;
  692. }
  693. /* FAT16 root_dir */
  694. if (IS_CLUS_FREE(p_dir->dir))
  695. return search_empty_slot(sb, &hint_femp, p_dir, num_entries);
  696. while ((dentry = search_empty_slot(sb, &hint_femp, p_dir, num_entries)) < 0) {
  697. if (dentry == -EIO)
  698. break;
  699. if (fsi->fs_func->check_max_dentries(fid))
  700. return -ENOSPC;
  701. /* we trust p_dir->size regardless of FAT type */
  702. if (__find_last_cluster(sb, p_dir, &last_clu))
  703. return -EIO;
  704. /*
  705. * Allocate new cluster to this directory
  706. */
  707. clu.dir = last_clu + 1;
  708. clu.size = 0; /* UNUSED */
  709. clu.flags = p_dir->flags;
  710. /* (0) check if there are reserved clusters
  711. * (create_dir 의 주석 참고)
  712. */
  713. if (!IS_CLUS_EOF(fsi->used_clusters) &&
  714. ((fsi->used_clusters + fsi->reserved_clusters) >= (fsi->num_clusters - 2)))
  715. return -ENOSPC;
  716. /* (1) allocate a cluster */
  717. ret = fsi->fs_func->alloc_cluster(sb, 1, &clu, ALLOC_HOT);
  718. if (ret)
  719. return ret;
  720. if (__clear_cluster(inode, clu.dir))
  721. return -EIO;
  722. /* (2) append to the FAT chain */
  723. if (clu.flags != p_dir->flags) {
  724. /* no-fat-chain bit is disabled,
  725. * so fat-chain should be synced with alloc-bmp
  726. */
  727. chain_cont_cluster(sb, p_dir->dir, p_dir->size);
  728. p_dir->flags = 0x01;
  729. hint_femp.cur.flags = 0x01;
  730. }
  731. if (clu.flags == 0x01)
  732. if (fat_ent_set(sb, last_clu, clu.dir))
  733. return -EIO;
  734. if (hint_femp.eidx == -1) {
  735. /* the special case that new dentry
  736. * should be allocated from the start of new cluster
  737. */
  738. hint_femp.eidx = (s32)(p_dir->size <<
  739. (fsi->cluster_size_bits - DENTRY_SIZE_BITS));
  740. hint_femp.count = fsi->dentries_per_clu;
  741. hint_femp.cur.dir = clu.dir;
  742. hint_femp.cur.size = 0;
  743. hint_femp.cur.flags = clu.flags;
  744. }
  745. hint_femp.cur.size++;
  746. p_dir->size++;
  747. size = (p_dir->size << fsi->cluster_size_bits);
  748. /* (3) update the directory entry */
  749. if ((fsi->vol_type == EXFAT) && (p_dir->dir != fsi->root_dir)) {
  750. ep = get_dentry_in_dir(sb,
  751. &(fid->dir), fid->entry+1, &sector);
  752. if (!ep)
  753. return -EIO;
  754. fsi->fs_func->set_entry_size(ep, size);
  755. fsi->fs_func->set_entry_flag(ep, p_dir->flags);
  756. if (dcache_modify(sb, sector))
  757. return -EIO;
  758. if (update_dir_chksum(sb, &(fid->dir), fid->entry))
  759. return -EIO;
  760. }
  761. /* directory inode should be updated in here */
  762. i_size_write(inode, (loff_t)size);
  763. SDFAT_I(inode)->i_size_ondisk += fsi->cluster_size;
  764. SDFAT_I(inode)->i_size_aligned += fsi->cluster_size;
  765. SDFAT_I(inode)->fid.size = size;
  766. SDFAT_I(inode)->fid.flags = p_dir->flags;
  767. inode->i_blocks += 1 << (fsi->cluster_size_bits - sb->s_blocksize_bits);
  768. }
  769. return dentry;
  770. } /* end of find_empty_entry */
  771. #define SDFAT_MIN_SUBDIR (2)
  772. static const char *dot_name[SDFAT_MIN_SUBDIR] = { DOS_CUR_DIR_NAME, DOS_PAR_DIR_NAME };
  773. static s32 __count_dos_name_entries(struct super_block *sb, CHAIN_T *p_dir, u32 type, u32 *dotcnt)
  774. {
  775. s32 i, count = 0, check_dot = 0;
  776. s32 dentries_per_clu;
  777. u32 entry_type;
  778. CHAIN_T clu;
  779. DENTRY_T *ep;
  780. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  781. if (IS_CLUS_FREE(p_dir->dir)) /* FAT16 root_dir */
  782. dentries_per_clu = fsi->dentries_in_root;
  783. else
  784. dentries_per_clu = fsi->dentries_per_clu;
  785. clu.dir = p_dir->dir;
  786. clu.size = p_dir->size;
  787. clu.flags = p_dir->flags;
  788. if (dotcnt) {
  789. *dotcnt = 0;
  790. if (fsi->vol_type != EXFAT)
  791. check_dot = 1;
  792. }
  793. while (!IS_CLUS_EOF(clu.dir)) {
  794. for (i = 0; i < dentries_per_clu; i++) {
  795. ep = get_dentry_in_dir(sb, &clu, i, NULL);
  796. if (!ep)
  797. return -EIO;
  798. entry_type = fsi->fs_func->get_entry_type(ep);
  799. if (entry_type == TYPE_UNUSED)
  800. return count;
  801. if (!(type & TYPE_CRITICAL_PRI) && !(type & TYPE_BENIGN_PRI))
  802. continue;
  803. if ((type != TYPE_ALL) && (type != entry_type))
  804. continue;
  805. count++;
  806. if (check_dot && (i < SDFAT_MIN_SUBDIR)) {
  807. BUG_ON(fsi->vol_type == EXFAT);
  808. /* 11 is DOS_NAME_LENGTH */
  809. if (!strncmp(ep->dummy, dot_name[i], 11))
  810. (*dotcnt)++;
  811. }
  812. }
  813. /* FAT16 root_dir */
  814. if (IS_CLUS_FREE(p_dir->dir))
  815. break;
  816. if (clu.flags == 0x03) {
  817. if ((--clu.size) > 0)
  818. clu.dir++;
  819. else
  820. clu.dir = CLUS_EOF;
  821. } else {
  822. if (get_next_clus_safe(sb, &(clu.dir)))
  823. return -EIO;
  824. }
  825. check_dot = 0;
  826. }
  827. return count;
  828. }
  829. s32 check_dir_empty(struct super_block *sb, CHAIN_T *p_dir)
  830. {
  831. s32 i, count = 0;
  832. s32 dentries_per_clu;
  833. u32 type;
  834. CHAIN_T clu;
  835. DENTRY_T *ep;
  836. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  837. if (IS_CLUS_FREE(p_dir->dir)) /* FAT16 root_dir */
  838. dentries_per_clu = fsi->dentries_in_root;
  839. else
  840. dentries_per_clu = fsi->dentries_per_clu;
  841. clu.dir = p_dir->dir;
  842. clu.size = p_dir->size;
  843. clu.flags = p_dir->flags;
  844. while (!IS_CLUS_EOF(clu.dir)) {
  845. for (i = 0; i < dentries_per_clu; i++) {
  846. ep = get_dentry_in_dir(sb, &clu, i, NULL);
  847. if (!ep)
  848. return -EIO;
  849. type = fsi->fs_func->get_entry_type(ep);
  850. if (type == TYPE_UNUSED)
  851. return 0;
  852. if ((type != TYPE_FILE) && (type != TYPE_DIR))
  853. continue;
  854. /* FAT16 root_dir */
  855. if (IS_CLUS_FREE(p_dir->dir))
  856. return -ENOTEMPTY;
  857. if (fsi->vol_type == EXFAT)
  858. return -ENOTEMPTY;
  859. if ((p_dir->dir == fsi->root_dir) || (++count > 2))
  860. return -ENOTEMPTY;
  861. }
  862. /* FAT16 root_dir */
  863. if (IS_CLUS_FREE(p_dir->dir))
  864. return -ENOTEMPTY;
  865. if (clu.flags == 0x03) {
  866. if ((--clu.size) > 0)
  867. clu.dir++;
  868. else
  869. clu.dir = CLUS_EOF;
  870. } else {
  871. if (get_next_clus_safe(sb, &(clu.dir)))
  872. return -EIO;
  873. }
  874. }
  875. return 0;
  876. }
  877. /*
  878. * Name Conversion Functions
  879. */
  880. #ifdef CONFIG_SDFAT_ALLOW_LOOKUP_LOSSY_SFN
  881. /* over name length only */
  882. #define NEED_INVALIDATE_SFN(x) ((x) & NLS_NAME_OVERLEN)
  883. #else
  884. /* all lossy case */
  885. #define NEED_INVALIDATE_SFN(x) (x)
  886. #endif
  887. /* NOTE :
  888. * We should keep shortname code compatible with v1.0.15 or lower
  889. * So, we try to check ext-only-name at create-mode only.
  890. *
  891. * i.e. '.mtp' ->
  892. * v1.0.15 : ' MTP' with name_case, 0x10
  893. * v1.1.0 : 'MT????~?' with name_case, 0x00 and longname.
  894. */
  895. static inline void preprocess_ext_only_sfn(s32 lookup, u16 first_char, DOS_NAME_T *p_dosname, s32 *lossy)
  896. {
  897. #ifdef CONFIG_SDFAT_RESTRICT_EXT_ONLY_SFN
  898. int i;
  899. /* check ext-only-name at create-mode */
  900. if (*lossy || lookup || (first_char != (u16)'.'))
  901. return;
  902. p_dosname->name_case = 0xFF;
  903. /* move ext-name to base-name */
  904. for (i = 0; i < 3; i++) {
  905. p_dosname->name[i] = p_dosname->name[8+i];
  906. if (p_dosname->name[i] == ' ')
  907. p_dosname->name[i] = '_';
  908. }
  909. /* fill remained space with '_' */
  910. for (i = 3; i < 8; i++)
  911. p_dosname->name[i] = '_';
  912. /* eliminate ext-name */
  913. for (i = 8; i < 11; i++)
  914. p_dosname->name[i] = ' ';
  915. *lossy = NLS_NAME_LOSSY;
  916. #endif /* CONFIG_SDFAT_CAN_CREATE_EXT_ONLY_SFN */
  917. }
  918. /* input : dir, uni_name
  919. * output : num_of_entry, dos_name(format : aaaaaa~1.bbb)
  920. */
  921. static s32 get_num_entries_and_dos_name(struct super_block *sb, CHAIN_T *p_dir,
  922. UNI_NAME_T *p_uniname, s32 *entries,
  923. DOS_NAME_T *p_dosname, s32 lookup)
  924. {
  925. s32 ret, num_entries, lossy = NLS_NAME_NO_LOSSY;
  926. s8 **r;
  927. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  928. /* Init null char. */
  929. p_dosname->name[0] = '\0';
  930. num_entries = fsi->fs_func->calc_num_entries(p_uniname);
  931. if (num_entries == 0)
  932. return -EINVAL;
  933. if (fsi->vol_type == EXFAT)
  934. goto out;
  935. nls_uni16s_to_sfn(sb, p_uniname, p_dosname, &lossy);
  936. preprocess_ext_only_sfn(lookup, p_uniname->name[0], p_dosname, &lossy);
  937. if (!lossy) {
  938. for (r = reserved_names; *r; r++) {
  939. if (!strncmp((void *) p_dosname->name, *r, 8))
  940. return -EINVAL;
  941. }
  942. if (p_dosname->name_case != 0xFF)
  943. num_entries = 1;
  944. } else if (!lookup) {
  945. /* create new dos name */
  946. ret = fat_generate_dos_name_new(sb, p_dir, p_dosname,
  947. num_entries);
  948. if (ret)
  949. return ret;
  950. } else if (NEED_INVALIDATE_SFN(lossy)) {
  951. /* FIXME : We should check num_entries */
  952. p_dosname->name[0] = '\0';
  953. }
  954. if (num_entries > 1)
  955. p_dosname->name_case = 0x0;
  956. out:
  957. *entries = num_entries;
  958. return 0;
  959. } /* end of get_num_entries_and_dos_name */
  960. void get_uniname_from_dos_entry(struct super_block *sb, DOS_DENTRY_T *ep, UNI_NAME_T *p_uniname, u8 mode)
  961. {
  962. DOS_NAME_T dos_name;
  963. if (mode == 0x0)
  964. dos_name.name_case = 0x0;
  965. else
  966. dos_name.name_case = ep->lcase;
  967. memcpy(dos_name.name, ep->name, DOS_NAME_LENGTH);
  968. nls_sfn_to_uni16s(sb, &dos_name, p_uniname);
  969. } /* end of get_uniname_from_dos_entry */
  970. /* returns the length of a struct qstr, ignoring trailing dots */
  971. static inline unsigned int __striptail_len(unsigned int len, const char *name)
  972. {
  973. while (len && name[len - 1] == '.')
  974. len--;
  975. return len;
  976. }
  977. /*
  978. * Name Resolution Functions :
  979. * Zero if it was successful; otherwise nonzero.
  980. */
  981. static s32 __resolve_path(struct inode *inode, const u8 *path, CHAIN_T *p_dir, UNI_NAME_T *p_uniname, int lookup)
  982. {
  983. s32 namelen;
  984. s32 lossy = NLS_NAME_NO_LOSSY;
  985. struct super_block *sb = inode->i_sb;
  986. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  987. FILE_ID_T *fid = &(SDFAT_I(inode)->fid);
  988. /* DOT and DOTDOT are handled by VFS layer */
  989. /* strip all trailing spaces */
  990. /* DO NOTHING : Is needed? */
  991. /* strip all trailing periods */
  992. namelen = __striptail_len(strlen(path), path);
  993. if (!namelen)
  994. return -ENOENT;
  995. /* the limitation of linux? */
  996. if (strlen(path) > (MAX_NAME_LENGTH * MAX_CHARSET_SIZE))
  997. return -ENAMETOOLONG;
  998. /*
  999. * strip all leading spaces :
  1000. * "MS windows 7" supports leading spaces.
  1001. * So we should skip this preprocessing for compatibility.
  1002. */
  1003. /* file name conversion :
  1004. * If lookup case, we allow bad-name for compatibility.
  1005. */
  1006. namelen = nls_vfsname_to_uni16s(sb, path, namelen, p_uniname, &lossy);
  1007. if (namelen < 0)
  1008. return namelen; /* return error value */
  1009. if ((lossy && !lookup) || !namelen)
  1010. return -EINVAL;
  1011. sdfat_debug_bug_on(fid->size != i_size_read(inode));
  1012. // fid->size = i_size_read(inode);
  1013. p_dir->dir = fid->start_clu;
  1014. p_dir->size = (u32)(fid->size >> fsi->cluster_size_bits);
  1015. p_dir->flags = fid->flags;
  1016. return 0;
  1017. }
  1018. static inline s32 resolve_path(struct inode *inode, const u8 *path, CHAIN_T *dir, UNI_NAME_T *uni)
  1019. {
  1020. return __resolve_path(inode, path, dir, uni, 0);
  1021. }
  1022. static inline s32 resolve_path_for_lookup(struct inode *inode, const u8 *path, CHAIN_T *dir, UNI_NAME_T *uni)
  1023. {
  1024. return __resolve_path(inode, path, dir, uni, 1);
  1025. }
  1026. static s32 create_dir(struct inode *inode, CHAIN_T *p_dir, UNI_NAME_T *p_uniname, FILE_ID_T *fid)
  1027. {
  1028. s32 dentry, num_entries;
  1029. u64 ret;
  1030. u64 size;
  1031. CHAIN_T clu;
  1032. DOS_NAME_T dos_name, dot_name;
  1033. struct super_block *sb = inode->i_sb;
  1034. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  1035. ret = get_num_entries_and_dos_name(sb, p_dir, p_uniname, &num_entries, &dos_name, 0);
  1036. if (ret)
  1037. return ret;
  1038. /* find_empty_entry must be called before alloc_cluster */
  1039. dentry = find_empty_entry(inode, p_dir, num_entries);
  1040. if (dentry < 0)
  1041. return dentry; /* -EIO or -ENOSPC */
  1042. clu.dir = CLUS_EOF;
  1043. clu.size = 0;
  1044. clu.flags = (fsi->vol_type == EXFAT) ? 0x03 : 0x01;
  1045. /* (0) Check if there are reserved clusters up to max. */
  1046. if ((fsi->used_clusters + fsi->reserved_clusters) >= (fsi->num_clusters - CLUS_BASE))
  1047. return -ENOSPC;
  1048. /* (1) allocate a cluster */
  1049. ret = fsi->fs_func->alloc_cluster(sb, 1, &clu, ALLOC_HOT);
  1050. if (ret)
  1051. return ret;
  1052. ret = __clear_cluster(inode, clu.dir);
  1053. if (ret)
  1054. return ret;
  1055. size = fsi->cluster_size;
  1056. if (fsi->vol_type != EXFAT) {
  1057. /* initialize the . and .. entry
  1058. * Information for . points to itself
  1059. * Information for .. points to parent dir
  1060. */
  1061. dot_name.name_case = 0x0;
  1062. memcpy(dot_name.name, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH);
  1063. ret = fsi->fs_func->init_dir_entry(sb, &clu, 0, TYPE_DIR, clu.dir, 0);
  1064. if (ret)
  1065. return ret;
  1066. ret = fsi->fs_func->init_ext_entry(sb, &clu, 0, 1, NULL, &dot_name);
  1067. if (ret)
  1068. return ret;
  1069. memcpy(dot_name.name, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH);
  1070. if (p_dir->dir == fsi->root_dir)
  1071. ret = fsi->fs_func->init_dir_entry(sb, &clu, 1, TYPE_DIR, CLUS_FREE, 0);
  1072. else
  1073. ret = fsi->fs_func->init_dir_entry(sb, &clu, 1, TYPE_DIR, p_dir->dir, 0);
  1074. if (ret)
  1075. return ret;
  1076. ret = fsi->fs_func->init_ext_entry(sb, &clu, 1, 1, NULL, &dot_name);
  1077. if (ret)
  1078. return ret;
  1079. }
  1080. /* (2) update the directory entry */
  1081. /* make sub-dir entry in parent directory */
  1082. ret = fsi->fs_func->init_dir_entry(sb, p_dir, dentry, TYPE_DIR, clu.dir, size);
  1083. if (ret)
  1084. return ret;
  1085. ret = fsi->fs_func->init_ext_entry(sb, p_dir, dentry, num_entries, p_uniname, &dos_name);
  1086. if (ret)
  1087. return ret;
  1088. fid->dir.dir = p_dir->dir;
  1089. fid->dir.size = p_dir->size;
  1090. fid->dir.flags = p_dir->flags;
  1091. fid->entry = dentry;
  1092. fid->attr = ATTR_SUBDIR;
  1093. fid->flags = (fsi->vol_type == EXFAT) ? 0x03 : 0x01;
  1094. fid->size = size;
  1095. fid->start_clu = clu.dir;
  1096. fid->type = TYPE_DIR;
  1097. fid->rwoffset = 0;
  1098. fid->hint_bmap.off = CLUS_EOF;
  1099. /* hint_stat will be used if this is directory. */
  1100. fid->version = 0;
  1101. fid->hint_stat.eidx = 0;
  1102. fid->hint_stat.clu = fid->start_clu;
  1103. fid->hint_femp.eidx = -1;
  1104. return 0;
  1105. } /* end of create_dir */
  1106. static s32 create_file(struct inode *inode, CHAIN_T *p_dir, UNI_NAME_T *p_uniname, u8 mode, FILE_ID_T *fid)
  1107. {
  1108. s32 ret, dentry, num_entries;
  1109. DOS_NAME_T dos_name;
  1110. struct super_block *sb = inode->i_sb;
  1111. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  1112. ret = get_num_entries_and_dos_name(sb, p_dir, p_uniname, &num_entries, &dos_name, 0);
  1113. if (ret)
  1114. return ret;
  1115. /* find_empty_entry must be called before alloc_cluster() */
  1116. dentry = find_empty_entry(inode, p_dir, num_entries);
  1117. if (dentry < 0)
  1118. return dentry; /* -EIO or -ENOSPC */
  1119. /* (1) update the directory entry */
  1120. /* fill the dos name directory entry information of the created file.
  1121. * the first cluster is not determined yet. (0)
  1122. */
  1123. ret = fsi->fs_func->init_dir_entry(sb, p_dir, dentry, TYPE_FILE | mode, CLUS_FREE, 0);
  1124. if (ret)
  1125. return ret;
  1126. ret = fsi->fs_func->init_ext_entry(sb, p_dir, dentry, num_entries, p_uniname, &dos_name);
  1127. if (ret)
  1128. return ret;
  1129. fid->dir.dir = p_dir->dir;
  1130. fid->dir.size = p_dir->size;
  1131. fid->dir.flags = p_dir->flags;
  1132. fid->entry = dentry;
  1133. fid->attr = ATTR_ARCHIVE | mode;
  1134. fid->flags = (fsi->vol_type == EXFAT) ? 0x03 : 0x01;
  1135. fid->size = 0;
  1136. fid->start_clu = CLUS_EOF;
  1137. fid->type = TYPE_FILE;
  1138. fid->rwoffset = 0;
  1139. fid->hint_bmap.off = CLUS_EOF;
  1140. /* hint_stat will be used if this is directory. */
  1141. fid->version = 0;
  1142. fid->hint_stat.eidx = 0;
  1143. fid->hint_stat.clu = fid->start_clu;
  1144. fid->hint_femp.eidx = -1;
  1145. return 0;
  1146. } /* end of create_file */
  1147. static s32 remove_file(struct inode *inode, CHAIN_T *p_dir, s32 entry)
  1148. {
  1149. s32 num_entries;
  1150. u64 sector;
  1151. DENTRY_T *ep;
  1152. struct super_block *sb = inode->i_sb;
  1153. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  1154. ep = get_dentry_in_dir(sb, p_dir, entry, &sector);
  1155. if (!ep)
  1156. return -EIO;
  1157. dcache_lock(sb, sector);
  1158. /* dcache_lock() before call count_ext_entries() */
  1159. num_entries = fsi->fs_func->count_ext_entries(sb, p_dir, entry, ep);
  1160. if (num_entries < 0) {
  1161. dcache_unlock(sb, sector);
  1162. return -EIO;
  1163. }
  1164. num_entries++;
  1165. dcache_unlock(sb, sector);
  1166. /* (1) update the directory entry */
  1167. return fsi->fs_func->delete_dir_entry(sb, p_dir, entry, 0, num_entries);
  1168. } /* end of remove_file */
  1169. static s32 rename_file(struct inode *inode, CHAIN_T *p_dir, s32 oldentry, UNI_NAME_T *p_uniname, FILE_ID_T *fid)
  1170. {
  1171. s32 ret, newentry = -1, num_old_entries, num_new_entries;
  1172. u64 sector_old, sector_new;
  1173. DOS_NAME_T dos_name;
  1174. DENTRY_T *epold, *epnew;
  1175. struct super_block *sb = inode->i_sb;
  1176. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  1177. epold = get_dentry_in_dir(sb, p_dir, oldentry, &sector_old);
  1178. if (!epold)
  1179. return -EIO;
  1180. dcache_lock(sb, sector_old);
  1181. /* dcache_lock() before call count_ext_entries() */
  1182. num_old_entries = fsi->fs_func->count_ext_entries(sb, p_dir, oldentry, epold);
  1183. if (num_old_entries < 0) {
  1184. dcache_unlock(sb, sector_old);
  1185. return -EIO;
  1186. }
  1187. num_old_entries++;
  1188. ret = get_num_entries_and_dos_name(sb, p_dir, p_uniname, &num_new_entries, &dos_name, 0);
  1189. if (ret) {
  1190. dcache_unlock(sb, sector_old);
  1191. return ret;
  1192. }
  1193. if (num_old_entries < num_new_entries) {
  1194. newentry = find_empty_entry(inode, p_dir, num_new_entries);
  1195. if (newentry < 0) {
  1196. dcache_unlock(sb, sector_old);
  1197. return newentry; /* -EIO or -ENOSPC */
  1198. }
  1199. epnew = get_dentry_in_dir(sb, p_dir, newentry, &sector_new);
  1200. if (!epnew) {
  1201. dcache_unlock(sb, sector_old);
  1202. return -EIO;
  1203. }
  1204. memcpy((void *) epnew, (void *) epold, DENTRY_SIZE);
  1205. if (fsi->fs_func->get_entry_type(epnew) == TYPE_FILE) {
  1206. fsi->fs_func->set_entry_attr(epnew, fsi->fs_func->get_entry_attr(epnew) | ATTR_ARCHIVE);
  1207. fid->attr |= ATTR_ARCHIVE;
  1208. }
  1209. dcache_modify(sb, sector_new);
  1210. dcache_unlock(sb, sector_old);
  1211. if (fsi->vol_type == EXFAT) {
  1212. epold = get_dentry_in_dir(sb, p_dir, oldentry+1, &sector_old);
  1213. dcache_lock(sb, sector_old);
  1214. epnew = get_dentry_in_dir(sb, p_dir, newentry+1, &sector_new);
  1215. if (!epold || !epnew) {
  1216. dcache_unlock(sb, sector_old);
  1217. return -EIO;
  1218. }
  1219. memcpy((void *) epnew, (void *) epold, DENTRY_SIZE);
  1220. dcache_modify(sb, sector_new);
  1221. dcache_unlock(sb, sector_old);
  1222. }
  1223. ret = fsi->fs_func->init_ext_entry(sb, p_dir, newentry, num_new_entries, p_uniname, &dos_name);
  1224. if (ret)
  1225. return ret;
  1226. fsi->fs_func->delete_dir_entry(sb, p_dir, oldentry, 0, num_old_entries);
  1227. fid->entry = newentry;
  1228. } else {
  1229. if (fsi->fs_func->get_entry_type(epold) == TYPE_FILE) {
  1230. fsi->fs_func->set_entry_attr(epold, fsi->fs_func->get_entry_attr(epold) | ATTR_ARCHIVE);
  1231. fid->attr |= ATTR_ARCHIVE;
  1232. }
  1233. dcache_modify(sb, sector_old);
  1234. dcache_unlock(sb, sector_old);
  1235. ret = fsi->fs_func->init_ext_entry(sb, p_dir, oldentry, num_new_entries, p_uniname, &dos_name);
  1236. if (ret)
  1237. return ret;
  1238. fsi->fs_func->delete_dir_entry(sb, p_dir, oldentry, num_new_entries, num_old_entries);
  1239. }
  1240. return 0;
  1241. } /* end of rename_file */
  1242. static s32 move_file(struct inode *inode, CHAIN_T *p_olddir, s32 oldentry,
  1243. CHAIN_T *p_newdir, UNI_NAME_T *p_uniname, FILE_ID_T *fid)
  1244. {
  1245. s32 ret, newentry, num_new_entries, num_old_entries;
  1246. u64 sector_mov, sector_new;
  1247. CHAIN_T clu;
  1248. DOS_NAME_T dos_name;
  1249. DENTRY_T *epmov, *epnew;
  1250. struct super_block *sb = inode->i_sb;
  1251. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  1252. epmov = get_dentry_in_dir(sb, p_olddir, oldentry, &sector_mov);
  1253. if (!epmov)
  1254. return -EIO;
  1255. /* check if the source and target directory is the same */
  1256. if (fsi->fs_func->get_entry_type(epmov) == TYPE_DIR &&
  1257. fsi->fs_func->get_entry_clu0(epmov) == p_newdir->dir)
  1258. return -EINVAL;
  1259. dcache_lock(sb, sector_mov);
  1260. /* dcache_lock() before call count_ext_entries() */
  1261. num_old_entries = fsi->fs_func->count_ext_entries(sb, p_olddir, oldentry, epmov);
  1262. if (num_old_entries < 0) {
  1263. dcache_unlock(sb, sector_mov);
  1264. return -EIO;
  1265. }
  1266. num_old_entries++;
  1267. ret = get_num_entries_and_dos_name(sb, p_newdir, p_uniname, &num_new_entries, &dos_name, 0);
  1268. if (ret) {
  1269. dcache_unlock(sb, sector_mov);
  1270. return ret;
  1271. }
  1272. newentry = find_empty_entry(inode, p_newdir, num_new_entries);
  1273. if (newentry < 0) {
  1274. dcache_unlock(sb, sector_mov);
  1275. return newentry; /* -EIO or -ENOSPC */
  1276. }
  1277. epnew = get_dentry_in_dir(sb, p_newdir, newentry, &sector_new);
  1278. if (!epnew) {
  1279. dcache_unlock(sb, sector_mov);
  1280. return -EIO;
  1281. }
  1282. memcpy((void *) epnew, (void *) epmov, DENTRY_SIZE);
  1283. if (fsi->fs_func->get_entry_type(epnew) == TYPE_FILE) {
  1284. fsi->fs_func->set_entry_attr(epnew, fsi->fs_func->get_entry_attr(epnew) | ATTR_ARCHIVE);
  1285. fid->attr |= ATTR_ARCHIVE;
  1286. }
  1287. dcache_modify(sb, sector_new);
  1288. dcache_unlock(sb, sector_mov);
  1289. if (fsi->vol_type == EXFAT) {
  1290. epmov = get_dentry_in_dir(sb, p_olddir, oldentry+1, &sector_mov);
  1291. dcache_lock(sb, sector_mov);
  1292. epnew = get_dentry_in_dir(sb, p_newdir, newentry+1, &sector_new);
  1293. if (!epmov || !epnew) {
  1294. dcache_unlock(sb, sector_mov);
  1295. return -EIO;
  1296. }
  1297. memcpy((void *) epnew, (void *) epmov, DENTRY_SIZE);
  1298. dcache_modify(sb, sector_new);
  1299. dcache_unlock(sb, sector_mov);
  1300. } else if (fsi->fs_func->get_entry_type(epnew) == TYPE_DIR) {
  1301. /* change ".." pointer to new parent dir */
  1302. clu.dir = fsi->fs_func->get_entry_clu0(epnew);
  1303. clu.flags = 0x01;
  1304. epnew = get_dentry_in_dir(sb, &clu, 1, &sector_new);
  1305. if (!epnew)
  1306. return -EIO;
  1307. if (p_newdir->dir == fsi->root_dir)
  1308. fsi->fs_func->set_entry_clu0(epnew, CLUS_FREE);
  1309. else
  1310. fsi->fs_func->set_entry_clu0(epnew, p_newdir->dir);
  1311. dcache_modify(sb, sector_new);
  1312. }
  1313. ret = fsi->fs_func->init_ext_entry(sb, p_newdir, newentry, num_new_entries, p_uniname, &dos_name);
  1314. if (ret)
  1315. return ret;
  1316. fsi->fs_func->delete_dir_entry(sb, p_olddir, oldentry, 0, num_old_entries);
  1317. fid->dir.dir = p_newdir->dir;
  1318. fid->dir.size = p_newdir->size;
  1319. fid->dir.flags = p_newdir->flags;
  1320. fid->entry = newentry;
  1321. return 0;
  1322. } /* end of move_file */
  1323. /*======================================================================*/
  1324. /* Global Function Definitions */
  1325. /*======================================================================*/
  1326. /* roll back to the initial state of the file system */
  1327. s32 fscore_init(void)
  1328. {
  1329. s32 ret;
  1330. ret = check_type_size();
  1331. if (ret)
  1332. return ret;
  1333. return extent_cache_init();
  1334. }
  1335. /* make free all memory-alloced global buffers */
  1336. s32 fscore_shutdown(void)
  1337. {
  1338. extent_cache_shutdown();
  1339. return 0;
  1340. }
  1341. /* check device is ejected */
  1342. s32 fscore_check_bdi_valid(struct super_block *sb)
  1343. {
  1344. return bdev_check_bdi_valid(sb);
  1345. }
  1346. static bool is_exfat(pbr_t *pbr)
  1347. {
  1348. int i = 53;
  1349. do {
  1350. if (pbr->bpb.f64.res_zero[i-1])
  1351. break;
  1352. } while (--i);
  1353. return i ? false : true;
  1354. }
  1355. static bool is_fat32(pbr_t *pbr)
  1356. {
  1357. if (le16_to_cpu(pbr->bpb.fat.num_fat_sectors))
  1358. return false;
  1359. return true;
  1360. }
  1361. inline pbr_t *read_pbr_with_logical_sector(struct super_block *sb, struct buffer_head **prev_bh)
  1362. {
  1363. pbr_t *p_pbr = (pbr_t *) (*prev_bh)->b_data;
  1364. u16 logical_sect = 0;
  1365. if (is_exfat(p_pbr))
  1366. logical_sect = 1 << p_pbr->bsx.f64.sect_size_bits;
  1367. else
  1368. logical_sect = get_unaligned_le16(&p_pbr->bpb.fat.sect_size);
  1369. /* is x a power of 2?
  1370. * (x) != 0 && (((x) & ((x) - 1)) == 0)
  1371. */
  1372. if (!is_power_of_2(logical_sect)
  1373. || (logical_sect < 512)
  1374. || (logical_sect > 4096)) {
  1375. sdfat_log_msg(sb, KERN_ERR, "bogus logical sector size %u",
  1376. logical_sect);
  1377. return NULL;
  1378. }
  1379. if (logical_sect < sb->s_blocksize) {
  1380. sdfat_log_msg(sb, KERN_ERR,
  1381. "logical sector size too small for device"
  1382. " (logical sector size = %u)", logical_sect);
  1383. return NULL;
  1384. }
  1385. if (logical_sect > sb->s_blocksize) {
  1386. struct buffer_head *bh = NULL;
  1387. __brelse(*prev_bh);
  1388. *prev_bh = NULL;
  1389. if (!sb_set_blocksize(sb, logical_sect)) {
  1390. sdfat_log_msg(sb, KERN_ERR,
  1391. "unable to set blocksize %u", logical_sect);
  1392. return NULL;
  1393. }
  1394. bh = sb_bread(sb, 0);
  1395. if (!bh) {
  1396. sdfat_log_msg(sb, KERN_ERR,
  1397. "unable to read boot sector "
  1398. "(logical sector size = %lu)", sb->s_blocksize);
  1399. return NULL;
  1400. }
  1401. *prev_bh = bh;
  1402. p_pbr = (pbr_t *) bh->b_data;
  1403. }
  1404. sdfat_log_msg(sb, KERN_INFO,
  1405. "set logical sector size : %lu", sb->s_blocksize);
  1406. return p_pbr;
  1407. }
  1408. /* mount the file system volume */
  1409. s32 fscore_mount(struct super_block *sb)
  1410. {
  1411. s32 ret;
  1412. pbr_t *p_pbr;
  1413. struct buffer_head *tmp_bh = NULL;
  1414. struct gendisk *disk = sb->s_bdev->bd_disk;
  1415. struct hd_struct *part = sb->s_bdev->bd_part;
  1416. struct sdfat_mount_options *opts = &(SDFAT_SB(sb)->options);
  1417. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  1418. /* initialize previous I/O error */
  1419. fsi->prev_eio = 0;
  1420. /* open the block device */
  1421. if (bdev_open_dev(sb))
  1422. return -EIO;
  1423. /* set block size to read super block */
  1424. sb_min_blocksize(sb, 512);
  1425. /* read boot sector */
  1426. ret = read_sect(sb, 0, &tmp_bh, 1);
  1427. if (ret) {
  1428. sdfat_log_msg(sb, KERN_ERR, "unable to read boot sector");
  1429. ret = -EIO;
  1430. goto bd_close;
  1431. }
  1432. /* PRB is read */
  1433. p_pbr = (pbr_t *) tmp_bh->b_data;
  1434. /* check the validity of PBR */
  1435. if (le16_to_cpu((p_pbr->signature)) != PBR_SIGNATURE) {
  1436. sdfat_log_msg(sb, KERN_ERR, "invalid boot record signature");
  1437. brelse(tmp_bh);
  1438. ret = -EINVAL;
  1439. goto bd_close;
  1440. }
  1441. /* check logical sector size */
  1442. p_pbr = read_pbr_with_logical_sector(sb, &tmp_bh);
  1443. if (!p_pbr) {
  1444. brelse(tmp_bh);
  1445. ret = -EIO;
  1446. goto bd_close;
  1447. }
  1448. /* fill fs_struct */
  1449. if (is_exfat(p_pbr)) {
  1450. if (opts->fs_type && opts->fs_type != FS_TYPE_EXFAT) {
  1451. sdfat_log_msg(sb, KERN_ERR,
  1452. "not specified filesystem type "
  1453. "(media:exfat, opts:%s)",
  1454. FS_TYPE_STR[opts->fs_type]);
  1455. ret = -EINVAL;
  1456. goto free_bh;
  1457. }
  1458. /* set maximum file size for exFAT */
  1459. sb->s_maxbytes = 0x7fffffffffffffffLL;
  1460. opts->improved_allocation = 0;
  1461. opts->defrag = 0;
  1462. ret = mount_exfat(sb, p_pbr);
  1463. } else {
  1464. if (opts->fs_type && opts->fs_type != FS_TYPE_VFAT) {
  1465. sdfat_log_msg(sb, KERN_ERR,
  1466. "not specified filesystem type "
  1467. "(media:vfat, opts:%s)",
  1468. FS_TYPE_STR[opts->fs_type]);
  1469. ret = -EINVAL;
  1470. goto free_bh;
  1471. }
  1472. /* set maximum file size for FAT */
  1473. sb->s_maxbytes = 0xffffffff;
  1474. if (is_fat32(p_pbr)) {
  1475. ret = mount_fat32(sb, p_pbr);
  1476. } else {
  1477. opts->improved_allocation = 0;
  1478. opts->defrag = 0;
  1479. ret = mount_fat16(sb, p_pbr);
  1480. }
  1481. }
  1482. free_bh:
  1483. brelse(tmp_bh);
  1484. if (ret) {
  1485. sdfat_log_msg(sb, KERN_ERR, "failed to mount fs-core");
  1486. goto bd_close;
  1487. }
  1488. /* warn misaligned data data start sector must be a multiple of clu_size */
  1489. sdfat_log_msg(sb, KERN_INFO,
  1490. "detected volume info : %s "
  1491. "(%04hX-%04hX, bps : %lu, spc : %u, data start : %llu, %s)",
  1492. sdfat_get_vol_type_str(fsi->vol_type),
  1493. (fsi->vol_id >> 16) & 0xffff, fsi->vol_id & 0xffff,
  1494. sb->s_blocksize, fsi->sect_per_clus, fsi->data_start_sector,
  1495. (fsi->data_start_sector & (fsi->sect_per_clus - 1)) ?
  1496. "misaligned" : "aligned");
  1497. sdfat_log_msg(sb, KERN_INFO,
  1498. "detected volume size : %llu KB (disk : %llu KB, "
  1499. "part : %llu KB)",
  1500. (fsi->num_sectors * (sb->s_blocksize >> SECTOR_SIZE_BITS)) >> 1,
  1501. disk ? (u64)((disk->part0.nr_sects) >> 1) : 0,
  1502. part ? (u64)((part->nr_sects) >> 1) : 0);
  1503. ret = load_upcase_table(sb);
  1504. if (ret) {
  1505. sdfat_log_msg(sb, KERN_ERR, "failed to load upcase table");
  1506. goto bd_close;
  1507. }
  1508. if (fsi->vol_type != EXFAT)
  1509. goto update_used_clus;
  1510. /* allocate-bitmap is only for exFAT */
  1511. ret = load_alloc_bmp(sb);
  1512. if (ret) {
  1513. sdfat_log_msg(sb, KERN_ERR, "failed to load alloc-bitmap");
  1514. goto free_upcase;
  1515. }
  1516. update_used_clus:
  1517. if (fsi->used_clusters == (u32) ~0) {
  1518. ret = fsi->fs_func->count_used_clusters(sb, &fsi->used_clusters);
  1519. if (ret) {
  1520. sdfat_log_msg(sb, KERN_ERR, "failed to scan clusters");
  1521. goto free_alloc_bmp;
  1522. }
  1523. }
  1524. return 0;
  1525. free_alloc_bmp:
  1526. if (fsi->vol_type == EXFAT)
  1527. free_alloc_bmp(sb);
  1528. free_upcase:
  1529. free_upcase_table(sb);
  1530. bd_close:
  1531. bdev_close_dev(sb);
  1532. return ret;
  1533. } /* end of fscore_mount */
  1534. /* umount the file system volume */
  1535. s32 fscore_umount(struct super_block *sb)
  1536. {
  1537. s32 ret = 0;
  1538. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  1539. if (fs_sync(sb, 0))
  1540. ret = -EIO;
  1541. if (fs_set_vol_flags(sb, VOL_CLEAN))
  1542. ret = -EIO;
  1543. free_upcase_table(sb);
  1544. if (fsi->vol_type == EXFAT)
  1545. free_alloc_bmp(sb);
  1546. if (fcache_release_all(sb))
  1547. ret = -EIO;
  1548. if (dcache_release_all(sb))
  1549. ret = -EIO;
  1550. amap_destroy(sb);
  1551. if (fsi->prev_eio)
  1552. ret = -EIO;
  1553. /* close the block device */
  1554. bdev_close_dev(sb);
  1555. return ret;
  1556. }
  1557. /* get the information of a file system volume */
  1558. s32 fscore_statfs(struct super_block *sb, VOL_INFO_T *info)
  1559. {
  1560. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  1561. if (fsi->used_clusters == (u32) ~0) {
  1562. if (fsi->fs_func->count_used_clusters(sb, &fsi->used_clusters))
  1563. return -EIO;
  1564. }
  1565. info->FatType = fsi->vol_type;
  1566. info->ClusterSize = fsi->cluster_size;
  1567. info->NumClusters = fsi->num_clusters - 2; /* clu 0 & 1 */
  1568. info->UsedClusters = fsi->used_clusters + fsi->reserved_clusters;
  1569. info->FreeClusters = info->NumClusters - info->UsedClusters;
  1570. return 0;
  1571. }
  1572. /* synchronize all file system volumes */
  1573. s32 fscore_sync_fs(struct super_block *sb, s32 do_sync)
  1574. {
  1575. /* synchronize the file system */
  1576. if (fs_sync(sb, do_sync))
  1577. return -EIO;
  1578. if (fs_set_vol_flags(sb, VOL_CLEAN))
  1579. return -EIO;
  1580. return 0;
  1581. }
  1582. /* stat allocation unit of a file system volume */
  1583. u32 fscore_get_au_stat(struct super_block *sb, s32 mode)
  1584. {
  1585. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  1586. if (fsi->fs_func->get_au_stat)
  1587. return fsi->fs_func->get_au_stat(sb, mode);
  1588. /* No error, just returns 0 */
  1589. return 0;
  1590. }
  1591. /*----------------------------------------------------------------------*/
  1592. /* File Operation Functions */
  1593. /*----------------------------------------------------------------------*/
  1594. /* lookup a file */
  1595. s32 fscore_lookup(struct inode *inode, u8 *path, FILE_ID_T *fid)
  1596. {
  1597. s32 ret, dentry, num_entries;
  1598. CHAIN_T dir;
  1599. UNI_NAME_T uni_name;
  1600. DOS_NAME_T dos_name;
  1601. DENTRY_T *ep, *ep2;
  1602. ENTRY_SET_CACHE_T *es = NULL;
  1603. struct super_block *sb = inode->i_sb;
  1604. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  1605. FILE_ID_T *dir_fid = &(SDFAT_I(inode)->fid);
  1606. TMSG("%s entered\n", __func__);
  1607. /* check the validity of directory name in the given pathname */
  1608. ret = resolve_path_for_lookup(inode, path, &dir, &uni_name);
  1609. if (ret)
  1610. return ret;
  1611. ret = get_num_entries_and_dos_name(sb, &dir, &uni_name, &num_entries, &dos_name, 1);
  1612. if (ret)
  1613. return ret;
  1614. /* check the validation of hint_stat and initialize it if required */
  1615. if (dir_fid->version != (u32)inode_peek_iversion(inode)) {
  1616. dir_fid->hint_stat.clu = dir.dir;
  1617. dir_fid->hint_stat.eidx = 0;
  1618. dir_fid->version = (u32)inode_peek_iversion(inode);
  1619. dir_fid->hint_femp.eidx = -1;
  1620. }
  1621. /* search the file name for directories */
  1622. dentry = fsi->fs_func->find_dir_entry(sb, dir_fid, &dir, &uni_name,
  1623. num_entries, &dos_name, TYPE_ALL);
  1624. if ((dentry < 0) && (dentry != -EEXIST))
  1625. return dentry; /* -error value */
  1626. fid->dir.dir = dir.dir;
  1627. fid->dir.size = dir.size;
  1628. fid->dir.flags = dir.flags;
  1629. fid->entry = dentry;
  1630. /* root directory itself */
  1631. if (unlikely(dentry == -EEXIST)) {
  1632. fid->type = TYPE_DIR;
  1633. fid->rwoffset = 0;
  1634. fid->hint_bmap.off = CLUS_EOF;
  1635. fid->attr = ATTR_SUBDIR;
  1636. fid->flags = 0x01;
  1637. fid->size = 0;
  1638. fid->start_clu = fsi->root_dir;
  1639. } else {
  1640. if (fsi->vol_type == EXFAT) {
  1641. es = get_dentry_set_in_dir(sb, &dir, dentry, ES_2_ENTRIES, &ep);
  1642. if (!es)
  1643. return -EIO;
  1644. ep2 = ep+1;
  1645. } else {
  1646. ep = get_dentry_in_dir(sb, &dir, dentry, NULL);
  1647. if (!ep)
  1648. return -EIO;
  1649. ep2 = ep;
  1650. }
  1651. fid->type = fsi->fs_func->get_entry_type(ep);
  1652. fid->rwoffset = 0;
  1653. fid->hint_bmap.off = CLUS_EOF;
  1654. fid->attr = fsi->fs_func->get_entry_attr(ep);
  1655. fid->size = fsi->fs_func->get_entry_size(ep2);
  1656. if ((fid->type == TYPE_FILE) && (fid->size == 0)) {
  1657. fid->flags = (fsi->vol_type == EXFAT) ? 0x03 : 0x01;
  1658. fid->start_clu = CLUS_EOF;
  1659. } else {
  1660. fid->flags = fsi->fs_func->get_entry_flag(ep2);
  1661. fid->start_clu = fsi->fs_func->get_entry_clu0(ep2);
  1662. }
  1663. if ((fid->type == TYPE_DIR) && (fsi->vol_type != EXFAT)) {
  1664. u32 num_clu = 0;
  1665. CHAIN_T tmp_dir;
  1666. tmp_dir.dir = fid->start_clu;
  1667. tmp_dir.flags = fid->flags;
  1668. tmp_dir.size = 0; /* UNUSED */
  1669. if (__count_num_clusters(sb, &tmp_dir, &num_clu))
  1670. return -EIO;
  1671. fid->size = (u64)num_clu << fsi->cluster_size_bits;
  1672. }
  1673. /* FOR GRACEFUL ERROR HANDLING */
  1674. if (IS_CLUS_FREE(fid->start_clu)) {
  1675. sdfat_fs_error(sb,
  1676. "non-zero size file starts with zero cluster "
  1677. "(size : %llu, p_dir : %u, entry : 0x%08x)",
  1678. fid->size, fid->dir.dir, fid->entry);
  1679. sdfat_debug_bug_on(1);
  1680. return -EIO;
  1681. }
  1682. if (fsi->vol_type == EXFAT)
  1683. release_dentry_set(es);
  1684. }
  1685. /* hint_stat will be used if this is directory. */
  1686. fid->version = 0;
  1687. fid->hint_stat.eidx = 0;
  1688. fid->hint_stat.clu = fid->start_clu;
  1689. fid->hint_femp.eidx = -1;
  1690. TMSG("%s exited successfully\n", __func__);
  1691. return 0;
  1692. } /* end of fscore_lookup */
  1693. /* create a file */
  1694. s32 fscore_create(struct inode *inode, u8 *path, u8 mode, FILE_ID_T *fid)
  1695. {
  1696. s32 ret/*, dentry*/;
  1697. CHAIN_T dir;
  1698. UNI_NAME_T uni_name;
  1699. struct super_block *sb = inode->i_sb;
  1700. /* check the validity of directory name in the given pathname */
  1701. ret = resolve_path(inode, path, &dir, &uni_name);
  1702. if (ret)
  1703. return ret;
  1704. fs_set_vol_flags(sb, VOL_DIRTY);
  1705. /* create a new file */
  1706. ret = create_file(inode, &dir, &uni_name, mode, fid);
  1707. fs_sync(sb, 0);
  1708. fs_set_vol_flags(sb, VOL_CLEAN);
  1709. return ret;
  1710. }
  1711. /* read data from a opened file */
  1712. s32 fscore_read_link(struct inode *inode, FILE_ID_T *fid, void *buffer, u64 count, u64 *rcount)
  1713. {
  1714. s32 ret = 0;
  1715. s32 offset, sec_offset;
  1716. u32 clu_offset;
  1717. u32 clu;
  1718. u64 logsector, oneblkread, read_bytes;
  1719. struct buffer_head *tmp_bh = NULL;
  1720. struct super_block *sb = inode->i_sb;
  1721. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  1722. /* check if the given file ID is opened */
  1723. if (fid->type != TYPE_FILE)
  1724. return -EPERM;
  1725. if (fid->rwoffset > fid->size)
  1726. fid->rwoffset = fid->size;
  1727. if (count > (fid->size - fid->rwoffset))
  1728. count = fid->size - fid->rwoffset;
  1729. if (count == 0) {
  1730. if (rcount)
  1731. *rcount = 0;
  1732. return 0;
  1733. }
  1734. read_bytes = 0;
  1735. while (count > 0) {
  1736. clu_offset = fid->rwoffset >> fsi->cluster_size_bits;
  1737. clu = fid->start_clu;
  1738. if (fid->flags == 0x03) {
  1739. clu += clu_offset;
  1740. } else {
  1741. /* hint information */
  1742. if ((clu_offset > 0) &&
  1743. ((fid->hint_bmap.off != CLUS_EOF) && (fid->hint_bmap.off > 0)) &&
  1744. (clu_offset >= fid->hint_bmap.off)) {
  1745. clu_offset -= fid->hint_bmap.off;
  1746. clu = fid->hint_bmap.clu;
  1747. }
  1748. while (clu_offset > 0) {
  1749. ret = get_next_clus_safe(sb, &clu);
  1750. if (ret)
  1751. goto err_out;
  1752. clu_offset--;
  1753. }
  1754. }
  1755. /* hint information */
  1756. fid->hint_bmap.off = fid->rwoffset >> fsi->cluster_size_bits;
  1757. fid->hint_bmap.clu = clu;
  1758. offset = (s32)(fid->rwoffset & (fsi->cluster_size - 1)); /* byte offset in cluster */
  1759. sec_offset = offset >> sb->s_blocksize_bits; /* sector offset in cluster */
  1760. offset &= (sb->s_blocksize - 1); /* byte offset in sector */
  1761. logsector = CLUS_TO_SECT(fsi, clu) + sec_offset;
  1762. oneblkread = (u64)(sb->s_blocksize - offset);
  1763. if (oneblkread > count)
  1764. oneblkread = count;
  1765. if ((offset == 0) && (oneblkread == sb->s_blocksize)) {
  1766. ret = read_sect(sb, logsector, &tmp_bh, 1);
  1767. if (ret)
  1768. goto err_out;
  1769. memcpy(((s8 *) buffer)+read_bytes, ((s8 *) tmp_bh->b_data), (s32) oneblkread);
  1770. } else {
  1771. ret = read_sect(sb, logsector, &tmp_bh, 1);
  1772. if (ret)
  1773. goto err_out;
  1774. memcpy(((s8 *) buffer)+read_bytes, ((s8 *) tmp_bh->b_data)+offset, (s32) oneblkread);
  1775. }
  1776. count -= oneblkread;
  1777. read_bytes += oneblkread;
  1778. fid->rwoffset += oneblkread;
  1779. }
  1780. err_out:
  1781. brelse(tmp_bh);
  1782. /* set the size of read bytes */
  1783. if (rcount != NULL)
  1784. *rcount = read_bytes;
  1785. return ret;
  1786. } /* end of fscore_read_link */
  1787. /* write data into a opened file */
  1788. s32 fscore_write_link(struct inode *inode, FILE_ID_T *fid, void *buffer, u64 count, u64 *wcount)
  1789. {
  1790. s32 ret = 0;
  1791. s32 modified = false, offset, sec_offset;
  1792. u32 clu_offset, num_clusters, num_alloc;
  1793. u32 clu, last_clu;
  1794. u64 logsector, sector, oneblkwrite, write_bytes;
  1795. CHAIN_T new_clu;
  1796. TIMESTAMP_T tm;
  1797. DENTRY_T *ep, *ep2;
  1798. ENTRY_SET_CACHE_T *es = NULL;
  1799. struct buffer_head *tmp_bh = NULL;
  1800. struct super_block *sb = inode->i_sb;
  1801. u32 blksize = (u32)sb->s_blocksize;
  1802. u32 blksize_mask = (u32)(sb->s_blocksize-1);
  1803. u8 blksize_bits = sb->s_blocksize_bits;
  1804. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  1805. /* check if the given file ID is opened */
  1806. if (fid->type != TYPE_FILE)
  1807. return -EPERM;
  1808. if (fid->rwoffset > fid->size)
  1809. fid->rwoffset = fid->size;
  1810. if (count == 0) {
  1811. if (wcount)
  1812. *wcount = 0;
  1813. return 0;
  1814. }
  1815. fs_set_vol_flags(sb, VOL_DIRTY);
  1816. if (fid->size == 0)
  1817. num_clusters = 0;
  1818. else
  1819. num_clusters = ((fid->size-1) >> fsi->cluster_size_bits) + 1;
  1820. write_bytes = 0;
  1821. while (count > 0) {
  1822. clu_offset = (fid->rwoffset >> fsi->cluster_size_bits);
  1823. clu = last_clu = fid->start_clu;
  1824. if (fid->flags == 0x03) {
  1825. if ((clu_offset > 0) && (!IS_CLUS_EOF(clu))) {
  1826. last_clu += clu_offset - 1;
  1827. if (clu_offset == num_clusters)
  1828. clu = CLUS_EOF;
  1829. else
  1830. clu += clu_offset;
  1831. }
  1832. } else {
  1833. /* hint information */
  1834. if ((clu_offset > 0) &&
  1835. ((fid->hint_bmap.off != CLUS_EOF) && (fid->hint_bmap.off > 0)) &&
  1836. (clu_offset >= fid->hint_bmap.off)) {
  1837. clu_offset -= fid->hint_bmap.off;
  1838. clu = fid->hint_bmap.clu;
  1839. }
  1840. while ((clu_offset > 0) && (!IS_CLUS_EOF(clu))) {
  1841. last_clu = clu;
  1842. ret = get_next_clus_safe(sb, &clu);
  1843. if (ret)
  1844. goto err_out;
  1845. clu_offset--;
  1846. }
  1847. }
  1848. if (IS_CLUS_EOF(clu)) {
  1849. num_alloc = ((count-1) >> fsi->cluster_size_bits) + 1;
  1850. new_clu.dir = IS_CLUS_EOF(last_clu) ? CLUS_EOF : last_clu+1;
  1851. new_clu.size = 0;
  1852. new_clu.flags = fid->flags;
  1853. /* (1) allocate a chain of clusters */
  1854. ret = fsi->fs_func->alloc_cluster(sb, num_alloc, &new_clu, ALLOC_COLD);
  1855. if (ret)
  1856. goto err_out;
  1857. /* (2) append to the FAT chain */
  1858. if (IS_CLUS_EOF(last_clu)) {
  1859. if (new_clu.flags == 0x01)
  1860. fid->flags = 0x01;
  1861. fid->start_clu = new_clu.dir;
  1862. modified = true;
  1863. } else {
  1864. if (new_clu.flags != fid->flags) {
  1865. /* no-fat-chain bit is disabled,
  1866. * so fat-chain should be synced with
  1867. * alloc-bmp
  1868. */
  1869. chain_cont_cluster(sb, fid->start_clu, num_clusters);
  1870. fid->flags = 0x01;
  1871. modified = true;
  1872. }
  1873. if (new_clu.flags == 0x01) {
  1874. ret = fat_ent_set(sb, last_clu, new_clu.dir);
  1875. if (ret)
  1876. goto err_out;
  1877. }
  1878. }
  1879. num_clusters += num_alloc;
  1880. clu = new_clu.dir;
  1881. }
  1882. /* hint information */
  1883. fid->hint_bmap.off = fid->rwoffset >> fsi->cluster_size_bits;
  1884. fid->hint_bmap.clu = clu;
  1885. /* byte offset in cluster */
  1886. offset = (s32)(fid->rwoffset & (fsi->cluster_size-1));
  1887. /* sector offset in cluster */
  1888. sec_offset = offset >> blksize_bits;
  1889. /* byte offset in sector */
  1890. offset &= blksize_mask;
  1891. logsector = CLUS_TO_SECT(fsi, clu) + sec_offset;
  1892. oneblkwrite = (u64)(blksize - offset);
  1893. if (oneblkwrite > count)
  1894. oneblkwrite = count;
  1895. if ((offset == 0) && (oneblkwrite == blksize)) {
  1896. ret = read_sect(sb, logsector, &tmp_bh, 0);
  1897. if (ret)
  1898. goto err_out;
  1899. memcpy(((s8 *)tmp_bh->b_data),
  1900. ((s8 *)buffer)+write_bytes,
  1901. (s32)oneblkwrite);
  1902. ret = write_sect(sb, logsector, tmp_bh, 0);
  1903. if (ret) {
  1904. brelse(tmp_bh);
  1905. goto err_out;
  1906. }
  1907. } else {
  1908. if ((offset > 0) || ((fid->rwoffset+oneblkwrite) < fid->size)) {
  1909. ret = read_sect(sb, logsector, &tmp_bh, 1);
  1910. if (ret)
  1911. goto err_out;
  1912. } else {
  1913. ret = read_sect(sb, logsector, &tmp_bh, 0);
  1914. if (ret)
  1915. goto err_out;
  1916. }
  1917. memcpy(((s8 *) tmp_bh->b_data)+offset, ((s8 *) buffer)+write_bytes, (s32) oneblkwrite);
  1918. ret = write_sect(sb, logsector, tmp_bh, 0);
  1919. if (ret) {
  1920. brelse(tmp_bh);
  1921. goto err_out;
  1922. }
  1923. }
  1924. count -= oneblkwrite;
  1925. write_bytes += oneblkwrite;
  1926. fid->rwoffset += oneblkwrite;
  1927. fid->attr |= ATTR_ARCHIVE;
  1928. if (fid->size < fid->rwoffset) {
  1929. fid->size = fid->rwoffset;
  1930. modified = true;
  1931. }
  1932. }
  1933. brelse(tmp_bh);
  1934. /* (3) update the direcoty entry */
  1935. /* get_entry_(set_)in_dir shoulb be check DIR_DELETED flag. */
  1936. if (fsi->vol_type == EXFAT) {
  1937. es = get_dentry_set_in_dir(sb, &(fid->dir), fid->entry, ES_ALL_ENTRIES, &ep);
  1938. if (!es) {
  1939. ret = -EIO;
  1940. goto err_out;
  1941. }
  1942. ep2 = ep+1;
  1943. } else {
  1944. ep = get_dentry_in_dir(sb, &(fid->dir), fid->entry, &sector);
  1945. if (!ep) {
  1946. ret = -EIO;
  1947. goto err_out;
  1948. }
  1949. ep2 = ep;
  1950. }
  1951. fsi->fs_func->set_entry_time(ep, tm_now(inode, &tm), TM_MODIFY);
  1952. fsi->fs_func->set_entry_attr(ep, fid->attr);
  1953. if (modified) {
  1954. if (fsi->fs_func->get_entry_flag(ep2) != fid->flags)
  1955. fsi->fs_func->set_entry_flag(ep2, fid->flags);
  1956. if (fsi->fs_func->get_entry_size(ep2) != fid->size)
  1957. fsi->fs_func->set_entry_size(ep2, fid->size);
  1958. if (fsi->fs_func->get_entry_clu0(ep2) != fid->start_clu)
  1959. fsi->fs_func->set_entry_clu0(ep2, fid->start_clu);
  1960. }
  1961. if (fsi->vol_type == EXFAT) {
  1962. if (update_dir_chksum_with_entry_set(sb, es)) {
  1963. ret = -EIO;
  1964. goto err_out;
  1965. }
  1966. release_dentry_set(es);
  1967. } else {
  1968. if (dcache_modify(sb, sector)) {
  1969. ret = -EIO;
  1970. goto err_out;
  1971. }
  1972. }
  1973. fs_sync(sb, 0);
  1974. fs_set_vol_flags(sb, VOL_CLEAN);
  1975. err_out:
  1976. /* set the size of written bytes */
  1977. if (wcount)
  1978. *wcount = write_bytes;
  1979. return ret;
  1980. } /* end of fscore_write_link */
  1981. /* resize the file length */
  1982. s32 fscore_truncate(struct inode *inode, u64 old_size, u64 new_size)
  1983. {
  1984. u32 num_clusters_new, num_clusters_da, num_clusters_phys;
  1985. u32 last_clu = CLUS_FREE;
  1986. u64 sector;
  1987. CHAIN_T clu;
  1988. TIMESTAMP_T tm;
  1989. DENTRY_T *ep, *ep2;
  1990. struct super_block *sb = inode->i_sb;
  1991. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  1992. FILE_ID_T *fid = &(SDFAT_I(inode)->fid);
  1993. ENTRY_SET_CACHE_T *es = NULL;
  1994. s32 evict = (fid->dir.dir == DIR_DELETED) ? 1 : 0;
  1995. /* check if the given file ID is opened */
  1996. if ((fid->type != TYPE_FILE) && (fid->type != TYPE_DIR))
  1997. return -EPERM;
  1998. /* TO CHECK inode type and size */
  1999. MMSG("%s: inode(%p) type(%s) size:%lld->%lld\n", __func__, inode,
  2000. (fid->type == TYPE_FILE) ? "file" : "dir", old_size, new_size);
  2001. /* XXX : This is for debugging. */
  2002. /* It can be when write failed */
  2003. #if 0
  2004. if (fid->size != old_size) {
  2005. DMSG("%s: inode(%p) size-mismatch(old:%lld != fid:%lld)\n",
  2006. __func__, inode, old_size, fid->size);
  2007. WARN_ON(1);
  2008. }
  2009. #endif
  2010. /*
  2011. * There is no lock to protect fid->size.
  2012. * So, we should get old_size and use it.
  2013. */
  2014. if (old_size <= new_size)
  2015. return 0;
  2016. fs_set_vol_flags(sb, VOL_DIRTY);
  2017. /* Reserved count update */
  2018. #define num_clusters(v) ((v) ? (u32)(((v) - 1) >> fsi->cluster_size_bits) + 1 : 0)
  2019. num_clusters_da = num_clusters(SDFAT_I(inode)->i_size_aligned);
  2020. num_clusters_new = num_clusters(i_size_read(inode));
  2021. num_clusters_phys = num_clusters(SDFAT_I(inode)->i_size_ondisk);
  2022. /* num_clusters(i_size_old) should be equal to num_clusters_da */
  2023. BUG_ON((num_clusters(old_size)) != (num_clusters(SDFAT_I(inode)->i_size_aligned)));
  2024. /* for debugging (FIXME: is okay on no-da case?) */
  2025. BUG_ON(num_clusters_da < num_clusters_phys);
  2026. if ((num_clusters_da != num_clusters_phys) &&
  2027. (num_clusters_new < num_clusters_da)) {
  2028. /* Decrement reserved clusters
  2029. * n_reserved = num_clusters_da - max(new,phys)
  2030. */
  2031. int n_reserved = (num_clusters_new > num_clusters_phys) ?
  2032. (num_clusters_da - num_clusters_new) :
  2033. (num_clusters_da - num_clusters_phys);
  2034. fsi->reserved_clusters -= n_reserved;
  2035. BUG_ON(fsi->reserved_clusters < 0);
  2036. }
  2037. clu.dir = fid->start_clu;
  2038. /* In no-da case, num_clusters_phys is equal to below value
  2039. * clu.size = (u32)((old_size-1) >> fsi->cluster_size_bits) + 1;
  2040. */
  2041. clu.size = num_clusters_phys;
  2042. clu.flags = fid->flags;
  2043. /* For bigdata */
  2044. sdfat_statistics_set_trunc(clu.flags, &clu);
  2045. if (new_size > 0) {
  2046. /* Truncate FAT chain num_clusters after the first cluster
  2047. * num_clusters = min(new, phys);
  2048. */
  2049. u32 num_clusters = (num_clusters_new < num_clusters_phys) ?
  2050. num_clusters_new : num_clusters_phys;
  2051. /* Follow FAT chain
  2052. * (defensive coding - works fine even with corrupted FAT table
  2053. */
  2054. if (clu.flags == 0x03) {
  2055. clu.dir += num_clusters;
  2056. clu.size -= num_clusters;
  2057. #if 0
  2058. /* extent_get_clus can`t know last_cluster
  2059. * when find target cluster in cache.
  2060. */
  2061. } else if (fid->type == TYPE_FILE) {
  2062. u32 fclus = 0;
  2063. s32 err = extent_get_clus(inode, num_clusters,
  2064. &fclus, &(clu.dir), &last_clu, 0);
  2065. if (err)
  2066. return -EIO;
  2067. ASSERT(fclus == num_clusters);
  2068. if ((num_clusters > 1) && (last_clu == fid->start_clu)) {
  2069. u32 fclus_tmp = 0;
  2070. u32 temp = 0;
  2071. err = extent_get_clus(inode, num_clusters - 1,
  2072. &fclus_tmp, &last_clu, &temp, 0);
  2073. if (err)
  2074. return -EIO;
  2075. ASSERT(fclus_tmp == (num_clusters - 1));
  2076. }
  2077. num_clusters -= fclus;
  2078. clu.size -= fclus;
  2079. #endif
  2080. } else {
  2081. while (num_clusters > 0) {
  2082. last_clu = clu.dir;
  2083. if (get_next_clus_safe(sb, &(clu.dir)))
  2084. return -EIO;
  2085. num_clusters--;
  2086. clu.size--;
  2087. }
  2088. }
  2089. /* Optimization avialable: */
  2090. #if 0
  2091. if (num_clusters_new < num_clusters) {
  2092. < loop >
  2093. } else {
  2094. // num_clusters_new >= num_clusters_phys
  2095. // FAT truncation is not necessary
  2096. clu.dir = CLUS_EOF;
  2097. clu.size = 0;
  2098. }
  2099. #endif
  2100. } else if (new_size == 0) {
  2101. fid->flags = (fsi->vol_type == EXFAT) ? 0x03 : 0x01;
  2102. fid->start_clu = CLUS_EOF;
  2103. }
  2104. fid->size = new_size;
  2105. if (fid->type == TYPE_FILE)
  2106. fid->attr |= ATTR_ARCHIVE;
  2107. /*
  2108. * clu.dir: free from
  2109. * clu.size: # of clusters to free (exFAT, 0x03 only), no fat_free if 0
  2110. * clu.flags: fid->flags (exFAT only)
  2111. */
  2112. /* (1) update the directory entry */
  2113. if (!evict) {
  2114. if (fsi->vol_type == EXFAT) {
  2115. es = get_dentry_set_in_dir(sb, &(fid->dir), fid->entry, ES_ALL_ENTRIES, &ep);
  2116. if (!es)
  2117. return -EIO;
  2118. ep2 = ep+1;
  2119. } else {
  2120. ep = get_dentry_in_dir(sb, &(fid->dir), fid->entry, &sector);
  2121. if (!ep)
  2122. return -EIO;
  2123. ep2 = ep;
  2124. }
  2125. fsi->fs_func->set_entry_time(ep, tm_now(inode, &tm), TM_MODIFY);
  2126. fsi->fs_func->set_entry_attr(ep, fid->attr);
  2127. /*
  2128. * if (fsi->vol_type != EXFAT)
  2129. * dcache_modify(sb, sector);
  2130. */
  2131. /* File size should be zero if there is no cluster allocated */
  2132. if (IS_CLUS_EOF(fid->start_clu))
  2133. fsi->fs_func->set_entry_size(ep2, 0);
  2134. else
  2135. fsi->fs_func->set_entry_size(ep2, new_size);
  2136. if (new_size == 0) {
  2137. /* Any directory can not be truncated to zero */
  2138. BUG_ON(fid->type != TYPE_FILE);
  2139. fsi->fs_func->set_entry_flag(ep2, 0x01);
  2140. fsi->fs_func->set_entry_clu0(ep2, CLUS_FREE);
  2141. }
  2142. if (fsi->vol_type == EXFAT) {
  2143. if (update_dir_chksum_with_entry_set(sb, es))
  2144. return -EIO;
  2145. release_dentry_set(es);
  2146. } else {
  2147. if (dcache_modify(sb, sector))
  2148. return -EIO;
  2149. }
  2150. } /* end of if(fid->dir.dir != DIR_DELETED) */
  2151. /* (2) cut off from the FAT chain */
  2152. if ((fid->flags == 0x01) &&
  2153. (!IS_CLUS_FREE(last_clu)) && (!IS_CLUS_EOF(last_clu))) {
  2154. if (fat_ent_set(sb, last_clu, CLUS_EOF))
  2155. return -EIO;
  2156. }
  2157. /* (3) invalidate cache and free the clusters */
  2158. /* clear extent cache */
  2159. extent_cache_inval_inode(inode);
  2160. /* hint information */
  2161. fid->hint_bmap.off = CLUS_EOF;
  2162. fid->hint_bmap.clu = CLUS_EOF;
  2163. if (fid->rwoffset > fid->size)
  2164. fid->rwoffset = fid->size;
  2165. /* hint_stat will be used if this is directory. */
  2166. fid->hint_stat.eidx = 0;
  2167. fid->hint_stat.clu = fid->start_clu;
  2168. fid->hint_femp.eidx = -1;
  2169. /* free the clusters */
  2170. if (fsi->fs_func->free_cluster(sb, &clu, evict))
  2171. return -EIO;
  2172. fs_sync(sb, 0);
  2173. fs_set_vol_flags(sb, VOL_CLEAN);
  2174. return 0;
  2175. } /* end of fscore_truncate */
  2176. static void update_parent_info(FILE_ID_T *fid, struct inode *parent_inode)
  2177. {
  2178. FS_INFO_T *fsi = &(SDFAT_SB(parent_inode->i_sb)->fsi);
  2179. FILE_ID_T *parent_fid = &(SDFAT_I(parent_inode)->fid);
  2180. /*
  2181. * the problem that FILE_ID_T caches wrong parent info.
  2182. *
  2183. * because of flag-mismatch of fid->dir,
  2184. * there is abnormal traversing cluster chain.
  2185. */
  2186. if (unlikely((parent_fid->flags != fid->dir.flags)
  2187. || (parent_fid->size != (fid->dir.size<<fsi->cluster_size_bits))
  2188. || (parent_fid->start_clu != fid->dir.dir))) {
  2189. fid->dir.dir = parent_fid->start_clu;
  2190. fid->dir.flags = parent_fid->flags;
  2191. fid->dir.size = ((parent_fid->size + (fsi->cluster_size-1))
  2192. >> fsi->cluster_size_bits);
  2193. }
  2194. }
  2195. /* rename or move a old file into a new file */
  2196. s32 fscore_rename(struct inode *old_parent_inode, FILE_ID_T *fid,
  2197. struct inode *new_parent_inode, struct dentry *new_dentry)
  2198. {
  2199. s32 ret;
  2200. s32 dentry;
  2201. CHAIN_T olddir, newdir;
  2202. CHAIN_T *p_dir = NULL;
  2203. UNI_NAME_T uni_name;
  2204. DENTRY_T *ep;
  2205. struct super_block *sb = old_parent_inode->i_sb;
  2206. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  2207. u8 *new_path = (u8 *) new_dentry->d_name.name;
  2208. struct inode *new_inode = new_dentry->d_inode;
  2209. int num_entries;
  2210. FILE_ID_T *new_fid = NULL;
  2211. u32 new_entry_type = TYPE_UNUSED;
  2212. s32 new_entry = 0;
  2213. /* check the validity of pointer parameters */
  2214. if ((new_path == NULL) || (strlen(new_path) == 0))
  2215. return -EINVAL;
  2216. if (fid->dir.dir == DIR_DELETED) {
  2217. EMSG("%s : abnormal access to deleted source dentry\n", __func__);
  2218. return -ENOENT;
  2219. }
  2220. /* patch 1.2.4 : the problem that FILE_ID_T caches wrong parent info. */
  2221. update_parent_info(fid, old_parent_inode);
  2222. olddir.dir = fid->dir.dir;
  2223. olddir.size = fid->dir.size;
  2224. olddir.flags = fid->dir.flags;
  2225. dentry = fid->entry;
  2226. /* check if the old file is "." or ".." */
  2227. if (fsi->vol_type != EXFAT) {
  2228. if ((olddir.dir != fsi->root_dir) && (dentry < 2))
  2229. return -EPERM;
  2230. }
  2231. ep = get_dentry_in_dir(sb, &olddir, dentry, NULL);
  2232. if (!ep)
  2233. return -EIO;
  2234. #ifdef CONFIG_SDFAT_CHECK_RO_ATTR
  2235. if (fsi->fs_func->get_entry_attr(ep) & ATTR_READONLY)
  2236. return -EPERM;
  2237. #endif
  2238. /* check whether new dir is existing directory and empty */
  2239. if (new_inode) {
  2240. ret = -EIO;
  2241. new_fid = &SDFAT_I(new_inode)->fid;
  2242. if (new_fid->dir.dir == DIR_DELETED) {
  2243. EMSG("%s : abnormal access to deleted target dentry\n", __func__);
  2244. goto out;
  2245. }
  2246. /* patch 1.2.4 :
  2247. * the problem that FILE_ID_T caches wrong parent info.
  2248. *
  2249. * FIXME : is needed?
  2250. */
  2251. update_parent_info(new_fid, new_parent_inode);
  2252. p_dir = &(new_fid->dir);
  2253. new_entry = new_fid->entry;
  2254. ep = get_dentry_in_dir(sb, p_dir, new_entry, NULL);
  2255. if (!ep)
  2256. goto out;
  2257. new_entry_type = fsi->fs_func->get_entry_type(ep);
  2258. /* if new_inode exists, update fid */
  2259. new_fid->size = i_size_read(new_inode);
  2260. if (new_entry_type == TYPE_DIR) {
  2261. CHAIN_T new_clu;
  2262. new_clu.dir = new_fid->start_clu;
  2263. new_clu.size = ((new_fid->size-1) >> fsi->cluster_size_bits) + 1;
  2264. new_clu.flags = new_fid->flags;
  2265. ret = check_dir_empty(sb, &new_clu);
  2266. if (ret)
  2267. return ret;
  2268. }
  2269. }
  2270. /* check the validity of directory name in the given new pathname */
  2271. ret = resolve_path(new_parent_inode, new_path, &newdir, &uni_name);
  2272. if (ret)
  2273. return ret;
  2274. fs_set_vol_flags(sb, VOL_DIRTY);
  2275. if (olddir.dir == newdir.dir)
  2276. ret = rename_file(new_parent_inode, &olddir, dentry, &uni_name, fid);
  2277. else
  2278. ret = move_file(new_parent_inode, &olddir, dentry, &newdir, &uni_name, fid);
  2279. if ((!ret) && new_inode) {
  2280. /* delete entries of new_dir */
  2281. ep = get_dentry_in_dir(sb, p_dir, new_entry, NULL);
  2282. if (!ep) {
  2283. ret = -EIO;
  2284. goto del_out;
  2285. }
  2286. num_entries = fsi->fs_func->count_ext_entries(sb, p_dir, new_entry, ep);
  2287. if (num_entries < 0) {
  2288. ret = -EIO;
  2289. goto del_out;
  2290. }
  2291. if (fsi->fs_func->delete_dir_entry(sb, p_dir, new_entry, 0, num_entries+1)) {
  2292. ret = -EIO;
  2293. goto del_out;
  2294. }
  2295. /* Free the clusters if new_inode is a dir(as if fscore_rmdir) */
  2296. if (new_entry_type == TYPE_DIR) {
  2297. /* new_fid, new_clu_to_free */
  2298. CHAIN_T new_clu_to_free;
  2299. new_clu_to_free.dir = new_fid->start_clu;
  2300. new_clu_to_free.size = ((new_fid->size-1) >> fsi->cluster_size_bits) + 1;
  2301. new_clu_to_free.flags = new_fid->flags;
  2302. if (fsi->fs_func->free_cluster(sb, &new_clu_to_free, 1)) {
  2303. /* just set I/O error only */
  2304. ret = -EIO;
  2305. }
  2306. new_fid->size = 0;
  2307. new_fid->start_clu = CLUS_EOF;
  2308. new_fid->flags = (fsi->vol_type == EXFAT) ? 0x03 : 0x01;
  2309. }
  2310. del_out:
  2311. /* Update new_inode fid
  2312. * Prevent syncing removed new_inode
  2313. * (new_fid is already initialized above code ("if (new_inode)")
  2314. */
  2315. new_fid->dir.dir = DIR_DELETED;
  2316. }
  2317. out:
  2318. fs_sync(sb, 0);
  2319. fs_set_vol_flags(sb, VOL_CLEAN);
  2320. return ret;
  2321. } /* end of fscore_rename */
  2322. /* remove a file */
  2323. s32 fscore_remove(struct inode *inode, FILE_ID_T *fid)
  2324. {
  2325. s32 ret;
  2326. s32 dentry;
  2327. CHAIN_T dir, clu_to_free;
  2328. DENTRY_T *ep;
  2329. struct super_block *sb = inode->i_sb;
  2330. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  2331. dir.dir = fid->dir.dir;
  2332. dir.size = fid->dir.size;
  2333. dir.flags = fid->dir.flags;
  2334. dentry = fid->entry;
  2335. if (fid->dir.dir == DIR_DELETED) {
  2336. EMSG("%s : abnormal access to deleted dentry\n", __func__);
  2337. return -ENOENT;
  2338. }
  2339. ep = get_dentry_in_dir(sb, &dir, dentry, NULL);
  2340. if (!ep)
  2341. return -EIO;
  2342. #ifdef CONFIG_SDFAT_CHECK_RO_ATTR
  2343. if (fsi->fs_func->get_entry_attr(ep) & ATTR_READONLY)
  2344. return -EPERM;
  2345. #endif
  2346. fs_set_vol_flags(sb, VOL_DIRTY);
  2347. /* (1) update the directory entry */
  2348. ret = remove_file(inode, &dir, dentry);
  2349. if (ret)
  2350. goto out;
  2351. clu_to_free.dir = fid->start_clu;
  2352. clu_to_free.size = ((fid->size-1) >> fsi->cluster_size_bits) + 1;
  2353. clu_to_free.flags = fid->flags;
  2354. /* (2) invalidate extent cache and free the clusters
  2355. */
  2356. /* clear extent cache */
  2357. extent_cache_inval_inode(inode);
  2358. ret = fsi->fs_func->free_cluster(sb, &clu_to_free, 0);
  2359. /* WARN : DO NOT RETURN ERROR IN HERE */
  2360. /* (3) update FILE_ID_T */
  2361. fid->size = 0;
  2362. fid->start_clu = CLUS_EOF;
  2363. fid->flags = (fsi->vol_type == EXFAT) ? 0x03 : 0x01;
  2364. fid->dir.dir = DIR_DELETED;
  2365. fs_sync(sb, 0);
  2366. fs_set_vol_flags(sb, VOL_CLEAN);
  2367. out:
  2368. return ret;
  2369. } /* end of fscore_remove */
  2370. /*
  2371. * Get the information of a given file
  2372. * REMARK : This function does not need any file name on linux
  2373. *
  2374. * info.Size means the value saved on disk.
  2375. * But root directory doesn`t have real dentry,
  2376. * so the size of root directory returns calculated one exceptively.
  2377. */
  2378. s32 fscore_read_inode(struct inode *inode, DIR_ENTRY_T *info)
  2379. {
  2380. u64 sector;
  2381. s32 count;
  2382. CHAIN_T dir;
  2383. TIMESTAMP_T tm;
  2384. DENTRY_T *ep, *ep2;
  2385. struct super_block *sb = inode->i_sb;
  2386. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  2387. FILE_ID_T *fid = &(SDFAT_I(inode)->fid);
  2388. ENTRY_SET_CACHE_T *es = NULL;
  2389. u8 is_dir = (fid->type == TYPE_DIR) ? 1 : 0;
  2390. TMSG("%s entered\n", __func__);
  2391. extent_cache_init_inode(inode);
  2392. /* if root directory */
  2393. if (is_dir && (fid->dir.dir == fsi->root_dir) && (fid->entry == -1)) {
  2394. info->Attr = ATTR_SUBDIR;
  2395. memset((s8 *) &info->CreateTimestamp, 0, sizeof(DATE_TIME_T));
  2396. memset((s8 *) &info->ModifyTimestamp, 0, sizeof(DATE_TIME_T));
  2397. memset((s8 *) &info->AccessTimestamp, 0, sizeof(DATE_TIME_T));
  2398. //strcpy(info->NameBuf.sfn, ".");
  2399. //strcpy(info->NameBuf.lfn, ".");
  2400. dir.dir = fsi->root_dir;
  2401. dir.flags = 0x01;
  2402. dir.size = 0; /* UNUSED */
  2403. /* FAT16 root_dir */
  2404. if (IS_CLUS_FREE(fsi->root_dir)) {
  2405. info->Size = fsi->dentries_in_root << DENTRY_SIZE_BITS;
  2406. } else {
  2407. u32 num_clu;
  2408. if (__count_num_clusters(sb, &dir, &num_clu))
  2409. return -EIO;
  2410. info->Size = (u64)num_clu << fsi->cluster_size_bits;
  2411. }
  2412. count = __count_dos_name_entries(sb, &dir, TYPE_DIR, NULL);
  2413. if (count < 0)
  2414. return -EIO;
  2415. info->NumSubdirs = count;
  2416. return 0;
  2417. }
  2418. /* get the directory entry of given file or directory */
  2419. if (fsi->vol_type == EXFAT) {
  2420. /* es should be released */
  2421. es = get_dentry_set_in_dir(sb, &(fid->dir), fid->entry, ES_2_ENTRIES, &ep);
  2422. if (!es)
  2423. return -EIO;
  2424. ep2 = ep+1;
  2425. } else {
  2426. ep = get_dentry_in_dir(sb, &(fid->dir), fid->entry, &sector);
  2427. if (!ep)
  2428. return -EIO;
  2429. ep2 = ep;
  2430. /* dcache should be unlocked */
  2431. dcache_lock(sb, sector);
  2432. }
  2433. /* set FILE_INFO structure using the acquired DENTRY_T */
  2434. info->Attr = fsi->fs_func->get_entry_attr(ep);
  2435. fsi->fs_func->get_entry_time(ep, &tm, TM_CREATE);
  2436. info->CreateTimestamp.Year = tm.year;
  2437. info->CreateTimestamp.Month = tm.mon;
  2438. info->CreateTimestamp.Day = tm.day;
  2439. info->CreateTimestamp.Hour = tm.hour;
  2440. info->CreateTimestamp.Minute = tm.min;
  2441. info->CreateTimestamp.Second = tm.sec;
  2442. info->CreateTimestamp.MilliSecond = 0;
  2443. info->CreateTimestamp.Timezone.value = tm.tz.value;
  2444. fsi->fs_func->get_entry_time(ep, &tm, TM_MODIFY);
  2445. info->ModifyTimestamp.Year = tm.year;
  2446. info->ModifyTimestamp.Month = tm.mon;
  2447. info->ModifyTimestamp.Day = tm.day;
  2448. info->ModifyTimestamp.Hour = tm.hour;
  2449. info->ModifyTimestamp.Minute = tm.min;
  2450. info->ModifyTimestamp.Second = tm.sec;
  2451. info->ModifyTimestamp.MilliSecond = 0;
  2452. info->ModifyTimestamp.Timezone.value = tm.tz.value;
  2453. memset((s8 *) &info->AccessTimestamp, 0, sizeof(DATE_TIME_T));
  2454. info->NumSubdirs = 0;
  2455. info->Size = fsi->fs_func->get_entry_size(ep2);
  2456. if (fsi->vol_type == EXFAT)
  2457. release_dentry_set(es);
  2458. else
  2459. dcache_unlock(sb, sector);
  2460. if (is_dir) {
  2461. u32 dotcnt = 0;
  2462. dir.dir = fid->start_clu;
  2463. dir.flags = fid->flags;
  2464. dir.size = fid->size >> fsi->cluster_size_bits;
  2465. /*
  2466. * NOTE :
  2467. * If "dir.flags" has 0x01, "dir.size" is meaningless.
  2468. */
  2469. #if 0
  2470. if (info->Size == 0) {
  2471. s32 num_clu;
  2472. if (__count_num_clusters(sb, &dir, &num_clu))
  2473. return -EIO;
  2474. info->Size = (u64)num_clu << fsi->cluster_size_bits;
  2475. }
  2476. #endif
  2477. count = __count_dos_name_entries(sb, &dir, TYPE_DIR, &dotcnt);
  2478. if (count < 0)
  2479. return -EIO;
  2480. if (fsi->vol_type == EXFAT) {
  2481. count += SDFAT_MIN_SUBDIR;
  2482. } else {
  2483. /*
  2484. * if directory has been corrupted,
  2485. * we have to adjust subdir count.
  2486. */
  2487. BUG_ON(dotcnt > SDFAT_MIN_SUBDIR);
  2488. if (dotcnt < SDFAT_MIN_SUBDIR) {
  2489. EMSG("%s: contents of the directory has been "
  2490. "corrupted (parent clus : %08x, idx : %d)",
  2491. __func__, fid->dir.dir, fid->entry);
  2492. }
  2493. count += (SDFAT_MIN_SUBDIR - dotcnt);
  2494. }
  2495. info->NumSubdirs = count;
  2496. }
  2497. TMSG("%s exited successfully\n", __func__);
  2498. return 0;
  2499. } /* end of fscore_read_inode */
  2500. /* set the information of a given file
  2501. * REMARK : This function does not need any file name on linux
  2502. */
  2503. s32 fscore_write_inode(struct inode *inode, DIR_ENTRY_T *info, s32 sync)
  2504. {
  2505. s32 ret = -EIO;
  2506. u64 sector;
  2507. TIMESTAMP_T tm;
  2508. DENTRY_T *ep, *ep2;
  2509. ENTRY_SET_CACHE_T *es = NULL;
  2510. struct super_block *sb = inode->i_sb;
  2511. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  2512. FILE_ID_T *fid = &(SDFAT_I(inode)->fid);
  2513. u8 is_dir = (fid->type == TYPE_DIR) ? 1 : 0;
  2514. /* SKIP WRITING INODE :
  2515. * if the indoe is already unlinked,
  2516. * there is no need for updating inode
  2517. */
  2518. if (fid->dir.dir == DIR_DELETED)
  2519. return 0;
  2520. if (is_dir && (fid->dir.dir == fsi->root_dir) && (fid->entry == -1))
  2521. return 0;
  2522. fs_set_vol_flags(sb, VOL_DIRTY);
  2523. /* get the directory entry of given file or directory */
  2524. if (fsi->vol_type == EXFAT) {
  2525. es = get_dentry_set_in_dir(sb, &(fid->dir), fid->entry, ES_ALL_ENTRIES, &ep);
  2526. if (!es)
  2527. return -EIO;
  2528. ep2 = ep+1;
  2529. } else {
  2530. /* for other than exfat */
  2531. ep = get_dentry_in_dir(sb, &(fid->dir), fid->entry, &sector);
  2532. if (!ep)
  2533. return -EIO;
  2534. ep2 = ep;
  2535. }
  2536. fsi->fs_func->set_entry_attr(ep, info->Attr);
  2537. /* set FILE_INFO structure using the acquired DENTRY_T */
  2538. tm.tz = info->CreateTimestamp.Timezone;
  2539. tm.sec = info->CreateTimestamp.Second;
  2540. tm.min = info->CreateTimestamp.Minute;
  2541. tm.hour = info->CreateTimestamp.Hour;
  2542. tm.day = info->CreateTimestamp.Day;
  2543. tm.mon = info->CreateTimestamp.Month;
  2544. tm.year = info->CreateTimestamp.Year;
  2545. fsi->fs_func->set_entry_time(ep, &tm, TM_CREATE);
  2546. tm.tz = info->ModifyTimestamp.Timezone;
  2547. tm.sec = info->ModifyTimestamp.Second;
  2548. tm.min = info->ModifyTimestamp.Minute;
  2549. tm.hour = info->ModifyTimestamp.Hour;
  2550. tm.day = info->ModifyTimestamp.Day;
  2551. tm.mon = info->ModifyTimestamp.Month;
  2552. tm.year = info->ModifyTimestamp.Year;
  2553. fsi->fs_func->set_entry_time(ep, &tm, TM_MODIFY);
  2554. if (is_dir && fsi->vol_type != EXFAT) {
  2555. /* overwirte dirsize if FAT32 and dir size != 0 */
  2556. if (fsi->fs_func->get_entry_size(ep2))
  2557. fsi->fs_func->set_entry_size(ep2, 0);
  2558. } else {
  2559. /* File size should be zero if there is no cluster allocated */
  2560. u64 on_disk_size = info->Size;
  2561. if (IS_CLUS_EOF(fid->start_clu))
  2562. on_disk_size = 0;
  2563. fsi->fs_func->set_entry_size(ep2, on_disk_size);
  2564. }
  2565. if (fsi->vol_type == EXFAT) {
  2566. ret = update_dir_chksum_with_entry_set(sb, es);
  2567. release_dentry_set(es);
  2568. } else {
  2569. ret = dcache_modify(sb, sector);
  2570. }
  2571. fs_sync(sb, sync);
  2572. /* Comment below code to prevent super block update frequently */
  2573. //fs_set_vol_flags(sb, VOL_CLEAN);
  2574. return ret;
  2575. } /* end of fscore_write_inode */
  2576. /*
  2577. * Input: inode, (logical) clu_offset, target allocation area
  2578. * Output: errcode, cluster number
  2579. * *clu = (~0), if it's unable to allocate a new cluster
  2580. */
  2581. s32 fscore_map_clus(struct inode *inode, u32 clu_offset, u32 *clu, int dest)
  2582. {
  2583. s32 ret, modified = false;
  2584. u32 last_clu;
  2585. u64 sector;
  2586. CHAIN_T new_clu;
  2587. DENTRY_T *ep;
  2588. ENTRY_SET_CACHE_T *es = NULL;
  2589. struct super_block *sb = inode->i_sb;
  2590. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  2591. FILE_ID_T *fid = &(SDFAT_I(inode)->fid);
  2592. u32 local_clu_offset = clu_offset;
  2593. s32 reserved_clusters = fsi->reserved_clusters;
  2594. u32 num_to_be_allocated = 0, num_clusters = 0;
  2595. fid->rwoffset = (s64)(clu_offset) << fsi->cluster_size_bits;
  2596. if (SDFAT_I(inode)->i_size_ondisk > 0)
  2597. num_clusters = (u32)((SDFAT_I(inode)->i_size_ondisk-1) >> fsi->cluster_size_bits) + 1;
  2598. if (clu_offset >= num_clusters)
  2599. num_to_be_allocated = clu_offset - num_clusters + 1;
  2600. if ((dest == ALLOC_NOWHERE) && (num_to_be_allocated > 0)) {
  2601. *clu = CLUS_EOF;
  2602. return 0;
  2603. }
  2604. /* check always request cluster is 1 */
  2605. //ASSERT(num_to_be_allocated == 1);
  2606. sdfat_debug_check_clusters(inode);
  2607. *clu = last_clu = fid->start_clu;
  2608. /* XXX: Defensive code needed.
  2609. * what if i_size_ondisk != # of allocated clusters
  2610. */
  2611. if (fid->flags == 0x03) {
  2612. if ((clu_offset > 0) && (!IS_CLUS_EOF(*clu))) {
  2613. last_clu += clu_offset - 1;
  2614. if (clu_offset == num_clusters)
  2615. *clu = CLUS_EOF;
  2616. else
  2617. *clu += clu_offset;
  2618. }
  2619. } else if (fid->type == TYPE_FILE) {
  2620. u32 fclus = 0;
  2621. s32 err = extent_get_clus(inode, clu_offset,
  2622. &fclus, clu, &last_clu, 1);
  2623. if (err)
  2624. return -EIO;
  2625. clu_offset -= fclus;
  2626. } else {
  2627. /* hint information */
  2628. if ((clu_offset > 0) &&
  2629. ((fid->hint_bmap.off != CLUS_EOF) && (fid->hint_bmap.off > 0)) &&
  2630. (clu_offset >= fid->hint_bmap.off)) {
  2631. clu_offset -= fid->hint_bmap.off;
  2632. /* hint_bmap.clu should be valid */
  2633. ASSERT(fid->hint_bmap.clu >= 2);
  2634. *clu = fid->hint_bmap.clu;
  2635. }
  2636. while ((clu_offset > 0) && (!IS_CLUS_EOF(*clu))) {
  2637. last_clu = *clu;
  2638. if (get_next_clus_safe(sb, clu))
  2639. return -EIO;
  2640. clu_offset--;
  2641. }
  2642. }
  2643. if (IS_CLUS_EOF(*clu)) {
  2644. fs_set_vol_flags(sb, VOL_DIRTY);
  2645. new_clu.dir = (IS_CLUS_EOF(last_clu)) ? CLUS_EOF : last_clu + 1;
  2646. new_clu.size = 0;
  2647. new_clu.flags = fid->flags;
  2648. /* (1) allocate a cluster */
  2649. if (num_to_be_allocated < 1) {
  2650. /* Broken FAT (i_sze > allocated FAT) */
  2651. EMSG("%s: invalid fat chain : inode(%p) "
  2652. "num_to_be_allocated(%d) "
  2653. "i_size_ondisk(%lld) fid->flags(%02x) "
  2654. "fid->start(%08x) fid->hint_off(%u) "
  2655. "fid->hint_clu(%u) fid->rwoffset(%llu) "
  2656. "modified_clu_off(%d) last_clu(%08x) "
  2657. "new_clu(%08x)", __func__, inode,
  2658. num_to_be_allocated,
  2659. (SDFAT_I(inode)->i_size_ondisk),
  2660. fid->flags, fid->start_clu,
  2661. fid->hint_bmap.off, fid->hint_bmap.clu,
  2662. fid->rwoffset, clu_offset,
  2663. last_clu, new_clu.dir);
  2664. sdfat_fs_error(sb, "broken FAT chain.");
  2665. return -EIO;
  2666. }
  2667. ret = fsi->fs_func->alloc_cluster(sb, num_to_be_allocated, &new_clu, ALLOC_COLD);
  2668. if (ret)
  2669. return ret;
  2670. if (IS_CLUS_EOF(new_clu.dir) || IS_CLUS_FREE(new_clu.dir)) {
  2671. sdfat_fs_error(sb, "bogus cluster new allocated"
  2672. "(last_clu : %u, new_clu : %u)",
  2673. last_clu, new_clu.dir);
  2674. ASSERT(0);
  2675. return -EIO;
  2676. }
  2677. /* Reserved cluster dec. */
  2678. // XXX: Inode DA flag needed
  2679. if (SDFAT_SB(sb)->options.improved_allocation & SDFAT_ALLOC_DELAY) {
  2680. BUG_ON(reserved_clusters < num_to_be_allocated);
  2681. reserved_clusters -= num_to_be_allocated;
  2682. }
  2683. /* (2) append to the FAT chain */
  2684. if (IS_CLUS_EOF(last_clu)) {
  2685. if (new_clu.flags == 0x01)
  2686. fid->flags = 0x01;
  2687. fid->start_clu = new_clu.dir;
  2688. modified = true;
  2689. } else {
  2690. if (new_clu.flags != fid->flags) {
  2691. /* no-fat-chain bit is disabled,
  2692. * so fat-chain should be synced with alloc-bmp
  2693. */
  2694. chain_cont_cluster(sb, fid->start_clu, num_clusters);
  2695. fid->flags = 0x01;
  2696. modified = true;
  2697. }
  2698. if (new_clu.flags == 0x01)
  2699. if (fat_ent_set(sb, last_clu, new_clu.dir))
  2700. return -EIO;
  2701. }
  2702. num_clusters += num_to_be_allocated;
  2703. *clu = new_clu.dir;
  2704. if (fid->dir.dir != DIR_DELETED) {
  2705. if (fsi->vol_type == EXFAT) {
  2706. es = get_dentry_set_in_dir(sb, &(fid->dir), fid->entry, ES_ALL_ENTRIES, &ep);
  2707. if (!es)
  2708. return -EIO;
  2709. /* get stream entry */
  2710. ep++;
  2711. }
  2712. /* (3) update directory entry */
  2713. if (modified) {
  2714. if (fsi->vol_type != EXFAT) {
  2715. ep = get_dentry_in_dir(sb, &(fid->dir), fid->entry, &sector);
  2716. if (!ep)
  2717. return -EIO;
  2718. }
  2719. if (fsi->fs_func->get_entry_flag(ep) != fid->flags)
  2720. fsi->fs_func->set_entry_flag(ep, fid->flags);
  2721. if (fsi->fs_func->get_entry_clu0(ep) != fid->start_clu)
  2722. fsi->fs_func->set_entry_clu0(ep, fid->start_clu);
  2723. fsi->fs_func->set_entry_size(ep, fid->size);
  2724. if (fsi->vol_type != EXFAT) {
  2725. if (dcache_modify(sb, sector))
  2726. return -EIO;
  2727. }
  2728. }
  2729. if (fsi->vol_type == EXFAT) {
  2730. if (update_dir_chksum_with_entry_set(sb, es))
  2731. return -EIO;
  2732. release_dentry_set(es);
  2733. }
  2734. } /* end of if != DIR_DELETED */
  2735. /* add number of new blocks to inode (non-DA only) */
  2736. if (!(SDFAT_SB(sb)->options.improved_allocation & SDFAT_ALLOC_DELAY)) {
  2737. inode->i_blocks += num_to_be_allocated << (fsi->cluster_size_bits - sb->s_blocksize_bits);
  2738. } else {
  2739. // DA의 경우, i_blocks가 이미 증가해있어야 함.
  2740. BUG_ON(clu_offset >= (inode->i_blocks >> (fsi->cluster_size_bits - sb->s_blocksize_bits)));
  2741. }
  2742. #if 0
  2743. fs_sync(sb, 0);
  2744. fs_set_vol_flags(sb, VOL_CLEAN);
  2745. #endif
  2746. /* (4) Move *clu pointer along FAT chains (hole care)
  2747. * because the caller of this function expect *clu to be the last cluster.
  2748. * This only works when num_to_be_allocated >= 2,
  2749. * *clu = (the first cluster of the allocated chain) => (the last cluster of ...)
  2750. */
  2751. if (fid->flags == 0x03) {
  2752. *clu += num_to_be_allocated - 1;
  2753. } else {
  2754. while (num_to_be_allocated > 1) {
  2755. if (get_next_clus_safe(sb, clu))
  2756. return -EIO;
  2757. num_to_be_allocated--;
  2758. }
  2759. }
  2760. }
  2761. /* update reserved_clusters */
  2762. fsi->reserved_clusters = reserved_clusters;
  2763. /* hint information */
  2764. fid->hint_bmap.off = local_clu_offset;
  2765. fid->hint_bmap.clu = *clu;
  2766. return 0;
  2767. } /* end of fscore_map_clus */
  2768. /* allocate reserved cluster */
  2769. s32 fscore_reserve_clus(struct inode *inode)
  2770. {
  2771. struct super_block *sb = inode->i_sb;
  2772. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  2773. if ((fsi->used_clusters + fsi->reserved_clusters) >= (fsi->num_clusters - 2))
  2774. return -ENOSPC;
  2775. if (bdev_check_bdi_valid(sb))
  2776. return -EIO;
  2777. fsi->reserved_clusters++;
  2778. /* inode->i_blocks update */
  2779. inode->i_blocks += 1 << (fsi->cluster_size_bits - sb->s_blocksize_bits);
  2780. sdfat_debug_check_clusters(inode);
  2781. return 0;
  2782. }
  2783. /* remove an entry, BUT don't truncate */
  2784. s32 fscore_unlink(struct inode *inode, FILE_ID_T *fid)
  2785. {
  2786. s32 dentry;
  2787. CHAIN_T dir;
  2788. DENTRY_T *ep;
  2789. struct super_block *sb = inode->i_sb;
  2790. dir.dir = fid->dir.dir;
  2791. dir.size = fid->dir.size;
  2792. dir.flags = fid->dir.flags;
  2793. dentry = fid->entry;
  2794. if (fid->dir.dir == DIR_DELETED) {
  2795. EMSG("%s : abnormal access to deleted dentry\n", __func__);
  2796. return -ENOENT;
  2797. }
  2798. ep = get_dentry_in_dir(sb, &dir, dentry, NULL);
  2799. if (!ep)
  2800. return -EIO;
  2801. #ifdef CONFIG_SDFAT_CHECK_RO_ATTR
  2802. if (SDFAT_SB(sb)->fsi.fs_func->get_entry_attr(ep) & ATTR_READONLY)
  2803. return -EPERM;
  2804. #endif
  2805. fs_set_vol_flags(sb, VOL_DIRTY);
  2806. /* (1) update the directory entry */
  2807. if (remove_file(inode, &dir, dentry))
  2808. return -EIO;
  2809. /* This doesn't modify fid */
  2810. fid->dir.dir = DIR_DELETED;
  2811. fs_sync(sb, 0);
  2812. fs_set_vol_flags(sb, VOL_CLEAN);
  2813. return 0;
  2814. }
  2815. /*----------------------------------------------------------------------*/
  2816. /* Directory Operation Functions */
  2817. /*----------------------------------------------------------------------*/
  2818. /* create a directory */
  2819. s32 fscore_mkdir(struct inode *inode, u8 *path, FILE_ID_T *fid)
  2820. {
  2821. s32 ret/*, dentry*/;
  2822. CHAIN_T dir;
  2823. UNI_NAME_T uni_name;
  2824. struct super_block *sb = inode->i_sb;
  2825. TMSG("%s entered\n", __func__);
  2826. /* check the validity of directory name in the given old pathname */
  2827. ret = resolve_path(inode, path, &dir, &uni_name);
  2828. if (ret)
  2829. goto out;
  2830. fs_set_vol_flags(sb, VOL_DIRTY);
  2831. ret = create_dir(inode, &dir, &uni_name, fid);
  2832. fs_sync(sb, 0);
  2833. fs_set_vol_flags(sb, VOL_CLEAN);
  2834. out:
  2835. TMSG("%s exited with err(%d)\n", __func__, ret);
  2836. return ret;
  2837. }
  2838. /* read a directory entry from the opened directory */
  2839. s32 fscore_readdir(struct inode *inode, DIR_ENTRY_T *dir_entry)
  2840. {
  2841. s32 i;
  2842. s32 dentries_per_clu, dentries_per_clu_bits = 0;
  2843. u32 type, clu_offset;
  2844. u64 sector;
  2845. CHAIN_T dir, clu;
  2846. UNI_NAME_T uni_name;
  2847. TIMESTAMP_T tm;
  2848. DENTRY_T *ep;
  2849. struct super_block *sb = inode->i_sb;
  2850. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  2851. FILE_ID_T *fid = &(SDFAT_I(inode)->fid);
  2852. u32 dentry = (u32)(fid->rwoffset & 0xFFFFFFFF); /* u32 is enough for directory */
  2853. /* check if the given file ID is opened */
  2854. if (fid->type != TYPE_DIR)
  2855. return -EPERM;
  2856. if (fid->entry == -1) {
  2857. dir.dir = fsi->root_dir;
  2858. dir.size = 0; /* just initialize, but will not use */
  2859. dir.flags = 0x01;
  2860. } else {
  2861. dir.dir = fid->start_clu;
  2862. dir.size = fid->size >> fsi->cluster_size_bits;
  2863. dir.flags = fid->flags;
  2864. sdfat_debug_bug_on(dentry >= (dir.size * fsi->dentries_per_clu));
  2865. }
  2866. if (IS_CLUS_FREE(dir.dir)) { /* FAT16 root_dir */
  2867. dentries_per_clu = fsi->dentries_in_root;
  2868. /* Prevent readdir over directory size */
  2869. if (dentry >= dentries_per_clu) {
  2870. clu.dir = CLUS_EOF;
  2871. } else {
  2872. clu.dir = dir.dir;
  2873. clu.size = dir.size;
  2874. clu.flags = dir.flags;
  2875. }
  2876. } else {
  2877. dentries_per_clu = fsi->dentries_per_clu;
  2878. dentries_per_clu_bits = ilog2(dentries_per_clu);
  2879. clu_offset = dentry >> dentries_per_clu_bits;
  2880. clu.dir = dir.dir;
  2881. clu.size = dir.size;
  2882. clu.flags = dir.flags;
  2883. if (clu.flags == 0x03) {
  2884. clu.dir += clu_offset;
  2885. clu.size -= clu_offset;
  2886. } else {
  2887. /* hint_information */
  2888. if ((clu_offset > 0) &&
  2889. ((fid->hint_bmap.off != CLUS_EOF) && (fid->hint_bmap.off > 0)) &&
  2890. (clu_offset >= fid->hint_bmap.off)) {
  2891. clu_offset -= fid->hint_bmap.off;
  2892. clu.dir = fid->hint_bmap.clu;
  2893. }
  2894. while (clu_offset > 0) {
  2895. if (get_next_clus_safe(sb, &(clu.dir)))
  2896. return -EIO;
  2897. clu_offset--;
  2898. }
  2899. }
  2900. }
  2901. while (!IS_CLUS_EOF(clu.dir)) {
  2902. if (IS_CLUS_FREE(dir.dir)) /* FAT16 root_dir */
  2903. i = dentry % dentries_per_clu;
  2904. else
  2905. i = dentry & (dentries_per_clu-1);
  2906. for ( ; i < dentries_per_clu; i++, dentry++) {
  2907. ep = get_dentry_in_dir(sb, &clu, i, &sector);
  2908. if (!ep)
  2909. return -EIO;
  2910. type = fsi->fs_func->get_entry_type(ep);
  2911. if (type == TYPE_UNUSED)
  2912. break;
  2913. if ((type != TYPE_FILE) && (type != TYPE_DIR))
  2914. continue;
  2915. dcache_lock(sb, sector);
  2916. dir_entry->Attr = fsi->fs_func->get_entry_attr(ep);
  2917. fsi->fs_func->get_entry_time(ep, &tm, TM_CREATE);
  2918. dir_entry->CreateTimestamp.Year = tm.year;
  2919. dir_entry->CreateTimestamp.Month = tm.mon;
  2920. dir_entry->CreateTimestamp.Day = tm.day;
  2921. dir_entry->CreateTimestamp.Hour = tm.hour;
  2922. dir_entry->CreateTimestamp.Minute = tm.min;
  2923. dir_entry->CreateTimestamp.Second = tm.sec;
  2924. dir_entry->CreateTimestamp.MilliSecond = 0;
  2925. fsi->fs_func->get_entry_time(ep, &tm, TM_MODIFY);
  2926. dir_entry->ModifyTimestamp.Year = tm.year;
  2927. dir_entry->ModifyTimestamp.Month = tm.mon;
  2928. dir_entry->ModifyTimestamp.Day = tm.day;
  2929. dir_entry->ModifyTimestamp.Hour = tm.hour;
  2930. dir_entry->ModifyTimestamp.Minute = tm.min;
  2931. dir_entry->ModifyTimestamp.Second = tm.sec;
  2932. dir_entry->ModifyTimestamp.MilliSecond = 0;
  2933. memset((s8 *) &dir_entry->AccessTimestamp, 0, sizeof(DATE_TIME_T));
  2934. *(uni_name.name) = 0x0;
  2935. fsi->fs_func->get_uniname_from_ext_entry(sb, &dir, dentry, uni_name.name);
  2936. if (*(uni_name.name) == 0x0)
  2937. get_uniname_from_dos_entry(sb, (DOS_DENTRY_T *) ep, &uni_name, 0x1);
  2938. nls_uni16s_to_vfsname(sb, &uni_name,
  2939. dir_entry->NameBuf.lfn,
  2940. dir_entry->NameBuf.lfnbuf_len);
  2941. dcache_unlock(sb, sector);
  2942. if (fsi->vol_type == EXFAT) {
  2943. ep = get_dentry_in_dir(sb, &clu, i+1, NULL);
  2944. if (!ep)
  2945. return -EIO;
  2946. } else {
  2947. get_uniname_from_dos_entry(sb, (DOS_DENTRY_T *) ep, &uni_name, 0x0);
  2948. nls_uni16s_to_vfsname(sb, &uni_name,
  2949. dir_entry->NameBuf.sfn,
  2950. dir_entry->NameBuf.sfnbuf_len);
  2951. }
  2952. dir_entry->Size = fsi->fs_func->get_entry_size(ep);
  2953. /*
  2954. * Update hint information :
  2955. * fat16 root directory does not need it.
  2956. */
  2957. if (!IS_CLUS_FREE(dir.dir)) {
  2958. fid->hint_bmap.off = dentry >> dentries_per_clu_bits;
  2959. fid->hint_bmap.clu = clu.dir;
  2960. }
  2961. fid->rwoffset = (s64) ++dentry;
  2962. return 0;
  2963. }
  2964. /* fat16 root directory */
  2965. if (IS_CLUS_FREE(dir.dir))
  2966. break;
  2967. if (clu.flags == 0x03) {
  2968. if ((--clu.size) > 0)
  2969. clu.dir++;
  2970. else
  2971. clu.dir = CLUS_EOF;
  2972. } else {
  2973. if (get_next_clus_safe(sb, &(clu.dir)))
  2974. return -EIO;
  2975. }
  2976. }
  2977. dir_entry->NameBuf.lfn[0] = '\0';
  2978. fid->rwoffset = (s64)dentry;
  2979. return 0;
  2980. } /* end of fscore_readdir */
  2981. /* remove a directory */
  2982. s32 fscore_rmdir(struct inode *inode, FILE_ID_T *fid)
  2983. {
  2984. s32 ret;
  2985. s32 dentry;
  2986. DENTRY_T *ep;
  2987. CHAIN_T dir, clu_to_free;
  2988. struct super_block *sb = inode->i_sb;
  2989. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  2990. dir.dir = fid->dir.dir;
  2991. dir.size = fid->dir.size;
  2992. dir.flags = fid->dir.flags;
  2993. dentry = fid->entry;
  2994. if (fid->dir.dir == DIR_DELETED) {
  2995. EMSG("%s : abnormal access to deleted dentry\n", __func__);
  2996. return -ENOENT;
  2997. }
  2998. /* check if the file is "." or ".." */
  2999. if (fsi->vol_type != EXFAT) {
  3000. if ((dir.dir != fsi->root_dir) && (dentry < 2))
  3001. return -EPERM;
  3002. }
  3003. ep = get_dentry_in_dir(sb, &dir, dentry, NULL);
  3004. if (!ep)
  3005. return -EIO;
  3006. #ifdef CONFIG_SDFAT_CHECK_RO_ATTR
  3007. if (SDFAT_SB(sb)->fsi.fs_func->get_entry_attr(ep) & ATTR_READONLY)
  3008. return -EPERM;
  3009. #endif
  3010. clu_to_free.dir = fid->start_clu;
  3011. clu_to_free.size = ((fid->size-1) >> fsi->cluster_size_bits) + 1;
  3012. clu_to_free.flags = fid->flags;
  3013. ret = check_dir_empty(sb, &clu_to_free);
  3014. if (ret) {
  3015. if (ret == -EIO)
  3016. EMSG("%s : failed to check_dir_empty : err(%d)\n",
  3017. __func__, ret);
  3018. return ret;
  3019. }
  3020. fs_set_vol_flags(sb, VOL_DIRTY);
  3021. /* (1) update the directory entry */
  3022. ret = remove_file(inode, &dir, dentry);
  3023. if (ret) {
  3024. EMSG("%s : failed to remove_file : err(%d)\n", __func__, ret);
  3025. return ret;
  3026. }
  3027. fid->dir.dir = DIR_DELETED;
  3028. fs_sync(sb, 0);
  3029. fs_set_vol_flags(sb, VOL_CLEAN);
  3030. return ret;
  3031. } /* end of fscore_rmdir */
  3032. /* end of core.c */