yaffs_guts.c 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010
  1. /*
  2. * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2011 Aleph One Ltd.
  5. * for Toby Churchill Ltd and Brightstar Engineering
  6. *
  7. * Created by Charles Manning <charles@aleph1.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include "yportenv.h"
  14. #include "yaffs_trace.h"
  15. #include "yaffs_guts.h"
  16. #include "yaffs_getblockinfo.h"
  17. #include "yaffs_tagscompat.h"
  18. #include "yaffs_nand.h"
  19. #include "yaffs_yaffs1.h"
  20. #include "yaffs_yaffs2.h"
  21. #include "yaffs_bitmap.h"
  22. #include "yaffs_verify.h"
  23. #include "yaffs_nand.h"
  24. #include "yaffs_packedtags2.h"
  25. #include "yaffs_nameval.h"
  26. #include "yaffs_allocator.h"
  27. #include "yaffs_attribs.h"
  28. #include "yaffs_summary.h"
  29. /* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */
  30. #define YAFFS_GC_GOOD_ENOUGH 2
  31. #define YAFFS_GC_PASSIVE_THRESHOLD 4
  32. #include "yaffs_ecc.h"
  33. /* Forward declarations */
  34. static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
  35. const u8 *buffer, int n_bytes, int use_reserve);
  36. /* Function to calculate chunk and offset */
  37. void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
  38. int *chunk_out, u32 *offset_out)
  39. {
  40. int chunk;
  41. u32 offset;
  42. chunk = (u32) (addr >> dev->chunk_shift);
  43. if (dev->chunk_div == 1) {
  44. /* easy power of 2 case */
  45. offset = (u32) (addr & dev->chunk_mask);
  46. } else {
  47. /* Non power-of-2 case */
  48. loff_t chunk_base;
  49. chunk /= dev->chunk_div;
  50. chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk;
  51. offset = (u32) (addr - chunk_base);
  52. }
  53. *chunk_out = chunk;
  54. *offset_out = offset;
  55. }
  56. /* Function to return the number of shifts for a power of 2 greater than or
  57. * equal to the given number
  58. * Note we don't try to cater for all possible numbers and this does not have to
  59. * be hellishly efficient.
  60. */
  61. static inline u32 calc_shifts_ceiling(u32 x)
  62. {
  63. int extra_bits;
  64. int shifts;
  65. shifts = extra_bits = 0;
  66. while (x > 1) {
  67. if (x & 1)
  68. extra_bits++;
  69. x >>= 1;
  70. shifts++;
  71. }
  72. if (extra_bits)
  73. shifts++;
  74. return shifts;
  75. }
  76. /* Function to return the number of shifts to get a 1 in bit 0
  77. */
  78. static inline u32 calc_shifts(u32 x)
  79. {
  80. u32 shifts;
  81. shifts = 0;
  82. if (!x)
  83. return 0;
  84. while (!(x & 1)) {
  85. x >>= 1;
  86. shifts++;
  87. }
  88. return shifts;
  89. }
  90. /*
  91. * Temporary buffer manipulations.
  92. */
  93. static int yaffs_init_tmp_buffers(struct yaffs_dev *dev)
  94. {
  95. int i;
  96. u8 *buf = (u8 *) 1;
  97. memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
  98. for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
  99. dev->temp_buffer[i].in_use = 0;
  100. buf = kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
  101. dev->temp_buffer[i].buffer = buf;
  102. }
  103. return buf ? YAFFS_OK : YAFFS_FAIL;
  104. }
  105. u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev)
  106. {
  107. int i;
  108. dev->temp_in_use++;
  109. if (dev->temp_in_use > dev->max_temp)
  110. dev->max_temp = dev->temp_in_use;
  111. for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  112. if (dev->temp_buffer[i].in_use == 0) {
  113. dev->temp_buffer[i].in_use = 1;
  114. return dev->temp_buffer[i].buffer;
  115. }
  116. }
  117. yaffs_trace(YAFFS_TRACE_BUFFERS, "Out of temp buffers");
  118. /*
  119. * If we got here then we have to allocate an unmanaged one
  120. * This is not good.
  121. */
  122. dev->unmanaged_buffer_allocs++;
  123. return kmalloc(dev->data_bytes_per_chunk, GFP_NOFS);
  124. }
  125. void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 *buffer)
  126. {
  127. int i;
  128. dev->temp_in_use--;
  129. for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  130. if (dev->temp_buffer[i].buffer == buffer) {
  131. dev->temp_buffer[i].in_use = 0;
  132. return;
  133. }
  134. }
  135. if (buffer) {
  136. /* assume it is an unmanaged one. */
  137. yaffs_trace(YAFFS_TRACE_BUFFERS,
  138. "Releasing unmanaged temp buffer");
  139. kfree(buffer);
  140. dev->unmanaged_buffer_deallocs++;
  141. }
  142. }
  143. /*
  144. * Determine if we have a managed buffer.
  145. */
  146. int yaffs_is_managed_tmp_buffer(struct yaffs_dev *dev, const u8 *buffer)
  147. {
  148. int i;
  149. for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
  150. if (dev->temp_buffer[i].buffer == buffer)
  151. return 1;
  152. }
  153. for (i = 0; i < dev->param.n_caches; i++) {
  154. if (dev->cache[i].data == buffer)
  155. return 1;
  156. }
  157. if (buffer == dev->checkpt_buffer)
  158. return 1;
  159. yaffs_trace(YAFFS_TRACE_ALWAYS,
  160. "yaffs: unmaged buffer detected.");
  161. return 0;
  162. }
  163. /*
  164. * Functions for robustisizing TODO
  165. *
  166. */
  167. static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
  168. const u8 *data,
  169. const struct yaffs_ext_tags *tags)
  170. {
  171. }
  172. static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
  173. const struct yaffs_ext_tags *tags)
  174. {
  175. }
  176. void yaffs_handle_chunk_error(struct yaffs_dev *dev,
  177. struct yaffs_block_info *bi)
  178. {
  179. if (!bi->gc_prioritise) {
  180. bi->gc_prioritise = 1;
  181. dev->has_pending_prioritised_gc = 1;
  182. bi->chunk_error_strikes++;
  183. if (bi->chunk_error_strikes > 3) {
  184. bi->needs_retiring = 1; /* Too many stikes, so retire */
  185. yaffs_trace(YAFFS_TRACE_ALWAYS,
  186. "yaffs: Block struck out");
  187. }
  188. }
  189. }
  190. static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
  191. int erased_ok)
  192. {
  193. int flash_block = nand_chunk / dev->param.chunks_per_block;
  194. struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
  195. yaffs_handle_chunk_error(dev, bi);
  196. if (erased_ok) {
  197. /* Was an actual write failure,
  198. * so mark the block for retirement.*/
  199. bi->needs_retiring = 1;
  200. yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  201. "**>> Block %d needs retiring", flash_block);
  202. }
  203. /* Delete the chunk */
  204. yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
  205. yaffs_skip_rest_of_block(dev);
  206. }
  207. /*
  208. * Verification code
  209. */
  210. /*
  211. * Simple hash function. Needs to have a reasonable spread
  212. */
  213. static inline int yaffs_hash_fn(int n)
  214. {
  215. if (n < 0)
  216. n = -n;
  217. return n % YAFFS_NOBJECT_BUCKETS;
  218. }
  219. /*
  220. * Access functions to useful fake objects.
  221. * Note that root might have a presence in NAND if permissions are set.
  222. */
  223. struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
  224. {
  225. return dev->root_dir;
  226. }
  227. struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
  228. {
  229. return dev->lost_n_found;
  230. }
  231. /*
  232. * Erased NAND checking functions
  233. */
  234. int yaffs_check_ff(u8 *buffer, int n_bytes)
  235. {
  236. /* Horrible, slow implementation */
  237. while (n_bytes--) {
  238. if (*buffer != 0xff)
  239. return 0;
  240. buffer++;
  241. }
  242. return 1;
  243. }
  244. static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk)
  245. {
  246. int retval = YAFFS_OK;
  247. u8 *data = yaffs_get_temp_buffer(dev);
  248. struct yaffs_ext_tags tags;
  249. yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
  250. if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
  251. retval = YAFFS_FAIL;
  252. if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) ||
  253. tags.chunk_used) {
  254. yaffs_trace(YAFFS_TRACE_NANDACCESS,
  255. "Chunk %d not erased", nand_chunk);
  256. retval = YAFFS_FAIL;
  257. }
  258. yaffs_release_temp_buffer(dev, data);
  259. return retval;
  260. }
  261. static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
  262. int nand_chunk,
  263. const u8 *data,
  264. struct yaffs_ext_tags *tags)
  265. {
  266. int retval = YAFFS_OK;
  267. struct yaffs_ext_tags temp_tags;
  268. u8 *buffer = yaffs_get_temp_buffer(dev);
  269. yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags);
  270. if (memcmp(buffer, data, dev->data_bytes_per_chunk) ||
  271. temp_tags.obj_id != tags->obj_id ||
  272. temp_tags.chunk_id != tags->chunk_id ||
  273. temp_tags.n_bytes != tags->n_bytes)
  274. retval = YAFFS_FAIL;
  275. yaffs_release_temp_buffer(dev, buffer);
  276. return retval;
  277. }
  278. int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
  279. {
  280. int reserved_chunks;
  281. int reserved_blocks = dev->param.n_reserved_blocks;
  282. int checkpt_blocks;
  283. checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
  284. reserved_chunks =
  285. (reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block;
  286. return (dev->n_free_chunks > (reserved_chunks + n_chunks));
  287. }
  288. static int yaffs_find_alloc_block(struct yaffs_dev *dev)
  289. {
  290. int i;
  291. struct yaffs_block_info *bi;
  292. if (dev->n_erased_blocks < 1) {
  293. /* Hoosterman we've got a problem.
  294. * Can't get space to gc
  295. */
  296. yaffs_trace(YAFFS_TRACE_ERROR,
  297. "yaffs tragedy: no more erased blocks");
  298. return -1;
  299. }
  300. /* Find an empty block. */
  301. for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
  302. dev->alloc_block_finder++;
  303. if (dev->alloc_block_finder < dev->internal_start_block
  304. || dev->alloc_block_finder > dev->internal_end_block) {
  305. dev->alloc_block_finder = dev->internal_start_block;
  306. }
  307. bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
  308. if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
  309. bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
  310. dev->seq_number++;
  311. bi->seq_number = dev->seq_number;
  312. dev->n_erased_blocks--;
  313. yaffs_trace(YAFFS_TRACE_ALLOCATE,
  314. "Allocated block %d, seq %d, %d left" ,
  315. dev->alloc_block_finder, dev->seq_number,
  316. dev->n_erased_blocks);
  317. return dev->alloc_block_finder;
  318. }
  319. }
  320. yaffs_trace(YAFFS_TRACE_ALWAYS,
  321. "yaffs tragedy: no more erased blocks, but there should have been %d",
  322. dev->n_erased_blocks);
  323. return -1;
  324. }
  325. static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
  326. struct yaffs_block_info **block_ptr)
  327. {
  328. int ret_val;
  329. struct yaffs_block_info *bi;
  330. if (dev->alloc_block < 0) {
  331. /* Get next block to allocate off */
  332. dev->alloc_block = yaffs_find_alloc_block(dev);
  333. dev->alloc_page = 0;
  334. }
  335. if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
  336. /* No space unless we're allowed to use the reserve. */
  337. return -1;
  338. }
  339. if (dev->n_erased_blocks < dev->param.n_reserved_blocks
  340. && dev->alloc_page == 0)
  341. yaffs_trace(YAFFS_TRACE_ALLOCATE, "Allocating reserve");
  342. /* Next page please.... */
  343. if (dev->alloc_block >= 0) {
  344. bi = yaffs_get_block_info(dev, dev->alloc_block);
  345. ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
  346. dev->alloc_page;
  347. bi->pages_in_use++;
  348. yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page);
  349. dev->alloc_page++;
  350. dev->n_free_chunks--;
  351. /* If the block is full set the state to full */
  352. if (dev->alloc_page >= dev->param.chunks_per_block) {
  353. bi->block_state = YAFFS_BLOCK_STATE_FULL;
  354. dev->alloc_block = -1;
  355. }
  356. if (block_ptr)
  357. *block_ptr = bi;
  358. return ret_val;
  359. }
  360. yaffs_trace(YAFFS_TRACE_ERROR,
  361. "!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!");
  362. return -1;
  363. }
  364. static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
  365. {
  366. int n;
  367. n = dev->n_erased_blocks * dev->param.chunks_per_block;
  368. if (dev->alloc_block > 0)
  369. n += (dev->param.chunks_per_block - dev->alloc_page);
  370. return n;
  371. }
  372. /*
  373. * yaffs_skip_rest_of_block() skips over the rest of the allocation block
  374. * if we don't want to write to it.
  375. */
  376. void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
  377. {
  378. struct yaffs_block_info *bi;
  379. if (dev->alloc_block > 0) {
  380. bi = yaffs_get_block_info(dev, dev->alloc_block);
  381. if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
  382. bi->block_state = YAFFS_BLOCK_STATE_FULL;
  383. dev->alloc_block = -1;
  384. }
  385. }
  386. }
  387. static int yaffs_write_new_chunk(struct yaffs_dev *dev,
  388. const u8 *data,
  389. struct yaffs_ext_tags *tags, int use_reserver)
  390. {
  391. int attempts = 0;
  392. int write_ok = 0;
  393. int chunk;
  394. yaffs2_checkpt_invalidate(dev);
  395. do {
  396. struct yaffs_block_info *bi = 0;
  397. int erased_ok = 0;
  398. chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
  399. if (chunk < 0) {
  400. /* no space */
  401. break;
  402. }
  403. /* First check this chunk is erased, if it needs
  404. * checking. The checking policy (unless forced
  405. * always on) is as follows:
  406. *
  407. * Check the first page we try to write in a block.
  408. * If the check passes then we don't need to check any
  409. * more. If the check fails, we check again...
  410. * If the block has been erased, we don't need to check.
  411. *
  412. * However, if the block has been prioritised for gc,
  413. * then we think there might be something odd about
  414. * this block and stop using it.
  415. *
  416. * Rationale: We should only ever see chunks that have
  417. * not been erased if there was a partially written
  418. * chunk due to power loss. This checking policy should
  419. * catch that case with very few checks and thus save a
  420. * lot of checks that are most likely not needed.
  421. *
  422. * Mods to the above
  423. * If an erase check fails or the write fails we skip the
  424. * rest of the block.
  425. */
  426. /* let's give it a try */
  427. attempts++;
  428. if (dev->param.always_check_erased)
  429. bi->skip_erased_check = 0;
  430. if (!bi->skip_erased_check) {
  431. erased_ok = yaffs_check_chunk_erased(dev, chunk);
  432. if (erased_ok != YAFFS_OK) {
  433. yaffs_trace(YAFFS_TRACE_ERROR,
  434. "**>> yaffs chunk %d was not erased",
  435. chunk);
  436. /* If not erased, delete this one,
  437. * skip rest of block and
  438. * try another chunk */
  439. yaffs_chunk_del(dev, chunk, 1, __LINE__);
  440. yaffs_skip_rest_of_block(dev);
  441. continue;
  442. }
  443. }
  444. write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags);
  445. if (!bi->skip_erased_check)
  446. write_ok =
  447. yaffs_verify_chunk_written(dev, chunk, data, tags);
  448. if (write_ok != YAFFS_OK) {
  449. /* Clean up aborted write, skip to next block and
  450. * try another chunk */
  451. yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
  452. continue;
  453. }
  454. bi->skip_erased_check = 1;
  455. /* Copy the data into the robustification buffer */
  456. yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
  457. } while (write_ok != YAFFS_OK &&
  458. (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
  459. if (!write_ok)
  460. chunk = -1;
  461. if (attempts > 1) {
  462. yaffs_trace(YAFFS_TRACE_ERROR,
  463. "**>> yaffs write required %d attempts",
  464. attempts);
  465. dev->n_retried_writes += (attempts - 1);
  466. }
  467. return chunk;
  468. }
  469. /*
  470. * Block retiring for handling a broken block.
  471. */
  472. static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block)
  473. {
  474. struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
  475. yaffs2_checkpt_invalidate(dev);
  476. yaffs2_clear_oldest_dirty_seq(dev, bi);
  477. if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
  478. if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
  479. yaffs_trace(YAFFS_TRACE_ALWAYS,
  480. "yaffs: Failed to mark bad and erase block %d",
  481. flash_block);
  482. } else {
  483. struct yaffs_ext_tags tags;
  484. int chunk_id =
  485. flash_block * dev->param.chunks_per_block;
  486. u8 *buffer = yaffs_get_temp_buffer(dev);
  487. memset(buffer, 0xff, dev->data_bytes_per_chunk);
  488. memset(&tags, 0, sizeof(tags));
  489. tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
  490. if (dev->param.write_chunk_tags_fn(dev, chunk_id -
  491. dev->chunk_offset,
  492. buffer,
  493. &tags) != YAFFS_OK)
  494. yaffs_trace(YAFFS_TRACE_ALWAYS,
  495. "yaffs: Failed to write bad block marker to block %d",
  496. flash_block);
  497. yaffs_release_temp_buffer(dev, buffer);
  498. }
  499. }
  500. bi->block_state = YAFFS_BLOCK_STATE_DEAD;
  501. bi->gc_prioritise = 0;
  502. bi->needs_retiring = 0;
  503. dev->n_retired_blocks++;
  504. }
  505. /*---------------- Name handling functions ------------*/
  506. static u16 yaffs_calc_name_sum(const YCHAR *name)
  507. {
  508. u16 sum = 0;
  509. u16 i = 1;
  510. if (!name)
  511. return 0;
  512. while ((*name) && i < (YAFFS_MAX_NAME_LENGTH / 2)) {
  513. /* 0x1f mask is case insensitive */
  514. sum += ((*name) & 0x1f) * i;
  515. i++;
  516. name++;
  517. }
  518. return sum;
  519. }
  520. void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name)
  521. {
  522. memset(obj->short_name, 0, sizeof(obj->short_name));
  523. if (name &&
  524. yaffs_strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <=
  525. YAFFS_SHORT_NAME_LENGTH)
  526. yaffs_strcpy(obj->short_name, name);
  527. else
  528. obj->short_name[0] = _Y('\0');
  529. obj->sum = yaffs_calc_name_sum(name);
  530. }
  531. void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
  532. const struct yaffs_obj_hdr *oh)
  533. {
  534. #ifdef CONFIG_YAFFS_AUTO_UNICODE
  535. YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1];
  536. memset(tmp_name, 0, sizeof(tmp_name));
  537. yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name,
  538. YAFFS_MAX_NAME_LENGTH + 1);
  539. yaffs_set_obj_name(obj, tmp_name);
  540. #else
  541. yaffs_set_obj_name(obj, oh->name);
  542. #endif
  543. }
  544. loff_t yaffs_max_file_size(struct yaffs_dev *dev)
  545. {
  546. return ((loff_t) YAFFS_MAX_CHUNK_ID) * dev->data_bytes_per_chunk;
  547. }
  548. /*-------------------- TNODES -------------------
  549. * List of spare tnodes
  550. * The list is hooked together using the first pointer
  551. * in the tnode.
  552. */
  553. struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
  554. {
  555. struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
  556. if (tn) {
  557. memset(tn, 0, dev->tnode_size);
  558. dev->n_tnodes++;
  559. }
  560. dev->checkpoint_blocks_required = 0; /* force recalculation */
  561. return tn;
  562. }
  563. /* FreeTnode frees up a tnode and puts it back on the free list */
  564. static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
  565. {
  566. yaffs_free_raw_tnode(dev, tn);
  567. dev->n_tnodes--;
  568. dev->checkpoint_blocks_required = 0; /* force recalculation */
  569. }
  570. static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
  571. {
  572. yaffs_deinit_raw_tnodes_and_objs(dev);
  573. dev->n_obj = 0;
  574. dev->n_tnodes = 0;
  575. }
  576. void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn,
  577. unsigned pos, unsigned val)
  578. {
  579. u32 *map = (u32 *) tn;
  580. u32 bit_in_map;
  581. u32 bit_in_word;
  582. u32 word_in_map;
  583. u32 mask;
  584. pos &= YAFFS_TNODES_LEVEL0_MASK;
  585. val >>= dev->chunk_grp_bits;
  586. bit_in_map = pos * dev->tnode_width;
  587. word_in_map = bit_in_map / 32;
  588. bit_in_word = bit_in_map & (32 - 1);
  589. mask = dev->tnode_mask << bit_in_word;
  590. map[word_in_map] &= ~mask;
  591. map[word_in_map] |= (mask & (val << bit_in_word));
  592. if (dev->tnode_width > (32 - bit_in_word)) {
  593. bit_in_word = (32 - bit_in_word);
  594. word_in_map++;
  595. mask =
  596. dev->tnode_mask >> bit_in_word;
  597. map[word_in_map] &= ~mask;
  598. map[word_in_map] |= (mask & (val >> bit_in_word));
  599. }
  600. }
  601. u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
  602. unsigned pos)
  603. {
  604. u32 *map = (u32 *) tn;
  605. u32 bit_in_map;
  606. u32 bit_in_word;
  607. u32 word_in_map;
  608. u32 val;
  609. pos &= YAFFS_TNODES_LEVEL0_MASK;
  610. bit_in_map = pos * dev->tnode_width;
  611. word_in_map = bit_in_map / 32;
  612. bit_in_word = bit_in_map & (32 - 1);
  613. val = map[word_in_map] >> bit_in_word;
  614. if (dev->tnode_width > (32 - bit_in_word)) {
  615. bit_in_word = (32 - bit_in_word);
  616. word_in_map++;
  617. val |= (map[word_in_map] << bit_in_word);
  618. }
  619. val &= dev->tnode_mask;
  620. val <<= dev->chunk_grp_bits;
  621. return val;
  622. }
  623. /* ------------------- End of individual tnode manipulation -----------------*/
  624. /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
  625. * The look up tree is represented by the top tnode and the number of top_level
  626. * in the tree. 0 means only the level 0 tnode is in the tree.
  627. */
  628. /* FindLevel0Tnode finds the level 0 tnode, if one exists. */
  629. struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
  630. struct yaffs_file_var *file_struct,
  631. u32 chunk_id)
  632. {
  633. struct yaffs_tnode *tn = file_struct->top;
  634. u32 i;
  635. int required_depth;
  636. int level = file_struct->top_level;
  637. /* Check sane level and chunk Id */
  638. if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
  639. return NULL;
  640. if (chunk_id > YAFFS_MAX_CHUNK_ID)
  641. return NULL;
  642. /* First check we're tall enough (ie enough top_level) */
  643. i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
  644. required_depth = 0;
  645. while (i) {
  646. i >>= YAFFS_TNODES_INTERNAL_BITS;
  647. required_depth++;
  648. }
  649. if (required_depth > file_struct->top_level)
  650. return NULL; /* Not tall enough, so we can't find it */
  651. /* Traverse down to level 0 */
  652. while (level > 0 && tn) {
  653. tn = tn->internal[(chunk_id >>
  654. (YAFFS_TNODES_LEVEL0_BITS +
  655. (level - 1) *
  656. YAFFS_TNODES_INTERNAL_BITS)) &
  657. YAFFS_TNODES_INTERNAL_MASK];
  658. level--;
  659. }
  660. return tn;
  661. }
  662. /* add_find_tnode_0 finds the level 0 tnode if it exists,
  663. * otherwise first expands the tree.
  664. * This happens in two steps:
  665. * 1. If the tree isn't tall enough, then make it taller.
  666. * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
  667. *
  668. * Used when modifying the tree.
  669. *
  670. * If the tn argument is NULL, then a fresh tnode will be added otherwise the
  671. * specified tn will be plugged into the ttree.
  672. */
  673. struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
  674. struct yaffs_file_var *file_struct,
  675. u32 chunk_id,
  676. struct yaffs_tnode *passed_tn)
  677. {
  678. int required_depth;
  679. int i;
  680. int l;
  681. struct yaffs_tnode *tn;
  682. u32 x;
  683. /* Check sane level and page Id */
  684. if (file_struct->top_level < 0 ||
  685. file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
  686. return NULL;
  687. if (chunk_id > YAFFS_MAX_CHUNK_ID)
  688. return NULL;
  689. /* First check we're tall enough (ie enough top_level) */
  690. x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
  691. required_depth = 0;
  692. while (x) {
  693. x >>= YAFFS_TNODES_INTERNAL_BITS;
  694. required_depth++;
  695. }
  696. if (required_depth > file_struct->top_level) {
  697. /* Not tall enough, gotta make the tree taller */
  698. for (i = file_struct->top_level; i < required_depth; i++) {
  699. tn = yaffs_get_tnode(dev);
  700. if (tn) {
  701. tn->internal[0] = file_struct->top;
  702. file_struct->top = tn;
  703. file_struct->top_level++;
  704. } else {
  705. yaffs_trace(YAFFS_TRACE_ERROR,
  706. "yaffs: no more tnodes");
  707. return NULL;
  708. }
  709. }
  710. }
  711. /* Traverse down to level 0, adding anything we need */
  712. l = file_struct->top_level;
  713. tn = file_struct->top;
  714. if (l > 0) {
  715. while (l > 0 && tn) {
  716. x = (chunk_id >>
  717. (YAFFS_TNODES_LEVEL0_BITS +
  718. (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
  719. YAFFS_TNODES_INTERNAL_MASK;
  720. if ((l > 1) && !tn->internal[x]) {
  721. /* Add missing non-level-zero tnode */
  722. tn->internal[x] = yaffs_get_tnode(dev);
  723. if (!tn->internal[x])
  724. return NULL;
  725. } else if (l == 1) {
  726. /* Looking from level 1 at level 0 */
  727. if (passed_tn) {
  728. /* If we already have one, release it */
  729. if (tn->internal[x])
  730. yaffs_free_tnode(dev,
  731. tn->internal[x]);
  732. tn->internal[x] = passed_tn;
  733. } else if (!tn->internal[x]) {
  734. /* Don't have one, none passed in */
  735. tn->internal[x] = yaffs_get_tnode(dev);
  736. if (!tn->internal[x])
  737. return NULL;
  738. }
  739. }
  740. tn = tn->internal[x];
  741. l--;
  742. }
  743. } else {
  744. /* top is level 0 */
  745. if (passed_tn) {
  746. memcpy(tn, passed_tn,
  747. (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8);
  748. yaffs_free_tnode(dev, passed_tn);
  749. }
  750. }
  751. return tn;
  752. }
  753. static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
  754. int chunk_obj)
  755. {
  756. return (tags->chunk_id == chunk_obj &&
  757. tags->obj_id == obj_id &&
  758. !tags->is_deleted) ? 1 : 0;
  759. }
  760. static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
  761. struct yaffs_ext_tags *tags, int obj_id,
  762. int inode_chunk)
  763. {
  764. int j;
  765. for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
  766. if (yaffs_check_chunk_bit
  767. (dev, the_chunk / dev->param.chunks_per_block,
  768. the_chunk % dev->param.chunks_per_block)) {
  769. if (dev->chunk_grp_size == 1)
  770. return the_chunk;
  771. else {
  772. yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
  773. tags);
  774. if (yaffs_tags_match(tags,
  775. obj_id, inode_chunk)) {
  776. /* found it; */
  777. return the_chunk;
  778. }
  779. }
  780. }
  781. the_chunk++;
  782. }
  783. return -1;
  784. }
  785. static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
  786. struct yaffs_ext_tags *tags)
  787. {
  788. /*Get the Tnode, then get the level 0 offset chunk offset */
  789. struct yaffs_tnode *tn;
  790. int the_chunk = -1;
  791. struct yaffs_ext_tags local_tags;
  792. int ret_val = -1;
  793. struct yaffs_dev *dev = in->my_dev;
  794. if (!tags) {
  795. /* Passed a NULL, so use our own tags space */
  796. tags = &local_tags;
  797. }
  798. tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
  799. if (!tn)
  800. return ret_val;
  801. the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
  802. ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
  803. inode_chunk);
  804. return ret_val;
  805. }
  806. static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
  807. struct yaffs_ext_tags *tags)
  808. {
  809. /* Get the Tnode, then get the level 0 offset chunk offset */
  810. struct yaffs_tnode *tn;
  811. int the_chunk = -1;
  812. struct yaffs_ext_tags local_tags;
  813. struct yaffs_dev *dev = in->my_dev;
  814. int ret_val = -1;
  815. if (!tags) {
  816. /* Passed a NULL, so use our own tags space */
  817. tags = &local_tags;
  818. }
  819. tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
  820. if (!tn)
  821. return ret_val;
  822. the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
  823. ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
  824. inode_chunk);
  825. /* Delete the entry in the filestructure (if found) */
  826. if (ret_val != -1)
  827. yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
  828. return ret_val;
  829. }
  830. int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
  831. int nand_chunk, int in_scan)
  832. {
  833. /* NB in_scan is zero unless scanning.
  834. * For forward scanning, in_scan is > 0;
  835. * for backward scanning in_scan is < 0
  836. *
  837. * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
  838. */
  839. struct yaffs_tnode *tn;
  840. struct yaffs_dev *dev = in->my_dev;
  841. int existing_cunk;
  842. struct yaffs_ext_tags existing_tags;
  843. struct yaffs_ext_tags new_tags;
  844. unsigned existing_serial, new_serial;
  845. if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
  846. /* Just ignore an attempt at putting a chunk into a non-file
  847. * during scanning.
  848. * If it is not during Scanning then something went wrong!
  849. */
  850. if (!in_scan) {
  851. yaffs_trace(YAFFS_TRACE_ERROR,
  852. "yaffs tragedy:attempt to put data chunk into a non-file"
  853. );
  854. BUG();
  855. }
  856. yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
  857. return YAFFS_OK;
  858. }
  859. tn = yaffs_add_find_tnode_0(dev,
  860. &in->variant.file_variant,
  861. inode_chunk, NULL);
  862. if (!tn)
  863. return YAFFS_FAIL;
  864. if (!nand_chunk)
  865. /* Dummy insert, bail now */
  866. return YAFFS_OK;
  867. existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
  868. if (in_scan != 0) {
  869. /* If we're scanning then we need to test for duplicates
  870. * NB This does not need to be efficient since it should only
  871. * happen when the power fails during a write, then only one
  872. * chunk should ever be affected.
  873. *
  874. * Correction for YAFFS2: This could happen quite a lot and we
  875. * need to think about efficiency! TODO
  876. * Update: For backward scanning we don't need to re-read tags
  877. * so this is quite cheap.
  878. */
  879. if (existing_cunk > 0) {
  880. /* NB Right now existing chunk will not be real
  881. * chunk_id if the chunk group size > 1
  882. * thus we have to do a FindChunkInFile to get the
  883. * real chunk id.
  884. *
  885. * We have a duplicate now we need to decide which
  886. * one to use:
  887. *
  888. * Backwards scanning YAFFS2: The old one is what
  889. * we use, dump the new one.
  890. * YAFFS1: Get both sets of tags and compare serial
  891. * numbers.
  892. */
  893. if (in_scan > 0) {
  894. /* Only do this for forward scanning */
  895. yaffs_rd_chunk_tags_nand(dev,
  896. nand_chunk,
  897. NULL, &new_tags);
  898. /* Do a proper find */
  899. existing_cunk =
  900. yaffs_find_chunk_in_file(in, inode_chunk,
  901. &existing_tags);
  902. }
  903. if (existing_cunk <= 0) {
  904. /*Hoosterman - how did this happen? */
  905. yaffs_trace(YAFFS_TRACE_ERROR,
  906. "yaffs tragedy: existing chunk < 0 in scan"
  907. );
  908. }
  909. /* NB The deleted flags should be false, otherwise
  910. * the chunks will not be loaded during a scan
  911. */
  912. if (in_scan > 0) {
  913. new_serial = new_tags.serial_number;
  914. existing_serial = existing_tags.serial_number;
  915. }
  916. if ((in_scan > 0) &&
  917. (existing_cunk <= 0 ||
  918. ((existing_serial + 1) & 3) == new_serial)) {
  919. /* Forward scanning.
  920. * Use new
  921. * Delete the old one and drop through to
  922. * update the tnode
  923. */
  924. yaffs_chunk_del(dev, existing_cunk, 1,
  925. __LINE__);
  926. } else {
  927. /* Backward scanning or we want to use the
  928. * existing one
  929. * Delete the new one and return early so that
  930. * the tnode isn't changed
  931. */
  932. yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
  933. return YAFFS_OK;
  934. }
  935. }
  936. }
  937. if (existing_cunk == 0)
  938. in->n_data_chunks++;
  939. yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
  940. return YAFFS_OK;
  941. }
  942. static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
  943. {
  944. struct yaffs_block_info *the_block;
  945. unsigned block_no;
  946. yaffs_trace(YAFFS_TRACE_DELETION, "soft delete chunk %d", chunk);
  947. block_no = chunk / dev->param.chunks_per_block;
  948. the_block = yaffs_get_block_info(dev, block_no);
  949. if (the_block) {
  950. the_block->soft_del_pages++;
  951. dev->n_free_chunks++;
  952. yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
  953. }
  954. }
  955. /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all
  956. * the chunks in the file.
  957. * All soft deleting does is increment the block's softdelete count and pulls
  958. * the chunk out of the tnode.
  959. * Thus, essentially this is the same as DeleteWorker except that the chunks
  960. * are soft deleted.
  961. */
  962. static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
  963. u32 level, int chunk_offset)
  964. {
  965. int i;
  966. int the_chunk;
  967. int all_done = 1;
  968. struct yaffs_dev *dev = in->my_dev;
  969. if (!tn)
  970. return 1;
  971. if (level > 0) {
  972. for (i = YAFFS_NTNODES_INTERNAL - 1;
  973. all_done && i >= 0;
  974. i--) {
  975. if (tn->internal[i]) {
  976. all_done =
  977. yaffs_soft_del_worker(in,
  978. tn->internal[i],
  979. level - 1,
  980. (chunk_offset <<
  981. YAFFS_TNODES_INTERNAL_BITS)
  982. + i);
  983. if (all_done) {
  984. yaffs_free_tnode(dev,
  985. tn->internal[i]);
  986. tn->internal[i] = NULL;
  987. } else {
  988. /* Can this happen? */
  989. }
  990. }
  991. }
  992. return (all_done) ? 1 : 0;
  993. }
  994. /* level 0 */
  995. for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
  996. the_chunk = yaffs_get_group_base(dev, tn, i);
  997. if (the_chunk) {
  998. yaffs_soft_del_chunk(dev, the_chunk);
  999. yaffs_load_tnode_0(dev, tn, i, 0);
  1000. }
  1001. }
  1002. return 1;
  1003. }
  1004. static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
  1005. {
  1006. struct yaffs_dev *dev = obj->my_dev;
  1007. struct yaffs_obj *parent;
  1008. yaffs_verify_obj_in_dir(obj);
  1009. parent = obj->parent;
  1010. yaffs_verify_dir(parent);
  1011. if (dev && dev->param.remove_obj_fn)
  1012. dev->param.remove_obj_fn(obj);
  1013. list_del_init(&obj->siblings);
  1014. obj->parent = NULL;
  1015. yaffs_verify_dir(parent);
  1016. }
  1017. void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj)
  1018. {
  1019. if (!directory) {
  1020. yaffs_trace(YAFFS_TRACE_ALWAYS,
  1021. "tragedy: Trying to add an object to a null pointer directory"
  1022. );
  1023. BUG();
  1024. return;
  1025. }
  1026. if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  1027. yaffs_trace(YAFFS_TRACE_ALWAYS,
  1028. "tragedy: Trying to add an object to a non-directory"
  1029. );
  1030. BUG();
  1031. }
  1032. if (obj->siblings.prev == NULL) {
  1033. /* Not initialised */
  1034. BUG();
  1035. }
  1036. yaffs_verify_dir(directory);
  1037. yaffs_remove_obj_from_dir(obj);
  1038. /* Now add it */
  1039. list_add(&obj->siblings, &directory->variant.dir_variant.children);
  1040. obj->parent = directory;
  1041. if (directory == obj->my_dev->unlinked_dir
  1042. || directory == obj->my_dev->del_dir) {
  1043. obj->unlinked = 1;
  1044. obj->my_dev->n_unlinked_files++;
  1045. obj->rename_allowed = 0;
  1046. }
  1047. yaffs_verify_dir(directory);
  1048. yaffs_verify_obj_in_dir(obj);
  1049. }
  1050. static int yaffs_change_obj_name(struct yaffs_obj *obj,
  1051. struct yaffs_obj *new_dir,
  1052. const YCHAR *new_name, int force, int shadows)
  1053. {
  1054. int unlink_op;
  1055. int del_op;
  1056. struct yaffs_obj *existing_target;
  1057. if (new_dir == NULL)
  1058. new_dir = obj->parent; /* use the old directory */
  1059. if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  1060. yaffs_trace(YAFFS_TRACE_ALWAYS,
  1061. "tragedy: yaffs_change_obj_name: new_dir is not a directory"
  1062. );
  1063. BUG();
  1064. }
  1065. unlink_op = (new_dir == obj->my_dev->unlinked_dir);
  1066. del_op = (new_dir == obj->my_dev->del_dir);
  1067. existing_target = yaffs_find_by_name(new_dir, new_name);
  1068. /* If the object is a file going into the unlinked directory,
  1069. * then it is OK to just stuff it in since duplicate names are OK.
  1070. * else only proceed if the new name does not exist and we're putting
  1071. * it into a directory.
  1072. */
  1073. if (!(unlink_op || del_op || force ||
  1074. shadows > 0 || !existing_target) ||
  1075. new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
  1076. return YAFFS_FAIL;
  1077. yaffs_set_obj_name(obj, new_name);
  1078. obj->dirty = 1;
  1079. yaffs_add_obj_to_dir(new_dir, obj);
  1080. if (unlink_op)
  1081. obj->unlinked = 1;
  1082. /* If it is a deletion then we mark it as a shrink for gc */
  1083. if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >= 0)
  1084. return YAFFS_OK;
  1085. return YAFFS_FAIL;
  1086. }
  1087. /*------------------------ Short Operations Cache ------------------------------
  1088. * In many situations where there is no high level buffering a lot of
  1089. * reads might be short sequential reads, and a lot of writes may be short
  1090. * sequential writes. eg. scanning/writing a jpeg file.
  1091. * In these cases, a short read/write cache can provide a huge perfomance
  1092. * benefit with dumb-as-a-rock code.
  1093. * In Linux, the page cache provides read buffering and the short op cache
  1094. * provides write buffering.
  1095. *
  1096. * There are a small number (~10) of cache chunks per device so that we don't
  1097. * need a very intelligent search.
  1098. */
  1099. static int yaffs_obj_cache_dirty(struct yaffs_obj *obj)
  1100. {
  1101. struct yaffs_dev *dev = obj->my_dev;
  1102. int i;
  1103. struct yaffs_cache *cache;
  1104. int n_caches = obj->my_dev->param.n_caches;
  1105. for (i = 0; i < n_caches; i++) {
  1106. cache = &dev->cache[i];
  1107. if (cache->object == obj && cache->dirty)
  1108. return 1;
  1109. }
  1110. return 0;
  1111. }
  1112. static void yaffs_flush_file_cache(struct yaffs_obj *obj)
  1113. {
  1114. struct yaffs_dev *dev = obj->my_dev;
  1115. int lowest = -99; /* Stop compiler whining. */
  1116. int i;
  1117. struct yaffs_cache *cache;
  1118. int chunk_written = 0;
  1119. int n_caches = obj->my_dev->param.n_caches;
  1120. if (n_caches < 1)
  1121. return;
  1122. do {
  1123. cache = NULL;
  1124. /* Find the lowest dirty chunk for this object */
  1125. for (i = 0; i < n_caches; i++) {
  1126. if (dev->cache[i].object == obj &&
  1127. dev->cache[i].dirty) {
  1128. if (!cache ||
  1129. dev->cache[i].chunk_id < lowest) {
  1130. cache = &dev->cache[i];
  1131. lowest = cache->chunk_id;
  1132. }
  1133. }
  1134. }
  1135. if (cache && !cache->locked) {
  1136. /* Write it out and free it up */
  1137. chunk_written =
  1138. yaffs_wr_data_obj(cache->object,
  1139. cache->chunk_id,
  1140. cache->data,
  1141. cache->n_bytes, 1);
  1142. cache->dirty = 0;
  1143. cache->object = NULL;
  1144. }
  1145. } while (cache && chunk_written > 0);
  1146. if (cache)
  1147. /* Hoosterman, disk full while writing cache out. */
  1148. yaffs_trace(YAFFS_TRACE_ERROR,
  1149. "yaffs tragedy: no space during cache write");
  1150. }
  1151. /*yaffs_flush_whole_cache(dev)
  1152. *
  1153. *
  1154. */
  1155. void yaffs_flush_whole_cache(struct yaffs_dev *dev)
  1156. {
  1157. struct yaffs_obj *obj;
  1158. int n_caches = dev->param.n_caches;
  1159. int i;
  1160. /* Find a dirty object in the cache and flush it...
  1161. * until there are no further dirty objects.
  1162. */
  1163. do {
  1164. obj = NULL;
  1165. for (i = 0; i < n_caches && !obj; i++) {
  1166. if (dev->cache[i].object && dev->cache[i].dirty)
  1167. obj = dev->cache[i].object;
  1168. }
  1169. if (obj)
  1170. yaffs_flush_file_cache(obj);
  1171. } while (obj);
  1172. }
  1173. /* Grab us a cache chunk for use.
  1174. * First look for an empty one.
  1175. * Then look for the least recently used non-dirty one.
  1176. * Then look for the least recently used dirty one...., flush and look again.
  1177. */
  1178. static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
  1179. {
  1180. int i;
  1181. if (dev->param.n_caches > 0) {
  1182. for (i = 0; i < dev->param.n_caches; i++) {
  1183. if (!dev->cache[i].object)
  1184. return &dev->cache[i];
  1185. }
  1186. }
  1187. return NULL;
  1188. }
  1189. static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
  1190. {
  1191. struct yaffs_cache *cache;
  1192. struct yaffs_obj *the_obj;
  1193. int usage;
  1194. int i;
  1195. if (dev->param.n_caches < 1)
  1196. return NULL;
  1197. /* Try find a non-dirty one... */
  1198. cache = yaffs_grab_chunk_worker(dev);
  1199. if (!cache) {
  1200. /* They were all dirty, find the LRU object and flush
  1201. * its cache, then find again.
  1202. * NB what's here is not very accurate,
  1203. * we actually flush the object with the LRU chunk.
  1204. */
  1205. /* With locking we can't assume we can use entry zero,
  1206. * Set the_obj to a valid pointer for Coverity. */
  1207. the_obj = dev->cache[0].object;
  1208. usage = -1;
  1209. cache = NULL;
  1210. for (i = 0; i < dev->param.n_caches; i++) {
  1211. if (dev->cache[i].object &&
  1212. !dev->cache[i].locked &&
  1213. (dev->cache[i].last_use < usage ||
  1214. !cache)) {
  1215. usage = dev->cache[i].last_use;
  1216. the_obj = dev->cache[i].object;
  1217. cache = &dev->cache[i];
  1218. }
  1219. }
  1220. if (!cache || cache->dirty) {
  1221. /* Flush and try again */
  1222. yaffs_flush_file_cache(the_obj);
  1223. cache = yaffs_grab_chunk_worker(dev);
  1224. }
  1225. }
  1226. return cache;
  1227. }
  1228. /* Find a cached chunk */
  1229. static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj,
  1230. int chunk_id)
  1231. {
  1232. struct yaffs_dev *dev = obj->my_dev;
  1233. int i;
  1234. if (dev->param.n_caches < 1)
  1235. return NULL;
  1236. for (i = 0; i < dev->param.n_caches; i++) {
  1237. if (dev->cache[i].object == obj &&
  1238. dev->cache[i].chunk_id == chunk_id) {
  1239. dev->cache_hits++;
  1240. return &dev->cache[i];
  1241. }
  1242. }
  1243. return NULL;
  1244. }
  1245. /* Mark the chunk for the least recently used algorithym */
  1246. static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
  1247. int is_write)
  1248. {
  1249. int i;
  1250. if (dev->param.n_caches < 1)
  1251. return;
  1252. if (dev->cache_last_use < 0 ||
  1253. dev->cache_last_use > 100000000) {
  1254. /* Reset the cache usages */
  1255. for (i = 1; i < dev->param.n_caches; i++)
  1256. dev->cache[i].last_use = 0;
  1257. dev->cache_last_use = 0;
  1258. }
  1259. dev->cache_last_use++;
  1260. cache->last_use = dev->cache_last_use;
  1261. if (is_write)
  1262. cache->dirty = 1;
  1263. }
  1264. /* Invalidate a single cache page.
  1265. * Do this when a whole page gets written,
  1266. * ie the short cache for this page is no longer valid.
  1267. */
  1268. static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id)
  1269. {
  1270. struct yaffs_cache *cache;
  1271. if (object->my_dev->param.n_caches > 0) {
  1272. cache = yaffs_find_chunk_cache(object, chunk_id);
  1273. if (cache)
  1274. cache->object = NULL;
  1275. }
  1276. }
  1277. /* Invalidate all the cache pages associated with this object
  1278. * Do this whenever ther file is deleted or resized.
  1279. */
  1280. static void yaffs_invalidate_whole_cache(struct yaffs_obj *in)
  1281. {
  1282. int i;
  1283. struct yaffs_dev *dev = in->my_dev;
  1284. if (dev->param.n_caches > 0) {
  1285. /* Invalidate it. */
  1286. for (i = 0; i < dev->param.n_caches; i++) {
  1287. if (dev->cache[i].object == in)
  1288. dev->cache[i].object = NULL;
  1289. }
  1290. }
  1291. }
  1292. static void yaffs_unhash_obj(struct yaffs_obj *obj)
  1293. {
  1294. int bucket;
  1295. struct yaffs_dev *dev = obj->my_dev;
  1296. /* If it is still linked into the bucket list, free from the list */
  1297. if (!list_empty(&obj->hash_link)) {
  1298. list_del_init(&obj->hash_link);
  1299. bucket = yaffs_hash_fn(obj->obj_id);
  1300. dev->obj_bucket[bucket].count--;
  1301. }
  1302. }
  1303. /* FreeObject frees up a Object and puts it back on the free list */
  1304. static void yaffs_free_obj(struct yaffs_obj *obj)
  1305. {
  1306. struct yaffs_dev *dev;
  1307. if (!obj) {
  1308. BUG();
  1309. return;
  1310. }
  1311. dev = obj->my_dev;
  1312. yaffs_trace(YAFFS_TRACE_OS, "FreeObject %p inode %p",
  1313. obj, obj->my_inode);
  1314. if (obj->parent)
  1315. BUG();
  1316. if (!list_empty(&obj->siblings))
  1317. BUG();
  1318. if (obj->my_inode) {
  1319. /* We're still hooked up to a cached inode.
  1320. * Don't delete now, but mark for later deletion
  1321. */
  1322. obj->defered_free = 1;
  1323. return;
  1324. }
  1325. yaffs_unhash_obj(obj);
  1326. yaffs_free_raw_obj(dev, obj);
  1327. dev->n_obj--;
  1328. dev->checkpoint_blocks_required = 0; /* force recalculation */
  1329. }
  1330. void yaffs_handle_defered_free(struct yaffs_obj *obj)
  1331. {
  1332. if (obj->defered_free)
  1333. yaffs_free_obj(obj);
  1334. }
  1335. static int yaffs_generic_obj_del(struct yaffs_obj *in)
  1336. {
  1337. /* Iinvalidate the file's data in the cache, without flushing. */
  1338. yaffs_invalidate_whole_cache(in);
  1339. if (in->my_dev->param.is_yaffs2 && in->parent != in->my_dev->del_dir) {
  1340. /* Move to unlinked directory so we have a deletion record */
  1341. yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0,
  1342. 0);
  1343. }
  1344. yaffs_remove_obj_from_dir(in);
  1345. yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
  1346. in->hdr_chunk = 0;
  1347. yaffs_free_obj(in);
  1348. return YAFFS_OK;
  1349. }
  1350. static void yaffs_soft_del_file(struct yaffs_obj *obj)
  1351. {
  1352. if (!obj->deleted ||
  1353. obj->variant_type != YAFFS_OBJECT_TYPE_FILE ||
  1354. obj->soft_del)
  1355. return;
  1356. if (obj->n_data_chunks <= 0) {
  1357. /* Empty file with no duplicate object headers,
  1358. * just delete it immediately */
  1359. yaffs_free_tnode(obj->my_dev, obj->variant.file_variant.top);
  1360. obj->variant.file_variant.top = NULL;
  1361. yaffs_trace(YAFFS_TRACE_TRACING,
  1362. "yaffs: Deleting empty file %d",
  1363. obj->obj_id);
  1364. yaffs_generic_obj_del(obj);
  1365. } else {
  1366. yaffs_soft_del_worker(obj,
  1367. obj->variant.file_variant.top,
  1368. obj->variant.
  1369. file_variant.top_level, 0);
  1370. obj->soft_del = 1;
  1371. }
  1372. }
  1373. /* Pruning removes any part of the file structure tree that is beyond the
  1374. * bounds of the file (ie that does not point to chunks).
  1375. *
  1376. * A file should only get pruned when its size is reduced.
  1377. *
  1378. * Before pruning, the chunks must be pulled from the tree and the
  1379. * level 0 tnode entries must be zeroed out.
  1380. * Could also use this for file deletion, but that's probably better handled
  1381. * by a special case.
  1382. *
  1383. * This function is recursive. For levels > 0 the function is called again on
  1384. * any sub-tree. For level == 0 we just check if the sub-tree has data.
  1385. * If there is no data in a subtree then it is pruned.
  1386. */
  1387. static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
  1388. struct yaffs_tnode *tn, u32 level,
  1389. int del0)
  1390. {
  1391. int i;
  1392. int has_data;
  1393. if (!tn)
  1394. return tn;
  1395. has_data = 0;
  1396. if (level > 0) {
  1397. for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
  1398. if (tn->internal[i]) {
  1399. tn->internal[i] =
  1400. yaffs_prune_worker(dev,
  1401. tn->internal[i],
  1402. level - 1,
  1403. (i == 0) ? del0 : 1);
  1404. }
  1405. if (tn->internal[i])
  1406. has_data++;
  1407. }
  1408. } else {
  1409. int tnode_size_u32 = dev->tnode_size / sizeof(u32);
  1410. u32 *map = (u32 *) tn;
  1411. for (i = 0; !has_data && i < tnode_size_u32; i++) {
  1412. if (map[i])
  1413. has_data++;
  1414. }
  1415. }
  1416. if (has_data == 0 && del0) {
  1417. /* Free and return NULL */
  1418. yaffs_free_tnode(dev, tn);
  1419. tn = NULL;
  1420. }
  1421. return tn;
  1422. }
  1423. static int yaffs_prune_tree(struct yaffs_dev *dev,
  1424. struct yaffs_file_var *file_struct)
  1425. {
  1426. int i;
  1427. int has_data;
  1428. int done = 0;
  1429. struct yaffs_tnode *tn;
  1430. if (file_struct->top_level < 1)
  1431. return YAFFS_OK;
  1432. file_struct->top =
  1433. yaffs_prune_worker(dev, file_struct->top, file_struct->top_level, 0);
  1434. /* Now we have a tree with all the non-zero branches NULL but
  1435. * the height is the same as it was.
  1436. * Let's see if we can trim internal tnodes to shorten the tree.
  1437. * We can do this if only the 0th element in the tnode is in use
  1438. * (ie all the non-zero are NULL)
  1439. */
  1440. while (file_struct->top_level && !done) {
  1441. tn = file_struct->top;
  1442. has_data = 0;
  1443. for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
  1444. if (tn->internal[i])
  1445. has_data++;
  1446. }
  1447. if (!has_data) {
  1448. file_struct->top = tn->internal[0];
  1449. file_struct->top_level--;
  1450. yaffs_free_tnode(dev, tn);
  1451. } else {
  1452. done = 1;
  1453. }
  1454. }
  1455. return YAFFS_OK;
  1456. }
  1457. /*-------------------- End of File Structure functions.-------------------*/
  1458. /* alloc_empty_obj gets us a clean Object.*/
  1459. static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
  1460. {
  1461. struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
  1462. if (!obj)
  1463. return obj;
  1464. dev->n_obj++;
  1465. /* Now sweeten it up... */
  1466. memset(obj, 0, sizeof(struct yaffs_obj));
  1467. obj->being_created = 1;
  1468. obj->my_dev = dev;
  1469. obj->hdr_chunk = 0;
  1470. obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
  1471. INIT_LIST_HEAD(&(obj->hard_links));
  1472. INIT_LIST_HEAD(&(obj->hash_link));
  1473. INIT_LIST_HEAD(&obj->siblings);
  1474. /* Now make the directory sane */
  1475. if (dev->root_dir) {
  1476. obj->parent = dev->root_dir;
  1477. list_add(&(obj->siblings),
  1478. &dev->root_dir->variant.dir_variant.children);
  1479. }
  1480. /* Add it to the lost and found directory.
  1481. * NB Can't put root or lost-n-found in lost-n-found so
  1482. * check if lost-n-found exists first
  1483. */
  1484. if (dev->lost_n_found)
  1485. yaffs_add_obj_to_dir(dev->lost_n_found, obj);
  1486. obj->being_created = 0;
  1487. dev->checkpoint_blocks_required = 0; /* force recalculation */
  1488. return obj;
  1489. }
  1490. static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
  1491. {
  1492. int i;
  1493. int l = 999;
  1494. int lowest = 999999;
  1495. /* Search for the shortest list or one that
  1496. * isn't too long.
  1497. */
  1498. for (i = 0; i < 10 && lowest > 4; i++) {
  1499. dev->bucket_finder++;
  1500. dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
  1501. if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
  1502. lowest = dev->obj_bucket[dev->bucket_finder].count;
  1503. l = dev->bucket_finder;
  1504. }
  1505. }
  1506. return l;
  1507. }
  1508. static int yaffs_new_obj_id(struct yaffs_dev *dev)
  1509. {
  1510. int bucket = yaffs_find_nice_bucket(dev);
  1511. int found = 0;
  1512. struct list_head *i;
  1513. u32 n = (u32) bucket;
  1514. /* Now find an object value that has not already been taken
  1515. * by scanning the list.
  1516. */
  1517. while (!found) {
  1518. found = 1;
  1519. n += YAFFS_NOBJECT_BUCKETS;
  1520. if (1 || dev->obj_bucket[bucket].count > 0) {
  1521. list_for_each(i, &dev->obj_bucket[bucket].list) {
  1522. /* If there is already one in the list */
  1523. if (i && list_entry(i, struct yaffs_obj,
  1524. hash_link)->obj_id == n) {
  1525. found = 0;
  1526. }
  1527. }
  1528. }
  1529. }
  1530. return n;
  1531. }
  1532. static void yaffs_hash_obj(struct yaffs_obj *in)
  1533. {
  1534. int bucket = yaffs_hash_fn(in->obj_id);
  1535. struct yaffs_dev *dev = in->my_dev;
  1536. list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
  1537. dev->obj_bucket[bucket].count++;
  1538. }
  1539. struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
  1540. {
  1541. int bucket = yaffs_hash_fn(number);
  1542. struct list_head *i;
  1543. struct yaffs_obj *in;
  1544. list_for_each(i, &dev->obj_bucket[bucket].list) {
  1545. /* Look if it is in the list */
  1546. in = list_entry(i, struct yaffs_obj, hash_link);
  1547. if (in->obj_id == number) {
  1548. /* Don't show if it is defered free */
  1549. if (in->defered_free)
  1550. return NULL;
  1551. return in;
  1552. }
  1553. }
  1554. return NULL;
  1555. }
  1556. struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
  1557. enum yaffs_obj_type type)
  1558. {
  1559. struct yaffs_obj *the_obj = NULL;
  1560. struct yaffs_tnode *tn = NULL;
  1561. if (number < 0)
  1562. number = yaffs_new_obj_id(dev);
  1563. if (type == YAFFS_OBJECT_TYPE_FILE) {
  1564. tn = yaffs_get_tnode(dev);
  1565. if (!tn)
  1566. return NULL;
  1567. }
  1568. the_obj = yaffs_alloc_empty_obj(dev);
  1569. if (!the_obj) {
  1570. if (tn)
  1571. yaffs_free_tnode(dev, tn);
  1572. return NULL;
  1573. }
  1574. the_obj->fake = 0;
  1575. the_obj->rename_allowed = 1;
  1576. the_obj->unlink_allowed = 1;
  1577. the_obj->obj_id = number;
  1578. yaffs_hash_obj(the_obj);
  1579. the_obj->variant_type = type;
  1580. yaffs_load_current_time(the_obj, 1, 1);
  1581. switch (type) {
  1582. case YAFFS_OBJECT_TYPE_FILE:
  1583. the_obj->variant.file_variant.file_size = 0;
  1584. the_obj->variant.file_variant.scanned_size = 0;
  1585. the_obj->variant.file_variant.shrink_size =
  1586. yaffs_max_file_size(dev);
  1587. the_obj->variant.file_variant.top_level = 0;
  1588. the_obj->variant.file_variant.top = tn;
  1589. break;
  1590. case YAFFS_OBJECT_TYPE_DIRECTORY:
  1591. INIT_LIST_HEAD(&the_obj->variant.dir_variant.children);
  1592. INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty);
  1593. break;
  1594. case YAFFS_OBJECT_TYPE_SYMLINK:
  1595. case YAFFS_OBJECT_TYPE_HARDLINK:
  1596. case YAFFS_OBJECT_TYPE_SPECIAL:
  1597. /* No action required */
  1598. break;
  1599. case YAFFS_OBJECT_TYPE_UNKNOWN:
  1600. /* todo this should not happen */
  1601. break;
  1602. }
  1603. return the_obj;
  1604. }
  1605. static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
  1606. int number, u32 mode)
  1607. {
  1608. struct yaffs_obj *obj =
  1609. yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
  1610. if (!obj)
  1611. return NULL;
  1612. obj->fake = 1; /* it is fake so it might not use NAND */
  1613. obj->rename_allowed = 0;
  1614. obj->unlink_allowed = 0;
  1615. obj->deleted = 0;
  1616. obj->unlinked = 0;
  1617. obj->yst_mode = mode;
  1618. obj->my_dev = dev;
  1619. obj->hdr_chunk = 0; /* Not a valid chunk. */
  1620. return obj;
  1621. }
  1622. static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
  1623. {
  1624. int i;
  1625. dev->n_obj = 0;
  1626. dev->n_tnodes = 0;
  1627. yaffs_init_raw_tnodes_and_objs(dev);
  1628. for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
  1629. INIT_LIST_HEAD(&dev->obj_bucket[i].list);
  1630. dev->obj_bucket[i].count = 0;
  1631. }
  1632. }
  1633. struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
  1634. int number,
  1635. enum yaffs_obj_type type)
  1636. {
  1637. struct yaffs_obj *the_obj = NULL;
  1638. if (number > 0)
  1639. the_obj = yaffs_find_by_number(dev, number);
  1640. if (!the_obj)
  1641. the_obj = yaffs_new_obj(dev, number, type);
  1642. return the_obj;
  1643. }
  1644. YCHAR *yaffs_clone_str(const YCHAR *str)
  1645. {
  1646. YCHAR *new_str = NULL;
  1647. int len;
  1648. if (!str)
  1649. str = _Y("");
  1650. len = yaffs_strnlen(str, YAFFS_MAX_ALIAS_LENGTH);
  1651. new_str = kmalloc((len + 1) * sizeof(YCHAR), GFP_NOFS);
  1652. if (new_str) {
  1653. yaffs_strncpy(new_str, str, len);
  1654. new_str[len] = 0;
  1655. }
  1656. return new_str;
  1657. }
  1658. /*
  1659. *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
  1660. * link (ie. name) is created or deleted in the directory.
  1661. *
  1662. * ie.
  1663. * create dir/a : update dir's mtime/ctime
  1664. * rm dir/a: update dir's mtime/ctime
  1665. * modify dir/a: don't update dir's mtimme/ctime
  1666. *
  1667. * This can be handled immediately or defered. Defering helps reduce the number
  1668. * of updates when many files in a directory are changed within a brief period.
  1669. *
  1670. * If the directory updating is defered then yaffs_update_dirty_dirs must be
  1671. * called periodically.
  1672. */
  1673. static void yaffs_update_parent(struct yaffs_obj *obj)
  1674. {
  1675. struct yaffs_dev *dev;
  1676. if (!obj)
  1677. return;
  1678. dev = obj->my_dev;
  1679. obj->dirty = 1;
  1680. yaffs_load_current_time(obj, 0, 1);
  1681. if (dev->param.defered_dir_update) {
  1682. struct list_head *link = &obj->variant.dir_variant.dirty;
  1683. if (list_empty(link)) {
  1684. list_add(link, &dev->dirty_dirs);
  1685. yaffs_trace(YAFFS_TRACE_BACKGROUND,
  1686. "Added object %d to dirty directories",
  1687. obj->obj_id);
  1688. }
  1689. } else {
  1690. yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
  1691. }
  1692. }
  1693. void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
  1694. {
  1695. struct list_head *link;
  1696. struct yaffs_obj *obj;
  1697. struct yaffs_dir_var *d_s;
  1698. union yaffs_obj_var *o_v;
  1699. yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update dirty directories");
  1700. while (!list_empty(&dev->dirty_dirs)) {
  1701. link = dev->dirty_dirs.next;
  1702. list_del_init(link);
  1703. d_s = list_entry(link, struct yaffs_dir_var, dirty);
  1704. o_v = list_entry(d_s, union yaffs_obj_var, dir_variant);
  1705. obj = list_entry(o_v, struct yaffs_obj, variant);
  1706. yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update directory %d",
  1707. obj->obj_id);
  1708. if (obj->dirty)
  1709. yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
  1710. }
  1711. }
  1712. /*
  1713. * Mknod (create) a new object.
  1714. * equiv_obj only has meaning for a hard link;
  1715. * alias_str only has meaning for a symlink.
  1716. * rdev only has meaning for devices (a subset of special objects)
  1717. */
  1718. static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
  1719. struct yaffs_obj *parent,
  1720. const YCHAR *name,
  1721. u32 mode,
  1722. u32 uid,
  1723. u32 gid,
  1724. struct yaffs_obj *equiv_obj,
  1725. const YCHAR *alias_str, u32 rdev)
  1726. {
  1727. struct yaffs_obj *in;
  1728. YCHAR *str = NULL;
  1729. struct yaffs_dev *dev = parent->my_dev;
  1730. /* Check if the entry exists.
  1731. * If it does then fail the call since we don't want a dup. */
  1732. if (yaffs_find_by_name(parent, name))
  1733. return NULL;
  1734. if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
  1735. str = yaffs_clone_str(alias_str);
  1736. if (!str)
  1737. return NULL;
  1738. }
  1739. in = yaffs_new_obj(dev, -1, type);
  1740. if (!in) {
  1741. kfree(str);
  1742. return NULL;
  1743. }
  1744. in->hdr_chunk = 0;
  1745. in->valid = 1;
  1746. in->variant_type = type;
  1747. in->yst_mode = mode;
  1748. yaffs_attribs_init(in, gid, uid, rdev);
  1749. in->n_data_chunks = 0;
  1750. yaffs_set_obj_name(in, name);
  1751. in->dirty = 1;
  1752. yaffs_add_obj_to_dir(parent, in);
  1753. in->my_dev = parent->my_dev;
  1754. switch (type) {
  1755. case YAFFS_OBJECT_TYPE_SYMLINK:
  1756. in->variant.symlink_variant.alias = str;
  1757. break;
  1758. case YAFFS_OBJECT_TYPE_HARDLINK:
  1759. in->variant.hardlink_variant.equiv_obj = equiv_obj;
  1760. in->variant.hardlink_variant.equiv_id = equiv_obj->obj_id;
  1761. list_add(&in->hard_links, &equiv_obj->hard_links);
  1762. break;
  1763. case YAFFS_OBJECT_TYPE_FILE:
  1764. case YAFFS_OBJECT_TYPE_DIRECTORY:
  1765. case YAFFS_OBJECT_TYPE_SPECIAL:
  1766. case YAFFS_OBJECT_TYPE_UNKNOWN:
  1767. /* do nothing */
  1768. break;
  1769. }
  1770. if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
  1771. /* Could not create the object header, fail */
  1772. yaffs_del_obj(in);
  1773. in = NULL;
  1774. }
  1775. if (in)
  1776. yaffs_update_parent(parent);
  1777. return in;
  1778. }
  1779. struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
  1780. const YCHAR *name, u32 mode, u32 uid,
  1781. u32 gid)
  1782. {
  1783. return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
  1784. uid, gid, NULL, NULL, 0);
  1785. }
  1786. struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR *name,
  1787. u32 mode, u32 uid, u32 gid)
  1788. {
  1789. return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
  1790. mode, uid, gid, NULL, NULL, 0);
  1791. }
  1792. struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
  1793. const YCHAR *name, u32 mode, u32 uid,
  1794. u32 gid, u32 rdev)
  1795. {
  1796. return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
  1797. uid, gid, NULL, NULL, rdev);
  1798. }
  1799. struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
  1800. const YCHAR *name, u32 mode, u32 uid,
  1801. u32 gid, const YCHAR *alias)
  1802. {
  1803. return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
  1804. uid, gid, NULL, alias, 0);
  1805. }
  1806. /* yaffs_link_obj returns the object id of the equivalent object.*/
  1807. struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
  1808. struct yaffs_obj *equiv_obj)
  1809. {
  1810. /* Get the real object in case we were fed a hard link obj */
  1811. equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
  1812. if (yaffs_create_obj(YAFFS_OBJECT_TYPE_HARDLINK,
  1813. parent, name, 0, 0, 0,
  1814. equiv_obj, NULL, 0))
  1815. return equiv_obj;
  1816. return NULL;
  1817. }
  1818. /*---------------------- Block Management and Page Allocation -------------*/
  1819. static void yaffs_deinit_blocks(struct yaffs_dev *dev)
  1820. {
  1821. if (dev->block_info_alt && dev->block_info)
  1822. vfree(dev->block_info);
  1823. else
  1824. kfree(dev->block_info);
  1825. dev->block_info_alt = 0;
  1826. dev->block_info = NULL;
  1827. if (dev->chunk_bits_alt && dev->chunk_bits)
  1828. vfree(dev->chunk_bits);
  1829. else
  1830. kfree(dev->chunk_bits);
  1831. dev->chunk_bits_alt = 0;
  1832. dev->chunk_bits = NULL;
  1833. }
  1834. static int yaffs_init_blocks(struct yaffs_dev *dev)
  1835. {
  1836. int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
  1837. dev->block_info = NULL;
  1838. dev->chunk_bits = NULL;
  1839. dev->alloc_block = -1; /* force it to get a new one */
  1840. /* If the first allocation strategy fails, thry the alternate one */
  1841. dev->block_info =
  1842. kmalloc(n_blocks * sizeof(struct yaffs_block_info), GFP_NOFS);
  1843. if (!dev->block_info) {
  1844. dev->block_info =
  1845. vmalloc(n_blocks * sizeof(struct yaffs_block_info));
  1846. dev->block_info_alt = 1;
  1847. } else {
  1848. dev->block_info_alt = 0;
  1849. }
  1850. if (!dev->block_info)
  1851. goto alloc_error;
  1852. /* Set up dynamic blockinfo stuff. Round up bytes. */
  1853. dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
  1854. dev->chunk_bits =
  1855. kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS);
  1856. if (!dev->chunk_bits) {
  1857. dev->chunk_bits =
  1858. vmalloc(dev->chunk_bit_stride * n_blocks);
  1859. dev->chunk_bits_alt = 1;
  1860. } else {
  1861. dev->chunk_bits_alt = 0;
  1862. }
  1863. if (!dev->chunk_bits)
  1864. goto alloc_error;
  1865. memset(dev->block_info, 0, n_blocks * sizeof(struct yaffs_block_info));
  1866. memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
  1867. return YAFFS_OK;
  1868. alloc_error:
  1869. yaffs_deinit_blocks(dev);
  1870. return YAFFS_FAIL;
  1871. }
  1872. void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
  1873. {
  1874. struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
  1875. int erased_ok = 0;
  1876. int i;
  1877. /* If the block is still healthy erase it and mark as clean.
  1878. * If the block has had a data failure, then retire it.
  1879. */
  1880. yaffs_trace(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
  1881. "yaffs_block_became_dirty block %d state %d %s",
  1882. block_no, bi->block_state,
  1883. (bi->needs_retiring) ? "needs retiring" : "");
  1884. yaffs2_clear_oldest_dirty_seq(dev, bi);
  1885. bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
  1886. /* If this is the block being garbage collected then stop gc'ing */
  1887. if (block_no == dev->gc_block)
  1888. dev->gc_block = 0;
  1889. /* If this block is currently the best candidate for gc
  1890. * then drop as a candidate */
  1891. if (block_no == dev->gc_dirtiest) {
  1892. dev->gc_dirtiest = 0;
  1893. dev->gc_pages_in_use = 0;
  1894. }
  1895. if (!bi->needs_retiring) {
  1896. yaffs2_checkpt_invalidate(dev);
  1897. erased_ok = yaffs_erase_block(dev, block_no);
  1898. if (!erased_ok) {
  1899. dev->n_erase_failures++;
  1900. yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  1901. "**>> Erasure failed %d", block_no);
  1902. }
  1903. }
  1904. /* Verify erasure if needed */
  1905. if (erased_ok &&
  1906. ((yaffs_trace_mask & YAFFS_TRACE_ERASE) ||
  1907. !yaffs_skip_verification(dev))) {
  1908. for (i = 0; i < dev->param.chunks_per_block; i++) {
  1909. if (!yaffs_check_chunk_erased(dev,
  1910. block_no * dev->param.chunks_per_block + i)) {
  1911. yaffs_trace(YAFFS_TRACE_ERROR,
  1912. ">>Block %d erasure supposedly OK, but chunk %d not erased",
  1913. block_no, i);
  1914. }
  1915. }
  1916. }
  1917. if (!erased_ok) {
  1918. /* We lost a block of free space */
  1919. dev->n_free_chunks -= dev->param.chunks_per_block;
  1920. yaffs_retire_block(dev, block_no);
  1921. yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
  1922. "**>> Block %d retired", block_no);
  1923. return;
  1924. }
  1925. /* Clean it up... */
  1926. bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
  1927. bi->seq_number = 0;
  1928. dev->n_erased_blocks++;
  1929. bi->pages_in_use = 0;
  1930. bi->soft_del_pages = 0;
  1931. bi->has_shrink_hdr = 0;
  1932. bi->skip_erased_check = 1; /* Clean, so no need to check */
  1933. bi->gc_prioritise = 0;
  1934. bi->has_summary = 0;
  1935. yaffs_clear_chunk_bits(dev, block_no);
  1936. yaffs_trace(YAFFS_TRACE_ERASE, "Erased block %d", block_no);
  1937. }
  1938. static inline int yaffs_gc_process_chunk(struct yaffs_dev *dev,
  1939. struct yaffs_block_info *bi,
  1940. int old_chunk, u8 *buffer)
  1941. {
  1942. int new_chunk;
  1943. int mark_flash = 1;
  1944. struct yaffs_ext_tags tags;
  1945. struct yaffs_obj *object;
  1946. int matching_chunk;
  1947. int ret_val = YAFFS_OK;
  1948. memset(&tags, 0, sizeof(tags));
  1949. yaffs_rd_chunk_tags_nand(dev, old_chunk,
  1950. buffer, &tags);
  1951. object = yaffs_find_by_number(dev, tags.obj_id);
  1952. yaffs_trace(YAFFS_TRACE_GC_DETAIL,
  1953. "Collecting chunk in block %d, %d %d %d ",
  1954. dev->gc_chunk, tags.obj_id,
  1955. tags.chunk_id, tags.n_bytes);
  1956. if (object && !yaffs_skip_verification(dev)) {
  1957. if (tags.chunk_id == 0)
  1958. matching_chunk =
  1959. object->hdr_chunk;
  1960. else if (object->soft_del)
  1961. /* Defeat the test */
  1962. matching_chunk = old_chunk;
  1963. else
  1964. matching_chunk =
  1965. yaffs_find_chunk_in_file
  1966. (object, tags.chunk_id,
  1967. NULL);
  1968. if (old_chunk != matching_chunk)
  1969. yaffs_trace(YAFFS_TRACE_ERROR,
  1970. "gc: page in gc mismatch: %d %d %d %d",
  1971. old_chunk,
  1972. matching_chunk,
  1973. tags.obj_id,
  1974. tags.chunk_id);
  1975. }
  1976. if (!object) {
  1977. yaffs_trace(YAFFS_TRACE_ERROR,
  1978. "page %d in gc has no object: %d %d %d ",
  1979. old_chunk,
  1980. tags.obj_id, tags.chunk_id,
  1981. tags.n_bytes);
  1982. }
  1983. if (object &&
  1984. object->deleted &&
  1985. object->soft_del && tags.chunk_id != 0) {
  1986. /* Data chunk in a soft deleted file,
  1987. * throw it away.
  1988. * It's a soft deleted data chunk,
  1989. * No need to copy this, just forget
  1990. * about it and fix up the object.
  1991. */
  1992. /* Free chunks already includes
  1993. * softdeleted chunks, how ever this
  1994. * chunk is going to soon be really
  1995. * deleted which will increment free
  1996. * chunks. We have to decrement free
  1997. * chunks so this works out properly.
  1998. */
  1999. dev->n_free_chunks--;
  2000. bi->soft_del_pages--;
  2001. object->n_data_chunks--;
  2002. if (object->n_data_chunks <= 0) {
  2003. /* remeber to clean up obj */
  2004. dev->gc_cleanup_list[dev->n_clean_ups] = tags.obj_id;
  2005. dev->n_clean_ups++;
  2006. }
  2007. mark_flash = 0;
  2008. } else if (object) {
  2009. /* It's either a data chunk in a live
  2010. * file or an ObjectHeader, so we're
  2011. * interested in it.
  2012. * NB Need to keep the ObjectHeaders of
  2013. * deleted files until the whole file
  2014. * has been deleted off
  2015. */
  2016. tags.serial_number++;
  2017. dev->n_gc_copies++;
  2018. if (tags.chunk_id == 0) {
  2019. /* It is an object Id,
  2020. * We need to nuke the
  2021. * shrinkheader flags since its
  2022. * work is done.
  2023. * Also need to clean up
  2024. * shadowing.
  2025. */
  2026. struct yaffs_obj_hdr *oh;
  2027. oh = (struct yaffs_obj_hdr *) buffer;
  2028. oh->is_shrink = 0;
  2029. tags.extra_is_shrink = 0;
  2030. oh->shadows_obj = 0;
  2031. oh->inband_shadowed_obj_id = 0;
  2032. tags.extra_shadows = 0;
  2033. /* Update file size */
  2034. if (object->variant_type == YAFFS_OBJECT_TYPE_FILE) {
  2035. yaffs_oh_size_load(oh,
  2036. object->variant.file_variant.file_size);
  2037. tags.extra_file_size =
  2038. object->variant.file_variant.file_size;
  2039. }
  2040. yaffs_verify_oh(object, oh, &tags, 1);
  2041. new_chunk =
  2042. yaffs_write_new_chunk(dev, (u8 *) oh, &tags, 1);
  2043. } else {
  2044. new_chunk =
  2045. yaffs_write_new_chunk(dev, buffer, &tags, 1);
  2046. }
  2047. if (new_chunk < 0) {
  2048. ret_val = YAFFS_FAIL;
  2049. } else {
  2050. /* Now fix up the Tnodes etc. */
  2051. if (tags.chunk_id == 0) {
  2052. /* It's a header */
  2053. object->hdr_chunk = new_chunk;
  2054. object->serial = tags.serial_number;
  2055. } else {
  2056. /* It's a data chunk */
  2057. yaffs_put_chunk_in_file(object, tags.chunk_id,
  2058. new_chunk, 0);
  2059. }
  2060. }
  2061. }
  2062. if (ret_val == YAFFS_OK)
  2063. yaffs_chunk_del(dev, old_chunk, mark_flash, __LINE__);
  2064. return ret_val;
  2065. }
  2066. static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
  2067. {
  2068. int old_chunk;
  2069. int ret_val = YAFFS_OK;
  2070. int i;
  2071. int is_checkpt_block;
  2072. int max_copies;
  2073. int chunks_before = yaffs_get_erased_chunks(dev);
  2074. int chunks_after;
  2075. struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
  2076. is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
  2077. yaffs_trace(YAFFS_TRACE_TRACING,
  2078. "Collecting block %d, in use %d, shrink %d, whole_block %d",
  2079. block, bi->pages_in_use, bi->has_shrink_hdr,
  2080. whole_block);
  2081. /*yaffs_verify_free_chunks(dev); */
  2082. if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
  2083. bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
  2084. bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
  2085. dev->gc_disable = 1;
  2086. yaffs_summary_gc(dev, block);
  2087. if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
  2088. yaffs_trace(YAFFS_TRACE_TRACING,
  2089. "Collecting block %d that has no chunks in use",
  2090. block);
  2091. yaffs_block_became_dirty(dev, block);
  2092. } else {
  2093. u8 *buffer = yaffs_get_temp_buffer(dev);
  2094. yaffs_verify_blk(dev, bi, block);
  2095. max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
  2096. old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
  2097. for (/* init already done */ ;
  2098. ret_val == YAFFS_OK &&
  2099. dev->gc_chunk < dev->param.chunks_per_block &&
  2100. (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
  2101. max_copies > 0;
  2102. dev->gc_chunk++, old_chunk++) {
  2103. if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
  2104. /* Page is in use and might need to be copied */
  2105. max_copies--;
  2106. ret_val = yaffs_gc_process_chunk(dev, bi,
  2107. old_chunk, buffer);
  2108. }
  2109. }
  2110. yaffs_release_temp_buffer(dev, buffer);
  2111. }
  2112. yaffs_verify_collected_blk(dev, bi, block);
  2113. if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
  2114. /*
  2115. * The gc did not complete. Set block state back to FULL
  2116. * because checkpointing does not restore gc.
  2117. */
  2118. bi->block_state = YAFFS_BLOCK_STATE_FULL;
  2119. } else {
  2120. /* The gc completed. */
  2121. /* Do any required cleanups */
  2122. for (i = 0; i < dev->n_clean_ups; i++) {
  2123. /* Time to delete the file too */
  2124. struct yaffs_obj *object =
  2125. yaffs_find_by_number(dev, dev->gc_cleanup_list[i]);
  2126. if (object) {
  2127. yaffs_free_tnode(dev,
  2128. object->variant.file_variant.top);
  2129. object->variant.file_variant.top = NULL;
  2130. yaffs_trace(YAFFS_TRACE_GC,
  2131. "yaffs: About to finally delete object %d",
  2132. object->obj_id);
  2133. yaffs_generic_obj_del(object);
  2134. object->my_dev->n_deleted_files--;
  2135. }
  2136. }
  2137. chunks_after = yaffs_get_erased_chunks(dev);
  2138. if (chunks_before >= chunks_after)
  2139. yaffs_trace(YAFFS_TRACE_GC,
  2140. "gc did not increase free chunks before %d after %d",
  2141. chunks_before, chunks_after);
  2142. dev->gc_block = 0;
  2143. dev->gc_chunk = 0;
  2144. dev->n_clean_ups = 0;
  2145. }
  2146. dev->gc_disable = 0;
  2147. return ret_val;
  2148. }
  2149. /*
  2150. * find_gc_block() selects the dirtiest block (or close enough)
  2151. * for garbage collection.
  2152. */
  2153. static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
  2154. int aggressive, int background)
  2155. {
  2156. int i;
  2157. int iterations;
  2158. unsigned selected = 0;
  2159. int prioritised = 0;
  2160. int prioritised_exist = 0;
  2161. struct yaffs_block_info *bi;
  2162. int threshold;
  2163. /* First let's see if we need to grab a prioritised block */
  2164. if (dev->has_pending_prioritised_gc && !aggressive) {
  2165. dev->gc_dirtiest = 0;
  2166. bi = dev->block_info;
  2167. for (i = dev->internal_start_block;
  2168. i <= dev->internal_end_block && !selected; i++) {
  2169. if (bi->gc_prioritise) {
  2170. prioritised_exist = 1;
  2171. if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
  2172. yaffs_block_ok_for_gc(dev, bi)) {
  2173. selected = i;
  2174. prioritised = 1;
  2175. }
  2176. }
  2177. bi++;
  2178. }
  2179. /*
  2180. * If there is a prioritised block and none was selected then
  2181. * this happened because there is at least one old dirty block
  2182. * gumming up the works. Let's gc the oldest dirty block.
  2183. */
  2184. if (prioritised_exist &&
  2185. !selected && dev->oldest_dirty_block > 0)
  2186. selected = dev->oldest_dirty_block;
  2187. if (!prioritised_exist) /* None found, so we can clear this */
  2188. dev->has_pending_prioritised_gc = 0;
  2189. }
  2190. /* If we're doing aggressive GC then we are happy to take a less-dirty
  2191. * block, and search harder.
  2192. * else (leasurely gc), then we only bother to do this if the
  2193. * block has only a few pages in use.
  2194. */
  2195. if (!selected) {
  2196. int pages_used;
  2197. int n_blocks =
  2198. dev->internal_end_block - dev->internal_start_block + 1;
  2199. if (aggressive) {
  2200. threshold = dev->param.chunks_per_block;
  2201. iterations = n_blocks;
  2202. } else {
  2203. int max_threshold;
  2204. if (background)
  2205. max_threshold = dev->param.chunks_per_block / 2;
  2206. else
  2207. max_threshold = dev->param.chunks_per_block / 8;
  2208. if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
  2209. max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
  2210. threshold = background ? (dev->gc_not_done + 2) * 2 : 0;
  2211. if (threshold < YAFFS_GC_PASSIVE_THRESHOLD)
  2212. threshold = YAFFS_GC_PASSIVE_THRESHOLD;
  2213. if (threshold > max_threshold)
  2214. threshold = max_threshold;
  2215. iterations = n_blocks / 16 + 1;
  2216. if (iterations > 100)
  2217. iterations = 100;
  2218. }
  2219. for (i = 0;
  2220. i < iterations &&
  2221. (dev->gc_dirtiest < 1 ||
  2222. dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH);
  2223. i++) {
  2224. dev->gc_block_finder++;
  2225. if (dev->gc_block_finder < dev->internal_start_block ||
  2226. dev->gc_block_finder > dev->internal_end_block)
  2227. dev->gc_block_finder =
  2228. dev->internal_start_block;
  2229. bi = yaffs_get_block_info(dev, dev->gc_block_finder);
  2230. pages_used = bi->pages_in_use - bi->soft_del_pages;
  2231. if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
  2232. pages_used < dev->param.chunks_per_block &&
  2233. (dev->gc_dirtiest < 1 ||
  2234. pages_used < dev->gc_pages_in_use) &&
  2235. yaffs_block_ok_for_gc(dev, bi)) {
  2236. dev->gc_dirtiest = dev->gc_block_finder;
  2237. dev->gc_pages_in_use = pages_used;
  2238. }
  2239. }
  2240. if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
  2241. selected = dev->gc_dirtiest;
  2242. }
  2243. /*
  2244. * If nothing has been selected for a while, try the oldest dirty
  2245. * because that's gumming up the works.
  2246. */
  2247. if (!selected && dev->param.is_yaffs2 &&
  2248. dev->gc_not_done >= (background ? 10 : 20)) {
  2249. yaffs2_find_oldest_dirty_seq(dev);
  2250. if (dev->oldest_dirty_block > 0) {
  2251. selected = dev->oldest_dirty_block;
  2252. dev->gc_dirtiest = selected;
  2253. dev->oldest_dirty_gc_count++;
  2254. bi = yaffs_get_block_info(dev, selected);
  2255. dev->gc_pages_in_use =
  2256. bi->pages_in_use - bi->soft_del_pages;
  2257. } else {
  2258. dev->gc_not_done = 0;
  2259. }
  2260. }
  2261. if (selected) {
  2262. yaffs_trace(YAFFS_TRACE_GC,
  2263. "GC Selected block %d with %d free, prioritised:%d",
  2264. selected,
  2265. dev->param.chunks_per_block - dev->gc_pages_in_use,
  2266. prioritised);
  2267. dev->n_gc_blocks++;
  2268. if (background)
  2269. dev->bg_gcs++;
  2270. dev->gc_dirtiest = 0;
  2271. dev->gc_pages_in_use = 0;
  2272. dev->gc_not_done = 0;
  2273. if (dev->refresh_skip > 0)
  2274. dev->refresh_skip--;
  2275. } else {
  2276. dev->gc_not_done++;
  2277. yaffs_trace(YAFFS_TRACE_GC,
  2278. "GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s",
  2279. dev->gc_block_finder, dev->gc_not_done, threshold,
  2280. dev->gc_dirtiest, dev->gc_pages_in_use,
  2281. dev->oldest_dirty_block, background ? " bg" : "");
  2282. }
  2283. return selected;
  2284. }
  2285. /* New garbage collector
  2286. * If we're very low on erased blocks then we do aggressive garbage collection
  2287. * otherwise we do "leasurely" garbage collection.
  2288. * Aggressive gc looks further (whole array) and will accept less dirty blocks.
  2289. * Passive gc only inspects smaller areas and only accepts more dirty blocks.
  2290. *
  2291. * The idea is to help clear out space in a more spread-out manner.
  2292. * Dunno if it really does anything useful.
  2293. */
  2294. static int yaffs_check_gc(struct yaffs_dev *dev, int background)
  2295. {
  2296. int aggressive = 0;
  2297. int gc_ok = YAFFS_OK;
  2298. int max_tries = 0;
  2299. int min_erased;
  2300. int erased_chunks;
  2301. int checkpt_block_adjust;
  2302. if (dev->param.gc_control && (dev->param.gc_control(dev) & 1) == 0)
  2303. return YAFFS_OK;
  2304. if (dev->gc_disable)
  2305. /* Bail out so we don't get recursive gc */
  2306. return YAFFS_OK;
  2307. /* This loop should pass the first time.
  2308. * Only loops here if the collection does not increase space.
  2309. */
  2310. do {
  2311. max_tries++;
  2312. checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
  2313. min_erased =
  2314. dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
  2315. erased_chunks =
  2316. dev->n_erased_blocks * dev->param.chunks_per_block;
  2317. /* If we need a block soon then do aggressive gc. */
  2318. if (dev->n_erased_blocks < min_erased)
  2319. aggressive = 1;
  2320. else {
  2321. if (!background
  2322. && erased_chunks > (dev->n_free_chunks / 4))
  2323. break;
  2324. if (dev->gc_skip > 20)
  2325. dev->gc_skip = 20;
  2326. if (erased_chunks < dev->n_free_chunks / 2 ||
  2327. dev->gc_skip < 1 || background)
  2328. aggressive = 0;
  2329. else {
  2330. dev->gc_skip--;
  2331. break;
  2332. }
  2333. }
  2334. dev->gc_skip = 5;
  2335. /* If we don't already have a block being gc'd then see if we
  2336. * should start another */
  2337. if (dev->gc_block < 1 && !aggressive) {
  2338. dev->gc_block = yaffs2_find_refresh_block(dev);
  2339. dev->gc_chunk = 0;
  2340. dev->n_clean_ups = 0;
  2341. }
  2342. if (dev->gc_block < 1) {
  2343. dev->gc_block =
  2344. yaffs_find_gc_block(dev, aggressive, background);
  2345. dev->gc_chunk = 0;
  2346. dev->n_clean_ups = 0;
  2347. }
  2348. if (dev->gc_block > 0) {
  2349. dev->all_gcs++;
  2350. if (!aggressive)
  2351. dev->passive_gc_count++;
  2352. yaffs_trace(YAFFS_TRACE_GC,
  2353. "yaffs: GC n_erased_blocks %d aggressive %d",
  2354. dev->n_erased_blocks, aggressive);
  2355. gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
  2356. }
  2357. if (dev->n_erased_blocks < (dev->param.n_reserved_blocks) &&
  2358. dev->gc_block > 0) {
  2359. yaffs_trace(YAFFS_TRACE_GC,
  2360. "yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d",
  2361. dev->n_erased_blocks, max_tries,
  2362. dev->gc_block);
  2363. }
  2364. } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
  2365. (dev->gc_block > 0) && (max_tries < 2));
  2366. return aggressive ? gc_ok : YAFFS_OK;
  2367. }
  2368. /*
  2369. * yaffs_bg_gc()
  2370. * Garbage collects. Intended to be called from a background thread.
  2371. * Returns non-zero if at least half the free chunks are erased.
  2372. */
  2373. int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
  2374. {
  2375. int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
  2376. yaffs_trace(YAFFS_TRACE_BACKGROUND, "Background gc %u", urgency);
  2377. yaffs_check_gc(dev, 1);
  2378. return erased_chunks > dev->n_free_chunks / 2;
  2379. }
  2380. /*-------------------- Data file manipulation -----------------*/
  2381. static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer)
  2382. {
  2383. int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
  2384. if (nand_chunk >= 0)
  2385. return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
  2386. buffer, NULL);
  2387. else {
  2388. yaffs_trace(YAFFS_TRACE_NANDACCESS,
  2389. "Chunk %d not found zero instead",
  2390. nand_chunk);
  2391. /* get sane (zero) data if you read a hole */
  2392. memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
  2393. return 0;
  2394. }
  2395. }
  2396. void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
  2397. int lyn)
  2398. {
  2399. int block;
  2400. int page;
  2401. struct yaffs_ext_tags tags;
  2402. struct yaffs_block_info *bi;
  2403. if (chunk_id <= 0)
  2404. return;
  2405. dev->n_deletions++;
  2406. block = chunk_id / dev->param.chunks_per_block;
  2407. page = chunk_id % dev->param.chunks_per_block;
  2408. if (!yaffs_check_chunk_bit(dev, block, page))
  2409. yaffs_trace(YAFFS_TRACE_VERIFY,
  2410. "Deleting invalid chunk %d", chunk_id);
  2411. bi = yaffs_get_block_info(dev, block);
  2412. yaffs2_update_oldest_dirty_seq(dev, block, bi);
  2413. yaffs_trace(YAFFS_TRACE_DELETION,
  2414. "line %d delete of chunk %d",
  2415. lyn, chunk_id);
  2416. if (!dev->param.is_yaffs2 && mark_flash &&
  2417. bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
  2418. memset(&tags, 0, sizeof(tags));
  2419. tags.is_deleted = 1;
  2420. yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
  2421. yaffs_handle_chunk_update(dev, chunk_id, &tags);
  2422. } else {
  2423. dev->n_unmarked_deletions++;
  2424. }
  2425. /* Pull out of the management area.
  2426. * If the whole block became dirty, this will kick off an erasure.
  2427. */
  2428. if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
  2429. bi->block_state == YAFFS_BLOCK_STATE_FULL ||
  2430. bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
  2431. bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
  2432. dev->n_free_chunks++;
  2433. yaffs_clear_chunk_bit(dev, block, page);
  2434. bi->pages_in_use--;
  2435. if (bi->pages_in_use == 0 &&
  2436. !bi->has_shrink_hdr &&
  2437. bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
  2438. bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCAN) {
  2439. yaffs_block_became_dirty(dev, block);
  2440. }
  2441. }
  2442. }
  2443. static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
  2444. const u8 *buffer, int n_bytes, int use_reserve)
  2445. {
  2446. /* Find old chunk Need to do this to get serial number
  2447. * Write new one and patch into tree.
  2448. * Invalidate old tags.
  2449. */
  2450. int prev_chunk_id;
  2451. struct yaffs_ext_tags prev_tags;
  2452. int new_chunk_id;
  2453. struct yaffs_ext_tags new_tags;
  2454. struct yaffs_dev *dev = in->my_dev;
  2455. yaffs_check_gc(dev, 0);
  2456. /* Get the previous chunk at this location in the file if it exists.
  2457. * If it does not exist then put a zero into the tree. This creates
  2458. * the tnode now, rather than later when it is harder to clean up.
  2459. */
  2460. prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
  2461. if (prev_chunk_id < 1 &&
  2462. !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
  2463. return 0;
  2464. /* Set up new tags */
  2465. memset(&new_tags, 0, sizeof(new_tags));
  2466. new_tags.chunk_id = inode_chunk;
  2467. new_tags.obj_id = in->obj_id;
  2468. new_tags.serial_number =
  2469. (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
  2470. new_tags.n_bytes = n_bytes;
  2471. if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
  2472. yaffs_trace(YAFFS_TRACE_ERROR,
  2473. "Writing %d bytes to chunk!!!!!!!!!",
  2474. n_bytes);
  2475. BUG();
  2476. }
  2477. new_chunk_id =
  2478. yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
  2479. if (new_chunk_id > 0) {
  2480. yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
  2481. if (prev_chunk_id > 0)
  2482. yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
  2483. yaffs_verify_file_sane(in);
  2484. }
  2485. return new_chunk_id;
  2486. }
  2487. static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set,
  2488. const YCHAR *name, const void *value, int size,
  2489. int flags)
  2490. {
  2491. struct yaffs_xattr_mod xmod;
  2492. int result;
  2493. xmod.set = set;
  2494. xmod.name = name;
  2495. xmod.data = value;
  2496. xmod.size = size;
  2497. xmod.flags = flags;
  2498. xmod.result = -ENOSPC;
  2499. result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
  2500. if (result > 0)
  2501. return xmod.result;
  2502. else
  2503. return -ENOSPC;
  2504. }
  2505. static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
  2506. struct yaffs_xattr_mod *xmod)
  2507. {
  2508. int retval = 0;
  2509. int x_offs = sizeof(struct yaffs_obj_hdr);
  2510. struct yaffs_dev *dev = obj->my_dev;
  2511. int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
  2512. char *x_buffer = buffer + x_offs;
  2513. if (xmod->set)
  2514. retval =
  2515. nval_set(x_buffer, x_size, xmod->name, xmod->data,
  2516. xmod->size, xmod->flags);
  2517. else
  2518. retval = nval_del(x_buffer, x_size, xmod->name);
  2519. obj->has_xattr = nval_hasvalues(x_buffer, x_size);
  2520. obj->xattr_known = 1;
  2521. xmod->result = retval;
  2522. return retval;
  2523. }
  2524. static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR *name,
  2525. void *value, int size)
  2526. {
  2527. char *buffer = NULL;
  2528. int result;
  2529. struct yaffs_ext_tags tags;
  2530. struct yaffs_dev *dev = obj->my_dev;
  2531. int x_offs = sizeof(struct yaffs_obj_hdr);
  2532. int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
  2533. char *x_buffer;
  2534. int retval = 0;
  2535. if (obj->hdr_chunk < 1)
  2536. return -ENODATA;
  2537. /* If we know that the object has no xattribs then don't do all the
  2538. * reading and parsing.
  2539. */
  2540. if (obj->xattr_known && !obj->has_xattr) {
  2541. if (name)
  2542. return -ENODATA;
  2543. else
  2544. return 0;
  2545. }
  2546. buffer = (char *)yaffs_get_temp_buffer(dev);
  2547. if (!buffer)
  2548. return -ENOMEM;
  2549. result =
  2550. yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
  2551. if (result != YAFFS_OK)
  2552. retval = -ENOENT;
  2553. else {
  2554. x_buffer = buffer + x_offs;
  2555. if (!obj->xattr_known) {
  2556. obj->has_xattr = nval_hasvalues(x_buffer, x_size);
  2557. obj->xattr_known = 1;
  2558. }
  2559. if (name)
  2560. retval = nval_get(x_buffer, x_size, name, value, size);
  2561. else
  2562. retval = nval_list(x_buffer, x_size, value, size);
  2563. }
  2564. yaffs_release_temp_buffer(dev, (u8 *) buffer);
  2565. return retval;
  2566. }
  2567. int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
  2568. const void *value, int size, int flags)
  2569. {
  2570. return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
  2571. }
  2572. int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name)
  2573. {
  2574. return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
  2575. }
  2576. int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
  2577. int size)
  2578. {
  2579. return yaffs_do_xattrib_fetch(obj, name, value, size);
  2580. }
  2581. int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
  2582. {
  2583. return yaffs_do_xattrib_fetch(obj, NULL, buffer, size);
  2584. }
  2585. static void yaffs_check_obj_details_loaded(struct yaffs_obj *in)
  2586. {
  2587. u8 *buf;
  2588. struct yaffs_obj_hdr *oh;
  2589. struct yaffs_dev *dev;
  2590. struct yaffs_ext_tags tags;
  2591. if (!in || !in->lazy_loaded || in->hdr_chunk < 1)
  2592. return;
  2593. dev = in->my_dev;
  2594. in->lazy_loaded = 0;
  2595. buf = yaffs_get_temp_buffer(dev);
  2596. yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, buf, &tags);
  2597. oh = (struct yaffs_obj_hdr *)buf;
  2598. in->yst_mode = oh->yst_mode;
  2599. yaffs_load_attribs(in, oh);
  2600. yaffs_set_obj_name_from_oh(in, oh);
  2601. if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
  2602. in->variant.symlink_variant.alias =
  2603. yaffs_clone_str(oh->alias);
  2604. }
  2605. yaffs_release_temp_buffer(dev, buf);
  2606. }
  2607. static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR *name,
  2608. const YCHAR *oh_name, int buff_size)
  2609. {
  2610. #ifdef CONFIG_YAFFS_AUTO_UNICODE
  2611. if (dev->param.auto_unicode) {
  2612. if (*oh_name) {
  2613. /* It is an ASCII name, do an ASCII to
  2614. * unicode conversion */
  2615. const char *ascii_oh_name = (const char *)oh_name;
  2616. int n = buff_size - 1;
  2617. while (n > 0 && *ascii_oh_name) {
  2618. *name = *ascii_oh_name;
  2619. name++;
  2620. ascii_oh_name++;
  2621. n--;
  2622. }
  2623. } else {
  2624. yaffs_strncpy(name, oh_name + 1, buff_size - 1);
  2625. }
  2626. } else {
  2627. #else
  2628. {
  2629. #endif
  2630. yaffs_strncpy(name, oh_name, buff_size - 1);
  2631. }
  2632. }
  2633. static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR *oh_name,
  2634. const YCHAR *name)
  2635. {
  2636. #ifdef CONFIG_YAFFS_AUTO_UNICODE
  2637. int is_ascii;
  2638. YCHAR *w;
  2639. if (dev->param.auto_unicode) {
  2640. is_ascii = 1;
  2641. w = name;
  2642. /* Figure out if the name will fit in ascii character set */
  2643. while (is_ascii && *w) {
  2644. if ((*w) & 0xff00)
  2645. is_ascii = 0;
  2646. w++;
  2647. }
  2648. if (is_ascii) {
  2649. /* It is an ASCII name, so convert unicode to ascii */
  2650. char *ascii_oh_name = (char *)oh_name;
  2651. int n = YAFFS_MAX_NAME_LENGTH - 1;
  2652. while (n > 0 && *name) {
  2653. *ascii_oh_name = *name;
  2654. name++;
  2655. ascii_oh_name++;
  2656. n--;
  2657. }
  2658. } else {
  2659. /* Unicode name, so save starting at the second YCHAR */
  2660. *oh_name = 0;
  2661. yaffs_strncpy(oh_name + 1, name, YAFFS_MAX_NAME_LENGTH - 2);
  2662. }
  2663. } else {
  2664. #else
  2665. {
  2666. #endif
  2667. yaffs_strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1);
  2668. }
  2669. }
  2670. /* UpdateObjectHeader updates the header on NAND for an object.
  2671. * If name is not NULL, then that new name is used.
  2672. */
  2673. int yaffs_update_oh(struct yaffs_obj *in, const YCHAR *name, int force,
  2674. int is_shrink, int shadows, struct yaffs_xattr_mod *xmod)
  2675. {
  2676. struct yaffs_block_info *bi;
  2677. struct yaffs_dev *dev = in->my_dev;
  2678. int prev_chunk_id;
  2679. int ret_val = 0;
  2680. int new_chunk_id;
  2681. struct yaffs_ext_tags new_tags;
  2682. struct yaffs_ext_tags old_tags;
  2683. const YCHAR *alias = NULL;
  2684. u8 *buffer = NULL;
  2685. YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
  2686. struct yaffs_obj_hdr *oh = NULL;
  2687. loff_t file_size = 0;
  2688. yaffs_strcpy(old_name, _Y("silly old name"));
  2689. if (in->fake && in != dev->root_dir && !force && !xmod)
  2690. return ret_val;
  2691. yaffs_check_gc(dev, 0);
  2692. yaffs_check_obj_details_loaded(in);
  2693. buffer = yaffs_get_temp_buffer(in->my_dev);
  2694. oh = (struct yaffs_obj_hdr *)buffer;
  2695. prev_chunk_id = in->hdr_chunk;
  2696. if (prev_chunk_id > 0) {
  2697. yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
  2698. buffer, &old_tags);
  2699. yaffs_verify_oh(in, oh, &old_tags, 0);
  2700. memcpy(old_name, oh->name, sizeof(oh->name));
  2701. memset(buffer, 0xff, sizeof(struct yaffs_obj_hdr));
  2702. } else {
  2703. memset(buffer, 0xff, dev->data_bytes_per_chunk);
  2704. }
  2705. oh->type = in->variant_type;
  2706. oh->yst_mode = in->yst_mode;
  2707. oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
  2708. yaffs_load_attribs_oh(oh, in);
  2709. if (in->parent)
  2710. oh->parent_obj_id = in->parent->obj_id;
  2711. else
  2712. oh->parent_obj_id = 0;
  2713. if (name && *name) {
  2714. memset(oh->name, 0, sizeof(oh->name));
  2715. yaffs_load_oh_from_name(dev, oh->name, name);
  2716. } else if (prev_chunk_id > 0) {
  2717. memcpy(oh->name, old_name, sizeof(oh->name));
  2718. } else {
  2719. memset(oh->name, 0, sizeof(oh->name));
  2720. }
  2721. oh->is_shrink = is_shrink;
  2722. switch (in->variant_type) {
  2723. case YAFFS_OBJECT_TYPE_UNKNOWN:
  2724. /* Should not happen */
  2725. break;
  2726. case YAFFS_OBJECT_TYPE_FILE:
  2727. if (oh->parent_obj_id != YAFFS_OBJECTID_DELETED &&
  2728. oh->parent_obj_id != YAFFS_OBJECTID_UNLINKED)
  2729. file_size = in->variant.file_variant.file_size;
  2730. yaffs_oh_size_load(oh, file_size);
  2731. break;
  2732. case YAFFS_OBJECT_TYPE_HARDLINK:
  2733. oh->equiv_id = in->variant.hardlink_variant.equiv_id;
  2734. break;
  2735. case YAFFS_OBJECT_TYPE_SPECIAL:
  2736. /* Do nothing */
  2737. break;
  2738. case YAFFS_OBJECT_TYPE_DIRECTORY:
  2739. /* Do nothing */
  2740. break;
  2741. case YAFFS_OBJECT_TYPE_SYMLINK:
  2742. alias = in->variant.symlink_variant.alias;
  2743. if (!alias)
  2744. alias = _Y("no alias");
  2745. yaffs_strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH);
  2746. oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
  2747. break;
  2748. }
  2749. /* process any xattrib modifications */
  2750. if (xmod)
  2751. yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
  2752. /* Tags */
  2753. memset(&new_tags, 0, sizeof(new_tags));
  2754. in->serial++;
  2755. new_tags.chunk_id = 0;
  2756. new_tags.obj_id = in->obj_id;
  2757. new_tags.serial_number = in->serial;
  2758. /* Add extra info for file header */
  2759. new_tags.extra_available = 1;
  2760. new_tags.extra_parent_id = oh->parent_obj_id;
  2761. new_tags.extra_file_size = file_size;
  2762. new_tags.extra_is_shrink = oh->is_shrink;
  2763. new_tags.extra_equiv_id = oh->equiv_id;
  2764. new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
  2765. new_tags.extra_obj_type = in->variant_type;
  2766. yaffs_verify_oh(in, oh, &new_tags, 1);
  2767. /* Create new chunk in NAND */
  2768. new_chunk_id =
  2769. yaffs_write_new_chunk(dev, buffer, &new_tags,
  2770. (prev_chunk_id > 0) ? 1 : 0);
  2771. if (buffer)
  2772. yaffs_release_temp_buffer(dev, buffer);
  2773. if (new_chunk_id < 0)
  2774. return new_chunk_id;
  2775. in->hdr_chunk = new_chunk_id;
  2776. if (prev_chunk_id > 0)
  2777. yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
  2778. if (!yaffs_obj_cache_dirty(in))
  2779. in->dirty = 0;
  2780. /* If this was a shrink, then mark the block
  2781. * that the chunk lives on */
  2782. if (is_shrink) {
  2783. bi = yaffs_get_block_info(in->my_dev,
  2784. new_chunk_id /
  2785. in->my_dev->param.chunks_per_block);
  2786. bi->has_shrink_hdr = 1;
  2787. }
  2788. return new_chunk_id;
  2789. }
  2790. /*--------------------- File read/write ------------------------
  2791. * Read and write have very similar structures.
  2792. * In general the read/write has three parts to it
  2793. * An incomplete chunk to start with (if the read/write is not chunk-aligned)
  2794. * Some complete chunks
  2795. * An incomplete chunk to end off with
  2796. *
  2797. * Curve-balls: the first chunk might also be the last chunk.
  2798. */
  2799. int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes)
  2800. {
  2801. int chunk;
  2802. u32 start;
  2803. int n_copy;
  2804. int n = n_bytes;
  2805. int n_done = 0;
  2806. struct yaffs_cache *cache;
  2807. struct yaffs_dev *dev;
  2808. dev = in->my_dev;
  2809. while (n > 0) {
  2810. yaffs_addr_to_chunk(dev, offset, &chunk, &start);
  2811. chunk++;
  2812. /* OK now check for the curveball where the start and end are in
  2813. * the same chunk.
  2814. */
  2815. if ((start + n) < dev->data_bytes_per_chunk)
  2816. n_copy = n;
  2817. else
  2818. n_copy = dev->data_bytes_per_chunk - start;
  2819. cache = yaffs_find_chunk_cache(in, chunk);
  2820. /* If the chunk is already in the cache or it is less than
  2821. * a whole chunk or we're using inband tags then use the cache
  2822. * (if there is caching) else bypass the cache.
  2823. */
  2824. if (cache || n_copy != dev->data_bytes_per_chunk ||
  2825. dev->param.inband_tags) {
  2826. if (dev->param.n_caches > 0) {
  2827. /* If we can't find the data in the cache,
  2828. * then load it up. */
  2829. if (!cache) {
  2830. cache =
  2831. yaffs_grab_chunk_cache(in->my_dev);
  2832. cache->object = in;
  2833. cache->chunk_id = chunk;
  2834. cache->dirty = 0;
  2835. cache->locked = 0;
  2836. yaffs_rd_data_obj(in, chunk,
  2837. cache->data);
  2838. cache->n_bytes = 0;
  2839. }
  2840. yaffs_use_cache(dev, cache, 0);
  2841. cache->locked = 1;
  2842. memcpy(buffer, &cache->data[start], n_copy);
  2843. cache->locked = 0;
  2844. } else {
  2845. /* Read into the local buffer then copy.. */
  2846. u8 *local_buffer =
  2847. yaffs_get_temp_buffer(dev);
  2848. yaffs_rd_data_obj(in, chunk, local_buffer);
  2849. memcpy(buffer, &local_buffer[start], n_copy);
  2850. yaffs_release_temp_buffer(dev, local_buffer);
  2851. }
  2852. } else {
  2853. /* A full chunk. Read directly into the buffer. */
  2854. yaffs_rd_data_obj(in, chunk, buffer);
  2855. }
  2856. n -= n_copy;
  2857. offset += n_copy;
  2858. buffer += n_copy;
  2859. n_done += n_copy;
  2860. }
  2861. return n_done;
  2862. }
  2863. int yaffs_do_file_wr(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
  2864. int n_bytes, int write_through)
  2865. {
  2866. int chunk;
  2867. u32 start;
  2868. int n_copy;
  2869. int n = n_bytes;
  2870. int n_done = 0;
  2871. int n_writeback;
  2872. loff_t start_write = offset;
  2873. int chunk_written = 0;
  2874. u32 n_bytes_read;
  2875. loff_t chunk_start;
  2876. struct yaffs_dev *dev;
  2877. dev = in->my_dev;
  2878. while (n > 0 && chunk_written >= 0) {
  2879. yaffs_addr_to_chunk(dev, offset, &chunk, &start);
  2880. if (((loff_t)chunk) *
  2881. dev->data_bytes_per_chunk + start != offset ||
  2882. start >= dev->data_bytes_per_chunk) {
  2883. yaffs_trace(YAFFS_TRACE_ERROR,
  2884. "AddrToChunk of offset %lld gives chunk %d start %d",
  2885. offset, chunk, start);
  2886. }
  2887. chunk++; /* File pos to chunk in file offset */
  2888. /* OK now check for the curveball where the start and end are in
  2889. * the same chunk.
  2890. */
  2891. if ((start + n) < dev->data_bytes_per_chunk) {
  2892. n_copy = n;
  2893. /* Now calculate how many bytes to write back....
  2894. * If we're overwriting and not writing to then end of
  2895. * file then we need to write back as much as was there
  2896. * before.
  2897. */
  2898. chunk_start = (((loff_t)(chunk - 1)) *
  2899. dev->data_bytes_per_chunk);
  2900. if (chunk_start > in->variant.file_variant.file_size)
  2901. n_bytes_read = 0; /* Past end of file */
  2902. else
  2903. n_bytes_read =
  2904. in->variant.file_variant.file_size -
  2905. chunk_start;
  2906. if (n_bytes_read > dev->data_bytes_per_chunk)
  2907. n_bytes_read = dev->data_bytes_per_chunk;
  2908. n_writeback =
  2909. (n_bytes_read >
  2910. (start + n)) ? n_bytes_read : (start + n);
  2911. if (n_writeback < 0 ||
  2912. n_writeback > dev->data_bytes_per_chunk)
  2913. BUG();
  2914. } else {
  2915. n_copy = dev->data_bytes_per_chunk - start;
  2916. n_writeback = dev->data_bytes_per_chunk;
  2917. }
  2918. if (n_copy != dev->data_bytes_per_chunk ||
  2919. dev->param.inband_tags) {
  2920. /* An incomplete start or end chunk (or maybe both
  2921. * start and end chunk), or we're using inband tags,
  2922. * so we want to use the cache buffers.
  2923. */
  2924. if (dev->param.n_caches > 0) {
  2925. struct yaffs_cache *cache;
  2926. /* If we can't find the data in the cache, then
  2927. * load the cache */
  2928. cache = yaffs_find_chunk_cache(in, chunk);
  2929. if (!cache &&
  2930. yaffs_check_alloc_available(dev, 1)) {
  2931. cache = yaffs_grab_chunk_cache(dev);
  2932. cache->object = in;
  2933. cache->chunk_id = chunk;
  2934. cache->dirty = 0;
  2935. cache->locked = 0;
  2936. yaffs_rd_data_obj(in, chunk,
  2937. cache->data);
  2938. } else if (cache &&
  2939. !cache->dirty &&
  2940. !yaffs_check_alloc_available(dev,
  2941. 1)) {
  2942. /* Drop the cache if it was a read cache
  2943. * item and no space check has been made
  2944. * for it.
  2945. */
  2946. cache = NULL;
  2947. }
  2948. if (cache) {
  2949. yaffs_use_cache(dev, cache, 1);
  2950. cache->locked = 1;
  2951. memcpy(&cache->data[start], buffer,
  2952. n_copy);
  2953. cache->locked = 0;
  2954. cache->n_bytes = n_writeback;
  2955. if (write_through) {
  2956. chunk_written =
  2957. yaffs_wr_data_obj
  2958. (cache->object,
  2959. cache->chunk_id,
  2960. cache->data,
  2961. cache->n_bytes, 1);
  2962. cache->dirty = 0;
  2963. }
  2964. } else {
  2965. chunk_written = -1; /* fail write */
  2966. }
  2967. } else {
  2968. /* An incomplete start or end chunk (or maybe
  2969. * both start and end chunk). Read into the
  2970. * local buffer then copy over and write back.
  2971. */
  2972. u8 *local_buffer = yaffs_get_temp_buffer(dev);
  2973. yaffs_rd_data_obj(in, chunk, local_buffer);
  2974. memcpy(&local_buffer[start], buffer, n_copy);
  2975. chunk_written =
  2976. yaffs_wr_data_obj(in, chunk,
  2977. local_buffer,
  2978. n_writeback, 0);
  2979. yaffs_release_temp_buffer(dev, local_buffer);
  2980. }
  2981. } else {
  2982. /* A full chunk. Write directly from the buffer. */
  2983. chunk_written =
  2984. yaffs_wr_data_obj(in, chunk, buffer,
  2985. dev->data_bytes_per_chunk, 0);
  2986. /* Since we've overwritten the cached data,
  2987. * we better invalidate it. */
  2988. yaffs_invalidate_chunk_cache(in, chunk);
  2989. }
  2990. if (chunk_written >= 0) {
  2991. n -= n_copy;
  2992. offset += n_copy;
  2993. buffer += n_copy;
  2994. n_done += n_copy;
  2995. }
  2996. }
  2997. /* Update file object */
  2998. if ((start_write + n_done) > in->variant.file_variant.file_size)
  2999. in->variant.file_variant.file_size = (start_write + n_done);
  3000. in->dirty = 1;
  3001. return n_done;
  3002. }
  3003. int yaffs_wr_file(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
  3004. int n_bytes, int write_through)
  3005. {
  3006. yaffs2_handle_hole(in, offset);
  3007. return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_through);
  3008. }
  3009. /* ---------------------- File resizing stuff ------------------ */
  3010. static void yaffs_prune_chunks(struct yaffs_obj *in, loff_t new_size)
  3011. {
  3012. struct yaffs_dev *dev = in->my_dev;
  3013. loff_t old_size = in->variant.file_variant.file_size;
  3014. int i;
  3015. int chunk_id;
  3016. u32 dummy;
  3017. int last_del;
  3018. int start_del;
  3019. if (old_size > 0)
  3020. yaffs_addr_to_chunk(dev, old_size - 1, &last_del, &dummy);
  3021. else
  3022. last_del = 0;
  3023. yaffs_addr_to_chunk(dev, new_size + dev->data_bytes_per_chunk - 1,
  3024. &start_del, &dummy);
  3025. last_del++;
  3026. start_del++;
  3027. /* Delete backwards so that we don't end up with holes if
  3028. * power is lost part-way through the operation.
  3029. */
  3030. for (i = last_del; i >= start_del; i--) {
  3031. /* NB this could be optimised somewhat,
  3032. * eg. could retrieve the tags and write them without
  3033. * using yaffs_chunk_del
  3034. */
  3035. chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
  3036. if (chunk_id < 1)
  3037. continue;
  3038. if (chunk_id <
  3039. (dev->internal_start_block * dev->param.chunks_per_block) ||
  3040. chunk_id >=
  3041. ((dev->internal_end_block + 1) *
  3042. dev->param.chunks_per_block)) {
  3043. yaffs_trace(YAFFS_TRACE_ALWAYS,
  3044. "Found daft chunk_id %d for %d",
  3045. chunk_id, i);
  3046. } else {
  3047. in->n_data_chunks--;
  3048. yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
  3049. }
  3050. }
  3051. }
  3052. void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size)
  3053. {
  3054. int new_full;
  3055. u32 new_partial;
  3056. struct yaffs_dev *dev = obj->my_dev;
  3057. yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
  3058. yaffs_prune_chunks(obj, new_size);
  3059. if (new_partial != 0) {
  3060. int last_chunk = 1 + new_full;
  3061. u8 *local_buffer = yaffs_get_temp_buffer(dev);
  3062. /* Rewrite the last chunk with its new size and zero pad */
  3063. yaffs_rd_data_obj(obj, last_chunk, local_buffer);
  3064. memset(local_buffer + new_partial, 0,
  3065. dev->data_bytes_per_chunk - new_partial);
  3066. yaffs_wr_data_obj(obj, last_chunk, local_buffer,
  3067. new_partial, 1);
  3068. yaffs_release_temp_buffer(dev, local_buffer);
  3069. }
  3070. obj->variant.file_variant.file_size = new_size;
  3071. yaffs_prune_tree(dev, &obj->variant.file_variant);
  3072. }
  3073. int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size)
  3074. {
  3075. struct yaffs_dev *dev = in->my_dev;
  3076. loff_t old_size = in->variant.file_variant.file_size;
  3077. yaffs_flush_file_cache(in);
  3078. yaffs_invalidate_whole_cache(in);
  3079. yaffs_check_gc(dev, 0);
  3080. if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
  3081. return YAFFS_FAIL;
  3082. if (new_size == old_size)
  3083. return YAFFS_OK;
  3084. if (new_size > old_size) {
  3085. yaffs2_handle_hole(in, new_size);
  3086. in->variant.file_variant.file_size = new_size;
  3087. } else {
  3088. /* new_size < old_size */
  3089. yaffs_resize_file_down(in, new_size);
  3090. }
  3091. /* Write a new object header to reflect the resize.
  3092. * show we've shrunk the file, if need be
  3093. * Do this only if the file is not in the deleted directories
  3094. * and is not shadowed.
  3095. */
  3096. if (in->parent &&
  3097. !in->is_shadowed &&
  3098. in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
  3099. in->parent->obj_id != YAFFS_OBJECTID_DELETED)
  3100. yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
  3101. return YAFFS_OK;
  3102. }
  3103. int yaffs_flush_file(struct yaffs_obj *in, int update_time, int data_sync)
  3104. {
  3105. if (!in->dirty)
  3106. return YAFFS_OK;
  3107. yaffs_flush_file_cache(in);
  3108. if (data_sync)
  3109. return YAFFS_OK;
  3110. if (update_time)
  3111. yaffs_load_current_time(in, 0, 0);
  3112. return (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >= 0) ?
  3113. YAFFS_OK : YAFFS_FAIL;
  3114. }
  3115. /* yaffs_del_file deletes the whole file data
  3116. * and the inode associated with the file.
  3117. * It does not delete the links associated with the file.
  3118. */
  3119. static int yaffs_unlink_file_if_needed(struct yaffs_obj *in)
  3120. {
  3121. int ret_val;
  3122. int del_now = 0;
  3123. struct yaffs_dev *dev = in->my_dev;
  3124. if (!in->my_inode)
  3125. del_now = 1;
  3126. if (del_now) {
  3127. ret_val =
  3128. yaffs_change_obj_name(in, in->my_dev->del_dir,
  3129. _Y("deleted"), 0, 0);
  3130. yaffs_trace(YAFFS_TRACE_TRACING,
  3131. "yaffs: immediate deletion of file %d",
  3132. in->obj_id);
  3133. in->deleted = 1;
  3134. in->my_dev->n_deleted_files++;
  3135. if (dev->param.disable_soft_del || dev->param.is_yaffs2)
  3136. yaffs_resize_file(in, 0);
  3137. yaffs_soft_del_file(in);
  3138. } else {
  3139. ret_val =
  3140. yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
  3141. _Y("unlinked"), 0, 0);
  3142. }
  3143. return ret_val;
  3144. }
  3145. int yaffs_del_file(struct yaffs_obj *in)
  3146. {
  3147. int ret_val = YAFFS_OK;
  3148. int deleted; /* Need to cache value on stack if in is freed */
  3149. struct yaffs_dev *dev = in->my_dev;
  3150. if (dev->param.disable_soft_del || dev->param.is_yaffs2)
  3151. yaffs_resize_file(in, 0);
  3152. if (in->n_data_chunks > 0) {
  3153. /* Use soft deletion if there is data in the file.
  3154. * That won't be the case if it has been resized to zero.
  3155. */
  3156. if (!in->unlinked)
  3157. ret_val = yaffs_unlink_file_if_needed(in);
  3158. deleted = in->deleted;
  3159. if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
  3160. in->deleted = 1;
  3161. deleted = 1;
  3162. in->my_dev->n_deleted_files++;
  3163. yaffs_soft_del_file(in);
  3164. }
  3165. return deleted ? YAFFS_OK : YAFFS_FAIL;
  3166. } else {
  3167. /* The file has no data chunks so we toss it immediately */
  3168. yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
  3169. in->variant.file_variant.top = NULL;
  3170. yaffs_generic_obj_del(in);
  3171. return YAFFS_OK;
  3172. }
  3173. }
  3174. int yaffs_is_non_empty_dir(struct yaffs_obj *obj)
  3175. {
  3176. return (obj &&
  3177. obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
  3178. !(list_empty(&obj->variant.dir_variant.children));
  3179. }
  3180. static int yaffs_del_dir(struct yaffs_obj *obj)
  3181. {
  3182. /* First check that the directory is empty. */
  3183. if (yaffs_is_non_empty_dir(obj))
  3184. return YAFFS_FAIL;
  3185. return yaffs_generic_obj_del(obj);
  3186. }
  3187. static int yaffs_del_symlink(struct yaffs_obj *in)
  3188. {
  3189. kfree(in->variant.symlink_variant.alias);
  3190. in->variant.symlink_variant.alias = NULL;
  3191. return yaffs_generic_obj_del(in);
  3192. }
  3193. static int yaffs_del_link(struct yaffs_obj *in)
  3194. {
  3195. /* remove this hardlink from the list associated with the equivalent
  3196. * object
  3197. */
  3198. list_del_init(&in->hard_links);
  3199. return yaffs_generic_obj_del(in);
  3200. }
  3201. int yaffs_del_obj(struct yaffs_obj *obj)
  3202. {
  3203. int ret_val = -1;
  3204. switch (obj->variant_type) {
  3205. case YAFFS_OBJECT_TYPE_FILE:
  3206. ret_val = yaffs_del_file(obj);
  3207. break;
  3208. case YAFFS_OBJECT_TYPE_DIRECTORY:
  3209. if (!list_empty(&obj->variant.dir_variant.dirty)) {
  3210. yaffs_trace(YAFFS_TRACE_BACKGROUND,
  3211. "Remove object %d from dirty directories",
  3212. obj->obj_id);
  3213. list_del_init(&obj->variant.dir_variant.dirty);
  3214. }
  3215. return yaffs_del_dir(obj);
  3216. break;
  3217. case YAFFS_OBJECT_TYPE_SYMLINK:
  3218. ret_val = yaffs_del_symlink(obj);
  3219. break;
  3220. case YAFFS_OBJECT_TYPE_HARDLINK:
  3221. ret_val = yaffs_del_link(obj);
  3222. break;
  3223. case YAFFS_OBJECT_TYPE_SPECIAL:
  3224. ret_val = yaffs_generic_obj_del(obj);
  3225. break;
  3226. case YAFFS_OBJECT_TYPE_UNKNOWN:
  3227. ret_val = 0;
  3228. break; /* should not happen. */
  3229. }
  3230. return ret_val;
  3231. }
  3232. static int yaffs_unlink_worker(struct yaffs_obj *obj)
  3233. {
  3234. int del_now = 0;
  3235. if (!obj)
  3236. return YAFFS_FAIL;
  3237. if (!obj->my_inode)
  3238. del_now = 1;
  3239. yaffs_update_parent(obj->parent);
  3240. if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
  3241. return yaffs_del_link(obj);
  3242. } else if (!list_empty(&obj->hard_links)) {
  3243. /* Curve ball: We're unlinking an object that has a hardlink.
  3244. *
  3245. * This problem arises because we are not strictly following
  3246. * The Linux link/inode model.
  3247. *
  3248. * We can't really delete the object.
  3249. * Instead, we do the following:
  3250. * - Select a hardlink.
  3251. * - Unhook it from the hard links
  3252. * - Move it from its parent directory so that the rename works.
  3253. * - Rename the object to the hardlink's name.
  3254. * - Delete the hardlink
  3255. */
  3256. struct yaffs_obj *hl;
  3257. struct yaffs_obj *parent;
  3258. int ret_val;
  3259. YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
  3260. hl = list_entry(obj->hard_links.next, struct yaffs_obj,
  3261. hard_links);
  3262. yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
  3263. parent = hl->parent;
  3264. list_del_init(&hl->hard_links);
  3265. yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
  3266. ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0);
  3267. if (ret_val == YAFFS_OK)
  3268. ret_val = yaffs_generic_obj_del(hl);
  3269. return ret_val;
  3270. } else if (del_now) {
  3271. switch (obj->variant_type) {
  3272. case YAFFS_OBJECT_TYPE_FILE:
  3273. return yaffs_del_file(obj);
  3274. break;
  3275. case YAFFS_OBJECT_TYPE_DIRECTORY:
  3276. list_del_init(&obj->variant.dir_variant.dirty);
  3277. return yaffs_del_dir(obj);
  3278. break;
  3279. case YAFFS_OBJECT_TYPE_SYMLINK:
  3280. return yaffs_del_symlink(obj);
  3281. break;
  3282. case YAFFS_OBJECT_TYPE_SPECIAL:
  3283. return yaffs_generic_obj_del(obj);
  3284. break;
  3285. case YAFFS_OBJECT_TYPE_HARDLINK:
  3286. case YAFFS_OBJECT_TYPE_UNKNOWN:
  3287. default:
  3288. return YAFFS_FAIL;
  3289. }
  3290. } else if (yaffs_is_non_empty_dir(obj)) {
  3291. return YAFFS_FAIL;
  3292. } else {
  3293. return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
  3294. _Y("unlinked"), 0, 0);
  3295. }
  3296. }
  3297. static int yaffs_unlink_obj(struct yaffs_obj *obj)
  3298. {
  3299. if (obj && obj->unlink_allowed)
  3300. return yaffs_unlink_worker(obj);
  3301. return YAFFS_FAIL;
  3302. }
  3303. int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR *name)
  3304. {
  3305. struct yaffs_obj *obj;
  3306. obj = yaffs_find_by_name(dir, name);
  3307. return yaffs_unlink_obj(obj);
  3308. }
  3309. /* Note:
  3310. * If old_name is NULL then we take old_dir as the object to be renamed.
  3311. */
  3312. int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR *old_name,
  3313. struct yaffs_obj *new_dir, const YCHAR *new_name)
  3314. {
  3315. struct yaffs_obj *obj = NULL;
  3316. struct yaffs_obj *existing_target = NULL;
  3317. int force = 0;
  3318. int result;
  3319. struct yaffs_dev *dev;
  3320. if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  3321. BUG();
  3322. return YAFFS_FAIL;
  3323. }
  3324. if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  3325. BUG();
  3326. return YAFFS_FAIL;
  3327. }
  3328. dev = old_dir->my_dev;
  3329. #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
  3330. /* Special case for case insemsitive systems.
  3331. * While look-up is case insensitive, the name isn't.
  3332. * Therefore we might want to change x.txt to X.txt
  3333. */
  3334. if (old_dir == new_dir &&
  3335. old_name && new_name &&
  3336. yaffs_strcmp(old_name, new_name) == 0)
  3337. force = 1;
  3338. #endif
  3339. if (yaffs_strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) >
  3340. YAFFS_MAX_NAME_LENGTH)
  3341. /* ENAMETOOLONG */
  3342. return YAFFS_FAIL;
  3343. if (old_name)
  3344. obj = yaffs_find_by_name(old_dir, old_name);
  3345. else{
  3346. obj = old_dir;
  3347. old_dir = obj->parent;
  3348. }
  3349. if (obj && obj->rename_allowed) {
  3350. /* Now handle an existing target, if there is one */
  3351. existing_target = yaffs_find_by_name(new_dir, new_name);
  3352. if (yaffs_is_non_empty_dir(existing_target)) {
  3353. return YAFFS_FAIL; /* ENOTEMPTY */
  3354. } else if (existing_target && existing_target != obj) {
  3355. /* Nuke the target first, using shadowing,
  3356. * but only if it isn't the same object.
  3357. *
  3358. * Note we must disable gc here otherwise it can mess
  3359. * up the shadowing.
  3360. *
  3361. */
  3362. dev->gc_disable = 1;
  3363. yaffs_change_obj_name(obj, new_dir, new_name, force,
  3364. existing_target->obj_id);
  3365. existing_target->is_shadowed = 1;
  3366. yaffs_unlink_obj(existing_target);
  3367. dev->gc_disable = 0;
  3368. }
  3369. result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
  3370. yaffs_update_parent(old_dir);
  3371. if (new_dir != old_dir)
  3372. yaffs_update_parent(new_dir);
  3373. return result;
  3374. }
  3375. return YAFFS_FAIL;
  3376. }
  3377. /*----------------------- Initialisation Scanning ---------------------- */
  3378. void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
  3379. int backward_scanning)
  3380. {
  3381. struct yaffs_obj *obj;
  3382. if (backward_scanning) {
  3383. /* Handle YAFFS2 case (backward scanning)
  3384. * If the shadowed object exists then ignore.
  3385. */
  3386. obj = yaffs_find_by_number(dev, obj_id);
  3387. if (obj)
  3388. return;
  3389. }
  3390. /* Let's create it (if it does not exist) assuming it is a file so that
  3391. * it can do shrinking etc.
  3392. * We put it in unlinked dir to be cleaned up after the scanning
  3393. */
  3394. obj =
  3395. yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE);
  3396. if (!obj)
  3397. return;
  3398. obj->is_shadowed = 1;
  3399. yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
  3400. obj->variant.file_variant.shrink_size = 0;
  3401. obj->valid = 1; /* So that we don't read any other info. */
  3402. }
  3403. void yaffs_link_fixup(struct yaffs_dev *dev, struct list_head *hard_list)
  3404. {
  3405. struct list_head *lh;
  3406. struct list_head *save;
  3407. struct yaffs_obj *hl;
  3408. struct yaffs_obj *in;
  3409. list_for_each_safe(lh, save, hard_list) {
  3410. hl = list_entry(lh, struct yaffs_obj, hard_links);
  3411. in = yaffs_find_by_number(dev,
  3412. hl->variant.hardlink_variant.equiv_id);
  3413. if (in) {
  3414. /* Add the hardlink pointers */
  3415. hl->variant.hardlink_variant.equiv_obj = in;
  3416. list_add(&hl->hard_links, &in->hard_links);
  3417. } else {
  3418. /* Todo Need to report/handle this better.
  3419. * Got a problem... hardlink to a non-existant object
  3420. */
  3421. hl->variant.hardlink_variant.equiv_obj = NULL;
  3422. INIT_LIST_HEAD(&hl->hard_links);
  3423. }
  3424. }
  3425. }
  3426. static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
  3427. {
  3428. /*
  3429. * Sort out state of unlinked and deleted objects after scanning.
  3430. */
  3431. struct list_head *i;
  3432. struct list_head *n;
  3433. struct yaffs_obj *l;
  3434. if (dev->read_only)
  3435. return;
  3436. /* Soft delete all the unlinked files */
  3437. list_for_each_safe(i, n,
  3438. &dev->unlinked_dir->variant.dir_variant.children) {
  3439. l = list_entry(i, struct yaffs_obj, siblings);
  3440. yaffs_del_obj(l);
  3441. }
  3442. list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) {
  3443. l = list_entry(i, struct yaffs_obj, siblings);
  3444. yaffs_del_obj(l);
  3445. }
  3446. }
  3447. /*
  3448. * This code iterates through all the objects making sure that they are rooted.
  3449. * Any unrooted objects are re-rooted in lost+found.
  3450. * An object needs to be in one of:
  3451. * - Directly under deleted, unlinked
  3452. * - Directly or indirectly under root.
  3453. *
  3454. * Note:
  3455. * This code assumes that we don't ever change the current relationships
  3456. * between directories:
  3457. * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
  3458. * lost-n-found->parent == root_dir
  3459. *
  3460. * This fixes the problem where directories might have inadvertently been
  3461. * deleted leaving the object "hanging" without being rooted in the
  3462. * directory tree.
  3463. */
  3464. static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
  3465. {
  3466. return (obj == dev->del_dir ||
  3467. obj == dev->unlinked_dir || obj == dev->root_dir);
  3468. }
  3469. static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
  3470. {
  3471. struct yaffs_obj *obj;
  3472. struct yaffs_obj *parent;
  3473. int i;
  3474. struct list_head *lh;
  3475. struct list_head *n;
  3476. int depth_limit;
  3477. int hanging;
  3478. if (dev->read_only)
  3479. return;
  3480. /* Iterate through the objects in each hash entry,
  3481. * looking at each object.
  3482. * Make sure it is rooted.
  3483. */
  3484. for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
  3485. list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
  3486. obj = list_entry(lh, struct yaffs_obj, hash_link);
  3487. parent = obj->parent;
  3488. if (yaffs_has_null_parent(dev, obj)) {
  3489. /* These directories are not hanging */
  3490. hanging = 0;
  3491. } else if (!parent ||
  3492. parent->variant_type !=
  3493. YAFFS_OBJECT_TYPE_DIRECTORY) {
  3494. hanging = 1;
  3495. } else if (yaffs_has_null_parent(dev, parent)) {
  3496. hanging = 0;
  3497. } else {
  3498. /*
  3499. * Need to follow the parent chain to
  3500. * see if it is hanging.
  3501. */
  3502. hanging = 0;
  3503. depth_limit = 100;
  3504. while (parent != dev->root_dir &&
  3505. parent->parent &&
  3506. parent->parent->variant_type ==
  3507. YAFFS_OBJECT_TYPE_DIRECTORY &&
  3508. depth_limit > 0) {
  3509. parent = parent->parent;
  3510. depth_limit--;
  3511. }
  3512. if (parent != dev->root_dir)
  3513. hanging = 1;
  3514. }
  3515. if (hanging) {
  3516. yaffs_trace(YAFFS_TRACE_SCAN,
  3517. "Hanging object %d moved to lost and found",
  3518. obj->obj_id);
  3519. yaffs_add_obj_to_dir(dev->lost_n_found, obj);
  3520. }
  3521. }
  3522. }
  3523. }
  3524. /*
  3525. * Delete directory contents for cleaning up lost and found.
  3526. */
  3527. static void yaffs_del_dir_contents(struct yaffs_obj *dir)
  3528. {
  3529. struct yaffs_obj *obj;
  3530. struct list_head *lh;
  3531. struct list_head *n;
  3532. if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
  3533. BUG();
  3534. list_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
  3535. obj = list_entry(lh, struct yaffs_obj, siblings);
  3536. if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
  3537. yaffs_del_dir_contents(obj);
  3538. yaffs_trace(YAFFS_TRACE_SCAN,
  3539. "Deleting lost_found object %d",
  3540. obj->obj_id);
  3541. yaffs_unlink_obj(obj);
  3542. }
  3543. }
  3544. static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
  3545. {
  3546. yaffs_del_dir_contents(dev->lost_n_found);
  3547. }
  3548. struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
  3549. const YCHAR *name)
  3550. {
  3551. int sum;
  3552. struct list_head *i;
  3553. YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
  3554. struct yaffs_obj *l;
  3555. if (!name)
  3556. return NULL;
  3557. if (!directory) {
  3558. yaffs_trace(YAFFS_TRACE_ALWAYS,
  3559. "tragedy: yaffs_find_by_name: null pointer directory"
  3560. );
  3561. BUG();
  3562. return NULL;
  3563. }
  3564. if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  3565. yaffs_trace(YAFFS_TRACE_ALWAYS,
  3566. "tragedy: yaffs_find_by_name: non-directory"
  3567. );
  3568. BUG();
  3569. }
  3570. sum = yaffs_calc_name_sum(name);
  3571. list_for_each(i, &directory->variant.dir_variant.children) {
  3572. l = list_entry(i, struct yaffs_obj, siblings);
  3573. if (l->parent != directory)
  3574. BUG();
  3575. yaffs_check_obj_details_loaded(l);
  3576. /* Special case for lost-n-found */
  3577. if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
  3578. if (!yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME))
  3579. return l;
  3580. } else if (l->sum == sum || l->hdr_chunk <= 0) {
  3581. /* LostnFound chunk called Objxxx
  3582. * Do a real check
  3583. */
  3584. yaffs_get_obj_name(l, buffer,
  3585. YAFFS_MAX_NAME_LENGTH + 1);
  3586. if (!yaffs_strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH))
  3587. return l;
  3588. }
  3589. }
  3590. return NULL;
  3591. }
  3592. /* GetEquivalentObject dereferences any hard links to get to the
  3593. * actual object.
  3594. */
  3595. struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
  3596. {
  3597. if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
  3598. obj = obj->variant.hardlink_variant.equiv_obj;
  3599. yaffs_check_obj_details_loaded(obj);
  3600. }
  3601. return obj;
  3602. }
  3603. /*
  3604. * A note or two on object names.
  3605. * * If the object name is missing, we then make one up in the form objnnn
  3606. *
  3607. * * ASCII names are stored in the object header's name field from byte zero
  3608. * * Unicode names are historically stored starting from byte zero.
  3609. *
  3610. * Then there are automatic Unicode names...
  3611. * The purpose of these is to save names in a way that can be read as
  3612. * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
  3613. * system to share files.
  3614. *
  3615. * These automatic unicode are stored slightly differently...
  3616. * - If the name can fit in the ASCII character space then they are saved as
  3617. * ascii names as per above.
  3618. * - If the name needs Unicode then the name is saved in Unicode
  3619. * starting at oh->name[1].
  3620. */
  3621. static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR *name,
  3622. int buffer_size)
  3623. {
  3624. /* Create an object name if we could not find one. */
  3625. if (yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) {
  3626. YCHAR local_name[20];
  3627. YCHAR num_string[20];
  3628. YCHAR *x = &num_string[19];
  3629. unsigned v = obj->obj_id;
  3630. num_string[19] = 0;
  3631. while (v > 0) {
  3632. x--;
  3633. *x = '0' + (v % 10);
  3634. v /= 10;
  3635. }
  3636. /* make up a name */
  3637. yaffs_strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
  3638. yaffs_strcat(local_name, x);
  3639. yaffs_strncpy(name, local_name, buffer_size - 1);
  3640. }
  3641. }
  3642. int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR *name, int buffer_size)
  3643. {
  3644. memset(name, 0, buffer_size * sizeof(YCHAR));
  3645. yaffs_check_obj_details_loaded(obj);
  3646. if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
  3647. yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
  3648. } else if (obj->short_name[0]) {
  3649. yaffs_strcpy(name, obj->short_name);
  3650. } else if (obj->hdr_chunk > 0) {
  3651. u8 *buffer = yaffs_get_temp_buffer(obj->my_dev);
  3652. struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer;
  3653. memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
  3654. if (obj->hdr_chunk > 0) {
  3655. yaffs_rd_chunk_tags_nand(obj->my_dev,
  3656. obj->hdr_chunk,
  3657. buffer, NULL);
  3658. }
  3659. yaffs_load_name_from_oh(obj->my_dev, name, oh->name,
  3660. buffer_size);
  3661. yaffs_release_temp_buffer(obj->my_dev, buffer);
  3662. }
  3663. yaffs_fix_null_name(obj, name, buffer_size);
  3664. return yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH);
  3665. }
  3666. loff_t yaffs_get_obj_length(struct yaffs_obj *obj)
  3667. {
  3668. /* Dereference any hard linking */
  3669. obj = yaffs_get_equivalent_obj(obj);
  3670. if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
  3671. return obj->variant.file_variant.file_size;
  3672. if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
  3673. if (!obj->variant.symlink_variant.alias)
  3674. return 0;
  3675. return yaffs_strnlen(obj->variant.symlink_variant.alias,
  3676. YAFFS_MAX_ALIAS_LENGTH);
  3677. } else {
  3678. /* Only a directory should drop through to here */
  3679. return obj->my_dev->data_bytes_per_chunk;
  3680. }
  3681. }
  3682. int yaffs_get_obj_link_count(struct yaffs_obj *obj)
  3683. {
  3684. int count = 0;
  3685. struct list_head *i;
  3686. if (!obj->unlinked)
  3687. count++; /* the object itself */
  3688. list_for_each(i, &obj->hard_links)
  3689. count++; /* add the hard links; */
  3690. return count;
  3691. }
  3692. int yaffs_get_obj_inode(struct yaffs_obj *obj)
  3693. {
  3694. obj = yaffs_get_equivalent_obj(obj);
  3695. return obj->obj_id;
  3696. }
  3697. unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
  3698. {
  3699. obj = yaffs_get_equivalent_obj(obj);
  3700. switch (obj->variant_type) {
  3701. case YAFFS_OBJECT_TYPE_FILE:
  3702. return DT_REG;
  3703. break;
  3704. case YAFFS_OBJECT_TYPE_DIRECTORY:
  3705. return DT_DIR;
  3706. break;
  3707. case YAFFS_OBJECT_TYPE_SYMLINK:
  3708. return DT_LNK;
  3709. break;
  3710. case YAFFS_OBJECT_TYPE_HARDLINK:
  3711. return DT_REG;
  3712. break;
  3713. case YAFFS_OBJECT_TYPE_SPECIAL:
  3714. if (S_ISFIFO(obj->yst_mode))
  3715. return DT_FIFO;
  3716. if (S_ISCHR(obj->yst_mode))
  3717. return DT_CHR;
  3718. if (S_ISBLK(obj->yst_mode))
  3719. return DT_BLK;
  3720. if (S_ISSOCK(obj->yst_mode))
  3721. return DT_SOCK;
  3722. return DT_REG;
  3723. break;
  3724. default:
  3725. return DT_REG;
  3726. break;
  3727. }
  3728. }
  3729. YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
  3730. {
  3731. obj = yaffs_get_equivalent_obj(obj);
  3732. if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
  3733. return yaffs_clone_str(obj->variant.symlink_variant.alias);
  3734. else
  3735. return yaffs_clone_str(_Y(""));
  3736. }
  3737. /*--------------------------- Initialisation code -------------------------- */
  3738. static int yaffs_check_dev_fns(const struct yaffs_dev *dev)
  3739. {
  3740. /* Common functions, gotta have */
  3741. if (!dev->param.erase_fn || !dev->param.initialise_flash_fn)
  3742. return 0;
  3743. /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
  3744. if (dev->param.write_chunk_tags_fn &&
  3745. dev->param.read_chunk_tags_fn &&
  3746. !dev->param.write_chunk_fn &&
  3747. !dev->param.read_chunk_fn &&
  3748. dev->param.bad_block_fn && dev->param.query_block_fn)
  3749. return 1;
  3750. /* Can use the "spare" style interface for yaffs1 */
  3751. if (!dev->param.is_yaffs2 &&
  3752. !dev->param.write_chunk_tags_fn &&
  3753. !dev->param.read_chunk_tags_fn &&
  3754. dev->param.write_chunk_fn &&
  3755. dev->param.read_chunk_fn &&
  3756. !dev->param.bad_block_fn && !dev->param.query_block_fn)
  3757. return 1;
  3758. return 0; /* bad */
  3759. }
  3760. static int yaffs_create_initial_dir(struct yaffs_dev *dev)
  3761. {
  3762. /* Initialise the unlinked, deleted, root and lost+found directories */
  3763. dev->lost_n_found = dev->root_dir = NULL;
  3764. dev->unlinked_dir = dev->del_dir = NULL;
  3765. dev->unlinked_dir =
  3766. yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
  3767. dev->del_dir =
  3768. yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
  3769. dev->root_dir =
  3770. yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
  3771. YAFFS_ROOT_MODE | S_IFDIR);
  3772. dev->lost_n_found =
  3773. yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
  3774. YAFFS_LOSTNFOUND_MODE | S_IFDIR);
  3775. if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir
  3776. && dev->del_dir) {
  3777. yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
  3778. return YAFFS_OK;
  3779. }
  3780. return YAFFS_FAIL;
  3781. }
  3782. int yaffs_guts_initialise(struct yaffs_dev *dev)
  3783. {
  3784. int init_failed = 0;
  3785. unsigned x;
  3786. int bits;
  3787. yaffs_trace(YAFFS_TRACE_TRACING, "yaffs: yaffs_guts_initialise()");
  3788. /* Check stuff that must be set */
  3789. if (!dev) {
  3790. yaffs_trace(YAFFS_TRACE_ALWAYS,
  3791. "yaffs: Need a device"
  3792. );
  3793. return YAFFS_FAIL;
  3794. }
  3795. if (dev->is_mounted) {
  3796. yaffs_trace(YAFFS_TRACE_ALWAYS, "device already mounted");
  3797. return YAFFS_FAIL;
  3798. }
  3799. dev->internal_start_block = dev->param.start_block;
  3800. dev->internal_end_block = dev->param.end_block;
  3801. dev->block_offset = 0;
  3802. dev->chunk_offset = 0;
  3803. dev->n_free_chunks = 0;
  3804. dev->gc_block = 0;
  3805. if (dev->param.start_block == 0) {
  3806. dev->internal_start_block = dev->param.start_block + 1;
  3807. dev->internal_end_block = dev->param.end_block + 1;
  3808. dev->block_offset = 1;
  3809. dev->chunk_offset = dev->param.chunks_per_block;
  3810. }
  3811. /* Check geometry parameters. */
  3812. if ((!dev->param.inband_tags && dev->param.is_yaffs2 &&
  3813. dev->param.total_bytes_per_chunk < 1024) ||
  3814. (!dev->param.is_yaffs2 &&
  3815. dev->param.total_bytes_per_chunk < 512) ||
  3816. (dev->param.inband_tags && !dev->param.is_yaffs2) ||
  3817. dev->param.chunks_per_block < 2 ||
  3818. dev->param.n_reserved_blocks < 2 ||
  3819. dev->internal_start_block <= 0 ||
  3820. dev->internal_end_block <= 0 ||
  3821. dev->internal_end_block <=
  3822. (dev->internal_start_block + dev->param.n_reserved_blocks + 2)
  3823. ) {
  3824. /* otherwise it is too small */
  3825. yaffs_trace(YAFFS_TRACE_ALWAYS,
  3826. "NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d ",
  3827. dev->param.total_bytes_per_chunk,
  3828. dev->param.is_yaffs2 ? "2" : "",
  3829. dev->param.inband_tags);
  3830. return YAFFS_FAIL;
  3831. }
  3832. if (yaffs_init_nand(dev) != YAFFS_OK) {
  3833. yaffs_trace(YAFFS_TRACE_ALWAYS, "InitialiseNAND failed");
  3834. return YAFFS_FAIL;
  3835. }
  3836. /* Sort out space for inband tags, if required */
  3837. if (dev->param.inband_tags)
  3838. dev->data_bytes_per_chunk =
  3839. dev->param.total_bytes_per_chunk -
  3840. sizeof(struct yaffs_packed_tags2_tags_only);
  3841. else
  3842. dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
  3843. /* Got the right mix of functions? */
  3844. if (!yaffs_check_dev_fns(dev)) {
  3845. /* Function missing */
  3846. yaffs_trace(YAFFS_TRACE_ALWAYS,
  3847. "device function(s) missing or wrong");
  3848. return YAFFS_FAIL;
  3849. }
  3850. /* Finished with most checks. Further checks happen later on too. */
  3851. dev->is_mounted = 1;
  3852. /* OK now calculate a few things for the device */
  3853. /*
  3854. * Calculate all the chunk size manipulation numbers:
  3855. */
  3856. x = dev->data_bytes_per_chunk;
  3857. /* We always use dev->chunk_shift and dev->chunk_div */
  3858. dev->chunk_shift = calc_shifts(x);
  3859. x >>= dev->chunk_shift;
  3860. dev->chunk_div = x;
  3861. /* We only use chunk mask if chunk_div is 1 */
  3862. dev->chunk_mask = (1 << dev->chunk_shift) - 1;
  3863. /*
  3864. * Calculate chunk_grp_bits.
  3865. * We need to find the next power of 2 > than internal_end_block
  3866. */
  3867. x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
  3868. bits = calc_shifts_ceiling(x);
  3869. /* Set up tnode width if wide tnodes are enabled. */
  3870. if (!dev->param.wide_tnodes_disabled) {
  3871. /* bits must be even so that we end up with 32-bit words */
  3872. if (bits & 1)
  3873. bits++;
  3874. if (bits < 16)
  3875. dev->tnode_width = 16;
  3876. else
  3877. dev->tnode_width = bits;
  3878. } else {
  3879. dev->tnode_width = 16;
  3880. }
  3881. dev->tnode_mask = (1 << dev->tnode_width) - 1;
  3882. /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
  3883. * so if the bitwidth of the
  3884. * chunk range we're using is greater than 16 we need
  3885. * to figure out chunk shift and chunk_grp_size
  3886. */
  3887. if (bits <= dev->tnode_width)
  3888. dev->chunk_grp_bits = 0;
  3889. else
  3890. dev->chunk_grp_bits = bits - dev->tnode_width;
  3891. dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8;
  3892. if (dev->tnode_size < sizeof(struct yaffs_tnode))
  3893. dev->tnode_size = sizeof(struct yaffs_tnode);
  3894. dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
  3895. if (dev->param.chunks_per_block < dev->chunk_grp_size) {
  3896. /* We have a problem because the soft delete won't work if
  3897. * the chunk group size > chunks per block.
  3898. * This can be remedied by using larger "virtual blocks".
  3899. */
  3900. yaffs_trace(YAFFS_TRACE_ALWAYS, "chunk group too large");
  3901. return YAFFS_FAIL;
  3902. }
  3903. /* Finished verifying the device, continue with initialisation */
  3904. /* More device initialisation */
  3905. dev->all_gcs = 0;
  3906. dev->passive_gc_count = 0;
  3907. dev->oldest_dirty_gc_count = 0;
  3908. dev->bg_gcs = 0;
  3909. dev->gc_block_finder = 0;
  3910. dev->buffered_block = -1;
  3911. dev->doing_buffered_block_rewrite = 0;
  3912. dev->n_deleted_files = 0;
  3913. dev->n_bg_deletions = 0;
  3914. dev->n_unlinked_files = 0;
  3915. dev->n_ecc_fixed = 0;
  3916. dev->n_ecc_unfixed = 0;
  3917. dev->n_tags_ecc_fixed = 0;
  3918. dev->n_tags_ecc_unfixed = 0;
  3919. dev->n_erase_failures = 0;
  3920. dev->n_erased_blocks = 0;
  3921. dev->gc_disable = 0;
  3922. dev->has_pending_prioritised_gc = 1;
  3923. /* Assume the worst for now, will get fixed on first GC */
  3924. INIT_LIST_HEAD(&dev->dirty_dirs);
  3925. dev->oldest_dirty_seq = 0;
  3926. dev->oldest_dirty_block = 0;
  3927. /* Initialise temporary buffers and caches. */
  3928. if (!yaffs_init_tmp_buffers(dev))
  3929. init_failed = 1;
  3930. dev->cache = NULL;
  3931. dev->gc_cleanup_list = NULL;
  3932. if (!init_failed && dev->param.n_caches > 0) {
  3933. int i;
  3934. void *buf;
  3935. int cache_bytes =
  3936. dev->param.n_caches * sizeof(struct yaffs_cache);
  3937. if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
  3938. dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
  3939. dev->cache = kmalloc(cache_bytes, GFP_NOFS);
  3940. buf = (u8 *) dev->cache;
  3941. if (dev->cache)
  3942. memset(dev->cache, 0, cache_bytes);
  3943. for (i = 0; i < dev->param.n_caches && buf; i++) {
  3944. dev->cache[i].object = NULL;
  3945. dev->cache[i].last_use = 0;
  3946. dev->cache[i].dirty = 0;
  3947. dev->cache[i].data = buf =
  3948. kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
  3949. }
  3950. if (!buf)
  3951. init_failed = 1;
  3952. dev->cache_last_use = 0;
  3953. }
  3954. dev->cache_hits = 0;
  3955. if (!init_failed) {
  3956. dev->gc_cleanup_list =
  3957. kmalloc(dev->param.chunks_per_block * sizeof(u32),
  3958. GFP_NOFS);
  3959. if (!dev->gc_cleanup_list)
  3960. init_failed = 1;
  3961. }
  3962. if (dev->param.is_yaffs2)
  3963. dev->param.use_header_file_size = 1;
  3964. if (!init_failed && !yaffs_init_blocks(dev))
  3965. init_failed = 1;
  3966. yaffs_init_tnodes_and_objs(dev);
  3967. if (!init_failed && !yaffs_create_initial_dir(dev))
  3968. init_failed = 1;
  3969. if (!init_failed && dev->param.is_yaffs2 &&
  3970. !dev->param.disable_summary &&
  3971. !yaffs_summary_init(dev))
  3972. init_failed = 1;
  3973. if (!init_failed) {
  3974. /* Now scan the flash. */
  3975. if (dev->param.is_yaffs2) {
  3976. if (yaffs2_checkpt_restore(dev)) {
  3977. yaffs_check_obj_details_loaded(dev->root_dir);
  3978. yaffs_trace(YAFFS_TRACE_CHECKPOINT |
  3979. YAFFS_TRACE_MOUNT,
  3980. "yaffs: restored from checkpoint"
  3981. );
  3982. } else {
  3983. /* Clean up the mess caused by an aborted
  3984. * checkpoint load then scan backwards.
  3985. */
  3986. yaffs_deinit_blocks(dev);
  3987. yaffs_deinit_tnodes_and_objs(dev);
  3988. dev->n_erased_blocks = 0;
  3989. dev->n_free_chunks = 0;
  3990. dev->alloc_block = -1;
  3991. dev->alloc_page = -1;
  3992. dev->n_deleted_files = 0;
  3993. dev->n_unlinked_files = 0;
  3994. dev->n_bg_deletions = 0;
  3995. if (!init_failed && !yaffs_init_blocks(dev))
  3996. init_failed = 1;
  3997. yaffs_init_tnodes_and_objs(dev);
  3998. if (!init_failed
  3999. && !yaffs_create_initial_dir(dev))
  4000. init_failed = 1;
  4001. if (!init_failed && !yaffs2_scan_backwards(dev))
  4002. init_failed = 1;
  4003. }
  4004. } else if (!yaffs1_scan(dev)) {
  4005. init_failed = 1;
  4006. }
  4007. yaffs_strip_deleted_objs(dev);
  4008. yaffs_fix_hanging_objs(dev);
  4009. if (dev->param.empty_lost_n_found)
  4010. yaffs_empty_l_n_f(dev);
  4011. }
  4012. if (init_failed) {
  4013. /* Clean up the mess */
  4014. yaffs_trace(YAFFS_TRACE_TRACING,
  4015. "yaffs: yaffs_guts_initialise() aborted.");
  4016. yaffs_deinitialise(dev);
  4017. return YAFFS_FAIL;
  4018. }
  4019. /* Zero out stats */
  4020. dev->n_page_reads = 0;
  4021. dev->n_page_writes = 0;
  4022. dev->n_erasures = 0;
  4023. dev->n_gc_copies = 0;
  4024. dev->n_retried_writes = 0;
  4025. dev->n_retired_blocks = 0;
  4026. yaffs_verify_free_chunks(dev);
  4027. yaffs_verify_blocks(dev);
  4028. /* Clean up any aborted checkpoint data */
  4029. if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
  4030. yaffs2_checkpt_invalidate(dev);
  4031. yaffs_trace(YAFFS_TRACE_TRACING,
  4032. "yaffs: yaffs_guts_initialise() done.");
  4033. return YAFFS_OK;
  4034. }
  4035. void yaffs_deinitialise(struct yaffs_dev *dev)
  4036. {
  4037. if (dev->is_mounted) {
  4038. int i;
  4039. yaffs_deinit_blocks(dev);
  4040. yaffs_deinit_tnodes_and_objs(dev);
  4041. yaffs_summary_deinit(dev);
  4042. if (dev->param.n_caches > 0 && dev->cache) {
  4043. for (i = 0; i < dev->param.n_caches; i++) {
  4044. kfree(dev->cache[i].data);
  4045. dev->cache[i].data = NULL;
  4046. }
  4047. kfree(dev->cache);
  4048. dev->cache = NULL;
  4049. }
  4050. kfree(dev->gc_cleanup_list);
  4051. for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
  4052. kfree(dev->temp_buffer[i].buffer);
  4053. dev->is_mounted = 0;
  4054. if (dev->param.deinitialise_flash_fn)
  4055. dev->param.deinitialise_flash_fn(dev);
  4056. }
  4057. }
  4058. int yaffs_count_free_chunks(struct yaffs_dev *dev)
  4059. {
  4060. int n_free = 0;
  4061. int b;
  4062. struct yaffs_block_info *blk;
  4063. blk = dev->block_info;
  4064. for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
  4065. switch (blk->block_state) {
  4066. case YAFFS_BLOCK_STATE_EMPTY:
  4067. case YAFFS_BLOCK_STATE_ALLOCATING:
  4068. case YAFFS_BLOCK_STATE_COLLECTING:
  4069. case YAFFS_BLOCK_STATE_FULL:
  4070. n_free +=
  4071. (dev->param.chunks_per_block - blk->pages_in_use +
  4072. blk->soft_del_pages);
  4073. break;
  4074. default:
  4075. break;
  4076. }
  4077. blk++;
  4078. }
  4079. return n_free;
  4080. }
  4081. int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
  4082. {
  4083. /* This is what we report to the outside world */
  4084. int n_free;
  4085. int n_dirty_caches;
  4086. int blocks_for_checkpt;
  4087. int i;
  4088. n_free = dev->n_free_chunks;
  4089. n_free += dev->n_deleted_files;
  4090. /* Now count and subtract the number of dirty chunks in the cache. */
  4091. for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) {
  4092. if (dev->cache[i].dirty)
  4093. n_dirty_caches++;
  4094. }
  4095. n_free -= n_dirty_caches;
  4096. n_free -=
  4097. ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
  4098. /* Now figure checkpoint space and report that... */
  4099. blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
  4100. n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);
  4101. if (n_free < 0)
  4102. n_free = 0;
  4103. return n_free;
  4104. }
  4105. /*\
  4106. * Marshalling functions to get loff_t file sizes into aand out of
  4107. * object headers.
  4108. */
  4109. void yaffs_oh_size_load(struct yaffs_obj_hdr *oh, loff_t fsize)
  4110. {
  4111. oh->file_size_low = (fsize & 0xFFFFFFFF);
  4112. oh->file_size_high = ((fsize >> 32) & 0xFFFFFFFF);
  4113. }
  4114. loff_t yaffs_oh_to_size(struct yaffs_obj_hdr *oh)
  4115. {
  4116. loff_t retval;
  4117. if (~(oh->file_size_high))
  4118. retval = (((loff_t) oh->file_size_high) << 32) |
  4119. (((loff_t) oh->file_size_low) & 0xFFFFFFFF);
  4120. else
  4121. retval = (loff_t) oh->file_size_low;
  4122. return retval;
  4123. }