sdfat.c 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304
  1. /*
  2. * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /************************************************************************/
  18. /* */
  19. /* PROJECT : exFAT & FAT12/16/32 File System */
  20. /* FILE : core.c */
  21. /* PURPOSE : sdFAT glue layer for supporting VFS */
  22. /* */
  23. /*----------------------------------------------------------------------*/
  24. /* NOTES */
  25. /* */
  26. /* */
  27. /************************************************************************/
  28. #include <linux/version.h>
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/time.h>
  32. #include <linux/slab.h>
  33. #include <linux/seq_file.h>
  34. #include <linux/pagemap.h>
  35. #include <linux/mpage.h>
  36. #include <linux/buffer_head.h>
  37. #include <linux/exportfs.h>
  38. #include <linux/mount.h>
  39. #include <linux/vfs.h>
  40. #include <linux/parser.h>
  41. #include <linux/uio.h>
  42. #include <linux/writeback.h>
  43. #include <linux/log2.h>
  44. #include <linux/hash.h>
  45. #include <linux/backing-dev.h>
  46. #include <linux/sched.h>
  47. #include <linux/fs_struct.h>
  48. #include <linux/namei.h>
  49. #include <linux/bio.h>
  50. #include <linux/blkdev.h>
  51. #include <linux/swap.h> /* for mark_page_accessed() */
  52. #include <linux/vmalloc.h>
  53. #include <asm/current.h>
  54. #include <asm/unaligned.h>
  55. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0)
  56. #include <linux/iversion.h>
  57. #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)
  58. #include <linux/aio.h>
  59. #endif
  60. #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 0, 0)
  61. #error SDFAT only supports linux kernel version 3.0 or higher
  62. #endif
  63. #include "sdfat.h"
  64. #include "version.h"
  65. /* skip iterating emit_dots when dir is empty */
  66. #define ITER_POS_FILLED_DOTS (2)
  67. /* type index declare at sdfat.h */
  68. const char *FS_TYPE_STR[] = {
  69. "auto",
  70. "exfat",
  71. "vfat"
  72. };
  73. static struct kset *sdfat_kset;
  74. static struct kmem_cache *sdfat_inode_cachep;
  75. static int sdfat_default_codepage = CONFIG_SDFAT_DEFAULT_CODEPAGE;
  76. static char sdfat_default_iocharset[] = CONFIG_SDFAT_DEFAULT_IOCHARSET;
  77. static const char sdfat_iocharset_with_utf8[] = "iso8859-1";
  78. #ifdef CONFIG_SDFAT_TRACE_SB_LOCK
  79. static unsigned long __lock_jiffies;
  80. #endif
  81. static void sdfat_truncate(struct inode *inode, loff_t old_size);
  82. static int sdfat_get_block(struct inode *inode, sector_t iblock,
  83. struct buffer_head *bh_result, int create);
  84. static struct inode *sdfat_iget(struct super_block *sb, loff_t i_pos);
  85. static struct inode *sdfat_build_inode(struct super_block *sb, const FILE_ID_T *fid, loff_t i_pos);
  86. static void sdfat_detach(struct inode *inode);
  87. static void sdfat_attach(struct inode *inode, loff_t i_pos);
  88. static inline unsigned long sdfat_hash(loff_t i_pos);
  89. static int __sdfat_write_inode(struct inode *inode, int sync);
  90. static int sdfat_sync_inode(struct inode *inode);
  91. static int sdfat_write_inode(struct inode *inode, struct writeback_control *wbc);
  92. static void sdfat_write_super(struct super_block *sb);
  93. static void sdfat_write_failed(struct address_space *mapping, loff_t to);
  94. static void sdfat_init_namebuf(DENTRY_NAMEBUF_T *nb);
  95. static int sdfat_alloc_namebuf(DENTRY_NAMEBUF_T *nb);
  96. static void sdfat_free_namebuf(DENTRY_NAMEBUF_T *nb);
  97. /*************************************************************************
  98. * INNER FUNCTIONS FOR FUNCTIONS WHICH HAS KERNEL VERSION DEPENDENCY
  99. *************************************************************************/
  100. static int __sdfat_getattr(struct inode *inode, struct kstat *stat);
  101. static void __sdfat_writepage_end_io(struct bio *bio, int err);
  102. static inline void __lock_super(struct super_block *sb);
  103. static inline void __unlock_super(struct super_block *sb);
  104. static int __sdfat_create(struct inode *dir, struct dentry *dentry);
  105. static int __sdfat_revalidate(struct dentry *dentry);
  106. static int __sdfat_revalidate_ci(struct dentry *dentry, unsigned int flags);
  107. static int __sdfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync);
  108. static struct dentry *__sdfat_lookup(struct inode *dir, struct dentry *dentry);
  109. static int __sdfat_mkdir(struct inode *dir, struct dentry *dentry);
  110. static int __sdfat_rename(struct inode *old_dir, struct dentry *old_dentry,
  111. struct inode *new_dir, struct dentry *new_dentry);
  112. static int __sdfat_show_options(struct seq_file *m, struct super_block *sb);
  113. static inline ssize_t __sdfat_blkdev_direct_IO(int rw, struct kiocb *iocb,
  114. struct inode *inode, void *iov_u, loff_t offset,
  115. unsigned long nr_segs);
  116. static inline ssize_t __sdfat_direct_IO(int rw, struct kiocb *iocb,
  117. struct inode *inode, void *iov_u, loff_t offset,
  118. loff_t count, unsigned long nr_segs);
  119. static int __sdfat_d_hash(const struct dentry *dentry, struct qstr *qstr);
  120. static int __sdfat_d_hashi(const struct dentry *dentry, struct qstr *qstr);
  121. static int __sdfat_cmp(const struct dentry *dentry, unsigned int len,
  122. const char *str, const struct qstr *name);
  123. static int __sdfat_cmpi(const struct dentry *dentry, unsigned int len,
  124. const char *str, const struct qstr *name);
  125. /*************************************************************************
  126. * FUNCTIONS WHICH HAS KERNEL VERSION DEPENDENCY
  127. *************************************************************************/
  128. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)
  129. static inline void inode_set_iversion(struct inode *inode, u64 val)
  130. {
  131. inode->i_version = val;
  132. }
  133. static inline u64 inode_peek_iversion(struct inode *inode)
  134. {
  135. return inode->i_version;
  136. }
  137. #endif
  138. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
  139. /* EMPTY */
  140. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 14, 0) */
  141. static inline void bio_set_dev(struct bio *bio, struct block_device *bdev)
  142. {
  143. bio->bi_bdev = bdev;
  144. }
  145. #endif
  146. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
  147. static int sdfat_getattr(const struct path *path, struct kstat *stat,
  148. u32 request_mask, unsigned int query_flags)
  149. {
  150. struct inode *inode = d_backing_inode(path->dentry);
  151. return __sdfat_getattr(inode, stat);
  152. }
  153. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0) */
  154. static int sdfat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  155. {
  156. struct inode *inode = dentry->d_inode;
  157. return __sdfat_getattr(inode, stat);
  158. }
  159. #endif
  160. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0)
  161. static inline void __sdfat_clean_bdev_aliases(struct block_device *bdev, sector_t block)
  162. {
  163. clean_bdev_aliases(bdev, block, 1);
  164. }
  165. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4,10,0) */
  166. static inline void __sdfat_clean_bdev_aliases(struct block_device *bdev, sector_t block)
  167. {
  168. unmap_underlying_metadata(bdev, block);
  169. }
  170. static inline int wbc_to_write_flags(struct writeback_control *wbc)
  171. {
  172. if (wbc->sync_mode == WB_SYNC_ALL)
  173. return WRITE_SYNC;
  174. return 0;
  175. }
  176. #endif
  177. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
  178. static int sdfat_rename(struct inode *old_dir, struct dentry *old_dentry,
  179. struct inode *new_dir, struct dentry *new_dentry,
  180. unsigned int flags)
  181. {
  182. /*
  183. * The VFS already checks for existence, so for local filesystems
  184. * the RENAME_NOREPLACE implementation is equivalent to plain rename.
  185. * Don't support any other flags
  186. */
  187. if (flags & ~RENAME_NOREPLACE)
  188. return -EINVAL;
  189. return __sdfat_rename(old_dir, old_dentry, new_dir, new_dentry);
  190. }
  191. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0) */
  192. static int sdfat_rename(struct inode *old_dir, struct dentry *old_dentry,
  193. struct inode *new_dir, struct dentry *new_dentry)
  194. {
  195. return __sdfat_rename(old_dir, old_dentry, new_dir, new_dentry);
  196. }
  197. static int setattr_prepare(struct dentry *dentry, struct iattr *attr)
  198. {
  199. struct inode *inode = dentry->d_inode;
  200. return inode_change_ok(inode, attr);
  201. }
  202. #endif
  203. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
  204. static inline void __sdfat_submit_bio_write(struct bio *bio,
  205. struct writeback_control *wbc)
  206. {
  207. int write_flags = wbc_to_write_flags(wbc);
  208. bio_set_op_attrs(bio, REQ_OP_WRITE, write_flags);
  209. submit_bio(bio);
  210. }
  211. static inline unsigned int __sdfat_full_name_hash(const struct dentry *dentry, const char *name, unsigned int len)
  212. {
  213. return full_name_hash(dentry, name, len);
  214. }
  215. static inline unsigned long __sdfat_init_name_hash(const struct dentry *dentry)
  216. {
  217. return init_name_hash(dentry);
  218. }
  219. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 8, 0) */
  220. static inline void __sdfat_submit_bio_write(struct bio *bio,
  221. struct writeback_control *wbc)
  222. {
  223. int write_flags = wbc_to_write_flags(wbc);
  224. submit_bio(WRITE | write_flags, bio);
  225. }
  226. static inline unsigned int __sdfat_full_name_hash(const struct dentry *unused, const char *name, unsigned int len)
  227. {
  228. return full_name_hash(name, len);
  229. }
  230. static inline unsigned long __sdfat_init_name_hash(const struct dentry *unused)
  231. {
  232. return init_name_hash();
  233. }
  234. #endif
  235. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 21)
  236. /* EMPTY */
  237. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 21) */
  238. static inline void inode_lock(struct inode *inode)
  239. {
  240. mutex_lock(&inode->i_mutex);
  241. }
  242. static inline void inode_unlock(struct inode *inode)
  243. {
  244. mutex_unlock(&inode->i_mutex);
  245. }
  246. #endif
  247. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
  248. static inline int sdfat_remount_syncfs(struct super_block *sb)
  249. {
  250. sync_filesystem(sb);
  251. return 0;
  252. }
  253. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 16, 0) */
  254. static inline int sdfat_remount_syncfs(struct super_block *sb)
  255. {
  256. /*
  257. * We don`t need to call sync_filesystem(sb),
  258. * Because VFS calls it.
  259. */
  260. return 0;
  261. }
  262. #endif
  263. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 15, 0)
  264. /* EMPTY */
  265. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0) */
  266. static inline void truncate_inode_pages_final(struct address_space *mapping)
  267. {
  268. truncate_inode_pages(mapping, 0);
  269. }
  270. #endif
  271. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)
  272. static inline sector_t __sdfat_bio_sector(struct bio *bio)
  273. {
  274. return bio->bi_iter.bi_sector;
  275. }
  276. static inline void __sdfat_set_bio_iterate(struct bio *bio, sector_t sector,
  277. unsigned int size, unsigned int idx, unsigned int done)
  278. {
  279. struct bvec_iter *iter = &(bio->bi_iter);
  280. iter->bi_sector = sector;
  281. iter->bi_size = size;
  282. iter->bi_idx = idx;
  283. iter->bi_bvec_done = done;
  284. }
  285. static void __sdfat_truncate_pagecache(struct inode *inode,
  286. loff_t to, loff_t newsize)
  287. {
  288. truncate_pagecache(inode, newsize);
  289. }
  290. static int sdfat_d_hash(const struct dentry *dentry, struct qstr *qstr)
  291. {
  292. return __sdfat_d_hash(dentry, qstr);
  293. }
  294. static int sdfat_d_hashi(const struct dentry *dentry, struct qstr *qstr)
  295. {
  296. return __sdfat_d_hashi(dentry, qstr);
  297. }
  298. //instead of sdfat_readdir
  299. static int sdfat_iterate(struct file *filp, struct dir_context *ctx)
  300. {
  301. struct inode *inode = filp->f_path.dentry->d_inode;
  302. struct super_block *sb = inode->i_sb;
  303. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  304. FS_INFO_T *fsi = &(sbi->fsi);
  305. DIR_ENTRY_T de;
  306. DENTRY_NAMEBUF_T *nb = &(de.NameBuf);
  307. unsigned long inum;
  308. loff_t cpos;
  309. int err = 0, fake_offset = 0;
  310. sdfat_init_namebuf(nb);
  311. __lock_super(sb);
  312. cpos = ctx->pos;
  313. if ((fsi->vol_type == EXFAT) || (inode->i_ino == SDFAT_ROOT_INO)) {
  314. if (!dir_emit_dots(filp, ctx))
  315. goto out;
  316. if (ctx->pos == ITER_POS_FILLED_DOTS) {
  317. cpos = 0;
  318. fake_offset = 1;
  319. }
  320. }
  321. if (cpos & (DENTRY_SIZE - 1)) {
  322. err = -ENOENT;
  323. goto out;
  324. }
  325. /* name buffer should be allocated before use */
  326. err = sdfat_alloc_namebuf(nb);
  327. if (err)
  328. goto out;
  329. get_new:
  330. SDFAT_I(inode)->fid.size = i_size_read(inode);
  331. SDFAT_I(inode)->fid.rwoffset = cpos >> DENTRY_SIZE_BITS;
  332. if (cpos >= SDFAT_I(inode)->fid.size)
  333. goto end_of_dir;
  334. err = fsapi_readdir(inode, &de);
  335. if (err) {
  336. // at least we tried to read a sector
  337. // move cpos to next sector position (should be aligned)
  338. if (err == -EIO) {
  339. cpos += 1 << (sb->s_blocksize_bits);
  340. cpos &= ~((u32)sb->s_blocksize-1);
  341. }
  342. err = -EIO;
  343. goto end_of_dir;
  344. }
  345. cpos = SDFAT_I(inode)->fid.rwoffset << DENTRY_SIZE_BITS;
  346. if (!nb->lfn[0])
  347. goto end_of_dir;
  348. if (!memcmp(nb->sfn, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH)) {
  349. inum = inode->i_ino;
  350. } else if (!memcmp(nb->sfn, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH)) {
  351. inum = parent_ino(filp->f_path.dentry);
  352. } else {
  353. loff_t i_pos = ((loff_t) SDFAT_I(inode)->fid.start_clu << 32) |
  354. ((SDFAT_I(inode)->fid.rwoffset-1) & 0xffffffff);
  355. struct inode *tmp = sdfat_iget(sb, i_pos);
  356. if (tmp) {
  357. inum = tmp->i_ino;
  358. iput(tmp);
  359. } else {
  360. inum = iunique(sb, SDFAT_ROOT_INO);
  361. }
  362. }
  363. /* Before calling dir_emit(), sb_lock should be released.
  364. * Because page fault can occur in dir_emit() when the size of buffer given
  365. * from user is larger than one page size
  366. */
  367. __unlock_super(sb);
  368. if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum,
  369. (de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
  370. goto out_unlocked;
  371. __lock_super(sb);
  372. ctx->pos = cpos;
  373. goto get_new;
  374. end_of_dir:
  375. if (!cpos && fake_offset)
  376. cpos = ITER_POS_FILLED_DOTS;
  377. ctx->pos = cpos;
  378. out:
  379. __unlock_super(sb);
  380. out_unlocked:
  381. /*
  382. * To improve performance, free namebuf after unlock sb_lock.
  383. * If namebuf is not allocated, this function do nothing
  384. */
  385. sdfat_free_namebuf(nb);
  386. return err;
  387. }
  388. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0) */
  389. static inline sector_t __sdfat_bio_sector(struct bio *bio)
  390. {
  391. return bio->bi_sector;
  392. }
  393. static inline void __sdfat_set_bio_iterate(struct bio *bio, sector_t sector,
  394. unsigned int size, unsigned int idx, unsigned int done)
  395. {
  396. bio->bi_sector = sector;
  397. bio->bi_idx = idx;
  398. bio->bi_size = size; //PAGE_SIZE;
  399. }
  400. static void __sdfat_truncate_pagecache(struct inode *inode,
  401. loff_t to, loff_t newsize)
  402. {
  403. truncate_pagecache(inode, to, newsize);
  404. }
  405. static int sdfat_d_hash(const struct dentry *dentry,
  406. const struct inode *inode, struct qstr *qstr)
  407. {
  408. return __sdfat_d_hash(dentry, qstr);
  409. }
  410. static int sdfat_d_hashi(const struct dentry *dentry,
  411. const struct inode *inode, struct qstr *qstr)
  412. {
  413. return __sdfat_d_hashi(dentry, qstr);
  414. }
  415. static int sdfat_readdir(struct file *filp, void *dirent, filldir_t filldir)
  416. {
  417. struct inode *inode = filp->f_path.dentry->d_inode;
  418. struct super_block *sb = inode->i_sb;
  419. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  420. FS_INFO_T *fsi = &(sbi->fsi);
  421. DIR_ENTRY_T de;
  422. DENTRY_NAMEBUF_T *nb = &(de.NameBuf);
  423. unsigned long inum;
  424. loff_t cpos;
  425. int err = 0, fake_offset = 0;
  426. sdfat_init_namebuf(nb);
  427. __lock_super(sb);
  428. cpos = filp->f_pos;
  429. /* Fake . and .. for the root directory. */
  430. if ((fsi->vol_type == EXFAT) || (inode->i_ino == SDFAT_ROOT_INO)) {
  431. while (cpos < ITER_POS_FILLED_DOTS) {
  432. if (inode->i_ino == SDFAT_ROOT_INO)
  433. inum = SDFAT_ROOT_INO;
  434. else if (cpos == 0)
  435. inum = inode->i_ino;
  436. else /* (cpos == 1) */
  437. inum = parent_ino(filp->f_path.dentry);
  438. if (filldir(dirent, "..", cpos+1, cpos, inum, DT_DIR) < 0)
  439. goto out;
  440. cpos++;
  441. filp->f_pos++;
  442. }
  443. if (cpos == ITER_POS_FILLED_DOTS) {
  444. cpos = 0;
  445. fake_offset = 1;
  446. }
  447. }
  448. if (cpos & (DENTRY_SIZE - 1)) {
  449. err = -ENOENT;
  450. goto out;
  451. }
  452. /* name buffer should be allocated before use */
  453. err = sdfat_alloc_namebuf(nb);
  454. if (err)
  455. goto out;
  456. get_new:
  457. SDFAT_I(inode)->fid.size = i_size_read(inode);
  458. SDFAT_I(inode)->fid.rwoffset = cpos >> DENTRY_SIZE_BITS;
  459. if (cpos >= SDFAT_I(inode)->fid.size)
  460. goto end_of_dir;
  461. err = fsapi_readdir(inode, &de);
  462. if (err) {
  463. // at least we tried to read a sector
  464. // move cpos to next sector position (should be aligned)
  465. if (err == -EIO) {
  466. cpos += 1 << (sb->s_blocksize_bits);
  467. cpos &= ~((u32)sb->s_blocksize-1);
  468. }
  469. err = -EIO;
  470. goto end_of_dir;
  471. }
  472. cpos = SDFAT_I(inode)->fid.rwoffset << DENTRY_SIZE_BITS;
  473. if (!nb->lfn[0])
  474. goto end_of_dir;
  475. if (!memcmp(nb->sfn, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH)) {
  476. inum = inode->i_ino;
  477. } else if (!memcmp(nb->sfn, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH)) {
  478. inum = parent_ino(filp->f_path.dentry);
  479. } else {
  480. loff_t i_pos = ((loff_t) SDFAT_I(inode)->fid.start_clu << 32) |
  481. ((SDFAT_I(inode)->fid.rwoffset-1) & 0xffffffff);
  482. struct inode *tmp = sdfat_iget(sb, i_pos);
  483. if (tmp) {
  484. inum = tmp->i_ino;
  485. iput(tmp);
  486. } else {
  487. inum = iunique(sb, SDFAT_ROOT_INO);
  488. }
  489. }
  490. /* Before calling dir_emit(), sb_lock should be released.
  491. * Because page fault can occur in dir_emit() when the size of buffer given
  492. * from user is larger than one page size
  493. */
  494. __unlock_super(sb);
  495. if (filldir(dirent, nb->lfn, strlen(nb->lfn), cpos, inum,
  496. (de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG) < 0)
  497. goto out_unlocked;
  498. __lock_super(sb);
  499. filp->f_pos = cpos;
  500. goto get_new;
  501. end_of_dir:
  502. if (!cpos && fake_offset)
  503. cpos = ITER_POS_FILLED_DOTS;
  504. filp->f_pos = cpos;
  505. out:
  506. __unlock_super(sb);
  507. out_unlocked:
  508. /*
  509. * To improve performance, free namebuf after unlock sb_lock.
  510. * If namebuf is not allocated, this function do nothing
  511. */
  512. sdfat_free_namebuf(nb);
  513. return err;
  514. }
  515. #endif
  516. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
  517. /* EMPTY */
  518. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0) */
  519. #endif
  520. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0)
  521. static inline int __is_sb_dirty(struct super_block *sb)
  522. {
  523. return SDFAT_SB(sb)->s_dirt;
  524. }
  525. static inline void __set_sb_clean(struct super_block *sb)
  526. {
  527. SDFAT_SB(sb)->s_dirt = 0;
  528. }
  529. /* Workqueue wrapper for sdfat_write_super () */
  530. static void __write_super_delayed(struct work_struct *work)
  531. {
  532. struct sdfat_sb_info *sbi;
  533. struct super_block *sb;
  534. sbi = container_of(work, struct sdfat_sb_info, write_super_work.work);
  535. sb = sbi->host_sb;
  536. /* XXX: Is this needed? */
  537. if (!sb || !down_read_trylock(&sb->s_umount)) {
  538. DMSG("%s: skip delayed work(write_super).\n", __func__);
  539. return;
  540. }
  541. DMSG("%s: do delayed_work(write_super).\n", __func__);
  542. spin_lock(&sbi->work_lock);
  543. sbi->write_super_queued = 0;
  544. spin_unlock(&sbi->work_lock);
  545. sdfat_write_super(sb);
  546. up_read(&sb->s_umount);
  547. }
  548. static void setup_sdfat_sync_super_wq(struct super_block *sb)
  549. {
  550. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  551. mutex_init(&sbi->s_lock);
  552. spin_lock_init(&sbi->work_lock);
  553. INIT_DELAYED_WORK(&sbi->write_super_work, __write_super_delayed);
  554. sbi->host_sb = sb;
  555. }
  556. static inline bool __cancel_delayed_work_sync(struct sdfat_sb_info *sbi)
  557. {
  558. return cancel_delayed_work_sync(&sbi->write_super_work);
  559. }
  560. static inline void lock_super(struct super_block *sb)
  561. {
  562. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  563. mutex_lock(&sbi->s_lock);
  564. }
  565. static inline void unlock_super(struct super_block *sb)
  566. {
  567. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  568. mutex_unlock(&sbi->s_lock);
  569. }
  570. static int sdfat_revalidate(struct dentry *dentry, unsigned int flags)
  571. {
  572. if (flags & LOOKUP_RCU)
  573. return -ECHILD;
  574. return __sdfat_revalidate(dentry);
  575. }
  576. static int sdfat_revalidate_ci(struct dentry *dentry, unsigned int flags)
  577. {
  578. if (flags & LOOKUP_RCU)
  579. return -ECHILD;
  580. return __sdfat_revalidate_ci(dentry, flags);
  581. }
  582. static struct inode *sdfat_iget(struct super_block *sb, loff_t i_pos)
  583. {
  584. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  585. struct sdfat_inode_info *info;
  586. struct hlist_head *head = sbi->inode_hashtable + sdfat_hash(i_pos);
  587. struct inode *inode = NULL;
  588. spin_lock(&sbi->inode_hash_lock);
  589. hlist_for_each_entry(info, head, i_hash_fat) {
  590. BUG_ON(info->vfs_inode.i_sb != sb);
  591. if (i_pos != info->i_pos)
  592. continue;
  593. inode = igrab(&info->vfs_inode);
  594. if (inode)
  595. break;
  596. }
  597. spin_unlock(&sbi->inode_hash_lock);
  598. return inode;
  599. }
  600. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0) */
  601. static inline int __is_sb_dirty(struct super_block *sb)
  602. {
  603. return sb->s_dirt;
  604. }
  605. static inline void __set_sb_clean(struct super_block *sb)
  606. {
  607. sb->s_dirt = 0;
  608. }
  609. static void setup_sdfat_sync_super_wq(struct super_block *sb)
  610. {
  611. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  612. sbi->host_sb = sb;
  613. }
  614. static inline bool __cancel_delayed_work_sync(struct sdfat_sb_info *sbi)
  615. {
  616. /* DO NOTHING */
  617. return 0;
  618. }
  619. static inline void clear_inode(struct inode *inode)
  620. {
  621. end_writeback(inode);
  622. }
  623. static int sdfat_revalidate(struct dentry *dentry, struct nameidata *nd)
  624. {
  625. if (nd && nd->flags & LOOKUP_RCU)
  626. return -ECHILD;
  627. return __sdfat_revalidate(dentry);
  628. }
  629. static int sdfat_revalidate_ci(struct dentry *dentry, struct nameidata *nd)
  630. {
  631. if (nd && nd->flags & LOOKUP_RCU)
  632. return -ECHILD;
  633. return __sdfat_revalidate_ci(dentry, nd ? nd->flags : 0);
  634. }
  635. static struct inode *sdfat_iget(struct super_block *sb, loff_t i_pos)
  636. {
  637. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  638. struct sdfat_inode_info *info;
  639. struct hlist_node *node;
  640. struct hlist_head *head = sbi->inode_hashtable + sdfat_hash(i_pos);
  641. struct inode *inode = NULL;
  642. spin_lock(&sbi->inode_hash_lock);
  643. hlist_for_each_entry(info, node, head, i_hash_fat) {
  644. BUG_ON(info->vfs_inode.i_sb != sb);
  645. if (i_pos != info->i_pos)
  646. continue;
  647. inode = igrab(&info->vfs_inode);
  648. if (inode)
  649. break;
  650. }
  651. spin_unlock(&sbi->inode_hash_lock);
  652. return inode;
  653. }
  654. #endif
  655. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
  656. static struct dentry *sdfat_lookup(struct inode *dir, struct dentry *dentry,
  657. unsigned int flags)
  658. {
  659. return __sdfat_lookup(dir, dentry);
  660. }
  661. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0) */
  662. static struct dentry *sdfat_lookup(struct inode *dir, struct dentry *dentry,
  663. struct nameidata *nd)
  664. {
  665. return __sdfat_lookup(dir, dentry);
  666. }
  667. #endif
  668. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
  669. static struct dentry *__d_make_root(struct inode *root_inode)
  670. {
  671. return d_make_root(root_inode);
  672. }
  673. static void __sdfat_do_truncate(struct inode *inode, loff_t old, loff_t new)
  674. {
  675. down_write(&SDFAT_I(inode)->truncate_lock);
  676. truncate_setsize(inode, new);
  677. sdfat_truncate(inode, old);
  678. up_write(&SDFAT_I(inode)->truncate_lock);
  679. }
  680. static sector_t sdfat_aop_bmap(struct address_space *mapping, sector_t block)
  681. {
  682. sector_t blocknr;
  683. /* sdfat_get_cluster() assumes the requested blocknr isn't truncated. */
  684. down_read(&SDFAT_I(mapping->host)->truncate_lock);
  685. blocknr = generic_block_bmap(mapping, block, sdfat_get_block);
  686. up_read(&SDFAT_I(mapping->host)->truncate_lock);
  687. return blocknr;
  688. }
  689. static int sdfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  690. {
  691. return __sdfat_mkdir(dir, dentry);
  692. }
  693. static int sdfat_show_options(struct seq_file *m, struct dentry *root)
  694. {
  695. return __sdfat_show_options(m, root->d_sb);
  696. }
  697. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0) */
  698. static inline void set_nlink(struct inode *inode, unsigned int nlink)
  699. {
  700. inode->i_nlink = nlink;
  701. }
  702. static struct dentry *__d_make_root(struct inode *root_inode)
  703. {
  704. return d_alloc_root(root_inode);
  705. }
  706. static void __sdfat_do_truncate(struct inode *inode, loff_t old, loff_t new)
  707. {
  708. truncate_setsize(inode, new);
  709. sdfat_truncate(inode, old);
  710. }
  711. static sector_t sdfat_aop_bmap(struct address_space *mapping, sector_t block)
  712. {
  713. sector_t blocknr;
  714. /* sdfat_get_cluster() assumes the requested blocknr isn't truncated. */
  715. down_read(&mapping->host->i_alloc_sem);
  716. blocknr = generic_block_bmap(mapping, block, sdfat_get_block);
  717. up_read(&mapping->host->i_alloc_sem);
  718. return blocknr;
  719. }
  720. static int sdfat_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  721. {
  722. return __sdfat_mkdir(dir, dentry);
  723. }
  724. static int sdfat_show_options(struct seq_file *m, struct vfsmount *mnt)
  725. {
  726. return __sdfat_show_options(m, mnt->mnt_sb);
  727. }
  728. #endif
  729. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 1, 0)
  730. #define __sdfat_generic_file_fsync(filp, start, end, datasync) \
  731. generic_file_fsync(filp, start, end, datasync)
  732. static int sdfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
  733. {
  734. return __sdfat_file_fsync(filp, start, end, datasync);
  735. }
  736. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0) */
  737. #define __sdfat_generic_file_fsync(filp, start, end, datasync) \
  738. generic_file_fsync(filp, datasync)
  739. static int sdfat_file_fsync(struct file *filp, int datasync)
  740. {
  741. return __sdfat_file_fsync(filp, 0, 0, datasync);
  742. }
  743. #endif
  744. /*************************************************************************
  745. * MORE FUNCTIONS WHICH HAS KERNEL VERSION DEPENDENCY
  746. *************************************************************************/
  747. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 13, 0)
  748. static void sdfat_writepage_end_io(struct bio *bio)
  749. {
  750. __sdfat_writepage_end_io(bio, blk_status_to_errno(bio->bi_status));
  751. }
  752. #elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
  753. static void sdfat_writepage_end_io(struct bio *bio)
  754. {
  755. __sdfat_writepage_end_io(bio, bio->bi_error);
  756. }
  757. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 3, 0) */
  758. static void sdfat_writepage_end_io(struct bio *bio, int err)
  759. {
  760. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  761. err = 0;
  762. __sdfat_writepage_end_io(bio, err);
  763. }
  764. #endif
  765. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
  766. static int sdfat_cmp(const struct dentry *dentry,
  767. unsigned int len, const char *str, const struct qstr *name)
  768. {
  769. return __sdfat_cmp(dentry, len, str, name);
  770. }
  771. static int sdfat_cmpi(const struct dentry *dentry,
  772. unsigned int len, const char *str, const struct qstr *name)
  773. {
  774. return __sdfat_cmpi(dentry, len, str, name);
  775. }
  776. #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)
  777. static int sdfat_cmp(const struct dentry *parent, const struct dentry *dentry,
  778. unsigned int len, const char *str, const struct qstr *name)
  779. {
  780. return __sdfat_cmp(dentry, len, str, name);
  781. }
  782. static int sdfat_cmpi(const struct dentry *parent, const struct dentry *dentry,
  783. unsigned int len, const char *str, const struct qstr *name)
  784. {
  785. return __sdfat_cmpi(dentry, len, str, name);
  786. }
  787. #else
  788. static int sdfat_cmp(const struct dentry *parent, const struct inode *pinode,
  789. const struct dentry *dentry, const struct inode *inode,
  790. unsigned int len, const char *str, const struct qstr *name)
  791. {
  792. return __sdfat_cmp(dentry, len, str, name);
  793. }
  794. static int sdfat_cmpi(const struct dentry *parent, const struct inode *pinode,
  795. const struct dentry *dentry, const struct inode *inode,
  796. unsigned int len, const char *str, const struct qstr *name)
  797. {
  798. return __sdfat_cmpi(dentry, len, str, name);
  799. }
  800. #endif
  801. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
  802. static ssize_t sdfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  803. {
  804. struct file *file = iocb->ki_filp;
  805. struct address_space *mapping = file->f_mapping;
  806. struct inode *inode = mapping->host;
  807. size_t count = iov_iter_count(iter);
  808. int rw = iov_iter_rw(iter);
  809. loff_t offset = iocb->ki_pos;
  810. return __sdfat_direct_IO(rw, iocb, inode,
  811. (void *)iter, offset, count, 0 /* UNUSED */);
  812. }
  813. #elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)
  814. static ssize_t sdfat_direct_IO(struct kiocb *iocb,
  815. struct iov_iter *iter,
  816. loff_t offset)
  817. {
  818. struct file *file = iocb->ki_filp;
  819. struct address_space *mapping = file->f_mapping;
  820. struct inode *inode = mapping->host;
  821. size_t count = iov_iter_count(iter);
  822. int rw = iov_iter_rw(iter);
  823. return __sdfat_direct_IO(rw, iocb, inode,
  824. (void *)iter, offset, count, 0 /* UNUSED */);
  825. }
  826. #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
  827. static ssize_t sdfat_direct_IO(int rw, struct kiocb *iocb,
  828. struct iov_iter *iter,
  829. loff_t offset)
  830. {
  831. struct file *file = iocb->ki_filp;
  832. struct address_space *mapping = file->f_mapping;
  833. struct inode *inode = mapping->host;
  834. size_t count = iov_iter_count(iter);
  835. return __sdfat_direct_IO(rw, iocb, inode,
  836. (void *)iter, offset, count, 0 /* UNUSED */);
  837. }
  838. #else
  839. static ssize_t sdfat_direct_IO(int rw, struct kiocb *iocb,
  840. const struct iovec *iov, loff_t offset, unsigned long nr_segs)
  841. {
  842. struct file *file = iocb->ki_filp;
  843. struct address_space *mapping = file->f_mapping;
  844. struct inode *inode = mapping->host;
  845. size_t count = iov_length(iov, nr_segs);
  846. return __sdfat_direct_IO(rw, iocb, inode,
  847. (void *)iov, offset, count, nr_segs);
  848. }
  849. #endif
  850. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
  851. static inline ssize_t __sdfat_blkdev_direct_IO(int unused, struct kiocb *iocb,
  852. struct inode *inode, void *iov_u, loff_t unused_1,
  853. unsigned long nr_segs)
  854. {
  855. struct iov_iter *iter = (struct iov_iter *)iov_u;
  856. return blockdev_direct_IO(iocb, inode, iter, sdfat_get_block);
  857. }
  858. #elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)
  859. static inline ssize_t __sdfat_blkdev_direct_IO(int unused, struct kiocb *iocb,
  860. struct inode *inode, void *iov_u, loff_t offset,
  861. unsigned long nr_segs)
  862. {
  863. struct iov_iter *iter = (struct iov_iter *)iov_u;
  864. return blockdev_direct_IO(iocb, inode, iter, offset, sdfat_get_block);
  865. }
  866. #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
  867. static inline ssize_t __sdfat_blkdev_direct_IO(int rw, struct kiocb *iocb,
  868. struct inode *inode, void *iov_u, loff_t offset,
  869. unsigned long nr_segs)
  870. {
  871. struct iov_iter *iter = (struct iov_iter *)iov_u;
  872. return blockdev_direct_IO(rw, iocb, inode, iter,
  873. offset, sdfat_get_block);
  874. }
  875. #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
  876. static inline ssize_t __sdfat_blkdev_direct_IO(int rw, struct kiocb *iocb,
  877. struct inode *inode, void *iov_u, loff_t offset,
  878. unsigned long nr_segs)
  879. {
  880. const struct iovec *iov = (const struct iovec *)iov_u;
  881. return blockdev_direct_IO(rw, iocb, inode, iov,
  882. offset, nr_segs, sdfat_get_block);
  883. }
  884. #else
  885. static inline ssize_t __sdfat_blkdev_direct_IO(int rw, struct kiocb *iocb,
  886. struct inode *inode, void *iov_u, loff_t offset,
  887. unsigned long nr_segs)
  888. {
  889. const struct iovec *iov = (const struct iovec *)iov_u;
  890. return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
  891. offset, nr_segs, sdfat_get_block, NULL);
  892. }
  893. #endif
  894. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
  895. static const char *sdfat_follow_link(struct dentry *dentry, struct inode *inode, struct delayed_call *done)
  896. {
  897. struct sdfat_inode_info *ei = SDFAT_I(inode);
  898. return (char *)(ei->target);
  899. }
  900. #elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
  901. static const char *sdfat_follow_link(struct dentry *dentry, void **cookie)
  902. {
  903. struct sdfat_inode_info *ei = SDFAT_I(dentry->d_inode);
  904. return *cookie = (char *)(ei->target);
  905. }
  906. #else
  907. static void *sdfat_follow_link(struct dentry *dentry, struct nameidata *nd)
  908. {
  909. struct sdfat_inode_info *ei = SDFAT_I(dentry->d_inode);
  910. nd_set_link(nd, (char *)(ei->target));
  911. return NULL;
  912. }
  913. #endif
  914. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
  915. static int sdfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  916. bool excl)
  917. {
  918. return __sdfat_create(dir, dentry);
  919. }
  920. #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
  921. static int sdfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  922. struct nameidata *nd)
  923. {
  924. return __sdfat_create(dir, dentry);
  925. }
  926. #else
  927. static int sdfat_create(struct inode *dir, struct dentry *dentry, int mode,
  928. struct nameidata *nd)
  929. {
  930. return __sdfat_create(dir, dentry);
  931. }
  932. #endif
  933. /*************************************************************************
  934. * WRAP FUNCTIONS FOR DEBUGGING
  935. *************************************************************************/
  936. #ifdef CONFIG_SDFAT_TRACE_SB_LOCK
  937. static inline void __lock_super(struct super_block *sb)
  938. {
  939. lock_super(sb);
  940. __lock_jiffies = jiffies;
  941. }
  942. static inline void __unlock_super(struct super_block *sb)
  943. {
  944. int time = ((jiffies - __lock_jiffies) * 1000 / HZ);
  945. /* FIXME : error message should be modified */
  946. if (time > 10)
  947. EMSG("lock_super in %s (%d ms)\n", __func__, time);
  948. unlock_super(sb);
  949. }
  950. #else /* CONFIG_SDFAT_TRACE_SB_LOCK */
  951. static inline void __lock_super(struct super_block *sb)
  952. {
  953. lock_super(sb);
  954. }
  955. static inline void __unlock_super(struct super_block *sb)
  956. {
  957. unlock_super(sb);
  958. }
  959. #endif /* CONFIG_SDFAT_TRACE_SB_LOCK */
  960. /*************************************************************************
  961. * NORMAL FUNCTIONS
  962. *************************************************************************/
  963. static inline loff_t sdfat_make_i_pos(FILE_ID_T *fid)
  964. {
  965. return ((loff_t) fid->dir.dir << 32) | (fid->entry & 0xffffffff);
  966. }
  967. /*======================================================================*/
  968. /* Directory Entry Name Buffer Operations */
  969. /*======================================================================*/
  970. static void sdfat_init_namebuf(DENTRY_NAMEBUF_T *nb)
  971. {
  972. nb->lfn = NULL;
  973. nb->sfn = NULL;
  974. nb->lfnbuf_len = 0;
  975. nb->sfnbuf_len = 0;
  976. }
  977. static int sdfat_alloc_namebuf(DENTRY_NAMEBUF_T *nb)
  978. {
  979. nb->lfn = __getname();
  980. if (!nb->lfn)
  981. return -ENOMEM;
  982. nb->sfn = nb->lfn + MAX_VFSNAME_BUF_SIZE;
  983. nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE;
  984. nb->sfnbuf_len = MAX_VFSNAME_BUF_SIZE;
  985. return 0;
  986. }
  987. static void sdfat_free_namebuf(DENTRY_NAMEBUF_T *nb)
  988. {
  989. if (!nb->lfn)
  990. return;
  991. __putname(nb->lfn);
  992. sdfat_init_namebuf(nb);
  993. }
  994. /*======================================================================*/
  995. /* Directory Entry Operations */
  996. /*======================================================================*/
  997. #define SDFAT_DSTATE_LOCKED (void *)(0xCAFE2016)
  998. #define SDFAT_DSTATE_UNLOCKED (void *)(0x00000000)
  999. static inline void __lock_d_revalidate(struct dentry *dentry)
  1000. {
  1001. spin_lock(&dentry->d_lock);
  1002. dentry->d_fsdata = SDFAT_DSTATE_LOCKED;
  1003. spin_unlock(&dentry->d_lock);
  1004. }
  1005. static inline void __unlock_d_revalidate(struct dentry *dentry)
  1006. {
  1007. spin_lock(&dentry->d_lock);
  1008. dentry->d_fsdata = SDFAT_DSTATE_UNLOCKED;
  1009. spin_unlock(&dentry->d_lock);
  1010. }
  1011. /* __check_dstate_locked requires dentry->d_lock */
  1012. static inline int __check_dstate_locked(struct dentry *dentry)
  1013. {
  1014. if (dentry->d_fsdata == SDFAT_DSTATE_LOCKED)
  1015. return 1;
  1016. return 0;
  1017. }
  1018. /*
  1019. * If new entry was created in the parent, it could create the 8.3
  1020. * alias (the shortname of logname). So, the parent may have the
  1021. * negative-dentry which matches the created 8.3 alias.
  1022. *
  1023. * If it happened, the negative dentry isn't actually negative
  1024. * anymore. So, drop it.
  1025. */
  1026. static int __sdfat_revalidate_common(struct dentry *dentry)
  1027. {
  1028. int ret = 1;
  1029. spin_lock(&dentry->d_lock);
  1030. if ((!dentry->d_inode) && (!__check_dstate_locked(dentry) &&
  1031. (dentry->d_time !=
  1032. (unsigned long)inode_peek_iversion(dentry->d_parent->d_inode)))) {
  1033. ret = 0;
  1034. }
  1035. spin_unlock(&dentry->d_lock);
  1036. return ret;
  1037. }
  1038. static int __sdfat_revalidate(struct dentry *dentry)
  1039. {
  1040. /* This is not negative dentry. Always valid. */
  1041. if (dentry->d_inode)
  1042. return 1;
  1043. return __sdfat_revalidate_common(dentry);
  1044. }
  1045. static int __sdfat_revalidate_ci(struct dentry *dentry, unsigned int flags)
  1046. {
  1047. /*
  1048. * This is not negative dentry. Always valid.
  1049. *
  1050. * Note, rename() to existing directory entry will have ->d_inode,
  1051. * and will use existing name which isn't specified name by user.
  1052. *
  1053. * We may be able to drop this positive dentry here. But dropping
  1054. * positive dentry isn't good idea. So it's unsupported like
  1055. * rename("filename", "FILENAME") for now.
  1056. */
  1057. if (dentry->d_inode)
  1058. return 1;
  1059. #if 0 /* Blocked below code for lookup_one_len() called by stackable FS */
  1060. /*
  1061. * This may be nfsd (or something), anyway, we can't see the
  1062. * intent of this. So, since this can be for creation, drop it.
  1063. */
  1064. if (!flags)
  1065. return 0;
  1066. #endif
  1067. /*
  1068. * Drop the negative dentry, in order to make sure to use the
  1069. * case sensitive name which is specified by user if this is
  1070. * for creation.
  1071. */
  1072. if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
  1073. return 0;
  1074. return __sdfat_revalidate_common(dentry);
  1075. }
  1076. /* returns the length of a struct qstr, ignoring trailing dots */
  1077. static unsigned int __sdfat_striptail_len(unsigned int len, const char *name)
  1078. {
  1079. while (len && name[len - 1] == '.')
  1080. len--;
  1081. return len;
  1082. }
  1083. static unsigned int sdfat_striptail_len(const struct qstr *qstr)
  1084. {
  1085. return __sdfat_striptail_len(qstr->len, qstr->name);
  1086. }
  1087. /*
  1088. * Compute the hash for the sdfat name corresponding to the dentry.
  1089. * Note: if the name is invalid, we leave the hash code unchanged so
  1090. * that the existing dentry can be used. The sdfat fs routines will
  1091. * return ENOENT or EINVAL as appropriate.
  1092. */
  1093. static int __sdfat_d_hash(const struct dentry *dentry, struct qstr *qstr)
  1094. {
  1095. unsigned int len = sdfat_striptail_len(qstr);
  1096. qstr->hash = __sdfat_full_name_hash(dentry, qstr->name, len);
  1097. return 0;
  1098. }
  1099. /*
  1100. * Compute the hash for the sdfat name corresponding to the dentry.
  1101. * Note: if the name is invalid, we leave the hash code unchanged so
  1102. * that the existing dentry can be used. The sdfat fs routines will
  1103. * return ENOENT or EINVAL as appropriate.
  1104. */
  1105. static int __sdfat_d_hashi(const struct dentry *dentry, struct qstr *qstr)
  1106. {
  1107. struct nls_table *t = SDFAT_SB(dentry->d_sb)->nls_io;
  1108. const unsigned char *name;
  1109. unsigned int len;
  1110. unsigned long hash;
  1111. name = qstr->name;
  1112. len = sdfat_striptail_len(qstr);
  1113. hash = __sdfat_init_name_hash(dentry);
  1114. while (len--)
  1115. hash = partial_name_hash(nls_tolower(t, *name++), hash);
  1116. qstr->hash = end_name_hash(hash);
  1117. return 0;
  1118. }
  1119. /*
  1120. * Case sensitive compare of two sdfat names.
  1121. */
  1122. static int __sdfat_cmp(const struct dentry *dentry, unsigned int len,
  1123. const char *str, const struct qstr *name)
  1124. {
  1125. unsigned int alen, blen;
  1126. /* A filename cannot end in '.' or we treat it like it has none */
  1127. alen = sdfat_striptail_len(name);
  1128. blen = __sdfat_striptail_len(len, str);
  1129. if (alen == blen) {
  1130. if (strncmp(name->name, str, alen) == 0)
  1131. return 0;
  1132. }
  1133. return 1;
  1134. }
  1135. /*
  1136. * Case insensitive compare of two sdfat names.
  1137. */
  1138. static int __sdfat_cmpi(const struct dentry *dentry, unsigned int len,
  1139. const char *str, const struct qstr *name)
  1140. {
  1141. struct nls_table *t = SDFAT_SB(dentry->d_sb)->nls_io;
  1142. unsigned int alen, blen;
  1143. /* A filename cannot end in '.' or we treat it like it has none */
  1144. alen = sdfat_striptail_len(name);
  1145. blen = __sdfat_striptail_len(len, str);
  1146. if (alen == blen) {
  1147. if (nls_strnicmp(t, name->name, str, alen) == 0)
  1148. return 0;
  1149. }
  1150. return 1;
  1151. }
  1152. static const struct dentry_operations sdfat_dentry_ops = {
  1153. .d_revalidate = sdfat_revalidate,
  1154. .d_hash = sdfat_d_hash,
  1155. .d_compare = sdfat_cmp,
  1156. };
  1157. static const struct dentry_operations sdfat_ci_dentry_ops = {
  1158. .d_revalidate = sdfat_revalidate_ci,
  1159. .d_hash = sdfat_d_hashi,
  1160. .d_compare = sdfat_cmpi,
  1161. };
  1162. #ifdef CONFIG_SDFAT_DFR
  1163. /*----------------------------------------------------------------------*/
  1164. /* Defragmentation related */
  1165. /*----------------------------------------------------------------------*/
  1166. /**
  1167. * @fn defrag_cleanup_reqs
  1168. * @brief clean-up defrag info depending on error flag
  1169. * @return void
  1170. * @param sb super block
  1171. * @param error error flag
  1172. */
  1173. static void defrag_cleanup_reqs(INOUT struct super_block *sb, IN int error)
  1174. {
  1175. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  1176. struct defrag_info *sb_dfr = &(sbi->dfr_info);
  1177. struct defrag_info *ino_dfr = NULL, *tmp = NULL;
  1178. /* sdfat patch 0.96 : sbi->dfr_info crash problem */
  1179. __lock_super(sb);
  1180. /* Clean-up ino_dfr */
  1181. if (!error) {
  1182. list_for_each_entry_safe(ino_dfr, tmp, &sb_dfr->entry, entry) {
  1183. struct inode *inode = &(container_of(ino_dfr, struct sdfat_inode_info, dfr_info)->vfs_inode);
  1184. mutex_lock(&ino_dfr->lock);
  1185. atomic_set(&ino_dfr->stat, DFR_INO_STAT_IDLE);
  1186. list_del(&ino_dfr->entry);
  1187. ino_dfr->chunks = NULL;
  1188. ino_dfr->nr_chunks = 0;
  1189. INIT_LIST_HEAD(&ino_dfr->entry);
  1190. BUG_ON(!mutex_is_locked(&ino_dfr->lock));
  1191. mutex_unlock(&ino_dfr->lock);
  1192. iput(inode);
  1193. }
  1194. }
  1195. /* Clean-up sb_dfr */
  1196. sb_dfr->chunks = NULL;
  1197. sb_dfr->nr_chunks = 0;
  1198. INIT_LIST_HEAD(&sb_dfr->entry);
  1199. /* Clear dfr_new_clus page */
  1200. memset(sbi->dfr_new_clus, 0, PAGE_SIZE);
  1201. sbi->dfr_new_idx = 1;
  1202. memset(sbi->dfr_page_wb, 0, PAGE_SIZE);
  1203. sbi->dfr_hint_clus = sbi->dfr_hint_idx = sbi->dfr_reserved_clus = 0;
  1204. __unlock_super(sb);
  1205. }
  1206. /**
  1207. * @fn defrag_validate_pages
  1208. * @brief validate and mark dirty for victiim pages
  1209. * @return 0 on success, -errno otherwise
  1210. * @param inode inode
  1211. * @param chunk given chunk
  1212. * @remark protected by inode_lock and super_lock
  1213. */
  1214. static int
  1215. defrag_validate_pages(
  1216. IN struct inode *inode,
  1217. IN struct defrag_chunk_info *chunk)
  1218. {
  1219. struct super_block *sb = inode->i_sb;
  1220. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  1221. struct page *page = NULL;
  1222. unsigned int i_size = 0, page_off = 0, page_nr = 0;
  1223. int buf_i = 0, i = 0, err = 0;
  1224. i_size = i_size_read(inode);
  1225. page_off = chunk->f_clus * PAGES_PER_CLUS(sb);
  1226. page_nr = (i_size / PAGE_SIZE) + ((i_size % PAGE_SIZE) ? 1 : 0);
  1227. if ((i_size <= 0) || (page_nr <= 0)) {
  1228. dfr_err("inode %p, i_size %d, page_nr %d", inode, i_size, page_nr);
  1229. return -EINVAL;
  1230. }
  1231. /* Get victim pages
  1232. * and check its dirty/writeback/mapped state
  1233. */
  1234. for (i = 0;
  1235. i < min((int)(page_nr - page_off), (int)(chunk->nr_clus * PAGES_PER_CLUS(sb)));
  1236. i++) {
  1237. page = find_get_page(inode->i_mapping, page_off + i);
  1238. if (page)
  1239. if (!trylock_page(page)) {
  1240. put_page(page);
  1241. page = NULL;
  1242. }
  1243. if (!page) {
  1244. dfr_debug("get/lock_page() failed, index %d", i);
  1245. err = -EINVAL;
  1246. goto error;
  1247. }
  1248. sbi->dfr_pagep[buf_i++] = page;
  1249. if (PageError(page) || !PageUptodate(page) || PageDirty(page) ||
  1250. PageWriteback(page) || page_mapped(page)) {
  1251. dfr_debug("page %p, err %d, uptodate %d, "
  1252. "dirty %d, wb %d, mapped %d",
  1253. page, PageError(page), PageUptodate(page),
  1254. PageDirty(page), PageWriteback(page),
  1255. page_mapped(page));
  1256. err = -EINVAL;
  1257. goto error;
  1258. }
  1259. set_bit((page->index & (PAGES_PER_CLUS(sb) - 1)),
  1260. (volatile unsigned long *)&(sbi->dfr_page_wb[chunk->new_idx + i / PAGES_PER_CLUS(sb)]));
  1261. page = NULL;
  1262. }
  1263. /**
  1264. * All pages in the chunks are valid.
  1265. */
  1266. i_size -= (chunk->f_clus * (sbi->fsi.cluster_size));
  1267. BUG_ON(((i_size / PAGE_SIZE) + ((i_size % PAGE_SIZE) ? 1 : 0)) != (page_nr - page_off));
  1268. for (i = 0; i < buf_i; i++) {
  1269. struct buffer_head *bh = NULL, *head = NULL;
  1270. int bh_idx = 0;
  1271. page = sbi->dfr_pagep[i];
  1272. BUG_ON(!page);
  1273. /* Mark dirty in page */
  1274. set_page_dirty(page);
  1275. mark_page_accessed(page);
  1276. /* Attach empty BHs */
  1277. if (!page_has_buffers(page))
  1278. create_empty_buffers(page, 1 << inode->i_blkbits, 0);
  1279. /* Mark dirty in BHs */
  1280. bh = head = page_buffers(page);
  1281. BUG_ON(!bh && !i_size);
  1282. do {
  1283. if ((bh_idx >= 1) && (bh_idx >= (i_size >> inode->i_blkbits))) {
  1284. clear_buffer_dirty(bh);
  1285. } else {
  1286. if (PageUptodate(page))
  1287. if (!buffer_uptodate(bh))
  1288. set_buffer_uptodate(bh);
  1289. /* Set this bh as delay */
  1290. set_buffer_new(bh);
  1291. set_buffer_delay(bh);
  1292. mark_buffer_dirty(bh);
  1293. }
  1294. bh_idx++;
  1295. bh = bh->b_this_page;
  1296. } while (bh != head);
  1297. /* Mark this page accessed */
  1298. mark_page_accessed(page);
  1299. i_size -= PAGE_SIZE;
  1300. }
  1301. error:
  1302. /* Unlock and put refs for pages */
  1303. for (i = 0; i < buf_i; i++) {
  1304. BUG_ON(!sbi->dfr_pagep[i]);
  1305. unlock_page(sbi->dfr_pagep[i]);
  1306. put_page(sbi->dfr_pagep[i]);
  1307. }
  1308. memset(sbi->dfr_pagep, 0, sizeof(PAGE_SIZE));
  1309. return err;
  1310. }
  1311. /**
  1312. * @fn defrag_validate_reqs
  1313. * @brief validate defrag requests
  1314. * @return negative if all requests not valid, 0 otherwise
  1315. * @param sb super block
  1316. * @param chunks given chunks
  1317. */
  1318. static int
  1319. defrag_validate_reqs(
  1320. IN struct super_block *sb,
  1321. INOUT struct defrag_chunk_info *chunks)
  1322. {
  1323. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  1324. struct defrag_info *sb_dfr = &(sbi->dfr_info);
  1325. int i = 0, err = 0, err_cnt = 0;
  1326. /* Validate all reqs */
  1327. for (i = REQ_HEADER_IDX + 1; i < sb_dfr->nr_chunks; i++) {
  1328. struct defrag_chunk_info *chunk = NULL;
  1329. struct inode *inode = NULL;
  1330. struct defrag_info *ino_dfr = NULL;
  1331. chunk = &chunks[i];
  1332. /* Check inode */
  1333. __lock_super(sb);
  1334. inode = sdfat_iget(sb, chunk->i_pos);
  1335. if (!inode) {
  1336. dfr_debug("inode not found, i_pos %08llx", chunk->i_pos);
  1337. chunk->stat = DFR_CHUNK_STAT_ERR;
  1338. err_cnt++;
  1339. __unlock_super(sb);
  1340. continue;
  1341. }
  1342. __unlock_super(sb);
  1343. dfr_debug("req[%d] inode %p, i_pos %08llx, f_clus %d, "
  1344. "d_clus %08x, nr %d, prev %08x, next %08x",
  1345. i, inode, chunk->i_pos, chunk->f_clus, chunk->d_clus,
  1346. chunk->nr_clus, chunk->prev_clus, chunk->next_clus);
  1347. /**
  1348. * Lock ordering: inode_lock -> lock_super
  1349. */
  1350. inode_lock(inode);
  1351. __lock_super(sb);
  1352. /* Check if enough buffers exist for chunk->new_idx */
  1353. if ((sbi->dfr_new_idx + chunk->nr_clus) >= (PAGE_SIZE / sizeof(int))) {
  1354. dfr_err("dfr_new_idx %d, chunk->nr_clus %d",
  1355. sbi->dfr_new_idx, chunk->nr_clus);
  1356. err = -ENOSPC;
  1357. goto unlock;
  1358. }
  1359. /* Reserve clusters for defrag with DA */
  1360. err = fsapi_dfr_reserve_clus(sb, chunk->nr_clus);
  1361. if (err)
  1362. goto unlock;
  1363. /* Check clusters */
  1364. err = fsapi_dfr_validate_clus(inode, chunk, 0);
  1365. if (err) {
  1366. fsapi_dfr_reserve_clus(sb, 0 - chunk->nr_clus);
  1367. dfr_debug("Cluster validation: err %d", err);
  1368. goto unlock;
  1369. }
  1370. /* Check pages */
  1371. err = defrag_validate_pages(inode, chunk);
  1372. if (err) {
  1373. fsapi_dfr_reserve_clus(sb, 0 - chunk->nr_clus);
  1374. dfr_debug("Page validation: err %d", err);
  1375. goto unlock;
  1376. }
  1377. /* Mark IGNORE flag to victim AU */
  1378. if (sbi->options.improved_allocation & SDFAT_ALLOC_SMART)
  1379. fsapi_dfr_mark_ignore(sb, chunk->d_clus);
  1380. ino_dfr = &(SDFAT_I(inode)->dfr_info);
  1381. mutex_lock(&ino_dfr->lock);
  1382. /* Update chunk info */
  1383. chunk->stat = DFR_CHUNK_STAT_REQ;
  1384. chunk->new_idx = sbi->dfr_new_idx;
  1385. /* Update ino_dfr info */
  1386. if (list_empty(&(ino_dfr->entry))) {
  1387. list_add_tail(&ino_dfr->entry, &sb_dfr->entry);
  1388. ino_dfr->chunks = chunk;
  1389. igrab(inode);
  1390. }
  1391. ino_dfr->nr_chunks++;
  1392. atomic_set(&ino_dfr->stat, DFR_INO_STAT_REQ);
  1393. BUG_ON(!mutex_is_locked(&ino_dfr->lock));
  1394. mutex_unlock(&ino_dfr->lock);
  1395. /* Reserved buffers for chunk->new_idx */
  1396. sbi->dfr_new_idx += chunk->nr_clus;
  1397. unlock:
  1398. if (err) {
  1399. chunk->stat = DFR_CHUNK_STAT_ERR;
  1400. err_cnt++;
  1401. }
  1402. iput(inode);
  1403. __unlock_super(sb);
  1404. inode_unlock(inode);
  1405. }
  1406. /* Return error if all chunks are invalid */
  1407. if (err_cnt == sb_dfr->nr_chunks - 1) {
  1408. dfr_debug("%s failed (err_cnt %d)", __func__, err_cnt);
  1409. return -ENXIO;
  1410. }
  1411. return 0;
  1412. }
  1413. /**
  1414. * @fn defrag_check_fs_busy
  1415. * @brief check if this module busy
  1416. * @return 0 when idle, 1 otherwise
  1417. * @param sb super block
  1418. * @param reserved_clus # of reserved clusters
  1419. * @param queued_pages # of queued pages
  1420. */
  1421. static int
  1422. defrag_check_fs_busy(
  1423. IN struct super_block *sb,
  1424. OUT int *reserved_clus,
  1425. OUT int *queued_pages)
  1426. {
  1427. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  1428. int err = 0;
  1429. *reserved_clus = *queued_pages = 0;
  1430. __lock_super(sb);
  1431. *reserved_clus = fsi->reserved_clusters;
  1432. *queued_pages = atomic_read(&SDFAT_SB(sb)->stat_n_pages_queued);
  1433. if (*reserved_clus || *queued_pages)
  1434. err = 1;
  1435. __unlock_super(sb);
  1436. return err;
  1437. }
  1438. /**
  1439. * @fn sdfat_ioctl_defrag_req
  1440. * @brief ioctl to send defrag requests
  1441. * @return 0 on success, -errno otherwise
  1442. * @param inode inode
  1443. * @param uarg given requests
  1444. */
  1445. static int
  1446. sdfat_ioctl_defrag_req(
  1447. IN struct inode *inode,
  1448. INOUT unsigned int *uarg)
  1449. {
  1450. struct super_block *sb = inode->i_sb;
  1451. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  1452. struct defrag_info *sb_dfr = &(sbi->dfr_info);
  1453. struct defrag_chunk_header head;
  1454. struct defrag_chunk_info *chunks = NULL;
  1455. unsigned int len = 0;
  1456. int err = 0;
  1457. unsigned long timeout = 0;
  1458. /* Check overlapped defrag */
  1459. if (atomic_cmpxchg(&sb_dfr->stat, DFR_SB_STAT_IDLE, DFR_SB_STAT_REQ)) {
  1460. dfr_debug("sb_dfr->stat %d", atomic_read(&sb_dfr->stat));
  1461. return -EBUSY;
  1462. }
  1463. /* Check if defrag required */
  1464. __lock_super(sb);
  1465. if (!fsapi_dfr_check_dfr_required(sb, NULL, NULL, NULL)) {
  1466. dfr_debug("Not enough space left for defrag (err %d)", -ENOSPC);
  1467. atomic_set(&sb_dfr->stat, DFR_SB_STAT_IDLE);
  1468. __unlock_super(sb);
  1469. return -ENOSPC;
  1470. }
  1471. __unlock_super(sb);
  1472. /* Copy args */
  1473. memset(&head, 0, sizeof(struct defrag_chunk_header));
  1474. err = copy_from_user(&head, uarg, sizeof(struct defrag_chunk_info));
  1475. ERR_HANDLE(err);
  1476. /* If FS busy, cancel defrag */
  1477. if (!(head.mode == DFR_MODE_TEST)) {
  1478. int reserved_clus = 0, queued_pages = 0;
  1479. err = defrag_check_fs_busy(sb, &reserved_clus, &queued_pages);
  1480. if (err) {
  1481. dfr_debug("FS busy, cancel defrag (reserved_clus %d, queued_pages %d)",
  1482. reserved_clus, queued_pages);
  1483. err = -EBUSY;
  1484. goto error;
  1485. }
  1486. }
  1487. /* Total length is saved in the chunk header's nr_chunks field */
  1488. len = head.nr_chunks;
  1489. ERR_HANDLE2(!len, err, -EINVAL);
  1490. dfr_debug("IOC_DFR_REQ started (mode %d, nr_req %d)", head.mode, len - 1);
  1491. if (get_order(len * sizeof(struct defrag_chunk_info)) > MAX_ORDER) {
  1492. dfr_debug("len %d, sizeof(struct defrag_chunk_info) %lu, MAX_ORDER %d",
  1493. len, sizeof(struct defrag_chunk_info), MAX_ORDER);
  1494. err = -EINVAL;
  1495. goto error;
  1496. }
  1497. chunks = alloc_pages_exact(len * sizeof(struct defrag_chunk_info),
  1498. GFP_KERNEL | __GFP_ZERO);
  1499. ERR_HANDLE2(!chunks, err, -ENOMEM)
  1500. err = copy_from_user(chunks, uarg, len * sizeof(struct defrag_chunk_info));
  1501. ERR_HANDLE(err);
  1502. /* Initialize sb_dfr */
  1503. sb_dfr->chunks = chunks;
  1504. sb_dfr->nr_chunks = len;
  1505. /* Validate reqs & mark defrag/dirty */
  1506. err = defrag_validate_reqs(sb, sb_dfr->chunks);
  1507. ERR_HANDLE(err);
  1508. atomic_set(&sb_dfr->stat, DFR_SB_STAT_VALID);
  1509. /* Wait for defrag completion */
  1510. if (head.mode == DFR_MODE_ONESHOT)
  1511. timeout = 0;
  1512. else if (head.mode & DFR_MODE_BACKGROUND)
  1513. timeout = DFR_DEFAULT_TIMEOUT;
  1514. else
  1515. timeout = DFR_MIN_TIMEOUT;
  1516. dfr_debug("Wait for completion (timeout %ld)", timeout);
  1517. init_completion(&sbi->dfr_complete);
  1518. timeout = wait_for_completion_timeout(&sbi->dfr_complete, timeout);
  1519. if (!timeout) {
  1520. /* Force defrag_updat_fat() after timeout. */
  1521. dfr_debug("Force sync(), mode %d, left-timeout %ld", head.mode, timeout);
  1522. down_read(&sb->s_umount);
  1523. sync_inodes_sb(sb);
  1524. __lock_super(sb);
  1525. fsapi_dfr_update_fat_next(sb);
  1526. fsapi_sync_fs(sb, 1);
  1527. #ifdef CONFIG_SDFAT_DFR_DEBUG
  1528. /* SPO test */
  1529. fsapi_dfr_spo_test(sb, DFR_SPO_FAT_NEXT, __func__);
  1530. #endif
  1531. fsapi_dfr_update_fat_prev(sb, 1);
  1532. fsapi_sync_fs(sb, 1);
  1533. __unlock_super(sb);
  1534. up_read(&sb->s_umount);
  1535. }
  1536. #ifdef CONFIG_SDFAT_DFR_DEBUG
  1537. /* SPO test */
  1538. fsapi_dfr_spo_test(sb, DFR_SPO_NORMAL, __func__);
  1539. #endif
  1540. __lock_super(sb);
  1541. /* Send DISCARD to clean-ed AUs */
  1542. fsapi_dfr_check_discard(sb);
  1543. #ifdef CONFIG_SDFAT_DFR_DEBUG
  1544. /* SPO test */
  1545. fsapi_dfr_spo_test(sb, DFR_SPO_DISCARD, __func__);
  1546. #endif
  1547. /* Unmark IGNORE flag to all victim AUs */
  1548. fsapi_dfr_unmark_ignore_all(sb);
  1549. __unlock_super(sb);
  1550. err = copy_to_user(uarg, sb_dfr->chunks, sizeof(struct defrag_chunk_info) * len);
  1551. ERR_HANDLE(err);
  1552. error:
  1553. /* Clean-up sb_dfr & ino_dfr */
  1554. defrag_cleanup_reqs(sb, err);
  1555. if (chunks)
  1556. free_pages_exact(chunks, len * sizeof(struct defrag_chunk_info));
  1557. /* Set sb_dfr's state as IDLE */
  1558. atomic_set(&sb_dfr->stat, DFR_SB_STAT_IDLE);
  1559. dfr_debug("IOC_DFR_REQ done (err %d)", err);
  1560. return err;
  1561. }
  1562. /**
  1563. * @fn sdfat_ioctl_defrag_trav
  1564. * @brief ioctl to traverse given directory for defrag
  1565. * @return 0 on success, -errno otherwise
  1566. * @param inode inode
  1567. * @param uarg output buffer
  1568. */
  1569. static int
  1570. sdfat_ioctl_defrag_trav(
  1571. IN struct inode *inode,
  1572. INOUT unsigned int *uarg)
  1573. {
  1574. struct super_block *sb = inode->i_sb;
  1575. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  1576. struct defrag_info *sb_dfr = &(sbi->dfr_info);
  1577. struct defrag_trav_arg *args = (struct defrag_trav_arg *) sbi->dfr_pagep;
  1578. struct defrag_trav_header *header = (struct defrag_trav_header *) args;
  1579. int err = 0;
  1580. /* Check overlapped defrag */
  1581. if (atomic_cmpxchg(&sb_dfr->stat, DFR_SB_STAT_IDLE, DFR_SB_STAT_REQ)) {
  1582. dfr_debug("sb_dfr->stat %d", atomic_read(&sb_dfr->stat));
  1583. return -EBUSY;
  1584. }
  1585. /* Check if defrag required */
  1586. __lock_super(sb);
  1587. if (!fsapi_dfr_check_dfr_required(sb, NULL, NULL, NULL)) {
  1588. dfr_debug("Not enough space left for defrag (err %d)", -ENOSPC);
  1589. atomic_set(&sb_dfr->stat, DFR_SB_STAT_IDLE);
  1590. __unlock_super(sb);
  1591. return -ENOSPC;
  1592. }
  1593. __unlock_super(sb);
  1594. /* Copy args */
  1595. err = copy_from_user(args, uarg, PAGE_SIZE);
  1596. ERR_HANDLE(err);
  1597. /**
  1598. * Check args.
  1599. * ROOT directory has i_pos = 0 and start_clus = 0 .
  1600. */
  1601. if (!(header->type & DFR_TRAV_TYPE_HEADER)) {
  1602. err = -EINVAL;
  1603. dfr_debug("type %d, i_pos %08llx, start_clus %08x",
  1604. header->type, header->i_pos, header->start_clus);
  1605. goto error;
  1606. }
  1607. /* If FS busy, cancel defrag */
  1608. if (!(header->type & DFR_TRAV_TYPE_TEST)) {
  1609. unsigned int reserved_clus = 0, queued_pages = 0;
  1610. err = defrag_check_fs_busy(sb, &reserved_clus, &queued_pages);
  1611. if (err) {
  1612. dfr_debug("FS busy, cancel defrag (reserved_clus %d, queued_pages %d)",
  1613. reserved_clus, queued_pages);
  1614. err = -EBUSY;
  1615. goto error;
  1616. }
  1617. }
  1618. /* Scan given directory and gather info */
  1619. inode_lock(inode);
  1620. __lock_super(sb);
  1621. err = fsapi_dfr_scan_dir(sb, (void *)args);
  1622. __unlock_super(sb);
  1623. inode_unlock(inode);
  1624. ERR_HANDLE(err);
  1625. /* Copy the result to user */
  1626. err = copy_to_user(uarg, args, PAGE_SIZE);
  1627. ERR_HANDLE(err);
  1628. error:
  1629. memset(sbi->dfr_pagep, 0, PAGE_SIZE);
  1630. atomic_set(&sb_dfr->stat, DFR_SB_STAT_IDLE);
  1631. return err;
  1632. }
  1633. /**
  1634. * @fn sdfat_ioctl_defrag_info
  1635. * @brief ioctl to get HW param info
  1636. * @return 0 on success, -errno otherwise
  1637. * @param sb super block
  1638. * @param uarg output buffer
  1639. */
  1640. static int
  1641. sdfat_ioctl_defrag_info(
  1642. IN struct super_block *sb,
  1643. OUT unsigned int *uarg)
  1644. {
  1645. struct defrag_info_arg info_arg;
  1646. int err = 0;
  1647. memset(&info_arg, 0, sizeof(struct defrag_info_arg));
  1648. __lock_super(sb);
  1649. err = fsapi_dfr_get_info(sb, &info_arg);
  1650. __unlock_super(sb);
  1651. ERR_HANDLE(err);
  1652. dfr_debug("IOC_DFR_INFO: sec_per_au %d, hidden_sectors %d",
  1653. info_arg.sec_per_au, info_arg.hidden_sectors);
  1654. err = copy_to_user(uarg, &info_arg, sizeof(struct defrag_info_arg));
  1655. error:
  1656. return err;
  1657. }
  1658. #endif /* CONFIG_SDFAT_DFR */
  1659. static inline int __do_dfr_map_cluster(struct inode *inode, u32 clu_offset, unsigned int *clus_ptr)
  1660. {
  1661. #ifdef CONFIG_SDFAT_DFR
  1662. return fsapi_dfr_map_clus(inode, clu_offset, clus_ptr);
  1663. #else
  1664. return 0;
  1665. #endif
  1666. }
  1667. static inline int __check_dfr_on(struct inode *inode, loff_t start, loff_t end, const char *fname)
  1668. {
  1669. #ifdef CONFIG_SDFAT_DFR
  1670. struct defrag_info *ino_dfr = &(SDFAT_I(inode)->dfr_info);
  1671. if ((atomic_read(&ino_dfr->stat) == DFR_INO_STAT_REQ) &&
  1672. fsapi_dfr_check_dfr_on(inode, start, end, 0, fname))
  1673. return 1;
  1674. #endif
  1675. return 0;
  1676. }
  1677. static inline int __cancel_dfr_work(struct inode *inode, loff_t start, loff_t end, const char *fname)
  1678. {
  1679. #ifdef CONFIG_SDFAT_DFR
  1680. struct defrag_info *ino_dfr = &(SDFAT_I(inode)->dfr_info);
  1681. /* Cancel DEFRAG */
  1682. if (atomic_read(&ino_dfr->stat) == DFR_INO_STAT_REQ)
  1683. fsapi_dfr_check_dfr_on(inode, start, end, 1, fname);
  1684. #endif
  1685. return 0;
  1686. }
  1687. static inline int __dfr_writepage_end_io(struct page *page)
  1688. {
  1689. #ifdef CONFIG_SDFAT_DFR
  1690. struct defrag_info *ino_dfr = &(SDFAT_I(page->mapping->host)->dfr_info);
  1691. if (atomic_read(&ino_dfr->stat) == DFR_INO_STAT_REQ)
  1692. fsapi_dfr_writepage_endio(page);
  1693. #endif
  1694. return 0;
  1695. }
  1696. static inline void __init_dfr_info(struct inode *inode)
  1697. {
  1698. #ifdef CONFIG_SDFAT_DFR
  1699. memset(&(SDFAT_I(inode)->dfr_info), 0, sizeof(struct defrag_info));
  1700. INIT_LIST_HEAD(&(SDFAT_I(inode)->dfr_info.entry));
  1701. mutex_init(&(SDFAT_I(inode)->dfr_info.lock));
  1702. #endif
  1703. }
  1704. static inline int __alloc_dfr_mem_if_required(struct super_block *sb)
  1705. {
  1706. #ifdef CONFIG_SDFAT_DFR
  1707. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  1708. if (!sbi->options.defrag)
  1709. return 0;
  1710. memset(&sbi->dfr_info, 0, sizeof(struct defrag_info));
  1711. INIT_LIST_HEAD(&(sbi->dfr_info.entry));
  1712. mutex_init(&(sbi->dfr_info.lock));
  1713. sbi->dfr_new_clus = kzalloc(PAGE_SIZE, GFP_KERNEL);
  1714. if (!sbi->dfr_new_clus) {
  1715. dfr_debug("error %d", -ENOMEM);
  1716. return -ENOMEM;
  1717. }
  1718. sbi->dfr_new_idx = 1;
  1719. sbi->dfr_page_wb = kzalloc(PAGE_SIZE, GFP_KERNEL);
  1720. if (!sbi->dfr_page_wb) {
  1721. dfr_debug("error %d", -ENOMEM);
  1722. return -ENOMEM;
  1723. }
  1724. sbi->dfr_pagep = alloc_pages_exact(sizeof(struct page *) *
  1725. PAGES_PER_AU(sb), GFP_KERNEL | __GFP_ZERO);
  1726. if (!sbi->dfr_pagep) {
  1727. dfr_debug("error %d", -ENOMEM);
  1728. return -ENOMEM;
  1729. }
  1730. #endif
  1731. return 0;
  1732. }
  1733. static void __free_dfr_mem_if_required(struct super_block *sb)
  1734. {
  1735. #ifdef CONFIG_SDFAT_DFR
  1736. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  1737. if (sbi->dfr_pagep) {
  1738. free_pages_exact(sbi->dfr_pagep, sizeof(struct page *) * PAGES_PER_AU(sb));
  1739. sbi->dfr_pagep = NULL;
  1740. }
  1741. /* thanks for kfree */
  1742. kfree(sbi->dfr_page_wb);
  1743. sbi->dfr_page_wb = NULL;
  1744. kfree(sbi->dfr_new_clus);
  1745. sbi->dfr_new_clus = NULL;
  1746. #endif
  1747. }
  1748. static int sdfat_file_mmap(struct file *file, struct vm_area_struct *vm_struct)
  1749. {
  1750. __cancel_dfr_work(file->f_mapping->host,
  1751. (loff_t)vm_struct->vm_start,
  1752. (loff_t)(vm_struct->vm_end - 1),
  1753. __func__);
  1754. return generic_file_mmap(file, vm_struct);
  1755. }
  1756. static int sdfat_ioctl_volume_id(struct inode *dir)
  1757. {
  1758. struct sdfat_sb_info *sbi = SDFAT_SB(dir->i_sb);
  1759. FS_INFO_T *fsi = &(sbi->fsi);
  1760. return fsi->vol_id;
  1761. }
  1762. static int sdfat_dfr_ioctl(struct inode *inode, struct file *filp,
  1763. unsigned int cmd, unsigned long arg)
  1764. {
  1765. #ifdef CONFIG_SDFAT_DFR
  1766. switch (cmd) {
  1767. case SDFAT_IOCTL_DFR_INFO: {
  1768. struct super_block *sb = inode->i_sb;
  1769. FS_INFO_T *fsi = &SDFAT_SB(sb)->fsi;
  1770. unsigned int __user *uarg = (unsigned int __user *) arg;
  1771. __lock_super(sb);
  1772. /* Check FS type (FAT32 only) */
  1773. if (fsi->vol_type != FAT32) {
  1774. dfr_err("Defrag not supported, vol_type %d", fsi->vol_type);
  1775. __unlock_super(sb);
  1776. return -EPERM;
  1777. }
  1778. /* Check if SB's defrag option enabled */
  1779. if (!(SDFAT_SB(sb)->options.defrag)) {
  1780. dfr_err("Defrag not supported, sbi->options.defrag %d", SDFAT_SB(sb)->options.defrag);
  1781. __unlock_super(sb);
  1782. return -EPERM;
  1783. }
  1784. /* Only IOCTL on mount-point allowed */
  1785. if (filp->f_path.mnt->mnt_root != filp->f_path.dentry) {
  1786. dfr_err("IOC_DFR_INFO only allowed on ROOT, root %p, dentry %p",
  1787. filp->f_path.mnt->mnt_root, filp->f_path.dentry);
  1788. __unlock_super(sb);
  1789. return -EPERM;
  1790. }
  1791. __unlock_super(sb);
  1792. return sdfat_ioctl_defrag_info(sb, uarg);
  1793. }
  1794. case SDFAT_IOCTL_DFR_TRAV: {
  1795. struct super_block *sb = inode->i_sb;
  1796. FS_INFO_T *fsi = &SDFAT_SB(sb)->fsi;
  1797. unsigned int __user *uarg = (unsigned int __user *) arg;
  1798. __lock_super(sb);
  1799. /* Check FS type (FAT32 only) */
  1800. if (fsi->vol_type != FAT32) {
  1801. dfr_err("Defrag not supported, vol_type %d", fsi->vol_type);
  1802. __unlock_super(sb);
  1803. return -EPERM;
  1804. }
  1805. /* Check if SB's defrag option enabled */
  1806. if (!(SDFAT_SB(sb)->options.defrag)) {
  1807. dfr_err("Defrag not supported, sbi->options.defrag %d", SDFAT_SB(sb)->options.defrag);
  1808. __unlock_super(sb);
  1809. return -EPERM;
  1810. }
  1811. __unlock_super(sb);
  1812. return sdfat_ioctl_defrag_trav(inode, uarg);
  1813. }
  1814. case SDFAT_IOCTL_DFR_REQ: {
  1815. struct super_block *sb = inode->i_sb;
  1816. FS_INFO_T *fsi = &SDFAT_SB(sb)->fsi;
  1817. unsigned int __user *uarg = (unsigned int __user *) arg;
  1818. __lock_super(sb);
  1819. /* Check if FS_ERROR occurred */
  1820. if (sb_rdonly(sb)) {
  1821. dfr_err("RDONLY partition (err %d)", -EPERM);
  1822. __unlock_super(sb);
  1823. return -EPERM;
  1824. }
  1825. /* Check FS type (FAT32 only) */
  1826. if (fsi->vol_type != FAT32) {
  1827. dfr_err("Defrag not supported, vol_type %d", fsi->vol_type);
  1828. __unlock_super(sb);
  1829. return -EINVAL;
  1830. }
  1831. /* Check if SB's defrag option enabled */
  1832. if (!(SDFAT_SB(sb)->options.defrag)) {
  1833. dfr_err("Defrag not supported, sbi->options.defrag %d", SDFAT_SB(sb)->options.defrag);
  1834. __unlock_super(sb);
  1835. return -EPERM;
  1836. }
  1837. /* Only IOCTL on mount-point allowed */
  1838. if (filp->f_path.mnt->mnt_root != filp->f_path.dentry) {
  1839. dfr_err("IOC_DFR_INFO only allowed on ROOT, root %p, dentry %p",
  1840. filp->f_path.mnt->mnt_root, filp->f_path.dentry);
  1841. __unlock_super(sb);
  1842. return -EINVAL;
  1843. }
  1844. __unlock_super(sb);
  1845. return sdfat_ioctl_defrag_req(inode, uarg);
  1846. }
  1847. #ifdef CONFIG_SDFAT_DFR_DEBUG
  1848. case SDFAT_IOCTL_DFR_SPO_FLAG: {
  1849. struct sdfat_sb_info *sbi = SDFAT_SB(inode->i_sb);
  1850. int ret = 0;
  1851. ret = get_user(sbi->dfr_spo_flag, (int __user *)arg);
  1852. dfr_debug("dfr_spo_flag %d", sbi->dfr_spo_flag);
  1853. return ret;
  1854. }
  1855. #endif /* CONFIG_SDFAT_DFR_DEBUG */
  1856. }
  1857. #endif /* CONFIG_SDFAT_DFR */
  1858. /* Inappropriate ioctl for device */
  1859. return -ENOTTY;
  1860. }
  1861. static int sdfat_dbg_ioctl(struct inode *inode, struct file *filp,
  1862. unsigned int cmd, unsigned long arg)
  1863. {
  1864. #ifdef CONFIG_SDFAT_DBG_IOCTL
  1865. struct super_block *sb = inode->i_sb;
  1866. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  1867. unsigned int flags;
  1868. switch (cmd) {
  1869. case SDFAT_IOC_GET_DEBUGFLAGS:
  1870. flags = sbi->debug_flags;
  1871. return put_user(flags, (int __user *)arg);
  1872. case SDFAT_IOC_SET_DEBUGFLAGS:
  1873. flags = 0;
  1874. if (!capable(CAP_SYS_ADMIN))
  1875. return -EPERM;
  1876. if (get_user(flags, (int __user *) arg))
  1877. return -EFAULT;
  1878. __lock_super(sb);
  1879. sbi->debug_flags = flags;
  1880. __unlock_super(sb);
  1881. return 0;
  1882. case SDFAT_IOCTL_PANIC:
  1883. panic("ioctl panic for test");
  1884. /* COULD NOT REACH HEAR */
  1885. return 0;
  1886. }
  1887. #endif /* CONFIG_SDFAT_DBG_IOCTL */
  1888. return -ENOTTY;
  1889. }
  1890. static long sdfat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  1891. {
  1892. struct inode *inode = file_inode(filp);
  1893. int err;
  1894. if (cmd == SDFAT_IOCTL_GET_VOLUME_ID)
  1895. return sdfat_ioctl_volume_id(inode);
  1896. err = sdfat_dfr_ioctl(inode, filp, cmd, arg);
  1897. if (err != -ENOTTY)
  1898. return err;
  1899. /* -ENOTTY if inappropriate ioctl for device */
  1900. return sdfat_dbg_ioctl(inode, filp, cmd, arg);
  1901. }
  1902. static int __sdfat_getattr(struct inode *inode, struct kstat *stat)
  1903. {
  1904. TMSG("%s entered\n", __func__);
  1905. generic_fillattr(inode, stat);
  1906. stat->blksize = SDFAT_SB(inode->i_sb)->fsi.cluster_size;
  1907. TMSG("%s exited\n", __func__);
  1908. return 0;
  1909. }
  1910. static void __sdfat_writepage_end_io(struct bio *bio, int err)
  1911. {
  1912. struct page *page = bio->bi_io_vec->bv_page;
  1913. struct super_block *sb = page->mapping->host->i_sb;
  1914. ASSERT(bio->bi_vcnt == 1); /* Single page endio */
  1915. ASSERT(bio_data_dir(bio)); /* Write */
  1916. if (err) {
  1917. SetPageError(page);
  1918. mapping_set_error(page->mapping, err);
  1919. }
  1920. __dfr_writepage_end_io(page);
  1921. #ifdef CONFIG_SDFAT_TRACE_IO
  1922. {
  1923. //struct sdfat_sb_info *sbi = SDFAT_SB(bio->bi_bdev->bd_super);
  1924. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  1925. sbi->stat_n_pages_written++;
  1926. if (page->mapping->host == sb->s_bdev->bd_inode)
  1927. sbi->stat_n_bdev_pages_written++;
  1928. /* 4 MB = 1024 pages => 0.4 sec (approx.)
  1929. * 32 KB = 64 pages => 0.025 sec
  1930. * Min. average latency b/w msgs. ~= 0.025 sec
  1931. */
  1932. if ((sbi->stat_n_pages_written & 63) == 0) {
  1933. DMSG("STAT:%u, %u, %u, %u (Sector #: %u)\n",
  1934. sbi->stat_n_pages_added, sbi->stat_n_pages_written,
  1935. sbi->stat_n_bdev_pages_witten,
  1936. sbi->stat_n_pages_confused,
  1937. (unsigned int)__sdfat_bio_sector(bio));
  1938. }
  1939. }
  1940. #endif
  1941. end_page_writeback(page);
  1942. bio_put(bio);
  1943. // Update trace info.
  1944. atomic_dec(&SDFAT_SB(sb)->stat_n_pages_queued);
  1945. }
  1946. static int __support_write_inode_sync(struct super_block *sb)
  1947. {
  1948. #ifdef CONFIG_SDFAT_SUPPORT_DIR_SYNC
  1949. #ifdef CONFIG_SDFAT_DELAYED_META_DIRTY
  1950. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  1951. if (sbi->fsi.vol_type != EXFAT)
  1952. return 0;
  1953. #endif
  1954. return 1;
  1955. #endif
  1956. return 0;
  1957. }
  1958. static int __sdfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
  1959. {
  1960. struct inode *inode = filp->f_mapping->host;
  1961. struct super_block *sb = inode->i_sb;
  1962. int res, err = 0;
  1963. res = __sdfat_generic_file_fsync(filp, start, end, datasync);
  1964. if (!__support_write_inode_sync(sb))
  1965. err = fsapi_sync_fs(sb, 1);
  1966. return res ? res : err;
  1967. }
  1968. static const struct file_operations sdfat_dir_operations = {
  1969. .llseek = generic_file_llseek,
  1970. .read = generic_read_dir,
  1971. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)
  1972. .iterate = sdfat_iterate,
  1973. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0) */
  1974. .readdir = sdfat_readdir,
  1975. #endif
  1976. .fsync = sdfat_file_fsync,
  1977. .unlocked_ioctl = sdfat_generic_ioctl,
  1978. };
  1979. static int __sdfat_create(struct inode *dir, struct dentry *dentry)
  1980. {
  1981. struct super_block *sb = dir->i_sb;
  1982. struct inode *inode;
  1983. sdfat_timespec_t ts;
  1984. FILE_ID_T fid;
  1985. loff_t i_pos;
  1986. int err;
  1987. __lock_super(sb);
  1988. TMSG("%s entered\n", __func__);
  1989. ts = current_time(dir);
  1990. err = fsapi_create(dir, (u8 *) dentry->d_name.name, FM_REGULAR, &fid);
  1991. if (err)
  1992. goto out;
  1993. __lock_d_revalidate(dentry);
  1994. inode_inc_iversion(dir);
  1995. dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
  1996. if (IS_DIRSYNC(dir))
  1997. (void) sdfat_sync_inode(dir);
  1998. else
  1999. mark_inode_dirty(dir);
  2000. i_pos = sdfat_make_i_pos(&fid);
  2001. inode = sdfat_build_inode(sb, &fid, i_pos);
  2002. if (IS_ERR(inode)) {
  2003. err = PTR_ERR(inode);
  2004. goto out;
  2005. }
  2006. inode_inc_iversion(inode);
  2007. inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
  2008. /* timestamp is already written, so mark_inode_dirty() is unneeded. */
  2009. d_instantiate(dentry, inode);
  2010. out:
  2011. __unlock_d_revalidate(dentry);
  2012. __unlock_super(sb);
  2013. TMSG("%s exited with err(%d)\n", __func__, err);
  2014. if (!err)
  2015. sdfat_statistics_set_create(fid.flags);
  2016. return err;
  2017. }
  2018. static int sdfat_find(struct inode *dir, struct qstr *qname, FILE_ID_T *fid)
  2019. {
  2020. int err;
  2021. if (qname->len == 0)
  2022. return -ENOENT;
  2023. err = fsapi_lookup(dir, (u8 *) qname->name, fid);
  2024. if (err)
  2025. return -ENOENT;
  2026. return 0;
  2027. }
  2028. static int sdfat_d_anon_disconn(struct dentry *dentry)
  2029. {
  2030. return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
  2031. }
  2032. static struct dentry *__sdfat_lookup(struct inode *dir, struct dentry *dentry)
  2033. {
  2034. struct super_block *sb = dir->i_sb;
  2035. struct inode *inode;
  2036. struct dentry *alias;
  2037. int err;
  2038. FILE_ID_T fid;
  2039. loff_t i_pos;
  2040. u64 ret;
  2041. mode_t i_mode;
  2042. __lock_super(sb);
  2043. TMSG("%s entered\n", __func__);
  2044. err = sdfat_find(dir, &dentry->d_name, &fid);
  2045. if (err) {
  2046. if (err == -ENOENT) {
  2047. inode = NULL;
  2048. goto out;
  2049. }
  2050. goto error;
  2051. }
  2052. i_pos = sdfat_make_i_pos(&fid);
  2053. inode = sdfat_build_inode(sb, &fid, i_pos);
  2054. if (IS_ERR(inode)) {
  2055. err = PTR_ERR(inode);
  2056. goto error;
  2057. }
  2058. i_mode = inode->i_mode;
  2059. if (S_ISLNK(i_mode) && !SDFAT_I(inode)->target) {
  2060. SDFAT_I(inode)->target = kmalloc((i_size_read(inode)+1), GFP_KERNEL);
  2061. if (!SDFAT_I(inode)->target) {
  2062. err = -ENOMEM;
  2063. goto error;
  2064. }
  2065. fsapi_read_link(dir, &fid, SDFAT_I(inode)->target, i_size_read(inode), &ret);
  2066. *(SDFAT_I(inode)->target + i_size_read(inode)) = '\0';
  2067. }
  2068. alias = d_find_alias(inode);
  2069. /*
  2070. * Checking "alias->d_parent == dentry->d_parent" to make sure
  2071. * FS is not corrupted (especially double linked dir).
  2072. */
  2073. if (alias && alias->d_parent == dentry->d_parent &&
  2074. !sdfat_d_anon_disconn(alias)) {
  2075. /*
  2076. * Unhashed alias is able to exist because of revalidate()
  2077. * called by lookup_fast. You can easily make this status
  2078. * by calling create and lookup concurrently
  2079. * In such case, we reuse an alias instead of new dentry
  2080. */
  2081. if (d_unhashed(alias)) {
  2082. #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 10, 0)
  2083. BUG_ON(alias->d_name.hash != dentry->d_name.hash && alias->d_name.len != dentry->d_name.len);
  2084. #else
  2085. BUG_ON(alias->d_name.hash_len != dentry->d_name.hash_len);
  2086. #endif
  2087. sdfat_msg(sb, KERN_INFO, "rehashed a dentry(%p) "
  2088. "in read lookup", alias);
  2089. d_drop(dentry);
  2090. d_rehash(alias);
  2091. } else if (!S_ISDIR(i_mode)) {
  2092. /*
  2093. * This inode has non anonymous-DCACHE_DISCONNECTED
  2094. * dentry. This means, the user did ->lookup() by an
  2095. * another name (longname vs 8.3 alias of it) in past.
  2096. *
  2097. * Switch to new one for reason of locality if possible.
  2098. */
  2099. d_move(alias, dentry);
  2100. }
  2101. iput(inode);
  2102. __unlock_super(sb);
  2103. TMSG("%s exited\n", __func__);
  2104. return alias;
  2105. }
  2106. dput(alias);
  2107. out:
  2108. /* initialize d_time even though it is positive dentry */
  2109. dentry->d_time = (unsigned long)inode_peek_iversion(dir);
  2110. __unlock_super(sb);
  2111. dentry = d_splice_alias(inode, dentry);
  2112. TMSG("%s exited\n", __func__);
  2113. return dentry;
  2114. error:
  2115. __unlock_super(sb);
  2116. TMSG("%s exited with err(%d)\n", __func__, err);
  2117. return ERR_PTR(err);
  2118. }
  2119. static int sdfat_unlink(struct inode *dir, struct dentry *dentry)
  2120. {
  2121. struct inode *inode = dentry->d_inode;
  2122. struct super_block *sb = dir->i_sb;
  2123. sdfat_timespec_t ts;
  2124. int err;
  2125. __lock_super(sb);
  2126. TMSG("%s entered\n", __func__);
  2127. ts = current_time(dir);
  2128. SDFAT_I(inode)->fid.size = i_size_read(inode);
  2129. __cancel_dfr_work(inode, 0, SDFAT_I(inode)->fid.size, __func__);
  2130. err = fsapi_unlink(dir, &(SDFAT_I(inode)->fid));
  2131. if (err)
  2132. goto out;
  2133. __lock_d_revalidate(dentry);
  2134. inode_inc_iversion(dir);
  2135. dir->i_mtime = dir->i_atime = ts;
  2136. if (IS_DIRSYNC(dir))
  2137. (void) sdfat_sync_inode(dir);
  2138. else
  2139. mark_inode_dirty(dir);
  2140. clear_nlink(inode);
  2141. inode->i_mtime = inode->i_atime = ts;
  2142. sdfat_detach(inode);
  2143. dentry->d_time = (unsigned long)inode_peek_iversion(dir);
  2144. out:
  2145. __unlock_d_revalidate(dentry);
  2146. __unlock_super(sb);
  2147. TMSG("%s exited with err(%d)\n", __func__, err);
  2148. return err;
  2149. }
  2150. static int sdfat_symlink(struct inode *dir, struct dentry *dentry, const char *target)
  2151. {
  2152. struct super_block *sb = dir->i_sb;
  2153. struct inode *inode;
  2154. sdfat_timespec_t ts;
  2155. FILE_ID_T fid;
  2156. loff_t i_pos;
  2157. int err;
  2158. u64 len = (u64) strlen(target);
  2159. u64 ret;
  2160. /* symlink option check */
  2161. if (!SDFAT_SB(sb)->options.symlink)
  2162. return -ENOTSUPP;
  2163. __lock_super(sb);
  2164. TMSG("%s entered\n", __func__);
  2165. ts = current_time(dir);
  2166. err = fsapi_create(dir, (u8 *) dentry->d_name.name, FM_SYMLINK, &fid);
  2167. if (err)
  2168. goto out;
  2169. err = fsapi_write_link(dir, &fid, (char *) target, len, &ret);
  2170. if (err) {
  2171. fsapi_remove(dir, &fid);
  2172. goto out;
  2173. }
  2174. __lock_d_revalidate(dentry);
  2175. inode_inc_iversion(dir);
  2176. dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
  2177. if (IS_DIRSYNC(dir))
  2178. (void) sdfat_sync_inode(dir);
  2179. else
  2180. mark_inode_dirty(dir);
  2181. i_pos = sdfat_make_i_pos(&fid);
  2182. inode = sdfat_build_inode(sb, &fid, i_pos);
  2183. if (IS_ERR(inode)) {
  2184. err = PTR_ERR(inode);
  2185. goto out;
  2186. }
  2187. inode_inc_iversion(inode);
  2188. inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
  2189. /* timestamp is already written, so mark_inode_dirty() is unneeded. */
  2190. SDFAT_I(inode)->target = kmalloc((len+1), GFP_KERNEL);
  2191. if (!SDFAT_I(inode)->target) {
  2192. err = -ENOMEM;
  2193. goto out;
  2194. }
  2195. memcpy(SDFAT_I(inode)->target, target, len+1);
  2196. d_instantiate(dentry, inode);
  2197. out:
  2198. __unlock_d_revalidate(dentry);
  2199. __unlock_super(sb);
  2200. TMSG("%s exited with err(%d)\n", __func__, err);
  2201. return err;
  2202. }
  2203. static int __sdfat_mkdir(struct inode *dir, struct dentry *dentry)
  2204. {
  2205. struct super_block *sb = dir->i_sb;
  2206. struct inode *inode;
  2207. sdfat_timespec_t ts;
  2208. FILE_ID_T fid;
  2209. loff_t i_pos;
  2210. int err;
  2211. __lock_super(sb);
  2212. TMSG("%s entered\n", __func__);
  2213. ts = current_time(dir);
  2214. err = fsapi_mkdir(dir, (u8 *) dentry->d_name.name, &fid);
  2215. if (err)
  2216. goto out;
  2217. __lock_d_revalidate(dentry);
  2218. inode_inc_iversion(dir);
  2219. dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
  2220. if (IS_DIRSYNC(dir))
  2221. (void) sdfat_sync_inode(dir);
  2222. else
  2223. mark_inode_dirty(dir);
  2224. inc_nlink(dir);
  2225. i_pos = sdfat_make_i_pos(&fid);
  2226. inode = sdfat_build_inode(sb, &fid, i_pos);
  2227. if (IS_ERR(inode)) {
  2228. err = PTR_ERR(inode);
  2229. goto out;
  2230. }
  2231. inode_inc_iversion(inode);
  2232. inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
  2233. /* timestamp is already written, so mark_inode_dirty() is unneeded. */
  2234. d_instantiate(dentry, inode);
  2235. out:
  2236. __unlock_d_revalidate(dentry);
  2237. __unlock_super(sb);
  2238. TMSG("%s exited with err(%d)\n", __func__, err);
  2239. if (!err)
  2240. sdfat_statistics_set_mkdir(fid.flags);
  2241. return err;
  2242. }
  2243. static int sdfat_rmdir(struct inode *dir, struct dentry *dentry)
  2244. {
  2245. struct inode *inode = dentry->d_inode;
  2246. struct super_block *sb = dir->i_sb;
  2247. sdfat_timespec_t ts;
  2248. int err;
  2249. __lock_super(sb);
  2250. TMSG("%s entered\n", __func__);
  2251. ts = current_time(dir);
  2252. SDFAT_I(inode)->fid.size = i_size_read(inode);
  2253. err = fsapi_rmdir(dir, &(SDFAT_I(inode)->fid));
  2254. if (err)
  2255. goto out;
  2256. __lock_d_revalidate(dentry);
  2257. inode_inc_iversion(dir);
  2258. dir->i_mtime = dir->i_atime = ts;
  2259. if (IS_DIRSYNC(dir))
  2260. (void) sdfat_sync_inode(dir);
  2261. else
  2262. mark_inode_dirty(dir);
  2263. drop_nlink(dir);
  2264. clear_nlink(inode);
  2265. inode->i_mtime = inode->i_atime = ts;
  2266. sdfat_detach(inode);
  2267. dentry->d_time = (unsigned long)inode_peek_iversion(dir);
  2268. out:
  2269. __unlock_d_revalidate(dentry);
  2270. __unlock_super(sb);
  2271. TMSG("%s exited with err(%d)\n", __func__, err);
  2272. return err;
  2273. }
  2274. static int __sdfat_rename(struct inode *old_dir, struct dentry *old_dentry,
  2275. struct inode *new_dir, struct dentry *new_dentry)
  2276. {
  2277. struct inode *old_inode, *new_inode;
  2278. struct super_block *sb = old_dir->i_sb;
  2279. sdfat_timespec_t ts;
  2280. loff_t i_pos;
  2281. int err;
  2282. __lock_super(sb);
  2283. TMSG("%s entered\n", __func__);
  2284. old_inode = old_dentry->d_inode;
  2285. new_inode = new_dentry->d_inode;
  2286. ts = current_time(old_inode);
  2287. SDFAT_I(old_inode)->fid.size = i_size_read(old_inode);
  2288. __cancel_dfr_work(old_inode, 0, 1, __func__);
  2289. err = fsapi_rename(old_dir, &(SDFAT_I(old_inode)->fid), new_dir, new_dentry);
  2290. if (err)
  2291. goto out;
  2292. __lock_d_revalidate(old_dentry);
  2293. __lock_d_revalidate(new_dentry);
  2294. inode_inc_iversion(new_dir);
  2295. new_dir->i_ctime = new_dir->i_mtime = new_dir->i_atime = ts;
  2296. if (IS_DIRSYNC(new_dir))
  2297. (void) sdfat_sync_inode(new_dir);
  2298. else
  2299. mark_inode_dirty(new_dir);
  2300. i_pos = sdfat_make_i_pos(&(SDFAT_I(old_inode)->fid));
  2301. sdfat_detach(old_inode);
  2302. sdfat_attach(old_inode, i_pos);
  2303. if (IS_DIRSYNC(new_dir))
  2304. (void) sdfat_sync_inode(old_inode);
  2305. else
  2306. mark_inode_dirty(old_inode);
  2307. if ((S_ISDIR(old_inode->i_mode)) && (old_dir != new_dir)) {
  2308. drop_nlink(old_dir);
  2309. if (!new_inode)
  2310. inc_nlink(new_dir);
  2311. }
  2312. inode_inc_iversion(old_dir);
  2313. old_dir->i_ctime = old_dir->i_mtime = ts;
  2314. if (IS_DIRSYNC(old_dir))
  2315. (void) sdfat_sync_inode(old_dir);
  2316. else
  2317. mark_inode_dirty(old_dir);
  2318. if (new_inode) {
  2319. sdfat_detach(new_inode);
  2320. /* skip drop_nlink if new_inode already has been dropped */
  2321. if (new_inode->i_nlink) {
  2322. drop_nlink(new_inode);
  2323. if (S_ISDIR(new_inode->i_mode))
  2324. drop_nlink(new_inode);
  2325. } else {
  2326. EMSG("%s : abnormal access to an inode dropped\n",
  2327. __func__);
  2328. WARN_ON(new_inode->i_nlink == 0);
  2329. }
  2330. new_inode->i_ctime = ts;
  2331. #if 0
  2332. (void) sdfat_sync_inode(new_inode);
  2333. #endif
  2334. }
  2335. out:
  2336. __unlock_d_revalidate(old_dentry);
  2337. __unlock_d_revalidate(new_dentry);
  2338. __unlock_super(sb);
  2339. TMSG("%s exited with err(%d)\n", __func__, err);
  2340. return err;
  2341. }
  2342. static int sdfat_cont_expand(struct inode *inode, loff_t size)
  2343. {
  2344. struct address_space *mapping = inode->i_mapping;
  2345. loff_t start = i_size_read(inode), count = size - i_size_read(inode);
  2346. int err, err2;
  2347. err = generic_cont_expand_simple(inode, size);
  2348. if (err)
  2349. return err;
  2350. inode->i_ctime = inode->i_mtime = current_time(inode);
  2351. mark_inode_dirty(inode);
  2352. if (!IS_SYNC(inode))
  2353. return 0;
  2354. err = filemap_fdatawrite_range(mapping, start, start + count - 1);
  2355. err2 = sync_mapping_buffers(mapping);
  2356. err = (err)?(err):(err2);
  2357. err2 = write_inode_now(inode, 1);
  2358. err = (err)?(err):(err2);
  2359. if (err)
  2360. return err;
  2361. return filemap_fdatawait_range(mapping, start, start + count - 1);
  2362. }
  2363. static int sdfat_allow_set_time(struct sdfat_sb_info *sbi, struct inode *inode)
  2364. {
  2365. mode_t allow_utime = sbi->options.allow_utime;
  2366. if (!uid_eq(current_fsuid(), inode->i_uid)) {
  2367. if (in_group_p(inode->i_gid))
  2368. allow_utime >>= 3;
  2369. if (allow_utime & MAY_WRITE)
  2370. return 1;
  2371. }
  2372. /* use a default check */
  2373. return 0;
  2374. }
  2375. static int sdfat_sanitize_mode(const struct sdfat_sb_info *sbi,
  2376. struct inode *inode, umode_t *mode_ptr)
  2377. {
  2378. mode_t i_mode, mask, perm;
  2379. i_mode = inode->i_mode;
  2380. if (S_ISREG(i_mode) || S_ISLNK(i_mode))
  2381. mask = sbi->options.fs_fmask;
  2382. else
  2383. mask = sbi->options.fs_dmask;
  2384. perm = *mode_ptr & ~(S_IFMT | mask);
  2385. /* Of the r and x bits, all (subject to umask) must be present.*/
  2386. if ((perm & (S_IRUGO | S_IXUGO)) != (i_mode & (S_IRUGO | S_IXUGO)))
  2387. return -EPERM;
  2388. if (sdfat_mode_can_hold_ro(inode)) {
  2389. /* Of the w bits, either all (subject to umask) or none must be present. */
  2390. if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
  2391. return -EPERM;
  2392. } else {
  2393. /* If sdfat_mode_can_hold_ro(inode) is false, can't change w bits. */
  2394. if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
  2395. return -EPERM;
  2396. }
  2397. *mode_ptr &= S_IFMT | perm;
  2398. return 0;
  2399. }
  2400. /*
  2401. * sdfat_block_truncate_page() zeroes out a mapping from file offset `from'
  2402. * up to the end of the block which corresponds to `from'.
  2403. * This is required during truncate to physically zeroout the tail end
  2404. * of that block so it doesn't yield old data if the file is later grown.
  2405. * Also, avoid causing failure from fsx for cases of "data past EOF"
  2406. */
  2407. static int sdfat_block_truncate_page(struct inode *inode, loff_t from)
  2408. {
  2409. return block_truncate_page(inode->i_mapping, from, sdfat_get_block);
  2410. }
  2411. static int sdfat_setattr(struct dentry *dentry, struct iattr *attr)
  2412. {
  2413. struct sdfat_sb_info *sbi = SDFAT_SB(dentry->d_sb);
  2414. struct inode *inode = dentry->d_inode;
  2415. unsigned int ia_valid;
  2416. int error;
  2417. loff_t old_size;
  2418. TMSG("%s entered\n", __func__);
  2419. if ((attr->ia_valid & ATTR_SIZE)
  2420. && (attr->ia_size > i_size_read(inode))) {
  2421. error = sdfat_cont_expand(inode, attr->ia_size);
  2422. if (error || attr->ia_valid == ATTR_SIZE)
  2423. goto out;
  2424. attr->ia_valid &= ~ATTR_SIZE;
  2425. }
  2426. /* Check for setting the inode time. */
  2427. ia_valid = attr->ia_valid;
  2428. if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET))
  2429. && sdfat_allow_set_time(sbi, inode)) {
  2430. attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET);
  2431. }
  2432. error = setattr_prepare(dentry, attr);
  2433. attr->ia_valid = ia_valid;
  2434. if (error)
  2435. goto out;
  2436. if (((attr->ia_valid & ATTR_UID) &&
  2437. (!uid_eq(attr->ia_uid, sbi->options.fs_uid))) ||
  2438. ((attr->ia_valid & ATTR_GID) &&
  2439. (!gid_eq(attr->ia_gid, sbi->options.fs_gid))) ||
  2440. ((attr->ia_valid & ATTR_MODE) &&
  2441. (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | S_IRWXUGO)))) {
  2442. error = -EPERM;
  2443. goto out;
  2444. }
  2445. /*
  2446. * We don't return -EPERM here. Yes, strange, but this is too
  2447. * old behavior.
  2448. */
  2449. if (attr->ia_valid & ATTR_MODE) {
  2450. if (sdfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
  2451. attr->ia_valid &= ~ATTR_MODE;
  2452. }
  2453. SDFAT_I(inode)->fid.size = i_size_read(inode);
  2454. /* patch 1.2.0 : fixed the problem of size mismatch. */
  2455. if (attr->ia_valid & ATTR_SIZE) {
  2456. error = sdfat_block_truncate_page(inode, attr->ia_size);
  2457. if (error)
  2458. goto out;
  2459. old_size = i_size_read(inode);
  2460. /* TO CHECK evicting directory works correctly */
  2461. MMSG("%s: inode(%p) truncate size (%llu->%llu)\n", __func__,
  2462. inode, (u64)old_size, (u64)attr->ia_size);
  2463. __sdfat_do_truncate(inode, old_size, attr->ia_size);
  2464. }
  2465. setattr_copy(inode, attr);
  2466. mark_inode_dirty(inode);
  2467. out:
  2468. TMSG("%s exited with err(%d)\n", __func__, error);
  2469. return error;
  2470. }
  2471. static const struct inode_operations sdfat_dir_inode_operations = {
  2472. .create = sdfat_create,
  2473. .lookup = sdfat_lookup,
  2474. .unlink = sdfat_unlink,
  2475. .symlink = sdfat_symlink,
  2476. .mkdir = sdfat_mkdir,
  2477. .rmdir = sdfat_rmdir,
  2478. .rename = sdfat_rename,
  2479. .setattr = sdfat_setattr,
  2480. .getattr = sdfat_getattr,
  2481. #ifdef CONFIG_SDFAT_VIRTUAL_XATTR
  2482. .listxattr = sdfat_listxattr,
  2483. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0)
  2484. .setxattr = sdfat_setxattr,
  2485. .getxattr = sdfat_getxattr,
  2486. .removexattr = sdfat_removexattr,
  2487. #endif
  2488. #endif
  2489. };
  2490. /*======================================================================*/
  2491. /* File Operations */
  2492. /*======================================================================*/
  2493. static const struct inode_operations sdfat_symlink_inode_operations = {
  2494. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0)
  2495. .readlink = generic_readlink,
  2496. #endif
  2497. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0)
  2498. .get_link = sdfat_follow_link,
  2499. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 5, 0) */
  2500. .follow_link = sdfat_follow_link,
  2501. #endif
  2502. #ifdef CONFIG_SDFAT_VIRTUAL_XATTR
  2503. .listxattr = sdfat_listxattr,
  2504. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0)
  2505. .setxattr = sdfat_setxattr,
  2506. .getxattr = sdfat_getxattr,
  2507. .removexattr = sdfat_removexattr,
  2508. #endif
  2509. #endif
  2510. };
  2511. static int sdfat_file_release(struct inode *inode, struct file *filp)
  2512. {
  2513. struct super_block *sb = inode->i_sb;
  2514. /* Moved below code from sdfat_write_inode
  2515. * TO FIX size-mismatch problem.
  2516. */
  2517. /* FIXME : Added bug_on to confirm that there is no size mismatch */
  2518. sdfat_debug_bug_on(SDFAT_I(inode)->fid.size != i_size_read(inode));
  2519. SDFAT_I(inode)->fid.size = i_size_read(inode);
  2520. fsapi_sync_fs(sb, 0);
  2521. return 0;
  2522. }
  2523. static const struct file_operations sdfat_file_operations = {
  2524. .llseek = generic_file_llseek,
  2525. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)
  2526. .read_iter = generic_file_read_iter,
  2527. .write_iter = generic_file_write_iter,
  2528. #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
  2529. .read = new_sync_read,
  2530. .write = new_sync_write,
  2531. .read_iter = generic_file_read_iter,
  2532. .write_iter = generic_file_write_iter,
  2533. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 16, 0) */
  2534. .read = do_sync_read,
  2535. .write = do_sync_write,
  2536. .aio_read = generic_file_aio_read,
  2537. .aio_write = generic_file_aio_write,
  2538. #endif
  2539. .mmap = sdfat_file_mmap,
  2540. .release = sdfat_file_release,
  2541. .unlocked_ioctl = sdfat_generic_ioctl,
  2542. .fsync = sdfat_file_fsync,
  2543. .splice_read = generic_file_splice_read,
  2544. };
  2545. static const struct address_space_operations sdfat_da_aops;
  2546. static const struct address_space_operations sdfat_aops;
  2547. static void sdfat_truncate(struct inode *inode, loff_t old_size)
  2548. {
  2549. struct super_block *sb = inode->i_sb;
  2550. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  2551. FS_INFO_T *fsi = &(sbi->fsi);
  2552. unsigned int blocksize = 1 << inode->i_blkbits;
  2553. loff_t aligned_size;
  2554. int err;
  2555. __lock_super(sb);
  2556. if (SDFAT_I(inode)->fid.start_clu == 0) {
  2557. /* Stange statement:
  2558. * Empty start_clu != ~0 (not allocated)
  2559. */
  2560. sdfat_fs_error(sb, "tried to truncate zeroed cluster.");
  2561. goto out;
  2562. }
  2563. sdfat_debug_check_clusters(inode);
  2564. __cancel_dfr_work(inode, (loff_t)i_size_read(inode), (loff_t)old_size, __func__);
  2565. err = fsapi_truncate(inode, old_size, i_size_read(inode));
  2566. if (err)
  2567. goto out;
  2568. inode->i_ctime = inode->i_mtime = current_time(inode);
  2569. if (IS_DIRSYNC(inode))
  2570. (void) sdfat_sync_inode(inode);
  2571. else
  2572. mark_inode_dirty(inode);
  2573. // FIXME: 확인 요망
  2574. // inode->i_blocks = ((SDFAT_I(inode)->i_size_ondisk + (fsi->cluster_size - 1))
  2575. inode->i_blocks = ((i_size_read(inode) + (fsi->cluster_size - 1)) &
  2576. ~((loff_t)fsi->cluster_size - 1)) >> inode->i_blkbits;
  2577. out:
  2578. /*
  2579. * This protects against truncating a file bigger than it was then
  2580. * trying to write into the hole.
  2581. *
  2582. * comment by sh.hong:
  2583. * This seems to mean 'intra page/block' truncate and writing.
  2584. * I couldn't find a reason to change the values prior to fsapi_truncate
  2585. * Therefore, I switched the order of operations
  2586. * so that it's possible to utilize i_size_ondisk in fsapi_truncate
  2587. */
  2588. aligned_size = i_size_read(inode);
  2589. if (aligned_size & (blocksize - 1)) {
  2590. aligned_size |= (blocksize - 1);
  2591. aligned_size++;
  2592. }
  2593. if (SDFAT_I(inode)->i_size_ondisk > i_size_read(inode))
  2594. SDFAT_I(inode)->i_size_ondisk = aligned_size;
  2595. sdfat_debug_check_clusters(inode);
  2596. if (SDFAT_I(inode)->i_size_aligned > i_size_read(inode))
  2597. SDFAT_I(inode)->i_size_aligned = aligned_size;
  2598. /* After truncation :
  2599. * 1) Delayed allocation is OFF
  2600. * i_size = i_size_ondisk <= i_size_aligned
  2601. * (useless size var.)
  2602. * (block-aligned)
  2603. * 2) Delayed allocation is ON
  2604. * i_size = i_size_ondisk = i_size_aligned
  2605. * (will be block-aligned after write)
  2606. * or
  2607. * i_size_ondisk < i_size <= i_size_aligned (block_aligned)
  2608. * (will be block-aligned after write)
  2609. */
  2610. __unlock_super(sb);
  2611. }
  2612. static const struct inode_operations sdfat_file_inode_operations = {
  2613. .setattr = sdfat_setattr,
  2614. .getattr = sdfat_getattr,
  2615. #ifdef CONFIG_SDFAT_VIRTUAL_XATTR
  2616. .listxattr = sdfat_listxattr,
  2617. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0)
  2618. .setxattr = sdfat_setxattr,
  2619. .getxattr = sdfat_getxattr,
  2620. .removexattr = sdfat_removexattr,
  2621. #endif
  2622. #endif
  2623. };
  2624. /*======================================================================*/
  2625. /* Address Space Operations */
  2626. /*======================================================================*/
  2627. /* 2-level option flag */
  2628. #define BMAP_NOT_CREATE 0
  2629. #define BMAP_ADD_BLOCK 1
  2630. #define BMAP_ADD_CLUSTER 2
  2631. #define BLOCK_ADDED(bmap_ops) (bmap_ops)
  2632. static int sdfat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
  2633. unsigned long *mapped_blocks, int *create)
  2634. {
  2635. struct super_block *sb = inode->i_sb;
  2636. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  2637. FS_INFO_T *fsi = &(sbi->fsi);
  2638. const unsigned long blocksize = sb->s_blocksize;
  2639. const unsigned char blocksize_bits = sb->s_blocksize_bits;
  2640. sector_t last_block;
  2641. unsigned int cluster, clu_offset, sec_offset;
  2642. int err = 0;
  2643. *phys = 0;
  2644. *mapped_blocks = 0;
  2645. /* core code should handle EIO */
  2646. #if 0
  2647. if (fsi->prev_eio && BLOCK_ADDED(*create))
  2648. return -EIO;
  2649. #endif
  2650. if (((fsi->vol_type == FAT12) || (fsi->vol_type == FAT16)) &&
  2651. (inode->i_ino == SDFAT_ROOT_INO)) {
  2652. if (sector < (fsi->dentries_in_root >>
  2653. (sb->s_blocksize_bits - DENTRY_SIZE_BITS))) {
  2654. *phys = sector + fsi->root_start_sector;
  2655. *mapped_blocks = 1;
  2656. }
  2657. return 0;
  2658. }
  2659. last_block = (i_size_read(inode) + (blocksize - 1)) >> blocksize_bits;
  2660. if ((sector >= last_block) && (*create == BMAP_NOT_CREATE))
  2661. return 0;
  2662. /* Is this block already allocated? */
  2663. clu_offset = sector >> fsi->sect_per_clus_bits; /* cluster offset */
  2664. SDFAT_I(inode)->fid.size = i_size_read(inode);
  2665. if (unlikely(__check_dfr_on(inode,
  2666. (loff_t)((loff_t)clu_offset << fsi->cluster_size_bits),
  2667. (loff_t)((loff_t)(clu_offset + 1) << fsi->cluster_size_bits),
  2668. __func__))) {
  2669. err = __do_dfr_map_cluster(inode, clu_offset, &cluster);
  2670. } else {
  2671. if (*create & BMAP_ADD_CLUSTER)
  2672. err = fsapi_map_clus(inode, clu_offset, &cluster, 1);
  2673. else
  2674. err = fsapi_map_clus(inode, clu_offset, &cluster, ALLOC_NOWHERE);
  2675. }
  2676. if (err) {
  2677. if (err != -ENOSPC)
  2678. return -EIO;
  2679. return err;
  2680. }
  2681. /* FOR BIGDATA */
  2682. sdfat_statistics_set_rw(SDFAT_I(inode)->fid.flags,
  2683. clu_offset, *create & BMAP_ADD_CLUSTER);
  2684. if (!IS_CLUS_EOF(cluster)) {
  2685. /* sector offset in cluster */
  2686. sec_offset = sector & (fsi->sect_per_clus - 1);
  2687. *phys = CLUS_TO_SECT(fsi, cluster) + sec_offset;
  2688. *mapped_blocks = fsi->sect_per_clus - sec_offset;
  2689. }
  2690. #if 0
  2691. else {
  2692. /* Debug purpose (new clu needed) */
  2693. ASSERT((*create & BMAP_ADD_CLUSTER) == 0);
  2694. ASSERT(sector >= last_block);
  2695. }
  2696. #endif
  2697. if (sector < last_block)
  2698. *create = BMAP_NOT_CREATE;
  2699. #if 0
  2700. else if (sector >= last_block)
  2701. *create = non-zero;
  2702. if (iblock <= last mapped-block)
  2703. *phys != 0
  2704. *create = BMAP_NOT_CREATE
  2705. else if (iblock <= last cluster)
  2706. *phys != 0
  2707. *create = non-zero
  2708. #endif
  2709. return 0;
  2710. }
  2711. static int sdfat_da_prep_block(struct inode *inode, sector_t iblock,
  2712. struct buffer_head *bh_result, int create)
  2713. {
  2714. struct super_block *sb = inode->i_sb;
  2715. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  2716. FS_INFO_T *fsi = &(sbi->fsi);
  2717. unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
  2718. unsigned long mapped_blocks;
  2719. sector_t phys;
  2720. loff_t pos;
  2721. int sec_offset;
  2722. int bmap_create = create ? BMAP_ADD_BLOCK : BMAP_NOT_CREATE;
  2723. int err = 0;
  2724. __lock_super(sb);
  2725. /* FAT32 only */
  2726. ASSERT(fsi->vol_type == FAT32);
  2727. err = sdfat_bmap(inode, iblock, &phys, &mapped_blocks, &bmap_create);
  2728. if (err) {
  2729. if (err != -ENOSPC)
  2730. sdfat_fs_error_ratelimit(sb, "%s: failed to bmap "
  2731. "(iblock:%u, err:%d)", __func__,
  2732. (u32)iblock, err);
  2733. goto unlock_ret;
  2734. }
  2735. sec_offset = iblock & (fsi->sect_per_clus - 1);
  2736. if (phys) {
  2737. /* the block in in the mapped cluster boundary */
  2738. max_blocks = min(mapped_blocks, max_blocks);
  2739. map_bh(bh_result, sb, phys);
  2740. BUG_ON(BLOCK_ADDED(bmap_create) && (sec_offset == 0));
  2741. } else if (create == 1) {
  2742. /* Not exist: new cluster needed */
  2743. if (!BLOCK_ADDED(bmap_create)) {
  2744. sector_t last_block;
  2745. last_block = (i_size_read(inode) + (sb->s_blocksize - 1))
  2746. >> sb->s_blocksize_bits;
  2747. sdfat_fs_error(sb, "%s: new cluster need, but "
  2748. "bmap_create == BMAP_NOT_CREATE(iblock:%lld, "
  2749. "last_block:%lld)", __func__,
  2750. (s64)iblock, (s64)last_block);
  2751. err = -EIO;
  2752. goto unlock_ret;
  2753. }
  2754. // Reserved Cluster (only if iblock is the first sector in a clu)
  2755. if (sec_offset == 0) {
  2756. err = fsapi_reserve_clus(inode);
  2757. if (err) {
  2758. if (err != -ENOSPC)
  2759. sdfat_fs_error_ratelimit(sb,
  2760. "%s: failed to bmap "
  2761. "(iblock:%u, err:%d)", __func__,
  2762. (u32)iblock, err);
  2763. goto unlock_ret;
  2764. }
  2765. }
  2766. // Delayed mapping
  2767. map_bh(bh_result, sb, ~((sector_t) 0xffff));
  2768. set_buffer_new(bh_result);
  2769. set_buffer_delay(bh_result);
  2770. } else {
  2771. /* get_block on non-existing addr. with create==0 */
  2772. /*
  2773. * CHECKME:
  2774. * i_size_aligned 보다 작으면 delay 매핑을 일단
  2775. * 켜줘야되는 게 아닌가?
  2776. * - 0-fill 을 항상 하기에, FAT 에서는 문제 없음.
  2777. * 중간에 영역이 꽉 찼으면, 디스크에 내려가지 않고는
  2778. * invalidate 될 일이 없음
  2779. */
  2780. goto unlock_ret;
  2781. }
  2782. /* Newly added blocks */
  2783. if (BLOCK_ADDED(bmap_create)) {
  2784. set_buffer_new(bh_result);
  2785. SDFAT_I(inode)->i_size_aligned += max_blocks << sb->s_blocksize_bits;
  2786. if (phys) {
  2787. /* i_size_ondisk changes if a block added in the existing cluster */
  2788. #define num_clusters(value) ((value) ? (s32)((value - 1) >> fsi->cluster_size_bits) + 1 : 0)
  2789. /* FOR GRACEFUL ERROR HANDLING */
  2790. if (num_clusters(SDFAT_I(inode)->i_size_aligned) !=
  2791. num_clusters(SDFAT_I(inode)->i_size_ondisk)) {
  2792. EMSG("%s: inode(%p) invalid size (create(%d) "
  2793. "bmap_create(%d) phys(%lld) aligned(%lld) "
  2794. "on_disk(%lld) iblock(%u) sec_off(%d))\n",
  2795. __func__, inode, create, bmap_create, (s64)phys,
  2796. (s64)SDFAT_I(inode)->i_size_aligned,
  2797. (s64)SDFAT_I(inode)->i_size_ondisk,
  2798. (u32)iblock,
  2799. (s32)sec_offset);
  2800. sdfat_debug_bug_on(1);
  2801. }
  2802. SDFAT_I(inode)->i_size_ondisk = SDFAT_I(inode)->i_size_aligned;
  2803. }
  2804. pos = (iblock + 1) << sb->s_blocksize_bits;
  2805. /* Debug purpose - defensive coding */
  2806. ASSERT(SDFAT_I(inode)->i_size_aligned == pos);
  2807. if (SDFAT_I(inode)->i_size_aligned < pos)
  2808. SDFAT_I(inode)->i_size_aligned = pos;
  2809. /* Debug end */
  2810. #ifdef CONFIG_SDFAT_TRACE_IO
  2811. /* New page added (ASSERTION: 8 blocks per page) */
  2812. if ((sec_offset & 7) == 0)
  2813. sbi->stat_n_pages_added++;
  2814. #endif
  2815. }
  2816. /* FOR GRACEFUL ERROR HANDLING */
  2817. if (i_size_read(inode) > SDFAT_I(inode)->i_size_aligned) {
  2818. sdfat_fs_error_ratelimit(sb, "%s: invalid size (inode(%p), "
  2819. "size(%llu) > aligned(%llu)\n", __func__, inode,
  2820. i_size_read(inode), SDFAT_I(inode)->i_size_aligned);
  2821. sdfat_debug_bug_on(1);
  2822. }
  2823. bh_result->b_size = max_blocks << sb->s_blocksize_bits;
  2824. unlock_ret:
  2825. __unlock_super(sb);
  2826. return err;
  2827. }
  2828. static int sdfat_get_block(struct inode *inode, sector_t iblock,
  2829. struct buffer_head *bh_result, int create)
  2830. {
  2831. struct super_block *sb = inode->i_sb;
  2832. unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
  2833. int err = 0;
  2834. unsigned long mapped_blocks;
  2835. sector_t phys;
  2836. loff_t pos;
  2837. int bmap_create = create ? BMAP_ADD_CLUSTER : BMAP_NOT_CREATE;
  2838. __lock_super(sb);
  2839. err = sdfat_bmap(inode, iblock, &phys, &mapped_blocks, &bmap_create);
  2840. if (err) {
  2841. if (err != -ENOSPC)
  2842. sdfat_fs_error_ratelimit(sb, "%s: failed to bmap "
  2843. "(inode:%p iblock:%u, err:%d)",
  2844. __func__, inode, (u32)iblock, err);
  2845. goto unlock_ret;
  2846. }
  2847. if (phys) {
  2848. max_blocks = min(mapped_blocks, max_blocks);
  2849. /* Treat newly added block / cluster */
  2850. if (BLOCK_ADDED(bmap_create) || buffer_delay(bh_result)) {
  2851. /* Update i_size_ondisk */
  2852. pos = (iblock + 1) << sb->s_blocksize_bits;
  2853. if (SDFAT_I(inode)->i_size_ondisk < pos) {
  2854. /* Debug purpose */
  2855. if ((pos - SDFAT_I(inode)->i_size_ondisk) > bh_result->b_size) {
  2856. /* This never happens without DA */
  2857. MMSG("Jumping get_block\n");
  2858. }
  2859. SDFAT_I(inode)->i_size_ondisk = pos;
  2860. sdfat_debug_check_clusters(inode);
  2861. }
  2862. if (BLOCK_ADDED(bmap_create)) {
  2863. /* Old way (w/o DA)
  2864. * create == 1 only if iblock > i_size
  2865. * (in block unit)
  2866. */
  2867. /* 20130723 CHECK
  2868. * Truncate와 동시에 발생할 경우,
  2869. * i_size < (i_block 위치) 면서 buffer_delay()가
  2870. * 켜져있을 수 있다.
  2871. *
  2872. * 기존에 할당된 영역을 다시 쓸 뿐이므로 큰 문제
  2873. * 없지만, 그 경우, 미리 i_size_aligned 가 확장된
  2874. * 영역이어야 한다.
  2875. */
  2876. /* FOR GRACEFUL ERROR HANDLING */
  2877. if (buffer_delay(bh_result) &&
  2878. (pos > SDFAT_I(inode)->i_size_aligned)) {
  2879. sdfat_fs_error(sb, "requested for bmap "
  2880. "out of range(pos:(%llu)>i_size_aligned(%llu)\n",
  2881. pos, SDFAT_I(inode)->i_size_aligned);
  2882. sdfat_debug_bug_on(1);
  2883. err = -EIO;
  2884. goto unlock_ret;
  2885. }
  2886. set_buffer_new(bh_result);
  2887. /*
  2888. * adjust i_size_aligned if i_size_ondisk is
  2889. * bigger than it. (i.e. non-DA)
  2890. */
  2891. if (SDFAT_I(inode)->i_size_ondisk >
  2892. SDFAT_I(inode)->i_size_aligned) {
  2893. SDFAT_I(inode)->i_size_aligned =
  2894. SDFAT_I(inode)->i_size_ondisk;
  2895. }
  2896. }
  2897. if (buffer_delay(bh_result))
  2898. clear_buffer_delay(bh_result);
  2899. #if 0
  2900. /* Debug purpose */
  2901. if (SDFAT_I(inode)->i_size_ondisk >
  2902. SDFAT_I(inode)->i_size_aligned) {
  2903. /* Only after truncate
  2904. * and the two size variables should indicate
  2905. * same i_block
  2906. */
  2907. unsigned int blocksize = 1 << inode->i_blkbits;
  2908. BUG_ON(SDFAT_I(inode)->i_size_ondisk -
  2909. SDFAT_I(inode)->i_size_aligned >= blocksize);
  2910. }
  2911. #endif
  2912. }
  2913. map_bh(bh_result, sb, phys);
  2914. }
  2915. bh_result->b_size = max_blocks << sb->s_blocksize_bits;
  2916. unlock_ret:
  2917. __unlock_super(sb);
  2918. return err;
  2919. }
  2920. static int sdfat_readpage(struct file *file, struct page *page)
  2921. {
  2922. int ret;
  2923. ret = mpage_readpage(page, sdfat_get_block);
  2924. return ret;
  2925. }
  2926. static int sdfat_readpages(struct file *file, struct address_space *mapping,
  2927. struct list_head *pages, unsigned int nr_pages)
  2928. {
  2929. int ret;
  2930. ret = mpage_readpages(mapping, pages, nr_pages, sdfat_get_block);
  2931. return ret;
  2932. }
  2933. static inline void sdfat_submit_fullpage_bio(struct block_device *bdev,
  2934. sector_t sector, unsigned int length,
  2935. struct page *page, struct writeback_control *wbc)
  2936. {
  2937. /* Single page bio submit */
  2938. struct bio *bio;
  2939. BUG_ON((length > PAGE_SIZE) || (length == 0));
  2940. /*
  2941. * If __GFP_WAIT is set, then bio_alloc will always be able to allocate
  2942. * a bio. This is due to the mempool guarantees. To make this work, callers
  2943. * must never allocate more than 1 bio at a time from this pool.
  2944. *
  2945. * #define GFP_NOIO (__GFP_WAIT)
  2946. */
  2947. bio = bio_alloc(GFP_NOIO, 1);
  2948. bio_set_dev(bio, bdev);
  2949. bio->bi_vcnt = 1;
  2950. bio->bi_io_vec[0].bv_page = page; /* Inline vec */
  2951. bio->bi_io_vec[0].bv_len = length; /* PAGE_SIZE */
  2952. bio->bi_io_vec[0].bv_offset = 0;
  2953. __sdfat_set_bio_iterate(bio, sector, length, 0, 0);
  2954. bio->bi_end_io = sdfat_writepage_end_io;
  2955. __sdfat_submit_bio_write(bio, wbc);
  2956. }
  2957. static int sdfat_writepage(struct page *page, struct writeback_control *wbc)
  2958. {
  2959. struct inode * const inode = page->mapping->host;
  2960. struct super_block *sb = inode->i_sb;
  2961. loff_t i_size = i_size_read(inode);
  2962. const pgoff_t end_index = i_size >> PAGE_SHIFT;
  2963. const unsigned int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
  2964. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  2965. struct buffer_head *bh, *head;
  2966. sector_t block, block_0, last_phys;
  2967. int ret;
  2968. unsigned int nr_blocks_towrite = blocks_per_page;
  2969. /* Don't distinguish 0-filled/clean block.
  2970. * Just write back the whole page
  2971. */
  2972. if (fsi->cluster_size < PAGE_SIZE)
  2973. goto confused;
  2974. if (!PageUptodate(page)) {
  2975. MMSG("%s: Not up-to-date page -> block_write_full_page\n",
  2976. __func__);
  2977. goto confused;
  2978. }
  2979. if (page->index >= end_index) {
  2980. /* last page or outside i_size */
  2981. unsigned int offset = i_size & (PAGE_SIZE-1);
  2982. /* If a truncation is in progress */
  2983. if (page->index > end_index || !offset)
  2984. goto confused;
  2985. /* 0-fill after i_size */
  2986. zero_user_segment(page, offset, PAGE_SIZE);
  2987. }
  2988. if (!page_has_buffers(page)) {
  2989. MMSG("WP: No buffers -> block_write_full_page\n");
  2990. goto confused;
  2991. }
  2992. block = (sector_t)page->index << (PAGE_SHIFT - inode->i_blkbits);
  2993. block_0 = block; /* first block */
  2994. head = page_buffers(page);
  2995. bh = head;
  2996. last_phys = 0;
  2997. do {
  2998. BUG_ON(buffer_locked(bh));
  2999. if (!buffer_dirty(bh) || !buffer_uptodate(bh)) {
  3000. if (nr_blocks_towrite == blocks_per_page)
  3001. nr_blocks_towrite = (unsigned int) (block - block_0);
  3002. BUG_ON(nr_blocks_towrite >= blocks_per_page);
  3003. // !uptodate but dirty??
  3004. if (buffer_dirty(bh))
  3005. goto confused;
  3006. // Nothing to writeback in this block
  3007. bh = bh->b_this_page;
  3008. block++;
  3009. continue;
  3010. }
  3011. if (nr_blocks_towrite != blocks_per_page)
  3012. // Dirty -> Non-dirty -> Dirty again case
  3013. goto confused;
  3014. /* Map if needed */
  3015. if (!buffer_mapped(bh) || buffer_delay(bh)) {
  3016. BUG_ON(bh->b_size != (1 << (inode->i_blkbits)));
  3017. ret = sdfat_get_block(inode, block, bh, 1);
  3018. if (ret)
  3019. goto confused;
  3020. if (buffer_new(bh)) {
  3021. clear_buffer_new(bh);
  3022. __sdfat_clean_bdev_aliases(bh->b_bdev, bh->b_blocknr);
  3023. }
  3024. }
  3025. /* continuity check */
  3026. if (((last_phys + 1) != bh->b_blocknr) && (last_phys != 0)) {
  3027. DMSG("Non-contiguous block mapping in single page");
  3028. goto confused;
  3029. }
  3030. last_phys = bh->b_blocknr;
  3031. bh = bh->b_this_page;
  3032. block++;
  3033. } while (bh != head);
  3034. if (nr_blocks_towrite == 0) {
  3035. DMSG("Page dirty but no dirty bh? alloc_208\n");
  3036. goto confused;
  3037. }
  3038. /* Write-back */
  3039. do {
  3040. clear_buffer_dirty(bh);
  3041. bh = bh->b_this_page;
  3042. } while (bh != head);
  3043. BUG_ON(PageWriteback(page));
  3044. set_page_writeback(page);
  3045. /**
  3046. * Turn off MAPPED flag in victim's bh if defrag on.
  3047. * Another write_begin can starts after get_block for defrag victims called.
  3048. * In this case, write_begin calls get_block and get original block number
  3049. * and previous defrag will be canceled.
  3050. */
  3051. if (unlikely(__check_dfr_on(inode,
  3052. (loff_t)(page->index << PAGE_SHIFT),
  3053. (loff_t)((page->index + 1) << PAGE_SHIFT),
  3054. __func__))) {
  3055. do {
  3056. clear_buffer_mapped(bh);
  3057. bh = bh->b_this_page;
  3058. } while (bh != head);
  3059. }
  3060. // Trace # of pages queued (Approx.)
  3061. atomic_inc(&SDFAT_SB(sb)->stat_n_pages_queued);
  3062. sdfat_submit_fullpage_bio(head->b_bdev,
  3063. head->b_blocknr << (sb->s_blocksize_bits - SECTOR_SIZE_BITS),
  3064. nr_blocks_towrite << inode->i_blkbits,
  3065. page, wbc);
  3066. unlock_page(page);
  3067. return 0;
  3068. confused:
  3069. #ifdef CONFIG_SDFAT_TRACE_IO
  3070. SDFAT_SB(sb)->stat_n_pages_confused++;
  3071. #endif
  3072. ret = block_write_full_page(page, sdfat_get_block, wbc);
  3073. return ret;
  3074. }
  3075. static int sdfat_da_writepages(struct address_space *mapping,
  3076. struct writeback_control *wbc)
  3077. {
  3078. MMSG("%s(inode:%p) with nr_to_write = 0x%08lx "
  3079. "(ku %d, bg %d, tag %d, rc %d )\n",
  3080. __func__, mapping->host, wbc->nr_to_write,
  3081. wbc->for_kupdate, wbc->for_background, wbc->tagged_writepages,
  3082. wbc->for_reclaim);
  3083. ASSERT(mapping->a_ops == &sdfat_da_aops);
  3084. #ifdef CONFIG_SDFAT_ALIGNED_MPAGE_WRITE
  3085. if (SDFAT_SB(mapping->host->i_sb)->options.adj_req)
  3086. return sdfat_mpage_writepages(mapping, wbc, sdfat_get_block);
  3087. #endif
  3088. return generic_writepages(mapping, wbc);
  3089. }
  3090. static int sdfat_writepages(struct address_space *mapping,
  3091. struct writeback_control *wbc)
  3092. {
  3093. MMSG("%s(inode:%p) with nr_to_write = 0x%08lx "
  3094. "(ku %d, bg %d, tag %d, rc %d )\n",
  3095. __func__, mapping->host, wbc->nr_to_write,
  3096. wbc->for_kupdate, wbc->for_background, wbc->tagged_writepages,
  3097. wbc->for_reclaim);
  3098. ASSERT(mapping->a_ops == &sdfat_aops);
  3099. #ifdef CONFIG_SDFAT_ALIGNED_MPAGE_WRITE
  3100. if (SDFAT_SB(mapping->host->i_sb)->options.adj_req)
  3101. return sdfat_mpage_writepages(mapping, wbc, sdfat_get_block);
  3102. #endif
  3103. return mpage_writepages(mapping, wbc, sdfat_get_block);
  3104. }
  3105. static void sdfat_write_failed(struct address_space *mapping, loff_t to)
  3106. {
  3107. struct inode *inode = mapping->host;
  3108. if (to > i_size_read(inode)) {
  3109. __sdfat_truncate_pagecache(inode, to, i_size_read(inode));
  3110. sdfat_truncate(inode, SDFAT_I(inode)->i_size_aligned);
  3111. }
  3112. }
  3113. static int sdfat_check_writable(struct super_block *sb)
  3114. {
  3115. if (fsapi_check_bdi_valid(sb))
  3116. return -EIO;
  3117. if (sb_rdonly(sb))
  3118. return -EROFS;
  3119. return 0;
  3120. }
  3121. static int __sdfat_write_begin(struct file *file, struct address_space *mapping,
  3122. loff_t pos, unsigned int len,
  3123. unsigned int flags, struct page **pagep,
  3124. void **fsdata, get_block_t *get_block,
  3125. loff_t *bytes, const char *fname)
  3126. {
  3127. struct super_block *sb = mapping->host->i_sb;
  3128. int ret;
  3129. __cancel_dfr_work(mapping->host, pos, (loff_t)(pos + len), fname);
  3130. ret = sdfat_check_writable(sb);
  3131. if (unlikely(ret < 0))
  3132. return ret;
  3133. *pagep = NULL;
  3134. ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  3135. get_block, bytes);
  3136. if (ret < 0)
  3137. sdfat_write_failed(mapping, pos+len);
  3138. return ret;
  3139. }
  3140. static int sdfat_da_write_begin(struct file *file, struct address_space *mapping,
  3141. loff_t pos, unsigned int len, unsigned int flags,
  3142. struct page **pagep, void **fsdata)
  3143. {
  3144. return __sdfat_write_begin(file, mapping, pos, len, flags,
  3145. pagep, fsdata, sdfat_da_prep_block,
  3146. &SDFAT_I(mapping->host)->i_size_aligned,
  3147. __func__);
  3148. }
  3149. static int sdfat_write_begin(struct file *file, struct address_space *mapping,
  3150. loff_t pos, unsigned int len, unsigned int flags,
  3151. struct page **pagep, void **fsdata)
  3152. {
  3153. return __sdfat_write_begin(file, mapping, pos, len, flags,
  3154. pagep, fsdata, sdfat_get_block,
  3155. &SDFAT_I(mapping->host)->i_size_ondisk,
  3156. __func__);
  3157. }
  3158. static int sdfat_write_end(struct file *file, struct address_space *mapping,
  3159. loff_t pos, unsigned int len, unsigned int copied,
  3160. struct page *pagep, void *fsdata)
  3161. {
  3162. struct inode *inode = mapping->host;
  3163. FILE_ID_T *fid = &(SDFAT_I(inode)->fid);
  3164. int err;
  3165. err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
  3166. /* FOR GRACEFUL ERROR HANDLING */
  3167. if (SDFAT_I(inode)->i_size_aligned < i_size_read(inode)) {
  3168. sdfat_fs_error(inode->i_sb, "invalid size(size(%llu) "
  3169. "> aligned(%llu)\n", i_size_read(inode),
  3170. SDFAT_I(inode)->i_size_aligned);
  3171. sdfat_debug_bug_on(1);
  3172. }
  3173. if (err < len)
  3174. sdfat_write_failed(mapping, pos+len);
  3175. if (!(err < 0) && !(fid->attr & ATTR_ARCHIVE)) {
  3176. inode->i_mtime = inode->i_ctime = current_time(inode);
  3177. fid->attr |= ATTR_ARCHIVE;
  3178. mark_inode_dirty(inode);
  3179. }
  3180. return err;
  3181. }
  3182. static inline ssize_t __sdfat_direct_IO(int rw, struct kiocb *iocb,
  3183. struct inode *inode, void *iov_u, loff_t offset,
  3184. loff_t count, unsigned long nr_segs)
  3185. {
  3186. struct address_space *mapping = inode->i_mapping;
  3187. loff_t size = offset + count;
  3188. ssize_t ret;
  3189. if (rw == WRITE) {
  3190. /*
  3191. * FIXME: blockdev_direct_IO() doesn't use ->write_begin(),
  3192. * so we need to update the ->i_size_aligned to block boundary.
  3193. *
  3194. * But we must fill the remaining area or hole by nul for
  3195. * updating ->i_size_aligned
  3196. *
  3197. * Return 0, and fallback to normal buffered write.
  3198. */
  3199. if (SDFAT_I(inode)->i_size_aligned < size)
  3200. return 0;
  3201. }
  3202. /*
  3203. * sdFAT need to use the DIO_LOCKING for avoiding the race
  3204. * condition of sdfat_get_block() and ->truncate().
  3205. */
  3206. ret = __sdfat_blkdev_direct_IO(rw, iocb, inode, iov_u, offset, nr_segs);
  3207. if (ret < 0 && (rw & WRITE))
  3208. sdfat_write_failed(mapping, size);
  3209. return ret;
  3210. }
  3211. static const struct address_space_operations sdfat_aops = {
  3212. .readpage = sdfat_readpage,
  3213. .readpages = sdfat_readpages,
  3214. .writepage = sdfat_writepage,
  3215. .writepages = sdfat_writepages,
  3216. .write_begin = sdfat_write_begin,
  3217. .write_end = sdfat_write_end,
  3218. .direct_IO = sdfat_direct_IO,
  3219. .bmap = sdfat_aop_bmap
  3220. };
  3221. static const struct address_space_operations sdfat_da_aops = {
  3222. .readpage = sdfat_readpage,
  3223. .readpages = sdfat_readpages,
  3224. .writepage = sdfat_writepage,
  3225. .writepages = sdfat_da_writepages,
  3226. .write_begin = sdfat_da_write_begin,
  3227. .write_end = sdfat_write_end,
  3228. .direct_IO = sdfat_direct_IO,
  3229. .bmap = sdfat_aop_bmap
  3230. };
  3231. /*======================================================================*/
  3232. /* Super Operations */
  3233. /*======================================================================*/
  3234. static inline unsigned long sdfat_hash(loff_t i_pos)
  3235. {
  3236. return hash_32(i_pos, SDFAT_HASH_BITS);
  3237. }
  3238. static void sdfat_attach(struct inode *inode, loff_t i_pos)
  3239. {
  3240. struct sdfat_sb_info *sbi = SDFAT_SB(inode->i_sb);
  3241. struct hlist_head *head = sbi->inode_hashtable + sdfat_hash(i_pos);
  3242. spin_lock(&sbi->inode_hash_lock);
  3243. SDFAT_I(inode)->i_pos = i_pos;
  3244. hlist_add_head(&SDFAT_I(inode)->i_hash_fat, head);
  3245. spin_unlock(&sbi->inode_hash_lock);
  3246. }
  3247. static void sdfat_detach(struct inode *inode)
  3248. {
  3249. struct sdfat_sb_info *sbi = SDFAT_SB(inode->i_sb);
  3250. spin_lock(&sbi->inode_hash_lock);
  3251. hlist_del_init(&SDFAT_I(inode)->i_hash_fat);
  3252. SDFAT_I(inode)->i_pos = 0;
  3253. spin_unlock(&sbi->inode_hash_lock);
  3254. }
  3255. /* doesn't deal with root inode */
  3256. static int sdfat_fill_inode(struct inode *inode, const FILE_ID_T *fid)
  3257. {
  3258. struct sdfat_sb_info *sbi = SDFAT_SB(inode->i_sb);
  3259. FS_INFO_T *fsi = &(sbi->fsi);
  3260. DIR_ENTRY_T info;
  3261. u64 size = fid->size;
  3262. memcpy(&(SDFAT_I(inode)->fid), fid, sizeof(FILE_ID_T));
  3263. SDFAT_I(inode)->i_pos = 0;
  3264. SDFAT_I(inode)->target = NULL;
  3265. inode->i_uid = sbi->options.fs_uid;
  3266. inode->i_gid = sbi->options.fs_gid;
  3267. inode_inc_iversion(inode);
  3268. inode->i_generation = get_seconds();
  3269. if (fsapi_read_inode(inode, &info) < 0) {
  3270. MMSG("%s: failed to read stat!\n", __func__);
  3271. return -EIO;
  3272. }
  3273. if (info.Attr & ATTR_SUBDIR) { /* directory */
  3274. inode->i_generation &= ~1;
  3275. inode->i_mode = sdfat_make_mode(sbi, info.Attr, S_IRWXUGO);
  3276. inode->i_op = &sdfat_dir_inode_operations;
  3277. inode->i_fop = &sdfat_dir_operations;
  3278. set_nlink(inode, info.NumSubdirs);
  3279. } else if (info.Attr & ATTR_SYMLINK) { /* symbolic link */
  3280. inode->i_op = &sdfat_symlink_inode_operations;
  3281. inode->i_generation |= 1;
  3282. inode->i_mode = sdfat_make_mode(sbi, info.Attr, S_IRWXUGO);
  3283. } else { /* regular file */
  3284. inode->i_generation |= 1;
  3285. inode->i_mode = sdfat_make_mode(sbi, info.Attr, S_IRWXUGO);
  3286. inode->i_op = &sdfat_file_inode_operations;
  3287. inode->i_fop = &sdfat_file_operations;
  3288. if (sbi->options.improved_allocation & SDFAT_ALLOC_DELAY)
  3289. inode->i_mapping->a_ops = &sdfat_da_aops;
  3290. else
  3291. inode->i_mapping->a_ops = &sdfat_aops;
  3292. inode->i_mapping->nrpages = 0;
  3293. }
  3294. /*
  3295. * Use fid->size instead of info.Size
  3296. * because info.Size means the value saved on disk
  3297. */
  3298. i_size_write(inode, size);
  3299. /* ondisk and aligned size should be aligned with block size */
  3300. if (size & (inode->i_sb->s_blocksize - 1)) {
  3301. size |= (inode->i_sb->s_blocksize - 1);
  3302. size++;
  3303. }
  3304. SDFAT_I(inode)->i_size_aligned = size;
  3305. SDFAT_I(inode)->i_size_ondisk = size;
  3306. sdfat_debug_check_clusters(inode);
  3307. sdfat_save_attr(inode, info.Attr);
  3308. inode->i_blocks = ((i_size_read(inode) + (fsi->cluster_size - 1))
  3309. & ~((loff_t)fsi->cluster_size - 1)) >> inode->i_blkbits;
  3310. sdfat_time_fat2unix(sbi, &inode->i_mtime, &info.ModifyTimestamp);
  3311. sdfat_time_fat2unix(sbi, &inode->i_ctime, &info.CreateTimestamp);
  3312. sdfat_time_fat2unix(sbi, &inode->i_atime, &info.AccessTimestamp);
  3313. __init_dfr_info(inode);
  3314. return 0;
  3315. }
  3316. static struct inode *sdfat_build_inode(struct super_block *sb,
  3317. const FILE_ID_T *fid, loff_t i_pos) {
  3318. struct inode *inode;
  3319. int err;
  3320. inode = sdfat_iget(sb, i_pos);
  3321. if (inode)
  3322. goto out;
  3323. inode = new_inode(sb);
  3324. if (!inode) {
  3325. inode = ERR_PTR(-ENOMEM);
  3326. goto out;
  3327. }
  3328. inode->i_ino = iunique(sb, SDFAT_ROOT_INO);
  3329. inode_set_iversion(inode, 1);
  3330. err = sdfat_fill_inode(inode, fid);
  3331. if (err) {
  3332. iput(inode);
  3333. inode = ERR_PTR(err);
  3334. goto out;
  3335. }
  3336. sdfat_attach(inode, i_pos);
  3337. insert_inode_hash(inode);
  3338. out:
  3339. return inode;
  3340. }
  3341. static struct inode *sdfat_alloc_inode(struct super_block *sb)
  3342. {
  3343. struct sdfat_inode_info *ei;
  3344. ei = kmem_cache_alloc(sdfat_inode_cachep, GFP_NOFS);
  3345. if (!ei)
  3346. return NULL;
  3347. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
  3348. init_rwsem(&ei->truncate_lock);
  3349. #endif
  3350. return &ei->vfs_inode;
  3351. }
  3352. static void sdfat_free_inode(struct inode *inode)
  3353. {
  3354. if (SDFAT_I(inode)->target) {
  3355. kfree(SDFAT_I(inode)->target);
  3356. SDFAT_I(inode)->target = NULL;
  3357. }
  3358. kmem_cache_free(sdfat_inode_cachep, SDFAT_I(inode));
  3359. }
  3360. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 4, 0)
  3361. /* Use free_inode instead of destroy_inode */
  3362. #define sdfat_destroy_inode (NULL)
  3363. #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)
  3364. static void sdfat_i_callback(struct rcu_head *head)
  3365. {
  3366. struct inode *inode = container_of(head, struct inode, i_rcu);
  3367. sdfat_free_inode(inode);
  3368. }
  3369. static void sdfat_destroy_inode(struct inode *inode)
  3370. {
  3371. call_rcu(&inode->i_rcu, sdfat_i_callback);
  3372. }
  3373. #else
  3374. static void sdfat_destroy_inode(struct inode *inode)
  3375. {
  3376. sdfat_free_inode(inode);
  3377. }
  3378. #endif
  3379. static int __sdfat_write_inode(struct inode *inode, int sync)
  3380. {
  3381. struct super_block *sb = inode->i_sb;
  3382. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  3383. DIR_ENTRY_T info;
  3384. if (inode->i_ino == SDFAT_ROOT_INO)
  3385. return 0;
  3386. info.Attr = sdfat_make_attr(inode);
  3387. info.Size = i_size_read(inode);
  3388. sdfat_time_unix2fat(sbi, &inode->i_mtime, &info.ModifyTimestamp);
  3389. sdfat_time_unix2fat(sbi, &inode->i_ctime, &info.CreateTimestamp);
  3390. sdfat_time_unix2fat(sbi, &inode->i_atime, &info.AccessTimestamp);
  3391. if (!__support_write_inode_sync(sb))
  3392. sync = 0;
  3393. /* FIXME : Do we need handling error? */
  3394. return fsapi_write_inode(inode, &info, sync);
  3395. }
  3396. static int sdfat_sync_inode(struct inode *inode)
  3397. {
  3398. return __sdfat_write_inode(inode, 1);
  3399. }
  3400. static int sdfat_write_inode(struct inode *inode, struct writeback_control *wbc)
  3401. {
  3402. return __sdfat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  3403. }
  3404. static void sdfat_evict_inode(struct inode *inode)
  3405. {
  3406. truncate_inode_pages_final(&inode->i_data);
  3407. if (!inode->i_nlink) {
  3408. loff_t old_size = i_size_read(inode);
  3409. i_size_write(inode, 0);
  3410. SDFAT_I(inode)->fid.size = old_size;
  3411. __cancel_dfr_work(inode, 0, (loff_t)old_size, __func__);
  3412. /* TO CHECK evicting directory works correctly */
  3413. MMSG("%s: inode(%p) evict %s (size(%llu) to zero)\n",
  3414. __func__, inode,
  3415. S_ISDIR(inode->i_mode) ? "directory" : "file",
  3416. (u64)old_size);
  3417. fsapi_truncate(inode, old_size, 0);
  3418. }
  3419. invalidate_inode_buffers(inode);
  3420. clear_inode(inode);
  3421. fsapi_invalidate_extent(inode);
  3422. sdfat_detach(inode);
  3423. /* after end of this function, caller will remove inode hash */
  3424. /* remove_inode_hash(inode); */
  3425. }
  3426. static void sdfat_free_sb_info(struct sdfat_sb_info *sbi)
  3427. {
  3428. if (sbi->nls_disk) {
  3429. unload_nls(sbi->nls_disk);
  3430. sbi->nls_disk = NULL;
  3431. sbi->options.codepage = sdfat_default_codepage;
  3432. }
  3433. if (sbi->nls_io) {
  3434. unload_nls(sbi->nls_io);
  3435. sbi->nls_io = NULL;
  3436. }
  3437. if (sbi->options.iocharset != sdfat_default_iocharset) {
  3438. kfree(sbi->options.iocharset);
  3439. sbi->options.iocharset = sdfat_default_iocharset;
  3440. }
  3441. if (sbi->use_vmalloc) {
  3442. vfree(sbi);
  3443. return;
  3444. }
  3445. kfree(sbi);
  3446. }
  3447. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)
  3448. static void delayed_free(struct rcu_head *p)
  3449. {
  3450. struct sdfat_sb_info *sbi = container_of(p, struct sdfat_sb_info, rcu);
  3451. sdfat_free_sb_info(sbi);
  3452. }
  3453. static void __sdfat_destroy_sb_info(struct super_block *sb)
  3454. {
  3455. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  3456. call_rcu(&sbi->rcu, delayed_free);
  3457. }
  3458. #else
  3459. static void __sdfat_destroy_sb_info(struct super_block *sb)
  3460. {
  3461. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  3462. sdfat_free_sb_info(sbi);
  3463. sb->s_fs_info = NULL;
  3464. }
  3465. #endif
  3466. static void sdfat_destroy_sb_info(struct super_block *sb)
  3467. {
  3468. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  3469. kobject_del(&sbi->sb_kobj);
  3470. kobject_put(&sbi->sb_kobj);
  3471. __sdfat_destroy_sb_info(sb);
  3472. }
  3473. static void sdfat_put_super(struct super_block *sb)
  3474. {
  3475. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  3476. int err;
  3477. sdfat_log_msg(sb, KERN_INFO, "trying to unmount...");
  3478. __cancel_delayed_work_sync(sbi);
  3479. if (__is_sb_dirty(sb))
  3480. sdfat_write_super(sb);
  3481. __free_dfr_mem_if_required(sb);
  3482. err = fsapi_umount(sb);
  3483. sdfat_destroy_sb_info(sb);
  3484. sdfat_log_msg(sb, KERN_INFO, "unmounted successfully! %s",
  3485. err ? "(with previous I/O errors)" : "");
  3486. }
  3487. static inline void __flush_delayed_meta(struct super_block *sb, s32 sync)
  3488. {
  3489. #ifdef CONFIG_SDFAT_DELAYED_META_DIRTY
  3490. fsapi_cache_flush(sb, sync);
  3491. #else
  3492. /* DO NOTHING */
  3493. #endif
  3494. }
  3495. static void sdfat_write_super(struct super_block *sb)
  3496. {
  3497. int time = 0;
  3498. __lock_super(sb);
  3499. __set_sb_clean(sb);
  3500. #ifdef CONFIG_SDFAT_DFR
  3501. if (atomic_read(&(SDFAT_SB(sb)->dfr_info.stat)) == DFR_SB_STAT_VALID)
  3502. fsapi_dfr_update_fat_next(sb);
  3503. #endif
  3504. /* flush delayed FAT/DIR dirty */
  3505. __flush_delayed_meta(sb, 0);
  3506. if (!sb_rdonly(sb))
  3507. fsapi_sync_fs(sb, 0);
  3508. __unlock_super(sb);
  3509. time = jiffies;
  3510. /* Issuing bdev requests is needed
  3511. * to guarantee DIR updates in time
  3512. * whether w/ or w/o delayed DIR dirty feature.
  3513. * (otherwise DIR updates could be delayed for 5 + 5 secs at max.)
  3514. */
  3515. sync_blockdev(sb->s_bdev);
  3516. #if (defined(CONFIG_SDFAT_DFR) && defined(CONFIG_SDFAT_DFR_DEBUG))
  3517. /* SPO test */
  3518. fsapi_dfr_spo_test(sb, DFR_SPO_FAT_NEXT, __func__);
  3519. #endif
  3520. MMSG("BD: sdfat_write_super (bdev_sync for %ld ms)\n",
  3521. (jiffies - time) * 1000 / HZ);
  3522. }
  3523. static void __dfr_update_fat_next(struct super_block *sb)
  3524. {
  3525. #ifdef CONFIG_SDFAT_DFR
  3526. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  3527. if (sbi->options.defrag &&
  3528. (atomic_read(&sbi->dfr_info.stat) == DFR_SB_STAT_VALID)) {
  3529. fsapi_dfr_update_fat_next(sb);
  3530. }
  3531. #endif
  3532. }
  3533. static void __dfr_update_fat_prev(struct super_block *sb, int wait)
  3534. {
  3535. #ifdef CONFIG_SDFAT_DFR
  3536. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  3537. struct defrag_info *sb_dfr = &sbi->dfr_info;
  3538. /* static time available? */
  3539. static int time; /* initialized by zero */
  3540. int uevent = 0, total = 0, clean = 0, full = 0;
  3541. int spent = jiffies - time;
  3542. if (!(sbi->options.defrag && wait))
  3543. return;
  3544. __lock_super(sb);
  3545. /* Update FAT for defrag */
  3546. if (atomic_read(&(sbi->dfr_info.stat)) == DFR_SB_STAT_VALID) {
  3547. fsapi_dfr_update_fat_prev(sb, 0);
  3548. /* flush delayed FAT/DIR dirty */
  3549. __flush_delayed_meta(sb, 0);
  3550. /* Complete defrag req */
  3551. fsapi_sync_fs(sb, 1);
  3552. atomic_set(&sb_dfr->stat, DFR_SB_STAT_REQ);
  3553. complete_all(&sbi->dfr_complete);
  3554. } else if (((spent < 0) || (spent > DFR_DEFAULT_TIMEOUT)) &&
  3555. (atomic_read(&(sbi->dfr_info.stat)) == DFR_SB_STAT_IDLE)) {
  3556. uevent = fsapi_dfr_check_dfr_required(sb, &total, &clean, &full);
  3557. time = jiffies;
  3558. }
  3559. __unlock_super(sb);
  3560. if (uevent) {
  3561. kobject_uevent(&SDFAT_SB(sb)->sb_kobj, KOBJ_CHANGE);
  3562. dfr_debug("uevent for defrag_daemon, total_au %d, "
  3563. "clean_au %d, full_au %d", total, clean, full);
  3564. }
  3565. #endif
  3566. }
  3567. static int sdfat_sync_fs(struct super_block *sb, int wait)
  3568. {
  3569. int err = 0;
  3570. /* If there are some dirty buffers in the bdev inode */
  3571. if (__is_sb_dirty(sb)) {
  3572. __lock_super(sb);
  3573. __set_sb_clean(sb);
  3574. __dfr_update_fat_next(sb);
  3575. err = fsapi_sync_fs(sb, 1);
  3576. #if (defined(CONFIG_SDFAT_DFR) && defined(CONFIG_SDFAT_DFR_DEBUG))
  3577. /* SPO test */
  3578. fsapi_dfr_spo_test(sb, DFR_SPO_FAT_NEXT, __func__);
  3579. #endif
  3580. __unlock_super(sb);
  3581. }
  3582. __dfr_update_fat_prev(sb, wait);
  3583. return err;
  3584. }
  3585. static int sdfat_statfs(struct dentry *dentry, struct kstatfs *buf)
  3586. {
  3587. /*
  3588. * patch 1.2.2 :
  3589. * fixed the slow-call problem because of volume-lock contention.
  3590. */
  3591. struct super_block *sb = dentry->d_sb;
  3592. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  3593. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  3594. VOL_INFO_T info;
  3595. /* fsapi_statfs will try to get a volume lock if needed */
  3596. if (fsapi_statfs(sb, &info))
  3597. return -EIO;
  3598. if (fsi->prev_eio)
  3599. sdfat_msg(sb, KERN_INFO, "called statfs with previous"
  3600. " I/O error(0x%02X).", fsi->prev_eio);
  3601. buf->f_type = sb->s_magic;
  3602. buf->f_bsize = info.ClusterSize;
  3603. buf->f_blocks = info.NumClusters;
  3604. buf->f_bfree = info.FreeClusters;
  3605. buf->f_bavail = info.FreeClusters;
  3606. buf->f_fsid.val[0] = (u32)id;
  3607. buf->f_fsid.val[1] = (u32)(id >> 32);
  3608. /* Unicode utf8 255 characters */
  3609. buf->f_namelen = MAX_NAME_LENGTH * MAX_CHARSET_SIZE;
  3610. return 0;
  3611. }
  3612. static int sdfat_remount(struct super_block *sb, int *flags, char *data)
  3613. {
  3614. unsigned long prev_sb_flags;
  3615. char *orig_data = kstrdup(data, GFP_KERNEL);
  3616. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  3617. FS_INFO_T *fsi = &(sbi->fsi);
  3618. *flags |= SB_NODIRATIME;
  3619. prev_sb_flags = sb->s_flags;
  3620. sdfat_remount_syncfs(sb);
  3621. fsapi_set_vol_flags(sb, VOL_CLEAN, 1);
  3622. sdfat_log_msg(sb, KERN_INFO, "re-mounted(%s->%s), eio=0x%x, Opts: %s",
  3623. (prev_sb_flags & SB_RDONLY) ? "ro" : "rw",
  3624. (*flags & SB_RDONLY) ? "ro" : "rw",
  3625. fsi->prev_eio, orig_data);
  3626. kfree(orig_data);
  3627. return 0;
  3628. }
  3629. static int __sdfat_show_options(struct seq_file *m, struct super_block *sb)
  3630. {
  3631. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  3632. struct sdfat_mount_options *opts = &sbi->options;
  3633. FS_INFO_T *fsi = &(sbi->fsi);
  3634. /* Show partition info */
  3635. seq_printf(m, ",fs=%s", sdfat_get_vol_type_str(fsi->vol_type));
  3636. if (fsi->prev_eio)
  3637. seq_printf(m, ",eio=0x%x", fsi->prev_eio);
  3638. if (!uid_eq(opts->fs_uid, GLOBAL_ROOT_UID))
  3639. seq_printf(m, ",uid=%u",
  3640. from_kuid_munged(&init_user_ns, opts->fs_uid));
  3641. if (!gid_eq(opts->fs_gid, GLOBAL_ROOT_GID))
  3642. seq_printf(m, ",gid=%u",
  3643. from_kgid_munged(&init_user_ns, opts->fs_gid));
  3644. seq_printf(m, ",fmask=%04o", opts->fs_fmask);
  3645. seq_printf(m, ",dmask=%04o", opts->fs_dmask);
  3646. if (opts->allow_utime)
  3647. seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
  3648. if (sbi->nls_disk)
  3649. seq_printf(m, ",codepage=%s", sbi->nls_disk->charset);
  3650. if (sbi->nls_io)
  3651. seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
  3652. if (opts->utf8)
  3653. seq_puts(m, ",utf8");
  3654. if (sbi->fsi.vol_type != EXFAT)
  3655. seq_puts(m, ",shortname=winnt");
  3656. seq_printf(m, ",namecase=%u", opts->casesensitive);
  3657. if (opts->tz_utc)
  3658. seq_puts(m, ",tz=UTC");
  3659. if (opts->improved_allocation & SDFAT_ALLOC_DELAY)
  3660. seq_puts(m, ",delay");
  3661. if (opts->improved_allocation & SDFAT_ALLOC_SMART)
  3662. seq_printf(m, ",smart,ausize=%u", opts->amap_opt.sect_per_au);
  3663. if (opts->defrag)
  3664. seq_puts(m, ",defrag");
  3665. if (opts->adj_hidsect)
  3666. seq_puts(m, ",adj_hid");
  3667. if (opts->adj_req)
  3668. seq_puts(m, ",adj_req");
  3669. seq_printf(m, ",symlink=%u", opts->symlink);
  3670. seq_printf(m, ",bps=%ld", sb->s_blocksize);
  3671. if (opts->errors == SDFAT_ERRORS_CONT)
  3672. seq_puts(m, ",errors=continue");
  3673. else if (opts->errors == SDFAT_ERRORS_PANIC)
  3674. seq_puts(m, ",errors=panic");
  3675. else
  3676. seq_puts(m, ",errors=remount-ro");
  3677. if (opts->discard)
  3678. seq_puts(m, ",discard");
  3679. return 0;
  3680. }
  3681. static const struct super_operations sdfat_sops = {
  3682. .alloc_inode = sdfat_alloc_inode,
  3683. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 4, 0)
  3684. .free_inode = sdfat_free_inode,
  3685. #else
  3686. .destroy_inode = sdfat_destroy_inode,
  3687. #endif
  3688. .write_inode = sdfat_write_inode,
  3689. .evict_inode = sdfat_evict_inode,
  3690. .put_super = sdfat_put_super,
  3691. #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
  3692. .write_super = sdfat_write_super,
  3693. #endif
  3694. .sync_fs = sdfat_sync_fs,
  3695. .statfs = sdfat_statfs,
  3696. .remount_fs = sdfat_remount,
  3697. .show_options = sdfat_show_options,
  3698. };
  3699. /*======================================================================*/
  3700. /* SYSFS Operations */
  3701. /*======================================================================*/
  3702. #define SDFAT_ATTR(name, mode, show, store) \
  3703. static struct sdfat_attr sdfat_attr_##name = __ATTR(name, mode, show, store)
  3704. struct sdfat_attr {
  3705. struct attribute attr;
  3706. ssize_t (*show)(struct sdfat_sb_info *, char *);
  3707. ssize_t (*store)(struct sdfat_sb_info *, const char *, size_t);
  3708. };
  3709. static ssize_t sdfat_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
  3710. {
  3711. struct sdfat_sb_info *sbi = container_of(kobj, struct sdfat_sb_info, sb_kobj);
  3712. struct sdfat_attr *a = container_of(attr, struct sdfat_attr, attr);
  3713. return a->show ? a->show(sbi, buf) : 0;
  3714. }
  3715. static ssize_t sdfat_attr_store(struct kobject *kobj, struct attribute *attr,
  3716. const char *buf, size_t len)
  3717. {
  3718. struct sdfat_sb_info *sbi = container_of(kobj, struct sdfat_sb_info, sb_kobj);
  3719. struct sdfat_attr *a = container_of(attr, struct sdfat_attr, attr);
  3720. return a->store ? a->store(sbi, buf, len) : len;
  3721. }
  3722. static const struct sysfs_ops sdfat_attr_ops = {
  3723. .show = sdfat_attr_show,
  3724. .store = sdfat_attr_store,
  3725. };
  3726. static ssize_t type_show(struct sdfat_sb_info *sbi, char *buf)
  3727. {
  3728. FS_INFO_T *fsi = &(sbi->fsi);
  3729. return snprintf(buf, PAGE_SIZE, "%s\n", sdfat_get_vol_type_str(fsi->vol_type));
  3730. }
  3731. SDFAT_ATTR(type, 0444, type_show, NULL);
  3732. static ssize_t eio_show(struct sdfat_sb_info *sbi, char *buf)
  3733. {
  3734. FS_INFO_T *fsi = &(sbi->fsi);
  3735. return snprintf(buf, PAGE_SIZE, "0x%x\n", fsi->prev_eio);
  3736. }
  3737. SDFAT_ATTR(eio, 0444, eio_show, NULL);
  3738. static ssize_t fratio_show(struct sdfat_sb_info *sbi, char *buf)
  3739. {
  3740. unsigned int n_total_au = 0;
  3741. unsigned int n_clean_au = 0;
  3742. unsigned int n_full_au = 0;
  3743. unsigned int n_dirty_au = 0;
  3744. unsigned int fr = 0;
  3745. n_total_au = fsapi_get_au_stat(sbi->host_sb, VOL_AU_STAT_TOTAL);
  3746. n_clean_au = fsapi_get_au_stat(sbi->host_sb, VOL_AU_STAT_CLEAN);
  3747. n_full_au = fsapi_get_au_stat(sbi->host_sb, VOL_AU_STAT_FULL);
  3748. n_dirty_au = n_total_au - (n_full_au + n_clean_au);
  3749. if (!n_dirty_au)
  3750. fr = 0;
  3751. else if (!n_clean_au)
  3752. fr = 100;
  3753. else
  3754. fr = (n_dirty_au * 100) / (n_clean_au + n_dirty_au);
  3755. return snprintf(buf, PAGE_SIZE, "%u\n", fr);
  3756. }
  3757. SDFAT_ATTR(fratio, 0444, fratio_show, NULL);
  3758. static ssize_t totalau_show(struct sdfat_sb_info *sbi, char *buf)
  3759. {
  3760. unsigned int n_au = 0;
  3761. n_au = fsapi_get_au_stat(sbi->host_sb, VOL_AU_STAT_TOTAL);
  3762. return snprintf(buf, PAGE_SIZE, "%u\n", n_au);
  3763. }
  3764. SDFAT_ATTR(totalau, 0444, totalau_show, NULL);
  3765. static ssize_t cleanau_show(struct sdfat_sb_info *sbi, char *buf)
  3766. {
  3767. unsigned int n_clean_au = 0;
  3768. n_clean_au = fsapi_get_au_stat(sbi->host_sb, VOL_AU_STAT_CLEAN);
  3769. return snprintf(buf, PAGE_SIZE, "%u\n", n_clean_au);
  3770. }
  3771. SDFAT_ATTR(cleanau, 0444, cleanau_show, NULL);
  3772. static ssize_t fullau_show(struct sdfat_sb_info *sbi, char *buf)
  3773. {
  3774. unsigned int n_full_au = 0;
  3775. n_full_au = fsapi_get_au_stat(sbi->host_sb, VOL_AU_STAT_FULL);
  3776. return snprintf(buf, PAGE_SIZE, "%u\n", n_full_au);
  3777. }
  3778. SDFAT_ATTR(fullau, 0444, fullau_show, NULL);
  3779. static struct attribute *sdfat_attrs[] = {
  3780. &sdfat_attr_type.attr,
  3781. &sdfat_attr_eio.attr,
  3782. &sdfat_attr_fratio.attr,
  3783. &sdfat_attr_totalau.attr,
  3784. &sdfat_attr_cleanau.attr,
  3785. &sdfat_attr_fullau.attr,
  3786. NULL,
  3787. };
  3788. static struct kobj_type sdfat_ktype = {
  3789. .default_attrs = sdfat_attrs,
  3790. .sysfs_ops = &sdfat_attr_ops,
  3791. };
  3792. static ssize_t version_show(struct kobject *kobj,
  3793. struct kobj_attribute *attr, char *buff)
  3794. {
  3795. return snprintf(buff, PAGE_SIZE, "FS Version %s\n", SDFAT_VERSION);
  3796. }
  3797. static struct kobj_attribute version_attr = __ATTR_RO(version);
  3798. static struct attribute *attributes[] = {
  3799. &version_attr.attr,
  3800. NULL,
  3801. };
  3802. static struct attribute_group attr_group = {
  3803. .attrs = attributes,
  3804. };
  3805. /*======================================================================*/
  3806. /* Super Block Read Operations */
  3807. /*======================================================================*/
  3808. enum {
  3809. Opt_uid,
  3810. Opt_gid,
  3811. Opt_umask,
  3812. Opt_dmask,
  3813. Opt_fmask,
  3814. Opt_allow_utime,
  3815. Opt_codepage,
  3816. Opt_charset,
  3817. Opt_utf8,
  3818. Opt_namecase,
  3819. Opt_tz_utc,
  3820. Opt_adj_hidsect,
  3821. Opt_delay,
  3822. Opt_smart,
  3823. Opt_ausize,
  3824. Opt_packing,
  3825. Opt_defrag,
  3826. Opt_symlink,
  3827. Opt_debug,
  3828. Opt_err_cont,
  3829. Opt_err_panic,
  3830. Opt_err_ro,
  3831. Opt_err,
  3832. Opt_discard,
  3833. Opt_fs,
  3834. Opt_adj_req,
  3835. #ifdef CONFIG_SDFAT_USE_FOR_VFAT
  3836. Opt_shortname_lower,
  3837. Opt_shortname_win95,
  3838. Opt_shortname_winnt,
  3839. Opt_shortname_mixed,
  3840. #endif /* CONFIG_SDFAT_USE_FOR_VFAT */
  3841. };
  3842. static const match_table_t sdfat_tokens = {
  3843. {Opt_uid, "uid=%u"},
  3844. {Opt_gid, "gid=%u"},
  3845. {Opt_umask, "umask=%o"},
  3846. {Opt_dmask, "dmask=%o"},
  3847. {Opt_fmask, "fmask=%o"},
  3848. {Opt_allow_utime, "allow_utime=%o"},
  3849. {Opt_codepage, "codepage=%u"},
  3850. {Opt_charset, "iocharset=%s"},
  3851. {Opt_utf8, "utf8"},
  3852. {Opt_namecase, "namecase=%u"},
  3853. {Opt_tz_utc, "tz=UTC"},
  3854. {Opt_adj_hidsect, "adj_hid"},
  3855. {Opt_delay, "delay"},
  3856. {Opt_smart, "smart"},
  3857. {Opt_ausize, "ausize=%u"},
  3858. {Opt_packing, "packing=%u"},
  3859. {Opt_defrag, "defrag"},
  3860. {Opt_symlink, "symlink=%u"},
  3861. {Opt_debug, "debug"},
  3862. {Opt_err_cont, "errors=continue"},
  3863. {Opt_err_panic, "errors=panic"},
  3864. {Opt_err_ro, "errors=remount-ro"},
  3865. {Opt_discard, "discard"},
  3866. {Opt_fs, "fs=%s"},
  3867. {Opt_adj_req, "adj_req"},
  3868. #ifdef CONFIG_SDFAT_USE_FOR_VFAT
  3869. {Opt_shortname_lower, "shortname=lower"},
  3870. {Opt_shortname_win95, "shortname=win95"},
  3871. {Opt_shortname_winnt, "shortname=winnt"},
  3872. {Opt_shortname_mixed, "shortname=mixed"},
  3873. #endif /* CONFIG_SDFAT_USE_FOR_VFAT */
  3874. {Opt_err, NULL}
  3875. };
  3876. static int parse_options(struct super_block *sb, char *options, int silent,
  3877. int *debug, struct sdfat_mount_options *opts)
  3878. {
  3879. char *p;
  3880. substring_t args[MAX_OPT_ARGS];
  3881. int option, i;
  3882. char *tmpstr;
  3883. opts->fs_uid = current_uid();
  3884. opts->fs_gid = current_gid();
  3885. opts->fs_fmask = opts->fs_dmask = current->fs->umask;
  3886. opts->allow_utime = (unsigned short) -1;
  3887. opts->codepage = sdfat_default_codepage;
  3888. opts->iocharset = sdfat_default_iocharset;
  3889. opts->casesensitive = 0;
  3890. opts->utf8 = 0;
  3891. opts->adj_hidsect = 0;
  3892. opts->tz_utc = 0;
  3893. opts->improved_allocation = 0;
  3894. opts->amap_opt.pack_ratio = 0; // Default packing
  3895. opts->amap_opt.sect_per_au = 0;
  3896. opts->amap_opt.misaligned_sect = 0;
  3897. opts->symlink = 0;
  3898. opts->errors = SDFAT_ERRORS_RO;
  3899. opts->discard = 0;
  3900. *debug = 0;
  3901. if (!options)
  3902. goto out;
  3903. while ((p = strsep(&options, ",")) != NULL) {
  3904. int token;
  3905. if (!*p)
  3906. continue;
  3907. token = match_token(p, sdfat_tokens, args);
  3908. switch (token) {
  3909. case Opt_uid:
  3910. if (match_int(&args[0], &option))
  3911. return 0;
  3912. opts->fs_uid = make_kuid(current_user_ns(), option);
  3913. break;
  3914. case Opt_gid:
  3915. if (match_int(&args[0], &option))
  3916. return 0;
  3917. opts->fs_gid = make_kgid(current_user_ns(), option);
  3918. break;
  3919. case Opt_umask:
  3920. case Opt_dmask:
  3921. case Opt_fmask:
  3922. if (match_octal(&args[0], &option))
  3923. return 0;
  3924. if (token != Opt_dmask)
  3925. opts->fs_fmask = option;
  3926. if (token != Opt_fmask)
  3927. opts->fs_dmask = option;
  3928. break;
  3929. case Opt_allow_utime:
  3930. if (match_octal(&args[0], &option))
  3931. return 0;
  3932. opts->allow_utime = option & (S_IWGRP | S_IWOTH);
  3933. break;
  3934. case Opt_codepage:
  3935. if (match_int(&args[0], &option))
  3936. return 0;
  3937. opts->codepage = option;
  3938. break;
  3939. case Opt_charset:
  3940. if (opts->iocharset != sdfat_default_iocharset)
  3941. kfree(opts->iocharset);
  3942. tmpstr = match_strdup(&args[0]);
  3943. if (!tmpstr)
  3944. return -ENOMEM;
  3945. opts->iocharset = tmpstr;
  3946. break;
  3947. case Opt_namecase:
  3948. if (match_int(&args[0], &option))
  3949. return 0;
  3950. opts->casesensitive = (option > 0) ? 1:0;
  3951. break;
  3952. case Opt_utf8:
  3953. opts->utf8 = 1;
  3954. break;
  3955. case Opt_adj_hidsect:
  3956. opts->adj_hidsect = 1;
  3957. break;
  3958. case Opt_tz_utc:
  3959. opts->tz_utc = 1;
  3960. break;
  3961. case Opt_symlink:
  3962. if (match_int(&args[0], &option))
  3963. return 0;
  3964. opts->symlink = option > 0 ? 1 : 0;
  3965. break;
  3966. case Opt_delay:
  3967. opts->improved_allocation |= SDFAT_ALLOC_DELAY;
  3968. break;
  3969. case Opt_smart:
  3970. opts->improved_allocation |= SDFAT_ALLOC_SMART;
  3971. break;
  3972. case Opt_ausize:
  3973. if (match_int(&args[0], &option))
  3974. return -EINVAL;
  3975. if (!is_power_of_2(option))
  3976. return -EINVAL;
  3977. opts->amap_opt.sect_per_au = option;
  3978. IMSG("set AU size by option : %u sectors\n", option);
  3979. break;
  3980. case Opt_packing:
  3981. if (match_int(&args[0], &option))
  3982. return 0;
  3983. opts->amap_opt.pack_ratio = option;
  3984. break;
  3985. case Opt_defrag:
  3986. #ifdef CONFIG_SDFAT_DFR
  3987. opts->defrag = 1;
  3988. #else
  3989. IMSG("defragmentation config is not enabled. ignore\n");
  3990. #endif
  3991. break;
  3992. case Opt_err_cont:
  3993. opts->errors = SDFAT_ERRORS_CONT;
  3994. break;
  3995. case Opt_err_panic:
  3996. opts->errors = SDFAT_ERRORS_PANIC;
  3997. break;
  3998. case Opt_err_ro:
  3999. opts->errors = SDFAT_ERRORS_RO;
  4000. break;
  4001. case Opt_debug:
  4002. *debug = 1;
  4003. break;
  4004. case Opt_discard:
  4005. opts->discard = 1;
  4006. break;
  4007. case Opt_fs:
  4008. tmpstr = match_strdup(&args[0]);
  4009. if (!tmpstr)
  4010. return -ENOMEM;
  4011. for (i = 0; i < FS_TYPE_MAX; i++) {
  4012. if (!strcmp(tmpstr, FS_TYPE_STR[i])) {
  4013. opts->fs_type = (unsigned char)i;
  4014. sdfat_log_msg(sb, KERN_ERR,
  4015. "set fs-type by option : %s",
  4016. FS_TYPE_STR[i]);
  4017. break;
  4018. }
  4019. }
  4020. kfree(tmpstr);
  4021. if (i == FS_TYPE_MAX) {
  4022. sdfat_log_msg(sb, KERN_ERR,
  4023. "invalid fs-type, "
  4024. "only allow auto, exfat, vfat");
  4025. return -EINVAL;
  4026. }
  4027. break;
  4028. case Opt_adj_req:
  4029. #ifdef CONFIG_SDFAT_ALIGNED_MPAGE_WRITE
  4030. opts->adj_req = 1;
  4031. #else
  4032. IMSG("adjust request config is not enabled. ignore\n");
  4033. #endif
  4034. break;
  4035. #ifdef CONFIG_SDFAT_USE_FOR_VFAT
  4036. case Opt_shortname_lower:
  4037. case Opt_shortname_win95:
  4038. case Opt_shortname_mixed:
  4039. pr_warn("[SDFAT] DRAGONS AHEAD! sdFAT only understands \"shortname=winnt\"!\n");
  4040. case Opt_shortname_winnt:
  4041. break;
  4042. #endif /* CONFIG_SDFAT_USE_FOR_VFAT */
  4043. default:
  4044. if (!silent) {
  4045. sdfat_msg(sb, KERN_ERR,
  4046. "unrecognized mount option \"%s\" "
  4047. "or missing value", p);
  4048. }
  4049. return -EINVAL;
  4050. }
  4051. }
  4052. out:
  4053. if (opts->allow_utime == (unsigned short) -1)
  4054. opts->allow_utime = ~opts->fs_dmask & (S_IWGRP | S_IWOTH);
  4055. if (opts->utf8 && strcmp(opts->iocharset, sdfat_iocharset_with_utf8)) {
  4056. sdfat_msg(sb, KERN_WARNING,
  4057. "utf8 enabled, \"iocharset=%s\" is recommended",
  4058. sdfat_iocharset_with_utf8);
  4059. }
  4060. if (opts->discard) {
  4061. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  4062. if (!blk_queue_discard(q))
  4063. sdfat_msg(sb, KERN_WARNING,
  4064. "mounting with \"discard\" option, but "
  4065. "the device does not support discard");
  4066. opts->discard = 0;
  4067. }
  4068. return 0;
  4069. }
  4070. static void sdfat_hash_init(struct super_block *sb)
  4071. {
  4072. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  4073. int i;
  4074. spin_lock_init(&sbi->inode_hash_lock);
  4075. for (i = 0; i < SDFAT_HASH_SIZE; i++)
  4076. INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
  4077. }
  4078. static int sdfat_read_root(struct inode *inode)
  4079. {
  4080. struct super_block *sb = inode->i_sb;
  4081. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  4082. sdfat_timespec_t ts;
  4083. FS_INFO_T *fsi = &(sbi->fsi);
  4084. DIR_ENTRY_T info;
  4085. ts = current_time(inode);
  4086. SDFAT_I(inode)->fid.dir.dir = fsi->root_dir;
  4087. SDFAT_I(inode)->fid.dir.flags = 0x01;
  4088. SDFAT_I(inode)->fid.entry = -1;
  4089. SDFAT_I(inode)->fid.start_clu = fsi->root_dir;
  4090. SDFAT_I(inode)->fid.flags = 0x01;
  4091. SDFAT_I(inode)->fid.type = TYPE_DIR;
  4092. SDFAT_I(inode)->fid.version = 0;
  4093. SDFAT_I(inode)->fid.rwoffset = 0;
  4094. SDFAT_I(inode)->fid.hint_bmap.off = CLUS_EOF;
  4095. SDFAT_I(inode)->fid.hint_stat.eidx = 0;
  4096. SDFAT_I(inode)->fid.hint_stat.clu = fsi->root_dir;
  4097. SDFAT_I(inode)->fid.hint_femp.eidx = -1;
  4098. SDFAT_I(inode)->target = NULL;
  4099. if (fsapi_read_inode(inode, &info) < 0)
  4100. return -EIO;
  4101. inode->i_uid = sbi->options.fs_uid;
  4102. inode->i_gid = sbi->options.fs_gid;
  4103. inode_inc_iversion(inode);
  4104. inode->i_generation = 0;
  4105. inode->i_mode = sdfat_make_mode(sbi, ATTR_SUBDIR, S_IRWXUGO);
  4106. inode->i_op = &sdfat_dir_inode_operations;
  4107. inode->i_fop = &sdfat_dir_operations;
  4108. i_size_write(inode, info.Size);
  4109. SDFAT_I(inode)->fid.size = info.Size;
  4110. inode->i_blocks = ((i_size_read(inode) + (fsi->cluster_size - 1))
  4111. & ~((loff_t)fsi->cluster_size - 1)) >> inode->i_blkbits;
  4112. SDFAT_I(inode)->i_pos = ((loff_t) fsi->root_dir << 32) | 0xffffffff;
  4113. SDFAT_I(inode)->i_size_aligned = i_size_read(inode);
  4114. SDFAT_I(inode)->i_size_ondisk = i_size_read(inode);
  4115. sdfat_save_attr(inode, ATTR_SUBDIR);
  4116. inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
  4117. set_nlink(inode, info.NumSubdirs + 2);
  4118. return 0;
  4119. }
  4120. static void setup_dops(struct super_block *sb)
  4121. {
  4122. if (SDFAT_SB(sb)->options.casesensitive == 0)
  4123. sb->s_d_op = &sdfat_ci_dentry_ops;
  4124. else
  4125. sb->s_d_op = &sdfat_dentry_ops;
  4126. }
  4127. static int sdfat_fill_super(struct super_block *sb, void *data, int silent)
  4128. {
  4129. struct inode *root_inode = NULL;
  4130. struct sdfat_sb_info *sbi;
  4131. int debug;
  4132. int err;
  4133. char buf[50];
  4134. struct block_device *bdev = sb->s_bdev;
  4135. dev_t bd_dev = bdev ? bdev->bd_dev : 0;
  4136. sdfat_log_msg(sb, KERN_INFO, "trying to mount...");
  4137. /*
  4138. * GFP_KERNEL is ok here, because while we do hold the
  4139. * supeblock lock, memory pressure can't call back into
  4140. * the filesystem, since we're only just about to mount
  4141. * it and have no inodes etc active!
  4142. */
  4143. sbi = kzalloc(sizeof(struct sdfat_sb_info), GFP_KERNEL);
  4144. if (!sbi) {
  4145. sdfat_log_msg(sb, KERN_INFO,
  4146. "trying to alloc sbi with vzalloc()");
  4147. sbi = vzalloc(sizeof(struct sdfat_sb_info));
  4148. if (!sbi) {
  4149. sdfat_log_msg(sb, KERN_ERR, "failed to mount! (ENOMEM)");
  4150. return -ENOMEM;
  4151. }
  4152. sbi->use_vmalloc = 1;
  4153. }
  4154. mutex_init(&sbi->s_vlock);
  4155. sb->s_fs_info = sbi;
  4156. sb->s_flags |= SB_NODIRATIME;
  4157. sb->s_magic = SDFAT_SUPER_MAGIC;
  4158. sb->s_op = &sdfat_sops;
  4159. ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL,
  4160. DEFAULT_RATELIMIT_BURST);
  4161. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)
  4162. sb->s_time_gran = NSEC_PER_SEC; /* the same with default */
  4163. sb->s_time_min = SDFAT_MIN_TIMESTAMP_SECS;
  4164. sb->s_time_max = SDFAT_MAX_TIMESTAMP_SECS;
  4165. #endif
  4166. err = parse_options(sb, data, silent, &debug, &sbi->options);
  4167. if (err) {
  4168. sdfat_log_msg(sb, KERN_ERR, "failed to parse options");
  4169. goto failed_mount;
  4170. }
  4171. setup_sdfat_xattr_handler(sb);
  4172. setup_sdfat_sync_super_wq(sb);
  4173. setup_dops(sb);
  4174. err = fsapi_mount(sb);
  4175. if (err) {
  4176. sdfat_log_msg(sb, KERN_ERR, "failed to recognize fat type");
  4177. goto failed_mount;
  4178. }
  4179. /* set up enough so that it can read an inode */
  4180. sdfat_hash_init(sb);
  4181. /*
  4182. * The low byte of FAT's first entry must have same value with
  4183. * media-field. But in real world, too many devices is
  4184. * writing wrong value. So, removed that validity check.
  4185. *
  4186. * if (FAT_FIRST_ENT(sb, media) != first)
  4187. */
  4188. err = -EINVAL;
  4189. sprintf(buf, "cp%d", sbi->options.codepage);
  4190. sbi->nls_disk = load_nls(buf);
  4191. if (!sbi->nls_disk) {
  4192. sdfat_log_msg(sb, KERN_ERR, "codepage %s not found", buf);
  4193. goto failed_mount2;
  4194. }
  4195. sbi->nls_io = load_nls(sbi->options.iocharset);
  4196. if (!sbi->nls_io) {
  4197. sdfat_log_msg(sb, KERN_ERR, "IO charset %s not found",
  4198. sbi->options.iocharset);
  4199. goto failed_mount2;
  4200. }
  4201. err = __alloc_dfr_mem_if_required(sb);
  4202. if (err) {
  4203. sdfat_log_msg(sb, KERN_ERR, "failed to initialize a memory for "
  4204. "defragmentation");
  4205. goto failed_mount3;
  4206. }
  4207. err = -ENOMEM;
  4208. root_inode = new_inode(sb);
  4209. if (!root_inode) {
  4210. sdfat_log_msg(sb, KERN_ERR, "failed to allocate root inode.");
  4211. goto failed_mount3;
  4212. }
  4213. root_inode->i_ino = SDFAT_ROOT_INO;
  4214. inode_set_iversion(root_inode, 1);
  4215. err = sdfat_read_root(root_inode);
  4216. if (err) {
  4217. sdfat_log_msg(sb, KERN_ERR, "failed to initialize root inode.");
  4218. goto failed_mount3;
  4219. }
  4220. sdfat_attach(root_inode, SDFAT_I(root_inode)->i_pos);
  4221. insert_inode_hash(root_inode);
  4222. err = -ENOMEM;
  4223. sb->s_root = __d_make_root(root_inode);
  4224. if (!sb->s_root) {
  4225. sdfat_msg(sb, KERN_ERR, "failed to get the root dentry");
  4226. goto failed_mount3;
  4227. }
  4228. /*
  4229. * Initialize filesystem attributes (for sysfs)
  4230. * ex: /sys/fs/sdfat/mmcblk1[179:17]
  4231. */
  4232. sbi->sb_kobj.kset = sdfat_kset;
  4233. err = kobject_init_and_add(&sbi->sb_kobj, &sdfat_ktype, NULL,
  4234. "%s[%d:%d]", sb->s_id, MAJOR(bd_dev), MINOR(bd_dev));
  4235. if (err) {
  4236. sdfat_msg(sb, KERN_ERR, "Unable to create sdfat attributes for"
  4237. " %s[%d:%d](%d)", sb->s_id,
  4238. MAJOR(bd_dev), MINOR(bd_dev), err);
  4239. goto failed_mount3;
  4240. }
  4241. sdfat_log_msg(sb, KERN_INFO, "mounted successfully!");
  4242. /* FOR BIGDATA */
  4243. sdfat_statistics_set_mnt(&sbi->fsi);
  4244. sdfat_statistics_set_vol_size(sb);
  4245. return 0;
  4246. failed_mount3:
  4247. __free_dfr_mem_if_required(sb);
  4248. failed_mount2:
  4249. fsapi_umount(sb);
  4250. failed_mount:
  4251. sdfat_log_msg(sb, KERN_INFO, "failed to mount! (%d)", err);
  4252. if (root_inode)
  4253. iput(root_inode);
  4254. sb->s_root = NULL;
  4255. if (sbi->nls_io)
  4256. unload_nls(sbi->nls_io);
  4257. if (sbi->nls_disk)
  4258. unload_nls(sbi->nls_disk);
  4259. if (sbi->options.iocharset != sdfat_default_iocharset)
  4260. kfree(sbi->options.iocharset);
  4261. sb->s_fs_info = NULL;
  4262. if (!sbi->use_vmalloc)
  4263. kfree(sbi);
  4264. else
  4265. vfree(sbi);
  4266. return err;
  4267. }
  4268. static struct dentry *sdfat_fs_mount(struct file_system_type *fs_type,
  4269. int flags, const char *dev_name, void *data) {
  4270. return mount_bdev(fs_type, flags, dev_name, data, sdfat_fill_super);
  4271. }
  4272. static void init_once(void *foo)
  4273. {
  4274. struct sdfat_inode_info *ei = (struct sdfat_inode_info *)foo;
  4275. INIT_HLIST_NODE(&ei->i_hash_fat);
  4276. inode_init_once(&ei->vfs_inode);
  4277. }
  4278. static int __init sdfat_init_inodecache(void)
  4279. {
  4280. sdfat_inode_cachep = kmem_cache_create("sdfat_inode_cache",
  4281. sizeof(struct sdfat_inode_info),
  4282. 0, (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
  4283. init_once);
  4284. if (!sdfat_inode_cachep)
  4285. return -ENOMEM;
  4286. return 0;
  4287. }
  4288. static void sdfat_destroy_inodecache(void)
  4289. {
  4290. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)
  4291. /*
  4292. * Make sure all delayed rcu free inodes are flushed before we
  4293. * destroy cache.
  4294. */
  4295. rcu_barrier();
  4296. #endif
  4297. kmem_cache_destroy(sdfat_inode_cachep);
  4298. }
  4299. #ifdef CONFIG_SDFAT_DBG_IOCTL
  4300. static void sdfat_debug_kill_sb(struct super_block *sb)
  4301. {
  4302. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  4303. struct block_device *bdev = sb->s_bdev;
  4304. long flags;
  4305. if (sbi) {
  4306. flags = sbi->debug_flags;
  4307. if (flags & SDFAT_DEBUGFLAGS_INVALID_UMOUNT) {
  4308. /* invalidate_bdev drops all device cache include dirty.
  4309. * we use this to simulate device removal
  4310. */
  4311. fsapi_cache_release(sb);
  4312. invalidate_bdev(bdev);
  4313. }
  4314. }
  4315. kill_block_super(sb);
  4316. }
  4317. #endif /* CONFIG_SDFAT_DBG_IOCTL */
  4318. static struct file_system_type sdfat_fs_type = {
  4319. .owner = THIS_MODULE,
  4320. .name = "sdfat",
  4321. .mount = sdfat_fs_mount,
  4322. #ifdef CONFIG_SDFAT_DBG_IOCTL
  4323. .kill_sb = sdfat_debug_kill_sb,
  4324. #else
  4325. .kill_sb = kill_block_super,
  4326. #endif /* CONFIG_SDFAT_DBG_IOCTL */
  4327. .fs_flags = FS_REQUIRES_DEV,
  4328. };
  4329. MODULE_ALIAS_FS("sdfat");
  4330. #ifdef CONFIG_SDFAT_USE_FOR_EXFAT
  4331. static struct file_system_type exfat_fs_type = {
  4332. .owner = THIS_MODULE,
  4333. .name = "exfat",
  4334. .mount = sdfat_fs_mount,
  4335. #ifdef CONFIG_SDFAT_DBG_IOCTL
  4336. .kill_sb = sdfat_debug_kill_sb,
  4337. #else
  4338. .kill_sb = kill_block_super,
  4339. #endif /* CONFIG_SDFAT_DBG_IOCTL */
  4340. .fs_flags = FS_REQUIRES_DEV,
  4341. };
  4342. MODULE_ALIAS_FS("exfat");
  4343. #endif /* CONFIG_SDFAT_USE_FOR_EXFAT */
  4344. #ifdef CONFIG_SDFAT_USE_FOR_VFAT
  4345. static struct file_system_type vfat_fs_type = {
  4346. .owner = THIS_MODULE,
  4347. .name = "vfat",
  4348. .mount = sdfat_fs_mount,
  4349. #ifdef CONFIG_SDFAT_DBG_IOCTL
  4350. .kill_sb = sdfat_debug_kill_sb,
  4351. #else
  4352. .kill_sb = kill_block_super,
  4353. #endif /* CONFIG_SDFAT_DBG_IOCTL */
  4354. .fs_flags = FS_REQUIRES_DEV,
  4355. };
  4356. MODULE_ALIAS_FS("vfat");
  4357. #endif /* CONFIG_SDFAT_USE_FOR_VFAT */
  4358. static int __init init_sdfat_fs(void)
  4359. {
  4360. int err;
  4361. sdfat_log_version();
  4362. err = fsapi_init();
  4363. if (err)
  4364. goto error;
  4365. sdfat_kset = kset_create_and_add("sdfat", NULL, fs_kobj);
  4366. if (!sdfat_kset) {
  4367. pr_err("[SDFAT] failed to create sdfat kset\n");
  4368. err = -ENOMEM;
  4369. goto error;
  4370. }
  4371. err = sysfs_create_group(&sdfat_kset->kobj, &attr_group);
  4372. if (err) {
  4373. pr_err("[SDFAT] failed to create sdfat version attributes\n");
  4374. goto error;
  4375. }
  4376. err = sdfat_statistics_init(sdfat_kset);
  4377. if (err)
  4378. goto error;
  4379. err = sdfat_uevent_init(sdfat_kset);
  4380. if (err)
  4381. goto error;
  4382. err = sdfat_init_inodecache();
  4383. if (err) {
  4384. pr_err("[SDFAT] failed to initialize inode cache\n");
  4385. goto error;
  4386. }
  4387. err = register_filesystem(&sdfat_fs_type);
  4388. if (err) {
  4389. pr_err("[SDFAT] failed to register filesystem\n");
  4390. goto error;
  4391. }
  4392. #ifdef CONFIG_SDFAT_USE_FOR_EXFAT
  4393. err = register_filesystem(&exfat_fs_type);
  4394. if (err) {
  4395. pr_err("[SDFAT] failed to register for exfat filesystem\n");
  4396. goto error;
  4397. }
  4398. #endif /* CONFIG_SDFAT_USE_FOR_EXFAT */
  4399. #ifdef CONFIG_SDFAT_USE_FOR_VFAT
  4400. err = register_filesystem(&vfat_fs_type);
  4401. if (err) {
  4402. pr_err("[SDFAT] failed to register for vfat filesystem\n");
  4403. goto error;
  4404. }
  4405. #endif /* CONFIG_SDFAT_USE_FOR_VFAT */
  4406. return 0;
  4407. error:
  4408. sdfat_uevent_uninit();
  4409. sdfat_statistics_uninit();
  4410. if (sdfat_kset) {
  4411. sysfs_remove_group(&sdfat_kset->kobj, &attr_group);
  4412. kset_unregister(sdfat_kset);
  4413. sdfat_kset = NULL;
  4414. }
  4415. sdfat_destroy_inodecache();
  4416. fsapi_shutdown();
  4417. pr_err("[SDFAT] failed to initialize FS driver(err:%d)\n", err);
  4418. return err;
  4419. }
  4420. static void __exit exit_sdfat_fs(void)
  4421. {
  4422. #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)
  4423. /*
  4424. * Make sure all delayed rcu free inodes are flushed before we
  4425. * destroy cache.
  4426. */
  4427. rcu_barrier();
  4428. #endif
  4429. sdfat_uevent_uninit();
  4430. sdfat_statistics_uninit();
  4431. if (sdfat_kset) {
  4432. sysfs_remove_group(&sdfat_kset->kobj, &attr_group);
  4433. kset_unregister(sdfat_kset);
  4434. sdfat_kset = NULL;
  4435. }
  4436. sdfat_destroy_inodecache();
  4437. unregister_filesystem(&sdfat_fs_type);
  4438. #ifdef CONFIG_SDFAT_USE_FOR_EXFAT
  4439. unregister_filesystem(&exfat_fs_type);
  4440. #endif /* CONFIG_SDFAT_USE_FOR_EXFAT */
  4441. #ifdef CONFIG_SDFAT_USE_FOR_VFAT
  4442. unregister_filesystem(&vfat_fs_type);
  4443. #endif /* CONFIG_SDFAT_USE_FOR_VFAT */
  4444. fsapi_shutdown();
  4445. }
  4446. module_init(init_sdfat_fs);
  4447. module_exit(exit_sdfat_fs);
  4448. MODULE_LICENSE("GPL");
  4449. MODULE_DESCRIPTION("FAT/exFAT filesystem support");
  4450. MODULE_AUTHOR("Samsung Electronics Co., Ltd.");