yaffs_guts.c 123 KB

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