yaffs_guts.c 127 KB

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