exfat.c 119 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052
  1. /* Some of the source code in this file came from "linux/fs/fat/misc.c". */
  2. /*
  3. * linux/fs/fat/misc.c
  4. *
  5. * Written 1992,1993 by Werner Almesberger
  6. * 22/11/2000 - Fixed fat_date_unix2dos for dates earlier than 01/01/1980
  7. * and date_dos2unix for date==0 by Igor Zhbanov(bsg@uniyar.ac.ru)
  8. */
  9. /*
  10. * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version 2
  15. * of the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  25. */
  26. #include <linux/version.h>
  27. #include "exfat_config.h"
  28. #include "exfat_global.h"
  29. #include "exfat_data.h"
  30. #include "exfat_oal.h"
  31. #include "exfat_blkdev.h"
  32. #include "exfat_cache.h"
  33. #include "exfat_nls.h"
  34. #include "exfat_api.h"
  35. #include "exfat_super.h"
  36. #include "exfat.h"
  37. #include <linux/blkdev.h>
  38. #define THERE_IS_MBR 0
  39. #if (THERE_IS_MBR == 1)
  40. #include "exfat_part.h"
  41. #endif
  42. #define DELAYED_SYNC 0
  43. #define ELAPSED_TIME 0
  44. #if (ELAPSED_TIME == 1)
  45. #include <linux/time.h>
  46. static UINT32 __t1, __t2;
  47. static UINT32 get_current_msec(void)
  48. {
  49. struct timeval tm;
  50. do_gettimeofday(&tm);
  51. return (UINT32)(tm.tv_sec*1000000 + tm.tv_usec);
  52. }
  53. #define TIME_START() do {__t1 = get_current_msec(); } while (0)
  54. #define TIME_END() do {__t2 = get_current_msec(); } while (0)
  55. #define PRINT_TIME(n) do {printk("[EXFAT] Elapsed time %d = %d (usec)\n", n, (__t2 - __t1)); } while (0)
  56. #else
  57. #define TIME_START()
  58. #define TIME_END()
  59. #define PRINT_TIME(n)
  60. #endif
  61. static void __set_sb_dirty(struct super_block *sb)
  62. {
  63. #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
  64. sb->s_dirt = 1;
  65. #else
  66. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  67. sbi->s_dirt = 1;
  68. #endif
  69. }
  70. extern UINT8 uni_upcase[];
  71. static UINT8 name_buf[MAX_PATH_LENGTH *MAX_CHARSET_SIZE];
  72. static INT8 *reserved_names[] = {
  73. "AUX ", "CON ", "NUL ", "PRN ",
  74. "COM1 ", "COM2 ", "COM3 ", "COM4 ",
  75. "COM5 ", "COM6 ", "COM7 ", "COM8 ", "COM9 ",
  76. "LPT1 ", "LPT2 ", "LPT3 ", "LPT4 ",
  77. "LPT5 ", "LPT6 ", "LPT7 ", "LPT8 ", "LPT9 ",
  78. NULL
  79. };
  80. static UINT8 free_bit[] = {
  81. 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2,
  82. 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3,
  83. 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,
  84. 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
  85. 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2,
  86. 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3,
  87. 0, 1, 0, 2, 0, 1, 0, 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,
  88. 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
  89. 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2,
  90. 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3,
  91. 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,
  92. 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
  93. 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
  94. };
  95. static UINT8 used_bit[] = {
  96. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3,
  97. 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4,
  98. 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5,
  99. 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  100. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4,
  101. 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6,
  102. 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4,
  103. 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  104. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5,
  105. 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5,
  106. 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6,
  107. 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  108. 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
  109. };
  110. INT32 ffsInit(void)
  111. {
  112. INT32 ret;
  113. ret = bdev_init();
  114. if (ret)
  115. return ret;
  116. ret = fs_init();
  117. if (ret)
  118. return ret;
  119. return FFS_SUCCESS;
  120. }
  121. INT32 ffsShutdown(void)
  122. {
  123. INT32 ret;
  124. ret = fs_shutdown();
  125. if (ret)
  126. return ret;
  127. ret = bdev_shutdown();
  128. if (ret)
  129. return ret;
  130. return FFS_SUCCESS;
  131. }
  132. INT32 ffsMountVol(struct super_block *sb, INT32 drv)
  133. {
  134. INT32 i, ret;
  135. #if (THERE_IS_MBR == 1)
  136. MBR_SECTOR_T *p_mbr;
  137. PART_ENTRY_T *p_pte;
  138. #endif
  139. PBR_SECTOR_T *p_pbr;
  140. struct buffer_head *tmp_bh = NULL;
  141. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  142. BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
  143. p_fs->drv = drv;
  144. p_fs->dev_ejected = FALSE;
  145. if (bdev_open(sb))
  146. return FFS_MEDIAERR;
  147. if (p_bd->sector_size < sb->s_blocksize)
  148. return FFS_MEDIAERR;
  149. if (p_bd->sector_size > sb->s_blocksize)
  150. sb_set_blocksize(sb, p_bd->sector_size);
  151. if (sector_read(sb, 0, &tmp_bh, 1) != FFS_SUCCESS)
  152. return FFS_MEDIAERR;
  153. #if (THERE_IS_MBR == 1)
  154. if (buf[0] != 0xEB) {
  155. p_mbr = (MBR_SECTOR_T *) tmp_bh->b_data;
  156. if (GET16_A(p_mbr->signature) != MBR_SIGNATURE) {
  157. brelse(tmp_bh);
  158. bdev_close(sb);
  159. return FFS_FORMATERR;
  160. }
  161. p_pte = (PART_ENTRY_T *) p_mbr->partition + 0;
  162. p_fs->PBR_sector = GET32(p_pte->start_sector);
  163. p_fs->num_sectors = GET32(p_pte->num_sectors);
  164. if (p_fs->num_sectors == 0) {
  165. brelse(tmp_bh);
  166. bdev_close(sb);
  167. return FFS_ERROR;
  168. }
  169. if (sector_read(sb, p_fs->PBR_sector, &tmp_bh, 1) != FFS_SUCCESS) {
  170. bdev_close(sb);
  171. return FFS_MEDIAERR;
  172. }
  173. } else {
  174. #endif
  175. p_fs->PBR_sector = 0;
  176. #if (THERE_IS_MBR == 1)
  177. }
  178. #endif
  179. p_pbr = (PBR_SECTOR_T *) tmp_bh->b_data;
  180. if (GET16_A(p_pbr->signature) != PBR_SIGNATURE) {
  181. brelse(tmp_bh);
  182. bdev_close(sb);
  183. return FFS_FORMATERR;
  184. }
  185. for (i = 0; i < 53; i++)
  186. if (p_pbr->bpb[i])
  187. break;
  188. ret = FFS_FORMATERR;
  189. if (i >= 53)
  190. ret = exfat_mount(sb, p_pbr);
  191. brelse(tmp_bh);
  192. if (ret) {
  193. bdev_close(sb);
  194. return ret;
  195. }
  196. if (p_fs->vol_type == EXFAT) {
  197. ret = load_alloc_bitmap(sb);
  198. if (ret) {
  199. bdev_close(sb);
  200. return ret;
  201. }
  202. ret = load_upcase_table(sb);
  203. if (ret) {
  204. free_alloc_bitmap(sb);
  205. bdev_close(sb);
  206. return ret;
  207. }
  208. }
  209. if (p_fs->dev_ejected) {
  210. if (p_fs->vol_type == EXFAT) {
  211. free_upcase_table(sb);
  212. free_alloc_bitmap(sb);
  213. }
  214. bdev_close(sb);
  215. return FFS_MEDIAERR;
  216. }
  217. return FFS_SUCCESS;
  218. }
  219. INT32 ffsUmountVol(struct super_block *sb)
  220. {
  221. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  222. fs_sync(sb, 0);
  223. fs_set_vol_flags(sb, VOL_CLEAN);
  224. if (p_fs->vol_type == EXFAT) {
  225. free_upcase_table(sb);
  226. free_alloc_bitmap(sb);
  227. }
  228. FAT_release_all(sb);
  229. buf_release_all(sb);
  230. bdev_close(sb);
  231. if (p_fs->dev_ejected)
  232. return FFS_MEDIAERR;
  233. return FFS_SUCCESS;
  234. }
  235. INT32 ffsGetVolInfo(struct super_block *sb, VOL_INFO_T *info)
  236. {
  237. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  238. if (p_fs->used_clusters == (UINT32) ~0)
  239. p_fs->used_clusters = p_fs->fs_func->count_used_clusters(sb);
  240. info->FatType = p_fs->vol_type;
  241. info->ClusterSize = p_fs->cluster_size;
  242. info->NumClusters = p_fs->num_clusters - 2;
  243. info->UsedClusters = p_fs->used_clusters;
  244. info->FreeClusters = info->NumClusters - info->UsedClusters;
  245. if (p_fs->dev_ejected)
  246. return FFS_MEDIAERR;
  247. return FFS_SUCCESS;
  248. }
  249. INT32 ffsSyncVol(struct super_block *sb, INT32 do_sync)
  250. {
  251. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  252. fs_sync(sb, do_sync);
  253. fs_set_vol_flags(sb, VOL_CLEAN);
  254. if (p_fs->dev_ejected)
  255. return FFS_MEDIAERR;
  256. return FFS_SUCCESS;
  257. }
  258. INT32 ffsLookupFile(struct inode *inode, UINT8 *path, FILE_ID_T *fid)
  259. {
  260. INT32 ret, dentry, num_entries;
  261. CHAIN_T dir;
  262. UNI_NAME_T uni_name;
  263. DOS_NAME_T dos_name;
  264. DENTRY_T *ep, *ep2;
  265. ENTRY_SET_CACHE_T *es=NULL;
  266. struct super_block *sb = inode->i_sb;
  267. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  268. PRINTK("ffsLookupFile entered\n");
  269. ret = resolve_path(inode, path, &dir, &uni_name);
  270. if (ret)
  271. return ret;
  272. ret = get_num_entries_and_dos_name(sb, &dir, &uni_name, &num_entries, &dos_name);
  273. if (ret)
  274. return ret;
  275. dentry = p_fs->fs_func->find_dir_entry(sb, &dir, &uni_name, num_entries, &dos_name, TYPE_ALL);
  276. if (dentry < -1)
  277. return FFS_NOTFOUND;
  278. fid->dir.dir = dir.dir;
  279. fid->dir.size = dir.size;
  280. fid->dir.flags = dir.flags;
  281. fid->entry = dentry;
  282. if (dentry == -1) {
  283. fid->type = TYPE_DIR;
  284. fid->rwoffset = 0;
  285. fid->hint_last_off = -1;
  286. fid->attr = ATTR_SUBDIR;
  287. fid->flags = 0x01;
  288. fid->size = 0;
  289. fid->start_clu = p_fs->root_dir;
  290. } else {
  291. if (p_fs->vol_type == EXFAT) {
  292. es = get_entry_set_in_dir(sb, &dir, dentry, ES_2_ENTRIES, &ep);
  293. if (!es)
  294. return FFS_MEDIAERR;
  295. ep2 = ep+1;
  296. } else {
  297. ep = get_entry_in_dir(sb, &dir, dentry, NULL);
  298. if (!ep)
  299. return FFS_MEDIAERR;
  300. ep2 = ep;
  301. }
  302. fid->type = p_fs->fs_func->get_entry_type(ep);
  303. fid->rwoffset = 0;
  304. fid->hint_last_off = -1;
  305. fid->attr = p_fs->fs_func->get_entry_attr(ep);
  306. fid->size = p_fs->fs_func->get_entry_size(ep2);
  307. if ((fid->type == TYPE_FILE) && (fid->size == 0)) {
  308. fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
  309. fid->start_clu = CLUSTER_32(~0);
  310. } else {
  311. fid->flags = p_fs->fs_func->get_entry_flag(ep2);
  312. fid->start_clu = p_fs->fs_func->get_entry_clu0(ep2);
  313. }
  314. if (p_fs->vol_type == EXFAT)
  315. release_entry_set(es);
  316. }
  317. if (p_fs->dev_ejected)
  318. return FFS_MEDIAERR;
  319. PRINTK("ffsLookupFile exited successfully\n");
  320. return FFS_SUCCESS;
  321. }
  322. INT32 ffsCreateFile(struct inode *inode, UINT8 *path, UINT8 mode, FILE_ID_T *fid)
  323. {
  324. INT32 ret;
  325. CHAIN_T dir;
  326. UNI_NAME_T uni_name;
  327. struct super_block *sb = inode->i_sb;
  328. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  329. ret = resolve_path(inode, path, &dir, &uni_name);
  330. if (ret)
  331. return ret;
  332. fs_set_vol_flags(sb, VOL_DIRTY);
  333. ret = create_file(inode, &dir, &uni_name, mode, fid);
  334. #if (DELAYED_SYNC == 0)
  335. fs_sync(sb, 0);
  336. fs_set_vol_flags(sb, VOL_CLEAN);
  337. #endif
  338. if (p_fs->dev_ejected)
  339. return FFS_MEDIAERR;
  340. return ret;
  341. }
  342. INT32 ffsReadFile(struct inode *inode, FILE_ID_T *fid, void *buffer, UINT64 count, UINT64 *rcount)
  343. {
  344. INT32 offset, sec_offset, clu_offset;
  345. UINT32 clu, LogSector;
  346. UINT64 oneblkread, read_bytes;
  347. struct buffer_head *tmp_bh = NULL;
  348. struct super_block *sb = inode->i_sb;
  349. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  350. BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
  351. if (fid->type != TYPE_FILE)
  352. return FFS_PERMISSIONERR;
  353. if (fid->rwoffset > fid->size)
  354. fid->rwoffset = fid->size;
  355. if (count > (fid->size - fid->rwoffset))
  356. count = fid->size - fid->rwoffset;
  357. if (count == 0) {
  358. if (rcount != NULL)
  359. *rcount = 0;
  360. return FFS_EOF;
  361. }
  362. read_bytes = 0;
  363. while (count > 0) {
  364. clu_offset = (INT32)(fid->rwoffset >> p_fs->cluster_size_bits);
  365. clu = fid->start_clu;
  366. if (fid->flags == 0x03) {
  367. clu += clu_offset;
  368. } else {
  369. if ((clu_offset > 0) && (fid->hint_last_off > 0) &&
  370. (clu_offset >= fid->hint_last_off)) {
  371. clu_offset -= fid->hint_last_off;
  372. clu = fid->hint_last_clu;
  373. }
  374. while (clu_offset > 0) {
  375. if (FAT_read(sb, clu, &clu) == -1)
  376. return FFS_MEDIAERR;
  377. clu_offset--;
  378. }
  379. }
  380. fid->hint_last_off = (INT32)(fid->rwoffset >> p_fs->cluster_size_bits);
  381. fid->hint_last_clu = clu;
  382. offset = (INT32)(fid->rwoffset & (p_fs->cluster_size-1));
  383. sec_offset = offset >> p_bd->sector_size_bits;
  384. offset &= p_bd->sector_size_mask;
  385. LogSector = START_SECTOR(clu) + sec_offset;
  386. oneblkread = (UINT64)(p_bd->sector_size - offset);
  387. if (oneblkread > count)
  388. oneblkread = count;
  389. if ((offset == 0) && (oneblkread == p_bd->sector_size)) {
  390. if (sector_read(sb, LogSector, &tmp_bh, 1) != FFS_SUCCESS)
  391. goto err_out;
  392. MEMCPY(((INT8 *) buffer)+read_bytes, ((INT8 *) tmp_bh->b_data), (INT32) oneblkread);
  393. } else {
  394. if (sector_read(sb, LogSector, &tmp_bh, 1) != FFS_SUCCESS)
  395. goto err_out;
  396. MEMCPY(((INT8 *) buffer)+read_bytes, ((INT8 *) tmp_bh->b_data)+offset, (INT32) oneblkread);
  397. }
  398. count -= oneblkread;
  399. read_bytes += oneblkread;
  400. fid->rwoffset += oneblkread;
  401. }
  402. brelse(tmp_bh);
  403. err_out:
  404. if (rcount != NULL)
  405. *rcount = read_bytes;
  406. if (p_fs->dev_ejected)
  407. return FFS_MEDIAERR;
  408. return FFS_SUCCESS;
  409. }
  410. INT32 ffsWriteFile(struct inode *inode, FILE_ID_T *fid, void *buffer, UINT64 count, UINT64 *wcount)
  411. {
  412. INT32 modified = FALSE, offset, sec_offset, clu_offset;
  413. INT32 num_clusters, num_alloc, num_alloced = (INT32) ~0;
  414. UINT32 clu, last_clu, LogSector, sector;
  415. UINT64 oneblkwrite, write_bytes;
  416. CHAIN_T new_clu;
  417. TIMESTAMP_T tm;
  418. DENTRY_T *ep, *ep2;
  419. ENTRY_SET_CACHE_T *es = NULL;
  420. struct buffer_head *tmp_bh = NULL;
  421. struct super_block *sb = inode->i_sb;
  422. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  423. BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
  424. UINT8 tz_utc = EXFAT_SB(sb)->options.tz_utc;
  425. if (fid->type != TYPE_FILE)
  426. return FFS_PERMISSIONERR;
  427. if (fid->rwoffset > fid->size)
  428. fid->rwoffset = fid->size;
  429. if (count == 0) {
  430. if (wcount != NULL)
  431. *wcount = 0;
  432. return FFS_SUCCESS;
  433. }
  434. fs_set_vol_flags(sb, VOL_DIRTY);
  435. if (fid->size == 0)
  436. num_clusters = 0;
  437. else
  438. num_clusters = (INT32)((fid->size-1) >> p_fs->cluster_size_bits) + 1;
  439. write_bytes = 0;
  440. while (count > 0) {
  441. clu_offset = (INT32)(fid->rwoffset >> p_fs->cluster_size_bits);
  442. clu = last_clu = fid->start_clu;
  443. if (fid->flags == 0x03) {
  444. if ((clu_offset > 0) && (clu != CLUSTER_32(~0))) {
  445. last_clu += clu_offset - 1;
  446. if (clu_offset == num_clusters)
  447. clu = CLUSTER_32(~0);
  448. else
  449. clu += clu_offset;
  450. }
  451. } else {
  452. if ((clu_offset > 0) && (fid->hint_last_off > 0) &&
  453. (clu_offset >= fid->hint_last_off)) {
  454. clu_offset -= fid->hint_last_off;
  455. clu = fid->hint_last_clu;
  456. }
  457. while ((clu_offset > 0) && (clu != CLUSTER_32(~0))) {
  458. last_clu = clu;
  459. if (FAT_read(sb, clu, &clu) == -1)
  460. return FFS_MEDIAERR;
  461. clu_offset--;
  462. }
  463. }
  464. if (clu == CLUSTER_32(~0)) {
  465. num_alloc = (INT32)((count-1) >> p_fs->cluster_size_bits) + 1;
  466. new_clu.dir = (last_clu == CLUSTER_32(~0)) ? CLUSTER_32(~0) : last_clu+1;
  467. new_clu.size = 0;
  468. new_clu.flags = fid->flags;
  469. num_alloced = p_fs->fs_func->alloc_cluster(sb, num_alloc, &new_clu);
  470. if (num_alloced == 0)
  471. break;
  472. else if (num_alloced < 0)
  473. return FFS_MEDIAERR;
  474. if (last_clu == CLUSTER_32(~0)) {
  475. if (new_clu.flags == 0x01)
  476. fid->flags = 0x01;
  477. fid->start_clu = new_clu.dir;
  478. modified = TRUE;
  479. } else {
  480. if (new_clu.flags != fid->flags) {
  481. exfat_chain_cont_cluster(sb, fid->start_clu, num_clusters);
  482. fid->flags = 0x01;
  483. modified = TRUE;
  484. }
  485. if (new_clu.flags == 0x01)
  486. FAT_write(sb, last_clu, new_clu.dir);
  487. }
  488. num_clusters += num_alloced;
  489. clu = new_clu.dir;
  490. }
  491. fid->hint_last_off = (INT32)(fid->rwoffset >> p_fs->cluster_size_bits);
  492. fid->hint_last_clu = clu;
  493. offset = (INT32)(fid->rwoffset & (p_fs->cluster_size-1));
  494. sec_offset = offset >> p_bd->sector_size_bits;
  495. offset &= p_bd->sector_size_mask;
  496. LogSector = START_SECTOR(clu) + sec_offset;
  497. oneblkwrite = (UINT64)(p_bd->sector_size - offset);
  498. if (oneblkwrite > count)
  499. oneblkwrite = count;
  500. if ((offset == 0) && (oneblkwrite == p_bd->sector_size)) {
  501. if (sector_read(sb, LogSector, &tmp_bh, 0) != FFS_SUCCESS)
  502. goto err_out;
  503. MEMCPY(((INT8 *) tmp_bh->b_data), ((INT8 *) buffer)+write_bytes, (INT32) oneblkwrite);
  504. if (sector_write(sb, LogSector, tmp_bh, 0) != FFS_SUCCESS) {
  505. brelse(tmp_bh);
  506. goto err_out;
  507. }
  508. } else {
  509. if ((offset > 0) || ((fid->rwoffset+oneblkwrite) < fid->size)) {
  510. if (sector_read(sb, LogSector, &tmp_bh, 1) != FFS_SUCCESS)
  511. goto err_out;
  512. } else {
  513. if (sector_read(sb, LogSector, &tmp_bh, 0) != FFS_SUCCESS)
  514. goto err_out;
  515. }
  516. MEMCPY(((INT8 *) tmp_bh->b_data)+offset, ((INT8 *) buffer)+write_bytes, (INT32) oneblkwrite);
  517. if (sector_write(sb, LogSector, tmp_bh, 0) != FFS_SUCCESS) {
  518. brelse(tmp_bh);
  519. goto err_out;
  520. }
  521. }
  522. count -= oneblkwrite;
  523. write_bytes += oneblkwrite;
  524. fid->rwoffset += oneblkwrite;
  525. fid->attr |= ATTR_ARCHIVE;
  526. if (fid->size < fid->rwoffset) {
  527. fid->size = fid->rwoffset;
  528. modified = TRUE;
  529. }
  530. }
  531. brelse(tmp_bh);
  532. if (p_fs->vol_type == EXFAT) {
  533. es = get_entry_set_in_dir(sb, &(fid->dir), fid->entry, ES_ALL_ENTRIES, &ep);
  534. if (es == NULL)
  535. goto err_out;
  536. ep2 = ep+1;
  537. } else {
  538. ep = get_entry_in_dir(sb, &(fid->dir), fid->entry, &sector);
  539. if (!ep)
  540. goto err_out;
  541. ep2 = ep;
  542. }
  543. p_fs->fs_func->set_entry_time(ep, tm_current(&tm, tz_utc), TM_MODIFY);
  544. p_fs->fs_func->set_entry_attr(ep, fid->attr);
  545. if (p_fs->vol_type != EXFAT)
  546. buf_modify(sb, sector);
  547. if (modified) {
  548. if (p_fs->fs_func->get_entry_flag(ep2) != fid->flags)
  549. p_fs->fs_func->set_entry_flag(ep2, fid->flags);
  550. if (p_fs->fs_func->get_entry_size(ep2) != fid->size)
  551. p_fs->fs_func->set_entry_size(ep2, fid->size);
  552. if (p_fs->fs_func->get_entry_clu0(ep2) != fid->start_clu)
  553. p_fs->fs_func->set_entry_clu0(ep2, fid->start_clu);
  554. if (p_fs->vol_type != EXFAT)
  555. buf_modify(sb, sector);
  556. }
  557. if (p_fs->vol_type == EXFAT) {
  558. update_dir_checksum_with_entry_set(sb, es);
  559. release_entry_set(es);
  560. }
  561. #if (DELAYED_SYNC == 0)
  562. fs_sync(sb, 0);
  563. fs_set_vol_flags(sb, VOL_CLEAN);
  564. #endif
  565. err_out:
  566. if (wcount != NULL)
  567. *wcount = write_bytes;
  568. if (num_alloced == 0)
  569. return FFS_FULL;
  570. if (p_fs->dev_ejected)
  571. return FFS_MEDIAERR;
  572. return FFS_SUCCESS;
  573. }
  574. INT32 ffsTruncateFile(struct inode *inode, UINT64 old_size, UINT64 new_size)
  575. {
  576. INT32 num_clusters;
  577. UINT32 last_clu = CLUSTER_32(0), sector;
  578. CHAIN_T clu;
  579. TIMESTAMP_T tm;
  580. DENTRY_T *ep, *ep2;
  581. struct super_block *sb = inode->i_sb;
  582. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  583. FILE_ID_T *fid = &(EXFAT_I(inode)->fid);
  584. ENTRY_SET_CACHE_T *es=NULL;
  585. UINT8 tz_utc = EXFAT_SB(sb)->options.tz_utc;
  586. if (fid->type != TYPE_FILE)
  587. return FFS_PERMISSIONERR;
  588. if (fid->size != old_size) {
  589. printk(KERN_ERR "[EXFAT] truncate : can't skip it because of "
  590. "size-mismatch(old:%lld->fid:%lld).\n"
  591. ,old_size, fid->size);
  592. }
  593. if (old_size <= new_size)
  594. return FFS_SUCCESS;
  595. fs_set_vol_flags(sb, VOL_DIRTY);
  596. clu.dir = fid->start_clu;
  597. clu.size = (INT32)((old_size-1) >> p_fs->cluster_size_bits) + 1;
  598. clu.flags = fid->flags;
  599. if (new_size > 0) {
  600. num_clusters = (INT32)((new_size-1) >> p_fs->cluster_size_bits) + 1;
  601. if (clu.flags == 0x03) {
  602. clu.dir += num_clusters;
  603. } else {
  604. while (num_clusters > 0) {
  605. last_clu = clu.dir;
  606. if (FAT_read(sb, clu.dir, &(clu.dir)) == -1)
  607. return FFS_MEDIAERR;
  608. num_clusters--;
  609. }
  610. }
  611. clu.size -= num_clusters;
  612. }
  613. fid->size = new_size;
  614. fid->attr |= ATTR_ARCHIVE;
  615. if (new_size == 0) {
  616. fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
  617. fid->start_clu = CLUSTER_32(~0);
  618. }
  619. if (fid->dir.dir != DIR_DELETED) {
  620. if (p_fs->vol_type == EXFAT) {
  621. es = get_entry_set_in_dir(sb, &(fid->dir), fid->entry, ES_ALL_ENTRIES, &ep);
  622. if (es == NULL)
  623. return FFS_MEDIAERR;
  624. ep2 = ep+1;
  625. } else {
  626. ep = get_entry_in_dir(sb, &(fid->dir), fid->entry, &sector);
  627. if (!ep)
  628. return FFS_MEDIAERR;
  629. ep2 = ep;
  630. }
  631. p_fs->fs_func->set_entry_time(ep, tm_current(&tm, tz_utc), TM_MODIFY);
  632. p_fs->fs_func->set_entry_attr(ep, fid->attr);
  633. p_fs->fs_func->set_entry_size(ep2, new_size);
  634. if (new_size == 0) {
  635. p_fs->fs_func->set_entry_flag(ep2, 0x01);
  636. p_fs->fs_func->set_entry_clu0(ep2, CLUSTER_32(0));
  637. }
  638. if (p_fs->vol_type != EXFAT)
  639. buf_modify(sb, sector);
  640. else {
  641. update_dir_checksum_with_entry_set(sb, es);
  642. release_entry_set(es);
  643. }
  644. }
  645. if (last_clu != CLUSTER_32(0)) {
  646. if (fid->flags == 0x01)
  647. FAT_write(sb, last_clu, CLUSTER_32(~0));
  648. }
  649. p_fs->fs_func->free_cluster(sb, &clu, 0);
  650. fid->hint_last_off = -1;
  651. if (fid->rwoffset > fid->size) {
  652. fid->rwoffset = fid->size;
  653. }
  654. #if (DELAYED_SYNC == 0)
  655. fs_sync(sb, 0);
  656. fs_set_vol_flags(sb, VOL_CLEAN);
  657. #endif
  658. if (p_fs->dev_ejected)
  659. return FFS_MEDIAERR;
  660. return FFS_SUCCESS;
  661. }
  662. static void update_parent_info( FILE_ID_T *fid, struct inode *parent_inode)
  663. {
  664. FS_INFO_T *p_fs = &(EXFAT_SB(parent_inode->i_sb)->fs_info);
  665. FILE_ID_T *parent_fid = &(EXFAT_I(parent_inode)->fid);
  666. if (unlikely((parent_fid->flags != fid->dir.flags)
  667. || (parent_fid->size != (fid->dir.size<<p_fs->cluster_size_bits))
  668. || (parent_fid->start_clu != fid->dir.dir))) {
  669. fid->dir.dir = parent_fid->start_clu;
  670. fid->dir.flags = parent_fid->flags;
  671. fid->dir.size = ((parent_fid->size + (p_fs->cluster_size-1))
  672. >> p_fs->cluster_size_bits);
  673. }
  674. }
  675. INT32 ffsMoveFile(struct inode *old_parent_inode, FILE_ID_T *fid, struct inode *new_parent_inode, struct dentry *new_dentry)
  676. {
  677. INT32 ret;
  678. INT32 dentry;
  679. CHAIN_T olddir, newdir;
  680. CHAIN_T *p_dir=NULL;
  681. UNI_NAME_T uni_name;
  682. DENTRY_T *ep;
  683. struct super_block *sb = old_parent_inode->i_sb;
  684. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  685. UINT8 *new_path = (UINT8 *) new_dentry->d_name.name;
  686. struct inode *new_inode = new_dentry->d_inode;
  687. int num_entries;
  688. FILE_ID_T *new_fid = NULL;
  689. UINT32 new_entry_type = TYPE_UNUSED;
  690. INT32 new_entry=0;
  691. if ((new_path == NULL) || (STRLEN(new_path) == 0))
  692. return FFS_ERROR;
  693. update_parent_info(fid, old_parent_inode);
  694. olddir.dir = fid->dir.dir;
  695. olddir.size = fid->dir.size;
  696. olddir.flags = fid->dir.flags;
  697. dentry = fid->entry;
  698. if (p_fs->vol_type != EXFAT) {
  699. if ((olddir.dir != p_fs->root_dir) && (dentry < 2))
  700. return FFS_PERMISSIONERR;
  701. }
  702. ep = get_entry_in_dir(sb, &olddir, dentry, NULL);
  703. if (!ep)
  704. return FFS_MEDIAERR;
  705. if (p_fs->fs_func->get_entry_attr(ep) & ATTR_READONLY)
  706. return FFS_PERMISSIONERR;
  707. if (new_inode) {
  708. ret = FFS_MEDIAERR;
  709. new_fid = &EXFAT_I(new_inode)->fid;
  710. update_parent_info(new_fid, new_parent_inode);
  711. p_dir = &(new_fid->dir);
  712. new_entry = new_fid->entry;
  713. ep = get_entry_in_dir(sb, p_dir, new_entry, NULL);
  714. if (!ep)
  715. goto out;
  716. new_entry_type = p_fs->fs_func->get_entry_type(ep);
  717. if (new_entry_type == TYPE_DIR) {
  718. CHAIN_T new_clu;
  719. new_clu.dir = new_fid->start_clu;
  720. new_clu.size = (INT32)((new_fid->size-1) >> p_fs->cluster_size_bits) + 1;
  721. new_clu.flags = new_fid->flags;
  722. if (!is_dir_empty(sb, &new_clu))
  723. return FFS_FILEEXIST;
  724. }
  725. }
  726. if (STRLEN(new_path) >= MAX_NAME_LENGTH)
  727. return FFS_NAMETOOLONG;
  728. ret = resolve_path(new_parent_inode, new_path, &newdir, &uni_name);
  729. if (ret)
  730. return ret;
  731. fs_set_vol_flags(sb, VOL_DIRTY);
  732. if (olddir.dir == newdir.dir)
  733. ret = rename_file(new_parent_inode, &olddir, dentry, &uni_name, fid);
  734. else
  735. ret = move_file(new_parent_inode, &olddir, dentry, &newdir, &uni_name, fid);
  736. if ((ret == FFS_SUCCESS) && new_inode) {
  737. ep = get_entry_in_dir(sb, p_dir, new_entry, NULL);
  738. if (!ep)
  739. goto out;
  740. num_entries = p_fs->fs_func->count_ext_entries(sb, p_dir, new_entry, ep);
  741. if (num_entries < 0)
  742. goto out;
  743. p_fs->fs_func->delete_dir_entry(sb, p_dir, new_entry, 0, num_entries+1);
  744. if (new_entry_type == TYPE_DIR) {
  745. CHAIN_T new_clu_to_free;
  746. new_clu_to_free.dir = new_fid->start_clu;
  747. new_clu_to_free.size = (INT32)((new_fid->size-1) >> p_fs->cluster_size_bits) + 1;
  748. new_clu_to_free.flags = new_fid->flags;
  749. p_fs->fs_func->free_cluster(sb, &new_clu_to_free, 1);
  750. new_fid->size = 0;
  751. new_fid->start_clu = CLUSTER_32(~0);
  752. new_fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
  753. }
  754. new_fid->dir.dir = DIR_DELETED;
  755. }
  756. out:
  757. #if (DELAYED_SYNC == 0)
  758. fs_sync(sb, 0);
  759. fs_set_vol_flags(sb, VOL_CLEAN);
  760. #endif
  761. if (p_fs->dev_ejected)
  762. return FFS_MEDIAERR;
  763. return ret;
  764. }
  765. INT32 ffsRemoveFile(struct inode *inode, FILE_ID_T *fid)
  766. {
  767. INT32 dentry;
  768. CHAIN_T dir, clu_to_free;
  769. DENTRY_T *ep;
  770. struct super_block *sb = inode->i_sb;
  771. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  772. dir.dir = fid->dir.dir;
  773. dir.size = fid->dir.size;
  774. dir.flags = fid->dir.flags;
  775. dentry = fid->entry;
  776. ep = get_entry_in_dir(sb, &dir, dentry, NULL);
  777. if (!ep)
  778. return FFS_MEDIAERR;
  779. if (p_fs->fs_func->get_entry_attr(ep) & ATTR_READONLY)
  780. return FFS_PERMISSIONERR;
  781. fs_set_vol_flags(sb, VOL_DIRTY);
  782. remove_file(inode, &dir, dentry);
  783. clu_to_free.dir = fid->start_clu;
  784. clu_to_free.size = (INT32)((fid->size-1) >> p_fs->cluster_size_bits) + 1;
  785. clu_to_free.flags = fid->flags;
  786. p_fs->fs_func->free_cluster(sb, &clu_to_free, 0);
  787. fid->size = 0;
  788. fid->start_clu = CLUSTER_32(~0);
  789. fid->flags = (p_fs->vol_type == EXFAT)? 0x03: 0x01;
  790. fid->dir.dir = DIR_DELETED;
  791. #if (DELAYED_SYNC == 0)
  792. fs_sync(sb, 0);
  793. fs_set_vol_flags(sb, VOL_CLEAN);
  794. #endif
  795. if (p_fs->dev_ejected)
  796. return FFS_MEDIAERR;
  797. return FFS_SUCCESS;
  798. }
  799. INT32 ffsSetAttr(struct inode *inode, UINT32 attr)
  800. {
  801. UINT32 type, sector;
  802. DENTRY_T *ep;
  803. struct super_block *sb = inode->i_sb;
  804. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  805. FILE_ID_T *fid = &(EXFAT_I(inode)->fid);
  806. UINT8 is_dir = (fid->type == TYPE_DIR) ? 1 : 0;
  807. ENTRY_SET_CACHE_T *es = NULL;
  808. if (fid->attr == attr) {
  809. if (p_fs->dev_ejected)
  810. return FFS_MEDIAERR;
  811. return FFS_SUCCESS;
  812. }
  813. if (fid->dir.dir == DIR_DELETED)
  814. return FFS_SUCCESS;
  815. if (is_dir) {
  816. if ((fid->dir.dir == p_fs->root_dir) &&
  817. (fid->entry == -1)) {
  818. if (p_fs->dev_ejected)
  819. return FFS_MEDIAERR;
  820. return FFS_SUCCESS;
  821. }
  822. }
  823. if (p_fs->vol_type == EXFAT) {
  824. es = get_entry_set_in_dir(sb, &(fid->dir), fid->entry, ES_ALL_ENTRIES, &ep);
  825. if (es == NULL)
  826. return FFS_MEDIAERR;
  827. } else {
  828. ep = get_entry_in_dir(sb, &(fid->dir), fid->entry, &sector);
  829. if (!ep)
  830. return FFS_MEDIAERR;
  831. }
  832. type = p_fs->fs_func->get_entry_type(ep);
  833. if (((type == TYPE_FILE) && (attr & ATTR_SUBDIR)) ||
  834. ((type == TYPE_DIR) && (!(attr & ATTR_SUBDIR)))) {
  835. INT32 err;
  836. if (p_fs->dev_ejected)
  837. err = FFS_MEDIAERR;
  838. else
  839. err = FFS_ERROR;
  840. if (p_fs->vol_type == EXFAT)
  841. release_entry_set(es);
  842. return err;
  843. }
  844. fs_set_vol_flags(sb, VOL_DIRTY);
  845. fid->attr = attr;
  846. p_fs->fs_func->set_entry_attr(ep, attr);
  847. if (p_fs->vol_type != EXFAT)
  848. buf_modify(sb, sector);
  849. else {
  850. update_dir_checksum_with_entry_set(sb, es);
  851. release_entry_set(es);
  852. }
  853. #if (DELAYED_SYNC == 0)
  854. fs_sync(sb, 0);
  855. fs_set_vol_flags(sb, VOL_CLEAN);
  856. #endif
  857. if (p_fs->dev_ejected)
  858. return FFS_MEDIAERR;
  859. return FFS_SUCCESS;
  860. }
  861. INT32 ffsGetStat(struct inode *inode, DIR_ENTRY_T *info)
  862. {
  863. UINT32 sector;
  864. INT32 count;
  865. CHAIN_T dir;
  866. UNI_NAME_T uni_name;
  867. TIMESTAMP_T tm;
  868. DENTRY_T *ep, *ep2;
  869. struct super_block *sb = inode->i_sb;
  870. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  871. FILE_ID_T *fid = &(EXFAT_I(inode)->fid);
  872. ENTRY_SET_CACHE_T *es=NULL;
  873. UINT8 is_dir = (fid->type == TYPE_DIR) ? 1 : 0;
  874. PRINTK("ffsGetStat entered\n");
  875. if (is_dir) {
  876. if ((fid->dir.dir == p_fs->root_dir) &&
  877. (fid->entry == -1)) {
  878. info->Attr = ATTR_SUBDIR;
  879. MEMSET((INT8 *) &info->CreateTimestamp, 0, sizeof(DATE_TIME_T));
  880. MEMSET((INT8 *) &info->ModifyTimestamp, 0, sizeof(DATE_TIME_T));
  881. MEMSET((INT8 *) &info->AccessTimestamp, 0, sizeof(DATE_TIME_T));
  882. STRCPY(info->ShortName, ".");
  883. STRCPY(info->Name, ".");
  884. dir.dir = p_fs->root_dir;
  885. dir.flags = 0x01;
  886. if (p_fs->root_dir == CLUSTER_32(0))
  887. info->Size = p_fs->dentries_in_root << DENTRY_SIZE_BITS;
  888. else
  889. info->Size = count_num_clusters(sb, &dir) << p_fs->cluster_size_bits;
  890. count = count_dos_name_entries(sb, &dir, TYPE_DIR);
  891. if (count < 0)
  892. return FFS_MEDIAERR;
  893. info->NumSubdirs = count;
  894. if (p_fs->dev_ejected)
  895. return FFS_MEDIAERR;
  896. return FFS_SUCCESS;
  897. }
  898. }
  899. if (p_fs->vol_type == EXFAT) {
  900. es = get_entry_set_in_dir(sb, &(fid->dir), fid->entry, ES_2_ENTRIES, &ep);
  901. if (es == NULL)
  902. return FFS_MEDIAERR;
  903. ep2 = ep+1;
  904. } else {
  905. ep = get_entry_in_dir(sb, &(fid->dir), fid->entry, &sector);
  906. if (!ep)
  907. return FFS_MEDIAERR;
  908. ep2 = ep;
  909. buf_lock(sb, sector);
  910. }
  911. info->Attr = p_fs->fs_func->get_entry_attr(ep);
  912. p_fs->fs_func->get_entry_time(ep, &tm, TM_CREATE);
  913. info->CreateTimestamp.Year = tm.year;
  914. info->CreateTimestamp.Month = tm.mon;
  915. info->CreateTimestamp.Day = tm.day;
  916. info->CreateTimestamp.Hour = tm.hour;
  917. info->CreateTimestamp.Minute = tm.min;
  918. info->CreateTimestamp.Second = tm.sec;
  919. info->CreateTimestamp.MilliSecond = 0;
  920. p_fs->fs_func->get_entry_time(ep, &tm, TM_MODIFY);
  921. info->ModifyTimestamp.Year = tm.year;
  922. info->ModifyTimestamp.Month = tm.mon;
  923. info->ModifyTimestamp.Day = tm.day;
  924. info->ModifyTimestamp.Hour = tm.hour;
  925. info->ModifyTimestamp.Minute = tm.min;
  926. info->ModifyTimestamp.Second = tm.sec;
  927. info->ModifyTimestamp.MilliSecond = 0;
  928. MEMSET((INT8 *) &info->AccessTimestamp, 0, sizeof(DATE_TIME_T));
  929. *(uni_name.name) = 0x0;
  930. p_fs->fs_func->get_uni_name_from_ext_entry(sb, &(fid->dir), fid->entry, uni_name.name);
  931. if (*(uni_name.name) == 0x0)
  932. get_uni_name_from_dos_entry(sb, (DOS_DENTRY_T *) ep, &uni_name, 0x1);
  933. nls_uniname_to_cstring(sb, info->Name, &uni_name);
  934. if (p_fs->vol_type == EXFAT) {
  935. info->NumSubdirs = 2;
  936. } else {
  937. buf_unlock(sb, sector);
  938. get_uni_name_from_dos_entry(sb, (DOS_DENTRY_T *) ep, &uni_name, 0x0);
  939. nls_uniname_to_cstring(sb, info->ShortName, &uni_name);
  940. info->NumSubdirs = 0;
  941. }
  942. info->Size = p_fs->fs_func->get_entry_size(ep2);
  943. if (p_fs->vol_type == EXFAT)
  944. release_entry_set(es);
  945. if (is_dir) {
  946. dir.dir = fid->start_clu;
  947. dir.flags = 0x01;
  948. if (info->Size == 0)
  949. info->Size = (UINT64) count_num_clusters(sb, &dir) << p_fs->cluster_size_bits;
  950. count = count_dos_name_entries(sb, &dir, TYPE_DIR);
  951. if (count < 0)
  952. return FFS_MEDIAERR;
  953. info->NumSubdirs += count;
  954. }
  955. if (p_fs->dev_ejected)
  956. return FFS_MEDIAERR;
  957. PRINTK("ffsGetStat exited successfully\n");
  958. return FFS_SUCCESS;
  959. }
  960. INT32 ffsSetStat(struct inode *inode, DIR_ENTRY_T *info)
  961. {
  962. UINT32 sector;
  963. TIMESTAMP_T tm;
  964. DENTRY_T *ep, *ep2;
  965. ENTRY_SET_CACHE_T *es=NULL;
  966. struct super_block *sb = inode->i_sb;
  967. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  968. FILE_ID_T *fid = &(EXFAT_I(inode)->fid);
  969. UINT8 is_dir = (fid->type == TYPE_DIR) ? 1 : 0;
  970. if (fid->dir.dir == DIR_DELETED)
  971. return FFS_SUCCESS;
  972. if (is_dir) {
  973. if ((fid->dir.dir == p_fs->root_dir) &&
  974. (fid->entry == -1)) {
  975. if (p_fs->dev_ejected)
  976. return FFS_MEDIAERR;
  977. return FFS_SUCCESS;
  978. }
  979. }
  980. fs_set_vol_flags(sb, VOL_DIRTY);
  981. if (p_fs->vol_type == EXFAT) {
  982. es = get_entry_set_in_dir(sb, &(fid->dir), fid->entry, ES_ALL_ENTRIES, &ep);
  983. if (es == NULL)
  984. return FFS_MEDIAERR;
  985. ep2 = ep+1;
  986. } else {
  987. ep = get_entry_in_dir(sb, &(fid->dir), fid->entry, &sector);
  988. if (!ep)
  989. return FFS_MEDIAERR;
  990. ep2 = ep;
  991. }
  992. p_fs->fs_func->set_entry_attr(ep, info->Attr);
  993. tm.sec = info->CreateTimestamp.Second;
  994. tm.min = info->CreateTimestamp.Minute;
  995. tm.hour = info->CreateTimestamp.Hour;
  996. tm.day = info->CreateTimestamp.Day;
  997. tm.mon = info->CreateTimestamp.Month;
  998. tm.year = info->CreateTimestamp.Year;
  999. p_fs->fs_func->set_entry_time(ep, &tm, TM_CREATE);
  1000. tm.sec = info->ModifyTimestamp.Second;
  1001. tm.min = info->ModifyTimestamp.Minute;
  1002. tm.hour = info->ModifyTimestamp.Hour;
  1003. tm.day = info->ModifyTimestamp.Day;
  1004. tm.mon = info->ModifyTimestamp.Month;
  1005. tm.year = info->ModifyTimestamp.Year;
  1006. p_fs->fs_func->set_entry_time(ep, &tm, TM_MODIFY);
  1007. p_fs->fs_func->set_entry_size(ep2, info->Size);
  1008. if (p_fs->vol_type != EXFAT) {
  1009. buf_modify(sb, sector);
  1010. } else {
  1011. update_dir_checksum_with_entry_set(sb, es);
  1012. release_entry_set(es);
  1013. }
  1014. if (p_fs->dev_ejected)
  1015. return FFS_MEDIAERR;
  1016. return FFS_SUCCESS;
  1017. }
  1018. INT32 ffsMapCluster(struct inode *inode, INT32 clu_offset, UINT32 *clu)
  1019. {
  1020. INT32 num_clusters, num_alloced, modified = FALSE;
  1021. UINT32 last_clu, sector;
  1022. CHAIN_T new_clu;
  1023. DENTRY_T *ep;
  1024. ENTRY_SET_CACHE_T *es = NULL;
  1025. struct super_block *sb = inode->i_sb;
  1026. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1027. FILE_ID_T *fid = &(EXFAT_I(inode)->fid);
  1028. fid->rwoffset = (INT64)(clu_offset) << p_fs->cluster_size_bits;
  1029. if (EXFAT_I(inode)->mmu_private == 0)
  1030. num_clusters = 0;
  1031. else
  1032. num_clusters = (INT32)((EXFAT_I(inode)->mmu_private-1) >> p_fs->cluster_size_bits) + 1;
  1033. *clu = last_clu = fid->start_clu;
  1034. if (fid->flags == 0x03) {
  1035. if ((clu_offset > 0) && (*clu != CLUSTER_32(~0))) {
  1036. last_clu += clu_offset - 1;
  1037. if (clu_offset == num_clusters)
  1038. *clu = CLUSTER_32(~0);
  1039. else
  1040. *clu += clu_offset;
  1041. }
  1042. } else {
  1043. if ((clu_offset > 0) && (fid->hint_last_off > 0) &&
  1044. (clu_offset >= fid->hint_last_off)) {
  1045. clu_offset -= fid->hint_last_off;
  1046. *clu = fid->hint_last_clu;
  1047. }
  1048. while ((clu_offset > 0) && (*clu != CLUSTER_32(~0))) {
  1049. last_clu = *clu;
  1050. if (FAT_read(sb, *clu, clu) == -1)
  1051. return FFS_MEDIAERR;
  1052. clu_offset--;
  1053. }
  1054. }
  1055. if (*clu == CLUSTER_32(~0)) {
  1056. fs_set_vol_flags(sb, VOL_DIRTY);
  1057. new_clu.dir = (last_clu == CLUSTER_32(~0)) ? CLUSTER_32(~0) : last_clu+1;
  1058. new_clu.size = 0;
  1059. new_clu.flags = fid->flags;
  1060. num_alloced = p_fs->fs_func->alloc_cluster(sb, 1, &new_clu);
  1061. if (num_alloced < 0)
  1062. return FFS_MEDIAERR;
  1063. else if (num_alloced == 0)
  1064. return FFS_FULL;
  1065. if (last_clu == CLUSTER_32(~0)) {
  1066. if (new_clu.flags == 0x01)
  1067. fid->flags = 0x01;
  1068. fid->start_clu = new_clu.dir;
  1069. modified = TRUE;
  1070. } else {
  1071. if (new_clu.flags != fid->flags) {
  1072. exfat_chain_cont_cluster(sb, fid->start_clu, num_clusters);
  1073. fid->flags = 0x01;
  1074. modified = TRUE;
  1075. }
  1076. if (new_clu.flags == 0x01)
  1077. FAT_write(sb, last_clu, new_clu.dir);
  1078. }
  1079. num_clusters += num_alloced;
  1080. *clu = new_clu.dir;
  1081. if (fid->dir.dir != DIR_DELETED) {
  1082. if (p_fs->vol_type == EXFAT) {
  1083. es = get_entry_set_in_dir(sb, &(fid->dir), fid->entry, ES_ALL_ENTRIES, &ep);
  1084. if (es == NULL)
  1085. return FFS_MEDIAERR;
  1086. ep++;
  1087. }
  1088. if (modified) {
  1089. if (p_fs->vol_type != EXFAT) {
  1090. ep = get_entry_in_dir(sb, &(fid->dir), fid->entry, &sector);
  1091. if (!ep)
  1092. return FFS_MEDIAERR;
  1093. }
  1094. if (p_fs->fs_func->get_entry_flag(ep) != fid->flags)
  1095. p_fs->fs_func->set_entry_flag(ep, fid->flags);
  1096. if (p_fs->fs_func->get_entry_clu0(ep) != fid->start_clu)
  1097. p_fs->fs_func->set_entry_clu0(ep, fid->start_clu);
  1098. if (p_fs->vol_type != EXFAT)
  1099. buf_modify(sb, sector);
  1100. }
  1101. if (p_fs->vol_type == EXFAT) {
  1102. update_dir_checksum_with_entry_set(sb, es);
  1103. release_entry_set(es);
  1104. }
  1105. }
  1106. inode->i_blocks += num_alloced << (p_fs->cluster_size_bits - sb->s_blocksize_bits);
  1107. }
  1108. fid->hint_last_off = (INT32)(fid->rwoffset >> p_fs->cluster_size_bits);
  1109. fid->hint_last_clu = *clu;
  1110. if (p_fs->dev_ejected)
  1111. return FFS_MEDIAERR;
  1112. return FFS_SUCCESS;
  1113. }
  1114. INT32 ffsCreateDir(struct inode *inode, UINT8 *path, FILE_ID_T *fid)
  1115. {
  1116. INT32 ret;
  1117. CHAIN_T dir;
  1118. UNI_NAME_T uni_name;
  1119. struct super_block *sb = inode->i_sb;
  1120. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1121. PRINTK("ffsCreateDir entered\n");
  1122. ret = resolve_path(inode, path, &dir, &uni_name);
  1123. if (ret)
  1124. return ret;
  1125. fs_set_vol_flags(sb, VOL_DIRTY);
  1126. ret = create_dir(inode, &dir, &uni_name, fid);
  1127. #if (DELAYED_SYNC == 0)
  1128. fs_sync(sb, 0);
  1129. fs_set_vol_flags(sb, VOL_CLEAN);
  1130. #endif
  1131. if (p_fs->dev_ejected)
  1132. return FFS_MEDIAERR;
  1133. return ret;
  1134. }
  1135. INT32 ffsReadDir(struct inode *inode, DIR_ENTRY_T *dir_entry)
  1136. {
  1137. INT32 i, dentry, clu_offset;
  1138. INT32 dentries_per_clu, dentries_per_clu_bits = 0;
  1139. UINT32 type, sector;
  1140. CHAIN_T dir, clu;
  1141. UNI_NAME_T uni_name;
  1142. TIMESTAMP_T tm;
  1143. DENTRY_T *ep;
  1144. struct super_block *sb = inode->i_sb;
  1145. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1146. FILE_ID_T *fid = &(EXFAT_I(inode)->fid);
  1147. if (fid->type != TYPE_DIR)
  1148. return FFS_PERMISSIONERR;
  1149. if (fid->entry == -1) {
  1150. dir.dir = p_fs->root_dir;
  1151. dir.flags = 0x01;
  1152. } else {
  1153. dir.dir = fid->start_clu;
  1154. dir.size = (INT32)(fid->size >> p_fs->cluster_size_bits);
  1155. dir.flags = fid->flags;
  1156. }
  1157. dentry = (INT32) fid->rwoffset;
  1158. if (dir.dir == CLUSTER_32(0)) {
  1159. dentries_per_clu = p_fs->dentries_in_root;
  1160. if (dentry == dentries_per_clu) {
  1161. clu.dir = CLUSTER_32(~0);
  1162. } else {
  1163. clu.dir = dir.dir;
  1164. clu.size = dir.size;
  1165. clu.flags = dir.flags;
  1166. }
  1167. } else {
  1168. dentries_per_clu = p_fs->dentries_per_clu;
  1169. dentries_per_clu_bits = my_log2(dentries_per_clu);
  1170. clu_offset = dentry >> dentries_per_clu_bits;
  1171. clu.dir = dir.dir;
  1172. clu.size = dir.size;
  1173. clu.flags = dir.flags;
  1174. if (clu.flags == 0x03) {
  1175. clu.dir += clu_offset;
  1176. clu.size -= clu_offset;
  1177. } else {
  1178. if ((clu_offset > 0) && (fid->hint_last_off > 0) &&
  1179. (clu_offset >= fid->hint_last_off)) {
  1180. clu_offset -= fid->hint_last_off;
  1181. clu.dir = fid->hint_last_clu;
  1182. }
  1183. while (clu_offset > 0) {
  1184. if (FAT_read(sb, clu.dir, &(clu.dir)) == -1)
  1185. return FFS_MEDIAERR;
  1186. clu_offset--;
  1187. }
  1188. }
  1189. }
  1190. while (clu.dir != CLUSTER_32(~0)) {
  1191. if (p_fs->dev_ejected)
  1192. break;
  1193. if (dir.dir == CLUSTER_32(0))
  1194. i = dentry % dentries_per_clu;
  1195. else
  1196. i = dentry & (dentries_per_clu-1);
  1197. for ( ; i < dentries_per_clu; i++, dentry++) {
  1198. ep = get_entry_in_dir(sb, &clu, i, &sector);
  1199. if (!ep)
  1200. return FFS_MEDIAERR;
  1201. type = p_fs->fs_func->get_entry_type(ep);
  1202. if (type == TYPE_UNUSED)
  1203. break;
  1204. if ((type != TYPE_FILE) && (type != TYPE_DIR))
  1205. continue;
  1206. buf_lock(sb, sector);
  1207. dir_entry->Attr = p_fs->fs_func->get_entry_attr(ep);
  1208. p_fs->fs_func->get_entry_time(ep, &tm, TM_CREATE);
  1209. dir_entry->CreateTimestamp.Year = tm.year;
  1210. dir_entry->CreateTimestamp.Month = tm.mon;
  1211. dir_entry->CreateTimestamp.Day = tm.day;
  1212. dir_entry->CreateTimestamp.Hour = tm.hour;
  1213. dir_entry->CreateTimestamp.Minute = tm.min;
  1214. dir_entry->CreateTimestamp.Second = tm.sec;
  1215. dir_entry->CreateTimestamp.MilliSecond = 0;
  1216. p_fs->fs_func->get_entry_time(ep, &tm, TM_MODIFY);
  1217. dir_entry->ModifyTimestamp.Year = tm.year;
  1218. dir_entry->ModifyTimestamp.Month = tm.mon;
  1219. dir_entry->ModifyTimestamp.Day = tm.day;
  1220. dir_entry->ModifyTimestamp.Hour = tm.hour;
  1221. dir_entry->ModifyTimestamp.Minute = tm.min;
  1222. dir_entry->ModifyTimestamp.Second = tm.sec;
  1223. dir_entry->ModifyTimestamp.MilliSecond = 0;
  1224. MEMSET((INT8 *) &dir_entry->AccessTimestamp, 0, sizeof(DATE_TIME_T));
  1225. *(uni_name.name) = 0x0;
  1226. p_fs->fs_func->get_uni_name_from_ext_entry(sb, &dir, dentry, uni_name.name);
  1227. if (*(uni_name.name) == 0x0)
  1228. get_uni_name_from_dos_entry(sb, (DOS_DENTRY_T *) ep, &uni_name, 0x1);
  1229. nls_uniname_to_cstring(sb, dir_entry->Name, &uni_name);
  1230. buf_unlock(sb, sector);
  1231. if (p_fs->vol_type == EXFAT) {
  1232. ep = get_entry_in_dir(sb, &clu, i+1, NULL);
  1233. if (!ep)
  1234. return FFS_MEDIAERR;
  1235. } else {
  1236. get_uni_name_from_dos_entry(sb, (DOS_DENTRY_T *) ep, &uni_name, 0x0);
  1237. nls_uniname_to_cstring(sb, dir_entry->ShortName, &uni_name);
  1238. }
  1239. dir_entry->Size = p_fs->fs_func->get_entry_size(ep);
  1240. if (dir.dir == CLUSTER_32(0)) {
  1241. } else {
  1242. fid->hint_last_off = dentry >> dentries_per_clu_bits;
  1243. fid->hint_last_clu = clu.dir;
  1244. }
  1245. fid->rwoffset = (INT64) ++dentry;
  1246. if (p_fs->dev_ejected)
  1247. return FFS_MEDIAERR;
  1248. return FFS_SUCCESS;
  1249. }
  1250. if (dir.dir == CLUSTER_32(0))
  1251. break;
  1252. if (clu.flags == 0x03) {
  1253. if ((--clu.size) > 0)
  1254. clu.dir++;
  1255. else
  1256. clu.dir = CLUSTER_32(~0);
  1257. } else {
  1258. if (FAT_read(sb, clu.dir, &(clu.dir)) == -1)
  1259. return FFS_MEDIAERR;
  1260. }
  1261. }
  1262. *(dir_entry->Name) = '\0';
  1263. fid->rwoffset = (INT64) ++dentry;
  1264. if (p_fs->dev_ejected)
  1265. return FFS_MEDIAERR;
  1266. return FFS_SUCCESS;
  1267. }
  1268. INT32 ffsRemoveDir(struct inode *inode, FILE_ID_T *fid)
  1269. {
  1270. INT32 dentry;
  1271. CHAIN_T dir, clu_to_free;
  1272. struct super_block *sb = inode->i_sb;
  1273. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1274. dir.dir = fid->dir.dir;
  1275. dir.size = fid->dir.size;
  1276. dir.flags = fid->dir.flags;
  1277. dentry = fid->entry;
  1278. if (p_fs->vol_type != EXFAT) {
  1279. if ((dir.dir != p_fs->root_dir) && (dentry < 2))
  1280. return FFS_PERMISSIONERR;
  1281. }
  1282. clu_to_free.dir = fid->start_clu;
  1283. clu_to_free.size = (INT32)((fid->size-1) >> p_fs->cluster_size_bits) + 1;
  1284. clu_to_free.flags = fid->flags;
  1285. if (!is_dir_empty(sb, &clu_to_free))
  1286. return FFS_FILEEXIST;
  1287. fs_set_vol_flags(sb, VOL_DIRTY);
  1288. remove_file(inode, &dir, dentry);
  1289. p_fs->fs_func->free_cluster(sb, &clu_to_free, 1);
  1290. fid->size = 0;
  1291. fid->start_clu = CLUSTER_32(~0);
  1292. fid->flags = (p_fs->vol_type == EXFAT)? 0x03: 0x01;
  1293. fid->dir.dir = DIR_DELETED;
  1294. #if (DELAYED_SYNC == 0)
  1295. fs_sync(sb, 0);
  1296. fs_set_vol_flags(sb, VOL_CLEAN);
  1297. #endif
  1298. if (p_fs->dev_ejected)
  1299. return FFS_MEDIAERR;
  1300. return FFS_SUCCESS;
  1301. }
  1302. INT32 ffsRemoveEntry(struct inode *inode, FILE_ID_T *fid)
  1303. {
  1304. INT32 dentry;
  1305. CHAIN_T dir;
  1306. DENTRY_T *ep;
  1307. struct super_block *sb = inode->i_sb;
  1308. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1309. dir.dir = fid->dir.dir;
  1310. dir.size = fid->dir.size;
  1311. dir.flags = fid->dir.flags;
  1312. dentry = fid->entry;
  1313. ep = get_entry_in_dir(sb, &dir, dentry, NULL);
  1314. if (!ep)
  1315. return FFS_MEDIAERR;
  1316. if (p_fs->fs_func->get_entry_attr(ep) & ATTR_READONLY)
  1317. return FFS_PERMISSIONERR;
  1318. fs_set_vol_flags(sb, VOL_DIRTY);
  1319. remove_file(inode, &dir, dentry);
  1320. fid->dir.dir = DIR_DELETED;
  1321. #if (DELAYED_SYNC == 0)
  1322. fs_sync(sb, 0);
  1323. fs_set_vol_flags(sb, VOL_CLEAN);
  1324. #endif
  1325. if (p_fs->dev_ejected)
  1326. return FFS_MEDIAERR;
  1327. return FFS_SUCCESS;
  1328. }
  1329. INT32 fs_init(void)
  1330. {
  1331. if (sizeof(DENTRY_T) != DENTRY_SIZE) {
  1332. return FFS_ALIGNMENTERR;
  1333. }
  1334. if (sizeof(DOS_DENTRY_T) != DENTRY_SIZE) {
  1335. return FFS_ALIGNMENTERR;
  1336. }
  1337. if (sizeof(EXT_DENTRY_T) != DENTRY_SIZE) {
  1338. return FFS_ALIGNMENTERR;
  1339. }
  1340. if (sizeof(FILE_DENTRY_T) != DENTRY_SIZE) {
  1341. return FFS_ALIGNMENTERR;
  1342. }
  1343. if (sizeof(STRM_DENTRY_T) != DENTRY_SIZE) {
  1344. return FFS_ALIGNMENTERR;
  1345. }
  1346. if (sizeof(NAME_DENTRY_T) != DENTRY_SIZE) {
  1347. return FFS_ALIGNMENTERR;
  1348. }
  1349. if (sizeof(BMAP_DENTRY_T) != DENTRY_SIZE) {
  1350. return FFS_ALIGNMENTERR;
  1351. }
  1352. if (sizeof(CASE_DENTRY_T) != DENTRY_SIZE) {
  1353. return FFS_ALIGNMENTERR;
  1354. }
  1355. if (sizeof(VOLM_DENTRY_T) != DENTRY_SIZE) {
  1356. return FFS_ALIGNMENTERR;
  1357. }
  1358. return FFS_SUCCESS;
  1359. }
  1360. INT32 fs_shutdown(void)
  1361. {
  1362. return FFS_SUCCESS;
  1363. }
  1364. void fs_set_vol_flags(struct super_block *sb, UINT32 new_flag)
  1365. {
  1366. PBR_SECTOR_T *p_pbr;
  1367. BPBEX_T *p_bpb;
  1368. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1369. if (p_fs->vol_flag == new_flag)
  1370. return;
  1371. p_fs->vol_flag = new_flag;
  1372. if (p_fs->vol_type == EXFAT) {
  1373. if (p_fs->pbr_bh == NULL) {
  1374. if (sector_read(sb, p_fs->PBR_sector, &(p_fs->pbr_bh), 1) != FFS_SUCCESS)
  1375. return;
  1376. }
  1377. p_pbr = (PBR_SECTOR_T *) p_fs->pbr_bh->b_data;
  1378. p_bpb = (BPBEX_T *) p_pbr->bpb;
  1379. SET16(p_bpb->vol_flags, (UINT16) new_flag);
  1380. if ((new_flag == VOL_DIRTY) && (!buffer_dirty(p_fs->pbr_bh)))
  1381. sector_write(sb, p_fs->PBR_sector, p_fs->pbr_bh, 1);
  1382. else
  1383. sector_write(sb, p_fs->PBR_sector, p_fs->pbr_bh, 0);
  1384. }
  1385. }
  1386. void fs_sync(struct super_block *sb, INT32 do_sync)
  1387. {
  1388. if (do_sync)
  1389. bdev_sync(sb);
  1390. }
  1391. void fs_error(struct super_block *sb)
  1392. {
  1393. struct exfat_mount_options *opts = &EXFAT_SB(sb)->options;
  1394. if (opts->errors == EXFAT_ERRORS_PANIC)
  1395. panic("[EXFAT] Filesystem panic from previous error\n");
  1396. else if ((opts->errors == EXFAT_ERRORS_RO) && !(sb->s_flags & MS_RDONLY)) {
  1397. sb->s_flags |= MS_RDONLY;
  1398. printk(KERN_ERR "[EXFAT] Filesystem has been set read-only\n");
  1399. ST_LOG("[EXFAT] Filesystem has been set read-only\n");
  1400. }
  1401. }
  1402. INT32 clear_cluster(struct super_block *sb, UINT32 clu)
  1403. {
  1404. UINT32 s, n;
  1405. INT32 ret = FFS_SUCCESS;
  1406. struct buffer_head *tmp_bh = NULL;
  1407. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1408. BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
  1409. if (clu == CLUSTER_32(0)) {
  1410. s = p_fs->root_start_sector;
  1411. n = p_fs->data_start_sector;
  1412. } else {
  1413. s = START_SECTOR(clu);
  1414. n = s + p_fs->sectors_per_clu;
  1415. }
  1416. for ( ; s < n; s++) {
  1417. if ((ret = sector_read(sb, s, &tmp_bh, 0)) != FFS_SUCCESS)
  1418. return ret;
  1419. MEMSET((INT8 *) tmp_bh->b_data, 0x0, p_bd->sector_size);
  1420. if ((ret = sector_write(sb, s, tmp_bh, 0)) !=FFS_SUCCESS)
  1421. break;
  1422. }
  1423. brelse(tmp_bh);
  1424. return ret;
  1425. }
  1426. INT32 fat_alloc_cluster(struct super_block *sb, INT32 num_alloc, CHAIN_T *p_chain)
  1427. {
  1428. INT32 i, num_clusters = 0;
  1429. UINT32 new_clu, last_clu = CLUSTER_32(~0), read_clu;
  1430. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1431. new_clu = p_chain->dir;
  1432. if (new_clu == CLUSTER_32(~0))
  1433. new_clu = p_fs->clu_srch_ptr;
  1434. else if (new_clu >= p_fs->num_clusters)
  1435. new_clu = 2;
  1436. __set_sb_dirty(sb);
  1437. p_chain->dir = CLUSTER_32(~0);
  1438. for (i = 2; i < p_fs->num_clusters; i++) {
  1439. if (FAT_read(sb, new_clu, &read_clu) != 0)
  1440. return -1;
  1441. if (read_clu == CLUSTER_32(0)) {
  1442. if(FAT_write(sb, new_clu, CLUSTER_32(~0)) < 0)
  1443. return -1;
  1444. num_clusters++;
  1445. if (p_chain->dir == CLUSTER_32(~0))
  1446. p_chain->dir = new_clu;
  1447. else {
  1448. if(FAT_write(sb, last_clu, new_clu) < 0)
  1449. return -1;
  1450. }
  1451. last_clu = new_clu;
  1452. if ((--num_alloc) == 0) {
  1453. p_fs->clu_srch_ptr = new_clu;
  1454. if (p_fs->used_clusters != (UINT32) ~0)
  1455. p_fs->used_clusters += num_clusters;
  1456. return(num_clusters);
  1457. }
  1458. }
  1459. if ((++new_clu) >= p_fs->num_clusters)
  1460. new_clu = 2;
  1461. }
  1462. p_fs->clu_srch_ptr = new_clu;
  1463. if (p_fs->used_clusters != (UINT32) ~0)
  1464. p_fs->used_clusters += num_clusters;
  1465. return(num_clusters);
  1466. }
  1467. INT32 exfat_alloc_cluster(struct super_block *sb, INT32 num_alloc, CHAIN_T *p_chain)
  1468. {
  1469. INT32 num_clusters = 0;
  1470. UINT32 hint_clu, new_clu, last_clu = CLUSTER_32(~0);
  1471. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1472. hint_clu = p_chain->dir;
  1473. if (hint_clu == CLUSTER_32(~0)) {
  1474. hint_clu = test_alloc_bitmap(sb, p_fs->clu_srch_ptr-2);
  1475. if (hint_clu == CLUSTER_32(~0))
  1476. return 0;
  1477. } else if (hint_clu >= p_fs->num_clusters) {
  1478. hint_clu = 2;
  1479. p_chain->flags = 0x01;
  1480. }
  1481. __set_sb_dirty(sb);
  1482. p_chain->dir = CLUSTER_32(~0);
  1483. while ((new_clu = test_alloc_bitmap(sb, hint_clu-2)) != CLUSTER_32(~0)) {
  1484. if (new_clu != hint_clu) {
  1485. if (p_chain->flags == 0x03) {
  1486. exfat_chain_cont_cluster(sb, p_chain->dir, num_clusters);
  1487. p_chain->flags = 0x01;
  1488. }
  1489. }
  1490. if (set_alloc_bitmap(sb, new_clu-2) != FFS_SUCCESS)
  1491. return -1;
  1492. num_clusters++;
  1493. if (p_chain->flags == 0x01) {
  1494. if(FAT_write(sb, new_clu, CLUSTER_32(~0)) < 0)
  1495. return -1;
  1496. }
  1497. if (p_chain->dir == CLUSTER_32(~0)) {
  1498. p_chain->dir = new_clu;
  1499. } else {
  1500. if (p_chain->flags == 0x01) {
  1501. if(FAT_write(sb, last_clu, new_clu) < 0)
  1502. return -1;
  1503. }
  1504. }
  1505. last_clu = new_clu;
  1506. if ((--num_alloc) == 0) {
  1507. p_fs->clu_srch_ptr = hint_clu;
  1508. if (p_fs->used_clusters != (UINT32) ~0)
  1509. p_fs->used_clusters += num_clusters;
  1510. p_chain->size += num_clusters;
  1511. return(num_clusters);
  1512. }
  1513. hint_clu = new_clu + 1;
  1514. if (hint_clu >= p_fs->num_clusters) {
  1515. hint_clu = 2;
  1516. if (p_chain->flags == 0x03) {
  1517. exfat_chain_cont_cluster(sb, p_chain->dir, num_clusters);
  1518. p_chain->flags = 0x01;
  1519. }
  1520. }
  1521. }
  1522. p_fs->clu_srch_ptr = hint_clu;
  1523. if (p_fs->used_clusters != (UINT32) ~0)
  1524. p_fs->used_clusters += num_clusters;
  1525. p_chain->size += num_clusters;
  1526. return(num_clusters);
  1527. }
  1528. void fat_free_cluster(struct super_block *sb, CHAIN_T *p_chain, INT32 do_relse)
  1529. {
  1530. INT32 num_clusters = 0;
  1531. UINT32 clu, prev;
  1532. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1533. INT32 i;
  1534. UINT32 sector;
  1535. if ((p_chain->dir == CLUSTER_32(0)) || (p_chain->dir == CLUSTER_32(~0)))
  1536. return;
  1537. __set_sb_dirty(sb);
  1538. clu = p_chain->dir;
  1539. if (p_chain->size <= 0)
  1540. return;
  1541. do {
  1542. if (p_fs->dev_ejected)
  1543. break;
  1544. if (do_relse) {
  1545. sector = START_SECTOR(clu);
  1546. for (i = 0; i < p_fs->sectors_per_clu; i++) {
  1547. buf_release(sb, sector+i);
  1548. }
  1549. }
  1550. prev = clu;
  1551. if (FAT_read(sb, clu, &clu) == -1)
  1552. break;
  1553. if (FAT_write(sb, prev, CLUSTER_32(0)) < 0)
  1554. break;
  1555. num_clusters++;
  1556. } while (clu != CLUSTER_32(~0));
  1557. if (p_fs->used_clusters != (UINT32) ~0)
  1558. p_fs->used_clusters -= num_clusters;
  1559. }
  1560. void exfat_free_cluster(struct super_block *sb, CHAIN_T *p_chain, INT32 do_relse)
  1561. {
  1562. INT32 num_clusters = 0;
  1563. UINT32 clu;
  1564. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1565. INT32 i;
  1566. UINT32 sector;
  1567. if ((p_chain->dir == CLUSTER_32(0)) || (p_chain->dir == CLUSTER_32(~0)))
  1568. return;
  1569. if (p_chain->size <= 0) {
  1570. printk(KERN_ERR "[EXFAT] free_cluster : skip free-req clu:%u, "
  1571. "because of zero-size truncation\n"
  1572. ,p_chain->dir);
  1573. return;
  1574. }
  1575. __set_sb_dirty(sb);
  1576. clu = p_chain->dir;
  1577. if (p_chain->flags == 0x03) {
  1578. do {
  1579. if (do_relse) {
  1580. sector = START_SECTOR(clu);
  1581. for (i = 0; i < p_fs->sectors_per_clu; i++) {
  1582. buf_release(sb, sector+i);
  1583. }
  1584. }
  1585. if (clr_alloc_bitmap(sb, clu-2) != FFS_SUCCESS)
  1586. break;
  1587. clu++;
  1588. num_clusters++;
  1589. } while (num_clusters < p_chain->size);
  1590. } else {
  1591. do {
  1592. if (p_fs->dev_ejected)
  1593. break;
  1594. if (do_relse) {
  1595. sector = START_SECTOR(clu);
  1596. for (i = 0; i < p_fs->sectors_per_clu; i++) {
  1597. buf_release(sb, sector+i);
  1598. }
  1599. }
  1600. if (clr_alloc_bitmap(sb, clu-2) != FFS_SUCCESS)
  1601. break;
  1602. if (FAT_read(sb, clu, &clu) == -1)
  1603. break;
  1604. num_clusters++;
  1605. } while ((clu != CLUSTER_32(0)) && (clu != CLUSTER_32(~0)));
  1606. }
  1607. if (p_fs->used_clusters != (UINT32) ~0)
  1608. p_fs->used_clusters -= num_clusters;
  1609. }
  1610. UINT32 find_last_cluster(struct super_block *sb, CHAIN_T *p_chain)
  1611. {
  1612. UINT32 clu, next;
  1613. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1614. clu = p_chain->dir;
  1615. if (p_chain->flags == 0x03) {
  1616. clu += p_chain->size - 1;
  1617. } else {
  1618. while((FAT_read(sb, clu, &next) == 0) && (next != CLUSTER_32(~0))) {
  1619. if (p_fs->dev_ejected)
  1620. break;
  1621. clu = next;
  1622. }
  1623. }
  1624. return(clu);
  1625. }
  1626. INT32 count_num_clusters(struct super_block *sb, CHAIN_T *p_chain)
  1627. {
  1628. INT32 i, count = 0;
  1629. UINT32 clu;
  1630. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1631. if ((p_chain->dir == CLUSTER_32(0)) || (p_chain->dir == CLUSTER_32(~0)))
  1632. return 0;
  1633. clu = p_chain->dir;
  1634. if (p_chain->flags == 0x03) {
  1635. count = p_chain->size;
  1636. } else {
  1637. for (i = 2; i < p_fs->num_clusters; i++) {
  1638. count++;
  1639. if (FAT_read(sb, clu, &clu) != 0)
  1640. return 0;
  1641. if (clu == CLUSTER_32(~0))
  1642. break;
  1643. }
  1644. }
  1645. return(count);
  1646. }
  1647. INT32 fat_count_used_clusters(struct super_block *sb)
  1648. {
  1649. INT32 i, count = 0;
  1650. UINT32 clu;
  1651. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1652. for (i = 2; i < p_fs->num_clusters; i++) {
  1653. if (FAT_read(sb, i, &clu) != 0)
  1654. break;
  1655. if (clu != CLUSTER_32(0))
  1656. count++;
  1657. }
  1658. return(count);
  1659. }
  1660. INT32 exfat_count_used_clusters(struct super_block *sb)
  1661. {
  1662. INT32 i, map_i, map_b, count = 0;
  1663. UINT8 k;
  1664. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1665. BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
  1666. map_i = map_b = 0;
  1667. for (i = 2; i < p_fs->num_clusters; i += 8) {
  1668. k = *(((UINT8 *) p_fs->vol_amap[map_i]->b_data) + map_b);
  1669. count += used_bit[k];
  1670. if ((++map_b) >= p_bd->sector_size) {
  1671. map_i++;
  1672. map_b = 0;
  1673. }
  1674. }
  1675. if ((p_fs->num_clusters - 2) < (UINT32)count)
  1676. count = p_fs->num_clusters - 2;
  1677. return(count);
  1678. }
  1679. void exfat_chain_cont_cluster(struct super_block *sb, UINT32 chain, INT32 len)
  1680. {
  1681. if (len == 0)
  1682. return;
  1683. while (len > 1) {
  1684. if (FAT_write(sb, chain, chain+1) < 0)
  1685. break;
  1686. chain++;
  1687. len--;
  1688. }
  1689. FAT_write(sb, chain, CLUSTER_32(~0));
  1690. }
  1691. INT32 load_alloc_bitmap(struct super_block *sb)
  1692. {
  1693. INT32 i, j, ret;
  1694. UINT32 map_size;
  1695. UINT32 type, sector;
  1696. CHAIN_T clu;
  1697. BMAP_DENTRY_T *ep;
  1698. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1699. BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
  1700. clu.dir = p_fs->root_dir;
  1701. clu.flags = 0x01;
  1702. while (clu.dir != CLUSTER_32(~0)) {
  1703. if (p_fs->dev_ejected)
  1704. break;
  1705. for (i = 0; i < p_fs->dentries_per_clu; i++) {
  1706. ep = (BMAP_DENTRY_T *) get_entry_in_dir(sb, &clu, i, NULL);
  1707. if (!ep)
  1708. return FFS_MEDIAERR;
  1709. type = p_fs->fs_func->get_entry_type((DENTRY_T *) ep);
  1710. if (type == TYPE_UNUSED)
  1711. break;
  1712. if (type != TYPE_BITMAP)
  1713. continue;
  1714. if (ep->flags == 0x0) {
  1715. p_fs->map_clu = GET32_A(ep->start_clu);
  1716. map_size = (UINT32) GET64_A(ep->size);
  1717. p_fs->map_sectors = ((map_size-1) >> p_bd->sector_size_bits) + 1;
  1718. p_fs->vol_amap = (struct buffer_head **) MALLOC(sizeof(struct buffer_head *) * p_fs->map_sectors);
  1719. if (p_fs->vol_amap == NULL)
  1720. return FFS_MEMORYERR;
  1721. sector = START_SECTOR(p_fs->map_clu);
  1722. for (j = 0; j < p_fs->map_sectors; j++) {
  1723. p_fs->vol_amap[j] = NULL;
  1724. ret = sector_read(sb, sector+j, &(p_fs->vol_amap[j]), 1);
  1725. if (ret != FFS_SUCCESS) {
  1726. i=0;
  1727. while (i < j)
  1728. brelse(p_fs->vol_amap[i++]);
  1729. FREE(p_fs->vol_amap);
  1730. p_fs->vol_amap = NULL;
  1731. return ret;
  1732. }
  1733. }
  1734. p_fs->pbr_bh = NULL;
  1735. return FFS_SUCCESS;
  1736. }
  1737. }
  1738. if (FAT_read(sb, clu.dir, &(clu.dir)) != 0)
  1739. return FFS_MEDIAERR;
  1740. }
  1741. return FFS_FORMATERR;
  1742. }
  1743. void free_alloc_bitmap(struct super_block *sb)
  1744. {
  1745. INT32 i;
  1746. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1747. brelse(p_fs->pbr_bh);
  1748. for (i = 0; i < p_fs->map_sectors; i++) {
  1749. __brelse(p_fs->vol_amap[i]);
  1750. }
  1751. FREE(p_fs->vol_amap);
  1752. p_fs->vol_amap = NULL;
  1753. }
  1754. INT32 set_alloc_bitmap(struct super_block *sb, UINT32 clu)
  1755. {
  1756. INT32 i, b;
  1757. UINT32 sector;
  1758. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1759. BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
  1760. i = clu >> (p_bd->sector_size_bits + 3);
  1761. b = clu & ((p_bd->sector_size << 3) - 1);
  1762. sector = START_SECTOR(p_fs->map_clu) + i;
  1763. Bitmap_set((UINT8 *) p_fs->vol_amap[i]->b_data, b);
  1764. return (sector_write(sb, sector, p_fs->vol_amap[i], 0));
  1765. }
  1766. INT32 clr_alloc_bitmap(struct super_block *sb, UINT32 clu)
  1767. {
  1768. INT32 i, b;
  1769. UINT32 sector;
  1770. #if EXFAT_CONFIG_DISCARD
  1771. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  1772. struct exfat_mount_options *opts = &sbi->options;
  1773. int ret;
  1774. #endif
  1775. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1776. BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
  1777. i = clu >> (p_bd->sector_size_bits + 3);
  1778. b = clu & ((p_bd->sector_size << 3) - 1);
  1779. sector = START_SECTOR(p_fs->map_clu) + i;
  1780. Bitmap_clear((UINT8 *) p_fs->vol_amap[i]->b_data, b);
  1781. return (sector_write(sb, sector, p_fs->vol_amap[i], 0));
  1782. #if EXFAT_CONFIG_DISCARD
  1783. if (opts->discard) {
  1784. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
  1785. ret = sb_issue_discard(sb, START_SECTOR(clu), (1 << p_fs->sectors_per_clu_bits));
  1786. #else
  1787. ret = sb_issue_discard(sb, START_SECTOR(clu), (1 << p_fs->sectors_per_clu_bits), GFP_NOFS, 0);
  1788. #endif
  1789. if (ret == -EOPNOTSUPP) {
  1790. printk(KERN_WARNING "discard not supported by device, disabling");
  1791. opts->discard = 0;
  1792. }
  1793. }
  1794. #endif
  1795. }
  1796. UINT32 test_alloc_bitmap(struct super_block *sb, UINT32 clu)
  1797. {
  1798. INT32 i, map_i, map_b;
  1799. UINT32 clu_base, clu_free;
  1800. UINT8 k, clu_mask;
  1801. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1802. BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
  1803. clu_base = (clu & ~(0x7)) + 2;
  1804. clu_mask = (1 << (clu - clu_base + 2)) - 1;
  1805. map_i = clu >> (p_bd->sector_size_bits + 3);
  1806. map_b = (clu >> 3) & p_bd->sector_size_mask;
  1807. for (i = 2; i < p_fs->num_clusters; i += 8) {
  1808. k = *(((UINT8 *) p_fs->vol_amap[map_i]->b_data) + map_b);
  1809. if (clu_mask > 0) {
  1810. k |= clu_mask;
  1811. clu_mask = 0;
  1812. }
  1813. if (k < 0xFF) {
  1814. clu_free = clu_base + free_bit[k];
  1815. if (clu_free < p_fs->num_clusters)
  1816. return(clu_free);
  1817. }
  1818. clu_base += 8;
  1819. if (((++map_b) >= p_bd->sector_size) || (clu_base >= p_fs->num_clusters)) {
  1820. if ((++map_i) >= p_fs->map_sectors) {
  1821. clu_base = 2;
  1822. map_i = 0;
  1823. }
  1824. map_b = 0;
  1825. }
  1826. }
  1827. return(CLUSTER_32(~0));
  1828. }
  1829. void sync_alloc_bitmap(struct super_block *sb)
  1830. {
  1831. INT32 i;
  1832. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1833. if (p_fs->vol_amap == NULL)
  1834. return;
  1835. for (i = 0; i < p_fs->map_sectors; i++) {
  1836. sync_dirty_buffer(p_fs->vol_amap[i]);
  1837. }
  1838. }
  1839. INT32 __load_upcase_table(struct super_block *sb, UINT32 sector, UINT32 num_sectors, UINT32 utbl_checksum)
  1840. {
  1841. INT32 i, ret = FFS_ERROR;
  1842. UINT32 j;
  1843. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1844. BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
  1845. struct buffer_head *tmp_bh = NULL;
  1846. UINT8 skip = FALSE;
  1847. UINT32 index = 0;
  1848. UINT16 uni = 0;
  1849. UINT16 **upcase_table;
  1850. UINT32 checksum = 0;
  1851. upcase_table = p_fs->vol_utbl = (UINT16 **) MALLOC(UTBL_COL_COUNT * sizeof(UINT16 *));
  1852. if(upcase_table == NULL)
  1853. return FFS_MEMORYERR;
  1854. MEMSET(upcase_table, 0, UTBL_COL_COUNT * sizeof(UINT16 *));
  1855. num_sectors += sector;
  1856. while(sector < num_sectors) {
  1857. ret = sector_read(sb, sector, &tmp_bh, 1);
  1858. if (ret != FFS_SUCCESS) {
  1859. PRINTK("sector read (0x%X)fail\n", sector);
  1860. goto error;
  1861. }
  1862. sector++;
  1863. for(i = 0; i < p_bd->sector_size && index <= 0xFFFF; i += 2) {
  1864. uni = GET16(((UINT8 *) tmp_bh->b_data)+i);
  1865. checksum = ((checksum & 1) ? 0x80000000 : 0 ) + (checksum >> 1) + *(((UINT8 *) tmp_bh->b_data)+i);
  1866. checksum = ((checksum & 1) ? 0x80000000 : 0 ) + (checksum >> 1) + *(((UINT8 *) tmp_bh->b_data)+(i+1));
  1867. if(skip) {
  1868. PRINTK("skip from 0x%X ", index);
  1869. index += uni;
  1870. PRINTK("to 0x%X (amount of 0x%X)\n", index, uni);
  1871. skip = FALSE;
  1872. } else if(uni == index)
  1873. index++;
  1874. else if(uni == 0xFFFF)
  1875. skip = TRUE;
  1876. else {
  1877. UINT16 col_index = get_col_index(index);
  1878. if(upcase_table[col_index]== NULL) {
  1879. PRINTK("alloc = 0x%X\n", col_index);
  1880. upcase_table[col_index] = (UINT16 *) MALLOC(UTBL_ROW_COUNT * sizeof(UINT16));
  1881. if(upcase_table[col_index] == NULL) {
  1882. ret = FFS_MEMORYERR;
  1883. goto error;
  1884. }
  1885. for(j = 0 ; j < UTBL_ROW_COUNT ; j++)
  1886. upcase_table[col_index][j] = (col_index << LOW_INDEX_BIT) | j;
  1887. }
  1888. upcase_table[col_index][get_row_index(index)] = uni;
  1889. index++;
  1890. }
  1891. }
  1892. }
  1893. if(index >= 0xFFFF && utbl_checksum == checksum) {
  1894. if(tmp_bh)
  1895. brelse(tmp_bh);
  1896. return FFS_SUCCESS;
  1897. }
  1898. ret = FFS_ERROR;
  1899. error:
  1900. if(tmp_bh)
  1901. brelse(tmp_bh);
  1902. free_upcase_table(sb);
  1903. return ret;
  1904. }
  1905. INT32 __load_default_upcase_table(struct super_block *sb)
  1906. {
  1907. INT32 i, ret = FFS_ERROR;
  1908. UINT32 j;
  1909. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1910. UINT8 skip = FALSE;
  1911. UINT32 index = 0;
  1912. UINT16 uni = 0;
  1913. UINT16 **upcase_table;
  1914. upcase_table = p_fs->vol_utbl = (UINT16 **) MALLOC(UTBL_COL_COUNT * sizeof(UINT16 *));
  1915. if(upcase_table == NULL)
  1916. return FFS_MEMORYERR;
  1917. MEMSET(upcase_table, 0, UTBL_COL_COUNT * sizeof(UINT16 *));
  1918. for(i = 0; index <= 0xFFFF && i < NUM_UPCASE*2; i += 2) {
  1919. uni = GET16(uni_upcase + i);
  1920. if(skip) {
  1921. PRINTK("skip from 0x%X ", index);
  1922. index += uni;
  1923. PRINTK("to 0x%X (amount of 0x%X)\n", index, uni);
  1924. skip = FALSE;
  1925. } else if(uni == index)
  1926. index++;
  1927. else if(uni == 0xFFFF)
  1928. skip = TRUE;
  1929. else {
  1930. UINT16 col_index = get_col_index(index);
  1931. if(upcase_table[col_index]== NULL) {
  1932. PRINTK("alloc = 0x%X\n", col_index);
  1933. upcase_table[col_index] = (UINT16 *) MALLOC(UTBL_ROW_COUNT * sizeof(UINT16));
  1934. if(upcase_table[col_index] == NULL) {
  1935. ret = FFS_MEMORYERR;
  1936. goto error;
  1937. }
  1938. for(j = 0 ; j < UTBL_ROW_COUNT ; j++)
  1939. upcase_table[col_index][j] = (col_index << LOW_INDEX_BIT) | j;
  1940. }
  1941. upcase_table[col_index][get_row_index(index)] = uni;
  1942. index ++;
  1943. }
  1944. }
  1945. if(index >= 0xFFFF)
  1946. return FFS_SUCCESS;
  1947. error:
  1948. free_upcase_table(sb);
  1949. return ret;
  1950. }
  1951. INT32 load_upcase_table(struct super_block *sb)
  1952. {
  1953. INT32 i;
  1954. UINT32 tbl_clu, tbl_size;
  1955. UINT32 type, sector, num_sectors;
  1956. CHAIN_T clu;
  1957. CASE_DENTRY_T *ep;
  1958. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1959. BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
  1960. clu.dir = p_fs->root_dir;
  1961. clu.flags = 0x01;
  1962. if (p_fs->dev_ejected)
  1963. return FFS_MEDIAERR;
  1964. while (clu.dir != CLUSTER_32(~0)) {
  1965. for (i = 0; i < p_fs->dentries_per_clu; i++) {
  1966. ep = (CASE_DENTRY_T *) get_entry_in_dir(sb, &clu, i, NULL);
  1967. if (!ep)
  1968. return FFS_MEDIAERR;
  1969. type = p_fs->fs_func->get_entry_type((DENTRY_T *) ep);
  1970. if (type == TYPE_UNUSED)
  1971. break;
  1972. if (type != TYPE_UPCASE)
  1973. continue;
  1974. tbl_clu = GET32_A(ep->start_clu);
  1975. tbl_size = (UINT32) GET64_A(ep->size);
  1976. sector = START_SECTOR(tbl_clu);
  1977. num_sectors = ((tbl_size-1) >> p_bd->sector_size_bits) + 1;
  1978. if(__load_upcase_table(sb, sector, num_sectors, GET32_A(ep->checksum)) != FFS_SUCCESS)
  1979. break;
  1980. else
  1981. return FFS_SUCCESS;
  1982. }
  1983. if (FAT_read(sb, clu.dir, &(clu.dir)) != 0)
  1984. return FFS_MEDIAERR;
  1985. }
  1986. return __load_default_upcase_table(sb);
  1987. }
  1988. void free_upcase_table(struct super_block *sb)
  1989. {
  1990. UINT32 i;
  1991. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  1992. UINT16 **upcase_table;
  1993. upcase_table = p_fs->vol_utbl;
  1994. for(i = 0 ; i < UTBL_COL_COUNT ; i ++)
  1995. FREE(upcase_table[i]);
  1996. FREE(p_fs->vol_utbl);
  1997. p_fs->vol_utbl = NULL;
  1998. }
  1999. UINT32 fat_get_entry_type(DENTRY_T *p_entry)
  2000. {
  2001. DOS_DENTRY_T *ep = (DOS_DENTRY_T *) p_entry;
  2002. if (*(ep->name) == 0x0)
  2003. return TYPE_UNUSED;
  2004. else if (*(ep->name) == 0xE5)
  2005. return TYPE_DELETED;
  2006. else if (ep->attr == ATTR_EXTEND)
  2007. return TYPE_EXTEND;
  2008. else if ((ep->attr & (ATTR_SUBDIR|ATTR_VOLUME)) == ATTR_VOLUME)
  2009. return TYPE_VOLUME;
  2010. else if ((ep->attr & (ATTR_SUBDIR|ATTR_VOLUME)) == ATTR_SUBDIR)
  2011. return TYPE_DIR;
  2012. return TYPE_FILE;
  2013. }
  2014. UINT32 exfat_get_entry_type(DENTRY_T *p_entry)
  2015. {
  2016. FILE_DENTRY_T *ep = (FILE_DENTRY_T *) p_entry;
  2017. if (ep->type == 0x0) {
  2018. return TYPE_UNUSED;
  2019. } else if (ep->type < 0x80) {
  2020. return TYPE_DELETED;
  2021. } else if (ep->type == 0x80) {
  2022. return TYPE_INVALID;
  2023. } else if (ep->type < 0xA0) {
  2024. if (ep->type == 0x81) {
  2025. return TYPE_BITMAP;
  2026. } else if (ep->type == 0x82) {
  2027. return TYPE_UPCASE;
  2028. } else if (ep->type == 0x83) {
  2029. return TYPE_VOLUME;
  2030. } else if (ep->type == 0x85) {
  2031. if (GET16_A(ep->attr) & ATTR_SUBDIR)
  2032. return TYPE_DIR;
  2033. else
  2034. return TYPE_FILE;
  2035. }
  2036. return TYPE_CRITICAL_PRI;
  2037. } else if (ep->type < 0xC0) {
  2038. if (ep->type == 0xA0) {
  2039. return TYPE_GUID;
  2040. } else if (ep->type == 0xA1) {
  2041. return TYPE_PADDING;
  2042. } else if (ep->type == 0xA2) {
  2043. return TYPE_ACLTAB;
  2044. }
  2045. return TYPE_BENIGN_PRI;
  2046. } else if (ep->type < 0xE0) {
  2047. if (ep->type == 0xC0) {
  2048. return TYPE_STREAM;
  2049. } else if (ep->type == 0xC1) {
  2050. return TYPE_EXTEND;
  2051. } else if (ep->type == 0xC2) {
  2052. return TYPE_ACL;
  2053. }
  2054. return TYPE_CRITICAL_SEC;
  2055. }
  2056. return TYPE_BENIGN_SEC;
  2057. }
  2058. void fat_set_entry_type(DENTRY_T *p_entry, UINT32 type)
  2059. {
  2060. DOS_DENTRY_T *ep = (DOS_DENTRY_T *) p_entry;
  2061. if (type == TYPE_UNUSED)
  2062. *(ep->name) = 0x0;
  2063. else if (type == TYPE_DELETED)
  2064. *(ep->name) = 0xE5;
  2065. else if (type == TYPE_EXTEND)
  2066. ep->attr = ATTR_EXTEND;
  2067. else if (type == TYPE_DIR)
  2068. ep->attr = ATTR_SUBDIR;
  2069. else if (type == TYPE_FILE)
  2070. ep->attr = ATTR_ARCHIVE;
  2071. else if (type == TYPE_SYMLINK)
  2072. ep->attr = ATTR_ARCHIVE | ATTR_SYMLINK;
  2073. }
  2074. void exfat_set_entry_type(DENTRY_T *p_entry, UINT32 type)
  2075. {
  2076. FILE_DENTRY_T *ep = (FILE_DENTRY_T *) p_entry;
  2077. if (type == TYPE_UNUSED) {
  2078. ep->type = 0x0;
  2079. } else if (type == TYPE_DELETED) {
  2080. ep->type &= ~0x80;
  2081. } else if (type == TYPE_STREAM) {
  2082. ep->type = 0xC0;
  2083. } else if (type == TYPE_EXTEND) {
  2084. ep->type = 0xC1;
  2085. } else if (type == TYPE_BITMAP) {
  2086. ep->type = 0x81;
  2087. } else if (type == TYPE_UPCASE) {
  2088. ep->type = 0x82;
  2089. } else if (type == TYPE_VOLUME) {
  2090. ep->type = 0x83;
  2091. } else if (type == TYPE_DIR) {
  2092. ep->type = 0x85;
  2093. SET16_A(ep->attr, ATTR_SUBDIR);
  2094. } else if (type == TYPE_FILE) {
  2095. ep->type = 0x85;
  2096. SET16_A(ep->attr, ATTR_ARCHIVE);
  2097. } else if (type == TYPE_SYMLINK) {
  2098. ep->type = 0x85;
  2099. SET16_A(ep->attr, ATTR_ARCHIVE | ATTR_SYMLINK);
  2100. }
  2101. }
  2102. UINT32 fat_get_entry_attr(DENTRY_T *p_entry)
  2103. {
  2104. DOS_DENTRY_T *ep = (DOS_DENTRY_T *) p_entry;
  2105. return((UINT32) ep->attr);
  2106. }
  2107. UINT32 exfat_get_entry_attr(DENTRY_T *p_entry)
  2108. {
  2109. FILE_DENTRY_T *ep = (FILE_DENTRY_T *) p_entry;
  2110. return((UINT32) GET16_A(ep->attr));
  2111. }
  2112. void fat_set_entry_attr(DENTRY_T *p_entry, UINT32 attr)
  2113. {
  2114. DOS_DENTRY_T *ep = (DOS_DENTRY_T *) p_entry;
  2115. ep->attr = (UINT8) attr;
  2116. }
  2117. void exfat_set_entry_attr(DENTRY_T *p_entry, UINT32 attr)
  2118. {
  2119. FILE_DENTRY_T *ep = (FILE_DENTRY_T *) p_entry;
  2120. SET16_A(ep->attr, (UINT16) attr);
  2121. }
  2122. UINT8 fat_get_entry_flag(DENTRY_T *p_entry)
  2123. {
  2124. return 0x01;
  2125. }
  2126. UINT8 exfat_get_entry_flag(DENTRY_T *p_entry)
  2127. {
  2128. STRM_DENTRY_T *ep = (STRM_DENTRY_T *) p_entry;
  2129. return(ep->flags);
  2130. }
  2131. void fat_set_entry_flag(DENTRY_T *p_entry, UINT8 flags)
  2132. {
  2133. }
  2134. void exfat_set_entry_flag(DENTRY_T *p_entry, UINT8 flags)
  2135. {
  2136. STRM_DENTRY_T *ep = (STRM_DENTRY_T *) p_entry;
  2137. ep->flags = flags;
  2138. }
  2139. UINT32 fat_get_entry_clu0(DENTRY_T *p_entry)
  2140. {
  2141. DOS_DENTRY_T *ep = (DOS_DENTRY_T *) p_entry;
  2142. return((GET32_A(ep->start_clu_hi) << 16) | GET16_A(ep->start_clu_lo));
  2143. }
  2144. UINT32 exfat_get_entry_clu0(DENTRY_T *p_entry)
  2145. {
  2146. STRM_DENTRY_T *ep = (STRM_DENTRY_T *) p_entry;
  2147. return(GET32_A(ep->start_clu));
  2148. }
  2149. void fat_set_entry_clu0(DENTRY_T *p_entry, UINT32 start_clu)
  2150. {
  2151. DOS_DENTRY_T *ep = (DOS_DENTRY_T *) p_entry;
  2152. SET16_A(ep->start_clu_lo, CLUSTER_16(start_clu));
  2153. SET16_A(ep->start_clu_hi, CLUSTER_16(start_clu >> 16));
  2154. }
  2155. void exfat_set_entry_clu0(DENTRY_T *p_entry, UINT32 start_clu)
  2156. {
  2157. STRM_DENTRY_T *ep = (STRM_DENTRY_T *) p_entry;
  2158. SET32_A(ep->start_clu, start_clu);
  2159. }
  2160. UINT64 fat_get_entry_size(DENTRY_T *p_entry)
  2161. {
  2162. DOS_DENTRY_T *ep = (DOS_DENTRY_T *) p_entry;
  2163. return((UINT64) GET32_A(ep->size));
  2164. }
  2165. UINT64 exfat_get_entry_size(DENTRY_T *p_entry)
  2166. {
  2167. STRM_DENTRY_T *ep = (STRM_DENTRY_T *) p_entry;
  2168. return(GET64_A(ep->valid_size));
  2169. }
  2170. void fat_set_entry_size(DENTRY_T *p_entry, UINT64 size)
  2171. {
  2172. DOS_DENTRY_T *ep = (DOS_DENTRY_T *) p_entry;
  2173. SET32_A(ep->size, (UINT32) size);
  2174. }
  2175. void exfat_set_entry_size(DENTRY_T *p_entry, UINT64 size)
  2176. {
  2177. STRM_DENTRY_T *ep = (STRM_DENTRY_T *) p_entry;
  2178. SET64_A(ep->valid_size, size);
  2179. SET64_A(ep->size, size);
  2180. }
  2181. void fat_get_entry_time(DENTRY_T *p_entry, TIMESTAMP_T *tp, UINT8 mode)
  2182. {
  2183. UINT16 t = 0x00, d = 0x21;
  2184. DOS_DENTRY_T *ep = (DOS_DENTRY_T *) p_entry;
  2185. switch (mode) {
  2186. case TM_CREATE:
  2187. t = GET16_A(ep->create_time);
  2188. d = GET16_A(ep->create_date);
  2189. break;
  2190. case TM_MODIFY:
  2191. t = GET16_A(ep->modify_time);
  2192. d = GET16_A(ep->modify_date);
  2193. break;
  2194. }
  2195. tp->sec = (t & 0x001F) << 1;
  2196. tp->min = (t >> 5) & 0x003F;
  2197. tp->hour = (t >> 11);
  2198. tp->day = (d & 0x001F);
  2199. tp->mon = (d >> 5) & 0x000F;
  2200. tp->year = (d >> 9);
  2201. }
  2202. void exfat_get_entry_time(DENTRY_T *p_entry, TIMESTAMP_T *tp, UINT8 mode)
  2203. {
  2204. UINT16 t = 0x00, d = 0x21;
  2205. FILE_DENTRY_T *ep = (FILE_DENTRY_T *) p_entry;
  2206. switch (mode) {
  2207. case TM_CREATE:
  2208. t = GET16_A(ep->create_time);
  2209. d = GET16_A(ep->create_date);
  2210. break;
  2211. case TM_MODIFY:
  2212. t = GET16_A(ep->modify_time);
  2213. d = GET16_A(ep->modify_date);
  2214. break;
  2215. case TM_ACCESS:
  2216. t = GET16_A(ep->access_time);
  2217. d = GET16_A(ep->access_date);
  2218. break;
  2219. }
  2220. tp->sec = (t & 0x001F) << 1;
  2221. tp->min = (t >> 5) & 0x003F;
  2222. tp->hour = (t >> 11);
  2223. tp->day = (d & 0x001F);
  2224. tp->mon = (d >> 5) & 0x000F;
  2225. tp->year = (d >> 9);
  2226. }
  2227. void fat_set_entry_time(DENTRY_T *p_entry, TIMESTAMP_T *tp, UINT8 mode)
  2228. {
  2229. UINT16 t, d;
  2230. DOS_DENTRY_T *ep = (DOS_DENTRY_T *) p_entry;
  2231. t = (tp->hour << 11) | (tp->min << 5) | (tp->sec >> 1);
  2232. d = (tp->year << 9) | (tp->mon << 5) | tp->day;
  2233. switch (mode) {
  2234. case TM_CREATE:
  2235. SET16_A(ep->create_time, t);
  2236. SET16_A(ep->create_date, d);
  2237. break;
  2238. case TM_MODIFY:
  2239. SET16_A(ep->modify_time, t);
  2240. SET16_A(ep->modify_date, d);
  2241. break;
  2242. }
  2243. }
  2244. void exfat_set_entry_time(DENTRY_T *p_entry, TIMESTAMP_T *tp, UINT8 mode)
  2245. {
  2246. UINT16 t, d;
  2247. FILE_DENTRY_T *ep = (FILE_DENTRY_T *) p_entry;
  2248. t = (tp->hour << 11) | (tp->min << 5) | (tp->sec >> 1);
  2249. d = (tp->year << 9) | (tp->mon << 5) | tp->day;
  2250. switch (mode) {
  2251. case TM_CREATE:
  2252. SET16_A(ep->create_time, t);
  2253. SET16_A(ep->create_date, d);
  2254. break;
  2255. case TM_MODIFY:
  2256. SET16_A(ep->modify_time, t);
  2257. SET16_A(ep->modify_date, d);
  2258. break;
  2259. case TM_ACCESS:
  2260. SET16_A(ep->access_time, t);
  2261. SET16_A(ep->access_date, d);
  2262. break;
  2263. }
  2264. }
  2265. INT32 fat_init_dir_entry(struct super_block *sb, CHAIN_T *p_dir, INT32 entry, UINT32 type,
  2266. UINT32 start_clu, UINT64 size)
  2267. {
  2268. UINT32 sector;
  2269. DOS_DENTRY_T *dos_ep;
  2270. UINT8 tz_utc = EXFAT_SB(sb)->options.tz_utc;
  2271. dos_ep = (DOS_DENTRY_T *) get_entry_in_dir(sb, p_dir, entry, &sector);
  2272. if (!dos_ep)
  2273. return FFS_MEDIAERR;
  2274. init_dos_entry(dos_ep, type, start_clu, tz_utc);
  2275. buf_modify(sb, sector);
  2276. return FFS_SUCCESS;
  2277. }
  2278. INT32 exfat_init_dir_entry(struct super_block *sb, CHAIN_T *p_dir, INT32 entry, UINT32 type,
  2279. UINT32 start_clu, UINT64 size)
  2280. {
  2281. UINT32 sector;
  2282. UINT8 flags;
  2283. FILE_DENTRY_T *file_ep;
  2284. STRM_DENTRY_T *strm_ep;
  2285. UINT8 tz_utc = EXFAT_SB(sb)->options.tz_utc;
  2286. flags = (type == TYPE_FILE) ? 0x01 : 0x03;
  2287. file_ep = (FILE_DENTRY_T *) get_entry_in_dir(sb, p_dir, entry, &sector);
  2288. if (!file_ep)
  2289. return FFS_MEDIAERR;
  2290. strm_ep = (STRM_DENTRY_T *) get_entry_in_dir(sb, p_dir, entry+1, &sector);
  2291. if (!strm_ep)
  2292. return FFS_MEDIAERR;
  2293. init_file_entry(file_ep, type, tz_utc);
  2294. buf_modify(sb, sector);
  2295. init_strm_entry(strm_ep, flags, start_clu, size);
  2296. buf_modify(sb, sector);
  2297. return FFS_SUCCESS;
  2298. }
  2299. INT32 fat_init_ext_entry(struct super_block *sb, CHAIN_T *p_dir, INT32 entry, INT32 num_entries,
  2300. UNI_NAME_T *p_uniname, DOS_NAME_T *p_dosname)
  2301. {
  2302. INT32 i;
  2303. UINT32 sector;
  2304. UINT8 chksum;
  2305. UINT16 *uniname = p_uniname->name;
  2306. DOS_DENTRY_T *dos_ep;
  2307. EXT_DENTRY_T *ext_ep;
  2308. dos_ep = (DOS_DENTRY_T *) get_entry_in_dir(sb, p_dir, entry, &sector);
  2309. if (!dos_ep)
  2310. return FFS_MEDIAERR;
  2311. dos_ep->lcase = p_dosname->name_case;
  2312. MEMCPY(dos_ep->name, p_dosname->name, DOS_NAME_LENGTH);
  2313. buf_modify(sb, sector);
  2314. if ((--num_entries) > 0) {
  2315. chksum = calc_checksum_1byte((void *) dos_ep->name, DOS_NAME_LENGTH, 0);
  2316. for (i = 1; i < num_entries; i++) {
  2317. ext_ep = (EXT_DENTRY_T *) get_entry_in_dir(sb, p_dir, entry-i, &sector);
  2318. if (!ext_ep)
  2319. return FFS_MEDIAERR;
  2320. init_ext_entry(ext_ep, i, chksum, uniname);
  2321. buf_modify(sb, sector);
  2322. uniname += 13;
  2323. }
  2324. ext_ep = (EXT_DENTRY_T *) get_entry_in_dir(sb, p_dir, entry-i, &sector);
  2325. if (!ext_ep)
  2326. return FFS_MEDIAERR;
  2327. init_ext_entry(ext_ep, i+0x40, chksum, uniname);
  2328. buf_modify(sb, sector);
  2329. }
  2330. return FFS_SUCCESS;
  2331. }
  2332. INT32 exfat_init_ext_entry(struct super_block *sb, CHAIN_T *p_dir, INT32 entry, INT32 num_entries,
  2333. UNI_NAME_T *p_uniname, DOS_NAME_T *p_dosname)
  2334. {
  2335. INT32 i;
  2336. UINT32 sector;
  2337. UINT16 *uniname = p_uniname->name;
  2338. FILE_DENTRY_T *file_ep;
  2339. STRM_DENTRY_T *strm_ep;
  2340. NAME_DENTRY_T *name_ep;
  2341. file_ep = (FILE_DENTRY_T *) get_entry_in_dir(sb, p_dir, entry, &sector);
  2342. if (!file_ep)
  2343. return FFS_MEDIAERR;
  2344. file_ep->num_ext = (UINT8)(num_entries - 1);
  2345. buf_modify(sb, sector);
  2346. strm_ep = (STRM_DENTRY_T *) get_entry_in_dir(sb, p_dir, entry+1, &sector);
  2347. if (!strm_ep)
  2348. return FFS_MEDIAERR;
  2349. strm_ep->name_len = p_uniname->name_len;
  2350. SET16_A(strm_ep->name_hash, p_uniname->name_hash);
  2351. buf_modify(sb, sector);
  2352. for (i = 2; i < num_entries; i++) {
  2353. name_ep = (NAME_DENTRY_T *) get_entry_in_dir(sb, p_dir, entry+i, &sector);
  2354. if (!name_ep)
  2355. return FFS_MEDIAERR;
  2356. init_name_entry(name_ep, uniname);
  2357. buf_modify(sb, sector);
  2358. uniname += 15;
  2359. }
  2360. update_dir_checksum(sb, p_dir, entry);
  2361. return FFS_SUCCESS;
  2362. }
  2363. void init_dos_entry(DOS_DENTRY_T *ep, UINT32 type, UINT32 start_clu, UINT8 tz_utc)
  2364. {
  2365. TIMESTAMP_T tm, *tp;
  2366. fat_set_entry_type((DENTRY_T *) ep, type);
  2367. SET16_A(ep->start_clu_lo, CLUSTER_16(start_clu));
  2368. SET16_A(ep->start_clu_hi, CLUSTER_16(start_clu >> 16));
  2369. SET32_A(ep->size, 0);
  2370. tp = tm_current(&tm, tz_utc);
  2371. fat_set_entry_time((DENTRY_T *) ep, tp, TM_CREATE);
  2372. fat_set_entry_time((DENTRY_T *) ep, tp, TM_MODIFY);
  2373. SET16_A(ep->access_date, 0);
  2374. ep->create_time_ms = 0;
  2375. }
  2376. void init_ext_entry(EXT_DENTRY_T *ep, INT32 order, UINT8 chksum, UINT16 *uniname)
  2377. {
  2378. INT32 i;
  2379. UINT8 end = FALSE;
  2380. fat_set_entry_type((DENTRY_T *) ep, TYPE_EXTEND);
  2381. ep->order = (UINT8) order;
  2382. ep->sysid = 0;
  2383. ep->checksum = chksum;
  2384. SET16_A(ep->start_clu, 0);
  2385. for (i = 0; i < 10; i += 2) {
  2386. if (!end) {
  2387. SET16(ep->unicode_0_4+i, *uniname);
  2388. if (*uniname == 0x0)
  2389. end = TRUE;
  2390. else
  2391. uniname++;
  2392. } else {
  2393. SET16(ep->unicode_0_4+i, 0xFFFF);
  2394. }
  2395. }
  2396. for (i = 0; i < 12; i += 2) {
  2397. if (!end) {
  2398. SET16_A(ep->unicode_5_10+i, *uniname);
  2399. if (*uniname == 0x0)
  2400. end = TRUE;
  2401. else
  2402. uniname++;
  2403. } else {
  2404. SET16_A(ep->unicode_5_10+i, 0xFFFF);
  2405. }
  2406. }
  2407. for (i = 0; i < 4; i += 2) {
  2408. if (!end) {
  2409. SET16_A(ep->unicode_11_12+i, *uniname);
  2410. if (*uniname == 0x0)
  2411. end = TRUE;
  2412. else
  2413. uniname++;
  2414. } else {
  2415. SET16_A(ep->unicode_11_12+i, 0xFFFF);
  2416. }
  2417. }
  2418. }
  2419. void init_file_entry(FILE_DENTRY_T *ep, UINT32 type, UINT8 tz_utc)
  2420. {
  2421. TIMESTAMP_T tm, *tp;
  2422. exfat_set_entry_type((DENTRY_T *) ep, type);
  2423. tp = tm_current(&tm, tz_utc);
  2424. exfat_set_entry_time((DENTRY_T *) ep, tp, TM_CREATE);
  2425. exfat_set_entry_time((DENTRY_T *) ep, tp, TM_MODIFY);
  2426. exfat_set_entry_time((DENTRY_T *) ep, tp, TM_ACCESS);
  2427. ep->create_time_ms = 0;
  2428. ep->modify_time_ms = 0;
  2429. ep->access_time_ms = 0;
  2430. }
  2431. void init_strm_entry(STRM_DENTRY_T *ep, UINT8 flags, UINT32 start_clu, UINT64 size)
  2432. {
  2433. exfat_set_entry_type((DENTRY_T *) ep, TYPE_STREAM);
  2434. ep->flags = flags;
  2435. SET32_A(ep->start_clu, start_clu);
  2436. SET64_A(ep->valid_size, size);
  2437. SET64_A(ep->size, size);
  2438. }
  2439. void init_name_entry(NAME_DENTRY_T *ep, UINT16 *uniname)
  2440. {
  2441. INT32 i;
  2442. exfat_set_entry_type((DENTRY_T *) ep, TYPE_EXTEND);
  2443. ep->flags = 0x0;
  2444. for (i = 0; i < 30; i++, i++) {
  2445. SET16_A(ep->unicode_0_14+i, *uniname);
  2446. if (*uniname == 0x0)
  2447. break;
  2448. uniname++;
  2449. }
  2450. }
  2451. void fat_delete_dir_entry(struct super_block *sb, CHAIN_T *p_dir, INT32 entry, INT32 order, INT32 num_entries)
  2452. {
  2453. INT32 i;
  2454. UINT32 sector;
  2455. DENTRY_T *ep;
  2456. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  2457. for (i = num_entries-1; i >= order; i--) {
  2458. ep = get_entry_in_dir(sb, p_dir, entry-i, &sector);
  2459. if (!ep)
  2460. return;
  2461. p_fs->fs_func->set_entry_type(ep, TYPE_DELETED);
  2462. buf_modify(sb, sector);
  2463. }
  2464. }
  2465. void exfat_delete_dir_entry(struct super_block *sb, CHAIN_T *p_dir, INT32 entry, INT32 order, INT32 num_entries)
  2466. {
  2467. INT32 i;
  2468. UINT32 sector;
  2469. DENTRY_T *ep;
  2470. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  2471. for (i = order; i < num_entries; i++) {
  2472. ep = get_entry_in_dir(sb, p_dir, entry+i, &sector);
  2473. if (!ep)
  2474. return;
  2475. p_fs->fs_func->set_entry_type(ep, TYPE_DELETED);
  2476. buf_modify(sb, sector);
  2477. }
  2478. }
  2479. void update_dir_checksum(struct super_block *sb, CHAIN_T *p_dir, INT32 entry)
  2480. {
  2481. INT32 i, num_entries;
  2482. UINT32 sector;
  2483. UINT16 chksum;
  2484. FILE_DENTRY_T *file_ep;
  2485. DENTRY_T *ep;
  2486. file_ep = (FILE_DENTRY_T *) get_entry_in_dir(sb, p_dir, entry, &sector);
  2487. if (!file_ep)
  2488. return;
  2489. buf_lock(sb, sector);
  2490. num_entries = (INT32) file_ep->num_ext + 1;
  2491. chksum = calc_checksum_2byte((void *) file_ep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
  2492. for (i = 1; i < num_entries; i++) {
  2493. ep = get_entry_in_dir(sb, p_dir, entry+i, NULL);
  2494. if (!ep) {
  2495. buf_unlock(sb, sector);
  2496. return;
  2497. }
  2498. chksum = calc_checksum_2byte((void *) ep, DENTRY_SIZE, chksum, CS_DEFAULT);
  2499. }
  2500. SET16_A(file_ep->checksum, chksum);
  2501. buf_modify(sb, sector);
  2502. buf_unlock(sb, sector);
  2503. }
  2504. void update_dir_checksum_with_entry_set (struct super_block *sb, ENTRY_SET_CACHE_T *es)
  2505. {
  2506. DENTRY_T *ep;
  2507. UINT16 chksum = 0;
  2508. INT32 chksum_type = CS_DIR_ENTRY, i;
  2509. ep = (DENTRY_T *)&(es->__buf);
  2510. for (i=0; i < es->num_entries; i++) {
  2511. PRINTK ("update_dir_checksum_with_entry_set ep %p\n", ep);
  2512. chksum = calc_checksum_2byte((void *) ep, DENTRY_SIZE, chksum, chksum_type);
  2513. ep++;
  2514. chksum_type = CS_DEFAULT;
  2515. }
  2516. ep = (DENTRY_T *)&(es->__buf);
  2517. SET16_A(((FILE_DENTRY_T *)ep)->checksum, chksum);
  2518. write_whole_entry_set(sb, es);
  2519. }
  2520. static INT32 _walk_fat_chain (struct super_block *sb, CHAIN_T *p_dir, INT32 byte_offset, UINT32 *clu)
  2521. {
  2522. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  2523. INT32 clu_offset;
  2524. UINT32 cur_clu;
  2525. clu_offset = byte_offset >> p_fs->cluster_size_bits;
  2526. cur_clu = p_dir->dir;
  2527. if (p_dir->flags == 0x03) {
  2528. cur_clu += clu_offset;
  2529. } else {
  2530. while (clu_offset > 0) {
  2531. if (FAT_read(sb, cur_clu, &cur_clu) == -1)
  2532. return FFS_MEDIAERR;
  2533. clu_offset--;
  2534. }
  2535. }
  2536. if (clu)
  2537. *clu = cur_clu;
  2538. return FFS_SUCCESS;
  2539. }
  2540. INT32 find_location(struct super_block *sb, CHAIN_T *p_dir, INT32 entry, UINT32 *sector, INT32 *offset)
  2541. {
  2542. INT32 off, ret;
  2543. UINT32 clu=0;
  2544. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  2545. BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
  2546. off = entry << DENTRY_SIZE_BITS;
  2547. if (p_dir->dir == CLUSTER_32(0)) {
  2548. *offset = off & p_bd->sector_size_mask;
  2549. *sector = off >> p_bd->sector_size_bits;
  2550. *sector += p_fs->root_start_sector;
  2551. } else {
  2552. ret =_walk_fat_chain(sb, p_dir, off, &clu);
  2553. if (ret != FFS_SUCCESS)
  2554. return ret;
  2555. off &= p_fs->cluster_size - 1;
  2556. *offset = off & p_bd->sector_size_mask;
  2557. *sector = off >> p_bd->sector_size_bits;
  2558. *sector += START_SECTOR(clu);
  2559. }
  2560. return FFS_SUCCESS;
  2561. }
  2562. DENTRY_T *get_entry_with_sector(struct super_block *sb, UINT32 sector, INT32 offset)
  2563. {
  2564. UINT8 *buf;
  2565. buf = buf_getblk(sb, sector);
  2566. if (buf == NULL)
  2567. return NULL;
  2568. return((DENTRY_T *)(buf + offset));
  2569. }
  2570. DENTRY_T *get_entry_in_dir(struct super_block *sb, CHAIN_T *p_dir, INT32 entry, UINT32 *sector)
  2571. {
  2572. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  2573. UINT32 dentries_per_page = PAGE_SIZE >> DENTRY_SIZE_BITS;
  2574. INT32 off;
  2575. UINT32 sec;
  2576. UINT8 *buf;
  2577. if (p_fs->dev_ejected)
  2578. return NULL;
  2579. BUG_ON(p_dir->dir == DIR_DELETED);
  2580. if (find_location(sb, p_dir, entry, &sec, &off) != FFS_SUCCESS)
  2581. return NULL;
  2582. /* if first entry, then do read-ahead */
  2583. if (!(entry & (dentries_per_page - 1)))
  2584. buf_cache_readahead(sb, sec);
  2585. buf = buf_getblk(sb, sec);
  2586. if (buf == NULL)
  2587. return NULL;
  2588. if (sector != NULL)
  2589. *sector = sec;
  2590. return((DENTRY_T *)(buf + off));
  2591. }
  2592. #define ES_MODE_STARTED 0
  2593. #define ES_MODE_GET_FILE_ENTRY 1
  2594. #define ES_MODE_GET_STRM_ENTRY 2
  2595. #define ES_MODE_GET_NAME_ENTRY 3
  2596. #define ES_MODE_GET_CRITICAL_SEC_ENTRY 4
  2597. ENTRY_SET_CACHE_T *get_entry_set_in_dir (struct super_block *sb, CHAIN_T *p_dir, INT32 entry, UINT32 type, DENTRY_T **file_ep)
  2598. {
  2599. INT32 off, ret, byte_offset;
  2600. UINT32 clu=0;
  2601. UINT32 sec, entry_type;
  2602. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  2603. BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
  2604. ENTRY_SET_CACHE_T *es = NULL;
  2605. DENTRY_T *ep, *pos;
  2606. UINT8 *buf;
  2607. UINT8 num_entries;
  2608. INT32 mode = ES_MODE_STARTED;
  2609. if (p_fs->dev_ejected)
  2610. return NULL;
  2611. BUG_ON(p_dir->dir == DIR_DELETED);
  2612. PRINTK("get_entry_set_in_dir entered\n");
  2613. PRINTK("p_dir dir %u flags %x size %d\n", p_dir->dir, p_dir->flags, p_dir->size);
  2614. byte_offset = entry << DENTRY_SIZE_BITS;
  2615. ret =_walk_fat_chain(sb, p_dir, byte_offset, &clu);
  2616. if (ret != FFS_SUCCESS)
  2617. return NULL;
  2618. byte_offset &= p_fs->cluster_size - 1;
  2619. off = byte_offset & p_bd->sector_size_mask;
  2620. sec = byte_offset >> p_bd->sector_size_bits;
  2621. sec += START_SECTOR(clu);
  2622. buf = buf_getblk(sb, sec);
  2623. if (buf == NULL)
  2624. goto err_out;
  2625. ep = (DENTRY_T *)(buf + off);
  2626. entry_type = p_fs->fs_func->get_entry_type(ep);
  2627. if ((entry_type != TYPE_FILE)
  2628. && (entry_type != TYPE_DIR))
  2629. goto err_out;
  2630. if (type == ES_ALL_ENTRIES)
  2631. num_entries = ((FILE_DENTRY_T *)ep)->num_ext+1;
  2632. else
  2633. num_entries = type;
  2634. PRINTK("trying to malloc %x bytes for %d entries\n", offsetof(ENTRY_SET_CACHE_T, __buf) + (num_entries) * sizeof(DENTRY_T), num_entries);
  2635. es = MALLOC(offsetof(ENTRY_SET_CACHE_T, __buf) + (num_entries) * sizeof(DENTRY_T));
  2636. if (es == NULL)
  2637. goto err_out;
  2638. es->num_entries = num_entries;
  2639. es->sector = sec;
  2640. es->offset = off;
  2641. es->alloc_flag = p_dir->flags;
  2642. pos = (DENTRY_T *) &(es->__buf);
  2643. while(num_entries) {
  2644. entry_type = p_fs->fs_func->get_entry_type(ep);
  2645. if ((entry_type == TYPE_UNUSED) || (entry_type == TYPE_DELETED))
  2646. goto err_out;
  2647. switch(mode) {
  2648. case ES_MODE_STARTED:
  2649. if ((entry_type == TYPE_FILE) || (entry_type == TYPE_DIR))
  2650. mode = ES_MODE_GET_FILE_ENTRY;
  2651. else
  2652. goto err_out;
  2653. break;
  2654. case ES_MODE_GET_FILE_ENTRY:
  2655. if (entry_type == TYPE_STREAM)
  2656. mode = ES_MODE_GET_STRM_ENTRY;
  2657. else
  2658. goto err_out;
  2659. break;
  2660. case ES_MODE_GET_STRM_ENTRY:
  2661. if (entry_type == TYPE_EXTEND)
  2662. mode = ES_MODE_GET_NAME_ENTRY;
  2663. else
  2664. goto err_out;
  2665. break;
  2666. case ES_MODE_GET_NAME_ENTRY:
  2667. if (entry_type == TYPE_EXTEND)
  2668. break;
  2669. else if (entry_type == TYPE_STREAM)
  2670. goto err_out;
  2671. else if (entry_type & TYPE_CRITICAL_SEC)
  2672. mode = ES_MODE_GET_CRITICAL_SEC_ENTRY;
  2673. else
  2674. goto err_out;
  2675. break;
  2676. case ES_MODE_GET_CRITICAL_SEC_ENTRY:
  2677. if ((entry_type == TYPE_EXTEND) || (entry_type == TYPE_STREAM))
  2678. goto err_out;
  2679. else if ((entry_type & TYPE_CRITICAL_SEC) != TYPE_CRITICAL_SEC)
  2680. goto err_out;
  2681. break;
  2682. }
  2683. COPY_DENTRY(pos, ep);
  2684. if (--num_entries == 0)
  2685. break;
  2686. if (((off + DENTRY_SIZE) & p_bd->sector_size_mask) < (off & p_bd->sector_size_mask)) {
  2687. if (IS_LAST_SECTOR_IN_CLUSTER(sec)) {
  2688. if (es->alloc_flag == 0x03) {
  2689. clu++;
  2690. } else {
  2691. if (FAT_read(sb, clu, &clu) == -1)
  2692. goto err_out;
  2693. }
  2694. sec = START_SECTOR(clu);
  2695. } else {
  2696. sec++;
  2697. }
  2698. buf = buf_getblk(sb, sec);
  2699. if (buf == NULL)
  2700. goto err_out;
  2701. off = 0;
  2702. ep = (DENTRY_T *)(buf);
  2703. } else {
  2704. ep++;
  2705. off += DENTRY_SIZE;
  2706. }
  2707. pos++;
  2708. }
  2709. if (file_ep)
  2710. *file_ep = (DENTRY_T *)&(es->__buf);
  2711. PRINTK("es sec %u offset %d flags %d, num_entries %u buf ptr %p\n",
  2712. es->sector, es->offset, es->alloc_flag, es->num_entries, &(es->__buf));
  2713. PRINTK("get_entry_set_in_dir exited %p\n", es);
  2714. return es;
  2715. err_out:
  2716. PRINTK("get_entry_set_in_dir exited NULL (es %p)\n", es);
  2717. if (es)
  2718. FREE(es);
  2719. return NULL;
  2720. }
  2721. void release_entry_set (ENTRY_SET_CACHE_T *es)
  2722. {
  2723. PRINTK("release_entry_set %p\n", es);
  2724. FREE(es);
  2725. }
  2726. static INT32 __write_partial_entries_in_entry_set (struct super_block *sb, ENTRY_SET_CACHE_T *es, UINT32 sec, INT32 off, UINT32 count)
  2727. {
  2728. INT32 num_entries, buf_off = (off - es->offset);
  2729. UINT32 remaining_byte_in_sector, copy_entries;
  2730. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  2731. BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
  2732. UINT32 clu;
  2733. UINT8 *buf, *esbuf = (UINT8 *)&(es->__buf);
  2734. PRINTK("__write_partial_entries_in_entry_set entered\n");
  2735. PRINTK("es %p sec %u off %d count %d\n", es, sec, off, count);
  2736. num_entries = count;
  2737. while(num_entries) {
  2738. remaining_byte_in_sector = (1 << p_bd->sector_size_bits) - off;
  2739. copy_entries = MIN(remaining_byte_in_sector>> DENTRY_SIZE_BITS , num_entries);
  2740. buf = buf_getblk(sb, sec);
  2741. if (buf == NULL)
  2742. goto err_out;
  2743. PRINTK("es->buf %p buf_off %u\n", esbuf, buf_off);
  2744. PRINTK("copying %d entries from %p to sector %u\n", copy_entries, (esbuf + buf_off), sec);
  2745. MEMCPY(buf + off, esbuf + buf_off, copy_entries << DENTRY_SIZE_BITS);
  2746. buf_modify(sb, sec);
  2747. num_entries -= copy_entries;
  2748. if (num_entries) {
  2749. if (IS_LAST_SECTOR_IN_CLUSTER(sec)) {
  2750. clu = GET_CLUSTER_FROM_SECTOR(sec);
  2751. if (es->alloc_flag == 0x03) {
  2752. clu++;
  2753. } else {
  2754. if (FAT_read(sb, clu, &clu) == -1)
  2755. goto err_out;
  2756. }
  2757. sec = START_SECTOR(clu);
  2758. } else {
  2759. sec++;
  2760. }
  2761. off = 0;
  2762. buf_off += copy_entries << DENTRY_SIZE_BITS;
  2763. }
  2764. }
  2765. PRINTK("__write_partial_entries_in_entry_set exited successfully\n");
  2766. return FFS_SUCCESS;
  2767. err_out:
  2768. PRINTK("__write_partial_entries_in_entry_set failed\n");
  2769. return FFS_ERROR;
  2770. }
  2771. INT32 write_whole_entry_set (struct super_block *sb, ENTRY_SET_CACHE_T *es)
  2772. {
  2773. return (__write_partial_entries_in_entry_set(sb, es, es->sector,es->offset, es->num_entries));
  2774. }
  2775. INT32 write_partial_entries_in_entry_set (struct super_block *sb, ENTRY_SET_CACHE_T *es, DENTRY_T *ep, UINT32 count)
  2776. {
  2777. INT32 ret, byte_offset, off;
  2778. UINT32 clu=0, sec;
  2779. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  2780. BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
  2781. CHAIN_T dir;
  2782. if (ep + count > ((DENTRY_T *)&(es->__buf)) + es->num_entries)
  2783. return FFS_ERROR;
  2784. dir.dir = GET_CLUSTER_FROM_SECTOR(es->sector);
  2785. dir.flags = es->alloc_flag;
  2786. dir.size = 0xffffffff;
  2787. byte_offset = (es->sector - START_SECTOR(dir.dir)) << p_bd->sector_size_bits;
  2788. byte_offset += (INT32)((unsigned long)ep - (unsigned long)&(es->__buf)) + es->offset;
  2789. ret =_walk_fat_chain(sb, &dir, byte_offset, &clu);
  2790. if (ret != FFS_SUCCESS)
  2791. return ret;
  2792. byte_offset &= p_fs->cluster_size - 1;
  2793. off = byte_offset & p_bd->sector_size_mask;
  2794. sec = byte_offset >> p_bd->sector_size_bits;
  2795. sec += START_SECTOR(clu);
  2796. return (__write_partial_entries_in_entry_set(sb, es, sec, off, count));
  2797. }
  2798. INT32 search_deleted_or_unused_entry(struct super_block *sb, CHAIN_T *p_dir, INT32 num_entries)
  2799. {
  2800. INT32 i, dentry, num_empty = 0;
  2801. INT32 dentries_per_clu;
  2802. UINT32 type;
  2803. CHAIN_T clu;
  2804. DENTRY_T *ep;
  2805. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  2806. if (p_dir->dir == CLUSTER_32(0))
  2807. dentries_per_clu = p_fs->dentries_in_root;
  2808. else
  2809. dentries_per_clu = p_fs->dentries_per_clu;
  2810. if (p_fs->hint_uentry.dir == p_dir->dir) {
  2811. if (p_fs->hint_uentry.entry == -1)
  2812. return -1;
  2813. clu.dir = p_fs->hint_uentry.clu.dir;
  2814. clu.size = p_fs->hint_uentry.clu.size;
  2815. clu.flags = p_fs->hint_uentry.clu.flags;
  2816. dentry = p_fs->hint_uentry.entry;
  2817. } else {
  2818. p_fs->hint_uentry.entry = -1;
  2819. clu.dir = p_dir->dir;
  2820. clu.size = p_dir->size;
  2821. clu.flags = p_dir->flags;
  2822. dentry = 0;
  2823. }
  2824. while (clu.dir != CLUSTER_32(~0)) {
  2825. if (p_fs->dev_ejected)
  2826. break;
  2827. if (p_dir->dir == CLUSTER_32(0))
  2828. i = dentry % dentries_per_clu;
  2829. else
  2830. i = dentry & (dentries_per_clu-1);
  2831. for ( ; i < dentries_per_clu; i++, dentry++) {
  2832. ep = get_entry_in_dir(sb, &clu, i, NULL);
  2833. if (!ep)
  2834. return -1;
  2835. type = p_fs->fs_func->get_entry_type(ep);
  2836. if (type == TYPE_UNUSED) {
  2837. num_empty++;
  2838. if (p_fs->hint_uentry.entry == -1) {
  2839. p_fs->hint_uentry.dir = p_dir->dir;
  2840. p_fs->hint_uentry.entry = dentry;
  2841. p_fs->hint_uentry.clu.dir = clu.dir;
  2842. p_fs->hint_uentry.clu.size = clu.size;
  2843. p_fs->hint_uentry.clu.flags = clu.flags;
  2844. }
  2845. } else if (type == TYPE_DELETED) {
  2846. num_empty++;
  2847. } else {
  2848. num_empty = 0;
  2849. }
  2850. if (num_empty >= num_entries) {
  2851. p_fs->hint_uentry.dir = CLUSTER_32(~0);
  2852. p_fs->hint_uentry.entry = -1;
  2853. if (p_fs->vol_type == EXFAT)
  2854. return(dentry - (num_entries-1));
  2855. else
  2856. return(dentry);
  2857. }
  2858. }
  2859. if (p_dir->dir == CLUSTER_32(0))
  2860. break;
  2861. if (clu.flags == 0x03) {
  2862. if ((--clu.size) > 0)
  2863. clu.dir++;
  2864. else
  2865. clu.dir = CLUSTER_32(~0);
  2866. } else {
  2867. if (FAT_read(sb, clu.dir, &(clu.dir)) != 0)
  2868. return -1;
  2869. }
  2870. }
  2871. return -1;
  2872. }
  2873. INT32 find_empty_entry(struct inode *inode, CHAIN_T *p_dir, INT32 num_entries)
  2874. {
  2875. INT32 ret, dentry;
  2876. UINT32 last_clu, sector;
  2877. UINT64 size = 0;
  2878. CHAIN_T clu;
  2879. DENTRY_T *ep = NULL;
  2880. struct super_block *sb = inode->i_sb;
  2881. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  2882. FILE_ID_T *fid = &(EXFAT_I(inode)->fid);
  2883. if (p_dir->dir == CLUSTER_32(0))
  2884. return(search_deleted_or_unused_entry(sb, p_dir, num_entries));
  2885. while ((dentry = search_deleted_or_unused_entry(sb, p_dir, num_entries)) < 0) {
  2886. if (p_fs->dev_ejected)
  2887. break;
  2888. if (p_fs->vol_type == EXFAT) {
  2889. if (p_dir->dir != p_fs->root_dir) {
  2890. size = i_size_read(inode);
  2891. }
  2892. }
  2893. last_clu = find_last_cluster(sb, p_dir);
  2894. clu.dir = last_clu + 1;
  2895. clu.size = 0;
  2896. clu.flags = p_dir->flags;
  2897. ret = p_fs->fs_func->alloc_cluster(sb, 1, &clu);
  2898. if (ret < 1)
  2899. return -1;
  2900. if (clear_cluster(sb, clu.dir) != FFS_SUCCESS)
  2901. return -1;
  2902. if (clu.flags != p_dir->flags) {
  2903. exfat_chain_cont_cluster(sb, p_dir->dir, p_dir->size);
  2904. p_dir->flags = 0x01;
  2905. p_fs->hint_uentry.clu.flags = 0x01;
  2906. }
  2907. if (clu.flags == 0x01)
  2908. if(FAT_write(sb, last_clu, clu.dir) < 0)
  2909. return -1;
  2910. if (p_fs->hint_uentry.entry == -1) {
  2911. p_fs->hint_uentry.dir = p_dir->dir;
  2912. p_fs->hint_uentry.entry = p_dir->size << (p_fs->cluster_size_bits - DENTRY_SIZE_BITS);
  2913. p_fs->hint_uentry.clu.dir = clu.dir;
  2914. p_fs->hint_uentry.clu.size = 0;
  2915. p_fs->hint_uentry.clu.flags = clu.flags;
  2916. }
  2917. p_fs->hint_uentry.clu.size++;
  2918. p_dir->size++;
  2919. if (p_fs->vol_type == EXFAT) {
  2920. if (p_dir->dir != p_fs->root_dir) {
  2921. size += p_fs->cluster_size;
  2922. ep = get_entry_in_dir(sb, &(fid->dir), fid->entry+1, &sector);
  2923. if (!ep)
  2924. return -1;
  2925. p_fs->fs_func->set_entry_size(ep, size);
  2926. p_fs->fs_func->set_entry_flag(ep, p_dir->flags);
  2927. buf_modify(sb, sector);
  2928. update_dir_checksum(sb, &(fid->dir), fid->entry);
  2929. }
  2930. }
  2931. i_size_write(inode, i_size_read(inode)+p_fs->cluster_size);
  2932. EXFAT_I(inode)->mmu_private += p_fs->cluster_size;
  2933. EXFAT_I(inode)->fid.size += p_fs->cluster_size;
  2934. EXFAT_I(inode)->fid.flags = p_dir->flags;
  2935. inode->i_blocks += 1 << (p_fs->cluster_size_bits - sb->s_blocksize_bits);
  2936. }
  2937. return(dentry);
  2938. }
  2939. INT32 fat_find_dir_entry(struct super_block *sb, CHAIN_T *p_dir, UNI_NAME_T *p_uniname, INT32 num_entries, DOS_NAME_T *p_dosname, UINT32 type)
  2940. {
  2941. INT32 i, dentry = 0, lossy = FALSE, len;
  2942. INT32 order = 0, is_feasible_entry = TRUE, has_ext_entry = FALSE;
  2943. INT32 dentries_per_clu;
  2944. UINT32 entry_type;
  2945. UINT16 entry_uniname[14], *uniname = NULL, unichar;
  2946. CHAIN_T clu;
  2947. DENTRY_T *ep;
  2948. DOS_DENTRY_T *dos_ep;
  2949. EXT_DENTRY_T *ext_ep;
  2950. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  2951. if (p_dir->dir == p_fs->root_dir) {
  2952. if ((!nls_uniname_cmp(sb, p_uniname->name, (UINT16 *) UNI_CUR_DIR_NAME)) ||
  2953. (!nls_uniname_cmp(sb, p_uniname->name, (UINT16 *) UNI_PAR_DIR_NAME)))
  2954. return -1;
  2955. }
  2956. if (p_dir->dir == CLUSTER_32(0))
  2957. dentries_per_clu = p_fs->dentries_in_root;
  2958. else
  2959. dentries_per_clu = p_fs->dentries_per_clu;
  2960. clu.dir = p_dir->dir;
  2961. clu.flags = p_dir->flags;
  2962. while (clu.dir != CLUSTER_32(~0)) {
  2963. if (p_fs->dev_ejected)
  2964. break;
  2965. for (i = 0; i < dentries_per_clu; i++, dentry++) {
  2966. ep = get_entry_in_dir(sb, &clu, i, NULL);
  2967. if (!ep)
  2968. return -2;
  2969. entry_type = p_fs->fs_func->get_entry_type(ep);
  2970. if ((entry_type == TYPE_FILE) || (entry_type == TYPE_DIR)) {
  2971. if ((type == TYPE_ALL) || (type == entry_type)) {
  2972. if (is_feasible_entry && has_ext_entry)
  2973. return(dentry);
  2974. dos_ep = (DOS_DENTRY_T *) ep;
  2975. if ((!lossy) && (!nls_dosname_cmp(sb, p_dosname->name, dos_ep->name)))
  2976. return(dentry);
  2977. }
  2978. is_feasible_entry = TRUE;
  2979. has_ext_entry = FALSE;
  2980. } else if (entry_type == TYPE_EXTEND) {
  2981. if (is_feasible_entry) {
  2982. ext_ep = (EXT_DENTRY_T *) ep;
  2983. if (ext_ep->order > 0x40) {
  2984. order = (INT32)(ext_ep->order - 0x40);
  2985. uniname = p_uniname->name + 13 * (order-1);
  2986. } else {
  2987. order = (INT32) ext_ep->order;
  2988. uniname -= 13;
  2989. }
  2990. len = extract_uni_name_from_ext_entry(ext_ep, entry_uniname, order);
  2991. unichar = *(uniname+len);
  2992. *(uniname+len) = 0x0;
  2993. if (nls_uniname_cmp(sb, uniname, entry_uniname)) {
  2994. is_feasible_entry = FALSE;
  2995. }
  2996. *(uniname+len) = unichar;
  2997. }
  2998. has_ext_entry = TRUE;
  2999. } else if (entry_type == TYPE_UNUSED) {
  3000. return -2;
  3001. } else {
  3002. is_feasible_entry = TRUE;
  3003. has_ext_entry = FALSE;
  3004. }
  3005. }
  3006. if (p_dir->dir == CLUSTER_32(0))
  3007. break;
  3008. if (FAT_read(sb, clu.dir, &(clu.dir)) != 0)
  3009. return -2;
  3010. }
  3011. return -2;
  3012. }
  3013. INT32 exfat_find_dir_entry(struct super_block *sb, CHAIN_T *p_dir, UNI_NAME_T *p_uniname, INT32 num_entries, DOS_NAME_T *p_dosname, UINT32 type)
  3014. {
  3015. INT32 i, dentry = 0, num_ext_entries = 0, len;
  3016. INT32 order = 0, is_feasible_entry = FALSE;
  3017. INT32 dentries_per_clu, num_empty = 0;
  3018. UINT32 entry_type;
  3019. UINT16 entry_uniname[16], *uniname = NULL, unichar;
  3020. CHAIN_T clu;
  3021. DENTRY_T *ep;
  3022. FILE_DENTRY_T *file_ep;
  3023. STRM_DENTRY_T *strm_ep;
  3024. NAME_DENTRY_T *name_ep;
  3025. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  3026. if (p_dir->dir == p_fs->root_dir) {
  3027. if ((!nls_uniname_cmp(sb, p_uniname->name, (UINT16 *) UNI_CUR_DIR_NAME)) ||
  3028. (!nls_uniname_cmp(sb, p_uniname->name, (UINT16 *) UNI_PAR_DIR_NAME)))
  3029. return -1;
  3030. }
  3031. if (p_dir->dir == CLUSTER_32(0))
  3032. dentries_per_clu = p_fs->dentries_in_root;
  3033. else
  3034. dentries_per_clu = p_fs->dentries_per_clu;
  3035. clu.dir = p_dir->dir;
  3036. clu.size = p_dir->size;
  3037. clu.flags = p_dir->flags;
  3038. p_fs->hint_uentry.dir = p_dir->dir;
  3039. p_fs->hint_uentry.entry = -1;
  3040. while (clu.dir != CLUSTER_32(~0)) {
  3041. if (p_fs->dev_ejected)
  3042. break;
  3043. for (i = 0; i < dentries_per_clu; i++, dentry++) {
  3044. ep = get_entry_in_dir(sb, &clu, i, NULL);
  3045. if (!ep)
  3046. return -2;
  3047. entry_type = p_fs->fs_func->get_entry_type(ep);
  3048. if ((entry_type == TYPE_UNUSED) || (entry_type == TYPE_DELETED)) {
  3049. is_feasible_entry = FALSE;
  3050. if (p_fs->hint_uentry.entry == -1) {
  3051. num_empty++;
  3052. if (num_empty == 1) {
  3053. p_fs->hint_uentry.clu.dir = clu.dir;
  3054. p_fs->hint_uentry.clu.size = clu.size;
  3055. p_fs->hint_uentry.clu.flags = clu.flags;
  3056. }
  3057. if ((num_empty >= num_entries) || (entry_type == TYPE_UNUSED)) {
  3058. p_fs->hint_uentry.entry = dentry - (num_empty-1);
  3059. }
  3060. }
  3061. if (entry_type == TYPE_UNUSED) {
  3062. return -2;
  3063. }
  3064. } else {
  3065. num_empty = 0;
  3066. if ((entry_type == TYPE_FILE) || (entry_type == TYPE_DIR)) {
  3067. if ((type == TYPE_ALL) || (type == entry_type)) {
  3068. file_ep = (FILE_DENTRY_T *) ep;
  3069. num_ext_entries = file_ep->num_ext;
  3070. is_feasible_entry = TRUE;
  3071. } else {
  3072. is_feasible_entry = FALSE;
  3073. }
  3074. } else if (entry_type == TYPE_STREAM) {
  3075. if (is_feasible_entry) {
  3076. strm_ep = (STRM_DENTRY_T *) ep;
  3077. if ((p_uniname->name_hash == GET16_A(strm_ep->name_hash)) &&
  3078. (p_uniname->name_len == strm_ep->name_len)) {
  3079. order = 1;
  3080. } else {
  3081. is_feasible_entry = FALSE;
  3082. }
  3083. }
  3084. } else if (entry_type == TYPE_EXTEND) {
  3085. if (is_feasible_entry) {
  3086. name_ep = (NAME_DENTRY_T *) ep;
  3087. if ((++order) == 2)
  3088. uniname = p_uniname->name;
  3089. else
  3090. uniname += 15;
  3091. len = extract_uni_name_from_name_entry(name_ep, entry_uniname, order);
  3092. unichar = *(uniname+len);
  3093. *(uniname+len) = 0x0;
  3094. if (nls_uniname_cmp(sb, uniname, entry_uniname)) {
  3095. is_feasible_entry = FALSE;
  3096. } else if (order == num_ext_entries) {
  3097. p_fs->hint_uentry.dir = CLUSTER_32(~0);
  3098. p_fs->hint_uentry.entry = -1;
  3099. return(dentry - (num_ext_entries));
  3100. }
  3101. *(uniname+len) = unichar;
  3102. }
  3103. } else {
  3104. is_feasible_entry = FALSE;
  3105. }
  3106. }
  3107. }
  3108. if (p_dir->dir == CLUSTER_32(0))
  3109. break;
  3110. if (clu.flags == 0x03) {
  3111. if ((--clu.size) > 0)
  3112. clu.dir++;
  3113. else
  3114. clu.dir = CLUSTER_32(~0);
  3115. } else {
  3116. if (FAT_read(sb, clu.dir, &(clu.dir)) != 0)
  3117. return -2;
  3118. }
  3119. }
  3120. return -2;
  3121. }
  3122. INT32 fat_count_ext_entries(struct super_block *sb, CHAIN_T *p_dir, INT32 entry, DENTRY_T *p_entry)
  3123. {
  3124. INT32 count = 0;
  3125. UINT8 chksum;
  3126. DOS_DENTRY_T *dos_ep = (DOS_DENTRY_T *) p_entry;
  3127. EXT_DENTRY_T *ext_ep;
  3128. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  3129. chksum = calc_checksum_1byte((void *) dos_ep->name, DOS_NAME_LENGTH, 0);
  3130. for (entry--; entry >= 0; entry--) {
  3131. ext_ep = (EXT_DENTRY_T *) get_entry_in_dir(sb, p_dir, entry, NULL);
  3132. if (!ext_ep)
  3133. return -1;
  3134. if ((p_fs->fs_func->get_entry_type((DENTRY_T *) ext_ep) == TYPE_EXTEND) &&
  3135. (ext_ep->checksum == chksum)) {
  3136. count++;
  3137. if (ext_ep->order > 0x40)
  3138. return(count);
  3139. } else {
  3140. return(count);
  3141. }
  3142. }
  3143. return(count);
  3144. }
  3145. INT32 exfat_count_ext_entries(struct super_block *sb, CHAIN_T *p_dir, INT32 entry, DENTRY_T *p_entry)
  3146. {
  3147. INT32 i, count = 0;
  3148. UINT32 type;
  3149. FILE_DENTRY_T *file_ep = (FILE_DENTRY_T *) p_entry;
  3150. DENTRY_T *ext_ep;
  3151. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  3152. for (i = 0, entry++; i < file_ep->num_ext; i++, entry++) {
  3153. ext_ep = get_entry_in_dir(sb, p_dir, entry, NULL);
  3154. if (!ext_ep)
  3155. return -1;
  3156. type = p_fs->fs_func->get_entry_type(ext_ep);
  3157. if ((type == TYPE_EXTEND) || (type == TYPE_STREAM)) {
  3158. count++;
  3159. } else {
  3160. return(count);
  3161. }
  3162. }
  3163. return(count);
  3164. }
  3165. INT32 count_dos_name_entries(struct super_block *sb, CHAIN_T *p_dir, UINT32 type)
  3166. {
  3167. INT32 i, count = 0;
  3168. INT32 dentries_per_clu;
  3169. UINT32 entry_type;
  3170. CHAIN_T clu;
  3171. DENTRY_T *ep;
  3172. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  3173. if (p_dir->dir == CLUSTER_32(0))
  3174. dentries_per_clu = p_fs->dentries_in_root;
  3175. else
  3176. dentries_per_clu = p_fs->dentries_per_clu;
  3177. clu.dir = p_dir->dir;
  3178. clu.size = p_dir->size;
  3179. clu.flags = p_dir->flags;
  3180. while (clu.dir != CLUSTER_32(~0)) {
  3181. if (p_fs->dev_ejected)
  3182. break;
  3183. for (i = 0; i < dentries_per_clu; i++) {
  3184. ep = get_entry_in_dir(sb, &clu, i, NULL);
  3185. if (!ep)
  3186. return -1;
  3187. entry_type = p_fs->fs_func->get_entry_type(ep);
  3188. if (entry_type == TYPE_UNUSED)
  3189. return(count);
  3190. if (!(type & TYPE_CRITICAL_PRI) && !(type & TYPE_BENIGN_PRI))
  3191. continue;
  3192. if ((type == TYPE_ALL) || (type == entry_type))
  3193. count++;
  3194. }
  3195. if (p_dir->dir == CLUSTER_32(0))
  3196. break;
  3197. if (clu.flags == 0x03) {
  3198. if ((--clu.size) > 0)
  3199. clu.dir++;
  3200. else
  3201. clu.dir = CLUSTER_32(~0);
  3202. } else {
  3203. if (FAT_read(sb, clu.dir, &(clu.dir)) != 0)
  3204. return -1;
  3205. }
  3206. }
  3207. return(count);
  3208. }
  3209. BOOL is_dir_empty(struct super_block *sb, CHAIN_T *p_dir)
  3210. {
  3211. INT32 i, count = 0;
  3212. INT32 dentries_per_clu;
  3213. UINT32 type;
  3214. CHAIN_T clu;
  3215. DENTRY_T *ep;
  3216. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  3217. if (p_dir->dir == CLUSTER_32(0))
  3218. dentries_per_clu = p_fs->dentries_in_root;
  3219. else
  3220. dentries_per_clu = p_fs->dentries_per_clu;
  3221. clu.dir = p_dir->dir;
  3222. clu.size = p_dir->size;
  3223. clu.flags = p_dir->flags;
  3224. while (clu.dir != CLUSTER_32(~0)) {
  3225. if (p_fs->dev_ejected)
  3226. break;
  3227. for (i = 0; i < dentries_per_clu; i++) {
  3228. ep = get_entry_in_dir(sb, &clu, i, NULL);
  3229. if (!ep)
  3230. break;
  3231. type = p_fs->fs_func->get_entry_type(ep);
  3232. if (type == TYPE_UNUSED)
  3233. return TRUE;
  3234. if ((type != TYPE_FILE) && (type != TYPE_DIR))
  3235. continue;
  3236. if (p_dir->dir == CLUSTER_32(0)) {
  3237. return FALSE;
  3238. } else {
  3239. if (p_fs->vol_type == EXFAT)
  3240. return FALSE;
  3241. if ((p_dir->dir == p_fs->root_dir) || ((++count) > 2))
  3242. return FALSE;
  3243. }
  3244. }
  3245. if (p_dir->dir == CLUSTER_32(0))
  3246. break;
  3247. if (clu.flags == 0x03) {
  3248. if ((--clu.size) > 0)
  3249. clu.dir++;
  3250. else
  3251. clu.dir = CLUSTER_32(~0);
  3252. } else {
  3253. if (FAT_read(sb, clu.dir, &(clu.dir)) != 0)
  3254. break;
  3255. }
  3256. }
  3257. return TRUE;
  3258. }
  3259. INT32 get_num_entries_and_dos_name(struct super_block *sb, CHAIN_T *p_dir, UNI_NAME_T *p_uniname, INT32 *entries, DOS_NAME_T *p_dosname)
  3260. {
  3261. INT32 ret, num_entries, lossy = FALSE;
  3262. INT8 **r;
  3263. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  3264. num_entries = p_fs->fs_func->calc_num_entries(p_uniname);
  3265. if (num_entries == 0)
  3266. return FFS_INVALIDPATH;
  3267. if (p_fs->vol_type != EXFAT) {
  3268. nls_uniname_to_dosname(sb, p_dosname, p_uniname, &lossy);
  3269. if (lossy) {
  3270. ret = fat_generate_dos_name(sb, p_dir, p_dosname);
  3271. if (ret)
  3272. return ret;
  3273. } else {
  3274. for (r = reserved_names; *r; r++) {
  3275. if (!STRNCMP((void *) p_dosname->name, *r, 8))
  3276. return FFS_INVALIDPATH;
  3277. }
  3278. if (p_dosname->name_case != 0xFF)
  3279. num_entries = 1;
  3280. }
  3281. if (num_entries > 1)
  3282. p_dosname->name_case = 0x0;
  3283. }
  3284. *entries = num_entries;
  3285. return FFS_SUCCESS;
  3286. }
  3287. void get_uni_name_from_dos_entry(struct super_block *sb, DOS_DENTRY_T *ep, UNI_NAME_T *p_uniname, UINT8 mode)
  3288. {
  3289. DOS_NAME_T dos_name;
  3290. if (mode == 0x0)
  3291. dos_name.name_case = 0x0;
  3292. else
  3293. dos_name.name_case = ep->lcase;
  3294. MEMCPY(dos_name.name, ep->name, DOS_NAME_LENGTH);
  3295. nls_dosname_to_uniname(sb, p_uniname, &dos_name);
  3296. }
  3297. void fat_get_uni_name_from_ext_entry(struct super_block *sb, CHAIN_T *p_dir, INT32 entry, UINT16 *uniname)
  3298. {
  3299. INT32 i;
  3300. EXT_DENTRY_T *ep;
  3301. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  3302. for (entry--, i = 1; entry >= 0; entry--, i++) {
  3303. ep = (EXT_DENTRY_T *) get_entry_in_dir(sb, p_dir, entry, NULL);
  3304. if (!ep)
  3305. return;
  3306. if (p_fs->fs_func->get_entry_type((DENTRY_T *) ep) == TYPE_EXTEND) {
  3307. extract_uni_name_from_ext_entry(ep, uniname, i);
  3308. if (ep->order > 0x40)
  3309. return;
  3310. } else {
  3311. return;
  3312. }
  3313. uniname += 13;
  3314. }
  3315. }
  3316. void exfat_get_uni_name_from_ext_entry(struct super_block *sb, CHAIN_T *p_dir, INT32 entry, UINT16 *uniname)
  3317. {
  3318. INT32 i;
  3319. DENTRY_T *ep;
  3320. ENTRY_SET_CACHE_T *es;
  3321. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  3322. es = get_entry_set_in_dir(sb, p_dir, entry, ES_ALL_ENTRIES, &ep);
  3323. if (es == NULL || es->num_entries < 3) {
  3324. if(es) {
  3325. release_entry_set(es);
  3326. }
  3327. return;
  3328. }
  3329. ep += 2;
  3330. for (i = 2; i < es->num_entries; i++, ep++) {
  3331. if (p_fs->fs_func->get_entry_type(ep) == TYPE_EXTEND) {
  3332. extract_uni_name_from_name_entry((NAME_DENTRY_T *)ep, uniname, i);
  3333. } else {
  3334. goto out;
  3335. }
  3336. uniname += 15;
  3337. }
  3338. out:
  3339. release_entry_set(es);
  3340. }
  3341. INT32 extract_uni_name_from_ext_entry(EXT_DENTRY_T *ep, UINT16 *uniname, INT32 order)
  3342. {
  3343. INT32 i, len = 0;
  3344. for (i = 0; i < 10; i += 2) {
  3345. *uniname = GET16(ep->unicode_0_4+i);
  3346. if (*uniname == 0x0)
  3347. return(len);
  3348. uniname++;
  3349. len++;
  3350. }
  3351. if (order < 20) {
  3352. for (i = 0; i < 12; i += 2) {
  3353. *uniname = GET16_A(ep->unicode_5_10+i);
  3354. if (*uniname == 0x0)
  3355. return(len);
  3356. uniname++;
  3357. len++;
  3358. }
  3359. } else {
  3360. for (i = 0; i < 8; i += 2) {
  3361. *uniname = GET16_A(ep->unicode_5_10+i);
  3362. if (*uniname == 0x0)
  3363. return(len);
  3364. uniname++;
  3365. len++;
  3366. }
  3367. *uniname = 0x0;
  3368. return(len);
  3369. }
  3370. for (i = 0; i < 4; i += 2) {
  3371. *uniname = GET16_A(ep->unicode_11_12+i);
  3372. if (*uniname == 0x0)
  3373. return(len);
  3374. uniname++;
  3375. len++;
  3376. }
  3377. *uniname = 0x0;
  3378. return(len);
  3379. }
  3380. INT32 extract_uni_name_from_name_entry(NAME_DENTRY_T *ep, UINT16 *uniname, INT32 order)
  3381. {
  3382. INT32 i, len = 0;
  3383. for (i = 0; i < 30; i += 2) {
  3384. *uniname = GET16_A(ep->unicode_0_14+i);
  3385. if (*uniname == 0x0)
  3386. return(len);
  3387. uniname++;
  3388. len++;
  3389. }
  3390. *uniname = 0x0;
  3391. return(len);
  3392. }
  3393. INT32 fat_generate_dos_name(struct super_block *sb, CHAIN_T *p_dir, DOS_NAME_T *p_dosname)
  3394. {
  3395. INT32 i, j, count = 0, count_begin = FALSE;
  3396. INT32 dentries_per_clu;
  3397. UINT32 type;
  3398. UINT8 bmap[128];
  3399. CHAIN_T clu;
  3400. DOS_DENTRY_T *ep;
  3401. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  3402. Bitmap_clear_all(bmap, 128);
  3403. Bitmap_set(bmap, 0);
  3404. if (p_dir->dir == CLUSTER_32(0))
  3405. dentries_per_clu = p_fs->dentries_in_root;
  3406. else
  3407. dentries_per_clu = p_fs->dentries_per_clu;
  3408. clu.dir = p_dir->dir;
  3409. clu.flags = p_dir->flags;
  3410. while (clu.dir != CLUSTER_32(~0)) {
  3411. if (p_fs->dev_ejected)
  3412. break;
  3413. for (i = 0; i < dentries_per_clu; i++) {
  3414. ep = (DOS_DENTRY_T *) get_entry_in_dir(sb, &clu, i, NULL);
  3415. if (!ep)
  3416. return FFS_MEDIAERR;
  3417. type = p_fs->fs_func->get_entry_type((DENTRY_T *) ep);
  3418. if (type == TYPE_UNUSED)
  3419. break;
  3420. if ((type != TYPE_FILE) && (type != TYPE_DIR))
  3421. continue;
  3422. count = 0;
  3423. count_begin = FALSE;
  3424. for (j = 0; j < 8; j++) {
  3425. if (ep->name[j] == ' ')
  3426. break;
  3427. if (ep->name[j] == '~') {
  3428. count_begin = TRUE;
  3429. } else if (count_begin) {
  3430. if ((ep->name[j] >= '0') && (ep->name[j] <= '9')) {
  3431. count = count * 10 + (ep->name[j] - '0');
  3432. } else {
  3433. count = 0;
  3434. count_begin = FALSE;
  3435. }
  3436. }
  3437. }
  3438. if ((count > 0) && (count < 1024))
  3439. Bitmap_set(bmap, count);
  3440. }
  3441. if (p_dir->dir == CLUSTER_32(0))
  3442. break;
  3443. if (FAT_read(sb, clu.dir, &(clu.dir)) != 0)
  3444. return FFS_MEDIAERR;
  3445. }
  3446. count = 0;
  3447. for (i = 0; i < 128; i++) {
  3448. if (bmap[i] != 0xFF) {
  3449. for (j = 0; j < 8; j++) {
  3450. if (Bitmap_test(&(bmap[i]), j) == 0) {
  3451. count = (i << 3) + j;
  3452. break;
  3453. }
  3454. }
  3455. if (count != 0)
  3456. break;
  3457. }
  3458. }
  3459. if ((count == 0) || (count >= 1024))
  3460. return FFS_FILEEXIST;
  3461. else
  3462. fat_attach_count_to_dos_name(p_dosname->name, count);
  3463. return FFS_SUCCESS;
  3464. }
  3465. void fat_attach_count_to_dos_name(UINT8 *dosname, INT32 count)
  3466. {
  3467. INT32 i, j, length;
  3468. INT8 str_count[6];
  3469. str_count[0] = '~';
  3470. str_count[1] = '\0';
  3471. my_itoa(&(str_count[1]), count);
  3472. length = STRLEN(str_count);
  3473. i = j = 0;
  3474. while (j <= (8 - length)) {
  3475. i = j;
  3476. if (dosname[j] == ' ')
  3477. break;
  3478. if (dosname[j] & 0x80)
  3479. j += 2;
  3480. else
  3481. j++;
  3482. }
  3483. for (j = 0; j < length; i++, j++)
  3484. dosname[i] = (UINT8) str_count[j];
  3485. if (i == 7)
  3486. dosname[7] = ' ';
  3487. }
  3488. INT32 fat_calc_num_entries(UNI_NAME_T *p_uniname)
  3489. {
  3490. INT32 len;
  3491. len = p_uniname->name_len;
  3492. if (len == 0)
  3493. return 0;
  3494. return((len-1) / 13 + 2);
  3495. }
  3496. INT32 exfat_calc_num_entries(UNI_NAME_T *p_uniname)
  3497. {
  3498. INT32 len;
  3499. len = p_uniname->name_len;
  3500. if (len == 0)
  3501. return 0;
  3502. return((len-1) / 15 + 3);
  3503. }
  3504. UINT8 calc_checksum_1byte(void *data, INT32 len, UINT8 chksum)
  3505. {
  3506. INT32 i;
  3507. UINT8 *c = (UINT8 *) data;
  3508. for (i = 0; i < len; i++, c++)
  3509. chksum = (((chksum & 1) << 7) | ((chksum & 0xFE) >> 1)) + *c;
  3510. return(chksum);
  3511. }
  3512. UINT16 calc_checksum_2byte(void *data, INT32 len, UINT16 chksum, INT32 type)
  3513. {
  3514. INT32 i;
  3515. UINT8 *c = (UINT8 *) data;
  3516. switch (type) {
  3517. case CS_DIR_ENTRY:
  3518. for (i = 0; i < len; i++, c++) {
  3519. if ((i == 2) || (i == 3))
  3520. continue;
  3521. chksum = (((chksum & 1) << 15) | ((chksum & 0xFFFE) >> 1)) + (UINT16) *c;
  3522. }
  3523. break;
  3524. default
  3525. :
  3526. for (i = 0; i < len; i++, c++) {
  3527. chksum = (((chksum & 1) << 15) | ((chksum & 0xFFFE) >> 1)) + (UINT16) *c;
  3528. }
  3529. }
  3530. return(chksum);
  3531. }
  3532. UINT32 calc_checksum_4byte(void *data, INT32 len, UINT32 chksum, INT32 type)
  3533. {
  3534. INT32 i;
  3535. UINT8 *c = (UINT8 *) data;
  3536. switch (type) {
  3537. case CS_PBR_SECTOR:
  3538. for (i = 0; i < len; i++, c++) {
  3539. if ((i == 106) || (i == 107) || (i == 112))
  3540. continue;
  3541. chksum = (((chksum & 1) << 31) | ((chksum & 0xFFFFFFFE) >> 1)) + (UINT32) *c;
  3542. }
  3543. break;
  3544. default
  3545. :
  3546. for (i = 0; i < len; i++, c++) {
  3547. chksum = (((chksum & 1) << 31) | ((chksum & 0xFFFFFFFE) >> 1)) + (UINT32) *c;
  3548. }
  3549. }
  3550. return(chksum);
  3551. }
  3552. INT32 resolve_path(struct inode *inode, UINT8 *path, CHAIN_T *p_dir, UNI_NAME_T *p_uniname)
  3553. {
  3554. INT32 lossy = FALSE;
  3555. struct super_block *sb = inode->i_sb;
  3556. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  3557. FILE_ID_T *fid = &(EXFAT_I(inode)->fid);
  3558. if (STRLEN(path) >= (MAX_NAME_LENGTH * MAX_CHARSET_SIZE))
  3559. return(FFS_INVALIDPATH);
  3560. STRCPY(name_buf, path);
  3561. nls_cstring_to_uniname(sb, p_uniname, name_buf, &lossy);
  3562. if (lossy)
  3563. return(FFS_INVALIDPATH);
  3564. fid->size = i_size_read(inode);
  3565. p_dir->dir = fid->start_clu;
  3566. p_dir->size = (INT32)(fid->size >> p_fs->cluster_size_bits);
  3567. p_dir->flags = fid->flags;
  3568. return(FFS_SUCCESS);
  3569. }
  3570. static FS_FUNC_T fat_fs_func = {
  3571. .alloc_cluster = fat_alloc_cluster,
  3572. .free_cluster = fat_free_cluster,
  3573. .count_used_clusters = fat_count_used_clusters,
  3574. .init_dir_entry = fat_init_dir_entry,
  3575. .init_ext_entry = fat_init_ext_entry,
  3576. .find_dir_entry = fat_find_dir_entry,
  3577. .delete_dir_entry = fat_delete_dir_entry,
  3578. .get_uni_name_from_ext_entry = fat_get_uni_name_from_ext_entry,
  3579. .count_ext_entries = fat_count_ext_entries,
  3580. .calc_num_entries = fat_calc_num_entries,
  3581. .get_entry_type = fat_get_entry_type,
  3582. .set_entry_type = fat_set_entry_type,
  3583. .get_entry_attr = fat_get_entry_attr,
  3584. .set_entry_attr = fat_set_entry_attr,
  3585. .get_entry_flag = fat_get_entry_flag,
  3586. .set_entry_flag = fat_set_entry_flag,
  3587. .get_entry_clu0 = fat_get_entry_clu0,
  3588. .set_entry_clu0 = fat_set_entry_clu0,
  3589. .get_entry_size = fat_get_entry_size,
  3590. .set_entry_size = fat_set_entry_size,
  3591. .get_entry_time = fat_get_entry_time,
  3592. .set_entry_time = fat_set_entry_time,
  3593. };
  3594. INT32 fat16_mount(struct super_block *sb, PBR_SECTOR_T *p_pbr)
  3595. {
  3596. INT32 num_reserved, num_root_sectors;
  3597. BPB16_T *p_bpb = (BPB16_T *) p_pbr->bpb;
  3598. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  3599. BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
  3600. if (p_bpb->num_fats == 0)
  3601. return FFS_FORMATERR;
  3602. num_root_sectors = GET16(p_bpb->num_root_entries) << DENTRY_SIZE_BITS;
  3603. num_root_sectors = ((num_root_sectors-1) >> p_bd->sector_size_bits) + 1;
  3604. p_fs->sectors_per_clu = p_bpb->sectors_per_clu;
  3605. p_fs->sectors_per_clu_bits = my_log2(p_bpb->sectors_per_clu);
  3606. p_fs->cluster_size_bits = p_fs->sectors_per_clu_bits + p_bd->sector_size_bits;
  3607. p_fs->cluster_size = 1 << p_fs->cluster_size_bits;
  3608. p_fs->num_FAT_sectors = GET16(p_bpb->num_fat_sectors);
  3609. p_fs->FAT1_start_sector = p_fs->PBR_sector + GET16(p_bpb->num_reserved);
  3610. if (p_bpb->num_fats == 1)
  3611. p_fs->FAT2_start_sector = p_fs->FAT1_start_sector;
  3612. else
  3613. p_fs->FAT2_start_sector = p_fs->FAT1_start_sector + p_fs->num_FAT_sectors;
  3614. p_fs->root_start_sector = p_fs->FAT2_start_sector + p_fs->num_FAT_sectors;
  3615. p_fs->data_start_sector = p_fs->root_start_sector + num_root_sectors;
  3616. p_fs->num_sectors = GET16(p_bpb->num_sectors);
  3617. if (p_fs->num_sectors == 0)
  3618. p_fs->num_sectors = GET32(p_bpb->num_huge_sectors);
  3619. num_reserved = p_fs->data_start_sector - p_fs->PBR_sector;
  3620. p_fs->num_clusters = ((p_fs->num_sectors - num_reserved) >> p_fs->sectors_per_clu_bits) + 2;
  3621. if (p_fs->num_clusters < FAT12_THRESHOLD)
  3622. p_fs->vol_type = FAT12;
  3623. else
  3624. p_fs->vol_type = FAT16;
  3625. p_fs->vol_id = GET32(p_bpb->vol_serial);
  3626. p_fs->root_dir = 0;
  3627. p_fs->dentries_in_root = GET16(p_bpb->num_root_entries);
  3628. p_fs->dentries_per_clu = 1 << (p_fs->cluster_size_bits - DENTRY_SIZE_BITS);
  3629. p_fs->vol_flag = VOL_CLEAN;
  3630. p_fs->clu_srch_ptr = 2;
  3631. p_fs->used_clusters = (UINT32) ~0;
  3632. p_fs->fs_func = &fat_fs_func;
  3633. return FFS_SUCCESS;
  3634. }
  3635. INT32 fat32_mount(struct super_block *sb, PBR_SECTOR_T *p_pbr)
  3636. {
  3637. INT32 num_reserved;
  3638. BPB32_T *p_bpb = (BPB32_T *) p_pbr->bpb;
  3639. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  3640. BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
  3641. if (p_bpb->num_fats == 0)
  3642. return FFS_FORMATERR;
  3643. p_fs->sectors_per_clu = p_bpb->sectors_per_clu;
  3644. p_fs->sectors_per_clu_bits = my_log2(p_bpb->sectors_per_clu);
  3645. p_fs->cluster_size_bits = p_fs->sectors_per_clu_bits + p_bd->sector_size_bits;
  3646. p_fs->cluster_size = 1 << p_fs->cluster_size_bits;
  3647. p_fs->num_FAT_sectors = GET32(p_bpb->num_fat32_sectors);
  3648. p_fs->FAT1_start_sector = p_fs->PBR_sector + GET16(p_bpb->num_reserved);
  3649. if (p_bpb->num_fats == 1)
  3650. p_fs->FAT2_start_sector = p_fs->FAT1_start_sector;
  3651. else
  3652. p_fs->FAT2_start_sector = p_fs->FAT1_start_sector + p_fs->num_FAT_sectors;
  3653. p_fs->root_start_sector = p_fs->FAT2_start_sector + p_fs->num_FAT_sectors;
  3654. p_fs->data_start_sector = p_fs->root_start_sector;
  3655. p_fs->num_sectors = GET32(p_bpb->num_huge_sectors);
  3656. num_reserved = p_fs->data_start_sector - p_fs->PBR_sector;
  3657. p_fs->num_clusters = ((p_fs->num_sectors-num_reserved) >> p_fs->sectors_per_clu_bits) + 2;
  3658. p_fs->vol_type = FAT32;
  3659. p_fs->vol_id = GET32(p_bpb->vol_serial);
  3660. p_fs->root_dir = GET32(p_bpb->root_cluster);
  3661. p_fs->dentries_in_root = 0;
  3662. p_fs->dentries_per_clu = 1 << (p_fs->cluster_size_bits - DENTRY_SIZE_BITS);
  3663. p_fs->vol_flag = VOL_CLEAN;
  3664. p_fs->clu_srch_ptr = 2;
  3665. p_fs->used_clusters = (UINT32) ~0;
  3666. p_fs->fs_func = &fat_fs_func;
  3667. return FFS_SUCCESS;
  3668. }
  3669. static FS_FUNC_T exfat_fs_func = {
  3670. .alloc_cluster = exfat_alloc_cluster,
  3671. .free_cluster = exfat_free_cluster,
  3672. .count_used_clusters = exfat_count_used_clusters,
  3673. .init_dir_entry = exfat_init_dir_entry,
  3674. .init_ext_entry = exfat_init_ext_entry,
  3675. .find_dir_entry = exfat_find_dir_entry,
  3676. .delete_dir_entry = exfat_delete_dir_entry,
  3677. .get_uni_name_from_ext_entry = exfat_get_uni_name_from_ext_entry,
  3678. .count_ext_entries = exfat_count_ext_entries,
  3679. .calc_num_entries = exfat_calc_num_entries,
  3680. .get_entry_type = exfat_get_entry_type,
  3681. .set_entry_type = exfat_set_entry_type,
  3682. .get_entry_attr = exfat_get_entry_attr,
  3683. .set_entry_attr = exfat_set_entry_attr,
  3684. .get_entry_flag = exfat_get_entry_flag,
  3685. .set_entry_flag = exfat_set_entry_flag,
  3686. .get_entry_clu0 = exfat_get_entry_clu0,
  3687. .set_entry_clu0 = exfat_set_entry_clu0,
  3688. .get_entry_size = exfat_get_entry_size,
  3689. .set_entry_size = exfat_set_entry_size,
  3690. .get_entry_time = exfat_get_entry_time,
  3691. .set_entry_time = exfat_set_entry_time,
  3692. };
  3693. INT32 exfat_mount(struct super_block *sb, PBR_SECTOR_T *p_pbr)
  3694. {
  3695. BPBEX_T *p_bpb = (BPBEX_T *) p_pbr->bpb;
  3696. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  3697. BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
  3698. if (p_bpb->num_fats == 0)
  3699. return FFS_FORMATERR;
  3700. p_fs->sectors_per_clu = 1 << p_bpb->sectors_per_clu_bits;
  3701. p_fs->sectors_per_clu_bits = p_bpb->sectors_per_clu_bits;
  3702. p_fs->cluster_size_bits = p_fs->sectors_per_clu_bits + p_bd->sector_size_bits;
  3703. p_fs->cluster_size = 1 << p_fs->cluster_size_bits;
  3704. p_fs->num_FAT_sectors = GET32(p_bpb->fat_length);
  3705. p_fs->FAT1_start_sector = p_fs->PBR_sector + GET32(p_bpb->fat_offset);
  3706. if (p_bpb->num_fats == 1)
  3707. p_fs->FAT2_start_sector = p_fs->FAT1_start_sector;
  3708. else
  3709. p_fs->FAT2_start_sector = p_fs->FAT1_start_sector + p_fs->num_FAT_sectors;
  3710. p_fs->root_start_sector = p_fs->PBR_sector + GET32(p_bpb->clu_offset);
  3711. p_fs->data_start_sector = p_fs->root_start_sector;
  3712. p_fs->num_sectors = GET64(p_bpb->vol_length);
  3713. p_fs->num_clusters = GET32(p_bpb->clu_count) + 2;
  3714. p_fs->vol_type = EXFAT;
  3715. p_fs->vol_id = GET32(p_bpb->vol_serial);
  3716. p_fs->root_dir = GET32(p_bpb->root_cluster);
  3717. p_fs->dentries_in_root = 0;
  3718. p_fs->dentries_per_clu = 1 << (p_fs->cluster_size_bits - DENTRY_SIZE_BITS);
  3719. p_fs->vol_flag = (UINT32) GET16(p_bpb->vol_flags);
  3720. p_fs->clu_srch_ptr = 2;
  3721. p_fs->used_clusters = (UINT32) ~0;
  3722. p_fs->fs_func = &exfat_fs_func;
  3723. return FFS_SUCCESS;
  3724. }
  3725. INT32 create_dir(struct inode *inode, CHAIN_T *p_dir, UNI_NAME_T *p_uniname, FILE_ID_T *fid)
  3726. {
  3727. INT32 ret, dentry, num_entries;
  3728. UINT64 size;
  3729. CHAIN_T clu;
  3730. DOS_NAME_T dos_name, dot_name;
  3731. struct super_block *sb = inode->i_sb;
  3732. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  3733. ret = get_num_entries_and_dos_name(sb, p_dir, p_uniname, &num_entries, &dos_name);
  3734. if (ret)
  3735. return ret;
  3736. dentry = find_empty_entry(inode, p_dir, num_entries);
  3737. if (dentry < 0)
  3738. return FFS_FULL;
  3739. clu.dir = CLUSTER_32(~0);
  3740. clu.size = 0;
  3741. clu.flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
  3742. ret = p_fs->fs_func->alloc_cluster(sb, 1, &clu);
  3743. if (ret < 0)
  3744. return FFS_MEDIAERR;
  3745. else if(ret == 0)
  3746. return FFS_FULL;
  3747. ret = clear_cluster(sb, clu.dir);
  3748. if (ret != FFS_SUCCESS)
  3749. return ret;
  3750. if (p_fs->vol_type == EXFAT) {
  3751. size = p_fs->cluster_size;
  3752. } else {
  3753. size = 0;
  3754. dot_name.name_case = 0x0;
  3755. MEMCPY(dot_name.name, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH);
  3756. ret = p_fs->fs_func->init_dir_entry(sb, &clu, 0, TYPE_DIR, clu.dir, 0);
  3757. if (ret != FFS_SUCCESS)
  3758. return ret;
  3759. ret = p_fs->fs_func->init_ext_entry(sb, &clu, 0, 1, NULL, &dot_name);
  3760. if (ret != FFS_SUCCESS)
  3761. return ret;
  3762. MEMCPY(dot_name.name, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH);
  3763. if (p_dir->dir == p_fs->root_dir)
  3764. ret = p_fs->fs_func->init_dir_entry(sb, &clu, 1, TYPE_DIR, CLUSTER_32(0), 0);
  3765. else
  3766. ret = p_fs->fs_func->init_dir_entry(sb, &clu, 1, TYPE_DIR, p_dir->dir, 0);
  3767. if (ret != FFS_SUCCESS)
  3768. return ret;
  3769. ret = p_fs->fs_func->init_ext_entry(sb, &clu, 1, 1, NULL, &dot_name);
  3770. if (ret != FFS_SUCCESS)
  3771. return ret;
  3772. }
  3773. ret = p_fs->fs_func->init_dir_entry(sb, p_dir, dentry, TYPE_DIR, clu.dir, size);
  3774. if (ret != FFS_SUCCESS)
  3775. return ret;
  3776. ret = p_fs->fs_func->init_ext_entry(sb, p_dir, dentry, num_entries, p_uniname, &dos_name);
  3777. if (ret != FFS_SUCCESS)
  3778. return ret;
  3779. fid->dir.dir = p_dir->dir;
  3780. fid->dir.size = p_dir->size;
  3781. fid->dir.flags = p_dir->flags;
  3782. fid->entry = dentry;
  3783. fid->attr = ATTR_SUBDIR;
  3784. fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
  3785. fid->size = size;
  3786. fid->start_clu = clu.dir;
  3787. fid->type= TYPE_DIR;
  3788. fid->rwoffset = 0;
  3789. fid->hint_last_off = -1;
  3790. return FFS_SUCCESS;
  3791. }
  3792. INT32 create_file(struct inode *inode, CHAIN_T *p_dir, UNI_NAME_T *p_uniname, UINT8 mode, FILE_ID_T *fid)
  3793. {
  3794. INT32 ret, dentry, num_entries;
  3795. DOS_NAME_T dos_name;
  3796. struct super_block *sb = inode->i_sb;
  3797. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  3798. ret = get_num_entries_and_dos_name(sb, p_dir, p_uniname, &num_entries, &dos_name);
  3799. if (ret)
  3800. return ret;
  3801. dentry = find_empty_entry(inode, p_dir, num_entries);
  3802. if (dentry < 0)
  3803. return FFS_FULL;
  3804. ret = p_fs->fs_func->init_dir_entry(sb, p_dir, dentry, TYPE_FILE | mode, CLUSTER_32(0), 0);
  3805. if (ret != FFS_SUCCESS)
  3806. return ret;
  3807. ret = p_fs->fs_func->init_ext_entry(sb, p_dir, dentry, num_entries, p_uniname, &dos_name);
  3808. if (ret != FFS_SUCCESS)
  3809. return ret;
  3810. fid->dir.dir = p_dir->dir;
  3811. fid->dir.size = p_dir->size;
  3812. fid->dir.flags = p_dir->flags;
  3813. fid->entry = dentry;
  3814. fid->attr = ATTR_ARCHIVE | mode;
  3815. fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
  3816. fid->size = 0;
  3817. fid->start_clu = CLUSTER_32(~0);
  3818. fid->type= TYPE_FILE;
  3819. fid->rwoffset = 0;
  3820. fid->hint_last_off = -1;
  3821. return FFS_SUCCESS;
  3822. }
  3823. void remove_file(struct inode *inode, CHAIN_T *p_dir, INT32 entry)
  3824. {
  3825. INT32 num_entries;
  3826. UINT32 sector;
  3827. DENTRY_T *ep;
  3828. struct super_block *sb = inode->i_sb;
  3829. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  3830. ep = get_entry_in_dir(sb, p_dir, entry, &sector);
  3831. if (!ep)
  3832. return;
  3833. buf_lock(sb, sector);
  3834. num_entries = p_fs->fs_func->count_ext_entries(sb, p_dir, entry, ep);
  3835. if (num_entries < 0) {
  3836. buf_unlock(sb, sector);
  3837. return;
  3838. }
  3839. num_entries++;
  3840. buf_unlock(sb, sector);
  3841. p_fs->fs_func->delete_dir_entry(sb, p_dir, entry, 0, num_entries);
  3842. }
  3843. INT32 rename_file(struct inode *inode, CHAIN_T *p_dir, INT32 oldentry, UNI_NAME_T *p_uniname, FILE_ID_T *fid)
  3844. {
  3845. INT32 ret, newentry = -1, num_old_entries, num_new_entries;
  3846. UINT32 sector_old, sector_new;
  3847. DOS_NAME_T dos_name;
  3848. DENTRY_T *epold, *epnew;
  3849. struct super_block *sb = inode->i_sb;
  3850. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  3851. epold = get_entry_in_dir(sb, p_dir, oldentry, &sector_old);
  3852. if (!epold)
  3853. return FFS_MEDIAERR;
  3854. buf_lock(sb, sector_old);
  3855. num_old_entries = p_fs->fs_func->count_ext_entries(sb, p_dir, oldentry, epold);
  3856. if (num_old_entries < 0) {
  3857. buf_unlock(sb, sector_old);
  3858. return FFS_MEDIAERR;
  3859. }
  3860. num_old_entries++;
  3861. ret = get_num_entries_and_dos_name(sb, p_dir, p_uniname, &num_new_entries, &dos_name);
  3862. if (ret) {
  3863. buf_unlock(sb, sector_old);
  3864. return ret;
  3865. }
  3866. if (num_old_entries < num_new_entries) {
  3867. newentry = find_empty_entry(inode, p_dir, num_new_entries);
  3868. if (newentry < 0) {
  3869. buf_unlock(sb, sector_old);
  3870. return FFS_FULL;
  3871. }
  3872. epnew = get_entry_in_dir(sb, p_dir, newentry, &sector_new);
  3873. if (!epnew) {
  3874. buf_unlock(sb, sector_old);
  3875. return FFS_MEDIAERR;
  3876. }
  3877. MEMCPY((void *) epnew, (void *) epold, DENTRY_SIZE);
  3878. if (p_fs->fs_func->get_entry_type(epnew) == TYPE_FILE) {
  3879. p_fs->fs_func->set_entry_attr(epnew, p_fs->fs_func->get_entry_attr(epnew) | ATTR_ARCHIVE);
  3880. fid->attr |= ATTR_ARCHIVE;
  3881. }
  3882. buf_modify(sb, sector_new);
  3883. buf_unlock(sb, sector_old);
  3884. if (p_fs->vol_type == EXFAT) {
  3885. epold = get_entry_in_dir(sb, p_dir, oldentry+1, &sector_old);
  3886. buf_lock(sb, sector_old);
  3887. epnew = get_entry_in_dir(sb, p_dir, newentry+1, &sector_new);
  3888. if (!epold || !epnew) {
  3889. buf_unlock(sb, sector_old);
  3890. return FFS_MEDIAERR;
  3891. }
  3892. MEMCPY((void *) epnew, (void *) epold, DENTRY_SIZE);
  3893. buf_modify(sb, sector_new);
  3894. buf_unlock(sb, sector_old);
  3895. }
  3896. ret = p_fs->fs_func->init_ext_entry(sb, p_dir, newentry, num_new_entries, p_uniname, &dos_name);
  3897. if (ret != FFS_SUCCESS)
  3898. return ret;
  3899. p_fs->fs_func->delete_dir_entry(sb, p_dir, oldentry, 0, num_old_entries);
  3900. fid->entry = newentry;
  3901. } else {
  3902. if (p_fs->fs_func->get_entry_type(epold) == TYPE_FILE) {
  3903. p_fs->fs_func->set_entry_attr(epold, p_fs->fs_func->get_entry_attr(epold) | ATTR_ARCHIVE);
  3904. fid->attr |= ATTR_ARCHIVE;
  3905. }
  3906. buf_modify(sb, sector_old);
  3907. buf_unlock(sb, sector_old);
  3908. ret = p_fs->fs_func->init_ext_entry(sb, p_dir, oldentry, num_new_entries, p_uniname, &dos_name);
  3909. if (ret != FFS_SUCCESS)
  3910. return ret;
  3911. p_fs->fs_func->delete_dir_entry(sb, p_dir, oldentry, num_new_entries, num_old_entries);
  3912. }
  3913. return FFS_SUCCESS;
  3914. }
  3915. INT32 move_file(struct inode *inode, CHAIN_T *p_olddir, INT32 oldentry, CHAIN_T *p_newdir, UNI_NAME_T *p_uniname, FILE_ID_T *fid)
  3916. {
  3917. INT32 ret, newentry, num_new_entries, num_old_entries;
  3918. UINT32 sector_mov, sector_new;
  3919. CHAIN_T clu;
  3920. DOS_NAME_T dos_name;
  3921. DENTRY_T *epmov, *epnew;
  3922. struct super_block *sb = inode->i_sb;
  3923. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  3924. epmov = get_entry_in_dir(sb, p_olddir, oldentry, &sector_mov);
  3925. if (!epmov)
  3926. return FFS_MEDIAERR;
  3927. if (p_fs->fs_func->get_entry_type(epmov) == TYPE_DIR &&
  3928. p_fs->fs_func->get_entry_clu0(epmov) == p_newdir->dir)
  3929. return FFS_INVALIDPATH;
  3930. buf_lock(sb, sector_mov);
  3931. num_old_entries = p_fs->fs_func->count_ext_entries(sb, p_olddir, oldentry, epmov);
  3932. if (num_old_entries < 0) {
  3933. buf_unlock(sb, sector_mov);
  3934. return FFS_MEDIAERR;
  3935. }
  3936. num_old_entries++;
  3937. ret = get_num_entries_and_dos_name(sb, p_newdir, p_uniname, &num_new_entries, &dos_name);
  3938. if (ret) {
  3939. buf_unlock(sb, sector_mov);
  3940. return ret;
  3941. }
  3942. newentry = find_empty_entry(inode, p_newdir, num_new_entries);
  3943. if (newentry < 0) {
  3944. buf_unlock(sb, sector_mov);
  3945. return FFS_FULL;
  3946. }
  3947. epnew = get_entry_in_dir(sb, p_newdir, newentry, &sector_new);
  3948. if (!epnew) {
  3949. buf_unlock(sb, sector_mov);
  3950. return FFS_MEDIAERR;
  3951. }
  3952. MEMCPY((void *) epnew, (void *) epmov, DENTRY_SIZE);
  3953. if (p_fs->fs_func->get_entry_type(epnew) == TYPE_FILE) {
  3954. p_fs->fs_func->set_entry_attr(epnew, p_fs->fs_func->get_entry_attr(epnew) | ATTR_ARCHIVE);
  3955. fid->attr |= ATTR_ARCHIVE;
  3956. }
  3957. buf_modify(sb, sector_new);
  3958. buf_unlock(sb, sector_mov);
  3959. if (p_fs->vol_type == EXFAT) {
  3960. epmov = get_entry_in_dir(sb, p_olddir, oldentry+1, &sector_mov);
  3961. buf_lock(sb, sector_mov);
  3962. epnew = get_entry_in_dir(sb, p_newdir, newentry+1, &sector_new);
  3963. if (!epmov || !epnew) {
  3964. buf_unlock(sb, sector_mov);
  3965. return FFS_MEDIAERR;
  3966. }
  3967. MEMCPY((void *) epnew, (void *) epmov, DENTRY_SIZE);
  3968. buf_modify(sb, sector_new);
  3969. buf_unlock(sb, sector_mov);
  3970. } else if (p_fs->fs_func->get_entry_type(epnew) == TYPE_DIR) {
  3971. clu.dir = p_fs->fs_func->get_entry_clu0(epnew);
  3972. clu.flags = 0x01;
  3973. epnew = get_entry_in_dir(sb, &clu, 1, &sector_new);
  3974. if (!epnew)
  3975. return FFS_MEDIAERR;
  3976. if (p_newdir->dir == p_fs->root_dir)
  3977. p_fs->fs_func->set_entry_clu0(epnew, CLUSTER_32(0));
  3978. else
  3979. p_fs->fs_func->set_entry_clu0(epnew, p_newdir->dir);
  3980. buf_modify(sb, sector_new);
  3981. }
  3982. ret = p_fs->fs_func->init_ext_entry(sb, p_newdir, newentry, num_new_entries, p_uniname, &dos_name);
  3983. if (ret != FFS_SUCCESS)
  3984. return ret;
  3985. p_fs->fs_func->delete_dir_entry(sb, p_olddir, oldentry, 0, num_old_entries);
  3986. fid->dir.dir = p_newdir->dir;
  3987. fid->dir.size = p_newdir->size;
  3988. fid->dir.flags = p_newdir->flags;
  3989. fid->entry = newentry;
  3990. return FFS_SUCCESS;
  3991. }
  3992. INT32 sector_read(struct super_block *sb, UINT32 sec, struct buffer_head **bh, INT32 read)
  3993. {
  3994. INT32 ret = FFS_MEDIAERR;
  3995. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  3996. if ((sec >= (p_fs->PBR_sector+p_fs->num_sectors)) && (p_fs->num_sectors > 0)) {
  3997. PRINT("[EXFAT] sector_read: out of range error! (sec = %d)\n", sec);
  3998. fs_error(sb);
  3999. return ret;
  4000. }
  4001. if (!p_fs->dev_ejected) {
  4002. ret = bdev_read(sb, sec, bh, 1, read);
  4003. if (ret != FFS_SUCCESS) {
  4004. fs_error(sb);
  4005. p_fs->dev_ejected = TRUE;
  4006. }
  4007. }
  4008. return ret;
  4009. }
  4010. INT32 sector_write(struct super_block *sb, UINT32 sec, struct buffer_head *bh, INT32 sync)
  4011. {
  4012. INT32 ret = FFS_MEDIAERR;
  4013. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  4014. if (sec >= (p_fs->PBR_sector+p_fs->num_sectors) && (p_fs->num_sectors > 0)) {
  4015. PRINT("[EXFAT] sector_write: out of range error! (sec = %d)\n", sec);
  4016. fs_error(sb);
  4017. return ret;
  4018. }
  4019. if (bh == NULL) {
  4020. PRINT("[EXFAT] sector_write: bh is NULL!\n");
  4021. fs_error(sb);
  4022. return ret;
  4023. }
  4024. if (!p_fs->dev_ejected) {
  4025. ret = bdev_write(sb, sec, bh, 1, sync);
  4026. if (ret != FFS_SUCCESS) {
  4027. fs_error(sb);
  4028. p_fs->dev_ejected = TRUE;
  4029. }
  4030. }
  4031. return ret;
  4032. }
  4033. INT32 multi_sector_read(struct super_block *sb, UINT32 sec, struct buffer_head **bh, INT32 num_secs, INT32 read)
  4034. {
  4035. INT32 ret = FFS_MEDIAERR;
  4036. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  4037. if (((sec+num_secs) > (p_fs->PBR_sector+p_fs->num_sectors)) && (p_fs->num_sectors > 0)) {
  4038. PRINT("[EXFAT] multi_sector_read: out of range error! (sec = %d, num_secs = %d)\n", sec, num_secs);
  4039. fs_error(sb);
  4040. return ret;
  4041. }
  4042. if (!p_fs->dev_ejected) {
  4043. ret = bdev_read(sb, sec, bh, num_secs, read);
  4044. if (ret != FFS_SUCCESS) {
  4045. fs_error(sb);
  4046. p_fs->dev_ejected = TRUE;
  4047. }
  4048. }
  4049. return ret;
  4050. }
  4051. INT32 multi_sector_write(struct super_block *sb, UINT32 sec, struct buffer_head *bh, INT32 num_secs, INT32 sync)
  4052. {
  4053. INT32 ret = FFS_MEDIAERR;
  4054. FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
  4055. if ((sec+num_secs) > (p_fs->PBR_sector+p_fs->num_sectors) && (p_fs->num_sectors > 0)) {
  4056. PRINT("[EXFAT] multi_sector_write: out of range error! (sec = %d, num_secs = %d)\n", sec, num_secs);
  4057. fs_error(sb);
  4058. return ret;
  4059. }
  4060. if (bh == NULL) {
  4061. PRINT("[EXFAT] multi_sector_write: bh is NULL!\n");
  4062. fs_error(sb);
  4063. return ret;
  4064. }
  4065. if (!p_fs->dev_ejected) {
  4066. ret = bdev_write(sb, sec, bh, num_secs, sync);
  4067. if (ret != FFS_SUCCESS) {
  4068. fs_error(sb);
  4069. p_fs->dev_ejected = TRUE;
  4070. }
  4071. }
  4072. return ret;
  4073. }